xref: /freebsd-13-stable/sys/fs/fdescfs/fdesc_vnops.c (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
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_vnops.c	8.9 (Berkeley) 1/21/94
35  */
36 
37 /*
38  * /dev/fd Filesystem
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.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/syscallsubr.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
59 
60 #include <fs/fdescfs/fdesc.h>
61 
62 #define	NFDCACHE 4
63 #define FD_NHASH(ix) \
64 	(&fdhashtbl[(ix) & fdhash])
65 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
66 static u_long fdhash;
67 
68 struct mtx fdesc_hashmtx;
69 
70 static vop_getattr_t	fdesc_getattr;
71 static vop_lookup_t	fdesc_lookup;
72 static vop_open_t	fdesc_open;
73 static vop_pathconf_t	fdesc_pathconf;
74 static vop_readdir_t	fdesc_readdir;
75 static vop_readlink_t	fdesc_readlink;
76 static vop_reclaim_t	fdesc_reclaim;
77 static vop_setattr_t	fdesc_setattr;
78 
79 static struct vop_vector fdesc_vnodeops = {
80 	.vop_default =		&default_vnodeops,
81 
82 	.vop_access =		VOP_NULL,
83 	.vop_getattr =		fdesc_getattr,
84 	.vop_lookup =		fdesc_lookup,
85 	.vop_open =		fdesc_open,
86 	.vop_pathconf =		fdesc_pathconf,
87 	.vop_readdir =		fdesc_readdir,
88 	.vop_readlink =		fdesc_readlink,
89 	.vop_reclaim =		fdesc_reclaim,
90 	.vop_setattr =		fdesc_setattr,
91 };
92 VFS_VOP_VECTOR_REGISTER(fdesc_vnodeops);
93 
94 static void fdesc_insmntque_dtr(struct vnode *, void *);
95 static void fdesc_remove_entry(struct fdescnode *);
96 
97 /*
98  * Initialise cache headers
99  */
100 int
fdesc_init(struct vfsconf * vfsp)101 fdesc_init(struct vfsconf *vfsp)
102 {
103 
104 	mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
105 	fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
106 	return (0);
107 }
108 
109 /*
110  * Uninit ready for unload.
111  */
112 int
fdesc_uninit(struct vfsconf * vfsp)113 fdesc_uninit(struct vfsconf *vfsp)
114 {
115 
116 	hashdestroy(fdhashtbl, M_CACHE, fdhash);
117 	mtx_destroy(&fdesc_hashmtx);
118 	return (0);
119 }
120 
121 /*
122  * If allocating vnode fails, call this.
123  */
124 static void
fdesc_insmntque_dtr(struct vnode * vp,void * arg)125 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
126 {
127 
128 	vgone(vp);
129 	vput(vp);
130 }
131 
132 /*
133  * Remove an entry from the hash if it exists.
134  */
135 static void
fdesc_remove_entry(struct fdescnode * fd)136 fdesc_remove_entry(struct fdescnode *fd)
137 {
138 	struct fdhashhead *fc;
139 	struct fdescnode *fd2;
140 
141 	fc = FD_NHASH(fd->fd_ix);
142 	mtx_lock(&fdesc_hashmtx);
143 	LIST_FOREACH(fd2, fc, fd_hash) {
144 		if (fd == fd2) {
145 			LIST_REMOVE(fd, fd_hash);
146 			break;
147 		}
148 	}
149 	mtx_unlock(&fdesc_hashmtx);
150 }
151 
152 int
fdesc_allocvp(fdntype ftype,unsigned fd_fd,int ix,struct mount * mp,struct vnode ** vpp)153 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, 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 	enum vgetstate vgs;
162 	int error;
163 
164 	td = curthread;
165 	fc = FD_NHASH(ix);
166 loop:
167 	mtx_lock(&fdesc_hashmtx);
168 	/*
169 	 * If a forced unmount is progressing, we need to drop it. The flags are
170 	 * protected by the hashmtx.
171 	 */
172 	fmp = mp->mnt_data;
173 	if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
174 		mtx_unlock(&fdesc_hashmtx);
175 		return (-1);
176 	}
177 
178 	LIST_FOREACH(fd, fc, fd_hash) {
179 		if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
180 			/* Get reference to vnode in case it's being free'd */
181 			vp = fd->fd_vnode;
182 			vgs = vget_prep(vp);
183 			mtx_unlock(&fdesc_hashmtx);
184 			if (vget_finish(vp, LK_EXCLUSIVE, vgs) != 0)
185 				goto loop;
186 			*vpp = vp;
187 			return (0);
188 		}
189 	}
190 	mtx_unlock(&fdesc_hashmtx);
191 
192 	fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
193 
194 	error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
195 	if (error) {
196 		free(fd, M_TEMP);
197 		return (error);
198 	}
199 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
200 	vp->v_data = fd;
201 	fd->fd_vnode = vp;
202 	fd->fd_type = ftype;
203 	fd->fd_fd = fd_fd;
204 	fd->fd_ix = ix;
205 	if (ftype == Fdesc) {
206 		if ((fmp->flags & FMNT_RDLNKF) != 0)
207 			vp->v_type = VLNK;
208 		else if ((fmp->flags & FMNT_LINRDLNKF) != 0)
209 			vp->v_vflag |= VV_READLINK;
210 	}
211 	error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
212 	if (error != 0) {
213 		*vpp = NULLVP;
214 		return (error);
215 	}
216 
217 	/* Make sure that someone didn't beat us when inserting the vnode. */
218 	mtx_lock(&fdesc_hashmtx);
219 	/*
220 	 * If a forced unmount is progressing, we need to drop it. The flags are
221 	 * protected by the hashmtx.
222 	 */
223 	fmp = mp->mnt_data;
224 	if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
225 		mtx_unlock(&fdesc_hashmtx);
226 		vgone(vp);
227 		vput(vp);
228 		*vpp = NULLVP;
229 		return (-1);
230 	}
231 
232 	LIST_FOREACH(fd2, fc, fd_hash) {
233 		if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
234 			/* Get reference to vnode in case it's being free'd */
235 			vp2 = fd2->fd_vnode;
236 			vgs = vget_prep(vp2);
237 			mtx_unlock(&fdesc_hashmtx);
238 			error = vget_finish(vp2, LK_EXCLUSIVE, vgs);
239 			/* Someone beat us, dec use count and wait for reclaim */
240 			vgone(vp);
241 			vput(vp);
242 			/* If we didn't get it, return no vnode. */
243 			if (error)
244 				vp2 = NULLVP;
245 			*vpp = vp2;
246 			return (error);
247 		}
248 	}
249 
250 	/* If we came here, we can insert it safely. */
251 	LIST_INSERT_HEAD(fc, fd, fd_hash);
252 	mtx_unlock(&fdesc_hashmtx);
253 	*vpp = vp;
254 	return (0);
255 }
256 
257 struct fdesc_get_ino_args {
258 	fdntype ftype;
259 	unsigned fd_fd;
260 	int ix;
261 	struct file *fp;
262 	struct thread *td;
263 	bool fdropped;
264 };
265 
266 static int
fdesc_get_ino_alloc(struct mount * mp,void * arg,int lkflags,struct vnode ** rvp)267 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
268     struct vnode **rvp)
269 {
270 	struct fdesc_get_ino_args *a;
271 	struct fdescmount *fdm;
272 	struct vnode *vp;
273 	int error;
274 
275 	a = arg;
276 	fdm = VFSTOFDESC(mp);
277 	if ((fdm->flags & FMNT_NODUP) != 0 && a->fp->f_type == DTYPE_VNODE) {
278 		vp = a->fp->f_vnode;
279 		vget(vp, lkflags | LK_RETRY);
280 		*rvp = vp;
281 		error = 0;
282 	} else {
283 		error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
284 	}
285 	fdrop(a->fp, a->td);
286 	a->fdropped = true;
287 	return (error);
288 }
289 
290 /*
291  * vp is the current namei directory
292  * ndp is the name to locate in that directory...
293  */
294 static int
fdesc_lookup(struct vop_lookup_args * ap)295 fdesc_lookup(struct vop_lookup_args *ap)
296 {
297 	struct vnode **vpp = ap->a_vpp;
298 	struct vnode *dvp = ap->a_dvp;
299 	struct componentname *cnp = ap->a_cnp;
300 	char *pname = cnp->cn_nameptr;
301 	struct thread *td = cnp->cn_thread;
302 	struct file *fp;
303 	struct fdesc_get_ino_args arg;
304 	int nlen = cnp->cn_namelen;
305 	u_int fd, fd1;
306 	int error;
307 	struct vnode *fvp;
308 
309 	if ((cnp->cn_flags & ISLASTCN) &&
310 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
311 		error = EROFS;
312 		goto bad;
313 	}
314 
315 	if (cnp->cn_namelen == 1 && *pname == '.') {
316 		*vpp = dvp;
317 		VREF(dvp);
318 		return (0);
319 	}
320 
321 	if (VTOFDESC(dvp)->fd_type != Froot) {
322 		error = ENOTDIR;
323 		goto bad;
324 	}
325 
326 	fd = 0;
327 	/* the only time a leading 0 is acceptable is if it's "0" */
328 	if (*pname == '0' && nlen != 1) {
329 		error = ENOENT;
330 		goto bad;
331 	}
332 	while (nlen--) {
333 		if (*pname < '0' || *pname > '9') {
334 			error = ENOENT;
335 			goto bad;
336 		}
337 		fd1 = 10 * fd + *pname++ - '0';
338 		if (fd1 < fd) {
339 			error = ENOENT;
340 			goto bad;
341 		}
342 		fd = fd1;
343 	}
344 
345 	/*
346 	 * No rights to check since 'fp' isn't actually used.
347 	 */
348 	if ((error = fget(td, fd, &cap_no_rights, &fp)) != 0)
349 		goto bad;
350 
351 	/*
352 	 * Make sure we do not deadlock looking up the dvp itself.
353 	 *
354 	 * Unlock our root node (dvp) when doing this, since we might
355 	 * deadlock since the vnode might be locked by another thread
356 	 * and the root vnode lock will be obtained afterwards (in case
357 	 * we're looking up the fd of the root vnode), which will be the
358 	 * opposite lock order.
359 	 */
360 	arg.ftype = Fdesc;
361 	arg.fd_fd = fd;
362 	arg.ix = FD_DESC + fd;
363 	arg.fp = fp;
364 	arg.td = td;
365 	arg.fdropped = false;
366 	error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
367 	    LK_EXCLUSIVE, &fvp);
368 
369 	if (!arg.fdropped) {
370 		/*
371 		 * In case we're holding the last reference to the file, the dvp
372 		 * will be re-acquired.
373 		 */
374 		VOP_UNLOCK(dvp);
375 		fdrop(fp, td);
376 
377 		vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
378 		fvp = dvp;
379 		if (error == 0 && VN_IS_DOOMED(dvp))
380 			error = ENOENT;
381 	}
382 
383 	if (error)
384 		goto bad;
385 	*vpp = fvp;
386 	return (0);
387 
388 bad:
389 	*vpp = NULL;
390 	return (error);
391 }
392 
393 static int
fdesc_open(struct vop_open_args * ap)394 fdesc_open(struct vop_open_args *ap)
395 {
396 	struct vnode *vp = ap->a_vp;
397 
398 	if (VTOFDESC(vp)->fd_type == Froot)
399 		return (0);
400 
401 	/*
402 	 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
403 	 * descriptor being sought for duplication. The error return ensures
404 	 * that the vnode for this device will be released by vn_open. Open
405 	 * will detect this special error and take the actions in dupfdopen.
406 	 * Other callers of vn_open or VOP_OPEN will simply report the
407 	 * error.
408 	 */
409 	ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
410 	return (ENODEV);
411 }
412 
413 static int
fdesc_pathconf(struct vop_pathconf_args * ap)414 fdesc_pathconf(struct vop_pathconf_args *ap)
415 {
416 	struct vnode *vp = ap->a_vp;
417 	int error;
418 
419 	switch (ap->a_name) {
420 	case _PC_NAME_MAX:
421 		*ap->a_retval = NAME_MAX;
422 		return (0);
423 	case _PC_LINK_MAX:
424 		if (VTOFDESC(vp)->fd_type == Froot)
425 			*ap->a_retval = 2;
426 		else
427 			*ap->a_retval = 1;
428 		return (0);
429 	default:
430 		if (VTOFDESC(vp)->fd_type == Froot)
431 			return (vop_stdpathconf(ap));
432 		vref(vp);
433 		VOP_UNLOCK(vp);
434 		error = kern_fpathconf(curthread, VTOFDESC(vp)->fd_fd,
435 		    ap->a_name, ap->a_retval);
436 		vn_lock(vp, LK_SHARED | LK_RETRY);
437 		vunref(vp);
438 		return (error);
439 	}
440 }
441 
442 static int
fdesc_getattr(struct vop_getattr_args * ap)443 fdesc_getattr(struct vop_getattr_args *ap)
444 {
445 	struct vnode *vp = ap->a_vp;
446 	struct vattr *vap = ap->a_vap;
447 	struct timeval boottime;
448 
449 	getboottime(&boottime);
450 	vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
451 	vap->va_fileid = VTOFDESC(vp)->fd_ix;
452 	vap->va_uid = 0;
453 	vap->va_gid = 0;
454 	vap->va_blocksize = DEV_BSIZE;
455 	vap->va_atime.tv_sec = boottime.tv_sec;
456 	vap->va_atime.tv_nsec = 0;
457 	vap->va_mtime = vap->va_atime;
458 	vap->va_ctime = vap->va_mtime;
459 	vap->va_gen = 0;
460 	vap->va_flags = 0;
461 	vap->va_bytes = 0;
462 	vap->va_filerev = 0;
463 
464 	switch (VTOFDESC(vp)->fd_type) {
465 	case Froot:
466 		vap->va_type = VDIR;
467 		vap->va_nlink = 2;
468 		vap->va_size = DEV_BSIZE;
469 		vap->va_rdev = NODEV;
470 		break;
471 
472 	case Fdesc:
473 		vap->va_type = (VFSTOFDESC(vp->v_mount)->flags &
474 		    (FMNT_RDLNKF | FMNT_LINRDLNKF)) == 0 ? VCHR : VLNK;
475 		vap->va_nlink = 1;
476 		vap->va_size = 0;
477 		vap->va_rdev = makedev(0, vap->va_fileid);
478 		break;
479 
480 	default:
481 		panic("fdesc_getattr");
482 		break;
483 	}
484 
485 	vp->v_type = vap->va_type;
486 	return (0);
487 }
488 
489 static int
fdesc_setattr(struct vop_setattr_args * ap)490 fdesc_setattr(struct vop_setattr_args *ap)
491 {
492 	struct vattr *vap = ap->a_vap;
493 	struct vnode *vp;
494 	struct mount *mp;
495 	struct file *fp;
496 	struct thread *td = curthread;
497 	cap_rights_t rights;
498 	unsigned fd;
499 	int error;
500 
501 	/*
502 	 * Can't mess with the root vnode
503 	 */
504 	if (VTOFDESC(ap->a_vp)->fd_type == Froot)
505 		return (EACCES);
506 
507 	fd = VTOFDESC(ap->a_vp)->fd_fd;
508 
509 	/*
510 	 * Allow setattr where there is an underlying vnode.
511 	 * For O_PATH descriptors, disallow truncate.
512 	 */
513 	if (vap->va_size != VNOVAL) {
514 		error = getvnode(td, fd,
515 		    cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
516 	} else {
517 		error = getvnode_path(td, fd,
518 		    cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
519 	}
520 	if (error) {
521 		/*
522 		 * getvnode() returns EINVAL if the file descriptor is not
523 		 * backed by a vnode.  Silently drop all changes except
524 		 * chflags(2) in this case.
525 		 */
526 		if (error == EINVAL) {
527 			if (vap->va_flags != VNOVAL)
528 				error = EOPNOTSUPP;
529 			else
530 				error = 0;
531 		}
532 		return (error);
533 	}
534 	vp = fp->f_vnode;
535 	if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
536 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
537 		error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
538 		VOP_UNLOCK(vp);
539 		vn_finished_write(mp);
540 	}
541 	fdrop(fp, td);
542 	return (error);
543 }
544 
545 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
546 
547 static int
fdesc_readdir(struct vop_readdir_args * ap)548 fdesc_readdir(struct vop_readdir_args *ap)
549 {
550 	struct fdescmount *fmp;
551 	struct uio *uio = ap->a_uio;
552 	struct filedesc *fdp;
553 	struct dirent d;
554 	struct dirent *dp = &d;
555 	int error, i, off, fcnt;
556 
557 	if (VTOFDESC(ap->a_vp)->fd_type != Froot)
558 		panic("fdesc_readdir: not dir");
559 
560 	fmp = VFSTOFDESC(ap->a_vp->v_mount);
561 	if (ap->a_ncookies != NULL)
562 		*ap->a_ncookies = 0;
563 
564 	off = (int)uio->uio_offset;
565 	if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
566 	    uio->uio_resid < UIO_MX)
567 		return (EINVAL);
568 	i = (u_int)off / UIO_MX;
569 	fdp = uio->uio_td->td_proc->p_fd;
570 	error = 0;
571 
572 	fcnt = i - 2;		/* The first two nodes are `.' and `..' */
573 
574 	FILEDESC_SLOCK(fdp);
575 	while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
576 		bzero((caddr_t)dp, UIO_MX);
577 		switch (i) {
578 		case 0:	/* `.' */
579 		case 1: /* `..' */
580 			dp->d_fileno = i + FD_ROOT;
581 			dp->d_namlen = i + 1;
582 			dp->d_reclen = UIO_MX;
583 			bcopy("..", dp->d_name, dp->d_namlen);
584 			dp->d_type = DT_DIR;
585 			dirent_terminate(dp);
586 			break;
587 		default:
588 			if (fdp->fd_ofiles[fcnt].fde_file == NULL)
589 				break;
590 			dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
591 			dp->d_reclen = UIO_MX;
592 			dp->d_type = (fmp->flags & (FMNT_RDLNKF |
593 			    FMNT_LINRDLNKF)) == 0 ? DT_CHR : DT_LNK;
594 			dp->d_fileno = i + FD_DESC;
595 			dirent_terminate(dp);
596 			break;
597 		}
598 		/* NOTE: d_off is the offset of the *next* entry. */
599 		dp->d_off = UIO_MX * (i + 1);
600 		if (dp->d_namlen != 0) {
601 			/*
602 			 * And ship to userland
603 			 */
604 			FILEDESC_SUNLOCK(fdp);
605 			error = uiomove(dp, UIO_MX, uio);
606 			if (error)
607 				goto done;
608 			FILEDESC_SLOCK(fdp);
609 		}
610 		i++;
611 		fcnt++;
612 	}
613 	FILEDESC_SUNLOCK(fdp);
614 
615 done:
616 	uio->uio_offset = i * UIO_MX;
617 	return (error);
618 }
619 
620 static int
fdesc_reclaim(struct vop_reclaim_args * ap)621 fdesc_reclaim(struct vop_reclaim_args *ap)
622 {
623 	struct vnode *vp;
624 	struct fdescnode *fd;
625 
626  	vp = ap->a_vp;
627  	fd = VTOFDESC(vp);
628 	fdesc_remove_entry(fd);
629 	free(vp->v_data, M_TEMP);
630 	vp->v_data = NULL;
631 	return (0);
632 }
633 
634 static int
fdesc_readlink(struct vop_readlink_args * va)635 fdesc_readlink(struct vop_readlink_args *va)
636 {
637 	struct vnode *vp, *vn;
638 	struct thread *td;
639 	struct uio *uio;
640 	struct file *fp;
641 	char *freepath, *fullpath;
642 	size_t pathlen;
643 	int lockflags, fd_fd;
644 	int error;
645 
646 	freepath = NULL;
647 	vn = va->a_vp;
648 	if (VTOFDESC(vn)->fd_type != Fdesc)
649 		panic("fdesc_readlink: not fdescfs link");
650 	fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
651 	lockflags = VOP_ISLOCKED(vn);
652 	VOP_UNLOCK(vn);
653 
654 	td = curthread;
655 	error = fget_cap(td, fd_fd, &cap_no_rights, &fp, NULL);
656 	if (error != 0)
657 		goto out;
658 
659 	switch (fp->f_type) {
660 	case DTYPE_VNODE:
661 		vp = fp->f_vnode;
662 		error = vn_fullpath(vp, &fullpath, &freepath);
663 		break;
664 	default:
665 		fullpath = "anon_inode:[unknown]";
666 		break;
667 	}
668 	if (error == 0) {
669 		uio = va->a_uio;
670 		pathlen = strlen(fullpath);
671 		error = uiomove(fullpath, pathlen, uio);
672 	}
673 	if (freepath != NULL)
674 		free(freepath, M_TEMP);
675 	fdrop(fp, td);
676 
677 out:
678 	vn_lock(vn, lockflags | LK_RETRY);
679 	return (error);
680 }
681