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