xref: /openbsd/sys/kern/vfs_sync.c (revision 133306f0)
1 /*       $OpenBSD: vfs_sync.c,v 1.12 2000/03/23 15:57:33 art Exp $  */
2 
3 /*
4  *  Portions of this code are:
5  *
6  * Copyright (c) 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 /*
44  * Syncer daemon
45  */
46 
47 #include <sys/queue.h>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/buf.h>
54 #include <sys/malloc.h>
55 
56 #include <sys/kernel.h>
57 
58 /*
59  * The workitem queue.
60  */
61 #define SYNCER_MAXDELAY	32		/* maximum sync delay time */
62 #define SYNCER_DEFAULT 30		/* default sync delay time */
63 int syncer_maxdelay = SYNCER_MAXDELAY;	/* maximum delay time */
64 time_t syncdelay = SYNCER_DEFAULT;	/* time to delay syncing vnodes */
65 
66 int rushjob = 0;			/* number of slots to run ASAP */
67 int stat_rush_requests = 0;		/* number of rush requests */
68 
69 static int syncer_delayno = 0;
70 static long syncer_last;
71 LIST_HEAD(synclist, vnode);
72 static struct synclist *syncer_workitem_pending;
73 
74 extern struct simplelock mountlist_slock;
75 
76 struct proc *syncerproc;
77 
78 /*
79  * The workitem queue.
80  *
81  * It is useful to delay writes of file data and filesystem metadata
82  * for tens of seconds so that quickly created and deleted files need
83  * not waste disk bandwidth being created and removed. To realize this,
84  * we append vnodes to a "workitem" queue. When running with a soft
85  * updates implementation, most pending metadata dependencies should
86  * not wait for more than a few seconds. Thus, mounted on block devices
87  * are delayed only about a half the time that file data is delayed.
88  * Similarly, directory updates are more critical, so are only delayed
89  * about a third the time that file data is delayed. Thus, there are
90  * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
91  * one each second (driven off the filesystem syner process). The
92  * syncer_delayno variable indicates the next queue that is to be processed.
93  * Items that need to be processed soon are placed in this queue:
94  *
95  *	syncer_workitem_pending[syncer_delayno]
96  *
97  * A delay of fifteen seconds is done by placing the request fifteen
98  * entries later in the queue:
99  *
100  *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
101  *
102  */
103 
104 void
105 vn_initialize_syncerd()
106 
107 {
108 	int i;
109 
110 	syncer_last = SYNCER_MAXDELAY + 2;
111 
112 	syncer_workitem_pending =
113 		malloc(syncer_last * sizeof(struct synclist),
114 		       M_VNODE, M_WAITOK);
115 
116 	for (i = 0; i < syncer_last; i++)
117 		LIST_INIT(&syncer_workitem_pending[i]);
118 }
119 
120 /*
121  * Add an item to the syncer work queue.
122  */
123 void
124 vn_syncer_add_to_worklist(vp, delay)
125 	struct vnode *vp;
126 	int delay;
127 {
128 	int s, slot;
129 
130 	s = splbio();
131 
132 	if (vp->v_flag & VONSYNCLIST)
133 		LIST_REMOVE(vp, v_synclist);
134 
135 	if (delay > syncer_maxdelay)
136 		delay = syncer_maxdelay;
137 	slot = (syncer_delayno + delay) % syncer_last;
138 	LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
139 	vp->v_flag |= VONSYNCLIST;
140 	splx(s);
141 }
142 
143 /*
144  * System filesystem synchronizer daemon.
145  */
146 
147 void
148 sched_sync(p)
149 	struct proc *p;
150 {
151 	struct synclist *slp;
152 	struct vnode *vp;
153 	long starttime;
154 	int s;
155 
156 	syncerproc = curproc;
157 
158 	for (;;) {
159 		starttime = time.tv_sec;
160 
161 		/*
162 		 * Push files whose dirty time has expired.
163 		 */
164 		s = splbio();
165 		slp = &syncer_workitem_pending[syncer_delayno];
166 		syncer_delayno += 1;
167 		if (syncer_delayno >= syncer_last)
168 			syncer_delayno = 0;
169 		splx(s);
170 		while ((vp = LIST_FIRST(slp)) != NULL) {
171 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
172 			(void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
173 			VOP_UNLOCK(vp, 0, p);
174 			if (LIST_FIRST(slp) == vp) {
175 				if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
176 				    vp->v_type != VBLK)
177 					panic("sched_sync: fsync failed");
178 				/*
179 				 * Move ourselves to the back of the sync list.
180 				 */
181 				vn_syncer_add_to_worklist(vp, syncdelay);
182 			}
183 		}
184 
185 		/*
186 		 * Do soft update processing.
187 		 */
188 		if (bioops.io_sync)
189 			(*bioops.io_sync)(NULL);
190 
191 		/*
192 		 * The variable rushjob allows the kernel to speed up the
193 		 * processing of the filesystem syncer process. A rushjob
194 		 * value of N tells the filesystem syncer to process the next
195 		 * N seconds worth of work on its queue ASAP. Currently rushjob
196 		 * is used by the soft update code to speed up the filesystem
197 		 * syncer process when the incore state is getting so far
198 		 * ahead of the disk that the kernel memory pool is being
199 		 * threatened with exhaustion.
200 		 */
201 		if (rushjob > 0) {
202 			rushjob -= 1;
203 			continue;
204 		}
205 		/*
206 		 * If it has taken us less than a second to process the
207 		 * current work, then wait. Otherwise start right over
208 		 * again. We can still lose time if any single round
209 		 * takes more than two seconds, but it does not really
210 		 * matter as we are just trying to generally pace the
211 		 * filesystem activity.
212 		 */
213 		if (time.tv_sec == starttime)
214 			tsleep(&lbolt, PPAUSE, "syncer", 0);
215 	}
216 }
217 
218 /*
219  * Request the syncer daemon to speed up its work.
220  * We never push it to speed up more than half of its
221  * normal turn time, otherwise it could take over the cpu.
222  */
223 int
224 speedup_syncer()
225 {
226 	int s;
227 
228 	s = splhigh();
229 	if (syncerproc && syncerproc->p_wchan == &lbolt)
230 		setrunnable(syncerproc);
231 	splx(s);
232 	if (rushjob < syncdelay / 2) {
233 		rushjob += 1;
234 		stat_rush_requests += 1;
235 		return 1;
236 	}
237 	return 0;
238 }
239 
240 /*
241  * Routine to create and manage a filesystem syncer vnode.
242  */
243 #define sync_close nullop
244 int   sync_fsync __P((void *));
245 int   sync_inactive __P((void *));
246 #define sync_reclaim nullop
247 #define sync_lock vop_generic_lock
248 #define sync_unlock vop_generic_unlock
249 int   sync_print __P((void *));
250 #define sync_islocked vop_generic_islocked
251 
252 int (**sync_vnodeop_p) __P((void *));
253 struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
254       { &vop_default_desc, vn_default_error },
255       { &vop_close_desc, sync_close },                /* close */
256       { &vop_fsync_desc, sync_fsync },                /* fsync */
257       { &vop_inactive_desc, sync_inactive },          /* inactive */
258       { &vop_reclaim_desc, sync_reclaim },            /* reclaim */
259       { &vop_lock_desc, sync_lock },                  /* lock */
260       { &vop_unlock_desc, sync_unlock },              /* unlock */
261       { &vop_print_desc, sync_print },                /* print */
262       { &vop_islocked_desc, sync_islocked },          /* islocked */
263       { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
264 };
265 struct vnodeopv_desc sync_vnodeop_opv_desc =
266       { &sync_vnodeop_p, sync_vnodeop_entries };
267 
268 /*
269  * Create a new filesystem syncer vnode for the specified mount point.
270  */
271 int
272 vfs_allocate_syncvnode(mp)
273 	struct mount *mp;
274 {
275 	struct vnode *vp;
276 	static long start, incr, next;
277 	int error;
278 
279 	/* Allocate a new vnode */
280 	if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
281 		mp->mnt_syncer = NULL;
282 		return (error);
283 	}
284 	vp->v_writecount = 1;
285 	vp->v_type = VNON;
286 	/*
287 	 * Place the vnode onto the syncer worklist. We attempt to
288 	 * scatter them about on the list so that they will go off
289 	 * at evenly distributed times even if all the filesystems
290 	 * are mounted at once.
291 	 */
292 	next += incr;
293 	if (next == 0 || next > syncer_maxdelay) {
294 		start /= 2;
295 		incr /= 2;
296 		if (start == 0) {
297 			start = syncer_maxdelay / 2;
298 			incr = syncer_maxdelay;
299 		}
300 		next = start;
301 	}
302 	vn_syncer_add_to_worklist(vp, next);
303 	mp->mnt_syncer = vp;
304 	return (0);
305 }
306 
307 /*
308  * Do a lazy sync of the filesystem.
309  */
310 int
311 sync_fsync(v)
312 	void *v;
313 {
314 	struct vop_fsync_args /* {
315 		struct vnode *a_vp;
316 		struct ucred *a_cred;
317 		int a_waitfor;
318 		struct proc *a_p;
319 	} */ *ap = v;
320 	struct vnode *syncvp = ap->a_vp;
321 	struct mount *mp = syncvp->v_mount;
322 	int asyncflag;
323 
324 	/*
325 	 * We only need to do something if this is a lazy evaluation.
326 	 */
327 	if (ap->a_waitfor != MNT_LAZY)
328 		return (0);
329 
330 	/*
331 	 * Move ourselves to the back of the sync list.
332 	 */
333 	vn_syncer_add_to_worklist(syncvp, syncdelay);
334 
335 	/*
336 	 * Walk the list of vnodes pushing all that are dirty and
337 	 * not already on the sync list.
338 	 */
339 	simple_lock(&mountlist_slock);
340 	if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, ap->a_p) == 0) {
341 		asyncflag = mp->mnt_flag & MNT_ASYNC;
342 		mp->mnt_flag &= ~MNT_ASYNC;
343 		VFS_SYNC(mp, MNT_LAZY, ap->a_cred, ap->a_p);
344 		if (asyncflag)
345 			mp->mnt_flag |= MNT_ASYNC;
346 		vfs_unbusy(mp, ap->a_p);
347 	} else
348 		simple_unlock(&mountlist_slock);
349 
350 	return (0);
351 }
352 
353 /*
354  * The syncer vnode is no longer needed and is being decommissioned.
355  */
356 int
357 sync_inactive(v)
358 	void *v;
359 {
360 	struct vop_inactive_args /* {
361 		struct vnode *a_vp;
362 		struct proc *a_p;
363 	} */ *ap = v;
364 
365 	struct vnode *vp = ap->a_vp;
366 
367 	if (vp->v_usecount == 0) {
368 		VOP_UNLOCK(vp, 0, ap->a_p);
369 		return (0);
370 	}
371 	vp->v_mount->mnt_syncer = NULL;
372 	LIST_REMOVE(vp, v_synclist);
373 	vp->v_writecount = 0;
374 	vput(vp);
375 	return (0);
376 }
377 
378 /*
379  * Print out a syncer vnode.
380  */
381 int
382 sync_print(v)
383 	void *v;
384 
385 {
386       struct vop_print_args /* {
387               struct vnode *a_vp;
388       } */ *ap = v;
389       struct vnode *vp = ap->a_vp;
390 
391       printf("syncer vnode");
392       if (vp->v_vnlock != NULL)
393               lockmgr_printinfo(vp->v_vnlock);
394       printf("\n");
395       return (0);
396 }
397