proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[dragonfly.git] / sys / vfs / fdesc / fdesc_vnops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
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.
23 *
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.
35 *
36 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
37 *
38 * $FreeBSD: src/sys/miscfs/fdesc/fdesc_vnops.c,v 1.47.2.1 2001/10/22 22:49:26 chris Exp $
41c20dac 39 * $DragonFly: src/sys/vfs/fdesc/fdesc_vnops.c,v 1.3 2003/06/23 17:55:43 dillon Exp $
984263bc
MD
40 */
41
42/*
43 * /dev/fd Filesystem
44 */
45
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/namei.h>
57#include <sys/proc.h>
58#include <sys/socket.h>
59#include <sys/stat.h>
60#include <sys/vnode.h>
61
62#include <miscfs/fdesc/fdesc.h>
63
64#define FDL_WANT 0x01
65#define FDL_LOCKED 0x02
66static int fdcache_lock;
67
68static vop_t **fdesc_vnodeop_p;
69
70#define NFDCACHE 4
71#define FD_NHASH(ix) \
72 (&fdhashtbl[(ix) & fdhash])
73static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
74static u_long fdhash;
75
76static int fdesc_getattr __P((struct vop_getattr_args *ap));
77static int fdesc_inactive __P((struct vop_inactive_args *ap));
78static int fdesc_lookup __P((struct vop_lookup_args *ap));
79static int fdesc_open __P((struct vop_open_args *ap));
80static int fdesc_print __P((struct vop_print_args *ap));
81static int fdesc_readdir __P((struct vop_readdir_args *ap));
82static int fdesc_reclaim __P((struct vop_reclaim_args *ap));
83static int fdesc_poll __P((struct vop_poll_args *ap));
84static int fdesc_setattr __P((struct vop_setattr_args *ap));
85
86/*
87 * Initialise cache headers
88 */
89int
90fdesc_init(vfsp)
91 struct vfsconf *vfsp;
92{
93
94 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
95 return (0);
96}
97
98int
99fdesc_allocvp(ftype, ix, mp, vpp, p)
100 fdntype ftype;
101 int ix;
102 struct mount *mp;
103 struct vnode **vpp;
104 struct proc *p;
105{
106 struct fdhashhead *fc;
107 struct fdescnode *fd;
108 int error = 0;
109
110 fc = FD_NHASH(ix);
111loop:
112 LIST_FOREACH(fd, fc, fd_hash) {
113 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
114 if (vget(fd->fd_vnode, 0, p))
115 goto loop;
116 *vpp = fd->fd_vnode;
117 return (error);
118 }
119 }
120
121 /*
122 * otherwise lock the array while we call getnewvnode
123 * since that can block.
124 */
125 if (fdcache_lock & FDL_LOCKED) {
126 fdcache_lock |= FDL_WANT;
127 (void) tsleep((caddr_t) &fdcache_lock, PINOD, "fdalvp", 0);
128 goto loop;
129 }
130 fdcache_lock |= FDL_LOCKED;
131
132 /*
133 * Do the MALLOC before the getnewvnode since doing so afterward
134 * might cause a bogus v_data pointer to get dereferenced
135 * elsewhere if MALLOC should block.
136 */
137 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
138
139 error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp);
140 if (error) {
141 FREE(fd, M_TEMP);
142 goto out;
143 }
144 (*vpp)->v_data = fd;
145 fd->fd_vnode = *vpp;
146 fd->fd_type = ftype;
147 fd->fd_fd = -1;
148 fd->fd_ix = ix;
149 LIST_INSERT_HEAD(fc, fd, fd_hash);
150
151out:
152 fdcache_lock &= ~FDL_LOCKED;
153
154 if (fdcache_lock & FDL_WANT) {
155 fdcache_lock &= ~FDL_WANT;
156 wakeup((caddr_t) &fdcache_lock);
157 }
158
159 return (error);
160}
161
162/*
163 * vp is the current namei directory
164 * ndp is the name to locate in that directory...
165 */
166static int
167fdesc_lookup(ap)
168 struct vop_lookup_args /* {
169 struct vnode * a_dvp;
170 struct vnode ** a_vpp;
171 struct componentname * a_cnp;
172 } */ *ap;
173{
174 struct vnode **vpp = ap->a_vpp;
175 struct vnode *dvp = ap->a_dvp;
176 struct componentname *cnp = ap->a_cnp;
177 char *pname = cnp->cn_nameptr;
178 struct proc *p = cnp->cn_proc;
179 int nlen = cnp->cn_namelen;
180 int nfiles = p->p_fd->fd_nfiles;
181 u_int fd;
182 int error;
183 struct vnode *fvp;
184
185 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
186 error = EROFS;
187 goto bad;
188 }
189
190 VOP_UNLOCK(dvp, 0, p);
191 if (cnp->cn_namelen == 1 && *pname == '.') {
192 *vpp = dvp;
193 VREF(dvp);
194 vn_lock(dvp, LK_SHARED | LK_RETRY, p);
195 return (0);
196 }
197
198 if (VTOFDESC(dvp)->fd_type != Froot) {
199 error = ENOTDIR;
200 goto bad;
201 }
202
203 fd = 0;
204 /* the only time a leading 0 is acceptable is if it's "0" */
205 if (*pname == '0' && nlen != 1) {
206 error = ENOENT;
207 goto bad;
208 }
209 while (nlen--) {
210 if (*pname < '0' || *pname > '9') {
211 error = ENOENT;
212 goto bad;
213 }
214 fd = 10 * fd + *pname++ - '0';
215 }
216
217 if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
218 error = EBADF;
219 goto bad;
220 }
221
222 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, p);
223 if (error)
224 goto bad;
225 VTOFDESC(fvp)->fd_fd = fd;
226 vn_lock(fvp, LK_SHARED | LK_RETRY, p);
227 *vpp = fvp;
228 return (0);
229
230bad:
231 vn_lock(dvp, LK_SHARED | LK_RETRY, p);
232 *vpp = NULL;
233 return (error);
234}
235
236static int
237fdesc_open(ap)
238 struct vop_open_args /* {
239 struct vnode *a_vp;
240 int a_mode;
241 struct ucred *a_cred;
242 struct proc *a_p;
243 } */ *ap;
244{
245 struct vnode *vp = ap->a_vp;
246
247 if (VTOFDESC(vp)->fd_type == Froot)
248 return (0);
249
250 /*
251 * XXX Kludge: set p->p_dupfd to contain the value of the the file
252 * descriptor being sought for duplication. The error return ensures
253 * that the vnode for this device will be released by vn_open. Open
254 * will detect this special error and take the actions in dupfdopen.
255 * Other callers of vn_open or VOP_OPEN will simply report the
256 * error.
257 */
258 ap->a_p->p_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
259 return (ENODEV);
260}
261
262static int
263fdesc_getattr(ap)
264 struct vop_getattr_args /* {
265 struct vnode *a_vp;
266 struct vattr *a_vap;
267 struct ucred *a_cred;
268 struct proc *a_p;
269 } */ *ap;
270{
271 struct vnode *vp = ap->a_vp;
272 struct vattr *vap = ap->a_vap;
273 struct filedesc *fdp = ap->a_p->p_fd;
274 struct file *fp;
275 struct stat stb;
276 u_int fd;
277 int error = 0;
278
279 switch (VTOFDESC(vp)->fd_type) {
280 case Froot:
281 VATTR_NULL(vap);
282
283 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
284 vap->va_type = VDIR;
285 vap->va_nlink = 2;
286 vap->va_size = DEV_BSIZE;
287 vap->va_fileid = VTOFDESC(vp)->fd_ix;
288 vap->va_uid = 0;
289 vap->va_gid = 0;
290 vap->va_blocksize = DEV_BSIZE;
291 vap->va_atime.tv_sec = boottime.tv_sec;
292 vap->va_atime.tv_nsec = 0;
293 vap->va_mtime = vap->va_atime;
294 vap->va_ctime = vap->va_mtime;
295 vap->va_gen = 0;
296 vap->va_flags = 0;
297 vap->va_rdev = 0;
298 vap->va_bytes = 0;
299 break;
300
301 case Fdesc:
302 fd = VTOFDESC(vp)->fd_fd;
303
304 if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
305 return (EBADF);
306
307 bzero(&stb, sizeof(stb));
308 error = fo_stat(fp, &stb, ap->a_p);
309 if (error == 0) {
310 VATTR_NULL(vap);
311 vap->va_type = IFTOVT(stb.st_mode);
312 vap->va_mode = stb.st_mode;
313#define FDRX (VREAD|VEXEC)
314 if (vap->va_type == VDIR)
315 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
316#undef FDRX
317 vap->va_nlink = 1;
318 vap->va_flags = 0;
319 vap->va_bytes = stb.st_blocks * stb.st_blksize;
320 vap->va_fileid = VTOFDESC(vp)->fd_ix;
321 vap->va_size = stb.st_size;
322 vap->va_blocksize = stb.st_blksize;
323 vap->va_rdev = stb.st_rdev;
324
325 /*
326 * If no time data is provided, use the current time.
327 */
328 if (stb.st_atimespec.tv_sec == 0 &&
329 stb.st_atimespec.tv_nsec == 0)
330 nanotime(&stb.st_atimespec);
331
332 if (stb.st_ctimespec.tv_sec == 0 &&
333 stb.st_ctimespec.tv_nsec == 0)
334 nanotime(&stb.st_ctimespec);
335
336 if (stb.st_mtimespec.tv_sec == 0 &&
337 stb.st_mtimespec.tv_nsec == 0)
338 nanotime(&stb.st_mtimespec);
339
340 vap->va_atime = stb.st_atimespec;
341 vap->va_mtime = stb.st_mtimespec;
342 vap->va_ctime = stb.st_ctimespec;
343 vap->va_uid = stb.st_uid;
344 vap->va_gid = stb.st_gid;
345 }
346 break;
347
348 default:
349 panic("fdesc_getattr");
350 break;
351 }
352
353 if (error == 0)
354 vp->v_type = vap->va_type;
355 return (error);
356}
357
358static int
359fdesc_setattr(ap)
360 struct vop_setattr_args /* {
361 struct vnode *a_vp;
362 struct vattr *a_vap;
363 struct ucred *a_cred;
364 struct proc *a_p;
365 } */ *ap;
366{
367 struct vattr *vap = ap->a_vap;
368 struct file *fp;
369 unsigned fd;
370 int error;
371
372 /*
373 * Can't mess with the root vnode
374 */
375 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
376 return (EACCES);
377
378 fd = VTOFDESC(ap->a_vp)->fd_fd;
379
380 /*
381 * Allow setattr where there is an underlying vnode.
382 */
383 error = getvnode(ap->a_p->p_fd, fd, &fp);
384 if (error) {
385 /*
386 * getvnode() returns EINVAL if the file descriptor is not
387 * backed by a vnode. Silently drop all changes except
388 * chflags(2) in this case.
389 */
390 if (error == EINVAL) {
391 if (vap->va_flags != VNOVAL)
392 error = EOPNOTSUPP;
393 else
394 error = 0;
395 }
396 return (error);
397 }
398 return (error);
399}
400
401#define UIO_MX 16
402
403static int
404fdesc_readdir(ap)
405 struct vop_readdir_args /* {
406 struct vnode *a_vp;
407 struct uio *a_uio;
408 struct ucred *a_cred;
409 int *a_eofflag;
410 u_long *a_cookies;
411 int a_ncookies;
412 } */ *ap;
413{
414 struct uio *uio = ap->a_uio;
415 struct filedesc *fdp;
416 struct dirent d;
417 struct dirent *dp = &d;
418 int error, i, off, fcnt;
419
420 /*
421 * We don't allow exporting fdesc mounts, and currently local
422 * requests do not need cookies.
423 */
424 if (ap->a_ncookies)
425 panic("fdesc_readdir: not hungry");
426
427 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
428 panic("fdesc_readdir: not dir");
429
430 off = (int)uio->uio_offset;
431 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
432 uio->uio_resid < UIO_MX)
433 return (EINVAL);
434 i = (u_int)off / UIO_MX;
435 fdp = uio->uio_procp->p_fd;
436 error = 0;
437
438 fcnt = i - 2; /* The first two nodes are `.' and `..' */
439
440 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
441 switch (i) {
442 case 0: /* `.' */
443 case 1: /* `..' */
444 bzero((caddr_t)dp, UIO_MX);
445
446 dp->d_fileno = i + FD_ROOT;
447 dp->d_namlen = i + 1;
448 dp->d_reclen = UIO_MX;
449 bcopy("..", dp->d_name, dp->d_namlen);
450 dp->d_name[i + 1] = '\0';
451 dp->d_type = DT_DIR;
452 break;
453 default:
454 if (fdp->fd_ofiles[fcnt] == NULL)
455 goto done;
456
457 bzero((caddr_t) dp, UIO_MX);
458 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
459 dp->d_reclen = UIO_MX;
460 dp->d_type = DT_UNKNOWN;
461 dp->d_fileno = i + FD_DESC;
462 break;
463 }
464 /*
465 * And ship to userland
466 */
467 error = uiomove((caddr_t) dp, UIO_MX, uio);
468 if (error)
469 break;
470 i++;
471 fcnt++;
472 }
473
474done:
475 uio->uio_offset = i * UIO_MX;
476 return (error);
477}
478
479static int
480fdesc_poll(ap)
481 struct vop_poll_args /* {
482 struct vnode *a_vp;
483 int a_events;
484 struct ucred *a_cred;
485 struct proc *a_p;
486 } */ *ap;
487{
41c20dac 488 return seltrue(0, ap->a_events, ap->a_p->p_thread);
984263bc
MD
489}
490
491static int
492fdesc_inactive(ap)
493 struct vop_inactive_args /* {
494 struct vnode *a_vp;
495 struct proc *a_p;
496 } */ *ap;
497{
498 struct vnode *vp = ap->a_vp;
499
500 /*
501 * Clear out the v_type field to avoid
502 * nasty things happening in vgone().
503 */
504 VOP_UNLOCK(vp, 0, ap->a_p);
505 vp->v_type = VNON;
506 return (0);
507}
508
509static int
510fdesc_reclaim(ap)
511 struct vop_reclaim_args /* {
512 struct vnode *a_vp;
513 } */ *ap;
514{
515 struct vnode *vp = ap->a_vp;
516 struct fdescnode *fd = VTOFDESC(vp);
517
518 LIST_REMOVE(fd, fd_hash);
519 FREE(vp->v_data, M_TEMP);
520 vp->v_data = 0;
521
522 return (0);
523}
524
525/*
526 * Print out the contents of a /dev/fd vnode.
527 */
528/* ARGSUSED */
529static int
530fdesc_print(ap)
531 struct vop_print_args /* {
532 struct vnode *a_vp;
533 } */ *ap;
534{
535
536 printf("tag VT_NON, fdesc vnode\n");
537 return (0);
538}
539
540static struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
541 { &vop_default_desc, (vop_t *) vop_defaultop },
542 { &vop_access_desc, (vop_t *) vop_null },
543 { &vop_getattr_desc, (vop_t *) fdesc_getattr },
544 { &vop_inactive_desc, (vop_t *) fdesc_inactive },
545 { &vop_lookup_desc, (vop_t *) fdesc_lookup },
546 { &vop_open_desc, (vop_t *) fdesc_open },
547 { &vop_pathconf_desc, (vop_t *) vop_stdpathconf },
548 { &vop_poll_desc, (vop_t *) fdesc_poll },
549 { &vop_print_desc, (vop_t *) fdesc_print },
550 { &vop_readdir_desc, (vop_t *) fdesc_readdir },
551 { &vop_reclaim_desc, (vop_t *) fdesc_reclaim },
552 { &vop_setattr_desc, (vop_t *) fdesc_setattr },
553 { NULL, NULL }
554};
555static struct vnodeopv_desc fdesc_vnodeop_opv_desc =
556 { &fdesc_vnodeop_p, fdesc_vnodeop_entries };
557
558VNODEOP_SET(fdesc_vnodeop_opv_desc);