1 /* $OpenBSD: vfs_sync.c,v 1.59 2018/05/27 06:02:14 visa 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/lock.h> 50 #include <sys/malloc.h> 51 52 #include <sys/kernel.h> 53 #include <sys/sched.h> 54 55 #ifdef FFS_SOFTUPDATES 56 int softdep_process_worklist(struct mount *); 57 #endif 58 59 /* 60 * The workitem queue. 61 */ 62 #define SYNCER_MAXDELAY 32 /* maximum sync delay time */ 63 #define SYNCER_DEFAULT 30 /* default sync delay time */ 64 int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 65 int syncdelay = SYNCER_DEFAULT; /* time to delay syncing vnodes */ 66 67 int rushjob = 0; /* number of slots to run ASAP */ 68 int stat_rush_requests = 0; /* number of rush requests */ 69 70 int syncer_delayno = 0; 71 long syncer_mask; 72 LIST_HEAD(synclist, vnode); 73 static struct synclist *syncer_workitem_pending; 74 75 struct proc *syncerproc; 76 77 /* 78 * The workitem queue. 79 * 80 * It is useful to delay writes of file data and filesystem metadata 81 * for tens of seconds so that quickly created and deleted files need 82 * not waste disk bandwidth being created and removed. To realize this, 83 * we append vnodes to a "workitem" queue. When running with a soft 84 * updates implementation, most pending metadata dependencies should 85 * not wait for more than a few seconds. Thus, mounted block devices 86 * are delayed only about half the time that file data is delayed. 87 * Similarly, directory updates are more critical, so are only delayed 88 * about a third the time that file data is delayed. Thus, there are 89 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 90 * one each second (driven off the filesystem syncer process). The 91 * syncer_delayno variable indicates the next queue that is to be processed. 92 * Items that need to be processed soon are placed in this queue: 93 * 94 * syncer_workitem_pending[syncer_delayno] 95 * 96 * A delay of fifteen seconds is done by placing the request fifteen 97 * entries later in the queue: 98 * 99 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 100 * 101 */ 102 103 void 104 vn_initialize_syncerd(void) 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(struct vnode *vp, int delay) 116 { 117 int s, slot; 118 119 if (delay > syncer_maxdelay - 2) 120 delay = syncer_maxdelay - 2; 121 slot = (syncer_delayno + delay) & syncer_mask; 122 123 s = splbio(); 124 if (vp->v_bioflag & VBIOONSYNCLIST) 125 LIST_REMOVE(vp, v_synclist); 126 127 vp->v_bioflag |= VBIOONSYNCLIST; 128 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist); 129 splx(s); 130 } 131 132 /* 133 * System filesystem synchronizer daemon. 134 */ 135 void 136 sched_sync(struct proc *p) 137 { 138 struct synclist *slp; 139 struct vnode *vp; 140 time_t starttime; 141 int s; 142 143 syncerproc = curproc; 144 145 for (;;) { 146 starttime = time_second; 147 148 /* 149 * Push files whose dirty time has expired. 150 */ 151 s = splbio(); 152 slp = &syncer_workitem_pending[syncer_delayno]; 153 154 syncer_delayno += 1; 155 if (syncer_delayno == syncer_maxdelay) 156 syncer_delayno = 0; 157 158 while ((vp = LIST_FIRST(slp)) != NULL) { 159 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) { 160 /* 161 * If we fail to get the lock, we move this 162 * vnode one second ahead in time. 163 * XXX - no good, but the best we can do. 164 */ 165 vn_syncer_add_to_worklist(vp, 1); 166 continue; 167 } 168 splx(s); 169 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p); 170 vput(vp); 171 s = splbio(); 172 if (LIST_FIRST(slp) == vp) { 173 /* 174 * Note: disk vps can remain on the 175 * worklist too with no dirty blocks, but 176 * since sync_fsync() moves it to a different 177 * slot we are safe. 178 */ 179 #ifdef DIAGNOSTIC 180 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL && 181 vp->v_type != VBLK) { 182 vprint("fsync failed", vp); 183 if (vp->v_mount != NULL) 184 printf("mounted on: %s\n", 185 vp->v_mount->mnt_stat.f_mntonname); 186 panic("sched_sync: fsync failed"); 187 } 188 #endif /* DIAGNOSTIC */ 189 /* 190 * Put us back on the worklist. The worklist 191 * routine will remove us from our current 192 * position and then add us back in at a later 193 * position. 194 */ 195 vn_syncer_add_to_worklist(vp, syncdelay); 196 } 197 198 sched_pause(yield); 199 } 200 201 splx(s); 202 203 #ifdef FFS_SOFTUPDATES 204 /* 205 * Do soft update processing. 206 */ 207 softdep_process_worklist(NULL); 208 #endif 209 210 /* 211 * The variable rushjob allows the kernel to speed up the 212 * processing of the filesystem syncer process. A rushjob 213 * value of N tells the filesystem syncer to process the next 214 * N seconds worth of work on its queue ASAP. Currently rushjob 215 * is used by the soft update code to speed up the filesystem 216 * syncer process when the incore state is getting so far 217 * ahead of the disk that the kernel memory pool is being 218 * threatened with exhaustion. 219 */ 220 if (rushjob > 0) { 221 rushjob -= 1; 222 continue; 223 } 224 /* 225 * If it has taken us less than a second to process the 226 * current work, then wait. Otherwise start right over 227 * again. We can still lose time if any single round 228 * takes more than two seconds, but it does not really 229 * matter as we are just trying to generally pace the 230 * filesystem activity. 231 */ 232 if (time_second == starttime) 233 tsleep(&lbolt, PPAUSE, "syncer", 0); 234 } 235 } 236 237 /* 238 * Request the syncer daemon to speed up its work. 239 * We never push it to speed up more than half of its 240 * normal turn time, otherwise it could take over the cpu. 241 */ 242 int 243 speedup_syncer(void) 244 { 245 int s; 246 247 SCHED_LOCK(s); 248 if (syncerproc && syncerproc->p_wchan == &lbolt) 249 setrunnable(syncerproc); 250 SCHED_UNLOCK(s); 251 if (rushjob < syncdelay / 2) { 252 rushjob += 1; 253 stat_rush_requests += 1; 254 return 1; 255 } 256 return 0; 257 } 258 259 /* Routine to create and manage a filesystem syncer vnode. */ 260 int sync_fsync(void *); 261 int sync_inactive(void *); 262 int sync_print(void *); 263 264 struct vops sync_vops = { 265 .vop_close = nullop, 266 .vop_fsync = sync_fsync, 267 .vop_inactive = sync_inactive, 268 .vop_reclaim = nullop, 269 .vop_lock = vop_generic_lock, 270 .vop_unlock = vop_generic_unlock, 271 .vop_islocked = vop_generic_islocked, 272 .vop_print = sync_print 273 }; 274 275 /* 276 * Create a new filesystem syncer vnode for the specified mount point. 277 */ 278 int 279 vfs_allocate_syncvnode(struct mount *mp) 280 { 281 struct vnode *vp; 282 static long start, incr, next; 283 int error; 284 285 /* Allocate a new vnode */ 286 if ((error = getnewvnode(VT_VFS, mp, &sync_vops, &vp)) != 0) { 287 mp->mnt_syncer = NULL; 288 return (error); 289 } 290 vp->v_writecount = 1; 291 vp->v_type = VNON; 292 /* 293 * Place the vnode onto the syncer worklist. We attempt to 294 * scatter them about on the list so that they will go off 295 * at evenly distributed times even if all the filesystems 296 * are mounted at once. 297 */ 298 next += incr; 299 if (next == 0 || next > syncer_maxdelay) { 300 start /= 2; 301 incr /= 2; 302 if (start == 0) { 303 start = syncer_maxdelay / 2; 304 incr = syncer_maxdelay; 305 } 306 next = start; 307 } 308 vn_syncer_add_to_worklist(vp, next); 309 mp->mnt_syncer = vp; 310 return (0); 311 } 312 313 /* 314 * Do a lazy sync of the filesystem. 315 */ 316 int 317 sync_fsync(void *v) 318 { 319 struct vop_fsync_args *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 if (vfs_busy(mp, VB_READ|VB_NOWAIT) == 0) { 340 asyncflag = mp->mnt_flag & MNT_ASYNC; 341 mp->mnt_flag &= ~MNT_ASYNC; 342 VFS_SYNC(mp, MNT_LAZY, 0, ap->a_cred, ap->a_p); 343 if (asyncflag) 344 mp->mnt_flag |= MNT_ASYNC; 345 vfs_unbusy(mp); 346 } 347 348 return (0); 349 } 350 351 /* 352 * The syncer vnode is no longer needed and is being decommissioned. 353 */ 354 int 355 sync_inactive(void *v) 356 { 357 struct vop_inactive_args *ap = v; 358 359 struct vnode *vp = ap->a_vp; 360 int s; 361 362 if (vp->v_usecount == 0) { 363 VOP_UNLOCK(vp); 364 return (0); 365 } 366 367 vp->v_mount->mnt_syncer = NULL; 368 369 s = splbio(); 370 371 LIST_REMOVE(vp, v_synclist); 372 vp->v_bioflag &= ~VBIOONSYNCLIST; 373 374 splx(s); 375 376 vp->v_writecount = 0; 377 vput(vp); 378 379 return (0); 380 } 381 382 /* 383 * Print out a syncer vnode. 384 */ 385 int 386 sync_print(void *v) 387 { 388 printf("syncer vnode\n"); 389 390 return (0); 391 } 392