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