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 FDL_LOCKED 0x02
61 static int fdcache_lock;
64 #define FD_NHASH(ix) \
65 (&fdhashtbl[(ix) & fdhash])
66 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
69 static vop_getattr_t fdesc_getattr;
70 static vop_inactive_t fdesc_inactive;
71 static vop_lookup_t fdesc_lookup;
72 static vop_open_t fdesc_open;
73 static vop_readdir_t fdesc_readdir;
74 static vop_reclaim_t fdesc_reclaim;
75 static vop_setattr_t fdesc_setattr;
77 static struct vop_vector fdesc_vnodeops = {
78 .vop_default = &default_vnodeops,
80 .vop_access = VOP_NULL,
81 .vop_getattr = fdesc_getattr,
82 .vop_inactive = fdesc_inactive,
83 .vop_lookup = fdesc_lookup,
84 .vop_open = fdesc_open,
85 .vop_pathconf = vop_stdpathconf,
86 .vop_readdir = fdesc_readdir,
87 .vop_reclaim = fdesc_reclaim,
88 .vop_setattr = fdesc_setattr,
92 * Initialise cache headers
99 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
104 fdesc_allocvp(ftype, ix, mp, vpp, td)
111 struct fdhashhead *fc;
112 struct fdescnode *fd;
117 LIST_FOREACH(fd, fc, fd_hash) {
118 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
119 if (vget(fd->fd_vnode, 0, td))
127 * otherwise lock the array while we call getnewvnode
128 * since that can block.
130 if (fdcache_lock & FDL_LOCKED) {
131 fdcache_lock |= FDL_WANT;
132 (void) tsleep( &fdcache_lock, PINOD, "fdalvp", 0);
135 fdcache_lock |= FDL_LOCKED;
138 * Do the MALLOC before the getnewvnode since doing so afterward
139 * might cause a bogus v_data pointer to get dereferenced
140 * elsewhere if MALLOC should block.
142 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
144 error = getnewvnode("fdesc", mp, &fdesc_vnodeops, vpp);
154 LIST_INSERT_HEAD(fc, fd, fd_hash);
157 fdcache_lock &= ~FDL_LOCKED;
159 if (fdcache_lock & FDL_WANT) {
160 fdcache_lock &= ~FDL_WANT;
161 wakeup( &fdcache_lock);
168 * vp is the current namei directory
169 * ndp is the name to locate in that directory...
173 struct vop_lookup_args /* {
174 struct vnode * a_dvp;
175 struct vnode ** a_vpp;
176 struct componentname * a_cnp;
179 struct vnode **vpp = ap->a_vpp;
180 struct vnode *dvp = ap->a_dvp;
181 struct componentname *cnp = ap->a_cnp;
182 char *pname = cnp->cn_nameptr;
183 struct thread *td = cnp->cn_thread;
185 int nlen = cnp->cn_namelen;
190 if ((cnp->cn_flags & ISLASTCN) &&
191 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
196 if (cnp->cn_namelen == 1 && *pname == '.') {
202 if (VTOFDESC(dvp)->fd_type != Froot) {
208 /* the only time a leading 0 is acceptable is if it's "0" */
209 if (*pname == '0' && nlen != 1) {
214 if (*pname < '0' || *pname > '9') {
218 fd = 10 * fd + *pname++ - '0';
221 if ((error = fget(td, fd, &fp)) != 0)
224 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, td);
228 VTOFDESC(fvp)->fd_fd = fd;
230 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, td);
241 struct vop_open_args /* {
244 struct ucred *a_cred;
248 struct vnode *vp = ap->a_vp;
250 if (VTOFDESC(vp)->fd_type == Froot)
254 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the the file
255 * descriptor being sought for duplication. The error return ensures
256 * that the vnode for this device will be released by vn_open. Open
257 * will detect this special error and take the actions in dupfdopen.
258 * Other callers of vn_open or VOP_OPEN will simply report the
261 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
267 struct vop_getattr_args /* {
270 struct ucred *a_cred;
274 struct vnode *vp = ap->a_vp;
275 struct vattr *vap = ap->a_vap;
281 switch (VTOFDESC(vp)->fd_type) {
285 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
288 vap->va_size = DEV_BSIZE;
289 vap->va_fileid = VTOFDESC(vp)->fd_ix;
292 vap->va_blocksize = DEV_BSIZE;
293 vap->va_atime.tv_sec = boottime.tv_sec;
294 vap->va_atime.tv_nsec = 0;
295 vap->va_mtime = vap->va_atime;
296 vap->va_ctime = vap->va_mtime;
304 fd = VTOFDESC(vp)->fd_fd;
306 if ((error = fget(ap->a_td, fd, &fp)) != 0)
309 bzero(&stb, sizeof(stb));
310 error = fo_stat(fp, &stb, ap->a_td->td_ucred, ap->a_td);
314 vap->va_type = IFTOVT(stb.st_mode);
315 vap->va_mode = stb.st_mode;
316 #define FDRX (VREAD|VEXEC)
317 if (vap->va_type == VDIR)
318 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
322 vap->va_bytes = stb.st_blocks * stb.st_blksize;
323 vap->va_fileid = VTOFDESC(vp)->fd_ix;
324 vap->va_size = stb.st_size;
325 vap->va_blocksize = stb.st_blksize;
326 vap->va_rdev = stb.st_rdev;
329 * If no time data is provided, use the current time.
331 if (stb.st_atimespec.tv_sec == 0 &&
332 stb.st_atimespec.tv_nsec == 0)
333 nanotime(&stb.st_atimespec);
335 if (stb.st_ctimespec.tv_sec == 0 &&
336 stb.st_ctimespec.tv_nsec == 0)
337 nanotime(&stb.st_ctimespec);
339 if (stb.st_mtimespec.tv_sec == 0 &&
340 stb.st_mtimespec.tv_nsec == 0)
341 nanotime(&stb.st_mtimespec);
343 vap->va_atime = stb.st_atimespec;
344 vap->va_mtime = stb.st_mtimespec;
345 vap->va_ctime = stb.st_ctimespec;
346 vap->va_uid = stb.st_uid;
347 vap->va_gid = stb.st_gid;
352 panic("fdesc_getattr");
357 vp->v_type = vap->va_type;
363 struct vop_setattr_args /* {
366 struct ucred *a_cred;
370 struct vattr *vap = ap->a_vap;
378 * Can't mess with the root vnode
380 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
383 fd = VTOFDESC(ap->a_vp)->fd_fd;
386 * Allow setattr where there is an underlying vnode.
388 error = getvnode(ap->a_td->td_proc->p_fd, fd, &fp);
391 * getvnode() returns EINVAL if the file descriptor is not
392 * backed by a vnode. Silently drop all changes except
393 * chflags(2) in this case.
395 if (error == EINVAL) {
396 if (vap->va_flags != VNOVAL)
404 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
405 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
406 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred, ap->a_td);
407 VOP_UNLOCK(vp, 0, ap->a_td);
408 vn_finished_write(mp);
418 struct vop_readdir_args /* {
421 struct ucred *a_cred;
427 struct uio *uio = ap->a_uio;
428 struct filedesc *fdp;
430 struct dirent *dp = &d;
431 int error, i, off, fcnt;
434 * We don't allow exporting fdesc mounts, and currently local
435 * requests do not need cookies.
438 panic("fdesc_readdir: not hungry");
440 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
441 panic("fdesc_readdir: not dir");
443 off = (int)uio->uio_offset;
444 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
445 uio->uio_resid < UIO_MX)
447 i = (u_int)off / UIO_MX;
448 fdp = uio->uio_td->td_proc->p_fd;
451 fcnt = i - 2; /* The first two nodes are `.' and `..' */
453 FILEDESC_LOCK_FAST(fdp);
454 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
458 bzero((caddr_t)dp, UIO_MX);
460 dp->d_fileno = i + FD_ROOT;
461 dp->d_namlen = i + 1;
462 dp->d_reclen = UIO_MX;
463 bcopy("..", dp->d_name, dp->d_namlen);
464 dp->d_name[i + 1] = '\0';
468 if (fdp->fd_ofiles[fcnt] == NULL) {
469 FILEDESC_UNLOCK_FAST(fdp);
473 bzero((caddr_t) dp, UIO_MX);
474 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
475 dp->d_reclen = UIO_MX;
476 dp->d_type = DT_UNKNOWN;
477 dp->d_fileno = i + FD_DESC;
481 * And ship to userland
483 FILEDESC_UNLOCK_FAST(fdp);
484 error = uiomove(dp, UIO_MX, uio);
487 FILEDESC_LOCK_FAST(fdp);
491 FILEDESC_UNLOCK_FAST(fdp);
494 uio->uio_offset = i * UIO_MX;
500 struct vop_inactive_args /* {
505 struct vnode *vp = ap->a_vp;
508 * Clear out the v_type field to avoid
509 * nasty things happening in vgone().
517 struct vop_reclaim_args /* {
521 struct vnode *vp = ap->a_vp;
522 struct fdescnode *fd = VTOFDESC(vp);
524 LIST_REMOVE(fd, fd_hash);
525 FREE(vp->v_data, M_TEMP);