AMD64 - Refactor uio_resid and size_t assumptions.
[dragonfly.git] / sys / vfs / fdesc / fdesc_vnops.c
blobc39e6945740f91ab4e5f8631843e78bf3ef162e3
1 /*
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
6 * Jan-Simon Pendry.
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. 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
34 * SUCH DAMAGE.
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 $
43 * /dev/fd Filesystem
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/dirent.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h> /* boottime */
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/file.h> /* Must come after sys/malloc.h */
55 #include <sys/mount.h>
56 #include <sys/proc.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/stat.h>
60 #include <sys/vnode.h>
61 #include <sys/file2.h>
63 #include <machine/limits.h>
65 #include "fdesc.h"
67 #define FDL_WANT 0x01
68 #define FDL_LOCKED 0x02
69 static int fdcache_lock;
71 #define NFDCACHE 4
72 #define FD_NHASH(ix) \
73 (&fdhashtbl[(ix) & fdhash])
74 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
75 static u_long fdhash;
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_poll (struct vop_poll_args *ap);
85 static int fdesc_setattr (struct vop_setattr_args *ap);
88 * Initialise cache headers
90 int
91 fdesc_init(struct vfsconf *vfsp)
94 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
95 return (0);
98 int
99 fdesc_uninit(struct vfsconf *vfsp)
101 if (fdhashtbl)
102 kfree(fdhashtbl, M_CACHE);
103 return (0);
106 fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
108 struct fdhashhead *fc;
109 struct fdescnode *fd;
110 int error = 0;
112 fc = FD_NHASH(ix);
113 loop:
114 LIST_FOREACH(fd, fc, fd_hash) {
115 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
116 if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
117 goto loop;
118 *vpp = fd->fd_vnode;
119 return (error);
124 * otherwise lock the array while we call getnewvnode
125 * since that can block.
127 if (fdcache_lock & FDL_LOCKED) {
128 fdcache_lock |= FDL_WANT;
129 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
130 goto loop;
132 fdcache_lock |= FDL_LOCKED;
135 * Do the MALLOC before the getnewvnode since doing so afterward
136 * might cause a bogus v_data pointer to get dereferenced
137 * elsewhere if MALLOC should block.
139 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
141 error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
142 if (error) {
143 FREE(fd, M_TEMP);
144 goto out;
146 (*vpp)->v_data = fd;
147 fd->fd_vnode = *vpp;
148 fd->fd_type = ftype;
149 fd->fd_fd = -1;
150 fd->fd_ix = ix;
151 LIST_INSERT_HEAD(fc, fd, fd_hash);
152 vx_unlock(*vpp);
154 out:
155 fdcache_lock &= ~FDL_LOCKED;
157 if (fdcache_lock & FDL_WANT) {
158 fdcache_lock &= ~FDL_WANT;
159 wakeup((caddr_t) &fdcache_lock);
162 return (error);
166 * vp is the current namei directory
167 * ndp is the name to locate in that directory...
169 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
170 * struct componentname *a_cnp)
172 static int
173 fdesc_lookup(struct vop_old_lookup_args *ap)
175 struct componentname *cnp = ap->a_cnp;
176 struct thread *td = cnp->cn_td;
177 struct proc *p = td->td_proc;
178 struct vnode **vpp = ap->a_vpp;
179 struct vnode *dvp = ap->a_dvp;
180 char *pname = cnp->cn_nameptr;
181 int nlen = cnp->cn_namelen;
182 int nfiles;
183 u_int fd;
184 int error;
185 struct vnode *fvp;
187 KKASSERT(p);
188 nfiles = p->p_fd->fd_nfiles;
189 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
190 error = EROFS;
191 goto bad;
194 vn_unlock(dvp);
195 if (cnp->cn_namelen == 1 && *pname == '.') {
196 *vpp = dvp;
197 vref(dvp);
198 vn_lock(dvp, LK_SHARED | LK_RETRY);
199 return (0);
202 if (VTOFDESC(dvp)->fd_type != Froot) {
203 error = ENOTDIR;
204 goto bad;
207 fd = 0;
208 /* the only time a leading 0 is acceptable is if it's "0" */
209 if (*pname == '0' && nlen != 1) {
210 error = ENOENT;
211 goto bad;
213 while (nlen--) {
214 if (*pname < '0' || *pname > '9') {
215 error = ENOENT;
216 goto bad;
218 fd = 10 * fd + *pname++ - '0';
221 if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
222 error = EBADF;
223 goto bad;
226 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
227 if (error)
228 goto bad;
229 VTOFDESC(fvp)->fd_fd = fd;
230 vn_lock(fvp, LK_SHARED | LK_RETRY);
231 *vpp = fvp;
232 return (0);
234 bad:
235 vn_lock(dvp, LK_SHARED | LK_RETRY);
236 *vpp = NULL;
237 return (error);
241 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
242 * struct file *a_fp)
244 static int
245 fdesc_open(struct vop_open_args *ap)
247 struct vnode *vp = ap->a_vp;
248 struct lwp *lp = curthread->td_lwp;
250 KKASSERT(lp);
252 if (VTOFDESC(vp)->fd_type == Froot)
253 return (vop_stdopen(ap));
256 * XXX Kludge: set lp->lwp_dupfd to contain the value of the the file
257 * descriptor being sought for duplication. The error return ensures
258 * that the vnode for this device will be released by vn_open. Open
259 * will detect this special error and take the actions in dupfdopen.
260 * Other callers of vn_open or VOP_OPEN will simply report the
261 * error.
263 lp->lwp_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
264 return (ENODEV);
268 * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
270 static int
271 fdesc_getattr(struct vop_getattr_args *ap)
273 struct proc *p = curproc;
274 struct vnode *vp = ap->a_vp;
275 struct vattr *vap = ap->a_vap;
276 struct file *fp;
277 struct stat stb;
278 u_int fd;
279 int error = 0;
281 KKASSERT(p);
283 switch (VTOFDESC(vp)->fd_type) {
284 case Froot:
285 VATTR_NULL(vap);
287 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
288 vap->va_type = VDIR;
289 vap->va_nlink = 2;
290 vap->va_size = DEV_BSIZE;
291 vap->va_fileid = VTOFDESC(vp)->fd_ix;
292 vap->va_uid = 0;
293 vap->va_gid = 0;
294 vap->va_blocksize = DEV_BSIZE;
295 vap->va_atime.tv_sec = boottime.tv_sec;
296 vap->va_atime.tv_nsec = 0;
297 vap->va_mtime = vap->va_atime;
298 vap->va_ctime = vap->va_mtime;
299 vap->va_gen = 0;
300 vap->va_flags = 0;
301 vap->va_rmajor = VNOVAL;
302 vap->va_rminor = VNOVAL;
303 vap->va_bytes = 0;
304 break;
306 case Fdesc:
307 fd = VTOFDESC(vp)->fd_fd;
309 fp = holdfp(p->p_fd, fd, -1);
310 if (fp == NULL)
311 return (EBADF);
313 bzero(&stb, sizeof(stb));
314 error = fo_stat(fp, &stb, curproc->p_ucred);
315 fdrop(fp);
317 if (error == 0) {
318 VATTR_NULL(vap);
319 vap->va_type = IFTOVT(stb.st_mode);
320 vap->va_mode = stb.st_mode;
321 #define FDRX (VREAD|VEXEC)
322 if (vap->va_type == VDIR)
323 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
324 #undef FDRX
325 vap->va_nlink = 1;
326 vap->va_flags = 0;
327 vap->va_bytes = stb.st_blocks * stb.st_blksize;
328 vap->va_fileid = VTOFDESC(vp)->fd_ix;
329 vap->va_size = stb.st_size;
330 vap->va_blocksize = stb.st_blksize;
331 vap->va_rmajor = umajor(stb.st_rdev);
332 vap->va_rminor = uminor(stb.st_rdev);
335 * If no time data is provided, use the current time.
337 if (stb.st_atimespec.tv_sec == 0 &&
338 stb.st_atimespec.tv_nsec == 0)
339 nanotime(&stb.st_atimespec);
341 if (stb.st_ctimespec.tv_sec == 0 &&
342 stb.st_ctimespec.tv_nsec == 0)
343 nanotime(&stb.st_ctimespec);
345 if (stb.st_mtimespec.tv_sec == 0 &&
346 stb.st_mtimespec.tv_nsec == 0)
347 nanotime(&stb.st_mtimespec);
349 vap->va_atime = stb.st_atimespec;
350 vap->va_mtime = stb.st_mtimespec;
351 vap->va_ctime = stb.st_ctimespec;
352 vap->va_uid = stb.st_uid;
353 vap->va_gid = stb.st_gid;
355 break;
357 default:
358 panic("fdesc_getattr");
359 break;
362 if (error == 0)
363 vp->v_type = vap->va_type;
364 return (error);
368 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
369 * struct ucred *a_cred)
371 static int
372 fdesc_setattr(struct vop_setattr_args *ap)
374 struct proc *p = curproc;
375 struct vattr *vap = ap->a_vap;
376 struct file *fp;
377 unsigned fd;
378 int error;
381 * Can't mess with the root vnode
383 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
384 return (EACCES);
386 fd = VTOFDESC(ap->a_vp)->fd_fd;
387 KKASSERT(p);
390 * Allow setattr where there is an underlying vnode.
392 error = holdvnode(p->p_fd, fd, &fp);
393 if (error) {
395 * holdvnode() returns EINVAL if the file descriptor is not
396 * backed by a vnode. Silently drop all changes except
397 * chflags(2) in this case.
399 if (error == EINVAL) {
400 if (vap->va_flags != VNOVAL)
401 error = EOPNOTSUPP;
402 else
403 error = 0;
405 } else {
406 fdrop(fp);
408 return (error);
411 #define UIO_MX 16
414 * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
415 * int *a_eofflag, off_t *a_cookies, int a_ncookies)
417 static int
418 fdesc_readdir(struct vop_readdir_args *ap)
420 struct uio *uio = ap->a_uio;
421 struct filedesc *fdp;
422 int error, i, fcnt;
423 size_t namelen;
424 char name[20]; /* enough for %d */
427 * We don't allow exporting fdesc mounts, and currently local
428 * requests do not need cookies.
430 if (ap->a_ncookies)
431 panic("fdesc_readdir: not hungry");
433 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
434 panic("fdesc_readdir: not dir");
436 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
437 return(EINVAL);
438 i = (int)uio->uio_offset;
439 KKASSERT(uio->uio_td->td_proc);
440 fdp = uio->uio_td->td_proc->p_fd;
441 error = 0;
443 fcnt = i - 2; /* The first two nodes are `.' and `..' */
445 while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
446 switch (i) {
447 case 0: /* `.' */
448 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
449 1, "."))
450 goto done;
451 if (error)
452 return (error);
453 break;
454 case 1: /* `..' */
455 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
456 2, ".."))
457 goto done;
458 if (error)
459 return (error);
460 break;
461 default:
462 if (fdp->fd_files[fcnt].fp == NULL) {
463 fcnt++;
464 continue;
467 namelen = ksnprintf(name, sizeof(name), "%d", fcnt);
468 if (vop_write_dirent(&error, uio, FD_ROOT + i,
469 DT_UNKNOWN, namelen, name))
470 goto done;
471 if (error)
472 return (error);
473 break;
475 i++;
476 fcnt++;
479 done:
480 if (i >= 2)
481 uio->uio_offset = fcnt + 2;
482 else
483 uio->uio_offset = i;
484 return (error);
488 * fdesc_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred)
490 static int
491 fdesc_poll(struct vop_poll_args *ap)
493 return seltrue(0, ap->a_events);
497 * fdesc_inactive(struct vnode *a_vp)
499 static int
500 fdesc_inactive(struct vop_inactive_args *ap)
502 struct vnode *vp = ap->a_vp;
505 * Clear out the v_type field to avoid
506 * nasty things happening in vgone().
508 vp->v_type = VNON;
509 return (0);
513 * fdesc_reclaim(struct vnode *a_vp)
515 static int
516 fdesc_reclaim(struct vop_reclaim_args *ap)
518 struct vnode *vp = ap->a_vp;
519 struct fdescnode *fd = VTOFDESC(vp);
521 LIST_REMOVE(fd, fd_hash);
522 FREE(vp->v_data, M_TEMP);
523 vp->v_data = 0;
525 return (0);
529 * Print out the contents of a /dev/fd vnode.
531 * fdesc_print(struct vnode *a_vp)
533 /* ARGSUSED */
534 static int
535 fdesc_print(struct vop_print_args *ap)
537 kprintf("tag VT_NON, fdesc vnode\n");
538 return (0);
541 struct vop_ops fdesc_vnode_vops = {
542 .vop_default = vop_defaultop,
543 .vop_access = (void *)vop_null,
544 .vop_getattr = fdesc_getattr,
545 .vop_inactive = fdesc_inactive,
546 .vop_old_lookup = fdesc_lookup,
547 .vop_open = fdesc_open,
548 .vop_pathconf = vop_stdpathconf,
549 .vop_poll = fdesc_poll,
550 .vop_print = fdesc_print,
551 .vop_readdir = fdesc_readdir,
552 .vop_reclaim = fdesc_reclaim,
553 .vop_setattr = fdesc_setattr