xref: /netbsd-src/sys/miscfs/procfs/procfs_vfsops.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /*
2  * Copyright (c) 1993 The Regents of the University of California.
3  * Copyright (c) 1993 Jan-Simon Pendry
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * From:
38  *	Id: procfs_vfsops.c,v 4.1 1993/12/17 10:47:45 jsp Rel
39  *
40  *	$Id: procfs_vfsops.c,v 1.15 1994/04/23 07:55:03 cgd Exp $
41  */
42 
43 /*
44  * procfs VFS interface
45  */
46 
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/buf.h>
53 #include <sys/syslog.h>
54 #include <sys/mount.h>
55 #include <sys/signalvar.h>
56 #include <sys/vnode.h>
57 #include <miscfs/procfs/procfs.h>
58 #include <vm/vm.h>
59 
60 /*
61  * VFS Operations.
62  *
63  * mount system call
64  */
65 /* ARGSUSED */
66 procfs_mount(mp, path, data, ndp, p)
67 	struct mount *mp;
68 	char *path;
69 	caddr_t data;
70 	struct nameidata *ndp;
71 	struct proc *p;
72 {
73 	u_int size;
74 	int error;
75 
76 	if (UIO_MX & (UIO_MX-1)) {
77 		log(LOG_ERR, "procfs: invalid directory entry size");
78 		return (EINVAL);
79 	}
80 
81 	if (mp->mnt_flag & MNT_UPDATE)
82 		return (EOPNOTSUPP);
83 
84 	/* mp->mnt_flag |= MNT_LOCAL; */
85 	mp->mnt_data = 0;
86 	getnewfsid(mp, makefstype(MOUNT_PROCFS));
87 
88 	(void) copyinstr(path, (caddr_t)mp->mnt_stat.f_mntonname, MNAMELEN, &size);
89 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
90 
91 	size = sizeof("proc") - 1;
92 	bcopy("proc", mp->mnt_stat.f_mntfromname, size);
93 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
94 
95 	return (0);
96 }
97 
98 /*
99  * unmount system call
100  */
101 procfs_unmount(mp, mntflags, p)
102 	struct mount *mp;
103 	int mntflags;
104 	struct proc *p;
105 {
106 	int error;
107 	extern int doforce;
108 	int flags = 0;
109 
110 	if (mntflags & MNT_FORCE) {
111 		/* procfs can never be rootfs so don't check for it */
112 		if (!doforce)
113 			return (EINVAL);
114 		flags |= FORCECLOSE;
115 	}
116 
117 	/*
118 	 * Clear out buffer cache.  I don't think we
119 	 * ever get anything cached at this level at the
120 	 * moment, but who knows...
121 	 */
122 	mntflushbuf(mp, 0);
123 
124 	if (mntinvalbuf(mp, 1))
125 		return (EBUSY);
126 
127 	if (error = vflush(mp, 0, flags))
128 		return (error);
129 
130 	return (0);
131 }
132 
133 procfs_root(mp, vpp)
134 	struct mount *mp;
135 	struct vnode **vpp;
136 {
137 	struct vnode *vp;
138 	int error;
139 
140 	error = procfs_allocvp(mp, &vp, (pid_t) 0, Proot);
141 	if (error)
142 		return (error);
143 
144 	*vpp = vp;
145 	return (0);
146 }
147 
148 /*
149  */
150 /* ARGSUSED */
151 procfs_start(mp, flags, p)
152 	struct mount *mp;
153 	int flags;
154 	struct proc *p;
155 {
156 
157 	return (0);
158 }
159 
160 /*
161  * Get file system statistics.
162  */
163 procfs_statfs(mp, sbp, p)
164 	struct mount *mp;
165 	struct statfs *sbp;
166 	struct proc *p;
167 {
168 #ifdef COMPAT_09
169 	sbp->f_type = 10;
170 #else
171 	sbp->f_type = 0;
172 #endif
173 	sbp->f_bsize = PAGE_SIZE >> 2;
174 	sbp->f_iosize = PAGE_SIZE;
175 	sbp->f_blocks = 1;	/* avoid divide by zero in some df's */
176 	sbp->f_bfree = 0;
177 	sbp->f_bavail = 0;
178 	sbp->f_files = maxproc;			/* approx */
179 	sbp->f_ffree = maxproc - nprocs;	/* approx */
180 
181 	if (sbp != &mp->mnt_stat) {
182 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
183 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
184 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
185 	}
186 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
187 	sbp->f_fstypename[MFSNAMELEN] = '\0';
188 
189 	return (0);
190 }
191 
192 
193 procfs_quotactl(mp, cmds, uid, arg, p)
194 	struct mount *mp;
195 	int cmds;
196 	uid_t uid;
197 	caddr_t arg;
198 	struct proc *p;
199 {
200 
201 	return (EOPNOTSUPP);
202 }
203 
204 procfs_sync(mp, waitfor)
205 	struct mount *mp;
206 	int waitfor;
207 {
208 
209 	return (0);
210 }
211 
212 procfs_fhtovp(mp, fhp, vpp)
213 	struct mount *mp;
214 	struct fid *fhp;
215 	struct vnode **vpp;
216 {
217 	/*
218 	 * NFS mounting of procfs doesn't work correctly.
219 	 * The files in procfs are more similar to devices
220 	 * than to regular files.
221 	 */
222 	return EOPNOTSUPP;
223 }
224 
225 procfs_vptofh(vp, fhp)
226 	struct vnode *vp;
227 	struct fid *fhp;
228 {
229 	/*
230 	 * NFS mounting of procfs doesn't work correctly.
231 	 * The files in procfs are more similar to devices
232 	 * than to regular files.
233 	 */
234 	return EOPNOTSUPP;
235 }
236 
237 procfs_init()
238 {
239 
240 	return (0);
241 }
242 
243 struct vfsops procfs_vfsops = {
244 	MOUNT_PROCFS,
245 	procfs_mount,
246 	procfs_start,
247 	procfs_unmount,
248 	procfs_root,
249 	procfs_quotactl,
250 	procfs_statfs,
251 	procfs_sync,
252 	procfs_fhtovp,
253 	procfs_vptofh,
254 	procfs_init,
255 };
256