kernel - Tear out vfs polling
[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 $
84009d92 39 * $DragonFly: src/sys/vfs/fdesc/fdesc_vnops.c,v 1.39 2007/11/20 21:03:50 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>
984263bc 56#include <sys/proc.h>
dadab5e9 57#include <sys/namei.h>
984263bc
MD
58#include <sys/socket.h>
59#include <sys/stat.h>
60#include <sys/vnode.h>
dadab5e9 61#include <sys/file2.h>
984263bc 62
709f67da
JS
63#include <machine/limits.h>
64
1f2de5d4 65#include "fdesc.h"
984263bc
MD
66
67#define FDL_WANT 0x01
68#define FDL_LOCKED 0x02
69static int fdcache_lock;
70
984263bc
MD
71#define NFDCACHE 4
72#define FD_NHASH(ix) \
73 (&fdhashtbl[(ix) & fdhash])
74static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
75static u_long fdhash;
76
a6ee311a
RG
77static int fdesc_getattr (struct vop_getattr_args *ap);
78static int fdesc_inactive (struct vop_inactive_args *ap);
e62afb5f 79static int fdesc_lookup (struct vop_old_lookup_args *ap);
a6ee311a
RG
80static int fdesc_open (struct vop_open_args *ap);
81static int fdesc_print (struct vop_print_args *ap);
82static int fdesc_readdir (struct vop_readdir_args *ap);
83static int fdesc_reclaim (struct vop_reclaim_args *ap);
a6ee311a 84static int fdesc_setattr (struct vop_setattr_args *ap);
984263bc
MD
85
86/*
87 * Initialise cache headers
88 */
89int
130da09f 90fdesc_init(struct vfsconf *vfsp)
984263bc
MD
91{
92
93 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
94 return (0);
95}
96
43c45e8f
HP
97int
98fdesc_uninit(struct vfsconf *vfsp)
99{
100 if (fdhashtbl)
efda3bd0 101 kfree(fdhashtbl, M_CACHE);
43c45e8f
HP
102 return (0);
103}
984263bc 104int
acde96db 105fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
984263bc
MD
106{
107 struct fdhashhead *fc;
108 struct fdescnode *fd;
109 int error = 0;
110
111 fc = FD_NHASH(ix);
112loop:
113 LIST_FOREACH(fd, fc, fd_hash) {
114 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
87de5057 115 if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
984263bc
MD
116 goto loop;
117 *vpp = fd->fd_vnode;
118 return (error);
119 }
120 }
121
122 /*
123 * otherwise lock the array while we call getnewvnode
124 * since that can block.
125 */
126 if (fdcache_lock & FDL_LOCKED) {
127 fdcache_lock |= FDL_WANT;
b1ce5639 128 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
984263bc
MD
129 goto loop;
130 }
131 fdcache_lock |= FDL_LOCKED;
132
133 /*
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.
137 */
138 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
139
6ddb7618 140 error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
984263bc
MD
141 if (error) {
142 FREE(fd, M_TEMP);
143 goto out;
144 }
145 (*vpp)->v_data = fd;
146 fd->fd_vnode = *vpp;
147 fd->fd_type = ftype;
148 fd->fd_fd = -1;
149 fd->fd_ix = ix;
150 LIST_INSERT_HEAD(fc, fd, fd_hash);
5fd012e0 151 vx_unlock(*vpp);
984263bc
MD
152
153out:
154 fdcache_lock &= ~FDL_LOCKED;
155
156 if (fdcache_lock & FDL_WANT) {
157 fdcache_lock &= ~FDL_WANT;
158 wakeup((caddr_t) &fdcache_lock);
159 }
160
161 return (error);
162}
163
164/*
165 * vp is the current namei directory
166 * ndp is the name to locate in that directory...
130da09f
CP
167 *
168 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
169 * struct componentname *a_cnp)
984263bc
MD
170 */
171static int
e62afb5f 172fdesc_lookup(struct vop_old_lookup_args *ap)
984263bc 173{
dadab5e9
MD
174 struct componentname *cnp = ap->a_cnp;
175 struct thread *td = cnp->cn_td;
176 struct proc *p = td->td_proc;
984263bc
MD
177 struct vnode **vpp = ap->a_vpp;
178 struct vnode *dvp = ap->a_dvp;
984263bc 179 char *pname = cnp->cn_nameptr;
984263bc 180 int nlen = cnp->cn_namelen;
dadab5e9 181 int nfiles;
984263bc
MD
182 u_int fd;
183 int error;
184 struct vnode *fvp;
185
dadab5e9
MD
186 KKASSERT(p);
187 nfiles = p->p_fd->fd_nfiles;
2b69e610 188 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
984263bc
MD
189 error = EROFS;
190 goto bad;
191 }
192
a11aaa81 193 vn_unlock(dvp);
984263bc
MD
194 if (cnp->cn_namelen == 1 && *pname == '.') {
195 *vpp = dvp;
597aea93 196 vref(dvp);
ca466bae 197 vn_lock(dvp, LK_SHARED | LK_RETRY);
984263bc
MD
198 return (0);
199 }
200
201 if (VTOFDESC(dvp)->fd_type != Froot) {
202 error = ENOTDIR;
203 goto bad;
204 }
205
206 fd = 0;
207 /* the only time a leading 0 is acceptable is if it's "0" */
208 if (*pname == '0' && nlen != 1) {
209 error = ENOENT;
210 goto bad;
211 }
212 while (nlen--) {
213 if (*pname < '0' || *pname > '9') {
214 error = ENOENT;
215 goto bad;
216 }
217 fd = 10 * fd + *pname++ - '0';
218 }
219
0679adc4 220 if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
984263bc
MD
221 error = EBADF;
222 goto bad;
223 }
224
acde96db 225 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
984263bc
MD
226 if (error)
227 goto bad;
228 VTOFDESC(fvp)->fd_fd = fd;
ca466bae 229 vn_lock(fvp, LK_SHARED | LK_RETRY);
984263bc
MD
230 *vpp = fvp;
231 return (0);
232
233bad:
ca466bae 234 vn_lock(dvp, LK_SHARED | LK_RETRY);
984263bc
MD
235 *vpp = NULL;
236 return (error);
237}
238
130da09f 239/*
b478fdce
SW
240 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
241 * struct file *a_fp)
130da09f 242 */
984263bc 243static int
130da09f 244fdesc_open(struct vop_open_args *ap)
984263bc
MD
245{
246 struct vnode *vp = ap->a_vp;
87de5057 247 struct lwp *lp = curthread->td_lwp;
dadab5e9 248
fbfe4e7d 249 KKASSERT(lp);
984263bc
MD
250
251 if (VTOFDESC(vp)->fd_type == Froot)
8ddc6004 252 return (vop_stdopen(ap));
984263bc
MD
253
254 /*
fbfe4e7d 255 * XXX Kludge: set lp->lwp_dupfd to contain the value of the the file
984263bc
MD
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
260 * error.
261 */
fbfe4e7d 262 lp->lwp_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
984263bc
MD
263 return (ENODEV);
264}
265
130da09f 266/*
87de5057 267 * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
130da09f 268 */
984263bc 269static int
130da09f 270fdesc_getattr(struct vop_getattr_args *ap)
984263bc 271{
87de5057 272 struct proc *p = curproc;
984263bc
MD
273 struct vnode *vp = ap->a_vp;
274 struct vattr *vap = ap->a_vap;
984263bc
MD
275 struct file *fp;
276 struct stat stb;
277 u_int fd;
278 int error = 0;
279
dadab5e9 280 KKASSERT(p);
228b401d 281
984263bc
MD
282 switch (VTOFDESC(vp)->fd_type) {
283 case Froot:
284 VATTR_NULL(vap);
285
286 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
287 vap->va_type = VDIR;
288 vap->va_nlink = 2;
289 vap->va_size = DEV_BSIZE;
290 vap->va_fileid = VTOFDESC(vp)->fd_ix;
291 vap->va_uid = 0;
292 vap->va_gid = 0;
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;
298 vap->va_gen = 0;
299 vap->va_flags = 0;
0e9b9130
MD
300 vap->va_rmajor = VNOVAL;
301 vap->va_rminor = VNOVAL;
984263bc
MD
302 vap->va_bytes = 0;
303 break;
304
305 case Fdesc:
306 fd = VTOFDESC(vp)->fd_fd;
307
228b401d
MD
308 fp = holdfp(p->p_fd, fd, -1);
309 if (fp == NULL)
984263bc
MD
310 return (EBADF);
311
312 bzero(&stb, sizeof(stb));
87de5057 313 error = fo_stat(fp, &stb, curproc->p_ucred);
228b401d
MD
314 fdrop(fp);
315
984263bc
MD
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;
0e9b9130
MD
330 vap->va_rmajor = umajor(stb.st_rdev);
331 vap->va_rminor = uminor(stb.st_rdev);
984263bc
MD
332
333 /*
334 * If no time data is provided, use the current time.
335 */
336 if (stb.st_atimespec.tv_sec == 0 &&
337 stb.st_atimespec.tv_nsec == 0)
338 nanotime(&stb.st_atimespec);
339
340 if (stb.st_ctimespec.tv_sec == 0 &&
341 stb.st_ctimespec.tv_nsec == 0)
342 nanotime(&stb.st_ctimespec);
343
344 if (stb.st_mtimespec.tv_sec == 0 &&
345 stb.st_mtimespec.tv_nsec == 0)
346 nanotime(&stb.st_mtimespec);
347
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;
353 }
354 break;
355
356 default:
357 panic("fdesc_getattr");
358 break;
359 }
360
361 if (error == 0)
362 vp->v_type = vap->va_type;
363 return (error);
364}
365
130da09f
CP
366/*
367 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
b478fdce 368 * struct ucred *a_cred)
130da09f 369 */
984263bc 370static int
130da09f 371fdesc_setattr(struct vop_setattr_args *ap)
984263bc 372{
87de5057 373 struct proc *p = curproc;
984263bc
MD
374 struct vattr *vap = ap->a_vap;
375 struct file *fp;
376 unsigned fd;
377 int error;
378
379 /*
380 * Can't mess with the root vnode
381 */
382 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
383 return (EACCES);
384
385 fd = VTOFDESC(ap->a_vp)->fd_fd;
dadab5e9 386 KKASSERT(p);
984263bc
MD
387
388 /*
389 * Allow setattr where there is an underlying vnode.
390 */
5b287bba 391 error = holdvnode(p->p_fd, fd, &fp);
984263bc
MD
392 if (error) {
393 /*
5b287bba 394 * holdvnode() returns EINVAL if the file descriptor is not
984263bc
MD
395 * backed by a vnode. Silently drop all changes except
396 * chflags(2) in this case.
397 */
398 if (error == EINVAL) {
399 if (vap->va_flags != VNOVAL)
400 error = EOPNOTSUPP;
401 else
402 error = 0;
403 }
5b287bba
MD
404 } else {
405 fdrop(fp);
984263bc
MD
406 }
407 return (error);
408}
409
410#define UIO_MX 16
411
130da09f
CP
412/*
413 * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
84009d92 414 * int *a_eofflag, off_t *a_cookies, int a_ncookies)
130da09f 415 */
984263bc 416static int
130da09f 417fdesc_readdir(struct vop_readdir_args *ap)
984263bc
MD
418{
419 struct uio *uio = ap->a_uio;
420 struct filedesc *fdp;
709f67da
JS
421 int error, i, fcnt;
422 size_t namelen;
423 char name[20]; /* enough for %d */
984263bc
MD
424
425 /*
426 * We don't allow exporting fdesc mounts, and currently local
427 * requests do not need cookies.
428 */
429 if (ap->a_ncookies)
430 panic("fdesc_readdir: not hungry");
431
432 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
433 panic("fdesc_readdir: not dir");
434
e54488bb 435 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
709f67da 436 return(EINVAL);
e54488bb 437 i = (int)uio->uio_offset;
dadab5e9
MD
438 KKASSERT(uio->uio_td->td_proc);
439 fdp = uio->uio_td->td_proc->p_fd;
984263bc
MD
440 error = 0;
441
442 fcnt = i - 2; /* The first two nodes are `.' and `..' */
443
709f67da 444 while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
984263bc
MD
445 switch (i) {
446 case 0: /* `.' */
709f67da
JS
447 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
448 1, "."))
449 goto done;
450 if (error)
451 return (error);
452 break;
984263bc 453 case 1: /* `..' */
709f67da
JS
454 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
455 2, ".."))
456 goto done;
457 if (error)
458 return (error);
984263bc
MD
459 break;
460 default:
709f67da
JS
461 if (fdp->fd_files[fcnt].fp == NULL) {
462 fcnt++;
463 continue;
464 }
465
f8c7a42d 466 namelen = ksnprintf(name, sizeof(name), "%d", fcnt);
709f67da
JS
467 if (vop_write_dirent(&error, uio, FD_ROOT + i,
468 DT_UNKNOWN, namelen, name))
984263bc 469 goto done;
709f67da
JS
470 if (error)
471 return (error);
984263bc
MD
472 break;
473 }
984263bc
MD
474 i++;
475 fcnt++;
476 }
477
478done:
709f67da
JS
479 if (i >= 2)
480 uio->uio_offset = fcnt + 2;
481 else
482 uio->uio_offset = i;
984263bc
MD
483 return (error);
484}
485
130da09f 486/*
b478fdce 487 * fdesc_inactive(struct vnode *a_vp)
130da09f 488 */
984263bc 489static int
130da09f 490fdesc_inactive(struct vop_inactive_args *ap)
984263bc
MD
491{
492 struct vnode *vp = ap->a_vp;
493
494 /*
495 * Clear out the v_type field to avoid
496 * nasty things happening in vgone().
497 */
984263bc
MD
498 vp->v_type = VNON;
499 return (0);
500}
501
130da09f
CP
502/*
503 * fdesc_reclaim(struct vnode *a_vp)
504 */
984263bc 505static int
130da09f 506fdesc_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
507{
508 struct vnode *vp = ap->a_vp;
509 struct fdescnode *fd = VTOFDESC(vp);
510
511 LIST_REMOVE(fd, fd_hash);
512 FREE(vp->v_data, M_TEMP);
513 vp->v_data = 0;
514
515 return (0);
516}
517
518/*
519 * Print out the contents of a /dev/fd vnode.
130da09f
CP
520 *
521 * fdesc_print(struct vnode *a_vp)
984263bc
MD
522 */
523/* ARGSUSED */
524static int
130da09f 525fdesc_print(struct vop_print_args *ap)
984263bc 526{
086c1d7e 527 kprintf("tag VT_NON, fdesc vnode\n");
984263bc
MD
528 return (0);
529}
530
66a1ddf5
MD
531struct 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,
66a1ddf5
MD
539 .vop_print = fdesc_print,
540 .vop_readdir = fdesc_readdir,
541 .vop_reclaim = fdesc_reclaim,
542 .vop_setattr = fdesc_setattr
984263bc 543};
984263bc 544