xref: /dragonfly/sys/kern/vfs_sync.c (revision 8e11cefe)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_subr.c	8.31 (Berkeley) 5/26/95
35  * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
36  */
37 
38 /*
39  * External virtual filesystem routines
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/conf.h>
46 #include <sys/dirent.h>
47 #include <sys/domain.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
50 #include <sys/kernel.h>
51 #include <sys/kthread.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/mount.h>
55 #include <sys/proc.h>
56 #include <sys/namei.h>
57 #include <sys/reboot.h>
58 #include <sys/socket.h>
59 #include <sys/stat.h>
60 #include <sys/sysctl.h>
61 #include <sys/syslog.h>
62 #include <sys/vmmeter.h>
63 #include <sys/vnode.h>
64 
65 #include <machine/limits.h>
66 
67 #include <vm/vm.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_kern.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pager.h>
75 #include <vm/vnode_pager.h>
76 
77 #include <sys/buf2.h>
78 
79 /*
80  * The workitem queue.
81  */
82 #define SYNCER_MAXDELAY		32
83 static int sysctl_kern_syncdelay(SYSCTL_HANDLER_ARGS);
84 time_t syncdelay = 30;		/* max time to delay syncing data */
85 SYSCTL_PROC(_kern, OID_AUTO, syncdelay, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
86 		sysctl_kern_syncdelay, "I", "VFS data synchronization delay");
87 time_t filedelay = 30;		/* time to delay syncing files */
88 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW,
89 		&filedelay, 0, "File synchronization delay");
90 time_t dirdelay = 29;		/* time to delay syncing directories */
91 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW,
92 		&dirdelay, 0, "Directory synchronization delay");
93 time_t metadelay = 28;		/* time to delay syncing metadata */
94 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW,
95 		&metadelay, 0, "VFS metadata synchronization delay");
96 time_t retrydelay = 1;		/* retry delay after failure */
97 SYSCTL_INT(_kern, OID_AUTO, retrydelay, CTLFLAG_RW,
98 		&retrydelay, 0, "VFS retry synchronization delay");
99 static int rushjob;			/* number of slots to run ASAP */
100 static int stat_rush_requests;	/* number of times I/O speeded up */
101 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW,
102 		&stat_rush_requests, 0, "");
103 
104 LIST_HEAD(synclist, vnode);
105 
106 #define	SC_FLAG_EXIT		(0x1)		/* request syncer exit */
107 #define	SC_FLAG_DONE		(0x2)		/* syncer confirm exit */
108 
109 struct syncer_ctx {
110 	struct mount		*sc_mp;
111 	struct lwkt_token 	sc_token;
112 	struct thread		*sc_thread;
113 	int			sc_flags;
114 	struct synclist 	*syncer_workitem_pending;
115 	long			syncer_mask;
116 	int 			syncer_delayno;
117 	int			syncer_forced;
118 	int			syncer_rushjob;	/* sequence vnodes faster */
119 	int			syncer_trigger;	/* trigger full sync */
120 	long			syncer_count;
121 };
122 
123 static void syncer_thread(void *);
124 
125 static int
126 sysctl_kern_syncdelay(SYSCTL_HANDLER_ARGS)
127 {
128 	int error;
129 	int v = syncdelay;
130 
131 	error = sysctl_handle_int(oidp, &v, 0, req);
132 	if (error || !req->newptr)
133 		return (error);
134 	if (v < 1)
135 		v = 1;
136 	if (v > SYNCER_MAXDELAY)
137 		v = SYNCER_MAXDELAY;
138 	syncdelay = v;
139 
140 	return(0);
141 }
142 
143 /*
144  * The workitem queue.
145  *
146  * It is useful to delay writes of file data and filesystem metadata
147  * for tens of seconds so that quickly created and deleted files need
148  * not waste disk bandwidth being created and removed. To realize this,
149  * we append vnodes to a "workitem" queue. When running with a soft
150  * updates implementation, most pending metadata dependencies should
151  * not wait for more than a few seconds. Thus, mounted on block devices
152  * are delayed only about a half the time that file data is delayed.
153  * Similarly, directory updates are more critical, so are only delayed
154  * about a third the time that file data is delayed. Thus, there are
155  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
156  * one each second (driven off the filesystem syncer process). The
157  * syncer_delayno variable indicates the next queue that is to be processed.
158  * Items that need to be processed soon are placed in this queue:
159  *
160  *	syncer_workitem_pending[syncer_delayno]
161  *
162  * A delay of fifteen seconds is done by placing the request fifteen
163  * entries later in the queue:
164  *
165  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
166  *
167  */
168 
169 /*
170  * Return the number of vnodes on the syncer's timed list.  This will
171  * include the syncer vnode (mp->mnt_syncer) so if used, a minimum
172  * value of 1 will be returned.
173  */
174 long
175 vn_syncer_count(struct mount *mp)
176 {
177 	struct syncer_ctx *ctx;
178 
179 	ctx = mp->mnt_syncer_ctx;
180 	if (ctx)
181 		return (ctx->syncer_count);
182 	return 0;
183 }
184 
185 /*
186  * Add an item to the syncer work queue.
187  *
188  * WARNING: Cannot get vp->v_token here if not already held, we must
189  *	    depend on the syncer_token (which might already be held by
190  *	    the caller) to protect v_synclist and VONWORKLST.
191  *
192  * WARNING: The syncer depends on this function not blocking if the caller
193  *	    already holds the syncer token.
194  */
195 void
196 vn_syncer_add(struct vnode *vp, int delay)
197 {
198 	struct syncer_ctx *ctx;
199 	int slot;
200 
201 	ctx = vp->v_mount->mnt_syncer_ctx;
202 	lwkt_gettoken(&ctx->sc_token);
203 
204 	if (vp->v_flag & VONWORKLST) {
205 		LIST_REMOVE(vp, v_synclist);
206 		--ctx->syncer_count;
207 	}
208 	if (delay <= 0) {
209 		slot = -delay & ctx->syncer_mask;
210 	} else {
211 		if (delay > SYNCER_MAXDELAY - 2)
212 			delay = SYNCER_MAXDELAY - 2;
213 		slot = (ctx->syncer_delayno + delay) & ctx->syncer_mask;
214 	}
215 
216 	LIST_INSERT_HEAD(&ctx->syncer_workitem_pending[slot], vp, v_synclist);
217 	vsetflags(vp, VONWORKLST);
218 	++ctx->syncer_count;
219 
220 	lwkt_reltoken(&ctx->sc_token);
221 }
222 
223 /*
224  * Removes the vnode from the syncer list.  Since we might block while
225  * acquiring the syncer_token we have to [re]check conditions to determine
226  * that it is ok to remove the vnode.
227  *
228  * Force removal if force != 0.  This can only occur during a forced unmount.
229  *
230  * vp->v_token held on call
231  */
232 void
233 vn_syncer_remove(struct vnode *vp, int force)
234 {
235 	struct syncer_ctx *ctx;
236 
237 	ctx = vp->v_mount->mnt_syncer_ctx;
238 	lwkt_gettoken(&ctx->sc_token);
239 
240 	if ((vp->v_flag & (VISDIRTY | VONWORKLST | VOBJDIRTY)) == VONWORKLST &&
241 	    RB_EMPTY(&vp->v_rbdirty_tree)) {
242 		vclrflags(vp, VONWORKLST);
243 		LIST_REMOVE(vp, v_synclist);
244 		--ctx->syncer_count;
245 	} else if (force && (vp->v_flag & VONWORKLST)) {
246 		vclrflags(vp, VONWORKLST);
247 		LIST_REMOVE(vp, v_synclist);
248 		--ctx->syncer_count;
249 	}
250 
251 	lwkt_reltoken(&ctx->sc_token);
252 }
253 
254 /*
255  * vnode must be locked
256  */
257 void
258 vclrisdirty(struct vnode *vp)
259 {
260 	vclrflags(vp, VISDIRTY);
261 	if (vp->v_flag & VONWORKLST)
262 		vn_syncer_remove(vp, 0);
263 }
264 
265 void
266 vclrobjdirty(struct vnode *vp)
267 {
268 	vclrflags(vp, VOBJDIRTY);
269 	if (vp->v_flag & VONWORKLST)
270 		vn_syncer_remove(vp, 0);
271 }
272 
273 /*
274  * vnode must be stable
275  */
276 void
277 vsetisdirty(struct vnode *vp)
278 {
279 	struct syncer_ctx *ctx;
280 
281 	if ((vp->v_flag & VISDIRTY) == 0) {
282 		ctx = vp->v_mount->mnt_syncer_ctx;
283 		vsetflags(vp, VISDIRTY);
284 		lwkt_gettoken(&ctx->sc_token);
285 		if ((vp->v_flag & VONWORKLST) == 0)
286 			vn_syncer_add(vp, syncdelay);
287 		lwkt_reltoken(&ctx->sc_token);
288 	}
289 }
290 
291 void
292 vsetobjdirty(struct vnode *vp)
293 {
294 	struct syncer_ctx *ctx;
295 
296 	if ((vp->v_flag & VOBJDIRTY) == 0) {
297 		ctx = vp->v_mount->mnt_syncer_ctx;
298 		vsetflags(vp, VOBJDIRTY);
299 		lwkt_gettoken(&ctx->sc_token);
300 		if ((vp->v_flag & VONWORKLST) == 0)
301 			vn_syncer_add(vp, syncdelay);
302 		lwkt_reltoken(&ctx->sc_token);
303 	}
304 }
305 
306 /*
307  * Create per-filesystem syncer process
308  */
309 void
310 vn_syncer_thr_create(struct mount *mp)
311 {
312 	struct syncer_ctx *ctx;
313 	static int syncalloc = 0;
314 
315 	ctx = kmalloc(sizeof(struct syncer_ctx), M_TEMP, M_WAITOK | M_ZERO);
316 	ctx->sc_mp = mp;
317 	ctx->sc_flags = 0;
318 	ctx->syncer_workitem_pending = hashinit(SYNCER_MAXDELAY, M_DEVBUF,
319 						&ctx->syncer_mask);
320 	ctx->syncer_delayno = 0;
321 	lwkt_token_init(&ctx->sc_token, "syncer");
322 	mp->mnt_syncer_ctx = ctx;
323 	kthread_create(syncer_thread, ctx, &ctx->sc_thread,
324 		       "syncer%d", ++syncalloc & 0x7FFFFFFF);
325 }
326 
327 /*
328  * Stop per-filesystem syncer process
329  */
330 void
331 vn_syncer_thr_stop(struct mount *mp)
332 {
333 	struct syncer_ctx *ctx;
334 
335 	ctx = mp->mnt_syncer_ctx;
336 	if (ctx == NULL)
337 		return;
338 
339 	lwkt_gettoken(&ctx->sc_token);
340 
341 	/* Signal the syncer process to exit */
342 	ctx->sc_flags |= SC_FLAG_EXIT;
343 	wakeup(ctx);
344 
345 	/* Wait till syncer process exits */
346 	while ((ctx->sc_flags & SC_FLAG_DONE) == 0)
347 		tsleep(&ctx->sc_flags, 0, "syncexit", hz);
348 
349 	mp->mnt_syncer_ctx = NULL;
350 	lwkt_reltoken(&ctx->sc_token);
351 
352 	hashdestroy(ctx->syncer_workitem_pending, M_DEVBUF, ctx->syncer_mask);
353 	kfree(ctx, M_TEMP);
354 }
355 
356 struct  thread *updatethread;
357 
358 /*
359  * System filesystem synchronizer daemon.
360  */
361 static void
362 syncer_thread(void *_ctx)
363 {
364 	struct syncer_ctx *ctx = _ctx;
365 	struct synclist *slp;
366 	struct vnode *vp;
367 	long starttime;
368 	int *sc_flagsp;
369 	int sc_flags;
370 	int vnodes_synced = 0;
371 	int delta;
372 	int dummy = 0;
373 
374 	for (;;) {
375 		kproc_suspend_loop();
376 
377 		starttime = time_uptime;
378 		lwkt_gettoken(&ctx->sc_token);
379 
380 		/*
381 		 * Push files whose dirty time has expired.  Be careful
382 		 * of interrupt race on slp queue.
383 		 *
384 		 * Note that vsyncscan() and vn_syncer_one() can pull items
385 		 * off the same list, so we shift vp's position in the
386 		 * list immediately.
387 		 */
388 		slp = &ctx->syncer_workitem_pending[ctx->syncer_delayno];
389 
390 		/*
391 		 * If syncer_trigger is set (from trigger_syncer(mp)),
392 		 * Immediately do a full filesystem sync and set up the
393 		 * following full filesystem sync to occur in 1 second.
394 		 */
395 		if (ctx->syncer_trigger) {
396 			ctx->syncer_trigger = 0;
397 			if (ctx->sc_mp && ctx->sc_mp->mnt_syncer) {
398 				vp = ctx->sc_mp->mnt_syncer;
399 				if (vp->v_flag & VONWORKLST) {
400 					vn_syncer_add(vp, retrydelay);
401 					if (vget(vp, LK_EXCLUSIVE) == 0) {
402 						VOP_FSYNC(vp, MNT_LAZY, 0);
403 						vput(vp);
404 						vnodes_synced++;
405 					}
406 				}
407 			}
408 		}
409 
410 		/*
411 		 * FSYNC items in this bucket
412 		 */
413 		while ((vp = LIST_FIRST(slp)) != NULL) {
414 			vn_syncer_add(vp, retrydelay);
415 			if (ctx->syncer_forced) {
416 				if (vget(vp, LK_EXCLUSIVE) == 0) {
417 					VOP_FSYNC(vp, MNT_NOWAIT, 0);
418 					vput(vp);
419 					vnodes_synced++;
420 				}
421 			} else {
422 				if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
423 					VOP_FSYNC(vp, MNT_LAZY, 0);
424 					vput(vp);
425 					vnodes_synced++;
426 				}
427 			}
428 		}
429 
430 		/*
431 		 * Increment the slot upon completion.  This is typically
432 		 * one-second but may be faster if the syncer is triggered.
433 		 */
434 		ctx->syncer_delayno = (ctx->syncer_delayno + 1) &
435 				      ctx->syncer_mask;
436 
437 		sc_flags = ctx->sc_flags;
438 
439 		/* Exit on unmount */
440 		if (sc_flags & SC_FLAG_EXIT)
441 			break;
442 
443 		lwkt_reltoken(&ctx->sc_token);
444 
445 		/*
446 		 * Do sync processing for each mount.
447 		 */
448 		if (ctx->sc_mp)
449 			bio_ops_sync(ctx->sc_mp);
450 
451 		/*
452 		 * The variable rushjob allows the kernel to speed up the
453 		 * processing of the filesystem syncer process. A rushjob
454 		 * value of N tells the filesystem syncer to process the next
455 		 * N seconds worth of work on its queue ASAP. Currently rushjob
456 		 * is used by the soft update code to speed up the filesystem
457 		 * syncer process when the incore state is getting so far
458 		 * ahead of the disk that the kernel memory pool is being
459 		 * threatened with exhaustion.
460 		 */
461 		delta = rushjob - ctx->syncer_rushjob;
462 		if ((u_int)delta > syncdelay / 2) {
463 			ctx->syncer_rushjob = rushjob - syncdelay / 2;
464 			tsleep(&dummy, 0, "rush", 1);
465 			continue;
466 		}
467 		if (delta) {
468 			++ctx->syncer_rushjob;
469 			tsleep(&dummy, 0, "rush", 1);
470 			continue;
471 		}
472 
473 		/*
474 		 * Normal syncer operation iterates once a second, unless
475 		 * specifically triggered.
476 		 */
477 		if (time_uptime == starttime &&
478 		    ctx->syncer_trigger == 0) {
479 			tsleep_interlock(ctx, 0);
480 			if (time_uptime == starttime &&
481 			    ctx->syncer_trigger == 0) {
482 				tsleep(ctx, PINTERLOCKED, "syncer", hz);
483 			}
484 		}
485 	}
486 
487 	/*
488 	 * Unmount/exit path for per-filesystem syncers; sc_token held
489 	 */
490 	ctx->sc_flags |= SC_FLAG_DONE;
491 	sc_flagsp = &ctx->sc_flags;
492 	lwkt_reltoken(&ctx->sc_token);
493 	wakeup(sc_flagsp);
494 
495 	kthread_exit();
496 }
497 
498 /*
499  * This allows a filesystem to pro-actively request that a dirty
500  * vnode be fsync()d.  This routine does not guarantee that one
501  * will actually be fsynced.
502  */
503 void
504 vn_syncer_one(struct mount *mp)
505 {
506 	struct syncer_ctx *ctx;
507 	struct synclist *slp;
508 	struct vnode *vp;
509 	int i;
510 	int n = syncdelay;
511 
512 	ctx = mp->mnt_syncer_ctx;
513 	i = ctx->syncer_delayno & ctx->syncer_mask;
514 	cpu_ccfence();
515 
516 	if (lwkt_trytoken(&ctx->sc_token) == 0)
517 		return;
518 
519 	/*
520 	 * Look ahead on our syncer time array.
521 	 */
522 	do {
523 		slp = &ctx->syncer_workitem_pending[i];
524 		vp = LIST_FIRST(slp);
525 		if (vp && vp->v_type == VNON)
526 			vp = LIST_NEXT(vp, v_synclist);
527 		if (vp)
528 			break;
529 		i = (i + 1) & ctx->syncer_mask;
530 		/* i will be wrong if we stop here but vp is NULL so ok */
531 	} while(--n);
532 
533 	/*
534 	 * Process one vnode, skip the syncer vnode but also stop
535 	 * if the syncer vnode is the only thing on this list.
536 	 */
537 	if (vp) {
538 		vn_syncer_add(vp, retrydelay);
539 		if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
540 			VOP_FSYNC(vp, MNT_LAZY, 0);
541 			vput(vp);
542 		}
543 	}
544 	lwkt_reltoken(&ctx->sc_token);
545 }
546 
547 /*
548  * Request that the syncer daemon for a specific mount speed up its work.
549  * If mp is NULL the caller generally wants to speed up all syncers.
550  */
551 void
552 speedup_syncer(struct mount *mp)
553 {
554 	/*
555 	 * Don't bother protecting the test.  unsleep_and_wakeup_thread()
556 	 * will only do something real if the thread is in the right state.
557 	 */
558 	atomic_add_int(&rushjob, 1);
559 	++stat_rush_requests;
560 	if (mp && mp->mnt_syncer_ctx)
561 		wakeup(mp->mnt_syncer_ctx);
562 }
563 
564 /*
565  * trigger a full sync
566  */
567 void
568 trigger_syncer(struct mount *mp)
569 {
570 	struct syncer_ctx *ctx;
571 
572 	if (mp && (ctx = mp->mnt_syncer_ctx) != NULL) {
573 		if (ctx->syncer_trigger == 0) {
574 			ctx->syncer_trigger = 1;
575 			wakeup(ctx);
576 		}
577 	}
578 }
579 
580 /*
581  * Routine to create and manage a filesystem syncer vnode.
582  */
583 static int sync_close(struct vop_close_args *);
584 static int sync_fsync(struct vop_fsync_args *);
585 static int sync_inactive(struct vop_inactive_args *);
586 static int sync_reclaim (struct vop_reclaim_args *);
587 static int sync_print(struct vop_print_args *);
588 
589 static struct vop_ops sync_vnode_vops = {
590 	.vop_default =	vop_eopnotsupp,
591 	.vop_close =	sync_close,
592 	.vop_fsync =	sync_fsync,
593 	.vop_inactive =	sync_inactive,
594 	.vop_reclaim =	sync_reclaim,
595 	.vop_print =	sync_print,
596 };
597 
598 static struct vop_ops *sync_vnode_vops_p = &sync_vnode_vops;
599 
600 VNODEOP_SET(sync_vnode_vops);
601 
602 /*
603  * Create a new filesystem syncer vnode for the specified mount point.
604  * This vnode is placed on the worklist and is responsible for sync'ing
605  * the filesystem.
606  *
607  * NOTE: read-only mounts are also placed on the worklist.  The filesystem
608  * sync code is also responsible for cleaning up vnodes.
609  */
610 int
611 vfs_allocate_syncvnode(struct mount *mp)
612 {
613 	struct vnode *vp;
614 	static long start, incr, next;
615 	int error;
616 
617 	/* Allocate a new vnode */
618 	error = getspecialvnode(VT_VFS, mp, &sync_vnode_vops_p, &vp, 0, 0);
619 	if (error) {
620 		mp->mnt_syncer = NULL;
621 		return (error);
622 	}
623 	vp->v_type = VNON;
624 	/*
625 	 * Place the vnode onto the syncer worklist. We attempt to
626 	 * scatter them about on the list so that they will go off
627 	 * at evenly distributed times even if all the filesystems
628 	 * are mounted at once.
629 	 */
630 	next += incr;
631 	if (next == 0 || next > SYNCER_MAXDELAY) {
632 		start /= 2;
633 		incr /= 2;
634 		if (start == 0) {
635 			start = SYNCER_MAXDELAY / 2;
636 			incr = SYNCER_MAXDELAY;
637 		}
638 		next = start;
639 	}
640 
641 	/*
642 	 * Only put the syncer vnode onto the syncer list if we have a
643 	 * syncer thread.  Some VFS's (aka NULLFS) don't need a syncer
644 	 * thread.
645 	 */
646 	if (mp->mnt_syncer_ctx)
647 		vn_syncer_add(vp, syncdelay > 0 ? next % syncdelay : 0);
648 
649 	/*
650 	 * The mnt_syncer field inherits the vnode reference, which is
651 	 * held until later decomissioning.
652 	 */
653 	mp->mnt_syncer = vp;
654 	vx_unlock(vp);
655 	return (0);
656 }
657 
658 static int
659 sync_close(struct vop_close_args *ap)
660 {
661 	return (0);
662 }
663 
664 /*
665  * Do a lazy sync of the filesystem.
666  *
667  * sync_fsync { struct vnode *a_vp, int a_waitfor }
668  */
669 static int
670 sync_fsync(struct vop_fsync_args *ap)
671 {
672 	struct vnode *syncvp = ap->a_vp;
673 	struct mount *mp = syncvp->v_mount;
674 	int asyncflag;
675 
676 	/*
677 	 * We only need to do something if this is a lazy evaluation.
678 	 */
679 	if ((ap->a_waitfor & MNT_LAZY) == 0)
680 		return (0);
681 
682 	/*
683 	 * Move ourselves to the back of the sync list.
684 	 */
685 	vn_syncer_add(syncvp, syncdelay);
686 
687 	/*
688 	 * Walk the list of vnodes pushing all that are dirty and
689 	 * not already on the sync list, and freeing vnodes which have
690 	 * no refs and whos VM objects are empty.  vfs_msync() handles
691 	 * the VM issues and must be called whether the mount is readonly
692 	 * or not.
693 	 */
694 	if (vfs_busy(mp, LK_NOWAIT) != 0)
695 		return (0);
696 	if (mp->mnt_flag & MNT_RDONLY) {
697 		vfs_msync(mp, MNT_NOWAIT);
698 	} else {
699 		asyncflag = mp->mnt_flag & MNT_ASYNC;
700 		mp->mnt_flag &= ~MNT_ASYNC;	/* ZZZ hack */
701 		vfs_msync(mp, MNT_NOWAIT);
702 		VFS_SYNC(mp, MNT_NOWAIT | MNT_LAZY);
703 		if (asyncflag)
704 			mp->mnt_flag |= MNT_ASYNC;
705 	}
706 	vfs_unbusy(mp);
707 	return (0);
708 }
709 
710 /*
711  * The syncer vnode is no longer referenced.
712  *
713  * sync_inactive { struct vnode *a_vp, struct proc *a_p }
714  */
715 static int
716 sync_inactive(struct vop_inactive_args *ap)
717 {
718 	vgone_vxlocked(ap->a_vp);
719 	return (0);
720 }
721 
722 /*
723  * The syncer vnode is no longer needed and is being decommissioned.
724  * This can only occur when the last reference has been released on
725  * mp->mnt_syncer, so mp->mnt_syncer had better be NULL.
726  *
727  * Modifications to the worklist must be protected with a critical
728  * section.
729  *
730  *	sync_reclaim { struct vnode *a_vp }
731  */
732 static int
733 sync_reclaim(struct vop_reclaim_args *ap)
734 {
735 	struct vnode *vp = ap->a_vp;
736 	struct syncer_ctx *ctx;
737 
738 	ctx = vp->v_mount->mnt_syncer_ctx;
739 	if (ctx) {
740 		lwkt_gettoken(&ctx->sc_token);
741 		KKASSERT(vp->v_mount->mnt_syncer != vp);
742 		if (vp->v_flag & VONWORKLST) {
743 			LIST_REMOVE(vp, v_synclist);
744 			vclrflags(vp, VONWORKLST);
745 			--ctx->syncer_count;
746 		}
747 		lwkt_reltoken(&ctx->sc_token);
748 	} else {
749 		KKASSERT((vp->v_flag & VONWORKLST) == 0);
750 	}
751 
752 	return (0);
753 }
754 
755 /*
756  * This is very similar to vmntvnodescan() but it only scans the
757  * vnodes on the syncer list.  VFS's which support faster VFS_SYNC
758  * operations use the VISDIRTY flag on the vnode to ensure that vnodes
759  * with dirty inodes are added to the syncer in addition to vnodes
760  * with dirty buffers, and can use this function instead of nmntvnodescan().
761  *
762  * This scan does not issue VOP_FSYNC()s.  The supplied callback is intended
763  * to synchronize the file in the manner intended by the VFS using it.
764  *
765  * This is important when a system has millions of vnodes.
766  */
767 int
768 vsyncscan(
769     struct mount *mp,
770     int vmsc_flags,
771     int (*slowfunc)(struct mount *mp, struct vnode *vp, void *data),
772     void *data
773 ) {
774 	struct syncer_ctx *ctx;
775 	struct synclist *slp;
776 	struct vnode *vp;
777 	int i;
778 	int count;
779 	int lkflags;
780 
781 	if (vmsc_flags & VMSC_NOWAIT)
782 		lkflags = LK_NOWAIT;
783 	else
784 		lkflags = 0;
785 
786 	/*
787 	 * Syncer list context.  This API requires a dedicated syncer thread.
788 	 * (MNTK_THR_SYNC).
789 	 */
790 	KKASSERT(mp->mnt_kern_flag & MNTK_THR_SYNC);
791 	ctx = mp->mnt_syncer_ctx;
792 	lwkt_gettoken(&ctx->sc_token);
793 
794 	/*
795 	 * Setup for loop.  Allow races against the syncer thread but
796 	 * require that the syncer thread no be lazy if we were told
797 	 * not to be lazy.
798 	 */
799 	i = ctx->syncer_delayno & ctx->syncer_mask;
800 	if ((vmsc_flags & VMSC_NOWAIT) == 0)
801 		++ctx->syncer_forced;
802 	for (count = 0; count <= ctx->syncer_mask; ++count) {
803 		slp = &ctx->syncer_workitem_pending[i];
804 
805 		while ((vp = LIST_FIRST(slp)) != NULL) {
806 			KKASSERT(vp->v_mount == mp);
807 			if (vmsc_flags & VMSC_GETVP) {
808 				if (vget(vp, LK_EXCLUSIVE | lkflags) == 0) {
809 					slowfunc(mp, vp, data);
810 					vput(vp);
811 				}
812 			} else if (vmsc_flags & VMSC_GETVX) {
813 				vx_get(vp);
814 				slowfunc(mp, vp, data);
815 				vx_put(vp);
816 			} else {
817 				vhold(vp);
818 				slowfunc(mp, vp, data);
819 				vdrop(vp);
820 			}
821 
822 			/*
823 			 * vp could be invalid.  However, if vp is still at
824 			 * the head of the list it is clearly valid and we
825 			 * can safely move it.
826 			 */
827 			if (LIST_FIRST(slp) == vp)
828 				vn_syncer_add(vp, -(i + syncdelay));
829 		}
830 		i = (i + 1) & ctx->syncer_mask;
831 	}
832 
833 	if ((vmsc_flags & VMSC_NOWAIT) == 0)
834 		--ctx->syncer_forced;
835 	lwkt_reltoken(&ctx->sc_token);
836 	return(0);
837 }
838 
839 /*
840  * Print out a syncer vnode.
841  *
842  *	sync_print { struct vnode *a_vp }
843  */
844 static int
845 sync_print(struct vop_print_args *ap)
846 {
847 	struct vnode *vp = ap->a_vp;
848 
849 	kprintf("syncer vnode");
850 	lockmgr_printinfo(&vp->v_lock);
851 	kprintf("\n");
852 	return (0);
853 }
854 
855