1 /* $OpenBSD: vfs_sync.c,v 1.67 2021/10/15 06:30:06 semarie 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 #include <sys/time.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 int 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 int syncer_delayno = 0; 72 long syncer_mask; 73 LIST_HEAD(synclist, vnode); 74 static struct synclist *syncer_workitem_pending; 75 76 struct proc *syncerproc; 77 int syncer_chan; 78 79 /* 80 * The workitem queue. 81 * 82 * It is useful to delay writes of file data and filesystem metadata 83 * for tens of seconds so that quickly created and deleted files need 84 * not waste disk bandwidth being created and removed. To realize this, 85 * we append vnodes to a "workitem" queue. When running with a soft 86 * updates implementation, most pending metadata dependencies should 87 * not wait for more than a few seconds. Thus, mounted block devices 88 * are delayed only about half the time that file data is delayed. 89 * Similarly, directory updates are more critical, so are only delayed 90 * about a third the time that file data is delayed. Thus, there are 91 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 92 * one each second (driven off the filesystem syncer process). The 93 * syncer_delayno variable indicates the next queue that is to be processed. 94 * Items that need to be processed soon are placed in this queue: 95 * 96 * syncer_workitem_pending[syncer_delayno] 97 * 98 * A delay of fifteen seconds is done by placing the request fifteen 99 * entries later in the queue: 100 * 101 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 102 * 103 */ 104 105 void 106 vn_initialize_syncerd(void) 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 117 vn_syncer_add_to_worklist(struct vnode *vp, 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 void 138 syncer_thread(void *arg) 139 { 140 uint64_t elapsed, start; 141 struct proc *p = curproc; 142 struct synclist *slp; 143 struct vnode *vp; 144 int s; 145 146 for (;;) { 147 start = getnsecuptime(); 148 149 /* 150 * Push files whose dirty time has expired. 151 */ 152 s = splbio(); 153 slp = &syncer_workitem_pending[syncer_delayno]; 154 155 syncer_delayno += 1; 156 if (syncer_delayno == syncer_maxdelay) 157 syncer_delayno = 0; 158 159 while ((vp = LIST_FIRST(slp)) != NULL) { 160 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) { 161 /* 162 * If we fail to get the lock, we move this 163 * vnode one second ahead in time. 164 * XXX - no good, but the best we can do. 165 */ 166 vn_syncer_add_to_worklist(vp, 1); 167 continue; 168 } 169 splx(s); 170 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p); 171 vput(vp); 172 s = splbio(); 173 if (LIST_FIRST(slp) == vp) { 174 /* 175 * Note: disk vps can remain on the 176 * worklist too with no dirty blocks, but 177 * since sync_fsync() moves it to a different 178 * slot we are safe. 179 */ 180 #ifdef DIAGNOSTIC 181 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL && 182 vp->v_type != VBLK) { 183 vprint("fsync failed", vp); 184 if (vp->v_mount != NULL) 185 printf("mounted on: %s\n", 186 vp->v_mount->mnt_stat.f_mntonname); 187 panic("%s: fsync failed", __func__); 188 } 189 #endif /* DIAGNOSTIC */ 190 /* 191 * Put us back on the worklist. The worklist 192 * routine will remove us from our current 193 * position and then add us back in at a later 194 * position. 195 */ 196 vn_syncer_add_to_worklist(vp, syncdelay); 197 } 198 199 sched_pause(yield); 200 } 201 202 splx(s); 203 204 #ifdef FFS_SOFTUPDATES 205 /* 206 * Do soft update processing. 207 */ 208 softdep_process_worklist(NULL); 209 #endif 210 211 /* 212 * The variable rushjob allows the kernel to speed up the 213 * processing of the filesystem syncer process. A rushjob 214 * value of N tells the filesystem syncer to process the next 215 * N seconds worth of work on its queue ASAP. Currently rushjob 216 * is used by the soft update code to speed up the filesystem 217 * syncer process when the incore state is getting so far 218 * ahead of the disk that the kernel memory pool is being 219 * threatened with exhaustion. 220 */ 221 if (rushjob > 0) { 222 rushjob -= 1; 223 continue; 224 } 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 elapsed = getnsecuptime() - start; 235 if (elapsed < SEC_TO_NSEC(1)) { 236 tsleep_nsec(&syncer_chan, PPAUSE, "syncer", 237 SEC_TO_NSEC(1) - elapsed); 238 } 239 } 240 } 241 242 /* 243 * Request the syncer daemon to speed up its work. 244 * We never push it to speed up more than half of its 245 * normal turn time, otherwise it could take over the cpu. 246 */ 247 int 248 speedup_syncer(void) 249 { 250 if (syncerproc) 251 wakeup_proc(syncerproc, &syncer_chan); 252 if (rushjob < syncdelay / 2) { 253 rushjob += 1; 254 stat_rush_requests += 1; 255 return 1; 256 } 257 return 0; 258 } 259 260 /* Routine to create and manage a filesystem syncer vnode. */ 261 int sync_fsync(void *); 262 int sync_inactive(void *); 263 int sync_print(void *); 264 265 const struct vops sync_vops = { 266 .vop_close = nullop, 267 .vop_fsync = sync_fsync, 268 .vop_inactive = sync_inactive, 269 .vop_reclaim = nullop, 270 .vop_lock = nullop, 271 .vop_unlock = nullop, 272 .vop_islocked = nullop, 273 .vop_print = sync_print 274 }; 275 276 /* 277 * Create a new filesystem syncer vnode for the specified mount point. 278 */ 279 int 280 vfs_allocate_syncvnode(struct mount *mp) 281 { 282 struct vnode *vp; 283 static long start, incr, next; 284 int error; 285 286 /* Allocate a new vnode */ 287 if ((error = getnewvnode(VT_VFS, mp, &sync_vops, &vp)) != 0) { 288 mp->mnt_syncer = NULL; 289 return (error); 290 } 291 vp->v_writecount = 1; 292 vp->v_type = VNON; 293 /* 294 * Place the vnode onto the syncer worklist. We attempt to 295 * scatter them about on the list so that they will go off 296 * at evenly distributed times even if all the filesystems 297 * are mounted at once. 298 */ 299 next += incr; 300 if (next == 0 || next > syncer_maxdelay) { 301 start /= 2; 302 incr /= 2; 303 if (start == 0) { 304 start = syncer_maxdelay / 2; 305 incr = syncer_maxdelay; 306 } 307 next = start; 308 } 309 vn_syncer_add_to_worklist(vp, next); 310 mp->mnt_syncer = vp; 311 return (0); 312 } 313 314 /* 315 * Do a lazy sync of the filesystem. 316 */ 317 int 318 sync_fsync(void *v) 319 { 320 struct vop_fsync_args *ap = v; 321 struct vnode *syncvp = ap->a_vp; 322 struct mount *mp = syncvp->v_mount; 323 int asyncflag; 324 325 /* 326 * We only need to do something if this is a lazy evaluation. 327 */ 328 if (ap->a_waitfor != MNT_LAZY) 329 return (0); 330 331 /* 332 * Move ourselves to the back of the sync list. 333 */ 334 vn_syncer_add_to_worklist(syncvp, syncdelay); 335 336 /* 337 * Walk the list of vnodes pushing all that are dirty and 338 * not already on the sync list. 339 */ 340 if (vfs_busy(mp, VB_READ|VB_NOWAIT) == 0) { 341 asyncflag = mp->mnt_flag & MNT_ASYNC; 342 mp->mnt_flag &= ~MNT_ASYNC; 343 VFS_SYNC(mp, MNT_LAZY, 0, ap->a_cred, ap->a_p); 344 if (asyncflag) 345 mp->mnt_flag |= MNT_ASYNC; 346 vfs_unbusy(mp); 347 } 348 349 return (0); 350 } 351 352 /* 353 * The syncer vnode is no longer needed and is being decommissioned. 354 */ 355 int 356 sync_inactive(void *v) 357 { 358 struct vop_inactive_args *ap = v; 359 360 struct vnode *vp = ap->a_vp; 361 int s; 362 363 if (vp->v_usecount == 0) { 364 VOP_UNLOCK(vp); 365 return (0); 366 } 367 368 vp->v_mount->mnt_syncer = NULL; 369 370 s = splbio(); 371 372 LIST_REMOVE(vp, v_synclist); 373 vp->v_bioflag &= ~VBIOONSYNCLIST; 374 375 splx(s); 376 377 vp->v_writecount = 0; 378 vput(vp); 379 380 return (0); 381 } 382 383 /* 384 * Print out a syncer vnode. 385 */ 386 int 387 sync_print(void *v) 388 { 389 printf("syncer vnode\n"); 390 391 return (0); 392 } 393