fdesc_vfsops.c revision 1.65 1 /* $NetBSD: fdesc_vfsops.c,v 1.65 2007/07/08 23:58:53 pooka 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.65 2007/07/08 23:58:53 pooka 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 int fdesc_mount(struct mount *, const char *, void *,
67 struct nameidata *, struct lwp *);
68 int fdesc_start(struct mount *, int, struct lwp *);
69 int fdesc_unmount(struct mount *, int, struct lwp *);
70 int fdesc_quotactl(struct mount *, int, uid_t, void *,
71 struct lwp *);
72 int fdesc_statvfs(struct mount *, struct statvfs *, struct lwp *);
73 int fdesc_sync(struct mount *, int, kauth_cred_t, struct lwp *);
74 int fdesc_vget(struct mount *, ino_t, struct vnode **);
75
76 /*
77 * Mount the per-process file descriptors (/dev/fd)
78 */
79 int
80 fdesc_mount(struct mount *mp, const char *path, void *data,
81 struct nameidata *ndp, struct lwp *l)
82 {
83 int error = 0;
84 struct fdescmount *fmp;
85 struct vnode *rvp;
86
87 if (mp->mnt_flag & MNT_GETARGS)
88 return 0;
89 /*
90 * Update is a no-op
91 */
92 if (mp->mnt_flag & MNT_UPDATE)
93 return (EOPNOTSUPP);
94
95 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
96 if (error)
97 return (error);
98
99 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
100 M_UFSMNT, M_WAITOK); /* XXX */
101 rvp->v_type = VDIR;
102 rvp->v_flag |= VROOT;
103 fmp->f_root = rvp;
104 mp->mnt_stat.f_namemax = MAXNAMLEN;
105 mp->mnt_flag |= MNT_LOCAL;
106 mp->mnt_data = fmp;
107 vfs_getnewfsid(mp);
108
109 error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
110 mp, l);
111 VOP_UNLOCK(rvp, 0);
112 return error;
113 }
114
115 int
116 fdesc_start(struct mount *mp, int flags,
117 struct lwp *l)
118 {
119 return (0);
120 }
121
122 int
123 fdesc_unmount(struct mount *mp, int mntflags, struct lwp *l)
124 {
125 int error;
126 int flags = 0;
127 struct vnode *rtvp = VFSTOFDESC(mp)->f_root;
128
129 if (mntflags & MNT_FORCE)
130 flags |= FORCECLOSE;
131
132 if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
133 return (EBUSY);
134 if ((error = vflush(mp, rtvp, flags)) != 0)
135 return (error);
136
137 /*
138 * Release reference on underlying root vnode
139 */
140 vrele(rtvp);
141 /*
142 * And blow it away for future re-use
143 */
144 vgone(rtvp);
145 /*
146 * Finally, throw away the fdescmount structure
147 */
148 free(mp->mnt_data, M_UFSMNT); /* XXX */
149 mp->mnt_data = 0;
150
151 return (0);
152 }
153
154 int
155 fdesc_root(mp, vpp)
156 struct mount *mp;
157 struct vnode **vpp;
158 {
159 struct vnode *vp;
160
161 /*
162 * Return locked reference to root.
163 */
164 vp = VFSTOFDESC(mp)->f_root;
165 VREF(vp);
166 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
167 *vpp = vp;
168 return (0);
169 }
170
171 int
172 fdesc_quotactl(struct mount *mp, int cmd, uid_t uid,
173 void *arg, struct lwp *l)
174 {
175
176 return (EOPNOTSUPP);
177 }
178
179 int
180 fdesc_statvfs(mp, sbp, l)
181 struct mount *mp;
182 struct statvfs *sbp;
183 struct lwp *l;
184 {
185 struct filedesc *fdp;
186 struct proc *p;
187 int lim;
188 int i;
189 int last;
190 int freefd;
191
192 /*
193 * Compute number of free file descriptors.
194 * [ Strange results will ensue if the open file
195 * limit is ever reduced below the current number
196 * of open files... ]
197 */
198 p = l->l_proc;
199 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
200 fdp = p->p_fd;
201 last = min(fdp->fd_nfiles, lim);
202 freefd = 0;
203 for (i = fdp->fd_freefile; i < last; i++)
204 if (fdp->fd_ofiles[i] == NULL)
205 freefd++;
206
207 /*
208 * Adjust for the fact that the fdesc array may not
209 * have been fully allocated yet.
210 */
211 if (fdp->fd_nfiles < lim)
212 freefd += (lim - fdp->fd_nfiles);
213
214 sbp->f_bsize = DEV_BSIZE;
215 sbp->f_frsize = DEV_BSIZE;
216 sbp->f_iosize = DEV_BSIZE;
217 sbp->f_blocks = 2; /* 1K to keep df happy */
218 sbp->f_bfree = 0;
219 sbp->f_bavail = 0;
220 sbp->f_bresvd = 0;
221 sbp->f_files = lim + 1; /* Allow for "." */
222 sbp->f_ffree = freefd; /* See comments above */
223 sbp->f_favail = freefd; /* See comments above */
224 sbp->f_fresvd = 0;
225 copy_statvfs_info(sbp, mp);
226 return (0);
227 }
228
229 /*ARGSUSED*/
230 int
231 fdesc_sync(struct mount *mp, int waitfor,
232 kauth_cred_t uc, struct lwp *l)
233 {
234
235 return (0);
236 }
237
238 /*
239 * Fdesc flat namespace lookup.
240 * Currently unsupported.
241 */
242 int
243 fdesc_vget(struct mount *mp, ino_t ino,
244 struct vnode **vpp)
245 {
246
247 return (EOPNOTSUPP);
248 }
249
250
251 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup")
252 {
253
254 sysctl_createv(clog, 0, NULL, NULL,
255 CTLFLAG_PERMANENT,
256 CTLTYPE_NODE, "vfs", NULL,
257 NULL, 0, NULL, 0,
258 CTL_VFS, CTL_EOL);
259 sysctl_createv(clog, 0, NULL, NULL,
260 CTLFLAG_PERMANENT,
261 CTLTYPE_NODE, "fdesc",
262 SYSCTL_DESCR("File-descriptor file system"),
263 NULL, 0, NULL, 0,
264 CTL_VFS, 7, CTL_EOL);
265 /*
266 * XXX the "7" above could be dynamic, thereby eliminating one
267 * more instance of the "number to vfs" mapping problem, but
268 * "7" is the order as taken from sys/mount.h
269 */
270 }
271
272 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
273
274 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
275 &fdesc_vnodeop_opv_desc,
276 NULL,
277 };
278
279 struct vfsops fdesc_vfsops = {
280 MOUNT_FDESC,
281 fdesc_mount,
282 fdesc_start,
283 fdesc_unmount,
284 fdesc_root,
285 fdesc_quotactl,
286 fdesc_statvfs,
287 fdesc_sync,
288 fdesc_vget,
289 (void *)eopnotsupp, /* vfs_fhtovp */
290 (void *)eopnotsupp, /* vfs_vptofh */
291 fdesc_init,
292 NULL,
293 fdesc_done,
294 NULL, /* vfs_mountroot */
295 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
296 vfs_stdextattrctl,
297 vfs_stdsuspendctl,
298 fdesc_vnodeopv_descs,
299 0,
300 { NULL, NULL},
301 };
302 VFS_ATTACH(fdesc_vfsops);
303