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