1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/types.h>
44*0Sstevel@tonic-gate #include <sys/t_lock.h>
45*0Sstevel@tonic-gate #include <sys/param.h>
46*0Sstevel@tonic-gate #include <sys/buf.h>
47*0Sstevel@tonic-gate #include <sys/cmn_err.h>
48*0Sstevel@tonic-gate #include <sys/debug.h>
49*0Sstevel@tonic-gate #include <sys/errno.h>
50*0Sstevel@tonic-gate #include <sys/vfs.h>
51*0Sstevel@tonic-gate #include <sys/swap.h>
52*0Sstevel@tonic-gate #include <sys/vnode.h>
53*0Sstevel@tonic-gate #include <sys/cred.h>
54*0Sstevel@tonic-gate #include <sys/fs/snode.h>
55*0Sstevel@tonic-gate #include <sys/thread.h>
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #include <fs/fs_subr.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * This is the loadable module wrapper.
61*0Sstevel@tonic-gate  */
62*0Sstevel@tonic-gate #include <sys/modctl.h>
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static vfsdef_t vfw = {
65*0Sstevel@tonic-gate 	VFSDEF_VERSION,
66*0Sstevel@tonic-gate 	"specfs",
67*0Sstevel@tonic-gate 	specinit,
68*0Sstevel@tonic-gate 	0,
69*0Sstevel@tonic-gate 	NULL
70*0Sstevel@tonic-gate };
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate extern struct mod_ops mod_fsops;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate  * Module linkage information for the kernel.
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate static struct modlfs modlfs = {
78*0Sstevel@tonic-gate 	&mod_fsops, "filesystem for specfs", &vfw
79*0Sstevel@tonic-gate };
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
82*0Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
83*0Sstevel@tonic-gate };
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate int
86*0Sstevel@tonic-gate _init(void)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate int
92*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  * N.B.
99*0Sstevel@tonic-gate  * No _fini routine. This module cannot be unloaded once loaded.
100*0Sstevel@tonic-gate  * The NO_UNLOAD_STUB in modstub.s must change if this module ever
101*0Sstevel@tonic-gate  * is modififed to become unloadable.
102*0Sstevel@tonic-gate  */
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate kmutex_t spec_syncbusy;		/* initialized in specinit() */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate  * Run though all the snodes and force write-back
108*0Sstevel@tonic-gate  * of all dirty pages on the block devices.
109*0Sstevel@tonic-gate  */
110*0Sstevel@tonic-gate /*ARGSUSED*/
111*0Sstevel@tonic-gate int
112*0Sstevel@tonic-gate spec_sync(struct vfs *vfsp,
113*0Sstevel@tonic-gate 	short	flag,
114*0Sstevel@tonic-gate 	struct cred *cr)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	struct snode *sync_list;
117*0Sstevel@tonic-gate 	register struct snode **spp, *sp, *spnext;
118*0Sstevel@tonic-gate 	register struct vnode *vp;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if (mutex_tryenter(&spec_syncbusy) == 0)
121*0Sstevel@tonic-gate 		return (0);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	if (flag & SYNC_ATTR) {
124*0Sstevel@tonic-gate 		mutex_exit(&spec_syncbusy);
125*0Sstevel@tonic-gate 		return (0);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	mutex_enter(&stable_lock);
128*0Sstevel@tonic-gate 	sync_list = NULL;
129*0Sstevel@tonic-gate 	/*
130*0Sstevel@tonic-gate 	 * Find all the snodes that are dirty and add them to the sync_list
131*0Sstevel@tonic-gate 	 */
132*0Sstevel@tonic-gate 	for (spp = stable; spp < &stable[STABLESIZE]; spp++) {
133*0Sstevel@tonic-gate 		for (sp = *spp; sp != NULL; sp = sp->s_next) {
134*0Sstevel@tonic-gate 			vp = STOV(sp);
135*0Sstevel@tonic-gate 			/*
136*0Sstevel@tonic-gate 			 * Don't bother sync'ing a vp if it's
137*0Sstevel@tonic-gate 			 * part of a virtual swap device.
138*0Sstevel@tonic-gate 			 */
139*0Sstevel@tonic-gate 			if (IS_SWAPVP(vp))
140*0Sstevel@tonic-gate 				continue;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 			if (vp->v_type == VBLK && vn_has_cached_data(vp)) {
143*0Sstevel@tonic-gate 				/*
144*0Sstevel@tonic-gate 				 * Prevent vp from going away before we
145*0Sstevel@tonic-gate 				 * we get a chance to do a VOP_PUTPAGE
146*0Sstevel@tonic-gate 				 * via sync_list processing
147*0Sstevel@tonic-gate 				 */
148*0Sstevel@tonic-gate 				VN_HOLD(vp);
149*0Sstevel@tonic-gate 				sp->s_list = sync_list;
150*0Sstevel@tonic-gate 				sync_list = sp;
151*0Sstevel@tonic-gate 			}
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 	mutex_exit(&stable_lock);
155*0Sstevel@tonic-gate 	/*
156*0Sstevel@tonic-gate 	 * Now write out all the snodes we marked asynchronously.
157*0Sstevel@tonic-gate 	 */
158*0Sstevel@tonic-gate 	for (sp = sync_list; sp != NULL; sp = spnext) {
159*0Sstevel@tonic-gate 		spnext = sp->s_list;
160*0Sstevel@tonic-gate 		vp = STOV(sp);
161*0Sstevel@tonic-gate 		(void) VOP_PUTPAGE(vp, (offset_t)0, (uint_t)0, B_ASYNC, cr);
162*0Sstevel@tonic-gate 		VN_RELE(vp);		/* Release our hold on vnode */
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 	mutex_exit(&spec_syncbusy);
165*0Sstevel@tonic-gate 	return (0);
166*0Sstevel@tonic-gate }
167