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