kernel: remove shouting from BREAD/BWRITE/BWRITE2/GETBLK
[unleashed.git] / kernel / fs / fsflush.c
blobd6e9754c49096e047ab0982b9e38ab2c1e951603
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #include <sys/types.h>
31 #include <sys/t_lock.h>
32 #include <sys/param.h>
33 #include <sys/tuneable.h>
34 #include <sys/inline.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/user.h>
38 #include <sys/var.h>
39 #include <sys/buf.h>
40 #include <sys/vfs.h>
41 #include <sys/cred.h>
42 #include <sys/kmem.h>
43 #include <sys/vnode.h>
44 #include <sys/swap.h>
45 #include <sys/vm.h>
46 #include <sys/debug.h>
47 #include <sys/cmn_err.h>
48 #include <sys/sysinfo.h>
49 #include <sys/callb.h>
50 #include <sys/reboot.h>
51 #include <sys/time.h>
52 #include <sys/fs/ufs_inode.h>
53 #include <sys/fs/ufs_bio.h>
55 #include <vm/hat.h>
56 #include <vm/page.h>
57 #include <vm/pvn.h>
58 #include <vm/seg_kmem.h>
60 int doiflush = 1; /* non-zero to turn inode flushing on */
61 int dopageflush = 1; /* non-zero to turn page flushing on */
64 * To improve boot performance, don't run the inode flushing loop until
65 * the specified number of seconds after boot. To revert to the old
66 * behavior, set fsflush_iflush_delay to 0. We have not created any new
67 * filesystem danger that did not exist previously, since there is always a
68 * window in between when fsflush does the inode flush loop during which the
69 * system could crash, fail to sync the filesystem, and fsck will be needed
70 * to recover. We have, however, widened this window. Finally,
71 * we never delay inode flushing if we're booting into single user mode,
72 * where the administrator may be modifying files or using fsck. This
73 * modification avoids inode flushes during boot whose only purpose is to
74 * update atimes on files which have been accessed during boot.
76 int fsflush_iflush_delay = 60;
78 kcondvar_t fsflush_cv;
79 static kmutex_t fsflush_lock; /* just for the cv_wait */
80 ksema_t fsflush_sema; /* to serialize with reboot */
83 * some statistics for fsflush_do_pages
85 typedef struct {
86 ulong_t fsf_scan; /* number of pages scanned */
87 ulong_t fsf_examined; /* number of page_t's actually examined, can */
88 /* be less than fsf_scan due to large pages */
89 ulong_t fsf_locked; /* pages we actually page_lock()ed */
90 ulong_t fsf_modified; /* number of modified pages found */
91 ulong_t fsf_coalesce; /* number of page coalesces done */
92 ulong_t fsf_time; /* nanoseconds of run time */
93 ulong_t fsf_releases; /* number of page_release() done */
94 } fsf_stat_t;
96 fsf_stat_t fsf_recent; /* counts for most recent duty cycle */
97 fsf_stat_t fsf_total; /* total of counts */
98 ulong_t fsf_cycles; /* number of runs refelected in fsf_total */
101 * data used to determine when we can coalesce consecutive free pages
102 * into larger pages.
104 #define MAX_PAGESIZES 32
105 static ulong_t fsf_npgsz;
106 static pgcnt_t fsf_pgcnt[MAX_PAGESIZES];
107 static pgcnt_t fsf_mask[MAX_PAGESIZES];
111 * Scan page_t's and issue I/O's for modified pages.
113 * Also coalesces consecutive small sized free pages into the next larger
114 * pagesize. This costs a tiny bit of time in fsflush, but will reduce time
115 * spent scanning on later passes and for anybody allocating large pages.
117 static void
118 fsflush_do_pages()
120 vnode_t *vp;
121 ulong_t pcount;
122 hrtime_t timer = gethrtime();
123 ulong_t releases = 0;
124 ulong_t nexamined = 0;
125 ulong_t nlocked = 0;
126 ulong_t nmodified = 0;
127 ulong_t ncoalesce = 0;
128 ulong_t cnt;
129 int mod;
130 int fspage = 1;
131 uoff_t offset;
132 uint_t szc;
134 page_t *coal_page = NULL; /* 1st page in group to coalesce */
135 uint_t coal_szc = 0; /* size code, coal_page->p_szc */
136 uint_t coal_cnt = 0; /* count of pages seen */
138 static ulong_t nscan = 0;
139 static pgcnt_t last_total_pages = 0;
140 static page_t *pp = NULL;
143 * Check to see if total_pages has changed.
145 if (total_pages != last_total_pages) {
146 last_total_pages = total_pages;
147 nscan = (last_total_pages * (tune.t_fsflushr))/v.v_autoup;
150 if (pp == NULL)
151 pp = memsegs->pages;
153 pcount = 0;
154 while (pcount < nscan) {
157 * move to the next page, skipping over large pages
158 * and issuing prefetches.
160 if (pp->p_szc && fspage == 0) {
161 pfn_t pfn;
163 pfn = page_pptonum(pp);
164 cnt = page_get_pagecnt(pp->p_szc);
165 cnt -= pfn & (cnt - 1);
166 } else
167 cnt = 1;
169 pp = page_nextn(pp, cnt);
170 prefetch_page_r((void *)pp);
171 ASSERT(pp != NULL);
172 pcount += cnt;
175 * Do a bunch of dirty tests (ie. no locking) to determine
176 * if we can quickly skip this page. These tests are repeated
177 * after acquiring the page lock.
179 ++nexamined;
180 if (PP_ISSWAP(pp)) {
181 fspage = 0;
182 coal_page = NULL;
183 continue;
187 * skip free pages too, but try coalescing them into larger
188 * pagesizes
190 if (PP_ISFREE(pp)) {
192 * skip pages with a file system identity or that
193 * are already maximum size
195 fspage = 0;
196 szc = pp->p_szc;
197 if (pp->p_vnode != NULL || szc == fsf_npgsz - 1) {
198 coal_page = NULL;
199 continue;
203 * If not in a coalescing candidate page or the size
204 * codes are different, start a new candidate.
206 if (coal_page == NULL || coal_szc != szc) {
209 * page must be properly aligned
211 if ((page_pptonum(pp) & fsf_mask[szc]) != 0) {
212 coal_page = NULL;
213 continue;
215 coal_page = pp;
216 coal_szc = szc;
217 coal_cnt = 1;
218 continue;
222 * acceptable to add this to existing candidate page
224 ++coal_cnt;
225 if (coal_cnt < fsf_pgcnt[coal_szc])
226 continue;
229 * We've got enough pages to coalesce, so do it.
230 * After promoting, we clear coal_page, so it will
231 * take another pass to promote this to an even
232 * larger page.
234 ++ncoalesce;
235 (void) page_promote_size(coal_page, coal_szc);
236 coal_page = NULL;
237 continue;
238 } else {
239 coal_page = NULL;
242 if (PP_ISKAS(pp) ||
243 PAGE_LOCKED(pp) ||
244 pp->p_lckcnt != 0 ||
245 pp->p_cowcnt != 0) {
246 fspage = 0;
247 continue;
252 * Reject pages that can't be "exclusively" locked.
254 if (!page_trylock(pp, SE_EXCL))
255 continue;
256 ++nlocked;
260 * After locking the page, redo the above checks.
261 * Since we locked the page, leave out the PAGE_LOCKED() test.
263 vp = pp->p_vnode;
264 if (PP_ISSWAP(pp) ||
265 PP_ISFREE(pp) ||
266 vp == NULL ||
267 PP_ISKAS(pp) ||
268 (vp->v_flag & VISSWAP) != 0) {
269 page_unlock(pp);
270 fspage = 0;
271 continue;
273 if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
274 page_unlock(pp);
275 continue;
278 fspage = 1;
279 ASSERT(vp->v_type != VCHR);
282 * Check the modified bit. Leaving the bit alone in hardware.
283 * It will be cleared if we do the putpage.
285 if (IS_VMODSORT(vp))
286 mod = hat_ismod(pp);
287 else
288 mod = hat_pagesync(pp,
289 HAT_SYNC_DONTZERO | HAT_SYNC_STOPON_MOD) & P_MOD;
291 if (mod) {
292 ++nmodified;
293 offset = pp->p_offset;
296 * Hold the vnode before releasing the page lock
297 * to prevent it from being freed and re-used by
298 * some other thread.
300 VN_HOLD(vp);
302 page_unlock(pp);
304 (void) fop_putpage(vp, offset, PAGESIZE, B_ASYNC,
305 kcred, NULL);
307 VN_RELE(vp);
308 } else {
311 * Catch any pages which should be on the cache list,
312 * but aren't yet.
314 if (hat_page_is_mapped(pp) == 0) {
315 ++releases;
316 (void) page_release(pp, 1);
317 } else {
318 page_unlock(pp);
324 * maintain statistics
325 * reset every million wakeups, just to avoid overflow
327 if (++fsf_cycles == 1000000) {
328 fsf_cycles = 0;
329 fsf_total.fsf_scan = 0;
330 fsf_total.fsf_examined = 0;
331 fsf_total.fsf_locked = 0;
332 fsf_total.fsf_modified = 0;
333 fsf_total.fsf_coalesce = 0;
334 fsf_total.fsf_time = 0;
335 fsf_total.fsf_releases = 0;
336 } else {
337 fsf_total.fsf_scan += fsf_recent.fsf_scan = nscan;
338 fsf_total.fsf_examined += fsf_recent.fsf_examined = nexamined;
339 fsf_total.fsf_locked += fsf_recent.fsf_locked = nlocked;
340 fsf_total.fsf_modified += fsf_recent.fsf_modified = nmodified;
341 fsf_total.fsf_coalesce += fsf_recent.fsf_coalesce = ncoalesce;
342 fsf_total.fsf_time += fsf_recent.fsf_time = gethrtime() - timer;
343 fsf_total.fsf_releases += fsf_recent.fsf_releases = releases;
348 * As part of file system hardening, this daemon is awakened
349 * every second to flush cached data which includes the
350 * buffer cache, the inode cache and mapped pages.
352 void
353 fsflush()
355 struct buf *bp, *dwp;
356 struct hbuf *hp;
357 int autoup;
358 unsigned int ix, icount, count = 0;
359 callb_cpr_t cprinfo;
360 uint_t bcount;
361 kmutex_t *hmp;
362 struct vfssw *vswp;
364 proc_fsflush = ttoproc(curthread);
365 proc_fsflush->p_cstime = 0;
366 proc_fsflush->p_stime = 0;
367 proc_fsflush->p_cutime = 0;
368 proc_fsflush->p_utime = 0;
369 bcopy("fsflush", curproc->p_user.u_psargs, 8);
370 bcopy("fsflush", curproc->p_user.u_comm, 7);
372 mutex_init(&fsflush_lock, NULL, MUTEX_DEFAULT, NULL);
373 sema_init(&fsflush_sema, 0, NULL, SEMA_DEFAULT, NULL);
376 * Setup page coalescing.
378 fsf_npgsz = page_num_pagesizes();
379 ASSERT(fsf_npgsz < MAX_PAGESIZES);
380 for (ix = 0; ix < fsf_npgsz - 1; ++ix) {
381 fsf_pgcnt[ix] =
382 page_get_pagesize(ix + 1) / page_get_pagesize(ix);
383 fsf_mask[ix] = page_get_pagecnt(ix + 1) - 1;
386 autoup = v.v_autoup * hz;
387 icount = v.v_autoup / tune.t_fsflushr;
388 CALLB_CPR_INIT(&cprinfo, &fsflush_lock, callb_generic_cpr, "fsflush");
389 loop:
390 sema_v(&fsflush_sema);
391 mutex_enter(&fsflush_lock);
392 CALLB_CPR_SAFE_BEGIN(&cprinfo);
393 cv_wait(&fsflush_cv, &fsflush_lock); /* wait for clock */
394 CALLB_CPR_SAFE_END(&cprinfo, &fsflush_lock);
395 mutex_exit(&fsflush_lock);
396 sema_p(&fsflush_sema);
399 * Write back all old B_DELWRI buffers on the freelist.
401 bcount = 0;
402 for (ix = 0; ix < v.v_hbuf; ix++) {
404 hp = &hbuf[ix];
405 dwp = (struct buf *)&dwbuf[ix];
407 bcount += (hp->b_length);
409 if (dwp->av_forw == dwp) {
410 continue;
413 hmp = &hbuf[ix].b_lock;
414 mutex_enter(hmp);
415 bp = dwp->av_forw;
418 * Go down only on the delayed write lists.
420 while (bp != dwp) {
422 ASSERT(bp->b_flags & B_DELWRI);
424 if ((bp->b_flags & B_DELWRI) &&
425 (ddi_get_lbolt() - bp->b_start >= autoup) &&
426 sema_tryp(&bp->b_sem)) {
427 bp->b_flags |= B_ASYNC;
428 hp->b_length--;
429 notavail(bp);
430 mutex_exit(hmp);
431 if (bp->b_vp == NULL) {
432 bwrite(bp);
433 } else {
434 UFS_BWRITE(VTOI(bp->b_vp)->i_ufsvfs,
435 bp);
437 mutex_enter(hmp);
438 bp = dwp->av_forw;
439 } else {
440 bp = bp->av_forw;
443 mutex_exit(hmp);
448 * There is no need to wakeup any thread waiting on bio_mem_cv
449 * since brelse will wake them up as soon as IO is complete.
451 bfreelist.b_bcount = bcount;
453 if (dopageflush)
454 fsflush_do_pages();
456 if (!doiflush)
457 goto loop;
460 * If the system was not booted to single user mode, skip the
461 * inode flushing until after fsflush_iflush_delay secs have elapsed.
463 if ((boothowto & RB_SINGLE) == 0 &&
464 (ddi_get_lbolt64() / hz) < fsflush_iflush_delay)
465 goto loop;
468 * Flush cached attribute information (e.g. inodes).
470 if (++count >= icount) {
471 count = 0;
474 * Sync back cached data.
476 RLOCK_VFSSW();
477 for (vswp = &vfssw[1]; vswp < &vfssw[nfstype]; vswp++) {
478 if (ALLOCATED_VFSSW(vswp) && VFS_INSTALLED(vswp)) {
479 vfs_refvfssw(vswp);
480 RUNLOCK_VFSSW();
481 (void) fsop_sync_by_kind(vswp - vfssw,
482 SYNC_ATTR, kcred);
483 vfs_unrefvfssw(vswp);
484 RLOCK_VFSSW();
487 RUNLOCK_VFSSW();
489 goto loop;