kernel - Optimize vfs_msync() when MNTK_THR_SYNC is used
[dragonfly.git] / sys / kern / vfs_sync.c
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 #include <sys/thread2.h>
79
80 /*
81  * The workitem queue.
82  */
83 #define SYNCER_MAXDELAY         32
84 static int sysctl_kern_syncdelay(SYSCTL_HANDLER_ARGS);
85 time_t syncdelay = 30;          /* max time to delay syncing data */
86 SYSCTL_PROC(_kern, OID_AUTO, syncdelay, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
87                 sysctl_kern_syncdelay, "I", "VFS data synchronization delay");
88 time_t filedelay = 30;          /* time to delay syncing files */
89 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW,
90                 &filedelay, 0, "File synchronization delay");
91 time_t dirdelay = 29;           /* time to delay syncing directories */
92 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW,
93                 &dirdelay, 0, "Directory synchronization delay");
94 time_t metadelay = 28;          /* time to delay syncing metadata */
95 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW,
96                 &metadelay, 0, "VFS metadata synchronization delay");
97 static int rushjob;                     /* number of slots to run ASAP */
98 static int stat_rush_requests;  /* number of times I/O speeded up */
99 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW,
100                 &stat_rush_requests, 0, "");
101
102 LIST_HEAD(synclist, vnode);
103
104 #define SC_FLAG_EXIT            (0x1)           /* request syncer exit */
105 #define SC_FLAG_DONE            (0x2)           /* syncer confirm exit */
106 #define         SC_FLAG_BIOOPS_ALL      (0x4)           /* do bufops_sync(NULL) */
107
108 struct syncer_ctx {
109         struct mount            *sc_mp;
110         struct lwkt_token       sc_token;
111         struct thread           *sc_thread;
112         int                     sc_flags;
113
114         struct synclist         *syncer_workitem_pending;
115         long                    syncer_mask;
116         int                     syncer_delayno;
117         int                     syncer_forced;
118 };
119
120 static struct syncer_ctx syncer_ctx0;
121
122 static void syncer_thread(void *);
123
124 static void
125 syncer_ctx_init(struct syncer_ctx *ctx, struct mount *mp)
126 {
127         ctx->sc_mp = mp; 
128         ctx->sc_flags = 0;
129         ctx->syncer_workitem_pending = hashinit(SYNCER_MAXDELAY, M_DEVBUF,
130                                                 &ctx->syncer_mask);
131         ctx->syncer_delayno = 0;
132         lwkt_token_init(&ctx->sc_token, "syncer");
133 }
134
135 /*
136  * Called from vfsinit()
137  */
138 void
139 vfs_sync_init(void)
140 {
141         syncer_ctx_init(&syncer_ctx0, NULL);
142         syncer_ctx0.sc_flags |= SC_FLAG_BIOOPS_ALL;
143         
144         /* Support schedcpu wakeup of syncer0 */
145         lbolt_syncer = &syncer_ctx0;
146 }
147
148 static int
149 sysctl_kern_syncdelay(SYSCTL_HANDLER_ARGS)
150 {
151         int error;
152         int v = syncdelay;
153
154         error = sysctl_handle_int(oidp, &v, 0, req);
155         if (error || !req->newptr)
156                 return (error);
157         if (v < 1)
158                 v = 1;
159         if (v > SYNCER_MAXDELAY)
160                 v = SYNCER_MAXDELAY;
161         syncdelay = v;
162
163         return(0);
164 }
165
166 static struct syncer_ctx *
167 vn_get_syncer(struct vnode *vp)
168 {
169         struct mount *mp;
170         struct syncer_ctx *ctx;
171
172         if ((mp = vp->v_mount) != NULL)
173                 ctx = mp->mnt_syncer_ctx;
174         else
175                 ctx = &syncer_ctx0;
176         return (ctx);
177 }
178
179 /*
180  * The workitem queue.
181  * 
182  * It is useful to delay writes of file data and filesystem metadata
183  * for tens of seconds so that quickly created and deleted files need
184  * not waste disk bandwidth being created and removed. To realize this,
185  * we append vnodes to a "workitem" queue. When running with a soft
186  * updates implementation, most pending metadata dependencies should
187  * not wait for more than a few seconds. Thus, mounted on block devices
188  * are delayed only about a half the time that file data is delayed.
189  * Similarly, directory updates are more critical, so are only delayed
190  * about a third the time that file data is delayed. Thus, there are
191  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
192  * one each second (driven off the filesystem syncer process). The
193  * syncer_delayno variable indicates the next queue that is to be processed.
194  * Items that need to be processed soon are placed in this queue:
195  *
196  *      syncer_workitem_pending[syncer_delayno]
197  *
198  * A delay of fifteen seconds is done by placing the request fifteen
199  * entries later in the queue:
200  *
201  *      syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
202  *
203  */
204
205 /*
206  * Add an item to the syncer work queue.
207  *
208  * WARNING: Cannot get vp->v_token here if not already held, we must
209  *          depend on the syncer_token (which might already be held by
210  *          the caller) to protect v_synclist and VONWORKLST.
211  *
212  * MPSAFE
213  */
214 void
215 vn_syncer_add(struct vnode *vp, int delay)
216 {
217         struct syncer_ctx *ctx;
218         int slot;
219
220         ctx = vn_get_syncer(vp);
221
222         lwkt_gettoken(&ctx->sc_token);
223
224         if (vp->v_flag & VONWORKLST)
225                 LIST_REMOVE(vp, v_synclist);
226         if (delay <= 0) {
227                 slot = -delay & ctx->syncer_mask;
228         } else {
229                 if (delay > SYNCER_MAXDELAY - 2)
230                         delay = SYNCER_MAXDELAY - 2;
231                 slot = (ctx->syncer_delayno + delay) & ctx->syncer_mask;
232         }
233
234         LIST_INSERT_HEAD(&ctx->syncer_workitem_pending[slot], vp, v_synclist);
235         vsetflags(vp, VONWORKLST);
236
237         lwkt_reltoken(&ctx->sc_token);
238 }
239
240 /*
241  * Removes the vnode from the syncer list.  Since we might block while
242  * acquiring the syncer_token we have to recheck conditions.
243  *
244  * vp->v_token held on call
245  */
246 void
247 vn_syncer_remove(struct vnode *vp)
248 {
249         struct syncer_ctx *ctx;
250
251         ctx = vn_get_syncer(vp);
252
253         lwkt_gettoken(&ctx->sc_token);
254
255         if ((vp->v_flag & (VISDIRTY | VONWORKLST | VOBJDIRTY)) == VONWORKLST &&
256             RB_EMPTY(&vp->v_rbdirty_tree)) {
257                 vclrflags(vp, VONWORKLST);
258                 LIST_REMOVE(vp, v_synclist);
259         }
260
261         lwkt_reltoken(&ctx->sc_token);
262 }
263
264 /*
265  * vnode must be locked
266  */
267 void
268 vclrisdirty(struct vnode *vp)
269 {
270         vclrflags(vp, VISDIRTY);
271         if (vp->v_flag & VONWORKLST)
272                 vn_syncer_remove(vp);
273 }
274
275 /*
276  * vnode must be stable
277  */
278 void
279 vsetisdirty(struct vnode *vp)
280 {
281         if ((vp->v_flag & VISDIRTY) == 0) {
282                 vsetflags(vp, VISDIRTY);
283                 vn_syncer_add(vp, syncdelay);
284         }
285 }
286
287 /*
288  * Create per-filesystem syncer process
289  */
290 void
291 vn_syncer_thr_create(struct mount *mp)
292 {
293         struct syncer_ctx *ctx;
294         static int syncalloc = 0;
295         int rc;
296
297         if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
298                 ctx = kmalloc(sizeof(struct syncer_ctx), M_TEMP,
299                               M_WAITOK | M_ZERO);
300                 syncer_ctx_init(ctx, mp);
301                 mp->mnt_syncer_ctx = ctx;
302                 rc = kthread_create(syncer_thread, ctx, &ctx->sc_thread, 
303                                     "syncer%d", ++syncalloc);
304         } else {
305                 mp->mnt_syncer_ctx = &syncer_ctx0;
306         }
307 }
308
309 /*
310  * Stop per-filesystem syncer process
311  */
312 void
313 vn_syncer_thr_stop(struct mount *mp)
314 {
315         struct syncer_ctx *ctx;
316
317         ctx = mp->mnt_syncer_ctx;
318         if (ctx == NULL || ctx == &syncer_ctx0)
319                 return;
320         KKASSERT(mp->mnt_kern_flag & MNTK_THR_SYNC);
321
322         lwkt_gettoken(&ctx->sc_token);
323
324         /* Signal the syncer process to exit */
325         ctx->sc_flags |= SC_FLAG_EXIT;
326         wakeup(ctx);
327         
328         /* Wait till syncer process exits */
329         while ((ctx->sc_flags & SC_FLAG_DONE) == 0) 
330                 tsleep(&ctx->sc_flags, 0, "syncexit", hz);
331
332         mp->mnt_syncer_ctx = NULL;
333         lwkt_reltoken(&ctx->sc_token);
334
335         hashdestroy(ctx->syncer_workitem_pending, M_DEVBUF, ctx->syncer_mask);
336         kfree(ctx, M_TEMP);
337 }
338
339 struct  thread *updatethread;
340
341 /*
342  * System filesystem synchronizer daemon.
343  */
344 static void
345 syncer_thread(void *_ctx)
346 {
347         struct thread *td = curthread;
348         struct syncer_ctx *ctx = _ctx;
349         struct synclist *slp;
350         struct vnode *vp;
351         long starttime;
352         int *sc_flagsp;
353         int sc_flags;
354         int vnodes_synced = 0;
355
356         /*
357          * syncer0 runs till system shutdown; per-filesystem syncers are
358          * terminated on filesystem unmount
359          */
360         if (ctx == &syncer_ctx0) 
361                 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
362                                       SHUTDOWN_PRI_LAST);
363         for (;;) {
364                 kproc_suspend_loop();
365
366                 starttime = time_uptime;
367                 lwkt_gettoken(&ctx->sc_token);
368
369                 /*
370                  * Push files whose dirty time has expired.  Be careful
371                  * of interrupt race on slp queue.
372                  */
373                 slp = &ctx->syncer_workitem_pending[ctx->syncer_delayno];
374                 ctx->syncer_delayno = (ctx->syncer_delayno + 1) &
375                                       ctx->syncer_mask;
376
377                 while ((vp = LIST_FIRST(slp)) != NULL) {
378                         if (ctx->syncer_forced) {
379                                 if (vget(vp, LK_EXCLUSIVE) == 0) {
380                                         VOP_FSYNC(vp, MNT_NOWAIT, 0);
381                                         vput(vp);
382                                         vnodes_synced++;
383                                 }
384                         } else {
385                                 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
386                                         VOP_FSYNC(vp, MNT_LAZY, 0);
387                                         vput(vp);
388                                         vnodes_synced++;
389                                 }
390                         }
391
392                         /*
393                          * vp is stale but can still be used if we can
394                          * verify that it remains at the head of the list.
395                          * Be careful not to try to get vp->v_token as
396                          * vp can become stale if this blocks.
397                          *
398                          * If the vp is still at the head of the list were
399                          * unable to completely flush it and move it to
400                          * a later slot to give other vnodes a fair shot.
401                          *
402                          * Note that v_tag VT_VFS vnodes can remain on the
403                          * worklist with no dirty blocks, but sync_fsync()
404                          * moves it to a later slot so we will never see it
405                          * here.
406                          *
407                          * It is possible to race a vnode with no dirty
408                          * buffers being removed from the list.  If this
409                          * occurs we will move the vnode in the synclist
410                          * and then the other thread will remove it.  Do
411                          * not try to remove it here.
412                          */
413                         if (LIST_FIRST(slp) == vp)
414                                 vn_syncer_add(vp, syncdelay);
415                 }
416
417                 sc_flags = ctx->sc_flags;
418
419                 /* Exit on unmount */
420                 if (sc_flags & SC_FLAG_EXIT)
421                         break;
422
423                 lwkt_reltoken(&ctx->sc_token);
424
425                 /*
426                  * Do sync processing for each mount.
427                  */
428                 if (ctx->sc_mp || sc_flags & SC_FLAG_BIOOPS_ALL)
429                         bio_ops_sync(ctx->sc_mp);
430
431                 /*
432                  * The variable rushjob allows the kernel to speed up the
433                  * processing of the filesystem syncer process. A rushjob
434                  * value of N tells the filesystem syncer to process the next
435                  * N seconds worth of work on its queue ASAP. Currently rushjob
436                  * is used by the soft update code to speed up the filesystem
437                  * syncer process when the incore state is getting so far
438                  * ahead of the disk that the kernel memory pool is being
439                  * threatened with exhaustion.
440                  */
441                 if (ctx == &syncer_ctx0 && rushjob > 0) {
442                         atomic_subtract_int(&rushjob, 1);
443                         continue;
444                 }
445                 /*
446                  * If it has taken us less than a second to process the
447                  * current work, then wait. Otherwise start right over
448                  * again. We can still lose time if any single round
449                  * takes more than two seconds, but it does not really
450                  * matter as we are just trying to generally pace the
451                  * filesystem activity.
452                  */
453                 if (time_uptime == starttime)
454                         tsleep(ctx, 0, "syncer", hz);
455         }
456
457         /*
458          * Unmount/exit path for per-filesystem syncers; sc_token held
459          */
460         ctx->sc_flags |= SC_FLAG_DONE;
461         sc_flagsp = &ctx->sc_flags;
462         lwkt_reltoken(&ctx->sc_token);
463         wakeup(sc_flagsp);
464
465         kthread_exit();
466 }
467
468 static void
469 syncer_thread_start(void)
470 {
471         syncer_thread(&syncer_ctx0);
472 }
473
474 static struct kproc_desc up_kp = {
475         "syncer0",
476         syncer_thread_start,
477         &updatethread
478 };
479 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
480
481 /*
482  * Request the syncer daemon to speed up its work.
483  * We never push it to speed up more than half of its
484  * normal turn time, otherwise it could take over the cpu.
485  */
486 int
487 speedup_syncer(void)
488 {
489         /*
490          * Don't bother protecting the test.  unsleep_and_wakeup_thread()
491          * will only do something real if the thread is in the right state.
492          */
493         wakeup(lbolt_syncer);
494         if (rushjob < syncdelay / 2) {
495                 atomic_add_int(&rushjob, 1);
496                 stat_rush_requests += 1;
497                 return (1);
498         }
499         return(0);
500 }
501
502 /*
503  * Routine to create and manage a filesystem syncer vnode.
504  */
505 static int sync_close(struct vop_close_args *);
506 static int sync_fsync(struct vop_fsync_args *);
507 static int sync_inactive(struct vop_inactive_args *);
508 static int sync_reclaim (struct vop_reclaim_args *);
509 static int sync_print(struct vop_print_args *);
510
511 static struct vop_ops sync_vnode_vops = {
512         .vop_default =  vop_eopnotsupp,
513         .vop_close =    sync_close,
514         .vop_fsync =    sync_fsync,
515         .vop_inactive = sync_inactive,
516         .vop_reclaim =  sync_reclaim,
517         .vop_print =    sync_print,
518 };
519
520 static struct vop_ops *sync_vnode_vops_p = &sync_vnode_vops;
521
522 VNODEOP_SET(sync_vnode_vops);
523
524 /*
525  * Create a new filesystem syncer vnode for the specified mount point.
526  * This vnode is placed on the worklist and is responsible for sync'ing
527  * the filesystem.
528  *
529  * NOTE: read-only mounts are also placed on the worklist.  The filesystem
530  * sync code is also responsible for cleaning up vnodes.
531  */
532 int
533 vfs_allocate_syncvnode(struct mount *mp)
534 {
535         struct vnode *vp;
536         static long start, incr, next;
537         int error;
538
539         /* Allocate a new vnode */
540         error = getspecialvnode(VT_VFS, mp, &sync_vnode_vops_p, &vp, 0, 0);
541         if (error) {
542                 mp->mnt_syncer = NULL;
543                 return (error);
544         }
545         vp->v_type = VNON;
546         /*
547          * Place the vnode onto the syncer worklist. We attempt to
548          * scatter them about on the list so that they will go off
549          * at evenly distributed times even if all the filesystems
550          * are mounted at once.
551          */
552         next += incr;
553         if (next == 0 || next > SYNCER_MAXDELAY) {
554                 start /= 2;
555                 incr /= 2;
556                 if (start == 0) {
557                         start = SYNCER_MAXDELAY / 2;
558                         incr = SYNCER_MAXDELAY;
559                 }
560                 next = start;
561         }
562         vn_syncer_add(vp, syncdelay > 0 ? next % syncdelay : 0);
563
564         /*
565          * The mnt_syncer field inherits the vnode reference, which is
566          * held until later decomissioning.
567          */
568         mp->mnt_syncer = vp;
569         vx_unlock(vp);
570         return (0);
571 }
572
573 static int
574 sync_close(struct vop_close_args *ap)
575 {
576         return (0);
577 }
578
579 /*
580  * Do a lazy sync of the filesystem.
581  *
582  * sync_fsync { struct vnode *a_vp, int a_waitfor }
583  */
584 static int
585 sync_fsync(struct vop_fsync_args *ap)
586 {
587         struct vnode *syncvp = ap->a_vp;
588         struct mount *mp = syncvp->v_mount;
589         int asyncflag;
590
591         /*
592          * We only need to do something if this is a lazy evaluation.
593          */
594         if ((ap->a_waitfor & MNT_LAZY) == 0)
595                 return (0);
596
597         /*
598          * Move ourselves to the back of the sync list.
599          */
600         vn_syncer_add(syncvp, syncdelay);
601
602         /*
603          * Walk the list of vnodes pushing all that are dirty and
604          * not already on the sync list, and freeing vnodes which have
605          * no refs and whos VM objects are empty.  vfs_msync() handles
606          * the VM issues and must be called whether the mount is readonly
607          * or not.
608          */
609         if (vfs_busy(mp, LK_NOWAIT) != 0)
610                 return (0);
611         if (mp->mnt_flag & MNT_RDONLY) {
612                 vfs_msync(mp, MNT_NOWAIT);
613         } else {
614                 asyncflag = mp->mnt_flag & MNT_ASYNC;
615                 mp->mnt_flag &= ~MNT_ASYNC;     /* ZZZ hack */
616                 vfs_msync(mp, MNT_NOWAIT);
617                 VFS_SYNC(mp, MNT_NOWAIT | MNT_LAZY);
618                 if (asyncflag)
619                         mp->mnt_flag |= MNT_ASYNC;
620         }
621         vfs_unbusy(mp);
622         return (0);
623 }
624
625 /*
626  * The syncer vnode is no longer referenced.
627  *
628  * sync_inactive { struct vnode *a_vp, struct proc *a_p }
629  */
630 static int
631 sync_inactive(struct vop_inactive_args *ap)
632 {
633         vgone_vxlocked(ap->a_vp);
634         return (0);
635 }
636
637 /*
638  * The syncer vnode is no longer needed and is being decommissioned.
639  * This can only occur when the last reference has been released on
640  * mp->mnt_syncer, so mp->mnt_syncer had better be NULL.
641  *
642  * Modifications to the worklist must be protected with a critical
643  * section.
644  *
645  *      sync_reclaim { struct vnode *a_vp }
646  */
647 static int
648 sync_reclaim(struct vop_reclaim_args *ap)
649 {
650         struct vnode *vp = ap->a_vp;
651         struct syncer_ctx *ctx;
652
653         ctx = vn_get_syncer(vp);
654
655         lwkt_gettoken(&ctx->sc_token);
656         KKASSERT(vp->v_mount->mnt_syncer != vp);
657         if (vp->v_flag & VONWORKLST) {
658                 LIST_REMOVE(vp, v_synclist);
659                 vclrflags(vp, VONWORKLST);
660         }
661         lwkt_reltoken(&ctx->sc_token);
662
663         return (0);
664 }
665
666 /*
667  * This is very similar to vmntvnodescan() but it only scans the
668  * vnodes on the syncer list.  VFS's which support faster VFS_SYNC
669  * operations use the VISDIRTY flag on the vnode to ensure that vnodes
670  * with dirty inodes are added to the syncer in addition to vnodes
671  * with dirty buffers, and can use this function instead of nmntvnodescan().
672  * 
673  * This is important when a system has millions of vnodes.
674  */
675 int
676 vsyncscan(
677     struct mount *mp,
678     int vmsc_flags,
679     int (*slowfunc)(struct mount *mp, struct vnode *vp, void *data),
680     void *data
681 ) {
682         struct syncer_ctx *ctx;
683         struct synclist *slp;
684         struct vnode *vp;
685         int b;
686         int i;
687         int lkflags;
688
689         if (vmsc_flags & VMSC_NOWAIT)
690                 lkflags = LK_NOWAIT;
691         else
692                 lkflags = 0;
693
694         /*
695          * Syncer list context.  This API requires a dedicated syncer thread.
696          * (MNTK_THR_SYNC).
697          */
698         KKASSERT(mp->mnt_kern_flag & MNTK_THR_SYNC);
699         ctx = mp->mnt_syncer_ctx;
700         KKASSERT(ctx != &syncer_ctx0);
701
702         lwkt_gettoken(&ctx->sc_token);
703
704         /*
705          * Setup for loop.  Allow races against the syncer thread but
706          * require that the syncer thread no be lazy if we were told
707          * not to be lazy.
708          */
709         b = ctx->syncer_delayno & ctx->syncer_mask;
710         i = b;
711         if ((vmsc_flags & VMSC_NOWAIT) == 0)
712                 ++ctx->syncer_forced;
713
714         do {
715                 slp = &ctx->syncer_workitem_pending[i];
716
717                 while ((vp = LIST_FIRST(slp)) != NULL) {
718                         KKASSERT(vp->v_mount == mp);
719                         if (vget(vp, LK_EXCLUSIVE | lkflags) == 0) {
720                                 slowfunc(mp, vp, data);
721                                 vput(vp);
722                         }
723                         if (LIST_FIRST(slp) == vp)
724                                 vn_syncer_add(vp, -(i + syncdelay));
725                 }
726                 i = (i + 1) & ctx->syncer_mask;
727         } while (i != b);
728
729         if ((vmsc_flags & VMSC_NOWAIT) == 0)
730                 --ctx->syncer_forced;
731         lwkt_reltoken(&ctx->sc_token);
732         return(0);
733 }
734
735 /*
736  * Print out a syncer vnode.
737  *
738  *      sync_print { struct vnode *a_vp }
739  */
740 static int
741 sync_print(struct vop_print_args *ap)
742 {
743         struct vnode *vp = ap->a_vp;
744
745         kprintf("syncer vnode");
746         lockmgr_printinfo(&vp->v_lock);
747         kprintf("\n");
748         return (0);
749 }
750