1 /* $OpenBSD: vfs_sync.c,v 1.26 2004/06/21 23:50:36 tholo 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. 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 39 /* 40 * Syncer daemon 41 */ 42 43 #include <sys/queue.h> 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/mount.h> 48 #include <sys/vnode.h> 49 #include <sys/buf.h> 50 #include <sys/malloc.h> 51 52 #include <sys/kernel.h> 53 54 #ifdef FFS_SOFTUPDATES 55 int softdep_process_worklist(struct mount *); 56 #endif 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_mask; 71 LIST_HEAD(synclist, vnode); 72 static struct synclist *syncer_workitem_pending; 73 74 struct proc *syncerproc; 75 76 /* 77 * The workitem queue. 78 * 79 * It is useful to delay writes of file data and filesystem metadata 80 * for tens of seconds so that quickly created and deleted files need 81 * not waste disk bandwidth being created and removed. To realize this, 82 * we append vnodes to a "workitem" queue. When running with a soft 83 * updates implementation, most pending metadata dependencies should 84 * not wait for more than a few seconds. Thus, mounted on block devices 85 * are delayed only about a half the time that file data is delayed. 86 * Similarly, directory updates are more critical, so are only delayed 87 * about a third the time that file data is delayed. Thus, there are 88 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 89 * one each second (driven off the filesystem syner process). The 90 * syncer_delayno variable indicates the next queue that is to be processed. 91 * Items that need to be processed soon are placed in this queue: 92 * 93 * syncer_workitem_pending[syncer_delayno] 94 * 95 * A delay of fifteen seconds is done by placing the request fifteen 96 * entries later in the queue: 97 * 98 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 99 * 100 */ 101 102 void 103 vn_initialize_syncerd() 104 105 { 106 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, M_WAITOK, 107 &syncer_mask); 108 syncer_maxdelay = syncer_mask + 1; 109 } 110 111 /* 112 * Add an item to the syncer work queue. 113 */ 114 void 115 vn_syncer_add_to_worklist(vp, delay) 116 struct vnode *vp; 117 int delay; 118 { 119 int s, slot; 120 121 if (delay > syncer_maxdelay - 2) 122 delay = syncer_maxdelay - 2; 123 slot = (syncer_delayno + delay) & syncer_mask; 124 125 s = splbio(); 126 if (vp->v_bioflag & VBIOONSYNCLIST) 127 LIST_REMOVE(vp, v_synclist); 128 129 vp->v_bioflag |= VBIOONSYNCLIST; 130 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist); 131 splx(s); 132 } 133 134 /* 135 * System filesystem synchronizer daemon. 136 */ 137 138 void 139 sched_sync(p) 140 struct proc *p; 141 { 142 struct synclist *slp; 143 struct vnode *vp; 144 long starttime; 145 int s; 146 147 syncerproc = curproc; 148 149 for (;;) { 150 starttime = time_second; 151 152 /* 153 * Push files whose dirty time has expired. 154 */ 155 slp = &syncer_workitem_pending[syncer_delayno]; 156 syncer_delayno += 1; 157 if (syncer_delayno == syncer_maxdelay) 158 syncer_delayno = 0; 159 s = splbio(); 160 while ((vp = LIST_FIRST(slp)) != NULL) { 161 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT, p) != 0) { 162 /* 163 * If we fail to get the lock, we move this 164 * vnode one second ahead in time. 165 * XXX - no good, but the best we can do. 166 */ 167 vn_syncer_add_to_worklist(vp, 1); 168 continue; 169 } 170 splx(s); 171 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p); 172 VOP_UNLOCK(vp, 0, p); 173 s = splbio(); 174 if (LIST_FIRST(slp) == vp) { 175 /* 176 * Note: disk vps can remain on the 177 * worklist too with no dirty blocks, but 178 * since sync_fsync() moves it to a different 179 * slot we are safe. 180 */ 181 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL && 182 vp->v_type != VBLK) 183 panic("sched_sync: fsync failed"); 184 /* 185 * Put us back on the worklist. The worklist 186 * routine will remove us from our current 187 * position and then add us back in at a later 188 * position. 189 */ 190 vn_syncer_add_to_worklist(vp, syncdelay); 191 } 192 } 193 194 splx(s); 195 196 #ifdef FFS_SOFTUPDATES 197 /* 198 * Do soft update processing. 199 */ 200 softdep_process_worklist(NULL); 201 #endif 202 203 /* 204 * The variable rushjob allows the kernel to speed up the 205 * processing of the filesystem syncer process. A rushjob 206 * value of N tells the filesystem syncer to process the next 207 * N seconds worth of work on its queue ASAP. Currently rushjob 208 * is used by the soft update code to speed up the filesystem 209 * syncer process when the incore state is getting so far 210 * ahead of the disk that the kernel memory pool is being 211 * threatened with exhaustion. 212 */ 213 if (rushjob > 0) { 214 rushjob -= 1; 215 continue; 216 } 217 /* 218 * If it has taken us less than a second to process the 219 * current work, then wait. Otherwise start right over 220 * again. We can still lose time if any single round 221 * takes more than two seconds, but it does not really 222 * matter as we are just trying to generally pace the 223 * filesystem activity. 224 */ 225 if (time_second == starttime) 226 tsleep(&lbolt, PPAUSE, "syncer", 0); 227 } 228 } 229 230 /* 231 * Request the syncer daemon to speed up its work. 232 * We never push it to speed up more than half of its 233 * normal turn time, otherwise it could take over the cpu. 234 */ 235 int 236 speedup_syncer() 237 { 238 int s; 239 240 s = splhigh(); 241 if (syncerproc && syncerproc->p_wchan == &lbolt) 242 setrunnable(syncerproc); 243 splx(s); 244 if (rushjob < syncdelay / 2) { 245 rushjob += 1; 246 stat_rush_requests += 1; 247 return 1; 248 } 249 return 0; 250 } 251 252 /* 253 * Routine to create and manage a filesystem syncer vnode. 254 */ 255 #define sync_close nullop 256 int sync_fsync(void *); 257 int sync_inactive(void *); 258 #define sync_reclaim nullop 259 #define sync_lock vop_generic_lock 260 #define sync_unlock vop_generic_unlock 261 int sync_print(void *); 262 #define sync_islocked vop_generic_islocked 263 264 int (**sync_vnodeop_p)(void *); 265 struct vnodeopv_entry_desc sync_vnodeop_entries[] = { 266 { &vop_default_desc, vn_default_error }, 267 { &vop_close_desc, sync_close }, /* close */ 268 { &vop_fsync_desc, sync_fsync }, /* fsync */ 269 { &vop_inactive_desc, sync_inactive }, /* inactive */ 270 { &vop_reclaim_desc, sync_reclaim }, /* reclaim */ 271 { &vop_lock_desc, sync_lock }, /* lock */ 272 { &vop_unlock_desc, sync_unlock }, /* unlock */ 273 { &vop_print_desc, sync_print }, /* print */ 274 { &vop_islocked_desc, sync_islocked }, /* islocked */ 275 { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL } 276 }; 277 struct vnodeopv_desc sync_vnodeop_opv_desc = { 278 &sync_vnodeop_p, sync_vnodeop_entries 279 }; 280 281 /* 282 * Create a new filesystem syncer vnode for the specified mount point. 283 */ 284 int 285 vfs_allocate_syncvnode(mp) 286 struct mount *mp; 287 { 288 struct vnode *vp; 289 static long start, incr, next; 290 int error; 291 292 /* Allocate a new vnode */ 293 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) { 294 mp->mnt_syncer = NULL; 295 return (error); 296 } 297 vp->v_writecount = 1; 298 vp->v_type = VNON; 299 /* 300 * Place the vnode onto the syncer worklist. We attempt to 301 * scatter them about on the list so that they will go off 302 * at evenly distributed times even if all the filesystems 303 * are mounted at once. 304 */ 305 next += incr; 306 if (next == 0 || next > syncer_maxdelay) { 307 start /= 2; 308 incr /= 2; 309 if (start == 0) { 310 start = syncer_maxdelay / 2; 311 incr = syncer_maxdelay; 312 } 313 next = start; 314 } 315 vn_syncer_add_to_worklist(vp, next); 316 mp->mnt_syncer = vp; 317 return (0); 318 } 319 320 /* 321 * Do a lazy sync of the filesystem. 322 */ 323 int 324 sync_fsync(v) 325 void *v; 326 { 327 struct vop_fsync_args /* { 328 struct vnodeop_desc *a_desc; 329 struct vnode *a_vp; 330 struct ucred *a_cred; 331 int a_waitfor; 332 struct proc *a_p; 333 } */ *ap = v; 334 struct vnode *syncvp = ap->a_vp; 335 struct mount *mp = syncvp->v_mount; 336 int asyncflag; 337 338 /* 339 * We only need to do something if this is a lazy evaluation. 340 */ 341 if (ap->a_waitfor != MNT_LAZY) 342 return (0); 343 344 /* 345 * Move ourselves to the back of the sync list. 346 */ 347 vn_syncer_add_to_worklist(syncvp, syncdelay); 348 349 /* 350 * Walk the list of vnodes pushing all that are dirty and 351 * not already on the sync list. 352 */ 353 simple_lock(&mountlist_slock); 354 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, ap->a_p) == 0) { 355 asyncflag = mp->mnt_flag & MNT_ASYNC; 356 mp->mnt_flag &= ~MNT_ASYNC; 357 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, ap->a_p); 358 if (asyncflag) 359 mp->mnt_flag |= MNT_ASYNC; 360 vfs_unbusy(mp, ap->a_p); 361 } else 362 simple_unlock(&mountlist_slock); 363 364 return (0); 365 } 366 367 /* 368 * The syncer vnode is no longer needed and is being decommissioned. 369 */ 370 int 371 sync_inactive(v) 372 void *v; 373 { 374 struct vop_inactive_args /* { 375 struct vnodeop_desc *a_desc; 376 struct vnode *a_vp; 377 struct proc *a_p; 378 } */ *ap = v; 379 380 struct vnode *vp = ap->a_vp; 381 382 if (vp->v_usecount == 0) { 383 VOP_UNLOCK(vp, 0, ap->a_p); 384 return (0); 385 } 386 vp->v_mount->mnt_syncer = NULL; 387 LIST_REMOVE(vp, v_synclist); 388 vp->v_bioflag &= ~VBIOONSYNCLIST; 389 vp->v_writecount = 0; 390 vput(vp); 391 return (0); 392 } 393 394 /* 395 * Print out a syncer vnode. 396 */ 397 int 398 sync_print(v) 399 void *v; 400 401 { 402 struct vop_print_args /* { 403 struct vnodeop_desc *a_desc; 404 struct vnode *a_vp; 405 } */ *ap = v; 406 struct vnode *vp = ap->a_vp; 407 408 printf("syncer vnode"); 409 if (vp->v_vnlock != NULL) 410 lockmgr_printinfo(vp->v_vnlock); 411 printf("\n"); 412 return (0); 413 } 414