xref: /netbsd-src/sys/miscfs/fdesc/fdesc_vfsops.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: fdesc_vfsops.c,v 1.70 2007/10/10 20:42:28 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)fdesc_vfsops.c	8.10 (Berkeley) 5/14/95
35  *
36  * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
37  */
38 
39 /*
40  * /dev/fd Filesystem
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.70 2007/10/10 20:42:28 ad Exp $");
45 
46 #if defined(_KERNEL_OPT)
47 #include "opt_compat_netbsd.h"
48 #endif
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 #include <sys/time.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/filedesc.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/dirent.h>
60 #include <sys/namei.h>
61 #include <sys/malloc.h>
62 #include <sys/kauth.h>
63 
64 #include <miscfs/fdesc/fdesc.h>
65 
66 VFS_PROTOS(fdesc);
67 
68 /*
69  * Mount the per-process file descriptors (/dev/fd)
70  */
71 int
72 fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len,
73     struct lwp *l)
74 {
75 	int error = 0;
76 	struct fdescmount *fmp;
77 	struct vnode *rvp;
78 
79 	if (mp->mnt_flag & MNT_GETARGS) {
80 		*data_len = 0;
81 		return 0;
82 	}
83 	/*
84 	 * Update is a no-op
85 	 */
86 	if (mp->mnt_flag & MNT_UPDATE)
87 		return (EOPNOTSUPP);
88 
89 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
90 	if (error)
91 		return (error);
92 
93 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
94 				M_UFSMNT, M_WAITOK);	/* XXX */
95 	rvp->v_type = VDIR;
96 	rvp->v_vflag |= VV_ROOT;
97 	fmp->f_root = rvp;
98 	mp->mnt_stat.f_namemax = MAXNAMLEN;
99 	mp->mnt_flag |= MNT_LOCAL;
100 	mp->mnt_data = fmp;
101 	vfs_getnewfsid(mp);
102 
103 	error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
104 	    mp->mnt_op->vfs_name, mp, l);
105 	VOP_UNLOCK(rvp, 0);
106 	return error;
107 }
108 
109 int
110 fdesc_start(struct mount *mp, int flags,
111     struct lwp *l)
112 {
113 	return (0);
114 }
115 
116 int
117 fdesc_unmount(struct mount *mp, int mntflags, struct lwp *l)
118 {
119 	int error;
120 	int flags = 0;
121 	struct vnode *rtvp = VFSTOFDESC(mp)->f_root;
122 
123 	if (mntflags & MNT_FORCE)
124 		flags |= FORCECLOSE;
125 
126 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
127 		return (EBUSY);
128 	if ((error = vflush(mp, rtvp, flags)) != 0)
129 		return (error);
130 
131 	/*
132 	 * Release reference on underlying root vnode
133 	 */
134 	vrele(rtvp);
135 	/*
136 	 * And blow it away for future re-use
137 	 */
138 	vgone(rtvp);
139 	/*
140 	 * Finally, throw away the fdescmount structure
141 	 */
142 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
143 	mp->mnt_data = 0;
144 
145 	return (0);
146 }
147 
148 int
149 fdesc_root(mp, vpp)
150 	struct mount *mp;
151 	struct vnode **vpp;
152 {
153 	struct vnode *vp;
154 
155 	/*
156 	 * Return locked reference to root.
157 	 */
158 	vp = VFSTOFDESC(mp)->f_root;
159 	VREF(vp);
160 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
161 	*vpp = vp;
162 	return (0);
163 }
164 
165 int
166 fdesc_quotactl(struct mount *mp, int cmd, uid_t uid,
167     void *arg, struct lwp *l)
168 {
169 
170 	return (EOPNOTSUPP);
171 }
172 
173 int
174 fdesc_statvfs(mp, sbp, l)
175 	struct mount *mp;
176 	struct statvfs *sbp;
177 	struct lwp *l;
178 {
179 	struct filedesc *fdp;
180 	struct proc *p;
181 	int lim;
182 	int i;
183 	int last;
184 	int freefd;
185 
186 	/*
187 	 * Compute number of free file descriptors.
188 	 * [ Strange results will ensue if the open file
189 	 * limit is ever reduced below the current number
190 	 * of open files... ]
191 	 */
192 	p = l->l_proc;
193 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
194 	fdp = p->p_fd;
195 	last = min(fdp->fd_nfiles, lim);
196 	freefd = 0;
197 	for (i = fdp->fd_freefile; i < last; i++)
198 		if (fdp->fd_ofiles[i] == NULL)
199 			freefd++;
200 
201 	/*
202 	 * Adjust for the fact that the fdesc array may not
203 	 * have been fully allocated yet.
204 	 */
205 	if (fdp->fd_nfiles < lim)
206 		freefd += (lim - fdp->fd_nfiles);
207 
208 	sbp->f_bsize = DEV_BSIZE;
209 	sbp->f_frsize = DEV_BSIZE;
210 	sbp->f_iosize = DEV_BSIZE;
211 	sbp->f_blocks = 2;		/* 1K to keep df happy */
212 	sbp->f_bfree = 0;
213 	sbp->f_bavail = 0;
214 	sbp->f_bresvd = 0;
215 	sbp->f_files = lim + 1;		/* Allow for "." */
216 	sbp->f_ffree = freefd;		/* See comments above */
217 	sbp->f_favail = freefd;		/* See comments above */
218 	sbp->f_fresvd = 0;
219 	copy_statvfs_info(sbp, mp);
220 	return (0);
221 }
222 
223 /*ARGSUSED*/
224 int
225 fdesc_sync(struct mount *mp, int waitfor,
226     kauth_cred_t uc, struct lwp *l)
227 {
228 
229 	return (0);
230 }
231 
232 /*
233  * Fdesc flat namespace lookup.
234  * Currently unsupported.
235  */
236 int
237 fdesc_vget(struct mount *mp, ino_t ino,
238     struct vnode **vpp)
239 {
240 
241 	return (EOPNOTSUPP);
242 }
243 
244 
245 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup")
246 {
247 
248 	sysctl_createv(clog, 0, NULL, NULL,
249 		       CTLFLAG_PERMANENT,
250 		       CTLTYPE_NODE, "vfs", NULL,
251 		       NULL, 0, NULL, 0,
252 		       CTL_VFS, CTL_EOL);
253 	sysctl_createv(clog, 0, NULL, NULL,
254 		       CTLFLAG_PERMANENT,
255 		       CTLTYPE_NODE, "fdesc",
256 		       SYSCTL_DESCR("File-descriptor file system"),
257 		       NULL, 0, NULL, 0,
258 		       CTL_VFS, 7, CTL_EOL);
259 	/*
260 	 * XXX the "7" above could be dynamic, thereby eliminating one
261 	 * more instance of the "number to vfs" mapping problem, but
262 	 * "7" is the order as taken from sys/mount.h
263 	 */
264 }
265 
266 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
267 
268 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
269 	&fdesc_vnodeop_opv_desc,
270 	NULL,
271 };
272 
273 struct vfsops fdesc_vfsops = {
274 	MOUNT_FDESC,
275 	0,
276 	fdesc_mount,
277 	fdesc_start,
278 	fdesc_unmount,
279 	fdesc_root,
280 	fdesc_quotactl,
281 	fdesc_statvfs,
282 	fdesc_sync,
283 	fdesc_vget,
284 	(void *)eopnotsupp,		/* vfs_fhtovp */
285 	(void *)eopnotsupp,		/* vfs_vptofh */
286 	fdesc_init,
287 	NULL,
288 	fdesc_done,
289 	NULL,				/* vfs_mountroot */
290 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
291 	vfs_stdextattrctl,
292 	(void *)eopnotsupp,		/* vfs_suspendctl */
293 	fdesc_vnodeopv_descs,
294 	0,
295 	{ NULL, NULL},
296 };
297 VFS_ATTACH(fdesc_vfsops);
298