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/capability.h>
44#include <sys/conf.h>
45#include <sys/dirent.h>
46#include <sys/filedesc.h>
47#include <sys/kernel.h>	/* boottime */
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/malloc.h>
51#include <sys/file.h>	/* Must come after sys/malloc.h */
52#include <sys/mount.h>
53#include <sys/namei.h>
54#include <sys/proc.h>
55#include <sys/stat.h>
56#include <sys/vnode.h>
57
58#include <fs/fdescfs/fdesc.h>
59
60#define	NFDCACHE 4
61#define FD_NHASH(ix) \
62	(&fdhashtbl[(ix) & fdhash])
63static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
64static u_long fdhash;
65
66struct mtx fdesc_hashmtx;
67
68static vop_getattr_t	fdesc_getattr;
69static vop_lookup_t	fdesc_lookup;
70static vop_open_t	fdesc_open;
71static vop_readdir_t	fdesc_readdir;
72static vop_reclaim_t	fdesc_reclaim;
73static vop_setattr_t	fdesc_setattr;
74
75static struct vop_vector fdesc_vnodeops = {
76	.vop_default =		&default_vnodeops,
77
78	.vop_access =		VOP_NULL,
79	.vop_getattr =		fdesc_getattr,
80	.vop_lookup =		fdesc_lookup,
81	.vop_open =		fdesc_open,
82	.vop_pathconf =		vop_stdpathconf,
83	.vop_readdir =		fdesc_readdir,
84	.vop_reclaim =		fdesc_reclaim,
85	.vop_setattr =		fdesc_setattr,
86};
87
88static void fdesc_insmntque_dtr(struct vnode *, void *);
89static void fdesc_remove_entry(struct fdescnode *);
90
91/*
92 * Initialise cache headers
93 */
94int
95fdesc_init(vfsp)
96	struct vfsconf *vfsp;
97{
98
99	mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
100	fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
101	return (0);
102}
103
104/*
105 * Uninit ready for unload.
106 */
107int
108fdesc_uninit(vfsp)
109	struct vfsconf *vfsp;
110{
111
112	hashdestroy(fdhashtbl, M_CACHE, fdhash);
113	mtx_destroy(&fdesc_hashmtx);
114	return (0);
115}
116
117/*
118 * If allocating vnode fails, call this.
119 */
120static void
121fdesc_insmntque_dtr(struct vnode *vp, void *arg)
122{
123
124	vgone(vp);
125	vput(vp);
126}
127
128/*
129 * Remove an entry from the hash if it exists.
130 */
131static void
132fdesc_remove_entry(struct fdescnode *fd)
133{
134	struct fdhashhead *fc;
135	struct fdescnode *fd2;
136
137	fc = FD_NHASH(fd->fd_ix);
138	mtx_lock(&fdesc_hashmtx);
139	LIST_FOREACH(fd2, fc, fd_hash) {
140		if (fd == fd2) {
141			LIST_REMOVE(fd, fd_hash);
142			break;
143		}
144	}
145	mtx_unlock(&fdesc_hashmtx);
146}
147
148int
149fdesc_allocvp(ftype, fd_fd, ix, mp, vpp)
150	fdntype ftype;
151	unsigned fd_fd;
152	int ix;
153	struct mount *mp;
154	struct vnode **vpp;
155{
156	struct fdescmount *fmp;
157	struct fdhashhead *fc;
158	struct fdescnode *fd, *fd2;
159	struct vnode *vp, *vp2;
160	struct thread *td;
161	int error = 0;
162
163	td = curthread;
164	fc = FD_NHASH(ix);
165loop:
166	mtx_lock(&fdesc_hashmtx);
167	/*
168	 * If a forced unmount is progressing, we need to drop it. The flags are
169	 * protected by the hashmtx.
170	 */
171	fmp = (struct fdescmount *)mp->mnt_data;
172	if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
173		mtx_unlock(&fdesc_hashmtx);
174		return (-1);
175	}
176
177	LIST_FOREACH(fd, fc, fd_hash) {
178		if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
179			/* Get reference to vnode in case it's being free'd */
180			vp = fd->fd_vnode;
181			VI_LOCK(vp);
182			mtx_unlock(&fdesc_hashmtx);
183			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
184				goto loop;
185			*vpp = vp;
186			return (0);
187		}
188	}
189	mtx_unlock(&fdesc_hashmtx);
190
191	fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
192
193	error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
194	if (error) {
195		free(fd, M_TEMP);
196		return (error);
197	}
198	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
199	vp->v_data = fd;
200	fd->fd_vnode = vp;
201	fd->fd_type = ftype;
202	fd->fd_fd = fd_fd;
203	fd->fd_ix = ix;
204	error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
205	if (error != 0) {
206		*vpp = NULLVP;
207		return (error);
208	}
209
210	/* Make sure that someone didn't beat us when inserting the vnode. */
211	mtx_lock(&fdesc_hashmtx);
212	/*
213	 * If a forced unmount is progressing, we need to drop it. The flags are
214	 * protected by the hashmtx.
215	 */
216	fmp = (struct fdescmount *)mp->mnt_data;
217	if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
218		mtx_unlock(&fdesc_hashmtx);
219		vgone(vp);
220		vput(vp);
221		*vpp = NULLVP;
222		return (-1);
223	}
224
225	LIST_FOREACH(fd2, fc, fd_hash) {
226		if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
227			/* Get reference to vnode in case it's being free'd */
228			vp2 = fd2->fd_vnode;
229			VI_LOCK(vp2);
230			mtx_unlock(&fdesc_hashmtx);
231			error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
232			/* Someone beat us, dec use count and wait for reclaim */
233			vgone(vp);
234			vput(vp);
235			/* If we didn't get it, return no vnode. */
236			if (error)
237				vp2 = NULLVP;
238			*vpp = vp2;
239			return (error);
240		}
241	}
242
243	/* If we came here, we can insert it safely. */
244	LIST_INSERT_HEAD(fc, fd, fd_hash);
245	mtx_unlock(&fdesc_hashmtx);
246	*vpp = vp;
247	return (0);
248}
249
250/*
251 * vp is the current namei directory
252 * ndp is the name to locate in that directory...
253 */
254static int
255fdesc_lookup(ap)
256	struct vop_lookup_args /* {
257		struct vnode * a_dvp;
258		struct vnode ** a_vpp;
259		struct componentname * a_cnp;
260	} */ *ap;
261{
262	struct vnode **vpp = ap->a_vpp;
263	struct vnode *dvp = ap->a_dvp;
264	struct componentname *cnp = ap->a_cnp;
265	char *pname = cnp->cn_nameptr;
266	struct thread *td = cnp->cn_thread;
267	struct file *fp;
268	int nlen = cnp->cn_namelen;
269	u_int fd, fd1;
270	int error;
271	struct vnode *fvp;
272
273	if ((cnp->cn_flags & ISLASTCN) &&
274	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
275		error = EROFS;
276		goto bad;
277	}
278
279	if (cnp->cn_namelen == 1 && *pname == '.') {
280		*vpp = dvp;
281		VREF(dvp);
282		return (0);
283	}
284
285	if (VTOFDESC(dvp)->fd_type != Froot) {
286		error = ENOTDIR;
287		goto bad;
288	}
289
290	fd = 0;
291	/* the only time a leading 0 is acceptable is if it's "0" */
292	if (*pname == '0' && nlen != 1) {
293		error = ENOENT;
294		goto bad;
295	}
296	while (nlen--) {
297		if (*pname < '0' || *pname > '9') {
298			error = ENOENT;
299			goto bad;
300		}
301		fd1 = 10 * fd + *pname++ - '0';
302		if (fd1 < fd) {
303			error = ENOENT;
304			goto bad;
305		}
306		fd = fd1;
307	}
308
309	/*
310	 * No rights to check since 'fp' isn't actually used.
311	 */
312	if ((error = fget(td, fd, 0, &fp)) != 0)
313		goto bad;
314
315	/* Check if we're looking up ourselves. */
316	if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
317		/*
318		 * In case we're holding the last reference to the file, the dvp
319		 * will be re-acquired.
320		 */
321		vhold(dvp);
322		VOP_UNLOCK(dvp, 0);
323		fdrop(fp, td);
324
325		/* Re-aquire the lock afterwards. */
326		vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
327		vdrop(dvp);
328		fvp = dvp;
329	} else {
330		/*
331		 * Unlock our root node (dvp) when doing this, since we might
332		 * deadlock since the vnode might be locked by another thread
333		 * and the root vnode lock will be obtained afterwards (in case
334		 * we're looking up the fd of the root vnode), which will be the
335		 * opposite lock order. Vhold the root vnode first so we don't
336		 * lose it.
337		 */
338		vhold(dvp);
339		VOP_UNLOCK(dvp, 0);
340		error = fdesc_allocvp(Fdesc, fd, FD_DESC + fd, dvp->v_mount,
341		    &fvp);
342		fdrop(fp, td);
343		/*
344		 * The root vnode must be locked last to prevent deadlock condition.
345		 */
346		vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
347		vdrop(dvp);
348	}
349
350	if (error)
351		goto bad;
352	*vpp = fvp;
353	return (0);
354
355bad:
356	*vpp = NULL;
357	return (error);
358}
359
360static int
361fdesc_open(ap)
362	struct vop_open_args /* {
363		struct vnode *a_vp;
364		int  a_mode;
365		struct ucred *a_cred;
366		struct thread *a_td;
367	} */ *ap;
368{
369	struct vnode *vp = ap->a_vp;
370
371	if (VTOFDESC(vp)->fd_type == Froot)
372		return (0);
373
374	/*
375	 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
376	 * descriptor being sought for duplication. The error return ensures
377	 * that the vnode for this device will be released by vn_open. Open
378	 * will detect this special error and take the actions in dupfdopen.
379	 * Other callers of vn_open or VOP_OPEN will simply report the
380	 * error.
381	 */
382	ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
383	return (ENODEV);
384}
385
386static int
387fdesc_getattr(ap)
388	struct vop_getattr_args /* {
389		struct vnode *a_vp;
390		struct vattr *a_vap;
391		struct ucred *a_cred;
392	} */ *ap;
393{
394	struct vnode *vp = ap->a_vp;
395	struct vattr *vap = ap->a_vap;
396
397	vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
398	vap->va_fileid = VTOFDESC(vp)->fd_ix;
399	vap->va_uid = 0;
400	vap->va_gid = 0;
401	vap->va_blocksize = DEV_BSIZE;
402	vap->va_atime.tv_sec = boottime.tv_sec;
403	vap->va_atime.tv_nsec = 0;
404	vap->va_mtime = vap->va_atime;
405	vap->va_ctime = vap->va_mtime;
406	vap->va_gen = 0;
407	vap->va_flags = 0;
408	vap->va_bytes = 0;
409	vap->va_filerev = 0;
410
411	switch (VTOFDESC(vp)->fd_type) {
412	case Froot:
413		vap->va_type = VDIR;
414		vap->va_nlink = 2;
415		vap->va_size = DEV_BSIZE;
416		vap->va_rdev = NODEV;
417		break;
418
419	case Fdesc:
420		vap->va_type = VCHR;
421		vap->va_nlink = 1;
422		vap->va_size = 0;
423		vap->va_rdev = makedev(0, vap->va_fileid);
424		break;
425
426	default:
427		panic("fdesc_getattr");
428		break;
429	}
430
431	vp->v_type = vap->va_type;
432	return (0);
433}
434
435static int
436fdesc_setattr(ap)
437	struct vop_setattr_args /* {
438		struct vnode *a_vp;
439		struct vattr *a_vap;
440		struct ucred *a_cred;
441	} */ *ap;
442{
443	struct vattr *vap = ap->a_vap;
444	struct vnode *vp;
445	struct mount *mp;
446	struct file *fp;
447	struct thread *td = curthread;
448	unsigned fd;
449	int error;
450
451	/*
452	 * Can't mess with the root vnode
453	 */
454	if (VTOFDESC(ap->a_vp)->fd_type == Froot)
455		return (EACCES);
456
457	fd = VTOFDESC(ap->a_vp)->fd_fd;
458
459	/*
460	 * Allow setattr where there is an underlying vnode.
461	 */
462	error = getvnode(td->td_proc->p_fd, fd, CAP_EXTATTR_SET, &fp);
463	if (error) {
464		/*
465		 * getvnode() returns EINVAL if the file descriptor is not
466		 * backed by a vnode.  Silently drop all changes except
467		 * chflags(2) in this case.
468		 */
469		if (error == EINVAL) {
470			if (vap->va_flags != VNOVAL)
471				error = EOPNOTSUPP;
472			else
473				error = 0;
474		}
475		return (error);
476	}
477	vp = fp->f_vnode;
478	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
479		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
480		error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
481		VOP_UNLOCK(vp, 0);
482		vn_finished_write(mp);
483	}
484	fdrop(fp, td);
485	return (error);
486}
487
488#define UIO_MX 16
489
490static int
491fdesc_readdir(ap)
492	struct vop_readdir_args /* {
493		struct vnode *a_vp;
494		struct uio *a_uio;
495		struct ucred *a_cred;
496		int *a_eofflag;
497		u_long *a_cookies;
498		int a_ncookies;
499	} */ *ap;
500{
501	struct uio *uio = ap->a_uio;
502	struct filedesc *fdp;
503	struct dirent d;
504	struct dirent *dp = &d;
505	int error, i, off, fcnt;
506
507	if (VTOFDESC(ap->a_vp)->fd_type != Froot)
508		panic("fdesc_readdir: not dir");
509
510	if (ap->a_ncookies != NULL)
511		*ap->a_ncookies = 0;
512
513	off = (int)uio->uio_offset;
514	if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
515	    uio->uio_resid < UIO_MX)
516		return (EINVAL);
517	i = (u_int)off / UIO_MX;
518	fdp = uio->uio_td->td_proc->p_fd;
519	error = 0;
520
521	fcnt = i - 2;		/* The first two nodes are `.' and `..' */
522
523	FILEDESC_SLOCK(fdp);
524	while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
525		bzero((caddr_t)dp, UIO_MX);
526		switch (i) {
527		case 0:	/* `.' */
528		case 1: /* `..' */
529			dp->d_fileno = i + FD_ROOT;
530			dp->d_namlen = i + 1;
531			dp->d_reclen = UIO_MX;
532			bcopy("..", dp->d_name, dp->d_namlen);
533			dp->d_name[i + 1] = '\0';
534			dp->d_type = DT_DIR;
535			break;
536		default:
537			if (fdp->fd_ofiles[fcnt] == NULL)
538				break;
539			dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
540			dp->d_reclen = UIO_MX;
541			dp->d_type = DT_CHR;
542			dp->d_fileno = i + FD_DESC;
543			break;
544		}
545		if (dp->d_namlen != 0) {
546			/*
547			 * And ship to userland
548			 */
549			FILEDESC_SUNLOCK(fdp);
550			error = uiomove(dp, UIO_MX, uio);
551			if (error)
552				goto done;
553			FILEDESC_SLOCK(fdp);
554		}
555		i++;
556		fcnt++;
557	}
558	FILEDESC_SUNLOCK(fdp);
559
560done:
561	uio->uio_offset = i * UIO_MX;
562	return (error);
563}
564
565static int
566fdesc_reclaim(ap)
567	struct vop_reclaim_args /* {
568		struct vnode *a_vp;
569	} */ *ap;
570{
571	struct vnode *vp;
572	struct fdescnode *fd;
573
574 	vp = ap->a_vp;
575 	fd = VTOFDESC(vp);
576	fdesc_remove_entry(fd);
577	free(vp->v_data, M_TEMP);
578	vp->v_data = NULL;
579	return (0);
580}
581