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 * 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.
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>
44 #include <sys/dirent.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h> /* boottime */
48 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h> /* Must come after sys/malloc.h */
51 #include <sys/mount.h>
52 #include <sys/namei.h>
55 #include <sys/vnode.h>
57 #include <fs/fdescfs/fdesc.h>
60 #define FD_NHASH(ix) \
61 (&fdhashtbl[(ix) & fdhash])
62 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
65 struct mtx fdesc_hashmtx;
67 static vop_getattr_t fdesc_getattr;
68 static vop_lookup_t fdesc_lookup;
69 static vop_open_t fdesc_open;
70 static vop_readdir_t fdesc_readdir;
71 static vop_reclaim_t fdesc_reclaim;
72 static vop_setattr_t fdesc_setattr;
74 static struct vop_vector fdesc_vnodeops = {
75 .vop_default = &default_vnodeops,
77 .vop_access = VOP_NULL,
78 .vop_getattr = fdesc_getattr,
79 .vop_lookup = fdesc_lookup,
80 .vop_open = fdesc_open,
81 .vop_pathconf = vop_stdpathconf,
82 .vop_readdir = fdesc_readdir,
83 .vop_reclaim = fdesc_reclaim,
84 .vop_setattr = fdesc_setattr,
87 static void fdesc_insmntque_dtr(struct vnode *, void *);
88 static void fdesc_remove_entry(struct fdescnode *);
91 * Initialise cache headers
98 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
99 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
104 * Uninit ready for unload.
108 struct vfsconf *vfsp;
111 hashdestroy(fdhashtbl, M_CACHE, fdhash);
112 mtx_destroy(&fdesc_hashmtx);
117 * If allocating vnode fails, call this.
120 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
128 * Remove an entry from the hash if it exists.
131 fdesc_remove_entry(struct fdescnode *fd)
133 struct fdhashhead *fc;
134 struct fdescnode *fd2;
136 fc = FD_NHASH(fd->fd_ix);
137 mtx_lock(&fdesc_hashmtx);
138 LIST_FOREACH(fd2, fc, fd_hash) {
140 LIST_REMOVE(fd, fd_hash);
144 mtx_unlock(&fdesc_hashmtx);
148 fdesc_allocvp(ftype, fd_fd, ix, mp, vpp)
155 struct fdescmount *fmp;
156 struct fdhashhead *fc;
157 struct fdescnode *fd, *fd2;
158 struct vnode *vp, *vp2;
165 mtx_lock(&fdesc_hashmtx);
167 * If a forced unmount is progressing, we need to drop it. The flags are
168 * protected by the hashmtx.
170 fmp = (struct fdescmount *)mp->mnt_data;
171 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
172 mtx_unlock(&fdesc_hashmtx);
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 */
181 mtx_unlock(&fdesc_hashmtx);
182 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
188 mtx_unlock(&fdesc_hashmtx);
190 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
192 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
197 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
203 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
209 /* Make sure that someone didn't beat us when inserting the vnode. */
210 mtx_lock(&fdesc_hashmtx);
212 * If a forced unmount is progressing, we need to drop it. The flags are
213 * protected by the hashmtx.
215 fmp = (struct fdescmount *)mp->mnt_data;
216 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
217 mtx_unlock(&fdesc_hashmtx);
224 LIST_FOREACH(fd2, fc, fd_hash) {
225 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
226 /* Get reference to vnode in case it's being free'd */
229 mtx_unlock(&fdesc_hashmtx);
230 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
231 /* Someone beat us, dec use count and wait for reclaim */
234 /* If we didn't get it, return no vnode. */
242 /* If we came here, we can insert it safely. */
243 LIST_INSERT_HEAD(fc, fd, fd_hash);
244 mtx_unlock(&fdesc_hashmtx);
250 * vp is the current namei directory
251 * ndp is the name to locate in that directory...
255 struct vop_lookup_args /* {
256 struct vnode * a_dvp;
257 struct vnode ** a_vpp;
258 struct componentname * a_cnp;
261 struct vnode **vpp = ap->a_vpp;
262 struct vnode *dvp = ap->a_dvp;
263 struct componentname *cnp = ap->a_cnp;
264 char *pname = cnp->cn_nameptr;
265 struct thread *td = cnp->cn_thread;
267 int nlen = cnp->cn_namelen;
272 if ((cnp->cn_flags & ISLASTCN) &&
273 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
278 if (cnp->cn_namelen == 1 && *pname == '.') {
284 if (VTOFDESC(dvp)->fd_type != Froot) {
290 /* the only time a leading 0 is acceptable is if it's "0" */
291 if (*pname == '0' && nlen != 1) {
296 if (*pname < '0' || *pname > '9') {
300 fd1 = 10 * fd + *pname++ - '0';
308 if ((error = fget(td, fd, &fp)) != 0)
311 /* Check if we're looking up ourselves. */
312 if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
314 * In case we're holding the last reference to the file, the dvp
315 * will be re-acquired.
321 /* Re-aquire the lock afterwards. */
322 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
327 * Unlock our root node (dvp) when doing this, since we might
328 * deadlock since the vnode might be locked by another thread
329 * and the root vnode lock will be obtained afterwards (in case
330 * we're looking up the fd of the root vnode), which will be the
331 * opposite lock order. Vhold the root vnode first so we don't
336 error = fdesc_allocvp(Fdesc, fd, FD_DESC + fd, dvp->v_mount,
340 * The root vnode must be locked last to prevent deadlock condition.
342 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
358 struct vop_open_args /* {
361 struct ucred *a_cred;
365 struct vnode *vp = ap->a_vp;
367 if (VTOFDESC(vp)->fd_type == Froot)
371 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the the file
372 * descriptor being sought for duplication. The error return ensures
373 * that the vnode for this device will be released by vn_open. Open
374 * will detect this special error and take the actions in dupfdopen.
375 * Other callers of vn_open or VOP_OPEN will simply report the
378 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
384 struct vop_getattr_args /* {
387 struct ucred *a_cred;
390 struct vnode *vp = ap->a_vp;
391 struct vattr *vap = ap->a_vap;
393 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
394 vap->va_fileid = VTOFDESC(vp)->fd_ix;
397 vap->va_blocksize = DEV_BSIZE;
398 vap->va_atime.tv_sec = boottime.tv_sec;
399 vap->va_atime.tv_nsec = 0;
400 vap->va_mtime = vap->va_atime;
401 vap->va_ctime = vap->va_mtime;
407 switch (VTOFDESC(vp)->fd_type) {
411 vap->va_size = DEV_BSIZE;
412 vap->va_rdev = NODEV;
419 vap->va_rdev = makedev(0, vap->va_fileid);
423 panic("fdesc_getattr");
427 vp->v_type = vap->va_type;
433 struct vop_setattr_args /* {
436 struct ucred *a_cred;
439 struct vattr *vap = ap->a_vap;
443 struct thread *td = curthread;
448 * Can't mess with the root vnode
450 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
453 fd = VTOFDESC(ap->a_vp)->fd_fd;
456 * Allow setattr where there is an underlying vnode.
458 error = getvnode(td->td_proc->p_fd, fd, &fp);
461 * getvnode() returns EINVAL if the file descriptor is not
462 * backed by a vnode. Silently drop all changes except
463 * chflags(2) in this case.
465 if (error == EINVAL) {
466 if (vap->va_flags != VNOVAL)
474 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
475 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
476 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
478 vn_finished_write(mp);
488 struct vop_readdir_args /* {
491 struct ucred *a_cred;
497 struct uio *uio = ap->a_uio;
498 struct filedesc *fdp;
500 struct dirent *dp = &d;
501 int error, i, off, fcnt;
504 * We don't allow exporting fdesc mounts, and currently local
505 * requests do not need cookies.
508 panic("fdesc_readdir: not hungry");
510 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
511 panic("fdesc_readdir: not dir");
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)
517 i = (u_int)off / UIO_MX;
518 fdp = uio->uio_td->td_proc->p_fd;
521 fcnt = i - 2; /* The first two nodes are `.' and `..' */
524 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
525 bzero((caddr_t)dp, UIO_MX);
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';
537 if (fdp->fd_ofiles[fcnt] == NULL)
539 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
540 dp->d_reclen = UIO_MX;
541 dp->d_type = DT_UNKNOWN;
542 dp->d_fileno = i + FD_DESC;
545 if (dp->d_namlen != 0) {
547 * And ship to userland
549 FILEDESC_SUNLOCK(fdp);
550 error = uiomove(dp, UIO_MX, uio);
558 FILEDESC_SUNLOCK(fdp);
561 uio->uio_offset = i * UIO_MX;
567 struct vop_reclaim_args /* {
572 struct fdescnode *fd;
576 fdesc_remove_entry(fd);
577 free(vp->v_data, M_TEMP);