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