| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright 1998, 2000 Marshall Kirk McKusick. All Rights Reserved. | |
| 3 | * | |
| 4 | * The soft updates code is derived from the appendix of a University | |
| 5 | * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, | |
| 6 | * "Soft Updates: A Solution to the Metadata Update Problem in File | |
| 7 | * Systems", CSE-TR-254-95, August 1995). | |
| 8 | * | |
| 9 | * Further information about soft updates can be obtained from: | |
| 10 | * | |
| 11 | * Marshall Kirk McKusick http://www.mckusick.com/softdep/ | |
| 12 | * 1614 Oxford Street mckusick@mckusick.com | |
| 13 | * Berkeley, CA 94709-1608 +1-510-843-9542 | |
| 14 | * USA | |
| 15 | * | |
| 16 | * Redistribution and use in source and binary forms, with or without | |
| 17 | * modification, are permitted provided that the following conditions | |
| 18 | * are met: | |
| 19 | * | |
| 20 | * 1. Redistributions of source code must retain the above copyright | |
| 21 | * notice, this list of conditions and the following disclaimer. | |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 23 | * notice, this list of conditions and the following disclaimer in the | |
| 24 | * documentation and/or other materials provided with the distribution. | |
| 25 | * | |
| 26 | * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY | |
| 27 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 29 | * DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR | |
| 30 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 36 | * SUCH DAMAGE. | |
| 37 | * | |
| 38 | * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 | |
| 39 | * $FreeBSD: src/sys/ufs/ffs/ffs_softdep.c,v 1.57.2.11 2002/02/05 18:46:53 dillon Exp $ | |
| c4df9635 | 40 | * $DragonFly: src/sys/vfs/ufs/ffs_softdep.c,v 1.57 2008/06/28 17:59:51 dillon Exp $ |
| 984263bc MD |
41 | */ |
| 42 | ||
| 43 | /* | |
| 44 | * For now we want the safety net that the DIAGNOSTIC and DEBUG flags provide. | |
| 45 | */ | |
| 46 | #ifndef DIAGNOSTIC | |
| 47 | #define DIAGNOSTIC | |
| 48 | #endif | |
| 49 | #ifndef DEBUG | |
| 50 | #define DEBUG | |
| 51 | #endif | |
| 52 | ||
| 53 | #include <sys/param.h> | |
| 54 | #include <sys/kernel.h> | |
| 55 | #include <sys/systm.h> | |
| 56 | #include <sys/buf.h> | |
| 57 | #include <sys/malloc.h> | |
| 58 | #include <sys/mount.h> | |
| 59 | #include <sys/proc.h> | |
| 60 | #include <sys/syslog.h> | |
| 61 | #include <sys/vnode.h> | |
| 62 | #include <sys/conf.h> | |
| 3020e3be | 63 | #include <sys/buf2.h> |
| f91a71dd | 64 | #include <machine/inttypes.h> |
| 1f2de5d4 MD |
65 | #include "dir.h" |
| 66 | #include "quota.h" | |
| 67 | #include "inode.h" | |
| 68 | #include "ufsmount.h" | |
| 69 | #include "fs.h" | |
| 70 | #include "softdep.h" | |
| 71 | #include "ffs_extern.h" | |
| 72 | #include "ufs_extern.h" | |
| 984263bc | 73 | |
| 165dba55 DR |
74 | #include <sys/thread2.h> |
| 75 | ||
| 984263bc MD |
76 | /* |
| 77 | * These definitions need to be adapted to the system to which | |
| 78 | * this file is being ported. | |
| 79 | */ | |
| 80 | /* | |
| 81 | * malloc types defined for the softdep system. | |
| 82 | */ | |
| 83 | MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies"); | |
| 84 | MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies"); | |
| 85 | MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation"); | |
| 86 | MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map"); | |
| 87 | MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode"); | |
| 88 | MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies"); | |
| 89 | MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block"); | |
| 90 | MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode"); | |
| 91 | MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode"); | |
| 92 | MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated"); | |
| 93 | MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry"); | |
| 94 | MALLOC_DEFINE(M_MKDIR, "mkdir","New directory"); | |
| 95 | MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted"); | |
| 96 | ||
| 97 | #define M_SOFTDEP_FLAGS (M_WAITOK | M_USE_RESERVE) | |
| 98 | ||
| 99 | #define D_PAGEDEP 0 | |
| 100 | #define D_INODEDEP 1 | |
| 101 | #define D_NEWBLK 2 | |
| 102 | #define D_BMSAFEMAP 3 | |
| 103 | #define D_ALLOCDIRECT 4 | |
| 104 | #define D_INDIRDEP 5 | |
| 105 | #define D_ALLOCINDIR 6 | |
| 106 | #define D_FREEFRAG 7 | |
| 107 | #define D_FREEBLKS 8 | |
| 108 | #define D_FREEFILE 9 | |
| 109 | #define D_DIRADD 10 | |
| 110 | #define D_MKDIR 11 | |
| 111 | #define D_DIRREM 12 | |
| 112 | #define D_LAST D_DIRREM | |
| 113 | ||
| 114 | /* | |
| 115 | * translate from workitem type to memory type | |
| 116 | * MUST match the defines above, such that memtype[D_XXX] == M_XXX | |
| 117 | */ | |
| 118 | static struct malloc_type *memtype[] = { | |
| 119 | M_PAGEDEP, | |
| 120 | M_INODEDEP, | |
| 121 | M_NEWBLK, | |
| 122 | M_BMSAFEMAP, | |
| 123 | M_ALLOCDIRECT, | |
| 124 | M_INDIRDEP, | |
| 125 | M_ALLOCINDIR, | |
| 126 | M_FREEFRAG, | |
| 127 | M_FREEBLKS, | |
| 128 | M_FREEFILE, | |
| 129 | M_DIRADD, | |
| 130 | M_MKDIR, | |
| 131 | M_DIRREM | |
| 132 | }; | |
| 133 | ||
| 134 | #define DtoM(type) (memtype[type]) | |
| 135 | ||
| 136 | /* | |
| 137 | * Names of malloc types. | |
| 138 | */ | |
| 139 | #define TYPENAME(type) \ | |
| 140 | ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???") | |
| 984263bc MD |
141 | /* |
| 142 | * End system adaptaion definitions. | |
| 143 | */ | |
| 144 | ||
| 145 | /* | |
| 146 | * Internal function prototypes. | |
| 147 | */ | |
| f719c866 DR |
148 | static void softdep_error(char *, int); |
| 149 | static void drain_output(struct vnode *, int); | |
| 150 | static int getdirtybuf(struct buf **, int); | |
| 151 | static void clear_remove(struct thread *); | |
| 152 | static void clear_inodedeps(struct thread *); | |
| 153 | static int flush_pagedep_deps(struct vnode *, struct mount *, | |
| a6ee311a | 154 | struct diraddhd *); |
| f719c866 DR |
155 | static int flush_inodedep_deps(struct fs *, ino_t); |
| 156 | static int handle_written_filepage(struct pagedep *, struct buf *); | |
| 157 | static void diradd_inode_written(struct diradd *, struct inodedep *); | |
| 158 | static int handle_written_inodeblock(struct inodedep *, struct buf *); | |
| 159 | static void handle_allocdirect_partdone(struct allocdirect *); | |
| 160 | static void handle_allocindir_partdone(struct allocindir *); | |
| 161 | static void initiate_write_filepage(struct pagedep *, struct buf *); | |
| 162 | static void handle_written_mkdir(struct mkdir *, int); | |
| 163 | static void initiate_write_inodeblock(struct inodedep *, struct buf *); | |
| 164 | static void handle_workitem_freefile(struct freefile *); | |
| 165 | static void handle_workitem_remove(struct dirrem *); | |
| 166 | static struct dirrem *newdirrem(struct buf *, struct inode *, | |
| a6ee311a | 167 | struct inode *, int, struct dirrem **); |
| f719c866 DR |
168 | static void free_diradd(struct diradd *); |
| 169 | static void free_allocindir(struct allocindir *, struct inodedep *); | |
| 54078292 | 170 | static int indir_trunc (struct inode *, off_t, int, ufs_lbn_t, long *); |
| f719c866 DR |
171 | static void deallocate_dependencies(struct buf *, struct inodedep *); |
| 172 | static void free_allocdirect(struct allocdirectlst *, | |
| a6ee311a | 173 | struct allocdirect *, int); |
| f719c866 DR |
174 | static int check_inode_unwritten(struct inodedep *); |
| 175 | static int free_inodedep(struct inodedep *); | |
| 176 | static void handle_workitem_freeblocks(struct freeblks *); | |
| 177 | static void merge_inode_lists(struct inodedep *); | |
| 178 | static void setup_allocindir_phase2(struct buf *, struct inode *, | |
| a6ee311a | 179 | struct allocindir *); |
| f719c866 | 180 | static struct allocindir *newallocindir(struct inode *, int, ufs_daddr_t, |
| a6ee311a | 181 | ufs_daddr_t); |
| f719c866 DR |
182 | static void handle_workitem_freefrag(struct freefrag *); |
| 183 | static struct freefrag *newfreefrag(struct inode *, ufs_daddr_t, long); | |
| 184 | static void allocdirect_merge(struct allocdirectlst *, | |
| a6ee311a | 185 | struct allocdirect *, struct allocdirect *); |
| f719c866 DR |
186 | static struct bmsafemap *bmsafemap_lookup(struct buf *); |
| 187 | static int newblk_lookup(struct fs *, ufs_daddr_t, int, | |
| a6ee311a | 188 | struct newblk **); |
| f719c866 DR |
189 | static int inodedep_lookup(struct fs *, ino_t, int, struct inodedep **); |
| 190 | static int pagedep_lookup(struct inode *, ufs_lbn_t, int, | |
| a6ee311a | 191 | struct pagedep **); |
| f719c866 DR |
192 | static void pause_timer(void *); |
| 193 | static int request_cleanup(int, int); | |
| 194 | static int process_worklist_item(struct mount *, int); | |
| 195 | static void add_to_worklist(struct worklist *); | |
| 984263bc MD |
196 | |
| 197 | /* | |
| 198 | * Exported softdep operations. | |
| 199 | */ | |
| f719c866 DR |
200 | static void softdep_disk_io_initiation(struct buf *); |
| 201 | static void softdep_disk_write_complete(struct buf *); | |
| 202 | static void softdep_deallocate_dependencies(struct buf *); | |
| 203 | static int softdep_fsync(struct vnode *); | |
| 204 | static int softdep_process_worklist(struct mount *); | |
| 205 | static void softdep_move_dependencies(struct buf *, struct buf *); | |
| 206 | static int softdep_count_dependencies(struct buf *bp, int); | |
| 27bc0cb1 MD |
207 | static int softdep_checkread(struct buf *bp); |
| 208 | static int softdep_checkwrite(struct buf *bp); | |
| 984263bc | 209 | |
| bc50d880 | 210 | static struct bio_ops softdep_bioops = { |
| 408357d8 MD |
211 | .io_start = softdep_disk_io_initiation, |
| 212 | .io_complete = softdep_disk_write_complete, | |
| 213 | .io_deallocate = softdep_deallocate_dependencies, | |
| 214 | .io_fsync = softdep_fsync, | |
| 215 | .io_sync = softdep_process_worklist, | |
| 216 | .io_movedeps = softdep_move_dependencies, | |
| 217 | .io_countdeps = softdep_count_dependencies, | |
| 27bc0cb1 MD |
218 | .io_checkread = softdep_checkread, |
| 219 | .io_checkwrite = softdep_checkwrite | |
| 984263bc MD |
220 | }; |
| 221 | ||
| 222 | /* | |
| 223 | * Locking primitives. | |
| 224 | * | |
| 225 | * For a uniprocessor, all we need to do is protect against disk | |
| 226 | * interrupts. For a multiprocessor, this lock would have to be | |
| 227 | * a mutex. A single mutex is used throughout this file, though | |
| 228 | * finer grain locking could be used if contention warranted it. | |
| 229 | * | |
| 230 | * For a multiprocessor, the sleep call would accept a lock and | |
| 231 | * release it after the sleep processing was complete. In a uniprocessor | |
| 232 | * implementation there is no such interlock, so we simple mark | |
| 233 | * the places where it needs to be done with the `interlocked' form | |
| 234 | * of the lock calls. Since the uniprocessor sleep already interlocks | |
| 235 | * the spl, there is nothing that really needs to be done. | |
| 236 | */ | |
| 237 | #ifndef /* NOT */ DEBUG | |
| 238 | static struct lockit { | |
| 984263bc | 239 | } lk = { 0 }; |
| 02d8a449 MD |
240 | #define ACQUIRE_LOCK(lk) crit_enter_id("softupdates"); |
| 241 | #define FREE_LOCK(lk) crit_exit_id("softupdates"); | |
| 984263bc MD |
242 | |
| 243 | #else /* DEBUG */ | |
| dadab5e9 MD |
244 | #define NOHOLDER ((struct thread *)-1) |
| 245 | #define SPECIAL_FLAG ((struct thread *)-2) | |
| 984263bc MD |
246 | static struct lockit { |
| 247 | int lkt_spl; | |
| dadab5e9 MD |
248 | struct thread *lkt_held; |
| 249 | } lk = { 0, NOHOLDER }; | |
| 984263bc MD |
250 | static int lockcnt; |
| 251 | ||
| f719c866 DR |
252 | static void acquire_lock(struct lockit *); |
| 253 | static void free_lock(struct lockit *); | |
| 254 | void softdep_panic(char *); | |
| 984263bc MD |
255 | |
| 256 | #define ACQUIRE_LOCK(lk) acquire_lock(lk) | |
| 257 | #define FREE_LOCK(lk) free_lock(lk) | |
| 258 | ||
| 259 | static void | |
| 3fcb1ab8 | 260 | acquire_lock(struct lockit *lk) |
| 984263bc | 261 | { |
| dadab5e9 | 262 | thread_t holder; |
| 984263bc | 263 | |
| dadab5e9 | 264 | if (lk->lkt_held != NOHOLDER) { |
| 984263bc MD |
265 | holder = lk->lkt_held; |
| 266 | FREE_LOCK(lk); | |
| dadab5e9 | 267 | if (holder == curthread) |
| 984263bc MD |
268 | panic("softdep_lock: locking against myself"); |
| 269 | else | |
| dadab5e9 | 270 | panic("softdep_lock: lock held by %p", holder); |
| 984263bc | 271 | } |
| 02d8a449 | 272 | crit_enter_id("softupdates"); |
| dadab5e9 | 273 | lk->lkt_held = curthread; |
| 984263bc MD |
274 | lockcnt++; |
| 275 | } | |
| 276 | ||
| 277 | static void | |
| 3fcb1ab8 | 278 | free_lock(struct lockit *lk) |
| 984263bc MD |
279 | { |
| 280 | ||
| dadab5e9 | 281 | if (lk->lkt_held == NOHOLDER) |
| 984263bc | 282 | panic("softdep_unlock: lock not held"); |
| dadab5e9 | 283 | lk->lkt_held = NOHOLDER; |
| 02d8a449 | 284 | crit_exit_id("softupdates"); |
| 984263bc MD |
285 | } |
| 286 | ||
| 287 | /* | |
| 288 | * Function to release soft updates lock and panic. | |
| 289 | */ | |
| 290 | void | |
| 3fcb1ab8 | 291 | softdep_panic(char *msg) |
| 984263bc MD |
292 | { |
| 293 | ||
| dadab5e9 | 294 | if (lk.lkt_held != NOHOLDER) |
| 984263bc MD |
295 | FREE_LOCK(&lk); |
| 296 | panic(msg); | |
| 297 | } | |
| 298 | #endif /* DEBUG */ | |
| 299 | ||
| f719c866 | 300 | static int interlocked_sleep(struct lockit *, int, void *, int, |
| a6ee311a | 301 | const char *, int); |
| 984263bc MD |
302 | |
| 303 | /* | |
| 304 | * When going to sleep, we must save our SPL so that it does | |
| 305 | * not get lost if some other process uses the lock while we | |
| 306 | * are sleeping. We restore it after we have slept. This routine | |
| 307 | * wraps the interlocking with functions that sleep. The list | |
| 308 | * below enumerates the available set of operations. | |
| 309 | */ | |
| 310 | #define UNKNOWN 0 | |
| 311 | #define SLEEP 1 | |
| 312 | #define LOCKBUF 2 | |
| 313 | ||
| 314 | static int | |
| 3fcb1ab8 SW |
315 | interlocked_sleep(struct lockit *lk, int op, void *ident, int flags, |
| 316 | const char *wmesg, int timo) | |
| 984263bc | 317 | { |
| dadab5e9 | 318 | thread_t holder; |
| 984263bc MD |
319 | int s, retval; |
| 320 | ||
| 321 | s = lk->lkt_spl; | |
| 322 | # ifdef DEBUG | |
| dadab5e9 | 323 | if (lk->lkt_held == NOHOLDER) |
| 984263bc | 324 | panic("interlocked_sleep: lock not held"); |
| dadab5e9 | 325 | lk->lkt_held = NOHOLDER; |
| 984263bc MD |
326 | # endif /* DEBUG */ |
| 327 | switch (op) { | |
| 328 | case SLEEP: | |
| 329 | retval = tsleep(ident, flags, wmesg, timo); | |
| 330 | break; | |
| 331 | case LOCKBUF: | |
| 332 | retval = BUF_LOCK((struct buf *)ident, flags); | |
| 333 | break; | |
| 334 | default: | |
| 335 | panic("interlocked_sleep: unknown operation"); | |
| 336 | } | |
| 337 | # ifdef DEBUG | |
| dadab5e9 | 338 | if (lk->lkt_held != NOHOLDER) { |
| 984263bc MD |
339 | holder = lk->lkt_held; |
| 340 | FREE_LOCK(lk); | |
| dadab5e9 | 341 | if (holder == curthread) |
| 984263bc MD |
342 | panic("interlocked_sleep: locking against self"); |
| 343 | else | |
| dadab5e9 | 344 | panic("interlocked_sleep: lock held by %p", holder); |
| 984263bc | 345 | } |
| dadab5e9 | 346 | lk->lkt_held = curthread; |
| 984263bc MD |
347 | lockcnt++; |
| 348 | # endif /* DEBUG */ | |
| 349 | lk->lkt_spl = s; | |
| 350 | return (retval); | |
| 351 | } | |
| 352 | ||
| 353 | /* | |
| 354 | * Place holder for real semaphores. | |
| 355 | */ | |
| 356 | struct sema { | |
| 357 | int value; | |
| dadab5e9 | 358 | thread_t holder; |
| 984263bc MD |
359 | char *name; |
| 360 | int prio; | |
| 361 | int timo; | |
| 362 | }; | |
| f719c866 DR |
363 | static void sema_init(struct sema *, char *, int, int); |
| 364 | static int sema_get(struct sema *, struct lockit *); | |
| 365 | static void sema_release(struct sema *); | |
| 984263bc MD |
366 | |
| 367 | static void | |
| 3fcb1ab8 | 368 | sema_init(struct sema *semap, char *name, int prio, int timo) |
| 984263bc MD |
369 | { |
| 370 | ||
| dadab5e9 | 371 | semap->holder = NOHOLDER; |
| 984263bc MD |
372 | semap->value = 0; |
| 373 | semap->name = name; | |
| 374 | semap->prio = prio; | |
| 375 | semap->timo = timo; | |
| 376 | } | |
| 377 | ||
| 378 | static int | |
| 3fcb1ab8 | 379 | sema_get(struct sema *semap, struct lockit *interlock) |
| 984263bc MD |
380 | { |
| 381 | ||
| 382 | if (semap->value++ > 0) { | |
| 383 | if (interlock != NULL) { | |
| 384 | interlocked_sleep(interlock, SLEEP, (caddr_t)semap, | |
| 385 | semap->prio, semap->name, semap->timo); | |
| 386 | FREE_LOCK(interlock); | |
| 387 | } else { | |
| 388 | tsleep((caddr_t)semap, semap->prio, semap->name, | |
| 389 | semap->timo); | |
| 390 | } | |
| 391 | return (0); | |
| 392 | } | |
| dadab5e9 | 393 | semap->holder = curthread; |
| 984263bc MD |
394 | if (interlock != NULL) |
| 395 | FREE_LOCK(interlock); | |
| 396 | return (1); | |
| 397 | } | |
| 398 | ||
| 399 | static void | |
| 3fcb1ab8 | 400 | sema_release(struct sema *semap) |
| 984263bc MD |
401 | { |
| 402 | ||
| dadab5e9 MD |
403 | if (semap->value <= 0 || semap->holder != curthread) { |
| 404 | if (lk.lkt_held != NOHOLDER) | |
| 984263bc MD |
405 | FREE_LOCK(&lk); |
| 406 | panic("sema_release: not held"); | |
| 407 | } | |
| 408 | if (--semap->value > 0) { | |
| 409 | semap->value = 0; | |
| 410 | wakeup(semap); | |
| 411 | } | |
| dadab5e9 | 412 | semap->holder = NOHOLDER; |
| 984263bc MD |
413 | } |
| 414 | ||
| 415 | /* | |
| 416 | * Worklist queue management. | |
| 417 | * These routines require that the lock be held. | |
| 418 | */ | |
| 419 | #ifndef /* NOT */ DEBUG | |
| 420 | #define WORKLIST_INSERT(head, item) do { \ | |
| 421 | (item)->wk_state |= ONWORKLIST; \ | |
| 422 | LIST_INSERT_HEAD(head, item, wk_list); \ | |
| 423 | } while (0) | |
| 408357d8 MD |
424 | |
| 425 | #define WORKLIST_INSERT_BP(bp, item) do { \ | |
| 426 | (item)->wk_state |= ONWORKLIST; \ | |
| 427 | (bp)->b_ops = &softdep_bioops; \ | |
| 428 | LIST_INSERT_HEAD(&(bp)->b_dep, item, wk_list); \ | |
| 429 | } while (0) | |
| 430 | ||
| 984263bc MD |
431 | #define WORKLIST_REMOVE(item) do { \ |
| 432 | (item)->wk_state &= ~ONWORKLIST; \ | |
| 433 | LIST_REMOVE(item, wk_list); \ | |
| 434 | } while (0) | |
| 408357d8 | 435 | |
| 984263bc MD |
436 | #define WORKITEM_FREE(item, type) FREE(item, DtoM(type)) |
| 437 | ||
| 438 | #else /* DEBUG */ | |
| f719c866 DR |
439 | static void worklist_insert(struct workhead *, struct worklist *); |
| 440 | static void worklist_remove(struct worklist *); | |
| 441 | static void workitem_free(struct worklist *, int); | |
| 984263bc | 442 | |
| 408357d8 MD |
443 | #define WORKLIST_INSERT_BP(bp, item) do { \ |
| 444 | (bp)->b_ops = &softdep_bioops; \ | |
| 445 | worklist_insert(&(bp)->b_dep, item); \ | |
| 446 | } while (0) | |
| 447 | ||
| 984263bc MD |
448 | #define WORKLIST_INSERT(head, item) worklist_insert(head, item) |
| 449 | #define WORKLIST_REMOVE(item) worklist_remove(item) | |
| 450 | #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)item, type) | |
| 451 | ||
| 452 | static void | |
| 3fcb1ab8 | 453 | worklist_insert(struct workhead *head, struct worklist *item) |
| 984263bc MD |
454 | { |
| 455 | ||
| dadab5e9 | 456 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
457 | panic("worklist_insert: lock not held"); |
| 458 | if (item->wk_state & ONWORKLIST) { | |
| 459 | FREE_LOCK(&lk); | |
| 460 | panic("worklist_insert: already on list"); | |
| 461 | } | |
| 462 | item->wk_state |= ONWORKLIST; | |
| 463 | LIST_INSERT_HEAD(head, item, wk_list); | |
| 464 | } | |
| 465 | ||
| 466 | static void | |
| 3fcb1ab8 | 467 | worklist_remove(struct worklist *item) |
| 984263bc MD |
468 | { |
| 469 | ||
| dadab5e9 | 470 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
471 | panic("worklist_remove: lock not held"); |
| 472 | if ((item->wk_state & ONWORKLIST) == 0) { | |
| 473 | FREE_LOCK(&lk); | |
| 474 | panic("worklist_remove: not on list"); | |
| 475 | } | |
| 476 | item->wk_state &= ~ONWORKLIST; | |
| 477 | LIST_REMOVE(item, wk_list); | |
| 478 | } | |
| 479 | ||
| 480 | static void | |
| 3fcb1ab8 | 481 | workitem_free(struct worklist *item, int type) |
| 984263bc MD |
482 | { |
| 483 | ||
| 484 | if (item->wk_state & ONWORKLIST) { | |
| dadab5e9 | 485 | if (lk.lkt_held != NOHOLDER) |
| 984263bc MD |
486 | FREE_LOCK(&lk); |
| 487 | panic("workitem_free: still on list"); | |
| 488 | } | |
| 489 | if (item->wk_type != type) { | |
| dadab5e9 | 490 | if (lk.lkt_held != NOHOLDER) |
| 984263bc MD |
491 | FREE_LOCK(&lk); |
| 492 | panic("workitem_free: type mismatch"); | |
| 493 | } | |
| 494 | FREE(item, DtoM(type)); | |
| 495 | } | |
| 496 | #endif /* DEBUG */ | |
| 497 | ||
| 498 | /* | |
| 499 | * Workitem queue management | |
| 500 | */ | |
| 501 | static struct workhead softdep_workitem_pending; | |
| 502 | static int num_on_worklist; /* number of worklist items to be processed */ | |
| 503 | static int softdep_worklist_busy; /* 1 => trying to do unmount */ | |
| 504 | static int softdep_worklist_req; /* serialized waiters */ | |
| 505 | static int max_softdeps; /* maximum number of structs before slowdown */ | |
| 506 | static int tickdelay = 2; /* number of ticks to pause during slowdown */ | |
| 507 | static int *stat_countp; /* statistic to count in proc_waiting timeout */ | |
| 508 | static int proc_waiting; /* tracks whether we have a timeout posted */ | |
| 47979091 | 509 | static struct callout handle; /* handle on posted proc_waiting timeout */ |
| dadab5e9 | 510 | static struct thread *filesys_syncer; /* proc of filesystem syncer process */ |
| 984263bc MD |
511 | static int req_clear_inodedeps; /* syncer process flush some inodedeps */ |
| 512 | #define FLUSH_INODES 1 | |
| 513 | static int req_clear_remove; /* syncer process flush some freeblks */ | |
| 514 | #define FLUSH_REMOVE 2 | |
| 515 | /* | |
| 516 | * runtime statistics | |
| 517 | */ | |
| 518 | static int stat_worklist_push; /* number of worklist cleanups */ | |
| 519 | static int stat_blk_limit_push; /* number of times block limit neared */ | |
| 520 | static int stat_ino_limit_push; /* number of times inode limit neared */ | |
| 521 | static int stat_blk_limit_hit; /* number of times block slowdown imposed */ | |
| 522 | static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ | |
| 523 | static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ | |
| 524 | static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ | |
| 525 | static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ | |
| 526 | static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ | |
| 527 | static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ | |
| 528 | #ifdef DEBUG | |
| 529 | #include <vm/vm.h> | |
| 530 | #include <sys/sysctl.h> | |
| 531 | SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, ""); | |
| 532 | SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, ""); | |
| 533 | SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,""); | |
| 534 | SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,""); | |
| 535 | SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,""); | |
| 536 | SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, ""); | |
| 537 | SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, ""); | |
| 538 | SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, ""); | |
| 539 | SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, ""); | |
| 540 | SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, ""); | |
| 541 | SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, ""); | |
| 542 | SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, ""); | |
| 543 | #endif /* DEBUG */ | |
| 544 | ||
| 545 | /* | |
| 546 | * Add an item to the end of the work queue. | |
| 547 | * This routine requires that the lock be held. | |
| 548 | * This is the only routine that adds items to the list. | |
| 549 | * The following routine is the only one that removes items | |
| 550 | * and does so in order from first to last. | |
| 551 | */ | |
| 552 | static void | |
| 3fcb1ab8 | 553 | add_to_worklist(struct worklist *wk) |
| 984263bc MD |
554 | { |
| 555 | static struct worklist *worklist_tail; | |
| 556 | ||
| 557 | if (wk->wk_state & ONWORKLIST) { | |
| dadab5e9 | 558 | if (lk.lkt_held != NOHOLDER) |
| 984263bc MD |
559 | FREE_LOCK(&lk); |
| 560 | panic("add_to_worklist: already on list"); | |
| 561 | } | |
| 562 | wk->wk_state |= ONWORKLIST; | |
| 563 | if (LIST_FIRST(&softdep_workitem_pending) == NULL) | |
| 564 | LIST_INSERT_HEAD(&softdep_workitem_pending, wk, wk_list); | |
| 565 | else | |
| 566 | LIST_INSERT_AFTER(worklist_tail, wk, wk_list); | |
| 567 | worklist_tail = wk; | |
| 568 | num_on_worklist += 1; | |
| 569 | } | |
| 570 | ||
| 571 | /* | |
| 572 | * Process that runs once per second to handle items in the background queue. | |
| 573 | * | |
| 574 | * Note that we ensure that everything is done in the order in which they | |
| 575 | * appear in the queue. The code below depends on this property to ensure | |
| 576 | * that blocks of a file are freed before the inode itself is freed. This | |
| 577 | * ordering ensures that no new <vfsid, inum, lbn> triples will be generated | |
| 578 | * until all the old ones have been purged from the dependency lists. | |
| 579 | */ | |
| 580 | static int | |
| 3fcb1ab8 | 581 | softdep_process_worklist(struct mount *matchmnt) |
| 984263bc | 582 | { |
| dadab5e9 | 583 | thread_t td = curthread; |
| 984263bc MD |
584 | int matchcnt, loopcount; |
| 585 | long starttime; | |
| 586 | ||
| 587 | /* | |
| 588 | * Record the process identifier of our caller so that we can give | |
| 589 | * this process preferential treatment in request_cleanup below. | |
| 590 | */ | |
| dadab5e9 | 591 | filesys_syncer = td; |
| 984263bc MD |
592 | matchcnt = 0; |
| 593 | ||
| 594 | /* | |
| 595 | * There is no danger of having multiple processes run this | |
| 596 | * code, but we have to single-thread it when softdep_flushfiles() | |
| 597 | * is in operation to get an accurate count of the number of items | |
| 598 | * related to its mount point that are in the list. | |
| 599 | */ | |
| 600 | if (matchmnt == NULL) { | |
| 601 | if (softdep_worklist_busy < 0) | |
| 602 | return(-1); | |
| 603 | softdep_worklist_busy += 1; | |
| 604 | } | |
| 605 | ||
| 606 | /* | |
| 607 | * If requested, try removing inode or removal dependencies. | |
| 608 | */ | |
| 609 | if (req_clear_inodedeps) { | |
| dadab5e9 | 610 | clear_inodedeps(td); |
| 984263bc MD |
611 | req_clear_inodedeps -= 1; |
| 612 | wakeup_one(&proc_waiting); | |
| 613 | } | |
| 614 | if (req_clear_remove) { | |
| dadab5e9 | 615 | clear_remove(td); |
| 984263bc MD |
616 | req_clear_remove -= 1; |
| 617 | wakeup_one(&proc_waiting); | |
| 618 | } | |
| 619 | loopcount = 1; | |
| 620 | starttime = time_second; | |
| 621 | while (num_on_worklist > 0) { | |
| 622 | matchcnt += process_worklist_item(matchmnt, 0); | |
| 623 | ||
| 624 | /* | |
| 625 | * If a umount operation wants to run the worklist | |
| 626 | * accurately, abort. | |
| 627 | */ | |
| 628 | if (softdep_worklist_req && matchmnt == NULL) { | |
| 629 | matchcnt = -1; | |
| 630 | break; | |
| 631 | } | |
| 632 | ||
| 633 | /* | |
| 634 | * If requested, try removing inode or removal dependencies. | |
| 635 | */ | |
| 636 | if (req_clear_inodedeps) { | |
| dadab5e9 | 637 | clear_inodedeps(td); |
| 984263bc MD |
638 | req_clear_inodedeps -= 1; |
| 639 | wakeup_one(&proc_waiting); | |
| 640 | } | |
| 641 | if (req_clear_remove) { | |
| dadab5e9 | 642 | clear_remove(td); |
| 984263bc MD |
643 | req_clear_remove -= 1; |
| 644 | wakeup_one(&proc_waiting); | |
| 645 | } | |
| 646 | /* | |
| 647 | * We do not generally want to stop for buffer space, but if | |
| 648 | * we are really being a buffer hog, we will stop and wait. | |
| 649 | */ | |
| 650 | if (loopcount++ % 128 == 0) | |
| c4df9635 | 651 | bwillinode(1); |
| 984263bc MD |
652 | /* |
| 653 | * Never allow processing to run for more than one | |
| 654 | * second. Otherwise the other syncer tasks may get | |
| 655 | * excessively backlogged. | |
| 656 | */ | |
| 657 | if (starttime != time_second && matchmnt == NULL) { | |
| 658 | matchcnt = -1; | |
| 659 | break; | |
| 660 | } | |
| 661 | } | |
| 662 | if (matchmnt == NULL) { | |
| 663 | --softdep_worklist_busy; | |
| 664 | if (softdep_worklist_req && softdep_worklist_busy == 0) | |
| 665 | wakeup(&softdep_worklist_req); | |
| 666 | } | |
| 667 | return (matchcnt); | |
| 668 | } | |
| 669 | ||
| 670 | /* | |
| 671 | * Process one item on the worklist. | |
| 672 | */ | |
| 673 | static int | |
| 3fcb1ab8 | 674 | process_worklist_item(struct mount *matchmnt, int flags) |
| 984263bc MD |
675 | { |
| 676 | struct worklist *wk; | |
| 677 | struct dirrem *dirrem; | |
| 678 | struct fs *matchfs; | |
| 679 | struct vnode *vp; | |
| 680 | int matchcnt = 0; | |
| 681 | ||
| 682 | matchfs = NULL; | |
| 683 | if (matchmnt != NULL) | |
| 684 | matchfs = VFSTOUFS(matchmnt)->um_fs; | |
| 685 | ACQUIRE_LOCK(&lk); | |
| 686 | /* | |
| 687 | * Normally we just process each item on the worklist in order. | |
| 688 | * However, if we are in a situation where we cannot lock any | |
| 689 | * inodes, we have to skip over any dirrem requests whose | |
| 690 | * vnodes are resident and locked. | |
| 691 | */ | |
| 692 | LIST_FOREACH(wk, &softdep_workitem_pending, wk_list) { | |
| 693 | if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM) | |
| 694 | break; | |
| 695 | dirrem = WK_DIRREM(wk); | |
| 696 | vp = ufs_ihashlookup(VFSTOUFS(dirrem->dm_mnt)->um_dev, | |
| 697 | dirrem->dm_oldinum); | |
| a11aaa81 | 698 | if (vp == NULL || !vn_islocked(vp)) |
| 984263bc MD |
699 | break; |
| 700 | } | |
| 701 | if (wk == 0) { | |
| 702 | FREE_LOCK(&lk); | |
| 703 | return (0); | |
| 704 | } | |
| 705 | WORKLIST_REMOVE(wk); | |
| 706 | num_on_worklist -= 1; | |
| 707 | FREE_LOCK(&lk); | |
| 708 | switch (wk->wk_type) { | |
| 709 | ||
| 710 | case D_DIRREM: | |
| 711 | /* removal of a directory entry */ | |
| 712 | if (WK_DIRREM(wk)->dm_mnt == matchmnt) | |
| 713 | matchcnt += 1; | |
| 714 | handle_workitem_remove(WK_DIRREM(wk)); | |
| 715 | break; | |
| 716 | ||
| 717 | case D_FREEBLKS: | |
| 718 | /* releasing blocks and/or fragments from a file */ | |
| 719 | if (WK_FREEBLKS(wk)->fb_fs == matchfs) | |
| 720 | matchcnt += 1; | |
| 721 | handle_workitem_freeblocks(WK_FREEBLKS(wk)); | |
| 722 | break; | |
| 723 | ||
| 724 | case D_FREEFRAG: | |
| 725 | /* releasing a fragment when replaced as a file grows */ | |
| 726 | if (WK_FREEFRAG(wk)->ff_fs == matchfs) | |
| 727 | matchcnt += 1; | |
| 728 | handle_workitem_freefrag(WK_FREEFRAG(wk)); | |
| 729 | break; | |
| 730 | ||
| 731 | case D_FREEFILE: | |
| 732 | /* releasing an inode when its link count drops to 0 */ | |
| 733 | if (WK_FREEFILE(wk)->fx_fs == matchfs) | |
| 734 | matchcnt += 1; | |
| 735 | handle_workitem_freefile(WK_FREEFILE(wk)); | |
| 736 | break; | |
| 737 | ||
| 738 | default: | |
| 739 | panic("%s_process_worklist: Unknown type %s", | |
| 740 | "softdep", TYPENAME(wk->wk_type)); | |
| 741 | /* NOTREACHED */ | |
| 742 | } | |
| 743 | return (matchcnt); | |
| 744 | } | |
| 745 | ||
| 746 | /* | |
| 747 | * Move dependencies from one buffer to another. | |
| 748 | */ | |
| 749 | static void | |
| 3fcb1ab8 | 750 | softdep_move_dependencies(struct buf *oldbp, struct buf *newbp) |
| 984263bc MD |
751 | { |
| 752 | struct worklist *wk, *wktail; | |
| 753 | ||
| 754 | if (LIST_FIRST(&newbp->b_dep) != NULL) | |
| 755 | panic("softdep_move_dependencies: need merge code"); | |
| 408357d8 | 756 | wktail = NULL; |
| 984263bc MD |
757 | ACQUIRE_LOCK(&lk); |
| 758 | while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { | |
| 759 | LIST_REMOVE(wk, wk_list); | |
| 408357d8 | 760 | if (wktail == NULL) |
| 984263bc MD |
761 | LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); |
| 762 | else | |
| 763 | LIST_INSERT_AFTER(wktail, wk, wk_list); | |
| 764 | wktail = wk; | |
| 408357d8 | 765 | newbp->b_ops = &softdep_bioops; |
| 984263bc MD |
766 | } |
| 767 | FREE_LOCK(&lk); | |
| 768 | } | |
| 769 | ||
| 770 | /* | |
| 771 | * Purge the work list of all items associated with a particular mount point. | |
| 772 | */ | |
| 773 | int | |
| 2aa32050 | 774 | softdep_flushfiles(struct mount *oldmnt, int flags) |
| 984263bc MD |
775 | { |
| 776 | struct vnode *devvp; | |
| 777 | int error, loopcnt; | |
| 778 | ||
| 779 | /* | |
| 780 | * Await our turn to clear out the queue, then serialize access. | |
| 781 | */ | |
| 782 | while (softdep_worklist_busy != 0) { | |
| 783 | softdep_worklist_req += 1; | |
| 377d4740 | 784 | tsleep(&softdep_worklist_req, 0, "softflush", 0); |
| 984263bc MD |
785 | softdep_worklist_req -= 1; |
| 786 | } | |
| 787 | softdep_worklist_busy = -1; | |
| 788 | ||
| 2aa32050 | 789 | if ((error = ffs_flushfiles(oldmnt, flags)) != 0) { |
| 984263bc MD |
790 | softdep_worklist_busy = 0; |
| 791 | if (softdep_worklist_req) | |
| 792 | wakeup(&softdep_worklist_req); | |
| 793 | return (error); | |
| 794 | } | |
| 795 | /* | |
| 796 | * Alternately flush the block device associated with the mount | |
| 797 | * point and process any dependencies that the flushing | |
| 798 | * creates. In theory, this loop can happen at most twice, | |
| 799 | * but we give it a few extra just to be sure. | |
| 800 | */ | |
| 801 | devvp = VFSTOUFS(oldmnt)->um_devvp; | |
| 802 | for (loopcnt = 10; loopcnt > 0; ) { | |
| 803 | if (softdep_process_worklist(oldmnt) == 0) { | |
| 804 | loopcnt--; | |
| 805 | /* | |
| 806 | * Do another flush in case any vnodes were brought in | |
| 807 | * as part of the cleanup operations. | |
| 808 | */ | |
| 2aa32050 | 809 | if ((error = ffs_flushfiles(oldmnt, flags)) != 0) |
| 984263bc MD |
810 | break; |
| 811 | /* | |
| 812 | * If we still found nothing to do, we are really done. | |
| 813 | */ | |
| 814 | if (softdep_process_worklist(oldmnt) == 0) | |
| 815 | break; | |
| 816 | } | |
| ca466bae | 817 | vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); |
| 52174f71 | 818 | error = VOP_FSYNC(devvp, MNT_WAIT, 0); |
| a11aaa81 | 819 | vn_unlock(devvp); |
| 984263bc MD |
820 | if (error) |
| 821 | break; | |
| 822 | } | |
| 823 | softdep_worklist_busy = 0; | |
| 824 | if (softdep_worklist_req) | |
| 825 | wakeup(&softdep_worklist_req); | |
| 826 | ||
| 827 | /* | |
| 828 | * If we are unmounting then it is an error to fail. If we | |
| 829 | * are simply trying to downgrade to read-only, then filesystem | |
| 830 | * activity can keep us busy forever, so we just fail with EBUSY. | |
| 831 | */ | |
| 832 | if (loopcnt == 0) { | |
| 833 | if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) | |
| 834 | panic("softdep_flushfiles: looping"); | |
| 835 | error = EBUSY; | |
| 836 | } | |
| 837 | return (error); | |
| 838 | } | |
| 839 | ||
| 840 | /* | |
| 841 | * Structure hashing. | |
| 842 | * | |
| 843 | * There are three types of structures that can be looked up: | |
| 844 | * 1) pagedep structures identified by mount point, inode number, | |
| 845 | * and logical block. | |
| 846 | * 2) inodedep structures identified by mount point and inode number. | |
| 847 | * 3) newblk structures identified by mount point and | |
| 848 | * physical block number. | |
| 849 | * | |
| 850 | * The "pagedep" and "inodedep" dependency structures are hashed | |
| 851 | * separately from the file blocks and inodes to which they correspond. | |
| 852 | * This separation helps when the in-memory copy of an inode or | |
| 853 | * file block must be replaced. It also obviates the need to access | |
| 854 | * an inode or file page when simply updating (or de-allocating) | |
| 855 | * dependency structures. Lookup of newblk structures is needed to | |
| 856 | * find newly allocated blocks when trying to associate them with | |
| 857 | * their allocdirect or allocindir structure. | |
| 858 | * | |
| 859 | * The lookup routines optionally create and hash a new instance when | |
| 860 | * an existing entry is not found. | |
| 861 | */ | |
| 862 | #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ | |
| 863 | #define NODELAY 0x0002 /* cannot do background work */ | |
| 864 | ||
| 865 | /* | |
| 866 | * Structures and routines associated with pagedep caching. | |
| 867 | */ | |
| 868 | LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl; | |
| 869 | u_long pagedep_hash; /* size of hash table - 1 */ | |
| 870 | #define PAGEDEP_HASH(mp, inum, lbn) \ | |
| 871 | (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \ | |
| 872 | pagedep_hash]) | |
| 873 | static struct sema pagedep_in_progress; | |
| 874 | ||
| 875 | /* | |
| 98a74972 MD |
876 | * Helper routine for pagedep_lookup() |
| 877 | */ | |
| 878 | static __inline | |
| 879 | struct pagedep * | |
| 880 | pagedep_find(struct pagedep_hashhead *pagedephd, ino_t ino, ufs_lbn_t lbn, | |
| 881 | struct mount *mp) | |
| 882 | { | |
| 883 | struct pagedep *pagedep; | |
| 884 | ||
| 885 | LIST_FOREACH(pagedep, pagedephd, pd_hash) { | |
| 886 | if (ino == pagedep->pd_ino && | |
| 887 | lbn == pagedep->pd_lbn && | |
| 888 | mp == pagedep->pd_mnt) { | |
| 889 | return (pagedep); | |
| 890 | } | |
| 891 | } | |
| 892 | return(NULL); | |
| 893 | } | |
| 894 | ||
| 895 | /* | |
| 984263bc MD |
896 | * Look up a pagedep. Return 1 if found, 0 if not found. |
| 897 | * If not found, allocate if DEPALLOC flag is passed. | |
| 898 | * Found or allocated entry is returned in pagedeppp. | |
| 899 | * This routine must be called with splbio interrupts blocked. | |
| 900 | */ | |
| 901 | static int | |
| 3fcb1ab8 SW |
902 | pagedep_lookup(struct inode *ip, ufs_lbn_t lbn, int flags, |
| 903 | struct pagedep **pagedeppp) | |
| 984263bc MD |
904 | { |
| 905 | struct pagedep *pagedep; | |
| 906 | struct pagedep_hashhead *pagedephd; | |
| 907 | struct mount *mp; | |
| 908 | int i; | |
| 909 | ||
| 910 | #ifdef DEBUG | |
| dadab5e9 | 911 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
912 | panic("pagedep_lookup: lock not held"); |
| 913 | #endif | |
| 914 | mp = ITOV(ip)->v_mount; | |
| 915 | pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn); | |
| 916 | top: | |
| 98a74972 MD |
917 | *pagedeppp = pagedep_find(pagedephd, ip->i_number, lbn, mp); |
| 918 | if (*pagedeppp) | |
| 919 | return(1); | |
| 920 | if ((flags & DEPALLOC) == 0) | |
| 984263bc | 921 | return (0); |
| 984263bc MD |
922 | if (sema_get(&pagedep_in_progress, &lk) == 0) { |
| 923 | ACQUIRE_LOCK(&lk); | |
| 924 | goto top; | |
| 925 | } | |
| 926 | MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep), M_PAGEDEP, | |
| 98a74972 MD |
927 | M_SOFTDEP_FLAGS | M_ZERO); |
| 928 | ||
| 929 | if (pagedep_find(pagedephd, ip->i_number, lbn, mp)) { | |
| 086c1d7e | 930 | kprintf("pagedep_lookup: blocking race avoided\n"); |
| 98a74972 MD |
931 | ACQUIRE_LOCK(&lk); |
| 932 | sema_release(&pagedep_in_progress); | |
| efda3bd0 | 933 | kfree(pagedep, M_PAGEDEP); |
| 98a74972 MD |
934 | goto top; |
| 935 | } | |
| 936 | ||
| 984263bc MD |
937 | pagedep->pd_list.wk_type = D_PAGEDEP; |
| 938 | pagedep->pd_mnt = mp; | |
| 939 | pagedep->pd_ino = ip->i_number; | |
| 940 | pagedep->pd_lbn = lbn; | |
| 941 | LIST_INIT(&pagedep->pd_dirremhd); | |
| 942 | LIST_INIT(&pagedep->pd_pendinghd); | |
| 943 | for (i = 0; i < DAHASHSZ; i++) | |
| 944 | LIST_INIT(&pagedep->pd_diraddhd[i]); | |
| 945 | ACQUIRE_LOCK(&lk); | |
| 946 | LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); | |
| 947 | sema_release(&pagedep_in_progress); | |
| 948 | *pagedeppp = pagedep; | |
| 949 | return (0); | |
| 950 | } | |
| 951 | ||
| 952 | /* | |
| 953 | * Structures and routines associated with inodedep caching. | |
| 954 | */ | |
| 955 | LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl; | |
| 956 | static u_long inodedep_hash; /* size of hash table - 1 */ | |
| 957 | static long num_inodedep; /* number of inodedep allocated */ | |
| 958 | #define INODEDEP_HASH(fs, inum) \ | |
| 959 | (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash]) | |
| 960 | static struct sema inodedep_in_progress; | |
| 961 | ||
| 962 | /* | |
| 98a74972 MD |
963 | * Helper routine for inodedep_lookup() |
| 964 | */ | |
| 965 | static __inline | |
| 966 | struct inodedep * | |
| 967 | inodedep_find(struct inodedep_hashhead *inodedephd, struct fs *fs, ino_t inum) | |
| 968 | { | |
| 969 | struct inodedep *inodedep; | |
| 970 | ||
| 971 | LIST_FOREACH(inodedep, inodedephd, id_hash) { | |
| 972 | if (inum == inodedep->id_ino && fs == inodedep->id_fs) | |
| 973 | return(inodedep); | |
| 974 | } | |
| 975 | return (NULL); | |
| 976 | } | |
| 977 | ||
| 978 | /* | |
| 984263bc MD |
979 | * Look up a inodedep. Return 1 if found, 0 if not found. |
| 980 | * If not found, allocate if DEPALLOC flag is passed. | |
| 981 | * Found or allocated entry is returned in inodedeppp. | |
| 982 | * This routine must be called with splbio interrupts blocked. | |
| 983 | */ | |
| 984 | static int | |
| 3fcb1ab8 SW |
985 | inodedep_lookup(struct fs *fs, ino_t inum, int flags, |
| 986 | struct inodedep **inodedeppp) | |
| 984263bc MD |
987 | { |
| 988 | struct inodedep *inodedep; | |
| 989 | struct inodedep_hashhead *inodedephd; | |
| 990 | int firsttry; | |
| 991 | ||
| 992 | #ifdef DEBUG | |
| dadab5e9 | 993 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
994 | panic("inodedep_lookup: lock not held"); |
| 995 | #endif | |
| 996 | firsttry = 1; | |
| 997 | inodedephd = INODEDEP_HASH(fs, inum); | |
| 998 | top: | |
| 98a74972 MD |
999 | *inodedeppp = inodedep_find(inodedephd, fs, inum); |
| 1000 | if (*inodedeppp) | |
| 984263bc | 1001 | return (1); |
| 98a74972 | 1002 | if ((flags & DEPALLOC) == 0) |
| 984263bc | 1003 | return (0); |
| 984263bc MD |
1004 | /* |
| 1005 | * If we are over our limit, try to improve the situation. | |
| 1006 | */ | |
| 1007 | if (num_inodedep > max_softdeps && firsttry && | |
| 1008 | speedup_syncer() == 0 && (flags & NODELAY) == 0 && | |
| 1009 | request_cleanup(FLUSH_INODES, 1)) { | |
| 1010 | firsttry = 0; | |
| 1011 | goto top; | |
| 1012 | } | |
| 1013 | if (sema_get(&inodedep_in_progress, &lk) == 0) { | |
| 1014 | ACQUIRE_LOCK(&lk); | |
| 1015 | goto top; | |
| 1016 | } | |
| 984263bc | 1017 | MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep), |
| 98a74972 MD |
1018 | M_INODEDEP, M_SOFTDEP_FLAGS | M_ZERO); |
| 1019 | if (inodedep_find(inodedephd, fs, inum)) { | |
| 086c1d7e | 1020 | kprintf("inodedep_lookup: blocking race avoided\n"); |
| 98a74972 MD |
1021 | ACQUIRE_LOCK(&lk); |
| 1022 | sema_release(&inodedep_in_progress); | |
| efda3bd0 | 1023 | kfree(inodedep, M_INODEDEP); |
| 98a74972 MD |
1024 | goto top; |
| 1025 | } | |
| 984263bc MD |
1026 | inodedep->id_list.wk_type = D_INODEDEP; |
| 1027 | inodedep->id_fs = fs; | |
| 1028 | inodedep->id_ino = inum; | |
| 1029 | inodedep->id_state = ALLCOMPLETE; | |
| 1030 | inodedep->id_nlinkdelta = 0; | |
| 1031 | inodedep->id_savedino = NULL; | |
| 1032 | inodedep->id_savedsize = -1; | |
| 1033 | inodedep->id_buf = NULL; | |
| 1034 | LIST_INIT(&inodedep->id_pendinghd); | |
| 1035 | LIST_INIT(&inodedep->id_inowait); | |
| 1036 | LIST_INIT(&inodedep->id_bufwait); | |
| 1037 | TAILQ_INIT(&inodedep->id_inoupdt); | |
| 1038 | TAILQ_INIT(&inodedep->id_newinoupdt); | |
| 1039 | ACQUIRE_LOCK(&lk); | |
| 98a74972 | 1040 | num_inodedep += 1; |
| 984263bc MD |
1041 | LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); |
| 1042 | sema_release(&inodedep_in_progress); | |
| 1043 | *inodedeppp = inodedep; | |
| 1044 | return (0); | |
| 1045 | } | |
| 1046 | ||
| 1047 | /* | |
| 1048 | * Structures and routines associated with newblk caching. | |
| 1049 | */ | |
| 1050 | LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl; | |
| 1051 | u_long newblk_hash; /* size of hash table - 1 */ | |
| 1052 | #define NEWBLK_HASH(fs, inum) \ | |
| 1053 | (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash]) | |
| 1054 | static struct sema newblk_in_progress; | |
| 1055 | ||
| 1056 | /* | |
| 98a74972 MD |
1057 | * Helper routine for newblk_lookup() |
| 1058 | */ | |
| 1059 | static __inline | |
| 1060 | struct newblk * | |
| 1061 | newblk_find(struct newblk_hashhead *newblkhd, struct fs *fs, | |
| 1062 | ufs_daddr_t newblkno) | |
| 1063 | { | |
| 1064 | struct newblk *newblk; | |
| 1065 | ||
| 1066 | LIST_FOREACH(newblk, newblkhd, nb_hash) { | |
| 1067 | if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs) | |
| 1068 | return (newblk); | |
| 1069 | } | |
| 1070 | return(NULL); | |
| 1071 | } | |
| 1072 | ||
| 1073 | /* | |
| 984263bc MD |
1074 | * Look up a newblk. Return 1 if found, 0 if not found. |
| 1075 | * If not found, allocate if DEPALLOC flag is passed. | |
| 1076 | * Found or allocated entry is returned in newblkpp. | |
| 1077 | */ | |
| 1078 | static int | |
| 3fcb1ab8 SW |
1079 | newblk_lookup(struct fs *fs, ufs_daddr_t newblkno, int flags, |
| 1080 | struct newblk **newblkpp) | |
| 984263bc MD |
1081 | { |
| 1082 | struct newblk *newblk; | |
| 1083 | struct newblk_hashhead *newblkhd; | |
| 1084 | ||
| 1085 | newblkhd = NEWBLK_HASH(fs, newblkno); | |
| 1086 | top: | |
| 98a74972 MD |
1087 | *newblkpp = newblk_find(newblkhd, fs, newblkno); |
| 1088 | if (*newblkpp) | |
| 1089 | return(1); | |
| 1090 | if ((flags & DEPALLOC) == 0) | |
| 984263bc | 1091 | return (0); |
| 984263bc MD |
1092 | if (sema_get(&newblk_in_progress, 0) == 0) |
| 1093 | goto top; | |
| 1094 | MALLOC(newblk, struct newblk *, sizeof(struct newblk), | |
| 98a74972 MD |
1095 | M_NEWBLK, M_SOFTDEP_FLAGS | M_ZERO); |
| 1096 | ||
| 1097 | if (newblk_find(newblkhd, fs, newblkno)) { | |
| 086c1d7e | 1098 | kprintf("newblk_lookup: blocking race avoided\n"); |
| 98a74972 | 1099 | sema_release(&pagedep_in_progress); |
| efda3bd0 | 1100 | kfree(newblk, M_NEWBLK); |
| 98a74972 MD |
1101 | goto top; |
| 1102 | } | |
| 984263bc MD |
1103 | newblk->nb_state = 0; |
| 1104 | newblk->nb_fs = fs; | |
| 1105 | newblk->nb_newblkno = newblkno; | |
| 1106 | LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); | |
| 1107 | sema_release(&newblk_in_progress); | |
| 1108 | *newblkpp = newblk; | |
| 1109 | return (0); | |
| 1110 | } | |
| 1111 | ||
| 1112 | /* | |
| 1113 | * Executed during filesystem system initialization before | |
| f719c866 | 1114 | * mounting any filesystems. |
| 984263bc MD |
1115 | */ |
| 1116 | void | |
| 3fcb1ab8 | 1117 | softdep_initialize(void) |
| 984263bc | 1118 | { |
| 47979091 | 1119 | callout_init(&handle); |
| 984263bc MD |
1120 | |
| 1121 | LIST_INIT(&mkdirlisthd); | |
| 1122 | LIST_INIT(&softdep_workitem_pending); | |
| 1123 | max_softdeps = min(desiredvnodes * 8, | |
| 1124 | M_INODEDEP->ks_limit / (2 * sizeof(struct inodedep))); | |
| 1125 | pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, | |
| 1126 | &pagedep_hash); | |
| 377d4740 | 1127 | sema_init(&pagedep_in_progress, "pagedep", 0, 0); |
| 984263bc | 1128 | inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash); |
| 377d4740 | 1129 | sema_init(&inodedep_in_progress, "inodedep", 0, 0); |
| 984263bc | 1130 | newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash); |
| 377d4740 | 1131 | sema_init(&newblk_in_progress, "newblk", 0, 0); |
| 408357d8 | 1132 | add_bio_ops(&softdep_bioops); |
| 984263bc MD |
1133 | } |
| 1134 | ||
| 1135 | /* | |
| 1136 | * Called at mount time to notify the dependency code that a | |
| 1137 | * filesystem wishes to use it. | |
| 1138 | */ | |
| 1139 | int | |
| 3fcb1ab8 | 1140 | softdep_mount(struct vnode *devvp, struct mount *mp, struct fs *fs) |
| 984263bc MD |
1141 | { |
| 1142 | struct csum cstotal; | |
| 1143 | struct cg *cgp; | |
| 1144 | struct buf *bp; | |
| 1145 | int error, cyl; | |
| 1146 | ||
| 1147 | mp->mnt_flag &= ~MNT_ASYNC; | |
| 1148 | mp->mnt_flag |= MNT_SOFTDEP; | |
| 408357d8 | 1149 | mp->mnt_bioops = &softdep_bioops; |
| 984263bc MD |
1150 | /* |
| 1151 | * When doing soft updates, the counters in the | |
| 1152 | * superblock may have gotten out of sync, so we have | |
| 1153 | * to scan the cylinder groups and recalculate them. | |
| 1154 | */ | |
| 1155 | if (fs->fs_clean != 0) | |
| 1156 | return (0); | |
| 1157 | bzero(&cstotal, sizeof cstotal); | |
| 1158 | for (cyl = 0; cyl < fs->fs_ncg; cyl++) { | |
| 54078292 MD |
1159 | if ((error = bread(devvp, fsbtodoff(fs, cgtod(fs, cyl)), |
| 1160 | fs->fs_cgsize, &bp)) != 0) { | |
| 984263bc MD |
1161 | brelse(bp); |
| 1162 | return (error); | |
| 1163 | } | |
| 1164 | cgp = (struct cg *)bp->b_data; | |
| 1165 | cstotal.cs_nffree += cgp->cg_cs.cs_nffree; | |
| 1166 | cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; | |
| 1167 | cstotal.cs_nifree += cgp->cg_cs.cs_nifree; | |
| 1168 | cstotal.cs_ndir += cgp->cg_cs.cs_ndir; | |
| 1169 | fs->fs_cs(fs, cyl) = cgp->cg_cs; | |
| 1170 | brelse(bp); | |
| 1171 | } | |
| 1172 | #ifdef DEBUG | |
| 1173 | if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) | |
| 086c1d7e | 1174 | kprintf("ffs_mountfs: superblock updated for soft updates\n"); |
| 984263bc MD |
1175 | #endif |
| 1176 | bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); | |
| 1177 | return (0); | |
| 1178 | } | |
| 1179 | ||
| 1180 | /* | |
| 1181 | * Protecting the freemaps (or bitmaps). | |
| 1182 | * | |
| f719c866 | 1183 | * To eliminate the need to execute fsck before mounting a filesystem |
| 984263bc MD |
1184 | * after a power failure, one must (conservatively) guarantee that the |
| 1185 | * on-disk copy of the bitmaps never indicate that a live inode or block is | |
| 1186 | * free. So, when a block or inode is allocated, the bitmap should be | |
| 1187 | * updated (on disk) before any new pointers. When a block or inode is | |
| 1188 | * freed, the bitmap should not be updated until all pointers have been | |
| 1189 | * reset. The latter dependency is handled by the delayed de-allocation | |
| 1190 | * approach described below for block and inode de-allocation. The former | |
| 1191 | * dependency is handled by calling the following procedure when a block or | |
| 1192 | * inode is allocated. When an inode is allocated an "inodedep" is created | |
| 1193 | * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. | |
| 1194 | * Each "inodedep" is also inserted into the hash indexing structure so | |
| 1195 | * that any additional link additions can be made dependent on the inode | |
| 1196 | * allocation. | |
| 1197 | * | |
| f719c866 | 1198 | * The ufs filesystem maintains a number of free block counts (e.g., per |
| 984263bc MD |
1199 | * cylinder group, per cylinder and per <cylinder, rotational position> pair) |
| 1200 | * in addition to the bitmaps. These counts are used to improve efficiency | |
| 1201 | * during allocation and therefore must be consistent with the bitmaps. | |
| 1202 | * There is no convenient way to guarantee post-crash consistency of these | |
| 1203 | * counts with simple update ordering, for two main reasons: (1) The counts | |
| 1204 | * and bitmaps for a single cylinder group block are not in the same disk | |
| 1205 | * sector. If a disk write is interrupted (e.g., by power failure), one may | |
| 1206 | * be written and the other not. (2) Some of the counts are located in the | |
| 1207 | * superblock rather than the cylinder group block. So, we focus our soft | |
| 1208 | * updates implementation on protecting the bitmaps. When mounting a | |
| 1209 | * filesystem, we recompute the auxiliary counts from the bitmaps. | |
| 1210 | */ | |
| 1211 | ||
| 1212 | /* | |
| 1213 | * Called just after updating the cylinder group block to allocate an inode. | |
| 3fcb1ab8 SW |
1214 | * |
| 1215 | * Parameters: | |
| 1216 | * bp: buffer for cylgroup block with inode map | |
| 1217 | * ip: inode related to allocation | |
| 1218 | * newinum: new inode number being allocated | |
| 984263bc MD |
1219 | */ |
| 1220 | void | |
| 3fcb1ab8 | 1221 | softdep_setup_inomapdep(struct buf *bp, struct inode *ip, ino_t newinum) |
| 984263bc MD |
1222 | { |
| 1223 | struct inodedep *inodedep; | |
| 1224 | struct bmsafemap *bmsafemap; | |
| 1225 | ||
| 1226 | /* | |
| 1227 | * Create a dependency for the newly allocated inode. | |
| 1228 | * Panic if it already exists as something is seriously wrong. | |
| 1229 | * Otherwise add it to the dependency list for the buffer holding | |
| 1230 | * the cylinder group map from which it was allocated. | |
| 1231 | */ | |
| 1232 | ACQUIRE_LOCK(&lk); | |
| 1233 | if ((inodedep_lookup(ip->i_fs, newinum, DEPALLOC|NODELAY, &inodedep))) { | |
| 1234 | FREE_LOCK(&lk); | |
| 1235 | panic("softdep_setup_inomapdep: found inode"); | |
| 1236 | } | |
| 1237 | inodedep->id_buf = bp; | |
| 1238 | inodedep->id_state &= ~DEPCOMPLETE; | |
| 1239 | bmsafemap = bmsafemap_lookup(bp); | |
| 1240 | LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); | |
| 1241 | FREE_LOCK(&lk); | |
| 1242 | } | |
| 1243 | ||
| 1244 | /* | |
| 1245 | * Called just after updating the cylinder group block to | |
| 1246 | * allocate block or fragment. | |
| 3fcb1ab8 SW |
1247 | * |
| 1248 | * Parameters: | |
| 1249 | * bp: buffer for cylgroup block with block map | |
| 1250 | * fs: filesystem doing allocation | |
| 1251 | * newblkno: number of newly allocated block | |
| 984263bc MD |
1252 | */ |
| 1253 | void | |
| 3fcb1ab8 SW |
1254 | softdep_setup_blkmapdep(struct buf *bp, struct fs *fs, |
| 1255 | ufs_daddr_t newblkno) | |
| 984263bc MD |
1256 | { |
| 1257 | struct newblk *newblk; | |
| 1258 | struct bmsafemap *bmsafemap; | |
| 1259 | ||
| 1260 | /* | |
| 1261 | * Create a dependency for the newly allocated block. | |
| 1262 | * Add it to the dependency list for the buffer holding | |
| 1263 | * the cylinder group map from which it was allocated. | |
| 1264 | */ | |
| 1265 | if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0) | |
| 1266 | panic("softdep_setup_blkmapdep: found block"); | |
| 1267 | ACQUIRE_LOCK(&lk); | |
| 1268 | newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(bp); | |
| 1269 | LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); | |
| 1270 | FREE_LOCK(&lk); | |
| 1271 | } | |
| 1272 | ||
| 1273 | /* | |
| 1274 | * Find the bmsafemap associated with a cylinder group buffer. | |
| 1275 | * If none exists, create one. The buffer must be locked when | |
| 1276 | * this routine is called and this routine must be called with | |
| 1277 | * splbio interrupts blocked. | |
| 1278 | */ | |
| 1279 | static struct bmsafemap * | |
| 3fcb1ab8 | 1280 | bmsafemap_lookup(struct buf *bp) |
| 984263bc MD |
1281 | { |
| 1282 | struct bmsafemap *bmsafemap; | |
| 1283 | struct worklist *wk; | |
| 1284 | ||
| 1285 | #ifdef DEBUG | |
| dadab5e9 | 1286 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
1287 | panic("bmsafemap_lookup: lock not held"); |
| 1288 | #endif | |
| 408357d8 | 1289 | LIST_FOREACH(wk, &bp->b_dep, wk_list) { |
| 984263bc MD |
1290 | if (wk->wk_type == D_BMSAFEMAP) |
| 1291 | return (WK_BMSAFEMAP(wk)); | |
| 408357d8 | 1292 | } |
| 984263bc MD |
1293 | FREE_LOCK(&lk); |
| 1294 | MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap), | |
| 1295 | M_BMSAFEMAP, M_SOFTDEP_FLAGS); | |
| 1296 | bmsafemap->sm_list.wk_type = D_BMSAFEMAP; | |
| 1297 | bmsafemap->sm_list.wk_state = 0; | |
| 1298 | bmsafemap->sm_buf = bp; | |
| 1299 | LIST_INIT(&bmsafemap->sm_allocdirecthd); | |
| 1300 | LIST_INIT(&bmsafemap->sm_allocindirhd); | |
| 1301 | LIST_INIT(&bmsafemap->sm_inodedephd); | |
| 1302 | LIST_INIT(&bmsafemap->sm_newblkhd); | |
| 1303 | ACQUIRE_LOCK(&lk); | |
| 408357d8 | 1304 | WORKLIST_INSERT_BP(bp, &bmsafemap->sm_list); |
| 984263bc MD |
1305 | return (bmsafemap); |
| 1306 | } | |
| 1307 | ||
| 1308 | /* | |
| 1309 | * Direct block allocation dependencies. | |
| 1310 | * | |
| 1311 | * When a new block is allocated, the corresponding disk locations must be | |
| 1312 | * initialized (with zeros or new data) before the on-disk inode points to | |
| 1313 | * them. Also, the freemap from which the block was allocated must be | |
| 1314 | * updated (on disk) before the inode's pointer. These two dependencies are | |
| 1315 | * independent of each other and are needed for all file blocks and indirect | |
| 1316 | * blocks that are pointed to directly by the inode. Just before the | |
| 1317 | * "in-core" version of the inode is updated with a newly allocated block | |
| 1318 | * number, a procedure (below) is called to setup allocation dependency | |
| 1319 | * structures. These structures are removed when the corresponding | |
| 1320 | * dependencies are satisfied or when the block allocation becomes obsolete | |
| 1321 | * (i.e., the file is deleted, the block is de-allocated, or the block is a | |
| 1322 | * fragment that gets upgraded). All of these cases are handled in | |
| 1323 | * procedures described later. | |
| 1324 | * | |
| 1325 | * When a file extension causes a fragment to be upgraded, either to a larger | |
| 1326 | * fragment or to a full block, the on-disk location may change (if the | |
| 1327 | * previous fragment could not simply be extended). In this case, the old | |
| 1328 | * fragment must be de-allocated, but not until after the inode's pointer has | |
| 1329 | * been updated. In most cases, this is handled by later procedures, which | |
| 1330 | * will construct a "freefrag" structure to be added to the workitem queue | |
| 1331 | * when the inode update is complete (or obsolete). The main exception to | |
| 1332 | * this is when an allocation occurs while a pending allocation dependency | |
| 1333 | * (for the same block pointer) remains. This case is handled in the main | |
| 1334 | * allocation dependency setup procedure by immediately freeing the | |
| 1335 | * unreferenced fragments. | |
| 3fcb1ab8 SW |
1336 | * |
| 1337 | * Parameters: | |
| 1338 | * ip: inode to which block is being added | |
| 1339 | * lbn: block pointer within inode | |
| 1340 | * newblkno: disk block number being added | |
| 1341 | * oldblkno: previous block number, 0 unless frag | |
| 1342 | * newsize: size of new block | |
| 1343 | * oldsize: size of new block | |
| 1344 | * bp: bp for allocated block | |
| 984263bc MD |
1345 | */ |
| 1346 | void | |
| 3fcb1ab8 SW |
1347 | softdep_setup_allocdirect(struct inode *ip, ufs_lbn_t lbn, ufs_daddr_t newblkno, |
| 1348 | ufs_daddr_t oldblkno, long newsize, long oldsize, | |
| 1349 | struct buf *bp) | |
| 984263bc MD |
1350 | { |
| 1351 | struct allocdirect *adp, *oldadp; | |
| 1352 | struct allocdirectlst *adphead; | |
| 1353 | struct bmsafemap *bmsafemap; | |
| 1354 | struct inodedep *inodedep; | |
| 1355 | struct pagedep *pagedep; | |
| 1356 | struct newblk *newblk; | |
| 1357 | ||
| 1358 | MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect), | |
| e7b4468c | 1359 | M_ALLOCDIRECT, M_SOFTDEP_FLAGS | M_ZERO); |
| 984263bc MD |
1360 | adp->ad_list.wk_type = D_ALLOCDIRECT; |
| 1361 | adp->ad_lbn = lbn; | |
| 1362 | adp->ad_newblkno = newblkno; | |
| 1363 | adp->ad_oldblkno = oldblkno; | |
| 1364 | adp->ad_newsize = newsize; | |
| 1365 | adp->ad_oldsize = oldsize; | |
| 1366 | adp->ad_state = ATTACHED; | |
| 1367 | if (newblkno == oldblkno) | |
| 1368 | adp->ad_freefrag = NULL; | |
| 1369 | else | |
| 1370 | adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize); | |
| 1371 | ||
| 1372 | if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0) | |
| 1373 | panic("softdep_setup_allocdirect: lost block"); | |
| 1374 | ||
| 1375 | ACQUIRE_LOCK(&lk); | |
| 1376 | inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC | NODELAY, &inodedep); | |
| 1377 | adp->ad_inodedep = inodedep; | |
| 1378 | ||
| 1379 | if (newblk->nb_state == DEPCOMPLETE) { | |
| 1380 | adp->ad_state |= DEPCOMPLETE; | |
| 1381 | adp->ad_buf = NULL; | |
| 1382 | } else { | |
| 1383 | bmsafemap = newblk->nb_bmsafemap; | |
| 1384 | adp->ad_buf = bmsafemap->sm_buf; | |
| 1385 | LIST_REMOVE(newblk, nb_deps); | |
| 1386 | LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps); | |
| 1387 | } | |
| 1388 | LIST_REMOVE(newblk, nb_hash); | |
| 1389 | FREE(newblk, M_NEWBLK); | |
| 1390 | ||
| 408357d8 | 1391 | WORKLIST_INSERT_BP(bp, &adp->ad_list); |
| 984263bc MD |
1392 | if (lbn >= NDADDR) { |
| 1393 | /* allocating an indirect block */ | |
| 1394 | if (oldblkno != 0) { | |
| 1395 | FREE_LOCK(&lk); | |
| 1396 | panic("softdep_setup_allocdirect: non-zero indir"); | |
| 1397 | } | |
| 1398 | } else { | |
| 1399 | /* | |
| 1400 | * Allocating a direct block. | |
| 1401 | * | |
| 1402 | * If we are allocating a directory block, then we must | |
| 1403 | * allocate an associated pagedep to track additions and | |
| 1404 | * deletions. | |
| 1405 | */ | |
| 1406 | if ((ip->i_mode & IFMT) == IFDIR && | |
| 408357d8 MD |
1407 | pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0) { |
| 1408 | WORKLIST_INSERT_BP(bp, &pagedep->pd_list); | |
| 1409 | } | |
| 984263bc MD |
1410 | } |
| 1411 | /* | |
| 1412 | * The list of allocdirects must be kept in sorted and ascending | |
| 1413 | * order so that the rollback routines can quickly determine the | |
| 1414 | * first uncommitted block (the size of the file stored on disk | |
| 1415 | * ends at the end of the lowest committed fragment, or if there | |
| 1416 | * are no fragments, at the end of the highest committed block). | |
| 1417 | * Since files generally grow, the typical case is that the new | |
| 1418 | * block is to be added at the end of the list. We speed this | |
| 1419 | * special case by checking against the last allocdirect in the | |
| 1420 | * list before laboriously traversing the list looking for the | |
| 1421 | * insertion point. | |
| 1422 | */ | |
| 1423 | adphead = &inodedep->id_newinoupdt; | |
| 1424 | oldadp = TAILQ_LAST(adphead, allocdirectlst); | |
| 1425 | if (oldadp == NULL || oldadp->ad_lbn <= lbn) { | |
| 1426 | /* insert at end of list */ | |
| 1427 | TAILQ_INSERT_TAIL(adphead, adp, ad_next); | |
| 1428 | if (oldadp != NULL && oldadp->ad_lbn == lbn) | |
| 1429 | allocdirect_merge(adphead, adp, oldadp); | |
| 1430 | FREE_LOCK(&lk); | |
| 1431 | return; | |
| 1432 | } | |
| 1433 | TAILQ_FOREACH(oldadp, adphead, ad_next) { | |
| 1434 | if (oldadp->ad_lbn >= lbn) | |
| 1435 | break; | |
| 1436 | } | |
| 1437 | if (oldadp == NULL) { | |
| 1438 | FREE_LOCK(&lk); | |
| 1439 | panic("softdep_setup_allocdirect: lost entry"); | |
| 1440 | } | |
| 1441 | /* insert in middle of list */ | |
| 1442 | TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); | |
| 1443 | if (oldadp->ad_lbn == lbn) | |
| 1444 | allocdirect_merge(adphead, adp, oldadp); | |
| 1445 | FREE_LOCK(&lk); | |
| 1446 | } | |
| 1447 | ||
| 1448 | /* | |
| 1449 | * Replace an old allocdirect dependency with a newer one. | |
| 1450 | * This routine must be called with splbio interrupts blocked. | |
| 3fcb1ab8 SW |
1451 | * |
| 1452 | * Parameters: | |
| 1453 | * adphead: head of list holding allocdirects | |
| 1454 | * newadp: allocdirect being added | |
| 1455 | * oldadp: existing allocdirect being checked | |
| 984263bc MD |
1456 | */ |
| 1457 | static void | |
| 3fcb1ab8 SW |
1458 | allocdirect_merge(struct allocdirectlst *adphead, |
| 1459 | struct allocdirect *newadp, | |
| 1460 | struct allocdirect *oldadp) | |
| 984263bc MD |
1461 | { |
| 1462 | struct freefrag *freefrag; | |
| 1463 | ||
| 1464 | #ifdef DEBUG | |
| dadab5e9 | 1465 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
1466 | panic("allocdirect_merge: lock not held"); |
| 1467 | #endif | |
| 1468 | if (newadp->ad_oldblkno != oldadp->ad_newblkno || | |
| 1469 | newadp->ad_oldsize != oldadp->ad_newsize || | |
| 1470 | newadp->ad_lbn >= NDADDR) { | |
| 1471 | FREE_LOCK(&lk); | |
| 1472 | panic("allocdirect_check: old %d != new %d || lbn %ld >= %d", | |
| 1473 | newadp->ad_oldblkno, oldadp->ad_newblkno, newadp->ad_lbn, | |
| 1474 | NDADDR); | |
| 1475 | } | |
| 1476 | newadp->ad_oldblkno = oldadp->ad_oldblkno; | |
| 1477 | newadp->ad_oldsize = oldadp->ad_oldsize; | |
| 1478 | /* | |
| 1479 | * If the old dependency had a fragment to free or had never | |
| 1480 | * previously had a block allocated, then the new dependency | |
| 1481 | * can immediately post its freefrag and adopt the old freefrag. | |
| 1482 | * This action is done by swapping the freefrag dependencies. | |
| 1483 | * The new dependency gains the old one's freefrag, and the | |
| 1484 | * old one gets the new one and then immediately puts it on | |
| 1485 | * the worklist when it is freed by free_allocdirect. It is | |
| 1486 | * not possible to do this swap when the old dependency had a | |
| 1487 | * non-zero size but no previous fragment to free. This condition | |
| 1488 | * arises when the new block is an extension of the old block. | |
| 1489 | * Here, the first part of the fragment allocated to the new | |
| 1490 | * dependency is part of the block currently claimed on disk by | |
| 1491 | * the old dependency, so cannot legitimately be freed until the | |
| 1492 | * conditions for the new dependency are fulfilled. | |
| 1493 | */ | |
| 1494 | if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { | |
| 1495 | freefrag = newadp->ad_freefrag; | |
| 1496 | newadp->ad_freefrag = oldadp->ad_freefrag; | |
| 1497 | oldadp->ad_freefrag = freefrag; | |
| 1498 | } | |
| 1499 | free_allocdirect(adphead, oldadp, 0); | |
| 1500 | } | |
| 1501 | ||
| 1502 | /* | |
| 1503 | * Allocate a new freefrag structure if needed. | |
| 1504 | */ | |
| 1505 | static struct freefrag * | |
| 3fcb1ab8 | 1506 | newfreefrag(struct inode *ip, ufs_daddr_t blkno, long size) |
| 984263bc MD |
1507 | { |
| 1508 | struct freefrag *freefrag; | |
| 1509 | struct fs *fs; | |
| 1510 | ||
| 1511 | if (blkno == 0) | |
| 1512 | return (NULL); | |
| 1513 | fs = ip->i_fs; | |
| 1514 | if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) | |
| 1515 | panic("newfreefrag: frag size"); | |
| 1516 | MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag), | |
| 1517 | M_FREEFRAG, M_SOFTDEP_FLAGS); | |
| 1518 | freefrag->ff_list.wk_type = D_FREEFRAG; | |
| 1519 | freefrag->ff_state = ip->i_uid & ~ONWORKLIST; /* XXX - used below */ | |
| 1520 | freefrag->ff_inum = ip->i_number; | |
| 1521 | freefrag->ff_fs = fs; | |
| 1522 | freefrag->ff_devvp = ip->i_devvp; | |
| 1523 | freefrag->ff_blkno = blkno; | |
| 1524 | freefrag->ff_fragsize = size; | |
| 1525 | return (freefrag); | |
| 1526 | } | |
| 1527 | ||
| 1528 | /* | |
| 1529 | * This workitem de-allocates fragments that were replaced during | |
| 1530 | * file block allocation. | |
| 1531 | */ | |
| 1532 | static void | |
| 3fcb1ab8 | 1533 | handle_workitem_freefrag(struct freefrag *freefrag) |
| 984263bc MD |
1534 | { |
| 1535 | struct inode tip; | |
| 1536 | ||
| 1537 | tip.i_fs = freefrag->ff_fs; | |
| 1538 | tip.i_devvp = freefrag->ff_devvp; | |
| 1539 | tip.i_dev = freefrag->ff_devvp->v_rdev; | |
| 1540 | tip.i_number = freefrag->ff_inum; | |
| 1541 | tip.i_uid = freefrag->ff_state & ~ONWORKLIST; /* XXX - set above */ | |
| 1542 | ffs_blkfree(&tip, freefrag->ff_blkno, freefrag->ff_fragsize); | |
| 1543 | FREE(freefrag, M_FREEFRAG); | |
| 1544 | } | |
| 1545 | ||
| 1546 | /* | |
| 1547 | * Indirect block allocation dependencies. | |
| 1548 | * | |
| 1549 | * The same dependencies that exist for a direct block also exist when | |
| 1550 | * a new block is allocated and pointed to by an entry in a block of | |
| 1551 | * indirect pointers. The undo/redo states described above are also | |
| 1552 | * used here. Because an indirect block contains many pointers that | |
| 1553 | * may have dependencies, a second copy of the entire in-memory indirect | |
| 1554 | * block is kept. The buffer cache copy is always completely up-to-date. | |
| 1555 | * The second copy, which is used only as a source for disk writes, | |
| 1556 | * contains only the safe pointers (i.e., those that have no remaining | |
| 1557 | * update dependencies). The second copy is freed when all pointers | |
| 1558 | * are safe. The cache is not allowed to replace indirect blocks with | |
| 1559 | * pending update dependencies. If a buffer containing an indirect | |
| 1560 | * block with dependencies is written, these routines will mark it | |
| 1561 | * dirty again. It can only be successfully written once all the | |
| 1562 | * dependencies are removed. The ffs_fsync routine in conjunction with | |
| 1563 | * softdep_sync_metadata work together to get all the dependencies | |
| 1564 | * removed so that a file can be successfully written to disk. Three | |
| 1565 | * procedures are used when setting up indirect block pointer | |
| 1566 | * dependencies. The division is necessary because of the organization | |
| 1567 | * of the "balloc" routine and because of the distinction between file | |
| 1568 | * pages and file metadata blocks. | |
| 1569 | */ | |
| 1570 | ||
| 1571 | /* | |
| 1572 | * Allocate a new allocindir structure. | |
| 3fcb1ab8 SW |
1573 | * |
| 1574 | * Parameters: | |
| 1575 | * ip: inode for file being extended | |
| 1576 | * ptrno: offset of pointer in indirect block | |
| 1577 | * newblkno: disk block number being added | |
| 1578 | * oldblkno: previous block number, 0 if none | |
| 984263bc MD |
1579 | */ |
| 1580 | static struct allocindir * | |
| 3fcb1ab8 SW |
1581 | newallocindir(struct inode *ip, int ptrno, ufs_daddr_t newblkno, |
| 1582 | ufs_daddr_t oldblkno) | |
| 984263bc MD |
1583 | { |
| 1584 | struct allocindir *aip; | |
| 1585 | ||
| 1586 | MALLOC(aip, struct allocindir *, sizeof(struct allocindir), | |
| e7b4468c | 1587 | M_ALLOCINDIR, M_SOFTDEP_FLAGS | M_ZERO); |
| 984263bc MD |
1588 | aip->ai_list.wk_type = D_ALLOCINDIR; |
| 1589 | aip->ai_state = ATTACHED; | |
| 1590 | aip->ai_offset = ptrno; | |
| 1591 | aip->ai_newblkno = newblkno; | |
| 1592 | aip->ai_oldblkno = oldblkno; | |
| 1593 | aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize); | |
| 1594 | return (aip); | |
| 1595 | } | |
| 1596 | ||
| 1597 | /* | |
| 1598 | * Called just before setting an indirect block pointer | |
| 1599 | * to a newly allocated file page. | |
| 3fcb1ab8 SW |
1600 | * |
| 1601 | * Parameters: | |
| 1602 | * ip: inode for file being extended | |
| 1603 | * lbn: allocated block number within file | |
| 1604 | * bp: buffer with indirect blk referencing page | |
| 1605 | * ptrno: offset of pointer in indirect block | |
| 1606 | * newblkno: disk block number being added | |
| 1607 | * oldblkno: previous block number, 0 if none | |
| 1608 | * nbp: buffer holding allocated page | |
| 984263bc MD |
1609 | */ |
| 1610 | void | |
| 3fcb1ab8 SW |
1611 | softdep_setup_allocindir_page(struct inode *ip, ufs_lbn_t lbn, |
| 1612 | struct buf *bp, int ptrno, | |
| 1613 | ufs_daddr_t newblkno, ufs_daddr_t oldblkno, | |
| 1614 | struct buf *nbp) | |
| 984263bc MD |
1615 | { |
| 1616 | struct allocindir *aip; | |
| 1617 | struct pagedep *pagedep; | |
| 1618 | ||
| 1619 | aip = newallocindir(ip, ptrno, newblkno, oldblkno); | |
| 1620 | ACQUIRE_LOCK(&lk); | |
| 1621 | /* | |
| 1622 | * If we are allocating a directory page, then we must | |
| 1623 | * allocate an associated pagedep to track additions and | |
| 1624 | * deletions. | |
| 1625 | */ | |
| 1626 | if ((ip->i_mode & IFMT) == IFDIR && | |
| 1627 | pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0) | |
| 408357d8 MD |
1628 | WORKLIST_INSERT_BP(nbp, &pagedep->pd_list); |
| 1629 | WORKLIST_INSERT_BP(nbp, &aip->ai_list); | |
| 984263bc MD |
1630 | FREE_LOCK(&lk); |
| 1631 | setup_allocindir_phase2(bp, ip, aip); | |
| 1632 | } | |
| 1633 | ||
| 1634 | /* | |
| 1635 | * Called just before setting an indirect block pointer to a | |
| 1636 | * newly allocated indirect block. | |
| 3fcb1ab8 SW |
1637 | * Parameters: |
| 1638 | * nbp: newly allocated indirect block | |
| 1639 | * ip: inode for file being extended | |
| 1640 | * bp: indirect block referencing allocated block | |
| 1641 | * ptrno: offset of pointer in indirect block | |
| 1642 | * newblkno: disk block number being added | |
| 984263bc MD |
1643 | */ |
| 1644 | void | |
| 3fcb1ab8 SW |
1645 | softdep_setup_allocindir_meta(struct buf *nbp, struct inode *ip, |
| 1646 | struct buf *bp, int ptrno, | |
| 1647 | ufs_daddr_t newblkno) | |
| 984263bc MD |
1648 | { |
| 1649 | struct allocindir *aip; | |
| 1650 | ||
| 1651 | aip = newallocindir(ip, ptrno, newblkno, 0); | |
| 1652 | ACQUIRE_LOCK(&lk); | |
| 408357d8 | 1653 | WORKLIST_INSERT_BP(nbp, &aip->ai_list); |
| 984263bc MD |
1654 | FREE_LOCK(&lk); |
| 1655 | setup_allocindir_phase2(bp, ip, aip); | |
| 1656 | } | |
| 1657 | ||
| 1658 | /* | |
| 1659 | * Called to finish the allocation of the "aip" allocated | |
| 1660 | * by one of the two routines above. | |
| 3fcb1ab8 SW |
1661 | * |
| 1662 | * Parameters: | |
| 1663 | * bp: in-memory copy of the indirect block | |
| 1664 | * ip: inode for file being extended | |
| 1665 | * aip: allocindir allocated by the above routines | |
| 984263bc MD |
1666 | */ |
| 1667 | static void | |
| 3fcb1ab8 SW |
1668 | setup_allocindir_phase2(struct buf *bp, struct inode *ip, |
| 1669 | struct allocindir *aip) | |
| 984263bc MD |
1670 | { |
| 1671 | struct worklist *wk; | |
| 1672 | struct indirdep *indirdep, *newindirdep; | |
| 1673 | struct bmsafemap *bmsafemap; | |
| 1674 | struct allocindir *oldaip; | |
| 1675 | struct freefrag *freefrag; | |
| 1676 | struct newblk *newblk; | |
| 1677 | ||
| 54078292 | 1678 | if (bp->b_loffset >= 0) |
| 984263bc MD |
1679 | panic("setup_allocindir_phase2: not indir blk"); |
| 1680 | for (indirdep = NULL, newindirdep = NULL; ; ) { | |
| 1681 | ACQUIRE_LOCK(&lk); | |
| 1682 | LIST_FOREACH(wk, &bp->b_dep, wk_list) { | |
| 1683 | if (wk->wk_type != D_INDIRDEP) | |
| 1684 | continue; | |
| 1685 | indirdep = WK_INDIRDEP(wk); | |
| 1686 | break; | |
| 1687 | } | |
| 1688 | if (indirdep == NULL && newindirdep) { | |
| 1689 | indirdep = newindirdep; | |
| 408357d8 | 1690 | WORKLIST_INSERT_BP(bp, &indirdep->ir_list); |
| 984263bc MD |
1691 | newindirdep = NULL; |
| 1692 | } | |
| 1693 | FREE_LOCK(&lk); | |
| 1694 | if (indirdep) { | |
| 1695 | if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0, | |
| 1696 | &newblk) == 0) | |
| 1697 | panic("setup_allocindir: lost block"); | |
| 1698 | ACQUIRE_LOCK(&lk); | |
| 1699 | if (newblk->nb_state == DEPCOMPLETE) { | |
| 1700 | aip->ai_state |= DEPCOMPLETE; | |
| 1701 | aip->ai_buf = NULL; | |
| 1702 | } else { | |
| 1703 | bmsafemap = newblk->nb_bmsafemap; | |
| 1704 | aip->ai_buf = bmsafemap->sm_buf; | |
| 1705 | LIST_REMOVE(newblk, nb_deps); | |
| 1706 | LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd, | |
| 1707 | aip, ai_deps); | |
| 1708 | } | |
| 1709 | LIST_REMOVE(newblk, nb_hash); | |
| 1710 | FREE(newblk, M_NEWBLK); | |
| 1711 | aip->ai_indirdep = indirdep; | |
| 1712 | /* | |
| 1713 | * Check to see if there is an existing dependency | |
| 1714 | * for this block. If there is, merge the old | |
| 1715 | * dependency into the new one. | |
| 1716 | */ | |
| 1717 | if (aip->ai_oldblkno == 0) | |
| 1718 | oldaip = NULL; | |
| 1719 | else | |
| 1720 | ||
| 1721 | LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) | |
| 1722 | if (oldaip->ai_offset == aip->ai_offset) | |
| 1723 | break; | |
| 1724 | if (oldaip != NULL) { | |
| 1725 | if (oldaip->ai_newblkno != aip->ai_oldblkno) { | |
| 1726 | FREE_LOCK(&lk); | |
| 1727 | panic("setup_allocindir_phase2: blkno"); | |
| 1728 | } | |
| 1729 | aip->ai_oldblkno = oldaip->ai_oldblkno; | |
| 1730 | freefrag = oldaip->ai_freefrag; | |
| 1731 | oldaip->ai_freefrag = aip->ai_freefrag; | |
| 1732 | aip->ai_freefrag = freefrag; | |
| 1733 | free_allocindir(oldaip, NULL); | |
| 1734 | } | |
| 1735 | LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); | |
| 1736 | ((ufs_daddr_t *)indirdep->ir_savebp->b_data) | |
| 1737 | [aip->ai_offset] = aip->ai_oldblkno; | |
| 1738 | FREE_LOCK(&lk); | |
| 1739 | } | |
| 1740 | if (newindirdep) { | |
| 7d618503 | 1741 | /* |
| 2ae68842 MD |
1742 | * Avoid any possibility of data corruption by |
| 1743 | * ensuring that our old version is thrown away. | |
| 7d618503 | 1744 | */ |
| 2ae68842 MD |
1745 | newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE; |
| 1746 | brelse(newindirdep->ir_savebp); | |
| 984263bc MD |
1747 | WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP); |
| 1748 | } | |
| 1749 | if (indirdep) | |
| 1750 | break; | |
| 1751 | MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep), | |
| 1752 | M_INDIRDEP, M_SOFTDEP_FLAGS); | |
| 1753 | newindirdep->ir_list.wk_type = D_INDIRDEP; | |
| 1754 | newindirdep->ir_state = ATTACHED; | |
| 1755 | LIST_INIT(&newindirdep->ir_deplisthd); | |
| 1756 | LIST_INIT(&newindirdep->ir_donehd); | |
| 54078292 MD |
1757 | if (bp->b_bio2.bio_offset == NOOFFSET) { |
| 1758 | VOP_BMAP(bp->b_vp, bp->b_bio1.bio_offset, | |
| e92ca23a MD |
1759 | &bp->b_bio2.bio_offset, NULL, NULL, |
| 1760 | BUF_CMD_WRITE); | |
| 984263bc | 1761 | } |
| 54078292 MD |
1762 | KKASSERT(bp->b_bio2.bio_offset != NOOFFSET); |
| 1763 | newindirdep->ir_savebp = getblk(ip->i_devvp, | |
| 1764 | bp->b_bio2.bio_offset, | |
| 1765 | bp->b_bcount, 0, 0); | |
| 984263bc MD |
1766 | BUF_KERNPROC(newindirdep->ir_savebp); |
| 1767 | bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); | |
| 1768 | } | |
| 1769 | } | |
| 1770 | ||
| 1771 | /* | |
| 1772 | * Block de-allocation dependencies. | |
| 1773 | * | |
| 1774 | * When blocks are de-allocated, the on-disk pointers must be nullified before | |
| 1775 | * the blocks are made available for use by other files. (The true | |
| 1776 | * requirement is that old pointers must be nullified before new on-disk | |
| 1777 | * pointers are set. We chose this slightly more stringent requirement to | |
| 1778 | * reduce complexity.) Our implementation handles this dependency by updating | |
| 1779 | * the inode (or indirect block) appropriately but delaying the actual block | |
| 1780 | * de-allocation (i.e., freemap and free space count manipulation) until | |
| 1781 | * after the updated versions reach stable storage. After the disk is | |
| 1782 | * updated, the blocks can be safely de-allocated whenever it is convenient. | |
| 1783 | * This implementation handles only the common case of reducing a file's | |
| 1784 | * length to zero. Other cases are handled by the conventional synchronous | |
| 1785 | * write approach. | |
| 1786 | * | |
| 1787 | * The ffs implementation with which we worked double-checks | |
| 1788 | * the state of the block pointers and file size as it reduces | |
| 1789 | * a file's length. Some of this code is replicated here in our | |
| 1790 | * soft updates implementation. The freeblks->fb_chkcnt field is | |
| 1791 | * used to transfer a part of this information to the procedure | |
| 1792 | * that eventually de-allocates the blocks. | |
| 1793 | * | |
| 1794 | * This routine should be called from the routine that shortens | |
| 1795 | * a file's length, before the inode's size or block pointers | |
| 1796 | * are modified. It will save the block pointer information for | |
| 1797 | * later release and zero the inode so that the calling routine | |
| 1798 | * can release it. | |
| 1799 | */ | |
| 6bae6177 MD |
1800 | struct softdep_setup_freeblocks_info { |
| 1801 | struct fs *fs; | |
| 1802 | struct inode *ip; | |
| 1803 | }; | |
| 1804 | ||
| 1805 | static int softdep_setup_freeblocks_bp(struct buf *bp, void *data); | |
| 1806 | ||
| 3fcb1ab8 SW |
1807 | /* |
| 1808 | * Parameters: | |
| 1809 | * ip: The inode whose length is to be reduced | |
| 1810 | * length: The new length for the file | |
| 1811 | */ | |
| 984263bc | 1812 | void |
| 3fcb1ab8 | 1813 | softdep_setup_freeblocks(struct inode *ip, off_t length) |
| 984263bc | 1814 | { |
| 6bae6177 | 1815 | struct softdep_setup_freeblocks_info info; |
| 984263bc MD |
1816 | struct freeblks *freeblks; |
| 1817 | struct inodedep *inodedep; | |
| 1818 | struct allocdirect *adp; | |
| 1819 | struct vnode *vp; | |
| 1820 | struct buf *bp; | |
| 1821 | struct fs *fs; | |
| 1822 | int i, error, delay; | |
| 6bae6177 | 1823 | int count; |
| 984263bc MD |
1824 | |
| 1825 | fs = ip->i_fs; | |
| 1826 | if (length != 0) | |
| 1827 | panic("softde_setup_freeblocks: non-zero length"); | |
| 1828 | MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks), | |
| e7b4468c | 1829 | M_FREEBLKS, M_SOFTDEP_FLAGS | M_ZERO); |
| 984263bc | 1830 | freeblks->fb_list.wk_type = D_FREEBLKS; |
| 89a5de29 | 1831 | freeblks->fb_state = ATTACHED; |
| 984263bc MD |
1832 | freeblks->fb_uid = ip->i_uid; |
| 1833 | freeblks->fb_previousinum = ip->i_number; | |
| 1834 | freeblks->fb_devvp = ip->i_devvp; | |
| 1835 | freeblks->fb_fs = fs; | |
| 1836 | freeblks->fb_oldsize = ip->i_size; | |
| 1837 | freeblks->fb_newsize = length; | |
| 1838 | freeblks->fb_chkcnt = ip->i_blocks; | |
| 1839 | for (i = 0; i < NDADDR; i++) { | |
| 1840 | freeblks->fb_dblks[i] = ip->i_db[i]; | |
| 1841 | ip->i_db[i] = 0; | |
| 1842 | } | |
| 1843 | for (i = 0; i < NIADDR; i++) { | |
| 1844 | freeblks->fb_iblks[i] = ip->i_ib[i]; | |
| 1845 | ip->i_ib[i] = 0; | |
| 1846 | } | |
| 1847 | ip->i_blocks = 0; | |
| 1848 | ip->i_size = 0; | |
| 1849 | /* | |
| 1850 | * Push the zero'ed inode to to its disk buffer so that we are free | |
| 1851 | * to delete its dependencies below. Once the dependencies are gone | |
| 1852 | * the buffer can be safely released. | |
| 1853 | */ | |
| 1854 | if ((error = bread(ip->i_devvp, | |
| 54078292 | 1855 | fsbtodoff(fs, ino_to_fsba(fs, ip->i_number)), |
| 3b568787 | 1856 | (int)fs->fs_bsize, &bp)) != 0) |
| 984263bc | 1857 | softdep_error("softdep_setup_freeblocks", error); |
| 50e58362 | 1858 | *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)) = |
| 984263bc MD |
1859 | ip->i_din; |
| 1860 | /* | |
| 1861 | * Find and eliminate any inode dependencies. | |
| 1862 | */ | |
| 1863 | ACQUIRE_LOCK(&lk); | |
| 1864 | (void) inodedep_lookup(fs, ip->i_number, DEPALLOC, &inodedep); | |
| 1865 | if ((inodedep->id_state & IOSTARTED) != 0) { | |
| 1866 | FREE_LOCK(&lk); | |
| 1867 | panic("softdep_setup_freeblocks: inode busy"); | |
| 1868 | } | |
| 1869 | /* | |
| 1870 | * Add the freeblks structure to the list of operations that | |
| 1871 | * must await the zero'ed inode being written to disk. If we | |
| 1872 | * still have a bitmap dependency (delay == 0), then the inode | |
| 1873 | * has never been written to disk, so we can process the | |
| 1874 | * freeblks below once we have deleted the dependencies. | |
| 1875 | */ | |
| 1876 | delay = (inodedep->id_state & DEPCOMPLETE); | |
| 1877 | if (delay) | |
| 1878 | WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list); | |
| 1879 | /* | |
| 1880 | * Because the file length has been truncated to zero, any | |
| 1881 | * pending block allocation dependency structures associated | |
| 1882 | * with this inode are obsolete and can simply be de-allocated. | |
| 1883 | * We must first merge the two dependency lists to get rid of | |
| 1884 | * any duplicate freefrag structures, then purge the merged list. | |
| 1885 | */ | |
| 1886 | merge_inode_lists(inodedep); | |
| 1887 | while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) | |
| 1888 | free_allocdirect(&inodedep->id_inoupdt, adp, 1); | |
| 1889 | FREE_LOCK(&lk); | |
| 1890 | bdwrite(bp); | |
| 1891 | /* | |
| 1892 | * We must wait for any I/O in progress to finish so that | |
| 1893 | * all potential buffers on the dirty list will be visible. | |
| 1894 | * Once they are all there, walk the list and get rid of | |
| 1895 | * any dependencies. | |
| 1896 | */ | |
| 1897 | vp = ITOV(ip); | |
| 1898 | ACQUIRE_LOCK(&lk); | |
| 1899 | drain_output(vp, 1); | |
| 6bae6177 MD |
1900 | |
| 1901 | info.fs = fs; | |
| 1902 | info.ip = ip; | |
| 3b998fa9 | 1903 | lwkt_gettoken(&vp->v_token); |
| 6bae6177 MD |
1904 | do { |
| 1905 | count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, | |
| 1906 | softdep_setup_freeblocks_bp, &info); | |
| 2ef16e04 | 1907 | } while (count != 0); |
| 3b998fa9 | 1908 | lwkt_reltoken(&vp->v_token); |
| 0202303b | 1909 | |
| 984263bc MD |
1910 | if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) != 0) |
| 1911 | (void)free_inodedep(inodedep); | |
| 89a5de29 MD |
1912 | |
| 1913 | if (delay) { | |
| 1914 | freeblks->fb_state |= DEPCOMPLETE; | |
| 1915 | /* | |
| 1916 | * If the inode with zeroed block pointers is now on disk | |
| 1917 | * we can start freeing blocks. Add freeblks to the worklist | |
| 1918 | * instead of calling handle_workitem_freeblocks directly as | |
| 1919 | * it is more likely that additional IO is needed to complete | |
| 1920 | * the request here than in the !delay case. | |
| 1921 | */ | |
| 1922 | if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) | |
| 1923 | add_to_worklist(&freeblks->fb_list); | |
| 1924 | } | |
| 1925 | ||
| 984263bc MD |
1926 | FREE_LOCK(&lk); |
| 1927 | /* | |
| 1928 | * If the inode has never been written to disk (delay == 0), | |
| 1929 | * then we can process the freeblks now that we have deleted | |
| 1930 | * the dependencies. | |
| 1931 | */ | |
| 1932 | if (!delay) | |
| 1933 | handle_workitem_freeblocks(freeblks); | |
| 1934 | } | |
| 1935 | ||
| 6bae6177 MD |
1936 | static int |
| 1937 | softdep_setup_freeblocks_bp(struct buf *bp, void *data) | |
| 1938 | { | |
| 1939 | struct softdep_setup_freeblocks_info *info = data; | |
| 1940 | struct inodedep *inodedep; | |
| 1941 | ||
| 2ef16e04 | 1942 | if (getdirtybuf(&bp, MNT_WAIT) == 0) { |
| 086c1d7e | 1943 | kprintf("softdep_setup_freeblocks_bp(1): caught bp %p going away\n", bp); |
| 6bae6177 | 1944 | return(-1); |
| 2ef16e04 MD |
1945 | } |
| 1946 | if (bp->b_vp != ITOV(info->ip) || (bp->b_flags & B_DELWRI) == 0) { | |
| 086c1d7e | 1947 | kprintf("softdep_setup_freeblocks_bp(2): caught bp %p going away\n", bp); |
| 2ef16e04 MD |
1948 | BUF_UNLOCK(bp); |
| 1949 | return(-1); | |
| 1950 | } | |
| 6bae6177 MD |
1951 | (void) inodedep_lookup(info->fs, info->ip->i_number, 0, &inodedep); |
| 1952 | deallocate_dependencies(bp, inodedep); | |
| 1953 | bp->b_flags |= B_INVAL | B_NOCACHE; | |
| 1954 | FREE_LOCK(&lk); | |
| 1955 | brelse(bp); | |
| 1956 | ACQUIRE_LOCK(&lk); | |
| 1957 | return(1); | |
| 1958 | } | |
| 1959 | ||
| 984263bc MD |
1960 | /* |
| 1961 | * Reclaim any dependency structures from a buffer that is about to | |
| 1962 | * be reallocated to a new vnode. The buffer must be locked, thus, | |
| 1963 | * no I/O completion operations can occur while we are manipulating | |
| 1964 | * its associated dependencies. The mutex is held so that other I/O's | |
| 1965 | * associated with related dependencies do not occur. | |
| 1966 | */ | |
| 1967 | static void | |
| 3fcb1ab8 | 1968 | deallocate_dependencies(struct buf *bp, struct inodedep *inodedep) |
| 984263bc MD |
1969 | { |
| 1970 | struct worklist *wk; | |
| 1971 | struct indirdep *indirdep; | |
| 1972 | struct allocindir *aip; | |
| 1973 | struct pagedep *pagedep; | |
| 1974 | struct dirrem *dirrem; | |
| 1975 | struct diradd *dap; | |
| 1976 | int i; | |
| 1977 | ||
| 1978 | while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { | |
| 1979 | switch (wk->wk_type) { | |
| 1980 | ||
| 1981 | case D_INDIRDEP: | |
| 1982 | indirdep = WK_INDIRDEP(wk); | |
| 1983 | /* | |
| 1984 | * None of the indirect pointers will ever be visible, | |
| 1985 | * so they can simply be tossed. GOINGAWAY ensures | |
| 1986 | * that allocated pointers will be saved in the buffer | |
| 1987 | * cache until they are freed. Note that they will | |
| 1988 | * only be able to be found by their physical address | |
| 1989 | * since the inode mapping the logical address will | |
| 1990 | * be gone. The save buffer used for the safe copy | |
| 1991 | * was allocated in setup_allocindir_phase2 using | |
| 1992 | * the physical address so it could be used for this | |
| 1993 | * purpose. Hence we swap the safe copy with the real | |
| 1994 | * copy, allowing the safe copy to be freed and holding | |
| 1995 | * on to the real copy for later use in indir_trunc. | |
| 81b5c339 MD |
1996 | * |
| 1997 | * NOTE: ir_savebp is relative to the block device | |
| 1998 | * so b_bio1 contains the device block number. | |
| 984263bc MD |
1999 | */ |
| 2000 | if (indirdep->ir_state & GOINGAWAY) { | |
| 2001 | FREE_LOCK(&lk); | |
| 2002 | panic("deallocate_dependencies: already gone"); | |
| 2003 | } | |
| 2004 | indirdep->ir_state |= GOINGAWAY; | |
| 2005 | while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) | |
| 2006 | free_allocindir(aip, inodedep); | |
| 54078292 MD |
2007 | if (bp->b_bio1.bio_offset >= 0 || |
| 2008 | bp->b_bio2.bio_offset != indirdep->ir_savebp->b_bio1.bio_offset) { | |
| 984263bc MD |
2009 | FREE_LOCK(&lk); |
| 2010 | panic("deallocate_dependencies: not indir"); | |
| 2011 | } | |
| 2012 | bcopy(bp->b_data, indirdep->ir_savebp->b_data, | |
| 2013 | bp->b_bcount); | |
| 2014 | WORKLIST_REMOVE(wk); | |
| 408357d8 | 2015 | WORKLIST_INSERT_BP(indirdep->ir_savebp, wk); |
| 984263bc MD |
2016 | continue; |
| 2017 | ||
| 2018 | case D_PAGEDEP: | |
| 2019 | pagedep = WK_PAGEDEP(wk); | |
| 2020 | /* | |
| 2021 | * None of the directory additions will ever be | |
| 2022 | * visible, so they can simply be tossed. | |
| 2023 | */ | |
| 2024 | for (i = 0; i < DAHASHSZ; i++) | |
| 2025 | while ((dap = | |
| 2026 | LIST_FIRST(&pagedep->pd_diraddhd[i]))) | |
| 2027 | free_diradd(dap); | |
| 2028 | while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0) | |
| 2029 | free_diradd(dap); | |
| 2030 | /* | |
| 2031 | * Copy any directory remove dependencies to the list | |
| 2032 | * to be processed after the zero'ed inode is written. | |
| 2033 | * If the inode has already been written, then they | |
| 2034 | * can be dumped directly onto the work list. | |
| 2035 | */ | |
| 2036 | LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { | |
| 2037 | LIST_REMOVE(dirrem, dm_next); | |
| 2038 | dirrem->dm_dirinum = pagedep->pd_ino; | |
| 2039 | if (inodedep == NULL || | |
| 2040 | (inodedep->id_state & ALLCOMPLETE) == | |
| 2041 | ALLCOMPLETE) | |
| 2042 | add_to_worklist(&dirrem->dm_list); | |
| 2043 | else | |
| 2044 | WORKLIST_INSERT(&inodedep->id_bufwait, | |
| 2045 | &dirrem->dm_list); | |
| 2046 | } | |
| 2047 | WORKLIST_REMOVE(&pagedep->pd_list); | |
| 2048 | LIST_REMOVE(pagedep, pd_hash); | |
| 2049 | WORKITEM_FREE(pagedep, D_PAGEDEP); | |
| 2050 | continue; | |
| 2051 | ||
| 2052 | case D_ALLOCINDIR: | |
| 2053 | free_allocindir(WK_ALLOCINDIR(wk), inodedep); | |
| 2054 | continue; | |
| 2055 | ||
| 2056 | case D_ALLOCDIRECT: | |
| 2057 | case D_INODEDEP: | |
| 2058 | FREE_LOCK(&lk); | |
| 2059 | panic("deallocate_dependencies: Unexpected type %s", | |
| 2060 | TYPENAME(wk->wk_type)); | |
| 2061 | /* NOTREACHED */ | |
| 2062 | ||
| 2063 | default: | |
| 2064 | FREE_LOCK(&lk); | |
| 2065 | panic("deallocate_dependencies: Unknown type %s", | |
| 2066 | TYPENAME(wk->wk_type)); | |
| 2067 | /* NOTREACHED */ | |
| 2068 | } | |
| 2069 | } | |
| 2070 | } | |
| 2071 | ||
| 2072 | /* | |
| 2073 | * Free an allocdirect. Generate a new freefrag work request if appropriate. | |
| 2074 | * This routine must be called with splbio interrupts blocked. | |
| 2075 | */ | |
| 2076 | static void | |
| 3fcb1ab8 SW |
2077 | free_allocdirect(struct allocdirectlst *adphead, |
| 2078 | struct allocdirect *adp, int delay) | |
| 984263bc MD |
2079 | { |
| 2080 | ||
| 2081 | #ifdef DEBUG | |
| dadab5e9 | 2082 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
2083 | panic("free_allocdirect: lock not held"); |
| 2084 | #endif | |
| 2085 | if ((adp->ad_state & DEPCOMPLETE) == 0) | |
| 2086 | LIST_REMOVE(adp, ad_deps); | |
| 2087 | TAILQ_REMOVE(adphead, adp, ad_next); | |
| 2088 | if ((adp->ad_state & COMPLETE) == 0) | |
| 2089 | WORKLIST_REMOVE(&adp->ad_list); | |
| 2090 | if (adp->ad_freefrag != NULL) { | |
| 2091 | if (delay) | |
| 2092 | WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait, | |
| 2093 | &adp->ad_freefrag->ff_list); | |
| 2094 | else | |
| 2095 | add_to_worklist(&adp->ad_freefrag->ff_list); | |
| 2096 | } | |
| 2097 | WORKITEM_FREE(adp, D_ALLOCDIRECT); | |
| 2098 | } | |
| 2099 | ||
| 2100 | /* | |
| 2101 | * Prepare an inode to be freed. The actual free operation is not | |
| 2102 | * done until the zero'ed inode has been written to disk. | |
| 2103 | */ | |
| 2104 | void | |
| 3fcb1ab8 | 2105 | softdep_freefile(struct vnode *pvp, ino_t ino, int mode) |
| 984263bc MD |
2106 | { |
| 2107 | struct inode *ip = VTOI(pvp); | |
| 2108 | struct inodedep *inodedep; | |
| 2109 | struct freefile *freefile; | |
| 2110 | ||
| 2111 | /* | |
| 2112 | * This sets up the inode de-allocation dependency. | |
| 2113 | */ | |
| 2114 | MALLOC(freefile, struct freefile *, sizeof(struct freefile), | |
| 2115 | M_FREEFILE, M_SOFTDEP_FLAGS); | |
| 2116 | freefile->fx_list.wk_type = D_FREEFILE; | |
| 2117 | freefile->fx_list.wk_state = 0; | |
| 2118 | freefile->fx_mode = mode; | |
| 2119 | freefile->fx_oldinum = ino; | |
| 2120 | freefile->fx_devvp = ip->i_devvp; | |
| 2121 | freefile->fx_fs = ip->i_fs; | |
| 2122 | ||
| 2123 | /* | |
| 2124 | * If the inodedep does not exist, then the zero'ed inode has | |
| 2125 | * been written to disk. If the allocated inode has never been | |
| 2126 | * written to disk, then the on-disk inode is zero'ed. In either | |
| 2127 | * case we can free the file immediately. | |
| 2128 | */ | |
| 2129 | ACQUIRE_LOCK(&lk); | |
| 2130 | if (inodedep_lookup(ip->i_fs, ino, 0, &inodedep) == 0 || | |
| 2131 | check_inode_unwritten(inodedep)) { | |
| 2132 | FREE_LOCK(&lk); | |
| 2133 | handle_workitem_freefile(freefile); | |
| 2134 | return; | |
| 2135 | } | |
| 2136 | WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); | |
| 2137 | FREE_LOCK(&lk); | |
| 2138 | } | |
| 2139 | ||
| 2140 | /* | |
| 2141 | * Check to see if an inode has never been written to disk. If | |
| 2142 | * so free the inodedep and return success, otherwise return failure. | |
| 2143 | * This routine must be called with splbio interrupts blocked. | |
| 2144 | * | |
| 2145 | * If we still have a bitmap dependency, then the inode has never | |
| 2146 | * been written to disk. Drop the dependency as it is no longer | |
| 2147 | * necessary since the inode is being deallocated. We set the | |
| 2148 | * ALLCOMPLETE flags since the bitmap now properly shows that the | |
| 2149 | * inode is not allocated. Even if the inode is actively being | |
| 2150 | * written, it has been rolled back to its zero'ed state, so we | |
| 2151 | * are ensured that a zero inode is what is on the disk. For short | |
| 2152 | * lived files, this change will usually result in removing all the | |
| 2153 | * dependencies from the inode so that it can be freed immediately. | |
| 2154 | */ | |
| 2155 | static int | |
| 3fcb1ab8 | 2156 | check_inode_unwritten(struct inodedep *inodedep) |
| 984263bc MD |
2157 | { |
| 2158 | ||
| 2159 | if ((inodedep->id_state & DEPCOMPLETE) != 0 || | |
| 2160 | LIST_FIRST(&inodedep->id_pendinghd) != NULL || | |
| 2161 | LIST_FIRST(&inodedep->id_bufwait) != NULL || | |
| 2162 | LIST_FIRST(&inodedep->id_inowait) != NULL || | |
| 2163 | TAILQ_FIRST(&inodedep->id_inoupdt) != NULL || | |
| 2164 | TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL || | |
| 2165 | inodedep->id_nlinkdelta != 0) | |
| 2166 | return (0); | |
| 83b29fff MD |
2167 | |
| 2168 | /* | |
| 2169 | * Another process might be in initiate_write_inodeblock | |
| 2170 | * trying to allocate memory without holding "Softdep Lock". | |
| 2171 | */ | |
| 2172 | if ((inodedep->id_state & IOSTARTED) != 0 && | |
| 2173 | inodedep->id_savedino == NULL) | |
| 2174 | return(0); | |
| 2175 | ||
| 984263bc MD |
2176 | inodedep->id_state |= ALLCOMPLETE; |
| 2177 | LIST_REMOVE(inodedep, id_deps); | |
| 2178 | inodedep->id_buf = NULL; | |
| 2179 | if (inodedep->id_state & ONWORKLIST) | |
| 2180 | WORKLIST_REMOVE(&inodedep->id_list); | |
| 2181 | if (inodedep->id_savedino != NULL) { | |
| 2182 | FREE(inodedep->id_savedino, M_INODEDEP); | |
| 2183 | inodedep->id_savedino = NULL; | |
| 2184 | } | |
| 2185 | if (free_inodedep(inodedep) == 0) { | |
| 2186 | FREE_LOCK(&lk); | |
| 2187 | panic("check_inode_unwritten: busy inode"); | |
| 2188 | } | |
| 2189 | return (1); | |
| 2190 | } | |
| 2191 | ||
| 2192 | /* | |
| 2193 | * Try to free an inodedep structure. Return 1 if it could be freed. | |
| 2194 | */ | |
| 2195 | static int | |
| 3fcb1ab8 | 2196 | free_inodedep(struct inodedep *inodedep) |
| 984263bc MD |
2197 | { |
| 2198 | ||
| 2199 | if ((inodedep->id_state & ONWORKLIST) != 0 || | |
| 2200 | (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || | |
| 2201 | LIST_FIRST(&inodedep->id_pendinghd) != NULL || | |
| 2202 | LIST_FIRST(&inodedep->id_bufwait) != NULL || | |
| 2203 | LIST_FIRST(&inodedep->id_inowait) != NULL || | |
| 2204 | TAILQ_FIRST(&inodedep->id_inoupdt) != NULL || | |
| 2205 | TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL || | |
| 2206 | inodedep->id_nlinkdelta != 0 || inodedep->id_savedino != NULL) | |
| 2207 | return (0); | |
| 2208 | LIST_REMOVE(inodedep, id_hash); | |
| 2209 | WORKITEM_FREE(inodedep, D_INODEDEP); | |
| 2210 | num_inodedep -= 1; | |
| 2211 | return (1); | |
| 2212 | } | |
| 2213 | ||
| 2214 | /* | |
| 2215 | * This workitem routine performs the block de-allocation. | |
| 2216 | * The workitem is added to the pending list after the updated | |
| 2217 | * inode block has been written to disk. As mentioned above, | |
| 2218 | * checks regarding the number of blocks de-allocated (compared | |
| 2219 | * to the number of blocks allocated for the file) are also | |
| 2220 | * performed in this function. | |
| 2221 | */ | |
| 2222 | static void | |
| 3fcb1ab8 | 2223 | handle_workitem_freeblocks(struct freeblks *freeblks) |
| 984263bc MD |
2224 | { |
| 2225 | struct inode tip; | |
| 2226 | ufs_daddr_t bn; | |
| 2227 | struct fs *fs; | |
| 2228 | int i, level, bsize; | |
| 2229 | long nblocks, blocksreleased = 0; | |
| 2230 | int error, allerror = 0; | |
| 2231 | ufs_lbn_t baselbns[NIADDR], tmpval; | |
| 2232 | ||
| 2233 | tip.i_number = freeblks->fb_previousinum; | |
| 2234 | tip.i_devvp = freeblks->fb_devvp; | |
| 2235 | tip.i_dev = freeblks->fb_devvp->v_rdev; | |
| 2236 | tip.i_fs = freeblks->fb_fs; | |
| 2237 | tip.i_size = freeblks->fb_oldsize; | |
| 2238 | tip.i_uid = freeblks->fb_uid; | |
| 2239 | fs = freeblks->fb_fs; | |
| 2240 | tmpval = 1; | |
| 2241 | baselbns[0] = NDADDR; | |
| 2242 | for (i = 1; i < NIADDR; i++) { | |
| 2243 | tmpval *= NINDIR(fs); | |
| 2244 | baselbns[i] = baselbns[i - 1] + tmpval; | |
| 2245 | } | |
| 2246 | nblocks = btodb(fs->fs_bsize); | |
| 2247 | blocksreleased = 0; | |
| 2248 | /* | |
| 2249 | * Indirect blocks first. | |
| 2250 | */ | |
| 2251 | for (level = (NIADDR - 1); level >= 0; level--) { | |
| 2252 | if ((bn = freeblks->fb_iblks[level]) == 0) | |
| 2253 | continue; | |
| 54078292 | 2254 | if ((error = indir_trunc(&tip, fsbtodoff(fs, bn), level, |
| 984263bc MD |
2255 | baselbns[level], &blocksreleased)) == 0) |
| 2256 | allerror = error; | |
| 2257 | ffs_blkfree(&tip, bn, fs->fs_bsize); | |
| 2258 | blocksreleased += nblocks; | |
| 2259 | } | |
| 2260 | /* | |
| 2261 | * All direct blocks or frags. | |
| 2262 | */ | |
| 2263 | for (i = (NDADDR - 1); i >= 0; i--) { | |
| 2264 | if ((bn = freeblks->fb_dblks[i]) == 0) | |
| 2265 | continue; | |
| 2266 | bsize = blksize(fs, &tip, i); | |
| 2267 | ffs_blkfree(&tip, bn, bsize); | |
| 2268 | blocksreleased += btodb(bsize); | |
| 2269 | } | |
| 2270 | ||
| 2271 | #ifdef DIAGNOSTIC | |
| 2272 | if (freeblks->fb_chkcnt != blocksreleased) | |
| 086c1d7e | 2273 | kprintf("handle_workitem_freeblocks: block count\n"); |
| 984263bc MD |
2274 | if (allerror) |
| 2275 | softdep_error("handle_workitem_freeblks", allerror); | |
| 2276 | #endif /* DIAGNOSTIC */ | |
| 2277 | WORKITEM_FREE(freeblks, D_FREEBLKS); | |
| 2278 | } | |
| 2279 | ||
| 2280 | /* | |
| 2281 | * Release blocks associated with the inode ip and stored in the indirect | |
| 54078292 MD |
2282 | * block at doffset. If level is greater than SINGLE, the block is an |
| 2283 | * indirect block and recursive calls to indirtrunc must be used to | |
| 2284 | * cleanse other indirect blocks. | |
| 984263bc MD |
2285 | */ |
| 2286 | static int | |
| 3fcb1ab8 SW |
2287 | indir_trunc(struct inode *ip, off_t doffset, int level, ufs_lbn_t lbn, |
| 2288 | long *countp) | |
| 984263bc MD |
2289 | { |
| 2290 | struct buf *bp; | |
| 2291 | ufs_daddr_t *bap; | |
| 2292 | ufs_daddr_t nb; | |
| 2293 | struct fs *fs; | |
| 2294 | struct worklist *wk; | |
| 2295 | struct indirdep *indirdep; | |
| 2296 | int i, lbnadd, nblocks; | |
| 2297 | int error, allerror = 0; | |
| 2298 | ||
| 2299 | fs = ip->i_fs; | |
| 2300 | lbnadd = 1; | |
| 2301 | for (i = level; i > 0; i--) | |
| 2302 | lbnadd *= NINDIR(fs); | |
| 2303 | /* | |
| 2304 | * Get buffer of block pointers to be freed. This routine is not | |
| 2305 | * called until the zero'ed inode has been written, so it is safe | |
| 2306 | * to free blocks as they are encountered. Because the inode has | |
| 2307 | * been zero'ed, calls to bmap on these blocks will fail. So, we | |
| 2308 | * have to use the on-disk address and the block device for the | |
| 2309 | * filesystem to look them up. If the file was deleted before its | |
| 2310 | * indirect blocks were all written to disk, the routine that set | |
| 2311 | * us up (deallocate_dependencies) will have arranged to leave | |
| 2312 | * a complete copy of the indirect block in memory for our use. | |
| 2313 | * Otherwise we have to read the blocks in from the disk. | |
| 2314 | */ | |
| 2315 | ACQUIRE_LOCK(&lk); | |
| b1c20cfa | 2316 | if ((bp = findblk(ip->i_devvp, doffset, FINDBLK_TEST)) != NULL && |
| 984263bc | 2317 | (wk = LIST_FIRST(&bp->b_dep)) != NULL) { |
| 1f1ea522 MD |
2318 | /* |
| 2319 | * bp must be ir_savebp, which is held locked for our use. | |
| 2320 | */ | |
| 984263bc MD |
2321 | if (wk->wk_type != D_INDIRDEP || |
| 2322 | (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp || | |
| 2323 | (indirdep->ir_state & GOINGAWAY) == 0) { | |
| 2324 | FREE_LOCK(&lk); | |
| 2325 | panic("indir_trunc: lost indirdep"); | |
| 2326 | } | |
| 2327 | WORKLIST_REMOVE(wk); | |
| 2328 | WORKITEM_FREE(indirdep, D_INDIRDEP); | |
| 2329 | if (LIST_FIRST(&bp->b_dep) != NULL) { | |
| 2330 | FREE_LOCK(&lk); | |
| 2331 | panic("indir_trunc: dangling dep"); | |
| 2332 | } | |
| 2333 | FREE_LOCK(&lk); | |
| 2334 | } else { | |
| 2335 | FREE_LOCK(&lk); | |
| 54078292 | 2336 | error = bread(ip->i_devvp, doffset, (int)fs->fs_bsize, &bp); |
| 984263bc MD |
2337 | if (error) |
| 2338 | return (error); | |
| 2339 | } | |
| 2340 | /* | |
| 2341 | * Recursively free indirect blocks. | |
| 2342 | */ | |
| 2343 | bap = (ufs_daddr_t *)bp->b_data; | |
| 2344 | nblocks = btodb(fs->fs_bsize); | |
| 2345 | for (i = NINDIR(fs) - 1; i >= 0; i--) { | |
| 2346 | if ((nb = bap[i]) == 0) | |
| 2347 | continue; | |
| 2348 | if (level != 0) { | |
| 54078292 | 2349 | if ((error = indir_trunc(ip, fsbtodoff(fs, nb), |
| 984263bc MD |
2350 | level - 1, lbn + (i * lbnadd), countp)) != 0) |
| 2351 | allerror = error; | |
| 2352 | } | |
| 2353 | ffs_blkfree(ip, nb, fs->fs_bsize); | |
| 2354 | *countp += nblocks; | |
| 2355 | } | |
| 2356 | bp->b_flags |= B_INVAL | B_NOCACHE; | |
| 2357 | brelse(bp); | |
| 2358 | return (allerror); | |
| 2359 | } | |
| 2360 | ||
| 2361 | /* | |
| 2362 | * Free an allocindir. | |
| 2363 | * This routine must be called with splbio interrupts blocked. | |
| 2364 | */ | |
| 2365 | static void | |
| 3fcb1ab8 | 2366 | free_allocindir(struct allocindir *aip, struct inodedep *inodedep) |
| 984263bc MD |
2367 | { |
| 2368 | struct freefrag *freefrag; | |
| 2369 | ||
| 2370 | #ifdef DEBUG | |
| dadab5e9 | 2371 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
2372 | panic("free_allocindir: lock not held"); |
| 2373 | #endif | |
| 2374 | if ((aip->ai_state & DEPCOMPLETE) == 0) | |
| 2375 | LIST_REMOVE(aip, ai_deps); | |
| 2376 | if (aip->ai_state & ONWORKLIST) | |
| 2377 | WORKLIST_REMOVE(&aip->ai_list); | |
| 2378 | LIST_REMOVE(aip, ai_next); | |
| 2379 | if ((freefrag = aip->ai_freefrag) != NULL) { | |
| 2380 | if (inodedep == NULL) | |
| 2381 | add_to_worklist(&freefrag->ff_list); | |
| 2382 | else | |
| 2383 | WORKLIST_INSERT(&inodedep->id_bufwait, | |
| 2384 | &freefrag->ff_list); | |
| 2385 | } | |
| 2386 | WORKITEM_FREE(aip, D_ALLOCINDIR); | |
| 2387 | } | |
| 2388 | ||
| 2389 | /* | |
| 2390 | * Directory entry addition dependencies. | |
| 2391 | * | |
| 2392 | * When adding a new directory entry, the inode (with its incremented link | |
| 2393 | * count) must be written to disk before the directory entry's pointer to it. | |
| 2394 | * Also, if the inode is newly allocated, the corresponding freemap must be | |
| 2395 | * updated (on disk) before the directory entry's pointer. These requirements | |
| 2396 | * are met via undo/redo on the directory entry's pointer, which consists | |
| 2397 | * simply of the inode number. | |
| 2398 | * | |
| 2399 | * As directory entries are added and deleted, the free space within a | |
| f719c866 | 2400 | * directory block can become fragmented. The ufs filesystem will compact |
| 984263bc MD |
2401 | * a fragmented directory block to make space for a new entry. When this |
| 2402 | * occurs, the offsets of previously added entries change. Any "diradd" | |
| 2403 | * dependency structures corresponding to these entries must be updated with | |
| 2404 | * the new offsets. | |
| 2405 | */ | |
| 2406 | ||
| 2407 | /* | |
| 2408 | * This routine is called after the in-memory inode's link | |
| 2409 | * count has been incremented, but before the directory entry's | |
| 2410 | * pointer to the inode has been set. | |
| 3fcb1ab8 SW |
2411 | * |
| 2412 | * Parameters: | |
| 2413 | * bp: buffer containing directory block | |
| 2414 | * dp: inode for directory | |
| 2415 | * diroffset: offset of new entry in directory | |
| 2416 | * newinum: inode referenced by new directory entry | |
| 2417 | * newdirbp: non-NULL => contents of new mkdir | |
| 984263bc MD |
2418 | */ |
| 2419 | void | |
| 3fcb1ab8 SW |
2420 | softdep_setup_directory_add(struct buf *bp, struct inode *dp, off_t diroffset, |
| 2421 | ino_t newinum, struct buf *newdirbp) | |
| 984263bc MD |
2422 | { |
| 2423 | int offset; /* offset of new entry within directory block */ | |
| 2424 | ufs_lbn_t lbn; /* block in directory containing new entry */ | |
| 2425 | struct fs *fs; | |
| 2426 | struct diradd *dap; | |
| 2427 | struct pagedep *pagedep; | |
| 2428 | struct inodedep *inodedep; | |
| 2429 | struct mkdir *mkdir1, *mkdir2; | |
| 2430 | ||
| 2431 | /* | |
| 2432 | * Whiteouts have no dependencies. | |
| 2433 | */ | |
| 2434 | if (newinum == WINO) { | |
| 2435 | if (newdirbp != NULL) | |
| 2436 | bdwrite(newdirbp); | |
| 2437 | return; | |
| 2438 | } | |
| 2439 | ||
| 2440 | fs = dp->i_fs; | |
| 2441 | lbn = lblkno(fs, diroffset); | |
| 2442 | offset = blkoff(fs, diroffset); | |
| 2443 | MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD, | |
| e7b4468c | 2444 | M_SOFTDEP_FLAGS | M_ZERO); |
| 984263bc MD |
2445 | dap->da_list.wk_type = D_DIRADD; |
| 2446 | dap->da_offset = offset; | |
| 2447 | dap->da_newinum = newinum; | |
| 2448 | dap->da_state = ATTACHED; | |
| 2449 | if (newdirbp == NULL) { | |
| 2450 | dap->da_state |= DEPCOMPLETE; | |
| 2451 | ACQUIRE_LOCK(&lk); | |
| 2452 | } else { | |
| 2453 | dap->da_state |= MKDIR_BODY | MKDIR_PARENT; | |
| 2454 | MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR, | |
| 2455 | M_SOFTDEP_FLAGS); | |
| 2456 | mkdir1->md_list.wk_type = D_MKDIR; | |
| 2457 | mkdir1->md_state = MKDIR_BODY; | |
| 2458 | mkdir1->md_diradd = dap; | |
| 2459 | MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR, | |
| 2460 | M_SOFTDEP_FLAGS); | |
| 2461 | mkdir2->md_list.wk_type = D_MKDIR; | |
| 2462 | mkdir2->md_state = MKDIR_PARENT; | |
| 2463 | mkdir2->md_diradd = dap; | |
| 2464 | /* | |
| 2465 | * Dependency on "." and ".." being written to disk. | |
| 2466 | */ | |
| 2467 | mkdir1->md_buf = newdirbp; | |
| 2468 | ACQUIRE_LOCK(&lk); | |
| 2469 | LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs); | |
| 408357d8 | 2470 | WORKLIST_INSERT_BP(newdirbp, &mkdir1->md_list); |
| 984263bc MD |
2471 | FREE_LOCK(&lk); |
| 2472 | bdwrite(newdirbp); | |
| 2473 | /* | |
| 2474 | * Dependency on link count increase for parent directory | |
| 2475 | */ | |
| 2476 | ACQUIRE_LOCK(&lk); | |
| 2477 | if (inodedep_lookup(dp->i_fs, dp->i_number, 0, &inodedep) == 0 | |
| 2478 | || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { | |
| 2479 | dap->da_state &= ~MKDIR_PARENT; | |
| 2480 | WORKITEM_FREE(mkdir2, D_MKDIR); | |
| 2481 | } else { | |
| 2482 | LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); | |
| 2483 | WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list); | |
| 2484 | } | |
| 2485 | } | |
| 2486 | /* | |
| 2487 | * Link into parent directory pagedep to await its being written. | |
| 2488 | */ | |
| 2489 | if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0) | |
| 408357d8 | 2490 | WORKLIST_INSERT_BP(bp, &pagedep->pd_list); |
| 984263bc MD |
2491 | dap->da_pagedep = pagedep; |
| 2492 | LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, | |
| 2493 | da_pdlist); | |
| 2494 | /* | |
| 2495 | * Link into its inodedep. Put it on the id_bufwait list if the inode | |
| 2496 | * is not yet written. If it is written, do the post-inode write | |
| 2497 | * processing to put it on the id_pendinghd list. | |
| 2498 | */ | |
| 2499 | (void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep); | |
| 2500 | if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) | |
| 2501 | diradd_inode_written(dap, inodedep); | |
| 2502 | else | |
| 2503 | WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); | |
| 2504 | FREE_LOCK(&lk); | |
| 2505 | } | |
| 2506 | ||
| 2507 | /* | |
| 2508 | * This procedure is called to change the offset of a directory | |
| 2509 | * entry when compacting a directory block which must be owned | |
| 2510 | * exclusively by the caller. Note that the actual entry movement | |
| 2511 | * must be done in this procedure to ensure that no I/O completions | |
| 2512 | * occur while the move is in progress. | |
| 3fcb1ab8 SW |
2513 | * |
| 2514 | * Parameters: | |
| 2515 | * dp: inode for directory | |
| 2516 | * base: address of dp->i_offset | |
| 2517 | * oldloc: address of old directory location | |
| 2518 | * newloc: address of new directory location | |
| 2519 | * entrysize: size of directory entry | |
| 984263bc MD |
2520 | */ |
| 2521 | void | |
| 3fcb1ab8 SW |
2522 | softdep_change_directoryentry_offset(struct inode *dp, caddr_t base, |
| 2523 | caddr_t oldloc, caddr_t newloc, | |
| 2524 | int entrysize) | |
| 984263bc MD |
2525 | { |
| 2526 | int offset, oldoffset, newoffset; | |
| 2527 | struct pagedep *pagedep; | |
| 2528 | struct diradd *dap; | |
| 2529 | ufs_lbn_t lbn; | |
| 2530 | ||
| 2531 | ACQUIRE_LOCK(&lk); | |
| 2532 | lbn = lblkno(dp->i_fs, dp->i_offset); | |
| 2533 | offset = blkoff(dp->i_fs, dp->i_offset); | |
| 2534 | if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0) | |
| 2535 | goto done; | |
| 2536 | oldoffset = offset + (oldloc - base); | |
| 2537 | newoffset = offset + (newloc - base); | |
| 2538 | ||
| 2539 | LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) { | |
| 2540 | if (dap->da_offset != oldoffset) | |
| 2541 | continue; | |
| 2542 | dap->da_offset = newoffset; | |
| 2543 | if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset)) | |
| 2544 | break; | |
| 2545 | LIST_REMOVE(dap, da_pdlist); | |
| 2546 | LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)], | |
| 2547 | dap, da_pdlist); | |
| 2548 | break; | |
| 2549 | } | |
| 2550 | if (dap == NULL) { | |
| 2551 | ||
| 2552 | LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) { | |
| 2553 | if (dap->da_offset == oldoffset) { | |
| 2554 | dap->da_offset = newoffset; | |
| 2555 | break; | |
| 2556 | } | |
| 2557 | } | |
| 2558 | } | |
| 2559 | done: | |
| 2560 | bcopy(oldloc, newloc, entrysize); | |
| 2561 | FREE_LOCK(&lk); | |
| 2562 | } | |
| 2563 | ||
| 2564 | /* | |
| 2565 | * Free a diradd dependency structure. This routine must be called | |
| 2566 | * with splbio interrupts blocked. | |
| 2567 | */ | |
| 2568 | static void | |
| 3fcb1ab8 | 2569 | free_diradd(struct diradd *dap) |
| 984263bc MD |
2570 | { |
| 2571 | struct dirrem *dirrem; | |
| 2572 | struct pagedep *pagedep; | |
| 2573 | struct inodedep *inodedep; | |
| 2574 | struct mkdir *mkdir, *nextmd; | |
| 2575 | ||
| 2576 | #ifdef DEBUG | |
| dadab5e9 | 2577 | if (lk.lkt_held == NOHOLDER) |
| 984263bc MD |
2578 | panic("free_diradd: lock not held"); |
| 2579 | #endif | |
| 2580 | WORKLIST_REMOVE(&dap->da_list); | |
| 2581 | LIST_REMOVE(dap, da_pdlist); | |
| 2582 | if ((dap->da_state & DIRCHG) == 0) { | |
| 2583 | pagedep = dap->da_pagedep; | |
| 2584 | } else { | |
| 2585 | dirrem = dap->da_previous; | |
| 2586 | pagedep = dirrem->dm_pagedep; | |
| 2587 | dirrem->dm_dirinum = pagedep->pd_ino; | |
| 2588 | add_to_worklist(&dirrem->dm_list); | |
| 2589 | } | |
| 2590 | if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum, | |
| 2591 | 0, &inodedep) != 0) | |
| 2592 | (void) free_inodedep(inodedep); | |
| 2593 | if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { | |
| 2594 | for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { | |
| 2595 | nextmd = LIST_NEXT(mkdir, md_mkdirs); | |
| 2596 | if (mkdir->md_diradd != dap) | |
| 2597 | continue; | |
| 2598 | dap->da_state &= ~mkdir->md_state; | |
| 2599 | WORKLIST_REMOVE(&mkdir->md_list); | |
| 2600 | LIST_REMOVE(mkdir, md_mkdirs); | |
| 2601 | WORKITEM_FREE(mkdir, D_MKDIR); | |
| 2602 | } | |
| 2603 | if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { | |
| 2604 | FREE_LOCK(&lk); | |
| 2605 | panic("free_diradd: unfound ref"); | |
| 2606 | } | |
| 2607 | } | |
| 2608 | WORKITEM_FREE(dap, D_DIRADD); | |
| 2609 | } | |
| 2610 | ||
| 2611 | /* | |
| 2612 | * Directory entry removal dependencies. | |
| 2613 | * | |
| 2614 | * When removing a directory entry, the entry's inode pointer must be | |
| 2615 | * zero'ed on disk before the corresponding inode's link count is decremented | |
| 2616 | * (possibly freeing the inode for re-use). This dependency is handled by | |
| 2617 | * updating the directory entry but delaying the inode count reduction until | |
| 2618 | * after the directory block has been written to disk. After this point, the | |
| 2619 | * inode count can be decremented whenever it is convenient. | |
| 2620 | */ | |
| 2621 | ||
| 2622 | /* | |
| 2623 | * This routine should be called immediately after removing | |
| 2624 | * a directory entry. The inode's link count should not be | |
| 2625 | * decremented by the calling procedure -- the soft updates | |
| 2626 | * code will do this task when it is safe. | |
| 3fcb1ab8 SW |
2627 | * |
| 2628 | * Parameters: | |
| 2629 | * bp: buffer containing directory block | |
| 2630 | * dp: inode for the directory being modified | |
| 2631 | * ip: inode for directory entry being removed | |
| 2632 | * isrmdir: indicates if doing RMDIR | |
| 984263bc MD |
2633 | */ |
| 2634 | void | |
| 3fcb1ab8 SW |
2635 | softdep_setup_remove(struct buf *bp, struct inode *dp, struct inode *ip, |
| 2636 | int isrmdir) | |
| 984263bc MD |
2637 | { |
| 2638 | struct dirrem *dirrem, *prevdirrem; | |
| 2639 | ||
| 2640 | /* | |
| 2641 | * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. | |
| 2642 | */ | |
| 2643 | dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); | |
| 2644 | ||
| 2645 | /* | |
| 2646 | * If the COMPLETE flag is clear, then there were no active | |
| 2647 | * entries and we want to roll back to a zeroed entry until | |
| 2648 | * the new inode is committed to disk. If the COMPLETE flag is | |
| 2649 | * set then we have deleted an entry that never made it to | |
| 2650 | * disk. If the entry we deleted resulted from a name change, | |
| 2651 | * then the old name still resides on disk. We cannot delete | |
| 2652 | * its inode (returned to us in prevdirrem) until the zeroed | |
| 2653 | * directory entry gets to disk. The new inode has never been | |
| 2654 | * referenced on the disk, so can be deleted immediately. | |
| 2655 | */ | |
| 2656 | if ((dirrem->dm_state & COMPLETE) == 0) { | |
| 2657 | LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, | |
| 2658 | dm_next); | |
| 2659 | FREE_LOCK(&lk); | |
| 2660 | } else { | |
| 2661 | if (prevdirrem != NULL) | |
| 2662 | LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, | |
| 2663 | prevdirrem, dm_next); | |
| 2664 | dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; | |
| 2665 | FREE_LOCK(&lk); | |
| 2666 | handle_workitem_remove(dirrem); | |
| 2667 | } | |
| 2668 | } | |
| 2669 | ||
| 2670 | /* | |
| 2671 | * Allocate a new dirrem if appropriate and return it along with | |
| 2672 | * its associated pagedep. Called without a lock, returns with lock. | |
| 2673 | */ | |
| 2674 | static long num_dirrem; /* number of dirrem allocated */ | |
| 3fcb1ab8 SW |
2675 | |
| 2676 | /* | |
| 2677 | * Parameters: | |
| 2678 | * bp: buffer containing directory block | |
| 2679 | * dp: inode for the directory being modified | |
| 2680 | * ip: inode for directory entry being removed | |
| 2681 | * isrmdir: indicates if doing RMDIR | |
| 2682 | * prevdirremp: previously referenced inode, if any | |
| 2683 | */ | |
| 984263bc | 2684 | static struct dirrem * |
| 3fcb1ab8 SW |
2685 | newdirrem(struct buf *bp, struct inode *dp, struct inode *ip, |
| 2686 | int isrmdir, struct dirrem **prevdirremp) | |
| 984263bc MD |
2687 | { |
| 2688 | int offset; | |
| 2689 | ufs_lbn_t lbn; | |
| 2690 | struct diradd *dap; | |
| 2691 | struct dirrem *dirrem; | |
| 2692 | struct pagedep *pagedep; | |
| 2693 | ||
| 2694 | /* | |
| 2695 | * Whiteouts have no deletion dependencies. | |
| 2696 | */ | |
| 2697 | if (ip == NULL) | |
| 2698 | panic("newdirrem: whiteout"); | |
| 2699 | /* | |
| 2700 | * If we are over our limit, try to improve the situation. | |
| 2701 | * Limiting the number of dirrem structures will also limit | |
| 2702 | * the number of freefile and freeblks structures. | |
| 2703 | */ | |
| 2704 | if (num_dirrem > max_softdeps / 2 && speedup_syncer() == 0) | |
| 2705 | (void) request_cleanup(FLUSH_REMOVE, 0); | |
| 2706 | num_dirrem += 1; | |
| 2707 | MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem), | |
| e7b4468c | 2708 | M_DIRREM, M_SOFTDEP_FLAGS | M_ZERO); |
| 984263bc MD |
2709 | dirrem->dm_list.wk_type = D_DIRREM; |
| 2710 | dirrem->dm_state = isrmdir ? RMDIR : 0; | |
| 2711 | dirrem->dm_mnt = ITOV(ip)->v_mount; | |
| 2712 | dirrem->dm_oldinum = ip->i_number; | |
| 2713 | *prevdirremp = NULL; | |
| 2714 | ||
| 2715 | ACQUIRE_LOCK(&lk); | |
| 2716 | lbn = lblkno(dp->i_fs, dp->i_offset); | |
| 2717 | offset = blkoff(dp->i_fs, dp->i_offset); | |
| 2718 | if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0) | |
| 408357d8 | 2719 | WORKLIST_INSERT_BP(bp, &pagedep->pd_list); |
| 984263bc MD |
2720 | dirrem->dm_pagedep = pagedep; |
| 2721 | /* | |
| 2722 | * Check for a diradd dependency for the same directory entry. | |
| 2723 | * If present, then both dependencies become obsolete and can | |
| 2724 | * be de-allocated. Check for an entry on both the pd_dirraddhd | |
| 2725 | * list and the pd_pendinghd list. | |
| 2726 | */ | |
| 2727 | ||
| 2728 | LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) | |
| 2729 | if (dap->da_offset == offset) | |
| 2730 | break; | |
| 2731 | if (dap == NULL) { | |
| 2732 | ||
| 2733 | LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) | |
| 2734 | if (dap->da_offset == offset) | |
| 2735 | break; | |
| 2736 | if (dap == NULL) | |
| 2737 | return (dirrem); | |
| 2738 | } | |
| 2739 | /* | |
| 2740 | * Must be ATTACHED at this point. | |
| 2741 | */ | |
| 2742 | if ((dap->da_state & ATTACHED) == 0) { | |
| 2743 | FREE_LOCK(&lk); | |
| 2744 | panic("newdirrem: not ATTACHED"); | |
| 2745 | } | |
| 2746 | if (dap->da_newinum != ip->i_number) { | |
| 2747 | FREE_LOCK(&lk); | |
| f91a71dd | 2748 | panic("newdirrem: inum %"PRId64" should be %"PRId64, |
| 984263bc MD |
2749 | ip->i_number, dap->da_newinum); |
| 2750 | } | |
| 2751 | /* | |
| 2752 | * If we are deleting a changed name that never made it to disk, | |
| 2753 | * then return the dirrem describing the previous inode (which | |
| 2754 | * represents the inode currently referenced from this entry on disk). | |
| 2755 | */ | |
| 2756 | if ((dap->da_state & DIRCHG) != 0) { | |
| 2757 | *prevdirremp = dap->da_previous; | |
| 2758 | dap->da_state &= ~DIRCHG; | |
| 2759 | dap->da_pagedep = pagedep; | |
| 2760 | } | |
| 2761 | /* | |
| 2762 | * We are deleting an entry that never made it to disk. | |
| 2763 | * Mark it COMPLETE so we can delete its inode immediately. | |
| 2764 | */ | |
| 2765 | dirrem->dm_state |= COMPLETE; | |
| 2766 | free_diradd(dap); | |
| 2767 | return (dirrem); | |
| 2768 | } | |
| 2769 | ||
| 2770 | /* | |
| 2771 | * Directory entry change dependencies. | |
| 2772 | * | |
| 2773 | * Changing an existing directory entry requires that an add operation | |
| 2774 | * be completed first followed by a deletion. The semantics for the addition | |
| 2775 | * are identical to the description of adding a new entry above except | |
| 2776 | * that the rollback is to the old inode number rather than zero. Once | |
| 2777 | * the addition dependency is completed, the removal is done as described | |
| 2778 | * in the removal routine above. | |
| 2779 | */ | |
| 2780 | ||
| 2781 | /* | |
| 2782 | * This routine should be called immediately after changing | |
| 2783 | * a directory entry. The inode's link count should not be | |
| 2784 | * decremented by the calling procedure -- the soft updates | |
| 2785 | * code will perform this task when it is safe. | |
| 3fcb1ab8 SW |
2786 | * |
| 2787 | * Parameters: | |
| 2788 | * bp: buffer containing directory block | |
| 2789 | * dp: inode for the directory being modified | |
| 2790 | * ip: inode for directory entry being removed | |
| 2791 | * newinum: new inode number for changed entry | |
| 2792 | * isrmdir: indicates if doing RMDIR | |
| 984263bc MD |
2793 | */ |
| 2794 | void | |
| 3fcb1ab8 SW |
2795 | softdep_setup_directory_change(struct buf *bp, struct inode *dp, |
| 2796 | struct inode *ip, ino_t newinum, | |
| 2797 | int isrmdir) | |
| 984263bc MD |
2798 | { |
| 2799 | int offset; | |
| 2800 | struct diradd *dap = NULL; | |
| 2801 | struct dirrem *dirrem, *prevdirrem; | |
| 2802 | struct pagedep *pagedep; | |
| 2803 | struct inodedep *inodedep; | |
| 2804 | ||
| 2805 | offset = blkoff(dp->i_fs, dp->i_offset); | |
| 2806 | ||
| 2807 | /* | |
| 2808 | * Whiteouts do not need diradd dependencies. | |
| 2809 | */ | |
| 2810 | if (newinum != WINO) { | |
| 2811 | MALLOC(dap, struct diradd *, sizeof(struct diradd), | |
| e7b4468c | 2812 | M_DIRADD, M_SOFTDEP_FLAGS | M_ZERO); |
| 984263bc MD |
2813 | dap->da_list.wk_type = D_DIRADD; |
| 2814 | dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; | |
| 2815 | dap->da_offset = offset; | |
| 2816 | dap->da_newinum = newinum; | |
| 2817 | } | |
| 2818 | ||
| 2819 | /* | |
| 2820 | * Allocate a new dirrem and ACQUIRE_LOCK. | |
| 2821 | */ | |
| 2822 | dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); | |
| 2823 | pagedep = dirrem->dm_pagedep; | |
| 2824 | /* | |
| 2825 | * The possible values for isrmdir: | |
| 2826 | * 0 - non-directory file rename | |
| 2827 | * 1 - directory rename within same directory | |
| 2828 | * inum - directory rename to new directory of given inode number | |
| 2829 | * When renaming to a new directory, we are both deleting and | |
| 2830 | * creating a new directory entry, so the link count on the new | |
| 2831 | * directory should not change. Thus we do not need the followup | |
| 2832 | * dirrem which is usually done in handle_workitem_remove. We set | |
| 2833 | * the DIRCHG flag to tell handle_workitem_remove to skip the | |
| 2834 | * followup dirrem. | |
| 2835 | */ | |
| 2836 | if (isrmdir > 1) | |
| 2837 | dirrem->dm_state |= DIRCHG; | |
| 2838 | ||
| 2839 | /* | |
| 2840 | * Whiteouts have no additional dependencies, | |
| 2841 | * so just put the dirrem on the correct list. | |
| 2842 | */ | |
| 2843 | if (newinum == WINO) { | |
| 2844 | if ((dirrem->dm_state & COMPLETE) == 0) { | |
| 2845 | LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, | |
| 2846 | dm_next); | |
| 2847 | } else { | |
| 2848 | dirrem->dm_dirinum = pagedep->pd_ino; | |
| 2849 | add_to_worklist(&dirrem->dm_list); | |
| 2850 | } | |
| 2851 | FREE_LOCK(&lk); | |
| 2852 | return; | |
| 2853 | } | |
| 2854 | ||
| 2855 | /* | |
| 2856 | * If the COMPLETE flag is clear, then there were no active | |
| 2857 | * entries and we want to roll back to the previous inode until | |
| 2858 | * the new inode is committed to disk. If the COMPLETE flag is | |
| 2859 | * set, then we have deleted an entry that never made it to disk. | |
| 2860 | * If the entry we deleted resulted from a name change, then the old | |
| 2861 | * inode reference still resides on disk. Any rollback that we do | |
| 2862 | * needs to be to that old inode (returned to us in prevdirrem). If | |
| 2863 | * the entry we deleted resulted from a create, then there is | |
| 2864 | * no entry on the disk, so we want to roll back to zero rather | |
| 2865 | * than the uncommitted inode. In either of the COMPLETE cases we | |
| 2866 | * want to immediately free the unwritten and unreferenced inode. | |
| 2867 | */ | |
| 2868 | if ((dirrem->dm_state & COMPLETE) == 0) { | |
| 2869 | dap->da_previous = dirrem; | |
| 2870 | } else { | |
| 2871 | if (prevdirrem != NULL) { | |
| 2872 | dap->da_previous = prevdirrem; | |
| 2873 | } else { | |
| 2874 | dap->da_state &= ~DIRCHG; | |
| 2875 | dap->da_pagedep = pagedep; | |
| 2876 | } | |
| 2877 | dirrem->dm_dirinum = pagedep->pd_ino; | |
| 2878 | add_to_worklist(&dirrem->dm_list); | |
| 2879 | } | |
| 2880 | /* | |
| 2881 | * Link into its inodedep. Put it on the id_bufwait list if the inode | |
| 2882 | * is not yet written. If it is written, do the post-inode write | |
| 2883 | * processing to put it on the id_pendinghd list. | |
| 2884 | */ | |
| 2885 | if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 || | |
| 2886 | (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { | |
| 2887 | dap->da_state |= COMPLETE; | |
| 2888 | LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); | |
| 2889 | WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); | |
| 2890 | } else { | |
| 2891 | LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], | |
| 2892 | dap, da_pdlist); | |
| 2893 | WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); | |
| 2894 | } | |
| 2895 | FREE_LOCK(&lk); | |
| 2896 | } | |
| 2897 | ||
| 2898 | /* | |
| 2899 | * Called whenever the link count on an inode is changed. | |
| 2900 | * It creates an inode dependency so that the new reference(s) | |
| 2901 | * to the inode cannot be committed to disk until the updated | |
| 2902 | * inode has been written. | |
| 3fcb1ab8 SW |
2903 | * |
| 2904 | * Parameters: | |
| 2905 | * ip: the inode with the increased link count | |
| 984263bc MD |
2906 | */ |
| 2907 | void | |
| 3fcb1ab8 | 2908 | softdep_change_linkcnt(struct inode *ip) |
| 984263bc MD |
2909 | { |
| 2910 | struct inodedep *inodedep; | |
| 2911 | ||
| 2912 | ACQUIRE_LOCK(&lk); | |
| 2913 | (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep); | |
| 2914 | if (ip->i_nlink < ip->i_effnlink) { | |
| 2915 | FREE_LOCK(&lk); | |
| 2916 | panic("softdep_change_linkcnt: bad delta"); | |
| 2917 | } | |
| 2918 | inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; | |
| 2919 | FREE_LOCK(&lk); | |
| 2920 | } | |
| 2921 | ||
| 2922 | /* | |
| 2923 | * This workitem decrements the inode's link count. | |
| 2924 | * If the link count reaches zero, the file is removed. | |
| 2925 | */ | |
| 2926 | static void | |
| 3fcb1ab8 | 2927 | handle_workitem_remove(struct dirrem *dirrem) |
| 984263bc | 2928 | { |
| 984263bc MD |
2929 | struct inodedep *inodedep; |
| 2930 | struct vnode *vp; | |
| 2931 | struct inode *ip; | |
| 2932 | ino_t oldinum; | |
| 2933 | int error; | |
| 2934 | ||
| b9b0a6d0 MD |
2935 | error = VFS_VGET(dirrem->dm_mnt, NULL, dirrem->dm_oldinum, &vp); |
| 2936 | if (error) { | |
| 984263bc MD |
2937 | softdep_error("handle_workitem_remove: vget", error); |
| 2938 | return; | |
| 2939 | } | |
| 2940 | ip = VTOI(vp); | |
| 2941 | ACQUIRE_LOCK(&lk); | |
| 2942 | if ((inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, 0, &inodedep)) == 0){ | |
| 2943 | FREE_LOCK(&lk); | |
| 2944 | panic("handle_workitem_remove: lost inodedep"); | |
| 2945 | } | |
| 2946 | /* | |
| 2947 | * Normal file deletion. | |
| 2948 | */ | |
| 2949 | if ((dirrem->dm_state & RMDIR) == 0) { | |
| 2950 | ip->i_nlink--; | |
| 2951 | ip->i_flag |= IN_CHANGE; | |
| 2952 | if (ip->i_nlink < ip->i_effnlink) { | |
| 2953 | FREE_LOCK(&lk); | |
| 2954 | panic("handle_workitem_remove: bad file delta"); | |
| 2955 | } | |
| 2956 | inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; | |
| 2957 | FREE_LOCK(&lk); | |
| 2958 | vput(vp); | |
| 2959 | num_dirrem -= 1; | |
| 2960 | WORKITEM_FREE(dirrem, D_DIRREM); | |
| 2961 | return; | |
| 2962 | } | |
| 2963 | /* | |
| 2964 | * Directory deletion. Decrement reference count for both the | |
| 2965 | * just deleted parent directory entry and the reference for ".". | |
| 2966 | * Next truncate the directory to length zero. When the | |
| 2967 | * truncation completes, arrange to have the reference count on | |
| 2968 | * the parent decremented to account for the loss of "..". | |
| 2969 | */ | |
| 2970 | ip->i_nlink -= 2; | |
| 2971 | ip->i_flag |= IN_CHANGE; | |
| 2972 | if (ip->i_nlink < ip->i_effnlink) { | |
| 2973 | FREE_LOCK(&lk); | |
| 2974 | panic("handle_workitem_remove: bad dir delta"); | |
| 2975 | } | |
| 2976 | inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; | |
| 2977 | FREE_LOCK(&lk); | |
| ac690a1d | 2978 | if ((error = ffs_truncate(vp, (off_t)0, 0, proc0.p_ucred)) != 0) |
| 984263bc MD |
2979 | softdep_error("handle_workitem_remove: truncate", error); |
| 2980 | /* | |
| 2981 | * Rename a directory to a new parent. Since, we are both deleting | |
| 2982 | * and creating a new directory entry, the link count on the new | |
| 2983 | * directory should not change. Thus we skip the followup dirrem. | |
| 2984 | */ | |
| 2985 | if (dirrem->dm_state & DIRCHG) { | |
| 2986 | vput(vp); | |
| 2987 | num_dirrem -= 1; | |
| 2988 | WORKITEM_FREE(dirrem, D_DIRREM); | |
| 2989 | return; | |
| 2990 | } | |
| 2991 | /* | |
| 2992 | * If the inodedep does not exist, then the zero'ed inode has | |
| 2993 | * been written to disk. If the allocated inode has never been | |
| 2994 | * written to disk, then the on-disk inode is zero'ed. In either | |
| 2995 | * case we can remove the file immediately. | |
| 2996 | */ | |
| 2997 | ACQUIRE_LOCK(&lk); | |
| 2998 | dirrem->dm_state = 0; | |
| 2999 | oldinum = dirrem->dm_oldinum; | |
| 3000 | dirrem->dm_oldinum = dirrem->dm_dirinum; | |
| 3001 | if (inodedep_lookup(ip->i_fs, oldinum, 0, &inodedep) == 0 || | |
| 3002 | check_inode_unwritten(inodedep)) { | |
| 3003 | FREE_LOCK(&lk); | |
| 3004 | vput(vp); | |
| 3005 | handle_workitem_remove(dirrem); | |
| 3006 | return; | |
| 3007 | } | |
| 3008 | WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); | |
| 3009 | FREE_LOCK(&lk); | |
| a0ff2e99 MD |
3010 | ip->i_flag |= IN_CHANGE; |
| 3011 | ffs_update(vp, 0); | |
| 984263bc MD |
3012 | vput(vp); |
| 3013 | } | |
| 3014 | ||
| 3015 | /* | |
| 3016 | * Inode de-allocation dependencies. | |
| 3017 | * | |
| 3018 | * When an inode's link count is reduced to zero, it can be de-allocated. We | |
| 3019 | * found it convenient to postpone de-allocation until after the inode is | |
| 3020 | * written to disk with its new link count (zero). At this point, all of the | |
| 3021 | * on-disk inode's block pointers are nullified and, with careful dependency | |
| 3022 | * list ordering, all dependencies related to the inode will be satisfied and | |
| 3023 | * the corresponding dependency structures de-allocated. So, if/when the | |
| 3024 | * inode is reused, there will be no mixing of old dependencies with new | |
| 3025 | * ones. This artificial dependency is set up by the block de-allocation | |
| 3026 | * procedure above (softdep_setup_freeblocks) and completed by the | |
| 3027 | * following procedure. | |
| 3028 | */ | |
| 3029 | static void | |
| 3fcb1ab8 | 3030 | handle_workitem_freefile(struct freefile *freefile) |
| 984263bc MD |
3031 | { |
| 3032 | struct vnode vp; | |
| 3033 | struct inode tip; | |
| 3034 | struct inodedep *idp; | |
| 3035 | int error; | |
| 3036 | ||
| 3037 | #ifdef DEBUG | |
| 3038 | ACQUIRE_LOCK(&lk); | |
| 3039 | error = inodedep_lookup(freefile->fx_fs, freefile->fx_oldinum, 0, &idp); | |
| 3040 | FREE_LOCK(&lk); | |
| 3041 | if (error) | |
| 3042 | panic("handle_workitem_freefile: inodedep survived"); | |
| 3043 | #endif | |
| 3044 | tip.i_devvp = freefile->fx_devvp; | |
| 3045 | tip.i_dev = freefile->fx_devvp->v_rdev; | |
| 3046 | tip.i_fs = freefile->fx_fs; | |
| 3047 | vp.v_data = &tip; | |
| 3048 | if ((error = ffs_freefile(&vp, freefile->fx_oldinum, freefile->fx_mode)) != 0) | |
| 3049 | softdep_error("handle_workitem_freefile", error); | |
| 3050 | WORKITEM_FREE(freefile, D_FREEFILE); | |
| 3051 | } | |
| 3052 | ||
| 3053 | /* | |
| 83b29fff MD |
3054 | * Helper function which unlinks marker element from work list and returns |
| 3055 | * the next element on the list. | |
| 3056 | */ | |
| 3057 | static __inline struct worklist * | |
| 3058 | markernext(struct worklist *marker) | |
| 3059 | { | |
| 3060 | struct worklist *next; | |
| 3061 | ||
| 3062 | next = LIST_NEXT(marker, wk_list); | |
| 3063 | LIST_REMOVE(marker, wk_list); | |
| 3064 | return next; | |
| 3065 | } | |
| 3066 | ||
| 3067 | /* | |
| 27bc0cb1 MD |
3068 | * checkread, checkwrite |
| 3069 | * | |
| 3070 | */ | |
| 3071 | static int | |
| 3072 | softdep_checkread(struct buf *bp) | |
| 3073 | { | |
| 3074 | return(0); | |
| 3075 | } | |
| 3076 | ||
| 3077 | static int | |
| 3078 | softdep_checkwrite(struct buf *bp) | |
| 3079 | { | |
| 3080 | return(0); | |
| 3081 | } | |
| 3082 | ||
| 3083 | /* | |
| 984263bc MD |
3084 | * Disk writes. |
| 3085 | * | |
| 3086 | * The dependency structures constructed above are most actively used when file | |
| 3087 | * system blocks are written to disk. No constraints are placed on when a | |
| 3088 | * block can be written, but unsatisfied update dependencies are made safe by | |
| 3089 | * modifying (or replacing) the source memory for the duration of the disk | |
| 3090 | * write. When the disk write completes, the memory block is again brought | |
| 3091 | * up-to-date. | |
| 3092 | * | |
| 3093 | * In-core inode structure reclamation. | |
| 3094 | * | |
| 3095 | * Because there are a finite number of "in-core" inode structures, they are | |
| 3096 | * reused regularly. By transferring all inode-related dependencies to the | |
| 3097 | * in-memory inode block and indexing them separately (via "inodedep"s), we | |
| 3098 | * can allow "in-core" inode structures to be reused at any time and avoid | |
| 3099 | * any increase in contention. | |
| 3100 | * | |
| 3101 | * Called just before entering the device driver to initiate a new disk I/O. | |
| 3102 | * The buffer must be locked, thus, no I/O completion operations can occur | |
| 3103 | * while we are manipulating its associated dependencies. | |
| 3fcb1ab8 SW |
3104 | * |
| 3105 | * Parameters: | |
| 3106 | * bp: structure describing disk write to occur | |
| 984263bc MD |
3107 | */ |
| 3108 | static void | |
| 3fcb1ab8 | 3109 | softdep_disk_io_initiation(struct buf *bp) |
| 984263bc | 3110 | { |
| 83b29fff MD |
3111 | struct worklist *wk; |
| 3112 | struct worklist marker; | |
| 984263bc MD |
3113 | struct indirdep *indirdep; |
| 3114 | ||
| 3115 | /* | |
| 3116 | * We only care about write operations. There should never | |
| 3117 | * be dependencies for reads. | |
| 3118 | */ | |
| 10f3fee5 | 3119 | if (bp->b_cmd == BUF_CMD_READ) |
| 984263bc | 3120 | panic("softdep_disk_io_initiation: read"); |
| 83b29fff MD |
3121 | |
| 3122 | marker.wk_type = D_LAST + 1; /* Not a normal workitem */ | |
| 3123 | ||
| 984263bc MD |
3124 | /* |
| 3125 | * Do any necessary pre-I/O processing. | |
| 3126 | */ | |
| 83b29fff MD |
3127 | for (wk = LIST_FIRST(&bp->b_dep); wk; wk = markernext(&marker)) { |
| 3128 | LIST_INSERT_AFTER(wk, &marker, wk_list); | |
| 3129 | ||
| 984263bc MD |
3130 | switch (wk->wk_type) { |
| 3131 | ||
| 3132 | case D_PAGEDEP: | |
| 3133 | initiate_write_filepage(WK_PAGEDEP(wk), bp); | |
| 3134 | continue; | |
| 3135 | ||
| 3136 | case D_INODEDEP: | |
| 3137 | initiate_write_inodeblock(WK_INODEDEP(wk), bp); | |
| 3138 | continue; | |
| 3139 | ||
| 3140 | case D_INDIRDEP: | |
| 3141 | indirdep = WK_INDIRDEP(wk); | |
| 3142 | if (indirdep->ir_state & GOINGAWAY) | |
| 3143 | panic("disk_io_initiation: indirdep gone"); | |
| 3144 | /* | |
| 3145 | * If there are no remaining dependencies, this | |
| 3146 | * will be writing the real pointers, so the | |
| 3147 | * dependency can be freed. | |
| 3148 | */ | |
| 3149 | if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) { | |
| 3150 | indirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE; | |
| 3151 | brelse(indirdep->ir_savebp); | |
| 3152 | /* inline expand WORKLIST_REMOVE(wk); */ | |
| 3153 | wk->wk_state &= ~ONWORKLIST; | |
| 3154 | LIST_REMOVE(wk, wk_list); | |
| 3155 | WORKITEM_FREE(indirdep, D_INDIRDEP); | |
| 3156 | continue; | |
| 3157 | } | |
| 3158 | /* | |
| 3159 | * Replace up-to-date version with safe version. | |
| 3160 | */ | |
| 3161 | MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount, | |
| 3162 | M_INDIRDEP, M_SOFTDEP_FLAGS); | |
| 3163 | ACQUIRE_LOCK(&lk); | |
| 3164 | indirdep->ir_state &= ~ATTACHED; | |
| 3165 | indirdep->ir_state |= UNDONE; | |
| 3166 | bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); | |
| 3167 | bcopy(indirdep->ir_savebp->b_data, bp->b_data, | |
| 3168 | bp->b_bcount); | |
| 3169 | FREE_LOCK(&lk); | |
| 3170 | continue; | |
| 3171 | ||
| 3172 | case D_MKDIR: | |
| 3173 | case D_BMSAFEMAP: | |
| 3174 | case D_ALLOCDIRECT: | |
| 3175 | case D_ALLOCINDIR: | |
| 3176 | continue; | |
| 3177 | ||
| 3178 | default: | |
| 3179 | panic("handle_disk_io_initiation: Unexpected type %s", | |
| 3180 | TYPENAME(wk->wk_type)); | |
| 3181 | /* NOTREACHED */ | |
| 3182 | } | |
| 3183 | } | |
| 3184 | } | |
| 3185 | ||
| 3186 | /* | |
| 3187 | * Called from within the procedure above to deal with unsatisfied | |
| 3188 | * allocation dependencies in a directory. The buffer must be locked, | |
| 3189 | * thus, no I/O completion operations can occur while we are | |
| 3190 | * manipulating its associated dependencies. | |
| 3191 | */ | |
| 3192 | static void | |
| 3fcb1ab8 | 3193 | initiate_write_filepage(struct pagedep *pagedep, struct buf *bp) |
| 984263bc MD |
3194 | { |
| 3195 | struct diradd *dap; | |
| 3196 | struct direct *ep; | |
| 3197 | int i; | |
| 3198 | ||
| 3199 | if (pagedep->pd_state & IOSTARTED) { | |
| 3200 | /* | |
| 3201 | * This can only happen if there is a driver that does not | |
| 3202 | * understand chaining. Here biodone will reissue the call | |
| 3203 | * to strategy for the incomplete buffers. | |
| 3204 | */ | |
| 086c1d7e | 3205 | kprintf("initiate_write_filepage: already started\n"); |
| 984263bc MD |
3206 | return; |
| 3207 | } | |
| 3208 | pagedep->pd_state |= IOSTARTED; | |
| 3209 | ACQUIRE_LOCK(&lk); | |
| 3210 | for (i = 0; i < DAHASHSZ; i++) { | |
| 3211 | LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { | |
| 3212 | ep = (struct direct *) | |
| 3213 | ((char *)bp->b_data + dap->da_offset); | |
| 3214 | if (ep->d_ino != dap->da_newinum) { | |
| 3215 | FREE_LOCK(&lk); | |
| f91a71dd | 3216 | panic("%s: dir inum %d != new %"PRId64, |
| 984263bc MD |
3217 | "initiate_write_filepage", |
| 3218 | ep->d_ino, dap->da_newinum); | |
| 3219 | } | |
| 3220 | if (dap->da_state & DIRCHG) | |
| 3221 | ep->d_ino = dap->da_previous->dm_oldinum; | |
| 3222 | else | |
| 3223 | ep->d_ino = 0; | |
| 3224 | dap->da_state &= ~ATTACHED; | |
| 3225 | dap->da_state |= UNDONE; | |
| 3226 | } | |
| 3227 | } | |
| 3228 | FREE_LOCK(&lk); | |
| 3229 | } | |
| 3230 | ||
| 3231 | /* | |
| 3232 | * Called from within the procedure above to deal with unsatisfied | |
| 3233 | * allocation dependencies in an inodeblock. The buffer must be | |
| 3234 | * locked, thus, no I/O completion operations can occur while we | |
| 3235 | * are manipulating its associated dependencies. | |
| 3fcb1ab8 SW |
3236 | * |
| 3237 | * Parameters: | |
| 3238 | * bp: The inode block | |
| 984263bc MD |
3239 | */ |
| 3240 | static void | |
| 3fcb1ab8 | 3241 | initiate_write_inodeblock(struct inodedep *inodedep, struct buf *bp) |
| 984263bc MD |
3242 | { |
| 3243 | struct allocdirect *adp, *lastadp; | |
| 50e58362 MD |
3244 | struct ufs1_dinode *dp; |
| 3245 | struct ufs1_dinode *sip; | |
| 984263bc MD |
3246 | struct fs *fs; |
| 3247 | ufs_lbn_t prevlbn = 0; | |
| 3248 | int i, deplist; | |
| 3249 | ||
| 3250 | if (inodedep->id_state & IOSTARTED) | |
| 3251 | panic("initiate_write_inodeblock: already started"); | |
| 3252 | inodedep->id_state |= IOSTARTED; | |
| 3253 | fs = inodedep->id_fs; | |
| 50e58362 | 3254 | dp = (struct ufs1_dinode *)bp->b_data + |
| 984263bc MD |
3255 | ino_to_fsbo(fs, inodedep->id_ino); |
| 3256 | /* | |
| 3257 | * If the bitmap is not yet written, then the allocated | |
| 3258 | * inode cannot be written to disk. | |
| 3259 | */ | |
| 3260 | if ((inodedep->id_state & DEPCOMPLETE) == 0) { | |
| 3261 | if (inodedep->id_savedino != NULL) | |
| 3262 | panic("initiate_write_inodeblock: already doing I/O"); | |
| 50e58362 MD |
3263 | MALLOC(sip, struct ufs1_dinode *, |
| 3264 | sizeof(struct ufs1_dinode), M_INODEDEP, M_SOFTDEP_FLAGS); | |
| 83b29fff | 3265 | inodedep->id_savedino = sip; |
| 984263bc | 3266 | *inodedep->id_savedino = *dp; |
| 50e58362 | 3267 | bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); |
| 83b29fff | 3268 | dp->di_gen = inodedep->id_savedino->di_gen; |
| 984263bc MD |
3269 | return; |
| 3270 | } | |
| 3271 | /* | |
| 3272 | * If no dependencies, then there is nothing to roll back. | |
| 3273 | */ | |
| 3274 | inodedep->id_savedsize = dp->di_size; | |
| 3275 | if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL) | |
| 3276 | return; | |
| 3277 | /* | |
| 3278 | * Set the dependencies to busy. | |
| 3279 | */ | |
| 3280 | ACQUIRE_LOCK(&lk); | |
| 3281 | for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; | |
| 3282 | adp = TAILQ_NEXT(adp, ad_next)) { | |
| 3283 | #ifdef DIAGNOSTIC | |
| 3284 | if (deplist != 0 && prevlbn >= adp->ad_lbn) { | |
| 3285 | FREE_LOCK(&lk); | |
| 3286 | panic("softdep_write_inodeblock: lbn order"); | |
| 3287 | } | |
| 3288 | prevlbn = adp->ad_lbn; | |
| 3289 | if (adp->ad_lbn < NDADDR && | |
| 3290 | dp->di_db[adp->ad_lbn] != adp->ad_newblkno) { | |
| 3291 | FREE_LOCK(&lk); | |
| 3292 | panic("%s: direct pointer #%ld mismatch %d != %d", | |
| 3293 | "softdep_write_inodeblock", adp->ad_lbn, | |
| 3294 | dp->di_db[adp->ad_lbn], adp->ad_newblkno); | |
| 3295 | } | |
| 3296 | if (adp->ad_lbn >= NDADDR && | |
| 3297 | dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) { | |
| 3298 | FREE_LOCK(&lk); | |
| 3299 | panic("%s: indirect pointer #%ld mismatch %d != %d", | |
| 3300 | "softdep_write_inodeblock", adp->ad_lbn - NDADDR, | |
| 3301 | dp->di_ib[adp->ad_lbn - NDADDR], adp->ad_newblkno); | |
| 3302 | } | |
| 3303 | deplist |= 1 << adp->ad_lbn; | |
| 3304 | if ((adp->ad_state & ATTACHED) == 0) { | |
| 3305 | FREE_LOCK(&lk); | |
| 3306 | panic("softdep_write_inodeblock: Unknown state 0x%x", | |
| 3307 | adp->ad_state); | |
| 3308 | } | |
| 3309 | #endif /* DIAGNOSTIC */ | |
| 3310 | adp->ad_state &= ~ATTACHED; | |
| 3311 | adp->ad_state |= UNDONE; | |
| 3312 | } | |
| 3313 | /* | |
| 3314 | * The on-disk inode cannot claim to be any larger than the last | |
| 3315 | * fragment that has been written. Otherwise, the on-disk inode | |
| 3316 | * might have fragments that were not the last block in the file | |
| 3317 | * which would corrupt the filesystem. | |
| 3318 | */ | |
| 3319 | for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; | |
| 3320 | lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { | |
| 3321 | if (adp->ad_lbn >= NDADDR) | |
| 3322 | break; | |
| 3323 | dp->di_db[adp->ad_lbn] = adp->ad_oldblkno; | |
| 3324 | /* keep going until hitting a rollback to a frag */ | |
| 3325 | if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) | |
| 3326 | continue; | |
| 3327 | dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize; | |
| 3328 | for (i = adp->ad_lbn + 1; i < NDADDR; i++) { | |
| 3329 | #ifdef DIAGNOSTIC | |
| 3330 | if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) { | |
| 3331 | FREE_LOCK(&lk); | |
| 3332 | panic("softdep_write_inodeblock: lost dep1"); | |
| 3333 | } | |
| 3334 | #endif /* DIAGNOSTIC */ | |
| 3335 | dp->di_db[i] = 0; | |
| 3336 | } | |
| 3337 | for (i = 0; i < NIADDR; i++) { | |
| 3338 | #ifdef DIAGNOSTIC | |
| 3339 | if (dp->di_ib[i] != 0 && | |
| 3340 | (deplist & ((1 << NDADDR) << i)) == 0) { | |
| 3341 | FREE_LOCK(&lk); | |
| 3342 | panic("softdep_write_inodeblock: lost dep2"); | |
| 3343 | } | |
| 3344 | #endif /* DIAGNOSTIC */ | |
| 3345 | dp->di_ib[i] = 0; | |
| 3346 | } | |
| 3347 | FREE_LOCK(&lk); | |
| 3348 | return; | |
| 3349 | } | |
| 3350 | /* | |
| 3351 | * If we have zero'ed out the last allocated block of the file, | |
| 3352 | * roll back the size to the last currently allocated block. | |
| 3353 | * We know that this last allocated block is a full-sized as | |
| 3354 | * we already checked for fragments in the loop above. | |
| 3355 | */ | |
| 3356 | if (lastadp != NULL && | |
| 3357 | dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) { | |
| 3358 | for (i = lastadp->ad_lbn; i >= 0; i--) | |
| 3359 | if (dp->di_db[i] != 0) | |
| 3360 | break; | |
| 3361 | dp->di_size = (i + 1) * fs->fs_bsize; | |
| 3362 | } | |
| 3363 | /* | |
| 3364 | * The only dependencies are for indirect blocks. | |
| 3365 | * | |
| 3366 | * The file size for indirect block additions is not guaranteed. | |
| 3367 | * Such a guarantee would be non-trivial to achieve. The conventional | |
| 3368 | * synchronous write implementation also does not make this guarantee. | |
| 3369 | * Fsck should catch and fix discrepancies. Arguably, the file size | |
| 3370 | * can be over-estimated without destroying integrity when the file | |
| 3371 | * moves into the indirect blocks (i.e., is large). If we want to | |
| 3372 | * postpone fsck, we are stuck with this argument. | |
| 3373 | */ | |
| 3374 | for (; adp; adp = TAILQ_NEXT(adp, ad_next)) | |
| 3375 | dp->di_ib[adp->ad_lbn - NDADDR] = 0; | |
| 3376 | FREE_LOCK(&lk); | |
| 3377 | } | |
| 3378 | ||
| 3379 | /* | |
| 3380 | * This routine is called during the completion interrupt | |
| 3381 | * service routine for a disk write (from the procedure called | |
| f719c866 | 3382 | * by the device driver to inform the filesystem caches of |
| 984263bc MD |
3383 | * a request completion). It should be called early in this |
| 3384 | * procedure, before the block is made available to other | |
| 3385 | * processes or other routines are called. | |
| 3fcb1ab8 SW |
3386 | * |
| 3387 | * Parameters: | |
| 3388 | * bp: describes the completed disk write | |
| 984263bc MD |
3389 | */ |
| 3390 | static void | |
| 3fcb1ab8 | 3391 | softdep_disk_write_complete(struct buf *bp) |
| 984263bc MD |
3392 | { |
| 3393 | struct worklist *wk; | |
| 3394 | struct workhead reattach; | |
| 3395 | struct newblk *newblk; | |
| 3396 | struct allocindir *aip; | |
| 3397 | struct allocdirect *adp; | |
| 3398 | struct indirdep *indirdep; | |
| 3399 | struct inodedep *inodedep; | |
| 3400 | struct bmsafemap *bmsafemap; | |
| 3401 | ||
| 3402 | #ifdef DEBUG | |
| dadab5e9 | 3403 | if (lk.lkt_held != NOHOLDER) |
| 984263bc | 3404 | panic("softdep_disk_write_complete: lock is held"); |
| dadab5e9 | 3405 | lk.lkt_held = SPECIAL_FLAG; |
| 984263bc MD |
3406 | #endif |
| 3407 | LIST_INIT(&reattach); | |
| 3408 | while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { | |
| 3409 | WORKLIST_REMOVE(wk); | |
| 3410 | switch (wk->wk_type) { | |
| 3411 | ||
| 3412 | case D_PAGEDEP: | |
| 3413 | if (handle_written_filepage(WK_PAGEDEP(wk), bp)) | |
| 3414 | WORKLIST_INSERT(&reattach, wk); | |
| 3415 | continue; | |
| 3416 | ||
| 3417 | case D_INODEDEP: | |
| 3418 | if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) | |
| 3419 | WORKLIST_INSERT(&reattach, wk); | |
| 3420 | continue; | |
| 3421 | ||
| 3422 | case D_BMSAFEMAP: | |
| 3423 | bmsafemap = WK_BMSAFEMAP(wk); | |
| 3424 | while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) { | |
| 3425 | newblk->nb_state |= DEPCOMPLETE; | |
| 3426 | newblk->nb_bmsafemap = NULL; | |
| 3427 | LIST_REMOVE(newblk, nb_deps); | |
| 3428 | } | |
| 3429 | while ((adp = | |
| 3430 | LIST_FIRST(&bmsafemap->sm_allocdirecthd))) { | |
| 3431 | adp->ad_state |= DEPCOMPLETE; | |
| 3432 | adp->ad_buf = NULL; | |
| 3433 | LIST_REMOVE(adp, ad_deps); | |
| 3434 | handle_allocdirect_partdone(adp); | |
| 3435 | } | |
| 3436 | while ((aip = | |
| 3437 | LIST_FIRST(&bmsafemap->sm_allocindirhd))) { | |
| 3438 | aip->ai_state |= DEPCOMPLETE; | |
| 3439 | aip->ai_buf = NULL; | |
| 3440 | LIST_REMOVE(aip, ai_deps); | |
| 3441 | handle_allocindir_partdone(aip); | |
| 3442 | } | |
| 3443 | while ((inodedep = | |
| 3444 | LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) { | |
| 3445 | inodedep->id_state |= DEPCOMPLETE; | |
| 3446 | LIST_REMOVE(inodedep, id_deps); | |
| 3447 | inodedep->id_buf = NULL; | |
| 3448 | } | |
| 3449 | WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); | |
| 3450 | continue; | |
| 3451 | ||
| 3452 | case D_MKDIR: | |
| 3453 | handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); | |
| 3454 | continue; | |
| 3455 | ||
| 3456 | case D_ALLOCDIRECT: | |
| 3457 | adp = WK_ALLOCDIRECT(wk); | |
| 3458 | adp->ad_state |= COMPLETE; | |
| 3459 | handle_allocdirect_partdone(adp); | |
| 3460 | continue; | |
| 3461 | ||
| 3462 | case D_ALLOCINDIR: | |
| 3463 | aip = WK_ALLOCINDIR(wk); | |
| 3464 | aip->ai_state |= COMPLETE; | |
| 3465 | handle_allocindir_partdone(aip); | |
| 3466 | continue; | |
| 3467 | ||
| 3468 | case D_INDIRDEP: | |
| 3469 | indirdep = WK_INDIRDEP(wk); | |
| 3470 | if (indirdep->ir_state & GOINGAWAY) { | |
| dadab5e9 | 3471 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3472 | panic("disk_write_complete: indirdep gone"); |
| 3473 | } | |
| 3474 | bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); | |
| 3475 | FREE(indirdep->ir_saveddata, M_INDIRDEP); | |
| 3476 | indirdep->ir_saveddata = 0; | |
| 3477 | indirdep->ir_state &= ~UNDONE; | |
| 3478 | indirdep->ir_state |= ATTACHED; | |
| 3479 | while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { | |
| 3480 | handle_allocindir_partdone(aip); | |
| 3481 | if (aip == LIST_FIRST(&indirdep->ir_donehd)) { | |
| dadab5e9 | 3482 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3483 | panic("disk_write_complete: not gone"); |
| 3484 | } | |
| 3485 | } | |
| 3486 | WORKLIST_INSERT(&reattach, wk); | |
| 3487 | if ((bp->b_flags & B_DELWRI) == 0) | |
| 3488 | stat_indir_blk_ptrs++; | |
| 3489 | bdirty(bp); | |
| 3490 | continue; | |
| 3491 | ||
| 3492 | default: | |
| dadab5e9 | 3493 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3494 | panic("handle_disk_write_complete: Unknown type %s", |
| 3495 | TYPENAME(wk->wk_type)); | |
| 3496 | /* NOTREACHED */ | |
| 3497 | } | |
| 3498 | } | |
| 3499 | /* | |
| 3500 | * Reattach any requests that must be redone. | |
| 3501 | */ | |
| 3502 | while ((wk = LIST_FIRST(&reattach)) != NULL) { | |
| 3503 | WORKLIST_REMOVE(wk); | |
| 408357d8 | 3504 | WORKLIST_INSERT_BP(bp, wk); |
| 984263bc MD |
3505 | } |
| 3506 | #ifdef DEBUG | |
| dadab5e9 | 3507 | if (lk.lkt_held != SPECIAL_FLAG) |
| 984263bc | 3508 | panic("softdep_disk_write_complete: lock lost"); |
| dadab5e9 | 3509 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3510 | #endif |
| 3511 | } | |
| 3512 | ||
| 3513 | /* | |
| 3514 | * Called from within softdep_disk_write_complete above. Note that | |
| 3515 | * this routine is always called from interrupt level with further | |
| 3516 | * splbio interrupts blocked. | |
| 3fcb1ab8 SW |
3517 | * |
| 3518 | * Parameters: | |
| 3519 | * adp: the completed allocdirect | |
| 984263bc MD |
3520 | */ |
| 3521 | static void | |
| 3fcb1ab8 | 3522 | handle_allocdirect_partdone(struct allocdirect *adp) |
| 984263bc MD |
3523 | { |
| 3524 | struct allocdirect *listadp; | |
| 3525 | struct inodedep *inodedep; | |
| 3526 | long bsize; | |
| 3527 | ||
| 3528 | if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) | |
| 3529 | return; | |
| 3530 | if (adp->ad_buf != NULL) { | |
| dadab5e9 | 3531 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3532 | panic("handle_allocdirect_partdone: dangling dep"); |
| 3533 | } | |
| 3534 | /* | |
| 3535 | * The on-disk inode cannot claim to be any larger than the last | |
| 3536 | * fragment that has been written. Otherwise, the on-disk inode | |
| 3537 | * might have fragments that were not the last block in the file | |
| 3538 | * which would corrupt the filesystem. Thus, we cannot free any | |
| 3539 | * allocdirects after one whose ad_oldblkno claims a fragment as | |
| 3540 | * these blocks must be rolled back to zero before writing the inode. | |
| 3541 | * We check the currently active set of allocdirects in id_inoupdt. | |
| 3542 | */ | |
| 3543 | inodedep = adp->ad_inodedep; | |
| 3544 | bsize = inodedep->id_fs->fs_bsize; | |
| 3545 | TAILQ_FOREACH(listadp, &inodedep->id_inoupdt, ad_next) { | |
| 3546 | /* found our block */ | |
| 3547 | if (listadp == adp) | |
| 3548 | break; | |
| 3549 | /* continue if ad_oldlbn is not a fragment */ | |
| 3550 | if (listadp->ad_oldsize == 0 || | |
| 3551 | listadp->ad_oldsize == bsize) | |
| 3552 | continue; | |
| 3553 | /* hit a fragment */ | |
| 3554 | return; | |
| 3555 | } | |
| 3556 | /* | |
| 3557 | * If we have reached the end of the current list without | |
| 3558 | * finding the just finished dependency, then it must be | |
| 3559 | * on the future dependency list. Future dependencies cannot | |
| 3560 | * be freed until they are moved to the current list. | |
| 3561 | */ | |
| 3562 | if (listadp == NULL) { | |
| 3563 | #ifdef DEBUG | |
| 3564 | TAILQ_FOREACH(listadp, &inodedep->id_newinoupdt, ad_next) | |
| 3565 | /* found our block */ | |
| 3566 | if (listadp == adp) | |
| 3567 | break; | |
| 3568 | if (listadp == NULL) { | |
| dadab5e9 | 3569 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3570 | panic("handle_allocdirect_partdone: lost dep"); |
| 3571 | } | |
| 3572 | #endif /* DEBUG */ | |
| 3573 | return; | |
| 3574 | } | |
| 3575 | /* | |
| 3576 | * If we have found the just finished dependency, then free | |
| 3577 | * it along with anything that follows it that is complete. | |
| 3578 | */ | |
| 3579 | for (; adp; adp = listadp) { | |
| 3580 | listadp = TAILQ_NEXT(adp, ad_next); | |
| 3581 | if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) | |
| 3582 | return; | |
| 3583 | free_allocdirect(&inodedep->id_inoupdt, adp, 1); | |
| 3584 | } | |
| 3585 | } | |
| 3586 | ||
| 3587 | /* | |
| 3588 | * Called from within softdep_disk_write_complete above. Note that | |
| 3589 | * this routine is always called from interrupt level with further | |
| 3590 | * splbio interrupts blocked. | |
| 3fcb1ab8 SW |
3591 | * |
| 3592 | * Parameters: | |
| 3593 | * aip: the completed allocindir | |
| 984263bc MD |
3594 | */ |
| 3595 | static void | |
| 3fcb1ab8 | 3596 | handle_allocindir_partdone(struct allocindir *aip) |
| 984263bc MD |
3597 | { |
| 3598 | struct indirdep *indirdep; | |
| 3599 | ||
| 3600 | if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) | |
| 3601 | return; | |
| 3602 | if (aip->ai_buf != NULL) { | |
| dadab5e9 | 3603 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3604 | panic("handle_allocindir_partdone: dangling dependency"); |
| 3605 | } | |
| 3606 | indirdep = aip->ai_indirdep; | |
| 3607 | if (indirdep->ir_state & UNDONE) { | |
| 3608 | LIST_REMOVE(aip, ai_next); | |
| 3609 | LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); | |
| 3610 | return; | |
| 3611 | } | |
| 3612 | ((ufs_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = | |
| 3613 | aip->ai_newblkno; | |
| 3614 | LIST_REMOVE(aip, ai_next); | |
| 3615 | if (aip->ai_freefrag != NULL) | |
| 3616 | add_to_worklist(&aip->ai_freefrag->ff_list); | |
| 3617 | WORKITEM_FREE(aip, D_ALLOCINDIR); | |
| 3618 | } | |
| 3619 | ||
| 3620 | /* | |
| 3621 | * Called from within softdep_disk_write_complete above to restore | |
| 3622 | * in-memory inode block contents to their most up-to-date state. Note | |
| 3623 | * that this routine is always called from interrupt level with further | |
| 3624 | * splbio interrupts blocked. | |
| 3fcb1ab8 SW |
3625 | * |
| 3626 | * Parameters: | |
| 3627 | * bp: buffer containing the inode block | |
| 984263bc MD |
3628 | */ |
| 3629 | static int | |
| 3fcb1ab8 | 3630 | handle_written_inodeblock(struct inodedep *inodedep, struct buf *bp) |
| 984263bc MD |
3631 | { |
| 3632 | struct worklist *wk, *filefree; | |
| 3633 | struct allocdirect *adp, *nextadp; | |
| 50e58362 | 3634 | struct ufs1_dinode *dp; |
| 984263bc MD |
3635 | int hadchanges; |
| 3636 | ||
| 3637 | if ((inodedep->id_state & IOSTARTED) == 0) { | |
| dadab5e9 | 3638 | lk.lkt_held = NOHOLDER; |
| 984263bc MD |
3639 | panic("handle_written_inodeblock: not started"); |
| 3640 | } | |
| 3641 | inodedep->id_state &= ~IOSTARTED; | |
| 50e58362 | 3642 |