2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.h>
45 #include <sys/dirent.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h> /* boottime */
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>
56 #include <sys/vnode.h>
58 #include <fs/fdescfs/fdesc.h>
61 #define FD_NHASH(ix) \
62 (&fdhashtbl[(ix) & fdhash])
63 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
66 struct mtx fdesc_hashmtx;
68 static vop_getattr_t fdesc_getattr;
69 static vop_lookup_t fdesc_lookup;
70 static vop_open_t fdesc_open;
71 static vop_readdir_t fdesc_readdir;
72 static vop_reclaim_t fdesc_reclaim;
73 static vop_setattr_t fdesc_setattr;
75 static struct vop_vector fdesc_vnodeops = {
76 .vop_default = &default_vnodeops,
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,
88 static void fdesc_insmntque_dtr(struct vnode *, void *);
89 static void fdesc_remove_entry(struct fdescnode *);
92 * Initialise cache headers
95 fdesc_init(struct vfsconf *vfsp)
98 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
99 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
104 * Uninit ready for unload.
107 fdesc_uninit(struct vfsconf *vfsp)
110 hashdestroy(fdhashtbl, M_CACHE, fdhash);
111 mtx_destroy(&fdesc_hashmtx);
116 * If allocating vnode fails, call this.
119 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
127 * Remove an entry from the hash if it exists.
130 fdesc_remove_entry(struct fdescnode *fd)
132 struct fdhashhead *fc;
133 struct fdescnode *fd2;
135 fc = FD_NHASH(fd->fd_ix);
136 mtx_lock(&fdesc_hashmtx);
137 LIST_FOREACH(fd2, fc, fd_hash) {
139 LIST_REMOVE(fd, fd_hash);
143 mtx_unlock(&fdesc_hashmtx);
147 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
150 struct fdescmount *fmp;
151 struct fdhashhead *fc;
152 struct fdescnode *fd, *fd2;
153 struct vnode *vp, *vp2;
160 mtx_lock(&fdesc_hashmtx);
162 * If a forced unmount is progressing, we need to drop it. The flags are
163 * protected by the hashmtx.
166 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
167 mtx_unlock(&fdesc_hashmtx);
171 LIST_FOREACH(fd, fc, fd_hash) {
172 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
173 /* Get reference to vnode in case it's being free'd */
176 mtx_unlock(&fdesc_hashmtx);
177 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
183 mtx_unlock(&fdesc_hashmtx);
185 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
187 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
192 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
198 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
204 /* Make sure that someone didn't beat us when inserting the vnode. */
205 mtx_lock(&fdesc_hashmtx);
207 * If a forced unmount is progressing, we need to drop it. The flags are
208 * protected by the hashmtx.
211 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
212 mtx_unlock(&fdesc_hashmtx);
219 LIST_FOREACH(fd2, fc, fd_hash) {
220 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
221 /* Get reference to vnode in case it's being free'd */
224 mtx_unlock(&fdesc_hashmtx);
225 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
226 /* Someone beat us, dec use count and wait for reclaim */
229 /* If we didn't get it, return no vnode. */
237 /* If we came here, we can insert it safely. */
238 LIST_INSERT_HEAD(fc, fd, fd_hash);
239 mtx_unlock(&fdesc_hashmtx);
244 struct fdesc_get_ino_args {
253 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
256 struct fdesc_get_ino_args *a;
260 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
267 * vp is the current namei directory
268 * ndp is the name to locate in that directory...
271 fdesc_lookup(struct vop_lookup_args *ap)
273 struct vnode **vpp = ap->a_vpp;
274 struct vnode *dvp = ap->a_dvp;
275 struct componentname *cnp = ap->a_cnp;
276 char *pname = cnp->cn_nameptr;
277 struct thread *td = cnp->cn_thread;
279 struct fdesc_get_ino_args arg;
281 int nlen = cnp->cn_namelen;
286 if ((cnp->cn_flags & ISLASTCN) &&
287 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
292 if (cnp->cn_namelen == 1 && *pname == '.') {
298 if (VTOFDESC(dvp)->fd_type != Froot) {
304 /* the only time a leading 0 is acceptable is if it's "0" */
305 if (*pname == '0' && nlen != 1) {
310 if (*pname < '0' || *pname > '9') {
314 fd1 = 10 * fd + *pname++ - '0';
323 * No rights to check since 'fp' isn't actually used.
325 if ((error = fget(td, fd, cap_rights_init(&rights), &fp)) != 0)
328 /* Check if we're looking up ourselves. */
329 if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
331 * In case we're holding the last reference to the file, the dvp
332 * will be re-acquired.
338 /* Re-aquire the lock afterwards. */
339 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
342 if ((dvp->v_iflag & VI_DOOMED) != 0)
346 * Unlock our root node (dvp) when doing this, since we might
347 * deadlock since the vnode might be locked by another thread
348 * and the root vnode lock will be obtained afterwards (in case
349 * we're looking up the fd of the root vnode), which will be the
350 * opposite lock order. Vhold the root vnode first so we don't
355 arg.ix = FD_DESC + fd;
358 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
373 fdesc_open(struct vop_open_args *ap)
375 struct vnode *vp = ap->a_vp;
377 if (VTOFDESC(vp)->fd_type == Froot)
381 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
382 * descriptor being sought for duplication. The error return ensures
383 * that the vnode for this device will be released by vn_open. Open
384 * will detect this special error and take the actions in dupfdopen.
385 * Other callers of vn_open or VOP_OPEN will simply report the
388 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
393 fdesc_getattr(struct vop_getattr_args *ap)
395 struct vnode *vp = ap->a_vp;
396 struct vattr *vap = ap->a_vap;
397 struct timeval boottime;
399 getboottime(&boottime);
400 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
401 vap->va_fileid = VTOFDESC(vp)->fd_ix;
404 vap->va_blocksize = DEV_BSIZE;
405 vap->va_atime.tv_sec = boottime.tv_sec;
406 vap->va_atime.tv_nsec = 0;
407 vap->va_mtime = vap->va_atime;
408 vap->va_ctime = vap->va_mtime;
414 switch (VTOFDESC(vp)->fd_type) {
418 vap->va_size = DEV_BSIZE;
419 vap->va_rdev = NODEV;
426 vap->va_rdev = makedev(0, vap->va_fileid);
430 panic("fdesc_getattr");
434 vp->v_type = vap->va_type;
439 fdesc_setattr(struct vop_setattr_args *ap)
441 struct vattr *vap = ap->a_vap;
445 struct thread *td = curthread;
451 * Can't mess with the root vnode
453 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
456 fd = VTOFDESC(ap->a_vp)->fd_fd;
459 * Allow setattr where there is an underlying vnode.
461 error = getvnode(td, fd,
462 cap_rights_init(&rights, CAP_EXTATTR_SET), &fp);
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.
469 if (error == EINVAL) {
470 if (vap->va_flags != VNOVAL)
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);
482 vn_finished_write(mp);
488 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
491 fdesc_readdir(struct vop_readdir_args *ap)
493 struct uio *uio = ap->a_uio;
494 struct filedesc *fdp;
496 struct dirent *dp = &d;
497 int error, i, off, fcnt;
499 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
500 panic("fdesc_readdir: not dir");
502 if (ap->a_ncookies != NULL)
505 off = (int)uio->uio_offset;
506 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
507 uio->uio_resid < UIO_MX)
509 i = (u_int)off / UIO_MX;
510 fdp = uio->uio_td->td_proc->p_fd;
513 fcnt = i - 2; /* The first two nodes are `.' and `..' */
516 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
517 bzero((caddr_t)dp, UIO_MX);
521 dp->d_fileno = i + FD_ROOT;
522 dp->d_namlen = i + 1;
523 dp->d_reclen = UIO_MX;
524 bcopy("..", dp->d_name, dp->d_namlen);
525 dp->d_name[i + 1] = '\0';
529 if (fdp->fd_ofiles[fcnt].fde_file == NULL)
531 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
532 dp->d_reclen = UIO_MX;
534 dp->d_fileno = i + FD_DESC;
537 if (dp->d_namlen != 0) {
539 * And ship to userland
541 FILEDESC_SUNLOCK(fdp);
542 error = uiomove(dp, UIO_MX, uio);
550 FILEDESC_SUNLOCK(fdp);
553 uio->uio_offset = i * UIO_MX;
558 fdesc_reclaim(struct vop_reclaim_args *ap)
561 struct fdescnode *fd;
565 fdesc_remove_entry(fd);
566 free(vp->v_data, M_TEMP);