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