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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
38 * $FreeBSD: src/sys/miscfs/fdesc/fdesc_vnops.c,v 1.47.2.1 2001/10/22 22:49:26 chris Exp $
39 * $DragonFly: src/sys/vfs/fdesc/fdesc_vnops.c,v 1.39 2007/11/20 21:03:50 dillon Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/dirent.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h> /* boottime */
53 #include <sys/malloc.h>
54 #include <sys/file.h> /* Must come after sys/malloc.h */
55 #include <sys/mount.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/file2.h>
63 #include <machine/limits.h>
68 #define FDL_LOCKED 0x02
69 static int fdcache_lock;
72 #define FD_NHASH(ix) \
73 (&fdhashtbl[(ix) & fdhash])
74 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
77 static int fdesc_getattr (struct vop_getattr_args *ap);
78 static int fdesc_inactive (struct vop_inactive_args *ap);
79 static int fdesc_lookup (struct vop_old_lookup_args *ap);
80 static int fdesc_open (struct vop_open_args *ap);
81 static int fdesc_print (struct vop_print_args *ap);
82 static int fdesc_readdir (struct vop_readdir_args *ap);
83 static int fdesc_reclaim (struct vop_reclaim_args *ap);
84 static int fdesc_setattr (struct vop_setattr_args *ap);
87 * Initialise cache headers
90 fdesc_init(struct vfsconf *vfsp)
93 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
98 fdesc_uninit(struct vfsconf *vfsp)
101 kfree(fdhashtbl, M_CACHE);
105 fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
107 struct fdhashhead *fc;
108 struct fdescnode *fd;
113 LIST_FOREACH(fd, fc, fd_hash) {
114 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
115 if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
123 * otherwise lock the array while we call getnewvnode
124 * since that can block.
126 if (fdcache_lock & FDL_LOCKED) {
127 fdcache_lock |= FDL_WANT;
128 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
131 fdcache_lock |= FDL_LOCKED;
134 * Do the MALLOC before the getnewvnode since doing so afterward
135 * might cause a bogus v_data pointer to get dereferenced
136 * elsewhere if MALLOC should block.
138 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
140 error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
150 LIST_INSERT_HEAD(fc, fd, fd_hash);
154 fdcache_lock &= ~FDL_LOCKED;
156 if (fdcache_lock & FDL_WANT) {
157 fdcache_lock &= ~FDL_WANT;
158 wakeup((caddr_t) &fdcache_lock);
165 * vp is the current namei directory
166 * ndp is the name to locate in that directory...
168 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
169 * struct componentname *a_cnp)
172 fdesc_lookup(struct vop_old_lookup_args *ap)
174 struct componentname *cnp = ap->a_cnp;
175 struct thread *td = cnp->cn_td;
176 struct proc *p = td->td_proc;
177 struct vnode **vpp = ap->a_vpp;
178 struct vnode *dvp = ap->a_dvp;
179 char *pname = cnp->cn_nameptr;
180 int nlen = cnp->cn_namelen;
187 nfiles = p->p_fd->fd_nfiles;
188 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
194 if (cnp->cn_namelen == 1 && *pname == '.') {
197 vn_lock(dvp, LK_SHARED | LK_RETRY);
201 if (VTOFDESC(dvp)->fd_type != Froot) {
207 /* the only time a leading 0 is acceptable is if it's "0" */
208 if (*pname == '0' && nlen != 1) {
213 if (*pname < '0' || *pname > '9') {
217 fd = 10 * fd + *pname++ - '0';
220 if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
225 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
228 VTOFDESC(fvp)->fd_fd = fd;
229 vn_lock(fvp, LK_SHARED | LK_RETRY);
234 vn_lock(dvp, LK_SHARED | LK_RETRY);
240 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
244 fdesc_open(struct vop_open_args *ap)
246 struct vnode *vp = ap->a_vp;
247 struct lwp *lp = curthread->td_lwp;
251 if (VTOFDESC(vp)->fd_type == Froot)
252 return (vop_stdopen(ap));
255 * XXX Kludge: set lp->lwp_dupfd to contain the value of the the file
256 * descriptor being sought for duplication. The error return ensures
257 * that the vnode for this device will be released by vn_open. Open
258 * will detect this special error and take the actions in dupfdopen.
259 * Other callers of vn_open or VOP_OPEN will simply report the
262 lp->lwp_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
267 * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
270 fdesc_getattr(struct vop_getattr_args *ap)
272 struct proc *p = curproc;
273 struct vnode *vp = ap->a_vp;
274 struct vattr *vap = ap->a_vap;
282 switch (VTOFDESC(vp)->fd_type) {
286 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
289 vap->va_size = DEV_BSIZE;
290 vap->va_fileid = VTOFDESC(vp)->fd_ix;
293 vap->va_blocksize = DEV_BSIZE;
294 vap->va_atime.tv_sec = boottime.tv_sec;
295 vap->va_atime.tv_nsec = 0;
296 vap->va_mtime = vap->va_atime;
297 vap->va_ctime = vap->va_mtime;
300 vap->va_rmajor = VNOVAL;
301 vap->va_rminor = VNOVAL;
306 fd = VTOFDESC(vp)->fd_fd;
308 fp = holdfp(p->p_fd, fd, -1);
312 bzero(&stb, sizeof(stb));
313 error = fo_stat(fp, &stb, curproc->p_ucred);
318 vap->va_type = IFTOVT(stb.st_mode);
319 vap->va_mode = stb.st_mode;
320 #define FDRX (VREAD|VEXEC)
321 if (vap->va_type == VDIR)
322 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
326 vap->va_bytes = stb.st_blocks * stb.st_blksize;
327 vap->va_fileid = VTOFDESC(vp)->fd_ix;
328 vap->va_size = stb.st_size;
329 vap->va_blocksize = stb.st_blksize;
330 vap->va_rmajor = umajor(stb.st_rdev);
331 vap->va_rminor = uminor(stb.st_rdev);
334 * If no time data is provided, use the current time.
336 if (stb.st_atimespec.tv_sec == 0 &&
337 stb.st_atimespec.tv_nsec == 0)
338 nanotime(&stb.st_atimespec);
340 if (stb.st_ctimespec.tv_sec == 0 &&
341 stb.st_ctimespec.tv_nsec == 0)
342 nanotime(&stb.st_ctimespec);
344 if (stb.st_mtimespec.tv_sec == 0 &&
345 stb.st_mtimespec.tv_nsec == 0)
346 nanotime(&stb.st_mtimespec);
348 vap->va_atime = stb.st_atimespec;
349 vap->va_mtime = stb.st_mtimespec;
350 vap->va_ctime = stb.st_ctimespec;
351 vap->va_uid = stb.st_uid;
352 vap->va_gid = stb.st_gid;
357 panic("fdesc_getattr");
362 vp->v_type = vap->va_type;
367 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
368 * struct ucred *a_cred)
371 fdesc_setattr(struct vop_setattr_args *ap)
373 struct proc *p = curproc;
374 struct vattr *vap = ap->a_vap;
380 * Can't mess with the root vnode
382 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
385 fd = VTOFDESC(ap->a_vp)->fd_fd;
389 * Allow setattr where there is an underlying vnode.
391 error = holdvnode(p->p_fd, fd, &fp);
394 * holdvnode() returns EINVAL if the file descriptor is not
395 * backed by a vnode. Silently drop all changes except
396 * chflags(2) in this case.
398 if (error == EINVAL) {
399 if (vap->va_flags != VNOVAL)
413 * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
414 * int *a_eofflag, off_t *a_cookies, int a_ncookies)
417 fdesc_readdir(struct vop_readdir_args *ap)
419 struct uio *uio = ap->a_uio;
420 struct filedesc *fdp;
423 char name[20]; /* enough for %d */
426 * We don't allow exporting fdesc mounts, and currently local
427 * requests do not need cookies.
430 panic("fdesc_readdir: not hungry");
432 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
433 panic("fdesc_readdir: not dir");
435 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
437 i = (int)uio->uio_offset;
438 KKASSERT(uio->uio_td->td_proc);
439 fdp = uio->uio_td->td_proc->p_fd;
442 fcnt = i - 2; /* The first two nodes are `.' and `..' */
444 while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
447 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
454 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
461 if (fdp->fd_files[fcnt].fp == NULL) {
466 namelen = ksnprintf(name, sizeof(name), "%d", fcnt);
467 if (vop_write_dirent(&error, uio, FD_ROOT + i,
468 DT_UNKNOWN, namelen, name))
480 uio->uio_offset = fcnt + 2;
487 * fdesc_inactive(struct vnode *a_vp)
490 fdesc_inactive(struct vop_inactive_args *ap)
492 struct vnode *vp = ap->a_vp;
495 * Clear out the v_type field to avoid
496 * nasty things happening in vgone().
503 * fdesc_reclaim(struct vnode *a_vp)
506 fdesc_reclaim(struct vop_reclaim_args *ap)
508 struct vnode *vp = ap->a_vp;
509 struct fdescnode *fd = VTOFDESC(vp);
511 LIST_REMOVE(fd, fd_hash);
512 FREE(vp->v_data, M_TEMP);
519 * Print out the contents of a /dev/fd vnode.
521 * fdesc_print(struct vnode *a_vp)
525 fdesc_print(struct vop_print_args *ap)
527 kprintf("tag VT_NON, fdesc vnode\n");
531 struct vop_ops fdesc_vnode_vops = {
532 .vop_default = vop_defaultop,
533 .vop_access = (void *)vop_null,
534 .vop_getattr = fdesc_getattr,
535 .vop_inactive = fdesc_inactive,
536 .vop_old_lookup = fdesc_lookup,
537 .vop_open = fdesc_open,
538 .vop_pathconf = vop_stdpathconf,
539 .vop_print = fdesc_print,
540 .vop_readdir = fdesc_readdir,
541 .vop_reclaim = fdesc_reclaim,
542 .vop_setattr = fdesc_setattr