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. 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/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
93 static void fdesc_insmntque_dtr(struct vnode *, void *);
94 static void fdesc_remove_entry(struct fdescnode *);
95
96 /*
97 * Initialise cache headers
98 */
99 int
100 fdesc_init(struct vfsconf *vfsp)
101 {
102
103 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
104 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
105 return (0);
106 }
107
108 /*
109 * Uninit ready for unload.
110 */
111 int
112 fdesc_uninit(struct vfsconf *vfsp)
113 {
114
115 hashdestroy(fdhashtbl, M_CACHE, fdhash);
116 mtx_destroy(&fdesc_hashmtx);
117 return (0);
118 }
119
120 /*
121 * If allocating vnode fails, call this.
122 */
123 static void
124 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
125 {
126
127 vgone(vp);
128 vput(vp);
129 }
130
131 /*
132 * Remove an entry from the hash if it exists.
133 */
134 static void
135 fdesc_remove_entry(struct fdescnode *fd)
136 {
137 struct fdhashhead *fc;
138 struct fdescnode *fd2;
139
140 fc = FD_NHASH(fd->fd_ix);
141 mtx_lock(&fdesc_hashmtx);
142 LIST_FOREACH(fd2, fc, fd_hash) {
143 if (fd == fd2) {
144 LIST_REMOVE(fd, fd_hash);
145 break;
146 }
147 }
148 mtx_unlock(&fdesc_hashmtx);
149 }
150
151 int
152 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
153 struct vnode **vpp)
154 {
155 struct fdescmount *fmp;
156 struct fdhashhead *fc;
157 struct fdescnode *fd, *fd2;
158 struct vnode *vp, *vp2;
159 struct thread *td;
160 int error;
161
162 td = curthread;
163 fc = FD_NHASH(ix);
164 loop:
165 mtx_lock(&fdesc_hashmtx);
166 /*
167 * If a forced unmount is progressing, we need to drop it. The flags are
168 * protected by the hashmtx.
169 */
170 fmp = mp->mnt_data;
171 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
172 mtx_unlock(&fdesc_hashmtx);
173 return (-1);
174 }
175
176 LIST_FOREACH(fd, fc, fd_hash) {
177 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
178 /* Get reference to vnode in case it's being free'd */
179 vp = fd->fd_vnode;
180 VI_LOCK(vp);
181 mtx_unlock(&fdesc_hashmtx);
182 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
183 goto loop;
184 *vpp = vp;
185 return (0);
186 }
187 }
188 mtx_unlock(&fdesc_hashmtx);
189
190 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
191
192 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
193 if (error) {
194 free(fd, M_TEMP);
195 return (error);
196 }
197 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
198 vp->v_data = fd;
199 fd->fd_vnode = vp;
200 fd->fd_type = ftype;
201 fd->fd_fd = fd_fd;
202 fd->fd_ix = ix;
203 if (ftype == Fdesc && fmp->flags & FMNT_LINRDLNKF)
204 vp->v_vflag |= VV_READLINK;
205 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
206 if (error != 0) {
207 *vpp = NULLVP;
208 return (error);
209 }
210
211 /* Make sure that someone didn't beat us when inserting the vnode. */
212 mtx_lock(&fdesc_hashmtx);
213 /*
214 * If a forced unmount is progressing, we need to drop it. The flags are
215 * protected by the hashmtx.
216 */
217 fmp = mp->mnt_data;
218 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
219 mtx_unlock(&fdesc_hashmtx);
220 vgone(vp);
221 vput(vp);
222 *vpp = NULLVP;
223 return (-1);
224 }
225
226 LIST_FOREACH(fd2, fc, fd_hash) {
227 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
228 /* Get reference to vnode in case it's being free'd */
229 vp2 = fd2->fd_vnode;
230 VI_LOCK(vp2);
231 mtx_unlock(&fdesc_hashmtx);
232 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
233 /* Someone beat us, dec use count and wait for reclaim */
234 vgone(vp);
235 vput(vp);
236 /* If we didn't get it, return no vnode. */
237 if (error)
238 vp2 = NULLVP;
239 *vpp = vp2;
240 return (error);
241 }
242 }
243
244 /* If we came here, we can insert it safely. */
245 LIST_INSERT_HEAD(fc, fd, fd_hash);
246 mtx_unlock(&fdesc_hashmtx);
247 *vpp = vp;
248 return (0);
249 }
250
251 struct fdesc_get_ino_args {
252 fdntype ftype;
253 unsigned fd_fd;
254 int ix;
255 struct file *fp;
256 struct thread *td;
257 };
258
259 static int
260 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
261 struct vnode **rvp)
262 {
263 struct fdesc_get_ino_args *a;
264 int error;
265
266 a = arg;
267 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
268 fdrop(a->fp, a->td);
269 return (error);
270 }
271
272
273 /*
274 * vp is the current namei directory
275 * ndp is the name to locate in that directory...
276 */
277 static int
278 fdesc_lookup(struct vop_lookup_args *ap)
279 {
280 struct vnode **vpp = ap->a_vpp;
281 struct vnode *dvp = ap->a_dvp;
282 struct componentname *cnp = ap->a_cnp;
283 char *pname = cnp->cn_nameptr;
284 struct thread *td = cnp->cn_thread;
285 struct file *fp;
286 struct fdesc_get_ino_args arg;
287 cap_rights_t rights;
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_rights_init(&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, 0);
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 ((dvp->v_iflag & VI_DOOMED) != 0)
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, 0);
420 error = kern_fpathconf(curthread, VTOFDESC(vp)->fd_fd,
421 ap->a_name);
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 */
497 error = getvnode(td, fd,
498 cap_rights_init(&rights, CAP_EXTATTR_SET), &fp);
499 if (error) {
500 /*
501 * getvnode() returns EINVAL if the file descriptor is not
502 * backed by a vnode. Silently drop all changes except
503 * chflags(2) in this case.
504 */
505 if (error == EINVAL) {
506 if (vap->va_flags != VNOVAL)
507 error = EOPNOTSUPP;
508 else
509 error = 0;
510 }
511 return (error);
512 }
513 vp = fp->f_vnode;
514 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
515 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
516 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
517 VOP_UNLOCK(vp, 0);
518 vn_finished_write(mp);
519 }
520 fdrop(fp, td);
521 return (error);
522 }
523
524 #define UIO_MX 16
525
526 static int
527 fdesc_readdir(struct vop_readdir_args *ap)
528 {
529 struct fdescmount *fmp;
530 struct uio *uio = ap->a_uio;
531 struct filedesc *fdp;
532 struct dirent d;
533 struct dirent *dp = &d;
534 int error, i, off, fcnt;
535
536 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
537 panic("fdesc_readdir: not dir");
538
539 fmp = VFSTOFDESC(ap->a_vp->v_mount);
540 if (ap->a_ncookies != NULL)
541 *ap->a_ncookies = 0;
542
543 off = (int)uio->uio_offset;
544 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
545 uio->uio_resid < UIO_MX)
546 return (EINVAL);
547 i = (u_int)off / UIO_MX;
548 fdp = uio->uio_td->td_proc->p_fd;
549 error = 0;
550
551 fcnt = i - 2; /* The first two nodes are `.' and `..' */
552
553 FILEDESC_SLOCK(fdp);
554 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
555 bzero((caddr_t)dp, UIO_MX);
556 switch (i) {
557 case 0: /* `.' */
558 case 1: /* `..' */
559 dp->d_fileno = i + FD_ROOT;
560 dp->d_namlen = i + 1;
561 dp->d_reclen = UIO_MX;
562 bcopy("..", dp->d_name, dp->d_namlen);
563 dp->d_type = DT_DIR;
564 dirent_terminate(dp);
565 break;
566 default:
567 if (fdp->fd_ofiles[fcnt].fde_file == NULL)
568 break;
569 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
570 dp->d_reclen = UIO_MX;
571 dp->d_type = (fmp->flags & FMNT_LINRDLNKF) == 0 ?
572 DT_CHR : DT_LNK;
573 dp->d_fileno = i + FD_DESC;
574 dirent_terminate(dp);
575 break;
576 }
577 if (dp->d_namlen != 0) {
578 /*
579 * And ship to userland
580 */
581 FILEDESC_SUNLOCK(fdp);
582 error = uiomove(dp, UIO_MX, uio);
583 if (error)
584 goto done;
585 FILEDESC_SLOCK(fdp);
586 }
587 i++;
588 fcnt++;
589 }
590 FILEDESC_SUNLOCK(fdp);
591
592 done:
593 uio->uio_offset = i * UIO_MX;
594 return (error);
595 }
596
597 static int
598 fdesc_reclaim(struct vop_reclaim_args *ap)
599 {
600 struct vnode *vp;
601 struct fdescnode *fd;
602
603 vp = ap->a_vp;
604 fd = VTOFDESC(vp);
605 fdesc_remove_entry(fd);
606 free(vp->v_data, M_TEMP);
607 vp->v_data = NULL;
608 return (0);
609 }
610
611 static int
612 fdesc_readlink(struct vop_readlink_args *va)
613 {
614 struct vnode *vp, *vn;
615 cap_rights_t rights;
616 struct thread *td;
617 struct uio *uio;
618 struct file *fp;
619 char *freepath, *fullpath;
620 size_t pathlen;
621 int lockflags, fd_fd;
622 int error;
623
624 freepath = NULL;
625 vn = va->a_vp;
626 if (VTOFDESC(vn)->fd_type != Fdesc)
627 panic("fdesc_readlink: not fdescfs link");
628 fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
629 lockflags = VOP_ISLOCKED(vn);
630 VOP_UNLOCK(vn, 0);
631
632 td = curthread;
633 error = fget_cap(td, fd_fd, cap_rights_init(&rights), &fp, NULL);
634 if (error != 0)
635 goto out;
636
637 switch (fp->f_type) {
638 case DTYPE_VNODE:
639 vp = fp->f_vnode;
640 error = vn_fullpath(td, vp, &fullpath, &freepath);
641 break;
642 default:
643 fullpath = "anon_inode:[unknown]";
644 break;
645 }
646 if (error == 0) {
647 uio = va->a_uio;
648 pathlen = strlen(fullpath);
649 error = uiomove(fullpath, pathlen, uio);
650 }
651 if (freepath != NULL)
652 free(freepath, M_TEMP);
653 fdrop(fp, td);
654
655 out:
656 vn_lock(vn, lockflags | LK_RETRY);
657 return (error);
658 }
Cache object: c8ec825749a4d66e7fc9965555a2d330
|