ptyfs_vfsops.c revision 1.7 1 /* $NetBSD: ptyfs_vfsops.c,v 1.7 2005/05/11 17:38:54 christos 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 */
35
36 /*
37 * Pseudo-tty Filesystem
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ptyfs_vfsops.c,v 1.7 2005/05/11 17:38:54 christos Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
46 #include <sys/conf.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/stat.h>
52 #include <sys/dirent.h>
53 #include <sys/malloc.h>
54 #include <sys/syslog.h>
55 #include <sys/select.h>
56 #include <sys/tty.h>
57 #include <sys/pty.h>
58
59 #include <fs/ptyfs/ptyfs.h>
60 #include <miscfs/specfs/specdev.h>
61
62 MALLOC_DEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
63
64 void ptyfs_init(void);
65 void ptyfs_reinit(void);
66 void ptyfs_done(void);
67 int ptyfs_mount(struct mount *, const char *, void *, struct nameidata *,
68 struct proc *);
69 int ptyfs_start(struct mount *, int, struct proc *);
70 int ptyfs_unmount(struct mount *, int, struct proc *);
71 int ptyfs_statvfs(struct mount *, struct statvfs *, struct proc *);
72 int ptyfs_quotactl(struct mount *, int, uid_t, void *, struct proc *);
73 int ptyfs_sync(struct mount *, int, struct ucred *, struct proc *);
74 int ptyfs_vget(struct mount *, ino_t, struct vnode **);
75 int ptyfs_fhtovp(struct mount *, struct fid *, struct vnode **);
76 int ptyfs_checkexp(struct mount *, struct mbuf *, int *, struct ucred **);
77 int ptyfs_vptofh(struct vnode *, struct fid *);
78
79 static int ptyfs__allocvp(struct ptm_pty *, struct proc *, struct vnode **,
80 dev_t, char);
81 static int ptyfs__makename(struct ptm_pty *, char *, size_t, dev_t, char);
82 static void ptyfs__getvattr(struct ptm_pty *, struct proc *, struct vattr *);
83
84 /*
85 * ptm glue: When we mount, we make ptm point to us.
86 */
87 struct ptm_pty *ptyfs_save_ptm;
88
89 struct ptm_pty ptm_ptyfspty = {
90 ptyfs__allocvp,
91 ptyfs__makename,
92 ptyfs__getvattr,
93 NULL
94 };
95
96 static int
97 ptyfs__makename(struct ptm_pty *pt, char *buf, size_t bufsiz, dev_t dev,
98 char ms)
99 {
100 struct mount *mp = pt->arg;
101 size_t len;
102
103 switch (ms) {
104 case 'p':
105 /* We don't provide access to the master, should we? */
106 len = snprintf(buf, bufsiz, "/dev/null");
107 break;
108 case 't':
109 len = snprintf(buf, bufsiz, "%s/%d", mp->mnt_stat.f_mntonname,
110 minor(dev));
111 break;
112 default:
113 return EINVAL;
114 }
115
116 return len >= bufsiz ? ENOSPC : 0;
117 }
118
119
120 static int
121 /*ARGSUSED*/
122 ptyfs__allocvp(struct ptm_pty *pt, struct proc *p, struct vnode **vpp,
123 dev_t dev, char ms)
124 {
125 struct mount *mp = pt->arg;
126 ptyfstype type;
127
128 switch (ms) {
129 case 'p':
130 type = PTYFSptc;
131 break;
132 case 't':
133 type = PTYFSpts;
134 break;
135 default:
136 return EINVAL;
137 }
138
139 return ptyfs_allocvp(mp, vpp, type, minor(dev), p);
140 }
141
142
143 static void
144 ptyfs__getvattr(struct ptm_pty *pt, struct proc *p, struct vattr *vattr)
145 {
146 struct mount *mp = pt->arg;
147 struct ptyfsmount *pmnt = VFSTOPTY(mp);
148 VATTR_NULL(vattr);
149 /* get real uid */
150 vattr->va_uid = p->p_cred->p_ruid;
151 vattr->va_gid = pmnt->pmnt_gid;
152 vattr->va_mode = pmnt->pmnt_mode;
153 }
154
155
156 void
157 ptyfs_init(void)
158 {
159 #ifdef _LKM
160 malloc_type_attach(M_PTYFSMNT);
161 #endif
162 ptyfs_hashinit();
163 }
164
165 void
166 ptyfs_reinit(void)
167 {
168 ptyfs_hashreinit();
169 }
170
171 void
172 ptyfs_done(void)
173 {
174 #ifdef _LKM
175 malloc_type_detach(M_PTYFSMNT);
176 #endif
177 ptyfs_hashdone();
178 }
179
180 /*
181 * Mount the Pseudo tty params filesystem
182 */
183 int
184 ptyfs_mount(struct mount *mp, const char *path, void *data,
185 struct nameidata *ndp, struct proc *p)
186 {
187 int error = 0;
188 struct ptyfsmount *pmnt;
189 struct ptyfs_args args;
190
191 if (UIO_MX & (UIO_MX - 1)) {
192 log(LOG_ERR, "ptyfs: invalid directory entry size");
193 return EINVAL;
194 }
195
196 if (mp->mnt_flag & MNT_GETARGS) {
197 pmnt = VFSTOPTY(mp);
198 if (pmnt == NULL)
199 return EIO;
200 args.version = PTYFS_ARGSVERSION;
201 args.mode = pmnt->pmnt_mode;
202 args.gid = pmnt->pmnt_gid;
203 return copyout(&args, data, sizeof(args));
204 }
205
206 if (mp->mnt_flag & MNT_UPDATE)
207 return EOPNOTSUPP;
208
209 if (data != NULL) {
210 error = copyin(data, &args, sizeof args);
211 if (error != 0)
212 return error;
213
214 if (args.version != PTYFS_ARGSVERSION)
215 return EINVAL;
216 } else {
217 /*
218 * Arguments are mandatory.
219 */
220 return EINVAL;
221 }
222
223 pmnt = malloc(sizeof(struct ptyfsmount), M_UFSMNT, M_WAITOK);
224
225 mp->mnt_data = pmnt;
226 pmnt->pmnt_gid = args.gid;
227 pmnt->pmnt_mode = args.mode;
228 mp->mnt_flag |= MNT_LOCAL;
229 vfs_getnewfsid(mp);
230
231 if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
232 UIO_SYSSPACE, mp, p)) != 0) {
233 return error;
234 }
235
236 /* Point pty access to us */
237 if (ptyfs_save_ptm != NULL)
238 return EBUSY;
239
240 ptm_ptyfspty.arg = mp;
241 ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
242 return 0;
243 }
244
245 /*ARGSUSED*/
246 int
247 ptyfs_start(struct mount *mp, int flags, struct proc *p)
248 {
249 return 0;
250 }
251
252 /*ARGSUSED*/
253 int
254 ptyfs_unmount(struct mount *mp, int mntflags, struct proc *p)
255 {
256 int error;
257 int flags = 0;
258
259 if (mntflags & MNT_FORCE)
260 flags |= FORCECLOSE;
261
262 if ((error = vflush(mp, 0, flags)) != 0)
263 return (error);
264
265 /* Restore where pty access was pointing */
266 ptm_ptyfspty.arg = NULL;
267 (void)pty_sethandler(ptyfs_save_ptm);
268 ptyfs_save_ptm = NULL;
269
270 /*
271 * Finally, throw away the ptyfsmount structure
272 */
273 free(mp->mnt_data, M_UFSMNT);
274 mp->mnt_data = 0;
275
276 return 0;
277 }
278
279 int
280 ptyfs_root(struct mount *mp, struct vnode **vpp)
281 {
282 /* setup "." */
283 return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
284 }
285
286 /*ARGSUSED*/
287 int
288 ptyfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct proc *p)
289 {
290 return EOPNOTSUPP;
291 }
292
293 /*ARGSUSED*/
294 int
295 ptyfs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
296 {
297 sbp->f_bsize = DEV_BSIZE;
298 sbp->f_frsize = DEV_BSIZE;
299 sbp->f_iosize = DEV_BSIZE;
300 sbp->f_blocks = 2; /* 1K to keep df happy */
301 sbp->f_bfree = 0;
302 sbp->f_bavail = 0;
303 sbp->f_bresvd = 0;
304 sbp->f_files = 1024; /* XXX lie */
305 sbp->f_ffree = 128; /* XXX lie */
306 sbp->f_favail = 128; /* XXX lie */
307 sbp->f_fresvd = 0;
308 sbp->f_namemax = MAXNAMLEN;
309 copy_statvfs_info(sbp, mp);
310 return 0;
311 }
312
313 /*ARGSUSED*/
314 int
315 ptyfs_sync(struct mount *mp, int waitfor, struct ucred *uc, struct proc *p)
316 {
317 return 0;
318 }
319
320 /*
321 * Kernfs flat namespace lookup.
322 * Currently unsupported.
323 */
324 /*ARGSUSED*/
325 int
326 ptyfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
327 {
328 return EOPNOTSUPP;
329 }
330
331 /*ARGSUSED*/
332 int
333 ptyfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
334 {
335 return EOPNOTSUPP;
336 }
337
338 /*ARGSUSED*/
339 int
340 ptyfs_checkexp(struct mount *mp, struct mbuf *mb, int *wh, struct ucred **anon)
341 {
342 return EOPNOTSUPP;
343 }
344
345 SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup")
346 {
347
348 sysctl_createv(clog, 0, NULL, NULL,
349 CTLFLAG_PERMANENT,
350 CTLTYPE_NODE, "vfs", NULL,
351 NULL, 0, NULL, 0,
352 CTL_VFS, CTL_EOL);
353 sysctl_createv(clog, 0, NULL, NULL,
354 CTLFLAG_PERMANENT,
355 CTLTYPE_NODE, "ptyfs",
356 SYSCTL_DESCR("Pty file system"),
357 NULL, 0, NULL, 0,
358 CTL_VFS, 23, CTL_EOL);
359 /*
360 * XXX the "23" above could be dynamic, thereby eliminating
361 * one more instance of the "number to vfs" mapping problem,
362 * but "23" is the order as taken from sys/mount.h
363 */
364 }
365
366
367 /*ARGSUSED*/
368 int
369 ptyfs_vptofh(struct vnode *vp, struct fid *fhp)
370 {
371 return EOPNOTSUPP;
372 }
373
374 extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
375
376 const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
377 &ptyfs_vnodeop_opv_desc,
378 NULL,
379 };
380
381 struct vfsops ptyfs_vfsops = {
382 MOUNT_PTYFS,
383 ptyfs_mount,
384 ptyfs_start,
385 ptyfs_unmount,
386 ptyfs_root,
387 ptyfs_quotactl,
388 ptyfs_statvfs,
389 ptyfs_sync,
390 ptyfs_vget,
391 ptyfs_fhtovp,
392 ptyfs_vptofh,
393 ptyfs_init,
394 ptyfs_reinit,
395 ptyfs_done,
396 NULL,
397 NULL, /* vfs_mountroot */
398 ptyfs_checkexp,
399 (int (*)(struct mount *, struct vnode *, struct timespec *))eopnotsupp,
400 (int (*)(struct mount *, int, struct vnode *, int, const char *,
401 struct proc *))eopnotsupp,
402 ptyfs_vnodeopv_descs,
403 };
404 VFS_ATTACH(ptyfs_vfsops);
405