xref: /freebsd/sys/fs/fdescfs/fdesc_vnops.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)fdesc_vnops.c	8.9 (Berkeley) 1/21/94
37  *
38  * $FreeBSD$
39  */
40 
41 /*
42  * /dev/fd Filesystem
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/kernel.h>	/* boottime */
49 #include <sys/filedesc.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/file.h>
53 #include <sys/stat.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/dirent.h>
57 #include <sys/conf.h>
58 #include <miscfs/fdesc/fdesc.h>
59 
60 #define FDL_WANT	0x01
61 #define FDL_LOCKED	0x02
62 static int fdcache_lock;
63 
64 static vop_t **fdesc_vnodeop_p;
65 
66 #define	NFDCACHE 4
67 #define FD_NHASH(ix) \
68 	(&fdhashtbl[(ix) & fdhash])
69 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
70 static u_long fdhash;
71 
72 static int	fdesc_badop __P((void));
73 static int	fdesc_getattr __P((struct vop_getattr_args *ap));
74 static int	fdesc_inactive __P((struct vop_inactive_args *ap));
75 static int	fdesc_lookup __P((struct vop_lookup_args *ap));
76 static int	fdesc_open __P((struct vop_open_args *ap));
77 static int	fdesc_print __P((struct vop_print_args *ap));
78 static int	fdesc_readdir __P((struct vop_readdir_args *ap));
79 static int	fdesc_reclaim __P((struct vop_reclaim_args *ap));
80 static int	fdesc_poll __P((struct vop_poll_args *ap));
81 static int	fdesc_setattr __P((struct vop_setattr_args *ap));
82 
83 /*
84  * Initialise cache headers
85  */
86 int
87 fdesc_init(vfsp)
88 	struct vfsconf *vfsp;
89 {
90 
91 	fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
92 	return (0);
93 }
94 
95 int
96 fdesc_allocvp(ftype, ix, mp, vpp, p)
97 	fdntype ftype;
98 	int ix;
99 	struct mount *mp;
100 	struct vnode **vpp;
101 	struct proc *p;
102 {
103 	struct fdhashhead *fc;
104 	struct fdescnode *fd;
105 	int error = 0;
106 
107 	fc = FD_NHASH(ix);
108 loop:
109 	for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) {
110 		if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
111 			if (vget(fd->fd_vnode, 0, p))
112 				goto loop;
113 			*vpp = fd->fd_vnode;
114 			return (error);
115 		}
116 	}
117 
118 	/*
119 	 * otherwise lock the array while we call getnewvnode
120 	 * since that can block.
121 	 */
122 	if (fdcache_lock & FDL_LOCKED) {
123 		fdcache_lock |= FDL_WANT;
124 		(void) tsleep((caddr_t) &fdcache_lock, PINOD, "fdalvp", 0);
125 		goto loop;
126 	}
127 	fdcache_lock |= FDL_LOCKED;
128 
129 	/*
130 	 * Do the MALLOC before the getnewvnode since doing so afterward
131 	 * might cause a bogus v_data pointer to get dereferenced
132 	 * elsewhere if MALLOC should block.
133 	 */
134 	MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
135 
136 	error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp);
137 	if (error) {
138 		FREE(fd, M_TEMP);
139 		goto out;
140 	}
141 	(*vpp)->v_data = fd;
142 	fd->fd_vnode = *vpp;
143 	fd->fd_type = ftype;
144 	fd->fd_fd = -1;
145 	fd->fd_ix = ix;
146 	LIST_INSERT_HEAD(fc, fd, fd_hash);
147 
148 out:
149 	fdcache_lock &= ~FDL_LOCKED;
150 
151 	if (fdcache_lock & FDL_WANT) {
152 		fdcache_lock &= ~FDL_WANT;
153 		wakeup((caddr_t) &fdcache_lock);
154 	}
155 
156 	return (error);
157 }
158 
159 /*
160  * vp is the current namei directory
161  * ndp is the name to locate in that directory...
162  */
163 static int
164 fdesc_lookup(ap)
165 	struct vop_lookup_args /* {
166 		struct vnode * a_dvp;
167 		struct vnode ** a_vpp;
168 		struct componentname * a_cnp;
169 	} */ *ap;
170 {
171 	struct vnode **vpp = ap->a_vpp;
172 	struct vnode *dvp = ap->a_dvp;
173 	struct componentname *cnp = ap->a_cnp;
174 	char *pname = cnp->cn_nameptr;
175 	struct proc *p = cnp->cn_proc;
176 	int nlen = cnp->cn_namelen;
177 	int nfiles = p->p_fd->fd_nfiles;
178 	u_int fd;
179 	int error;
180 	struct vnode *fvp;
181 
182 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
183 		error = EROFS;
184 		goto bad;
185 	}
186 
187 	VOP_UNLOCK(dvp, 0, p);
188 	if (cnp->cn_namelen == 1 && *pname == '.') {
189 		*vpp = dvp;
190 		VREF(dvp);
191 		vn_lock(dvp, LK_SHARED | LK_RETRY, p);
192 		return (0);
193 	}
194 
195 	if (VTOFDESC(dvp)->fd_type != Froot) {
196 		error = ENOTDIR;
197 		goto bad;
198 	}
199 
200 	fd = 0;
201 	/* the only time a leading 0 is acceptable is if it's "0" */
202 	if (*pname == '0' && nlen != 1) {
203 		error = ENOENT;
204 		goto bad;
205 	}
206 	while (nlen--) {
207 		if (*pname < '0' || *pname > '9') {
208 			error = ENOENT;
209 			goto bad;
210 		}
211 		fd = 10 * fd + *pname++ - '0';
212 	}
213 
214 	if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
215 		error = EBADF;
216 		goto bad;
217 	}
218 
219 	error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, p);
220 	if (error)
221 		goto bad;
222 	VTOFDESC(fvp)->fd_fd = fd;
223 	vn_lock(fvp, LK_SHARED | LK_RETRY, p);
224 	*vpp = fvp;
225 	return (0);
226 
227 bad:
228 	vn_lock(dvp, LK_SHARED | LK_RETRY, p);
229 	*vpp = NULL;
230 	return (error);
231 }
232 
233 static int
234 fdesc_open(ap)
235 	struct vop_open_args /* {
236 		struct vnode *a_vp;
237 		int  a_mode;
238 		struct ucred *a_cred;
239 		struct proc *a_p;
240 	} */ *ap;
241 {
242 	struct vnode *vp = ap->a_vp;
243 
244 	if (VTOFDESC(vp)->fd_type == Froot)
245 		return (0);
246 
247 	/*
248 	 * XXX Kludge: set p->p_dupfd to contain the value of the the file
249 	 * descriptor being sought for duplication. The error return ensures
250 	 * that the vnode for this device will be released by vn_open. Open
251 	 * will detect this special error and take the actions in dupfdopen.
252 	 * Other callers of vn_open or VOP_OPEN will simply report the
253 	 * error.
254 	 */
255 	ap->a_p->p_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
256 	return (ENODEV);
257 }
258 
259 static int
260 fdesc_getattr(ap)
261 	struct vop_getattr_args /* {
262 		struct vnode *a_vp;
263 		struct vattr *a_vap;
264 		struct ucred *a_cred;
265 		struct proc *a_p;
266 	} */ *ap;
267 {
268 	struct vnode *vp = ap->a_vp;
269 	struct vattr *vap = ap->a_vap;
270 	struct filedesc *fdp = ap->a_p->p_fd;
271 	struct file *fp;
272 	struct stat stb;
273 	u_int fd;
274 	int error = 0;
275 
276 	switch (VTOFDESC(vp)->fd_type) {
277 	case Froot:
278 		VATTR_NULL(vap);
279 
280 		vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
281 		vap->va_type = VDIR;
282 		vap->va_nlink = 2;
283 		vap->va_size = DEV_BSIZE;
284 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
285 		vap->va_uid = 0;
286 		vap->va_gid = 0;
287 		vap->va_blocksize = DEV_BSIZE;
288 		vap->va_atime.tv_sec = boottime.tv_sec;
289 		vap->va_atime.tv_nsec = 0;
290 		vap->va_mtime = vap->va_atime;
291 		vap->va_ctime = vap->va_mtime;
292 		vap->va_gen = 0;
293 		vap->va_flags = 0;
294 		vap->va_rdev = 0;
295 		vap->va_bytes = 0;
296 		break;
297 
298 	case Fdesc:
299 		fd = VTOFDESC(vp)->fd_fd;
300 
301 		if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
302 			return (EBADF);
303 
304 		bzero(&stb, sizeof(stb));
305 		error = fo_stat(fp, &stb, ap->a_p);
306 		if (error == 0) {
307 			VATTR_NULL(vap);
308 			vap->va_type = IFTOVT(stb.st_mode);
309 			vap->va_mode = stb.st_mode;
310 #define FDRX (VREAD|VEXEC)
311 			if (vap->va_type == VDIR)
312 				vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
313 #undef FDRX
314 			vap->va_nlink = 1;
315 			vap->va_flags = 0;
316 			vap->va_bytes = stb.st_blocks * stb.st_blksize;
317 			vap->va_fileid = VTOFDESC(vp)->fd_ix;
318 			vap->va_size = stb.st_size;
319 			vap->va_blocksize = stb.st_blksize;
320 			vap->va_rdev = stb.st_rdev;
321 
322 			/*
323 			 * If no time data is provided, use the current time.
324 			 */
325 			if (stb.st_atimespec.tv_sec == 0 &&
326 			    stb.st_atimespec.tv_nsec == 0)
327 				nanotime(&stb.st_atimespec);
328 
329 			if (stb.st_ctimespec.tv_sec == 0 &&
330 			    stb.st_ctimespec.tv_nsec == 0)
331 				nanotime(&stb.st_ctimespec);
332 
333 			if (stb.st_mtimespec.tv_sec == 0 &&
334 			    stb.st_mtimespec.tv_nsec == 0)
335 				nanotime(&stb.st_mtimespec);
336 
337 			vap->va_atime = stb.st_atimespec;
338 			vap->va_mtime = stb.st_mtimespec;
339 			vap->va_ctime = stb.st_ctimespec;
340 			vap->va_uid = stb.st_uid;
341 			vap->va_gid = stb.st_gid;
342 		}
343 		break;
344 
345 	default:
346 		panic("fdesc_getattr");
347 		break;
348 	}
349 
350 	if (error == 0)
351 		vp->v_type = vap->va_type;
352 	return (error);
353 }
354 
355 static int
356 fdesc_setattr(ap)
357 	struct vop_setattr_args /* {
358 		struct vnode *a_vp;
359 		struct vattr *a_vap;
360 		struct ucred *a_cred;
361 		struct proc *a_p;
362 	} */ *ap;
363 {
364 	struct vattr *vap = ap->a_vap;
365 	struct vnode *vp;
366 	struct mount *mp;
367 	struct file *fp;
368 	unsigned fd;
369 	int error;
370 
371 	/*
372 	 * Can't mess with the root vnode
373 	 */
374 	if (VTOFDESC(ap->a_vp)->fd_type == Froot)
375 		return (EACCES);
376 
377 	fd = VTOFDESC(ap->a_vp)->fd_fd;
378 
379 	/*
380 	 * Allow setattr where there is an underlying vnode.
381 	 */
382 	error = getvnode(ap->a_p->p_fd, fd, &fp);
383 	if (error) {
384 		/*
385 		 * getvnode() returns EINVAL if the file descriptor is not
386 		 * backed by a vnode.  Silently drop all changes except
387 		 * chflags(2) in this case.
388 		 */
389 		if (error == EINVAL) {
390 			if (vap->va_flags != VNOVAL)
391 				error = EOPNOTSUPP;
392 			else
393 				error = 0;
394 		}
395 		return (error);
396 	}
397 	vp = (struct vnode *)fp->f_data;
398 	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
399 		return (error);
400 	error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
401 	vn_finished_write(mp);
402 	return (error);
403 }
404 
405 #define UIO_MX 16
406 
407 static int
408 fdesc_readdir(ap)
409 	struct vop_readdir_args /* {
410 		struct vnode *a_vp;
411 		struct uio *a_uio;
412 		struct ucred *a_cred;
413 		int *a_eofflag;
414 		u_long *a_cookies;
415 		int a_ncookies;
416 	} */ *ap;
417 {
418 	struct uio *uio = ap->a_uio;
419 	struct filedesc *fdp;
420 	struct dirent d;
421 	struct dirent *dp = &d;
422 	int error, i, off, fcnt;
423 
424 	/*
425 	 * We don't allow exporting fdesc mounts, and currently local
426 	 * requests do not need cookies.
427 	 */
428 	if (ap->a_ncookies)
429 		panic("fdesc_readdir: not hungry");
430 
431 	if (VTOFDESC(ap->a_vp)->fd_type != Froot)
432 		panic("fdesc_readdir: not dir");
433 
434 	off = (int)uio->uio_offset;
435 	if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
436 	    uio->uio_resid < UIO_MX)
437 		return (EINVAL);
438 	i = (u_int)off / UIO_MX;
439 	fdp = uio->uio_procp->p_fd;
440 	error = 0;
441 
442 	fcnt = i - 2;		/* The first two nodes are `.' and `..' */
443 
444 	while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
445 		switch (i) {
446 		case 0:	/* `.' */
447 		case 1: /* `..' */
448 			bzero((caddr_t)dp, UIO_MX);
449 
450 			dp->d_fileno = i + FD_ROOT;
451 			dp->d_namlen = i + 1;
452 			dp->d_reclen = UIO_MX;
453 			bcopy("..", dp->d_name, dp->d_namlen);
454 			dp->d_name[i + 1] = '\0';
455 			dp->d_type = DT_DIR;
456 			break;
457 		default:
458 			if (fdp->fd_ofiles[fcnt] == NULL)
459 				goto done;
460 
461 			bzero((caddr_t) dp, UIO_MX);
462 			dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
463 			dp->d_reclen = UIO_MX;
464 			dp->d_type = DT_UNKNOWN;
465 			dp->d_fileno = i + FD_DESC;
466 			break;
467 		}
468 		/*
469 		 * And ship to userland
470 		 */
471 		error = uiomove((caddr_t) dp, UIO_MX, uio);
472 		if (error)
473 			break;
474 		i++;
475 		fcnt++;
476 	}
477 
478 done:
479 	uio->uio_offset = i * UIO_MX;
480 	return (error);
481 }
482 
483 static int
484 fdesc_poll(ap)
485 	struct vop_poll_args /* {
486 		struct vnode *a_vp;
487 		int  a_events;
488 		struct ucred *a_cred;
489 		struct proc *a_p;
490 	} */ *ap;
491 {
492 	return seltrue(0, ap->a_events, ap->a_p);
493 }
494 
495 static int
496 fdesc_inactive(ap)
497 	struct vop_inactive_args /* {
498 		struct vnode *a_vp;
499 		struct proc *a_p;
500 	} */ *ap;
501 {
502 	struct vnode *vp = ap->a_vp;
503 
504 	/*
505 	 * Clear out the v_type field to avoid
506 	 * nasty things happening in vgone().
507 	 */
508 	VOP_UNLOCK(vp, 0, ap->a_p);
509 	vp->v_type = VNON;
510 	return (0);
511 }
512 
513 static int
514 fdesc_reclaim(ap)
515 	struct vop_reclaim_args /* {
516 		struct vnode *a_vp;
517 	} */ *ap;
518 {
519 	struct vnode *vp = ap->a_vp;
520 	struct fdescnode *fd = VTOFDESC(vp);
521 
522 	LIST_REMOVE(fd, fd_hash);
523 	FREE(vp->v_data, M_TEMP);
524 	vp->v_data = 0;
525 
526 	return (0);
527 }
528 
529 /*
530  * Print out the contents of a /dev/fd vnode.
531  */
532 /* ARGSUSED */
533 static int
534 fdesc_print(ap)
535 	struct vop_print_args /* {
536 		struct vnode *a_vp;
537 	} */ *ap;
538 {
539 
540 	printf("tag VT_NON, fdesc vnode\n");
541 	return (0);
542 }
543 
544 /*
545  * /dev/fd "should never get here" operation
546  */
547 static int
548 fdesc_badop()
549 {
550 
551 	panic("fdesc: bad op");
552 	/* NOTREACHED */
553 }
554 
555 static struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
556 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
557 	{ &vop_access_desc,		(vop_t *) vop_null },
558 	{ &vop_bmap_desc,		(vop_t *) fdesc_badop },
559 	{ &vop_getattr_desc,		(vop_t *) fdesc_getattr },
560 	{ &vop_inactive_desc,		(vop_t *) fdesc_inactive },
561 	{ &vop_lookup_desc,		(vop_t *) fdesc_lookup },
562 	{ &vop_open_desc,		(vop_t *) fdesc_open },
563 	{ &vop_pathconf_desc,		(vop_t *) vop_stdpathconf },
564 	{ &vop_poll_desc,		(vop_t *) fdesc_poll },
565 	{ &vop_print_desc,		(vop_t *) fdesc_print },
566 	{ &vop_readdir_desc,		(vop_t *) fdesc_readdir },
567 	{ &vop_reclaim_desc,		(vop_t *) fdesc_reclaim },
568 	{ &vop_setattr_desc,		(vop_t *) fdesc_setattr },
569 	{ NULL, NULL }
570 };
571 static struct vnodeopv_desc fdesc_vnodeop_opv_desc =
572 	{ &fdesc_vnodeop_p, fdesc_vnodeop_entries };
573 
574 VNODEOP_SET(fdesc_vnodeop_opv_desc);
575