Use vop_write_dirent. Correctly handle the case of spare fd tables, e.g.
[dragonfly/netmp.git] / sys / vfs / fdesc / fdesc_vnops.c
blob90e01e986d460a0a99987efbe8a72966da38f5d7
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.20 2005/08/16 22:40:03 joerg 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_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 free(fdhashtbl, M_CACHE);
103 return (0);
106 fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp,
107 struct thread *td)
109 struct fdhashhead *fc;
110 struct fdescnode *fd;
111 int error = 0;
113 fc = FD_NHASH(ix);
114 loop:
115 LIST_FOREACH(fd, fc, fd_hash) {
116 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
117 if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL, td))
118 goto loop;
119 *vpp = fd->fd_vnode;
120 return (error);
125 * otherwise lock the array while we call getnewvnode
126 * since that can block.
128 if (fdcache_lock & FDL_LOCKED) {
129 fdcache_lock |= FDL_WANT;
130 (void) tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
131 goto loop;
133 fdcache_lock |= FDL_LOCKED;
136 * Do the MALLOC before the getnewvnode since doing so afterward
137 * might cause a bogus v_data pointer to get dereferenced
138 * elsewhere if MALLOC should block.
140 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
142 error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
143 if (error) {
144 FREE(fd, M_TEMP);
145 goto out;
147 (*vpp)->v_data = fd;
148 fd->fd_vnode = *vpp;
149 fd->fd_type = ftype;
150 fd->fd_fd = -1;
151 fd->fd_ix = ix;
152 LIST_INSERT_HEAD(fc, fd, fd_hash);
153 vx_unlock(*vpp);
155 out:
156 fdcache_lock &= ~FDL_LOCKED;
158 if (fdcache_lock & FDL_WANT) {
159 fdcache_lock &= ~FDL_WANT;
160 wakeup((caddr_t) &fdcache_lock);
163 return (error);
167 * vp is the current namei directory
168 * ndp is the name to locate in that directory...
170 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
171 * struct componentname *a_cnp)
173 static int
174 fdesc_lookup(struct vop_lookup_args *ap)
176 struct componentname *cnp = ap->a_cnp;
177 struct thread *td = cnp->cn_td;
178 struct proc *p = td->td_proc;
179 struct vnode **vpp = ap->a_vpp;
180 struct vnode *dvp = ap->a_dvp;
181 char *pname = cnp->cn_nameptr;
182 int nlen = cnp->cn_namelen;
183 int nfiles;
184 u_int fd;
185 int error;
186 struct vnode *fvp;
188 KKASSERT(p);
189 nfiles = p->p_fd->fd_nfiles;
190 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
191 error = EROFS;
192 goto bad;
195 VOP_UNLOCK(dvp, 0, td);
196 if (cnp->cn_namelen == 1 && *pname == '.') {
197 *vpp = dvp;
198 vref(dvp);
199 vn_lock(dvp, LK_SHARED | LK_RETRY, td);
200 return (0);
203 if (VTOFDESC(dvp)->fd_type != Froot) {
204 error = ENOTDIR;
205 goto bad;
208 fd = 0;
209 /* the only time a leading 0 is acceptable is if it's "0" */
210 if (*pname == '0' && nlen != 1) {
211 error = ENOENT;
212 goto bad;
214 while (nlen--) {
215 if (*pname < '0' || *pname > '9') {
216 error = ENOENT;
217 goto bad;
219 fd = 10 * fd + *pname++ - '0';
222 if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
223 error = EBADF;
224 goto bad;
227 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, td);
228 if (error)
229 goto bad;
230 VTOFDESC(fvp)->fd_fd = fd;
231 vn_lock(fvp, LK_SHARED | LK_RETRY, td);
232 *vpp = fvp;
233 return (0);
235 bad:
236 vn_lock(dvp, LK_SHARED | LK_RETRY, td);
237 *vpp = NULL;
238 return (error);
242 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
243 * struct thread *a_td)
245 static int
246 fdesc_open(struct vop_open_args *ap)
248 struct vnode *vp = ap->a_vp;
249 struct proc *p = ap->a_td->td_proc;
251 KKASSERT(p);
253 if (VTOFDESC(vp)->fd_type == Froot)
254 return (0);
257 * XXX Kludge: set p->p_dupfd to contain the value of the the file
258 * descriptor being sought for duplication. The error return ensures
259 * that the vnode for this device will be released by vn_open. Open
260 * will detect this special error and take the actions in dupfdopen.
261 * Other callers of vn_open or VOP_OPEN will simply report the
262 * error.
264 p->p_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
265 return (ENODEV);
269 * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap,
270 * struct ucred *a_cred, struct thread *a_td)
272 static int
273 fdesc_getattr(struct vop_getattr_args *ap)
275 struct proc *p = ap->a_td->td_proc;
276 struct vnode *vp = ap->a_vp;
277 struct vattr *vap = ap->a_vap;
278 struct filedesc *fdp;
279 struct file *fp;
280 struct stat stb;
281 u_int fd;
282 int error = 0;
284 KKASSERT(p);
285 fdp = p->p_fd;
286 switch (VTOFDESC(vp)->fd_type) {
287 case Froot:
288 VATTR_NULL(vap);
290 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
291 vap->va_type = VDIR;
292 vap->va_nlink = 2;
293 vap->va_size = DEV_BSIZE;
294 vap->va_fileid = VTOFDESC(vp)->fd_ix;
295 vap->va_uid = 0;
296 vap->va_gid = 0;
297 vap->va_blocksize = DEV_BSIZE;
298 vap->va_atime.tv_sec = boottime.tv_sec;
299 vap->va_atime.tv_nsec = 0;
300 vap->va_mtime = vap->va_atime;
301 vap->va_ctime = vap->va_mtime;
302 vap->va_gen = 0;
303 vap->va_flags = 0;
304 vap->va_rdev = 0;
305 vap->va_bytes = 0;
306 break;
308 case Fdesc:
309 fd = VTOFDESC(vp)->fd_fd;
311 if (fd >= fdp->fd_nfiles || (fp = fdp->fd_files[fd].fp) == NULL)
312 return (EBADF);
314 bzero(&stb, sizeof(stb));
315 error = fo_stat(fp, &stb, ap->a_td);
316 if (error == 0) {
317 VATTR_NULL(vap);
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));
323 #undef FDRX
324 vap->va_nlink = 1;
325 vap->va_flags = 0;
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_rdev = stb.st_rdev;
333 * If no time data is provided, use the current time.
335 if (stb.st_atimespec.tv_sec == 0 &&
336 stb.st_atimespec.tv_nsec == 0)
337 nanotime(&stb.st_atimespec);
339 if (stb.st_ctimespec.tv_sec == 0 &&
340 stb.st_ctimespec.tv_nsec == 0)
341 nanotime(&stb.st_ctimespec);
343 if (stb.st_mtimespec.tv_sec == 0 &&
344 stb.st_mtimespec.tv_nsec == 0)
345 nanotime(&stb.st_mtimespec);
347 vap->va_atime = stb.st_atimespec;
348 vap->va_mtime = stb.st_mtimespec;
349 vap->va_ctime = stb.st_ctimespec;
350 vap->va_uid = stb.st_uid;
351 vap->va_gid = stb.st_gid;
353 break;
355 default:
356 panic("fdesc_getattr");
357 break;
360 if (error == 0)
361 vp->v_type = vap->va_type;
362 return (error);
366 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
367 * struct ucred *a_cred, struct thread *a_td)
369 static int
370 fdesc_setattr(struct vop_setattr_args *ap)
372 struct proc *p = ap->a_td->td_proc;
373 struct vattr *vap = ap->a_vap;
374 struct file *fp;
375 unsigned fd;
376 int error;
379 * Can't mess with the root vnode
381 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
382 return (EACCES);
384 fd = VTOFDESC(ap->a_vp)->fd_fd;
385 KKASSERT(p);
388 * Allow setattr where there is an underlying vnode.
390 error = getvnode(p->p_fd, fd, &fp);
391 if (error) {
393 * getvnode() returns EINVAL if the file descriptor is not
394 * backed by a vnode. Silently drop all changes except
395 * chflags(2) in this case.
397 if (error == EINVAL) {
398 if (vap->va_flags != VNOVAL)
399 error = EOPNOTSUPP;
400 else
401 error = 0;
403 return (error);
405 return (error);
408 #define UIO_MX 16
411 * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
412 * int *a_eofflag, u_long *a_cookies, int a_ncookies)
414 static int
415 fdesc_readdir(struct vop_readdir_args *ap)
417 struct uio *uio = ap->a_uio;
418 struct filedesc *fdp;
419 int error, i, fcnt;
420 size_t namelen;
421 char name[20]; /* enough for %d */
424 * We don't allow exporting fdesc mounts, and currently local
425 * requests do not need cookies.
427 if (ap->a_ncookies)
428 panic("fdesc_readdir: not hungry");
430 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
431 panic("fdesc_readdir: not dir");
433 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX ||
434 uio->uio_resid < 0)
435 return(EINVAL);
436 i = uio->uio_offset;
437 KKASSERT(uio->uio_td->td_proc);
438 fdp = uio->uio_td->td_proc->p_fd;
439 error = 0;
441 fcnt = i - 2; /* The first two nodes are `.' and `..' */
443 while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
444 switch (i) {
445 case 0: /* `.' */
446 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
447 1, "."))
448 goto done;
449 if (error)
450 return (error);
451 break;
452 case 1: /* `..' */
453 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
454 2, ".."))
455 goto done;
456 if (error)
457 return (error);
458 break;
459 default:
460 if (fdp->fd_files[fcnt].fp == NULL) {
461 fcnt++;
462 continue;
465 namelen = snprintf(name, sizeof(name), "%d", fcnt);
466 if (vop_write_dirent(&error, uio, FD_ROOT + i,
467 DT_UNKNOWN, namelen, name))
468 goto done;
469 if (error)
470 return (error);
471 break;
473 i++;
474 fcnt++;
477 done:
478 if (i >= 2)
479 uio->uio_offset = fcnt + 2;
480 else
481 uio->uio_offset = i;
482 return (error);
486 * fdesc_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
487 * struct thread *a_td)
489 static int
490 fdesc_poll(struct vop_poll_args *ap)
492 return seltrue(0, ap->a_events, ap->a_td);
496 * fdesc_inactive(struct vnode *a_vp, struct thread *a_td)
498 static int
499 fdesc_inactive(struct vop_inactive_args *ap)
501 struct vnode *vp = ap->a_vp;
504 * Clear out the v_type field to avoid
505 * nasty things happening in vgone().
507 vp->v_type = VNON;
508 return (0);
512 * fdesc_reclaim(struct vnode *a_vp)
514 static int
515 fdesc_reclaim(struct vop_reclaim_args *ap)
517 struct vnode *vp = ap->a_vp;
518 struct fdescnode *fd = VTOFDESC(vp);
520 LIST_REMOVE(fd, fd_hash);
521 FREE(vp->v_data, M_TEMP);
522 vp->v_data = 0;
524 return (0);
528 * Print out the contents of a /dev/fd vnode.
530 * fdesc_print(struct vnode *a_vp)
532 /* ARGSUSED */
533 static int
534 fdesc_print(struct vop_print_args *ap)
536 printf("tag VT_NON, fdesc vnode\n");
537 return (0);
540 struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
541 { &vop_default_desc, vop_defaultop },
542 { &vop_access_desc, vop_null },
543 { &vop_getattr_desc, (vnodeopv_entry_t) fdesc_getattr },
544 { &vop_inactive_desc, (vnodeopv_entry_t) fdesc_inactive },
545 { &vop_lookup_desc, (vnodeopv_entry_t) fdesc_lookup },
546 { &vop_open_desc, (vnodeopv_entry_t) fdesc_open },
547 { &vop_pathconf_desc, (vnodeopv_entry_t) vop_stdpathconf },
548 { &vop_poll_desc, (vnodeopv_entry_t) fdesc_poll },
549 { &vop_print_desc, (vnodeopv_entry_t) fdesc_print },
550 { &vop_readdir_desc, (vnodeopv_entry_t) fdesc_readdir },
551 { &vop_reclaim_desc, (vnodeopv_entry_t) fdesc_reclaim },
552 { &vop_setattr_desc, (vnodeopv_entry_t) fdesc_setattr },
553 { NULL, NULL }