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