kernel - lwkt_token revamp
[dragonfly.git] / sys / kern / vfs_sync.c
CommitLineData
5fd012e0
MD
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR 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 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
2ec4b00d 40 * $DragonFly: src/sys/kern/vfs_sync.c,v 1.18 2008/05/18 05:54:25 dillon Exp $
5fd012e0
MD
41 */
42
43/*
44 * External virtual filesystem routines
45 */
46#include "opt_ddb.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/buf.h>
51#include <sys/conf.h>
52#include <sys/dirent.h>
53#include <sys/domain.h>
54#include <sys/eventhandler.h>
55#include <sys/fcntl.h>
56#include <sys/kernel.h>
57#include <sys/kthread.h>
58#include <sys/malloc.h>
59#include <sys/mbuf.h>
60#include <sys/mount.h>
61#include <sys/proc.h>
62#include <sys/namei.h>
63#include <sys/reboot.h>
64#include <sys/socket.h>
65#include <sys/stat.h>
66#include <sys/sysctl.h>
67#include <sys/syslog.h>
68#include <sys/vmmeter.h>
69#include <sys/vnode.h>
70
71#include <machine/limits.h>
72
73#include <vm/vm.h>
74#include <vm/vm_object.h>
75#include <vm/vm_extern.h>
76#include <vm/vm_kern.h>
77#include <vm/pmap.h>
78#include <vm/vm_map.h>
79#include <vm/vm_page.h>
80#include <vm/vm_pager.h>
81#include <vm/vnode_pager.h>
82
83#include <sys/buf2.h>
84#include <sys/thread2.h>
85
86/*
87 * The workitem queue.
88 */
89#define SYNCER_MAXDELAY 32
90static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
91time_t syncdelay = 30; /* max time to delay syncing data */
92SYSCTL_INT(_kern, OID_AUTO, syncdelay, CTLFLAG_RW,
93 &syncdelay, 0, "VFS data synchronization delay");
94time_t filedelay = 30; /* time to delay syncing files */
95SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW,
96 &filedelay, 0, "File synchronization delay");
97time_t dirdelay = 29; /* time to delay syncing directories */
98SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW,
99 &dirdelay, 0, "Directory synchronization delay");
100time_t metadelay = 28; /* time to delay syncing metadata */
101SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW,
102 &metadelay, 0, "VFS metadata synchronization delay");
103static int rushjob; /* number of slots to run ASAP */
104static int stat_rush_requests; /* number of times I/O speeded up */
105SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW,
106 &stat_rush_requests, 0, "");
107
108static int syncer_delayno = 0;
109static long syncer_mask;
0202303b 110static struct lwkt_token syncer_token;
5fd012e0
MD
111LIST_HEAD(synclist, vnode);
112static struct synclist *syncer_workitem_pending;
113
114/*
115 * Called from vfsinit()
116 */
117void
118vfs_sync_init(void)
119{
120 syncer_workitem_pending = hashinit(syncer_maxdelay, M_DEVBUF,
121 &syncer_mask);
122 syncer_maxdelay = syncer_mask + 1;
3b998fa9 123 lwkt_token_init(&syncer_token, 1);
5fd012e0
MD
124}
125
126/*
127 * The workitem queue.
128 *
129 * It is useful to delay writes of file data and filesystem metadata
130 * for tens of seconds so that quickly created and deleted files need
131 * not waste disk bandwidth being created and removed. To realize this,
132 * we append vnodes to a "workitem" queue. When running with a soft
133 * updates implementation, most pending metadata dependencies should
134 * not wait for more than a few seconds. Thus, mounted on block devices
135 * are delayed only about a half the time that file data is delayed.
136 * Similarly, directory updates are more critical, so are only delayed
137 * about a third the time that file data is delayed. Thus, there are
138 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
139 * one each second (driven off the filesystem syncer process). The
140 * syncer_delayno variable indicates the next queue that is to be processed.
141 * Items that need to be processed soon are placed in this queue:
142 *
143 * syncer_workitem_pending[syncer_delayno]
144 *
145 * A delay of fifteen seconds is done by placing the request fifteen
146 * entries later in the queue:
147 *
148 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
149 *
150 */
151
152/*
153 * Add an item to the syncer work queue.
b1c20cfa
MD
154 *
155 * MPSAFE
5fd012e0
MD
156 */
157void
158vn_syncer_add_to_worklist(struct vnode *vp, int delay)
159{
160 int slot;
161
3b998fa9 162 lwkt_gettoken(&syncer_token);
5fd012e0 163
0202303b 164 if (vp->v_flag & VONWORKLST)
5fd012e0 165 LIST_REMOVE(vp, v_synclist);
5fd012e0
MD
166 if (delay > syncer_maxdelay - 2)
167 delay = syncer_maxdelay - 2;
168 slot = (syncer_delayno + delay) & syncer_mask;
169
170 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
2247fe02 171 vsetflags(vp, VONWORKLST);
0202303b 172
3b998fa9 173 lwkt_reltoken(&syncer_token);
5fd012e0
MD
174}
175
176struct thread *updatethread;
177static void sched_sync (void);
178static struct kproc_desc up_kp = {
179 "syncer",
180 sched_sync,
181 &updatethread
182};
183SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
184
185/*
186 * System filesystem synchronizer daemon.
187 */
188void
189sched_sync(void)
190{
0202303b 191 struct thread *td = curthread;
5fd012e0
MD
192 struct synclist *slp;
193 struct vnode *vp;
194 long starttime;
5fd012e0
MD
195
196 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, td,
197 SHUTDOWN_PRI_LAST);
198
199 for (;;) {
200 kproc_suspend_loop();
201
202 starttime = time_second;
3b998fa9 203 lwkt_gettoken(&syncer_token);
5fd012e0
MD
204
205 /*
206 * Push files whose dirty time has expired. Be careful
207 * of interrupt race on slp queue.
208 */
5fd012e0
MD
209 slp = &syncer_workitem_pending[syncer_delayno];
210 syncer_delayno += 1;
211 if (syncer_delayno == syncer_maxdelay)
212 syncer_delayno = 0;
5fd012e0
MD
213
214 while ((vp = LIST_FIRST(slp)) != NULL) {
87de5057 215 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
52174f71 216 VOP_FSYNC(vp, MNT_LAZY, 0);
0e0b6202 217 vput(vp);
5fd012e0 218 }
6bae6177
MD
219
220 /*
221 * If the vnode is still at the head of the list
222 * we were not able to completely flush it. To
223 * give other vnodes a fair shake we move it to
224 * a later slot.
225 *
226 * Note that v_tag VT_VFS vnodes can remain on the
227 * worklist with no dirty blocks, but sync_fsync()
228 * moves it to a later slot so we will never see it
229 * here.
230 */
5fd012e0 231 if (LIST_FIRST(slp) == vp) {
3b998fa9 232 lwkt_gettoken(&vp->v_token);
0202303b
MD
233 if (LIST_FIRST(slp) == vp) {
234 if (RB_EMPTY(&vp->v_rbdirty_tree) &&
235 !vn_isdisk(vp, NULL)) {
236 panic("sched_sync: fsync "
237 "failed vp %p tag %d",
238 vp, vp->v_tag);
239 }
240 vn_syncer_add_to_worklist(vp, syncdelay);
6bae6177 241 }
3b998fa9 242 lwkt_reltoken(&vp->v_token);
5fd012e0 243 }
5fd012e0 244 }
3b998fa9 245 lwkt_reltoken(&syncer_token);
5fd012e0
MD
246
247 /*
408357d8 248 * Do sync processing for each mount.
5fd012e0 249 */
408357d8 250 bio_ops_sync(NULL);
5fd012e0
MD
251
252 /*
253 * The variable rushjob allows the kernel to speed up the
254 * processing of the filesystem syncer process. A rushjob
255 * value of N tells the filesystem syncer to process the next
256 * N seconds worth of work on its queue ASAP. Currently rushjob
257 * is used by the soft update code to speed up the filesystem
258 * syncer process when the incore state is getting so far
259 * ahead of the disk that the kernel memory pool is being
260 * threatened with exhaustion.
261 */
262 if (rushjob > 0) {
263 rushjob -= 1;
264 continue;
265 }
266 /*
267 * If it has taken us less than a second to process the
268 * current work, then wait. Otherwise start right over
269 * again. We can still lose time if any single round
270 * takes more than two seconds, but it does not really
271 * matter as we are just trying to generally pace the
272 * filesystem activity.
273 */
274 if (time_second == starttime)
344ad853 275 tsleep(&lbolt_syncer, 0, "syncer", 0);
5fd012e0
MD
276 }
277}
278
279/*
280 * Request the syncer daemon to speed up its work.
281 * We never push it to speed up more than half of its
282 * normal turn time, otherwise it could take over the cpu.
283 *
284 * YYY wchan field protected by the BGL.
285 */
286int
287speedup_syncer(void)
288{
344ad853
MD
289 /*
290 * Don't bother protecting the test. unsleep_and_wakeup_thread()
291 * will only do something real if the thread is in the right state.
292 */
293 wakeup(&lbolt_syncer);
5fd012e0
MD
294 if (rushjob < syncdelay / 2) {
295 rushjob += 1;
296 stat_rush_requests += 1;
297 return (1);
298 }
299 return(0);
300}
301
302/*
303 * Routine to create and manage a filesystem syncer vnode.
304 */
fef8985e
MD
305static int sync_close(struct vop_close_args *);
306static int sync_fsync(struct vop_fsync_args *);
307static int sync_inactive(struct vop_inactive_args *);
308static int sync_reclaim (struct vop_reclaim_args *);
309static int sync_print(struct vop_print_args *);
5fd012e0 310
66a1ddf5
MD
311static struct vop_ops sync_vnode_vops = {
312 .vop_default = vop_eopnotsupp,
313 .vop_close = sync_close,
314 .vop_fsync = sync_fsync,
315 .vop_inactive = sync_inactive,
316 .vop_reclaim = sync_reclaim,
66a1ddf5 317 .vop_print = sync_print,
5fd012e0
MD
318};
319
66a1ddf5 320static struct vop_ops *sync_vnode_vops_p = &sync_vnode_vops;
5fd012e0 321
66a1ddf5 322VNODEOP_SET(sync_vnode_vops);
5fd012e0
MD
323
324/*
325 * Create a new filesystem syncer vnode for the specified mount point.
326 * This vnode is placed on the worklist and is responsible for sync'ing
327 * the filesystem.
328 *
329 * NOTE: read-only mounts are also placed on the worklist. The filesystem
330 * sync code is also responsible for cleaning up vnodes.
331 */
332int
333vfs_allocate_syncvnode(struct mount *mp)
334{
335 struct vnode *vp;
336 static long start, incr, next;
337 int error;
338
339 /* Allocate a new vnode */
66a1ddf5 340 error = getspecialvnode(VT_VFS, mp, &sync_vnode_vops_p, &vp, 0, 0);
5fd012e0
MD
341 if (error) {
342 mp->mnt_syncer = NULL;
343 return (error);
344 }
345 vp->v_type = VNON;
346 /*
347 * Place the vnode onto the syncer worklist. We attempt to
348 * scatter them about on the list so that they will go off
349 * at evenly distributed times even if all the filesystems
350 * are mounted at once.
351 */
352 next += incr;
353 if (next == 0 || next > syncer_maxdelay) {
354 start /= 2;
355 incr /= 2;
356 if (start == 0) {
357 start = syncer_maxdelay / 2;
358 incr = syncer_maxdelay;
359 }
360 next = start;
361 }
362 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2ec4b00d
MD
363
364 /*
365 * The mnt_syncer field inherits the vnode reference, which is
366 * held until later decomissioning.
367 */
5fd012e0
MD
368 mp->mnt_syncer = vp;
369 vx_unlock(vp);
370 return (0);
fef8985e
MD
371}
372
373static int
374sync_close(struct vop_close_args *ap)
375{
376 return (0);
5fd012e0
MD
377}
378
379/*
380 * Do a lazy sync of the filesystem.
381 *
b478fdce 382 * sync_fsync { struct vnode *a_vp, int a_waitfor }
5fd012e0
MD
383 */
384static int
385sync_fsync(struct vop_fsync_args *ap)
386{
387 struct vnode *syncvp = ap->a_vp;
388 struct mount *mp = syncvp->v_mount;
5fd012e0
MD
389 int asyncflag;
390
391 /*
392 * We only need to do something if this is a lazy evaluation.
393 */
394 if (ap->a_waitfor != MNT_LAZY)
395 return (0);
396
397 /*
398 * Move ourselves to the back of the sync list.
399 */
400 vn_syncer_add_to_worklist(syncvp, syncdelay);
401
402 /*
403 * Walk the list of vnodes pushing all that are dirty and
404 * not already on the sync list, and freeing vnodes which have
405 * no refs and whos VM objects are empty. vfs_msync() handles
406 * the VM issues and must be called whether the mount is readonly
407 * or not.
408 */
f9642f56 409 if (vfs_busy(mp, LK_NOWAIT) != 0)
5fd012e0 410 return (0);
5fd012e0
MD
411 if (mp->mnt_flag & MNT_RDONLY) {
412 vfs_msync(mp, MNT_NOWAIT);
413 } else {
414 asyncflag = mp->mnt_flag & MNT_ASYNC;
415 mp->mnt_flag &= ~MNT_ASYNC; /* ZZZ hack */
416 vfs_msync(mp, MNT_NOWAIT);
87de5057 417 VFS_SYNC(mp, MNT_LAZY);
5fd012e0
MD
418 if (asyncflag)
419 mp->mnt_flag |= MNT_ASYNC;
420 }
f9642f56 421 vfs_unbusy(mp);
5fd012e0
MD
422 return (0);
423}
424
425/*
3c37c940 426 * The syncer vnode is no longer referenced.
5fd012e0
MD
427 *
428 * sync_inactive { struct vnode *a_vp, struct proc *a_p }
429 */
430static int
431sync_inactive(struct vop_inactive_args *ap)
432{
3c37c940 433 vgone_vxlocked(ap->a_vp);
5fd012e0
MD
434 return (0);
435}
436
437/*
438 * The syncer vnode is no longer needed and is being decommissioned.
2ec4b00d
MD
439 * This can only occur when the last reference has been released on
440 * mp->mnt_syncer, so mp->mnt_syncer had better be NULL.
5fd012e0 441 *
e43a034f
MD
442 * Modifications to the worklist must be protected with a critical
443 * section.
5fd012e0
MD
444 *
445 * sync_reclaim { struct vnode *a_vp }
446 */
447static int
448sync_reclaim(struct vop_reclaim_args *ap)
449{
450 struct vnode *vp = ap->a_vp;
5fd012e0 451
3b998fa9 452 lwkt_gettoken(&syncer_token);
2ec4b00d 453 KKASSERT(vp->v_mount->mnt_syncer != vp);
5fd012e0
MD
454 if (vp->v_flag & VONWORKLST) {
455 LIST_REMOVE(vp, v_synclist);
2247fe02 456 vclrflags(vp, VONWORKLST);
5fd012e0 457 }
3b998fa9 458 lwkt_reltoken(&syncer_token);
5fd012e0
MD
459
460 return (0);
461}
462
463/*
464 * Print out a syncer vnode.
465 *
466 * sync_print { struct vnode *a_vp }
467 */
468static int
469sync_print(struct vop_print_args *ap)
470{
471 struct vnode *vp = ap->a_vp;
472
6ea70f76 473 kprintf("syncer vnode");
5fd012e0 474 lockmgr_printinfo(&vp->v_lock);
6ea70f76 475 kprintf("\n");
5fd012e0
MD
476 return (0);
477}
478