Add IFF_MONITOR support.
[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 $
efda3bd0 39 * $DragonFly: src/sys/vfs/fdesc/fdesc_vnops.c,v 1.34 2006/09/05 00:55: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);
84static int fdesc_poll (struct vop_poll_args *ap);
85static int fdesc_setattr (struct vop_setattr_args *ap);
984263bc
MD
86
87/*
88 * Initialise cache headers
89 */
90int
130da09f 91fdesc_init(struct vfsconf *vfsp)
984263bc
MD
92{
93
94 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
95 return (0);
96}
97
43c45e8f
HP
98int
99fdesc_uninit(struct vfsconf *vfsp)
100{
101 if (fdhashtbl)
efda3bd0 102 kfree(fdhashtbl, M_CACHE);
43c45e8f
HP
103 return (0);
104}
984263bc 105int
acde96db 106fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
984263bc
MD
107{
108 struct fdhashhead *fc;
109 struct fdescnode *fd;
110 int error = 0;
111
112 fc = FD_NHASH(ix);
113loop:
114 LIST_FOREACH(fd, fc, fd_hash) {
115 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
87de5057 116 if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
984263bc
MD
117 goto loop;
118 *vpp = fd->fd_vnode;
119 return (error);
120 }
121 }
122
123 /*
124 * otherwise lock the array while we call getnewvnode
125 * since that can block.
126 */
127 if (fdcache_lock & FDL_LOCKED) {
128 fdcache_lock |= FDL_WANT;
b1ce5639 129 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
984263bc
MD
130 goto loop;
131 }
132 fdcache_lock |= FDL_LOCKED;
133
134 /*
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.
138 */
139 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
140
6ddb7618 141 error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
984263bc
MD
142 if (error) {
143 FREE(fd, M_TEMP);
144 goto out;
145 }
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);
5fd012e0 152 vx_unlock(*vpp);
984263bc
MD
153
154out:
155 fdcache_lock &= ~FDL_LOCKED;
156
157 if (fdcache_lock & FDL_WANT) {
158 fdcache_lock &= ~FDL_WANT;
159 wakeup((caddr_t) &fdcache_lock);
160 }
161
162 return (error);
163}
164
165/*
166 * vp is the current namei directory
167 * ndp is the name to locate in that directory...
130da09f
CP
168 *
169 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
170 * struct componentname *a_cnp)
984263bc
MD
171 */
172static int
e62afb5f 173fdesc_lookup(struct vop_old_lookup_args *ap)
984263bc 174{
dadab5e9
MD
175 struct componentname *cnp = ap->a_cnp;
176 struct thread *td = cnp->cn_td;
177 struct proc *p = td->td_proc;
984263bc
MD
178 struct vnode **vpp = ap->a_vpp;
179 struct vnode *dvp = ap->a_dvp;
984263bc 180 char *pname = cnp->cn_nameptr;
984263bc 181 int nlen = cnp->cn_namelen;
dadab5e9 182 int nfiles;
984263bc
MD
183 u_int fd;
184 int error;
185 struct vnode *fvp;
186
dadab5e9
MD
187 KKASSERT(p);
188 nfiles = p->p_fd->fd_nfiles;
2b69e610 189 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
984263bc
MD
190 error = EROFS;
191 goto bad;
192 }
193
a11aaa81 194 vn_unlock(dvp);
984263bc
MD
195 if (cnp->cn_namelen == 1 && *pname == '.') {
196 *vpp = dvp;
597aea93 197 vref(dvp);
ca466bae 198 vn_lock(dvp, LK_SHARED | LK_RETRY);
984263bc
MD
199 return (0);
200 }
201
202 if (VTOFDESC(dvp)->fd_type != Froot) {
203 error = ENOTDIR;
204 goto bad;
205 }
206
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;
212 }
213 while (nlen--) {
214 if (*pname < '0' || *pname > '9') {
215 error = ENOENT;
216 goto bad;
217 }
218 fd = 10 * fd + *pname++ - '0';
219 }
220
0679adc4 221 if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
984263bc
MD
222 error = EBADF;
223 goto bad;
224 }
225
acde96db 226 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
984263bc
MD
227 if (error)
228 goto bad;
229 VTOFDESC(fvp)->fd_fd = fd;
ca466bae 230 vn_lock(fvp, LK_SHARED | LK_RETRY);
984263bc
MD
231 *vpp = fvp;
232 return (0);
233
234bad:
ca466bae 235 vn_lock(dvp, LK_SHARED | LK_RETRY);
984263bc
MD
236 *vpp = NULL;
237 return (error);
238}
239
130da09f 240/*
87de5057 241 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
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;
300 vap->va_rdev = 0;
301 vap->va_bytes = 0;
302 break;
303
304 case Fdesc:
305 fd = VTOFDESC(vp)->fd_fd;
306
228b401d
MD
307 fp = holdfp(p->p_fd, fd, -1);
308 if (fp == NULL)
984263bc
MD
309 return (EBADF);
310
311 bzero(&stb, sizeof(stb));
87de5057 312 error = fo_stat(fp, &stb, curproc->p_ucred);
228b401d
MD
313 fdrop(fp);
314
984263bc
MD
315 if (error == 0) {
316 VATTR_NULL(vap);
317 vap->va_type = IFTOVT(stb.st_mode);
318 vap->va_mode = stb.st_mode;
319#define FDRX (VREAD|VEXEC)
320 if (vap->va_type == VDIR)
321 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
322#undef FDRX
323 vap->va_nlink = 1;
324 vap->va_flags = 0;
325 vap->va_bytes = stb.st_blocks * stb.st_blksize;
326 vap->va_fileid = VTOFDESC(vp)->fd_ix;
327 vap->va_size = stb.st_size;
328 vap->va_blocksize = stb.st_blksize;
329 vap->va_rdev = stb.st_rdev;
330
331 /*
332 * If no time data is provided, use the current time.
333 */
334 if (stb.st_atimespec.tv_sec == 0 &&
335 stb.st_atimespec.tv_nsec == 0)
336 nanotime(&stb.st_atimespec);
337
338 if (stb.st_ctimespec.tv_sec == 0 &&
339 stb.st_ctimespec.tv_nsec == 0)
340 nanotime(&stb.st_ctimespec);
341
342 if (stb.st_mtimespec.tv_sec == 0 &&
343 stb.st_mtimespec.tv_nsec == 0)
344 nanotime(&stb.st_mtimespec);
345
346 vap->va_atime = stb.st_atimespec;
347 vap->va_mtime = stb.st_mtimespec;
348 vap->va_ctime = stb.st_ctimespec;
349 vap->va_uid = stb.st_uid;
350 vap->va_gid = stb.st_gid;
351 }
352 break;
353
354 default:
355 panic("fdesc_getattr");
356 break;
357 }
358
359 if (error == 0)
360 vp->v_type = vap->va_type;
361 return (error);
362}
363
130da09f
CP
364/*
365 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
366 * struct ucred *a_cred, struct thread *a_td)
367 */
984263bc 368static int
130da09f 369fdesc_setattr(struct vop_setattr_args *ap)
984263bc 370{
87de5057 371 struct proc *p = curproc;
984263bc
MD
372 struct vattr *vap = ap->a_vap;
373 struct file *fp;
374 unsigned fd;
375 int error;
376
377 /*
378 * Can't mess with the root vnode
379 */
380 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
381 return (EACCES);
382
383 fd = VTOFDESC(ap->a_vp)->fd_fd;
dadab5e9 384 KKASSERT(p);
984263bc
MD
385
386 /*
387 * Allow setattr where there is an underlying vnode.
388 */
5b287bba 389 error = holdvnode(p->p_fd, fd, &fp);
984263bc
MD
390 if (error) {
391 /*
5b287bba 392 * holdvnode() returns EINVAL if the file descriptor is not
984263bc
MD
393 * backed by a vnode. Silently drop all changes except
394 * chflags(2) in this case.
395 */
396 if (error == EINVAL) {
397 if (vap->va_flags != VNOVAL)
398 error = EOPNOTSUPP;
399 else
400 error = 0;
401 }
5b287bba
MD
402 } else {
403 fdrop(fp);
984263bc
MD
404 }
405 return (error);
406}
407
408#define UIO_MX 16
409
130da09f
CP
410/*
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)
413 */
984263bc 414static int
130da09f 415fdesc_readdir(struct vop_readdir_args *ap)
984263bc
MD
416{
417 struct uio *uio = ap->a_uio;
418 struct filedesc *fdp;
709f67da
JS
419 int error, i, fcnt;
420 size_t namelen;
421 char name[20]; /* enough for %d */
984263bc
MD
422
423 /*
424 * We don't allow exporting fdesc mounts, and currently local
425 * requests do not need cookies.
426 */
427 if (ap->a_ncookies)
428 panic("fdesc_readdir: not hungry");
429
430 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
431 panic("fdesc_readdir: not dir");
432
709f67da
JS
433 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX ||
434 uio->uio_resid < 0)
435 return(EINVAL);
436 i = uio->uio_offset;
dadab5e9
MD
437 KKASSERT(uio->uio_td->td_proc);
438 fdp = uio->uio_td->td_proc->p_fd;
984263bc
MD
439 error = 0;
440
441 fcnt = i - 2; /* The first two nodes are `.' and `..' */
442
709f67da 443 while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
984263bc
MD
444 switch (i) {
445 case 0: /* `.' */
709f67da
JS
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;
984263bc 452 case 1: /* `..' */
709f67da
JS
453 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
454 2, ".."))
455 goto done;
456 if (error)
457 return (error);
984263bc
MD
458 break;
459 default:
709f67da
JS
460 if (fdp->fd_files[fcnt].fp == NULL) {
461 fcnt++;
462 continue;
463 }
464
465 namelen = snprintf(name, sizeof(name), "%d", fcnt);
466 if (vop_write_dirent(&error, uio, FD_ROOT + i,
467 DT_UNKNOWN, namelen, name))
984263bc 468 goto done;
709f67da
JS
469 if (error)
470 return (error);
984263bc
MD
471 break;
472 }
984263bc
MD
473 i++;
474 fcnt++;
475 }
476
477done:
709f67da
JS
478 if (i >= 2)
479 uio->uio_offset = fcnt + 2;
480 else
481 uio->uio_offset = i;
984263bc
MD
482 return (error);
483}
484
130da09f
CP
485/*
486 * fdesc_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
487 * struct thread *a_td)
488 */
984263bc 489static int
130da09f 490fdesc_poll(struct vop_poll_args *ap)
984263bc 491{
fef8985e 492 return seltrue(0, ap->a_events);
984263bc
MD
493}
494
130da09f
CP
495/*
496 * fdesc_inactive(struct vnode *a_vp, struct thread *a_td)
497 */
984263bc 498static int
130da09f 499fdesc_inactive(struct vop_inactive_args *ap)
984263bc
MD
500{
501 struct vnode *vp = ap->a_vp;
502
503 /*
504 * Clear out the v_type field to avoid
505 * nasty things happening in vgone().
506 */
984263bc
MD
507 vp->v_type = VNON;
508 return (0);
509}
510
130da09f
CP
511/*
512 * fdesc_reclaim(struct vnode *a_vp)
513 */
984263bc 514static int
130da09f 515fdesc_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
516{
517 struct vnode *vp = ap->a_vp;
518 struct fdescnode *fd = VTOFDESC(vp);
519
520 LIST_REMOVE(fd, fd_hash);
521 FREE(vp->v_data, M_TEMP);
522 vp->v_data = 0;
523
524 return (0);
525}
526
527/*
528 * Print out the contents of a /dev/fd vnode.
130da09f
CP
529 *
530 * fdesc_print(struct vnode *a_vp)
984263bc
MD
531 */
532/* ARGSUSED */
533static int
130da09f 534fdesc_print(struct vop_print_args *ap)
984263bc 535{
984263bc
MD
536 printf("tag VT_NON, fdesc vnode\n");
537 return (0);
538}
539
66a1ddf5
MD
540struct vop_ops fdesc_vnode_vops = {
541 .vop_default = vop_defaultop,
542 .vop_access = (void *)vop_null,
543 .vop_getattr = fdesc_getattr,
544 .vop_inactive = fdesc_inactive,
545 .vop_old_lookup = fdesc_lookup,
546 .vop_open = fdesc_open,
547 .vop_pathconf = vop_stdpathconf,
548 .vop_poll = fdesc_poll,
549 .vop_print = fdesc_print,
550 .vop_readdir = fdesc_readdir,
551 .vop_reclaim = fdesc_reclaim,
552 .vop_setattr = fdesc_setattr
984263bc 553};
984263bc 554