fdesc_vfsops.c revision 1.72 1 /* $NetBSD: fdesc_vfsops.c,v 1.72 2008/01/02 11:48:58 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.72 2008/01/02 11:48:58 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 {
74 struct lwp *l = curlwp;
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 {
112 return (0);
113 }
114
115 int
116 fdesc_unmount(struct mount *mp, int mntflags)
117 {
118 int error;
119 int flags = 0;
120 struct vnode *rtvp = VFSTOFDESC(mp)->f_root;
121
122 if (mntflags & MNT_FORCE)
123 flags |= FORCECLOSE;
124
125 if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
126 return (EBUSY);
127 if ((error = vflush(mp, rtvp, flags)) != 0)
128 return (error);
129
130 /*
131 * Blow it away for future re-use
132 */
133 vgone(rtvp);
134 /*
135 * Finally, throw away the fdescmount structure
136 */
137 free(mp->mnt_data, M_UFSMNT); /* XXX */
138 mp->mnt_data = 0;
139
140 return (0);
141 }
142
143 int
144 fdesc_root(mp, vpp)
145 struct mount *mp;
146 struct vnode **vpp;
147 {
148 struct vnode *vp;
149
150 /*
151 * Return locked reference to root.
152 */
153 vp = VFSTOFDESC(mp)->f_root;
154 VREF(vp);
155 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
156 *vpp = vp;
157 return (0);
158 }
159
160 int
161 fdesc_statvfs(mp, sbp)
162 struct mount *mp;
163 struct statvfs *sbp;
164 {
165 struct lwp *l = curlwp;
166 struct filedesc *fdp;
167 struct proc *p;
168 int lim;
169 int i;
170 int last;
171 int freefd;
172
173 /*
174 * Compute number of free file descriptors.
175 * [ Strange results will ensue if the open file
176 * limit is ever reduced below the current number
177 * of open files... ]
178 */
179 p = l->l_proc;
180 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
181 fdp = p->p_fd;
182 last = min(fdp->fd_nfiles, lim);
183 freefd = 0;
184 for (i = fdp->fd_freefile; i < last; i++)
185 if (fdp->fd_ofiles[i] == NULL)
186 freefd++;
187
188 /*
189 * Adjust for the fact that the fdesc array may not
190 * have been fully allocated yet.
191 */
192 if (fdp->fd_nfiles < lim)
193 freefd += (lim - fdp->fd_nfiles);
194
195 sbp->f_bsize = DEV_BSIZE;
196 sbp->f_frsize = DEV_BSIZE;
197 sbp->f_iosize = DEV_BSIZE;
198 sbp->f_blocks = 2; /* 1K to keep df happy */
199 sbp->f_bfree = 0;
200 sbp->f_bavail = 0;
201 sbp->f_bresvd = 0;
202 sbp->f_files = lim + 1; /* Allow for "." */
203 sbp->f_ffree = freefd; /* See comments above */
204 sbp->f_favail = freefd; /* See comments above */
205 sbp->f_fresvd = 0;
206 copy_statvfs_info(sbp, mp);
207 return (0);
208 }
209
210 /*ARGSUSED*/
211 int
212 fdesc_sync(struct mount *mp, int waitfor,
213 kauth_cred_t uc)
214 {
215
216 return (0);
217 }
218
219 /*
220 * Fdesc flat namespace lookup.
221 * Currently unsupported.
222 */
223 int
224 fdesc_vget(struct mount *mp, ino_t ino,
225 struct vnode **vpp)
226 {
227
228 return (EOPNOTSUPP);
229 }
230
231
232 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup")
233 {
234
235 sysctl_createv(clog, 0, NULL, NULL,
236 CTLFLAG_PERMANENT,
237 CTLTYPE_NODE, "vfs", NULL,
238 NULL, 0, NULL, 0,
239 CTL_VFS, CTL_EOL);
240 sysctl_createv(clog, 0, NULL, NULL,
241 CTLFLAG_PERMANENT,
242 CTLTYPE_NODE, "fdesc",
243 SYSCTL_DESCR("File-descriptor file system"),
244 NULL, 0, NULL, 0,
245 CTL_VFS, 7, CTL_EOL);
246 /*
247 * XXX the "7" above could be dynamic, thereby eliminating one
248 * more instance of the "number to vfs" mapping problem, but
249 * "7" is the order as taken from sys/mount.h
250 */
251 }
252
253 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
254
255 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
256 &fdesc_vnodeop_opv_desc,
257 NULL,
258 };
259
260 struct vfsops fdesc_vfsops = {
261 MOUNT_FDESC,
262 0,
263 fdesc_mount,
264 fdesc_start,
265 fdesc_unmount,
266 fdesc_root,
267 (void *)eopnotsupp, /* vfs_quotactl */
268 fdesc_statvfs,
269 fdesc_sync,
270 fdesc_vget,
271 (void *)eopnotsupp, /* vfs_fhtovp */
272 (void *)eopnotsupp, /* vfs_vptofh */
273 fdesc_init,
274 NULL,
275 fdesc_done,
276 NULL, /* vfs_mountroot */
277 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
278 vfs_stdextattrctl,
279 (void *)eopnotsupp, /* vfs_suspendctl */
280 fdesc_vnodeopv_descs,
281 0,
282 { NULL, NULL},
283 };
284 VFS_ATTACH(fdesc_vfsops);
285