Fix .Os and a .Xr. Expand HISTORY a bit.
[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 $
87de5057 39 * $DragonFly: src/sys/vfs/fdesc/fdesc_vnops.c,v 1.27 2006/05/06 02:43:13 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)
102 free(fdhashtbl, M_CACHE);
103 return (0);
104}
984263bc 105int
130da09f
CP
106fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp,
107 struct thread *td)
984263bc
MD
108{
109 struct fdhashhead *fc;
110 struct fdescnode *fd;
111 int error = 0;
112
113 fc = FD_NHASH(ix);
114loop:
115 LIST_FOREACH(fd, fc, fd_hash) {
116 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
87de5057 117 if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
984263bc
MD
118 goto loop;
119 *vpp = fd->fd_vnode;
120 return (error);
121 }
122 }
123
124 /*
125 * otherwise lock the array while we call getnewvnode
126 * since that can block.
127 */
128 if (fdcache_lock & FDL_LOCKED) {
129 fdcache_lock |= FDL_WANT;
b1ce5639 130 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
984263bc
MD
131 goto loop;
132 }
133 fdcache_lock |= FDL_LOCKED;
134
135 /*
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.
139 */
140 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
141
6ddb7618 142 error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
984263bc
MD
143 if (error) {
144 FREE(fd, M_TEMP);
145 goto out;
146 }
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);
5fd012e0 153 vx_unlock(*vpp);
984263bc
MD
154
155out:
156 fdcache_lock &= ~FDL_LOCKED;
157
158 if (fdcache_lock & FDL_WANT) {
159 fdcache_lock &= ~FDL_WANT;
160 wakeup((caddr_t) &fdcache_lock);
161 }
162
163 return (error);
164}
165
166/*
167 * vp is the current namei directory
168 * ndp is the name to locate in that directory...
130da09f
CP
169 *
170 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
171 * struct componentname *a_cnp)
984263bc
MD
172 */
173static int
e62afb5f 174fdesc_lookup(struct vop_old_lookup_args *ap)
984263bc 175{
dadab5e9
MD
176 struct componentname *cnp = ap->a_cnp;
177 struct thread *td = cnp->cn_td;
178 struct proc *p = td->td_proc;
984263bc
MD
179 struct vnode **vpp = ap->a_vpp;
180 struct vnode *dvp = ap->a_dvp;
984263bc 181 char *pname = cnp->cn_nameptr;
984263bc 182 int nlen = cnp->cn_namelen;
dadab5e9 183 int nfiles;
984263bc
MD
184 u_int fd;
185 int error;
186 struct vnode *fvp;
187
dadab5e9
MD
188 KKASSERT(p);
189 nfiles = p->p_fd->fd_nfiles;
2b69e610 190 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
984263bc
MD
191 error = EROFS;
192 goto bad;
193 }
194
ca466bae 195 VOP_UNLOCK(dvp, 0);
984263bc
MD
196 if (cnp->cn_namelen == 1 && *pname == '.') {
197 *vpp = dvp;
597aea93 198 vref(dvp);
ca466bae 199 vn_lock(dvp, LK_SHARED | LK_RETRY);
984263bc
MD
200 return (0);
201 }
202
203 if (VTOFDESC(dvp)->fd_type != Froot) {
204 error = ENOTDIR;
205 goto bad;
206 }
207
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;
213 }
214 while (nlen--) {
215 if (*pname < '0' || *pname > '9') {
216 error = ENOENT;
217 goto bad;
218 }
219 fd = 10 * fd + *pname++ - '0';
220 }
221
0679adc4 222 if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
984263bc
MD
223 error = EBADF;
224 goto bad;
225 }
226
dadab5e9 227 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, td);
984263bc
MD
228 if (error)
229 goto bad;
230 VTOFDESC(fvp)->fd_fd = fd;
ca466bae 231 vn_lock(fvp, LK_SHARED | LK_RETRY);
984263bc
MD
232 *vpp = fvp;
233 return (0);
234
235bad:
ca466bae 236 vn_lock(dvp, LK_SHARED | LK_RETRY);
984263bc
MD
237 *vpp = NULL;
238 return (error);
239}
240
130da09f 241/*
87de5057 242 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
130da09f 243 */
984263bc 244static int
130da09f 245fdesc_open(struct vop_open_args *ap)
984263bc
MD
246{
247 struct vnode *vp = ap->a_vp;
87de5057 248 struct lwp *lp = curthread->td_lwp;
dadab5e9 249
fbfe4e7d 250 KKASSERT(lp);
984263bc
MD
251
252 if (VTOFDESC(vp)->fd_type == Froot)
8ddc6004 253 return (vop_stdopen(ap));
984263bc
MD
254
255 /*
fbfe4e7d 256 * XXX Kludge: set lp->lwp_dupfd to contain the value of the the file
984263bc
MD
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.
262 */
fbfe4e7d 263 lp->lwp_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
984263bc
MD
264 return (ENODEV);
265}
266
130da09f 267/*
87de5057 268 * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
130da09f 269 */
984263bc 270static int
130da09f 271fdesc_getattr(struct vop_getattr_args *ap)
984263bc 272{
87de5057 273 struct proc *p = curproc;
984263bc
MD
274 struct vnode *vp = ap->a_vp;
275 struct vattr *vap = ap->a_vap;
dadab5e9 276 struct filedesc *fdp;
984263bc
MD
277 struct file *fp;
278 struct stat stb;
279 u_int fd;
280 int error = 0;
281
dadab5e9
MD
282 KKASSERT(p);
283 fdp = p->p_fd;
984263bc
MD
284 switch (VTOFDESC(vp)->fd_type) {
285 case Froot:
286 VATTR_NULL(vap);
287
288 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
289 vap->va_type = VDIR;
290 vap->va_nlink = 2;
291 vap->va_size = DEV_BSIZE;
292 vap->va_fileid = VTOFDESC(vp)->fd_ix;
293 vap->va_uid = 0;
294 vap->va_gid = 0;
295 vap->va_blocksize = DEV_BSIZE;
296 vap->va_atime.tv_sec = boottime.tv_sec;
297 vap->va_atime.tv_nsec = 0;
298 vap->va_mtime = vap->va_atime;
299 vap->va_ctime = vap->va_mtime;
300 vap->va_gen = 0;
301 vap->va_flags = 0;
302 vap->va_rdev = 0;
303 vap->va_bytes = 0;
304 break;
305
306 case Fdesc:
307 fd = VTOFDESC(vp)->fd_fd;
308
0679adc4 309 if (fd >= fdp->fd_nfiles || (fp = fdp->fd_files[fd].fp) == NULL)
984263bc
MD
310 return (EBADF);
311
312 bzero(&stb, sizeof(stb));
87de5057 313 error = fo_stat(fp, &stb, curproc->p_ucred);
984263bc
MD
314 if (error == 0) {
315 VATTR_NULL(vap);
316 vap->va_type = IFTOVT(stb.st_mode);
317 vap->va_mode = stb.st_mode;
318#define FDRX (VREAD|VEXEC)
319 if (vap->va_type == VDIR)
320 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
321#undef FDRX
322 vap->va_nlink = 1;
323 vap->va_flags = 0;
324 vap->va_bytes = stb.st_blocks * stb.st_blksize;
325 vap->va_fileid = VTOFDESC(vp)->fd_ix;
326 vap->va_size = stb.st_size;
327 vap->va_blocksize = stb.st_blksize;
328 vap->va_rdev = stb.st_rdev;
329
330 /*
331 * If no time data is provided, use the current time.
332 */
333 if (stb.st_atimespec.tv_sec == 0 &&
334 stb.st_atimespec.tv_nsec == 0)
335 nanotime(&stb.st_atimespec);
336
337 if (stb.st_ctimespec.tv_sec == 0 &&
338 stb.st_ctimespec.tv_nsec == 0)
339 nanotime(&stb.st_ctimespec);
340
341 if (stb.st_mtimespec.tv_sec == 0 &&
342 stb.st_mtimespec.tv_nsec == 0)
343 nanotime(&stb.st_mtimespec);
344
345 vap->va_atime = stb.st_atimespec;
346 vap->va_mtime = stb.st_mtimespec;
347 vap->va_ctime = stb.st_ctimespec;
348 vap->va_uid = stb.st_uid;
349 vap->va_gid = stb.st_gid;
350 }
351 break;
352
353 default:
354 panic("fdesc_getattr");
355 break;
356 }
357
358 if (error == 0)
359 vp->v_type = vap->va_type;
360 return (error);
361}
362
130da09f
CP
363/*
364 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
365 * struct ucred *a_cred, struct thread *a_td)
366 */
984263bc 367static int
130da09f 368fdesc_setattr(struct vop_setattr_args *ap)
984263bc 369{
87de5057 370 struct proc *p = curproc;
984263bc
MD
371 struct vattr *vap = ap->a_vap;
372 struct file *fp;
373 unsigned fd;
374 int error;
375
376 /*
377 * Can't mess with the root vnode
378 */
379 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
380 return (EACCES);
381
382 fd = VTOFDESC(ap->a_vp)->fd_fd;
dadab5e9 383 KKASSERT(p);
984263bc
MD
384
385 /*
386 * Allow setattr where there is an underlying vnode.
387 */
dadab5e9 388 error = getvnode(p->p_fd, fd, &fp);
984263bc
MD
389 if (error) {
390 /*
391 * getvnode() returns EINVAL if the file descriptor is not
392 * backed by a vnode. Silently drop all changes except
393 * chflags(2) in this case.
394 */
395 if (error == EINVAL) {
396 if (vap->va_flags != VNOVAL)
397 error = EOPNOTSUPP;
398 else
399 error = 0;
400 }
401 return (error);
402 }
403 return (error);
404}
405
406#define UIO_MX 16
407
130da09f
CP
408/*
409 * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
410 * int *a_eofflag, u_long *a_cookies, int a_ncookies)
411 */
984263bc 412static int
130da09f 413fdesc_readdir(struct vop_readdir_args *ap)
984263bc
MD
414{
415 struct uio *uio = ap->a_uio;
416 struct filedesc *fdp;
709f67da
JS
417 int error, i, fcnt;
418 size_t namelen;
419 char name[20]; /* enough for %d */
984263bc
MD
420
421 /*
422 * We don't allow exporting fdesc mounts, and currently local
423 * requests do not need cookies.
424 */
425 if (ap->a_ncookies)
426 panic("fdesc_readdir: not hungry");
427
428 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
429 panic("fdesc_readdir: not dir");
430
709f67da
JS
431 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX ||
432 uio->uio_resid < 0)
433 return(EINVAL);
434 i = uio->uio_offset;
dadab5e9
MD
435 KKASSERT(uio->uio_td->td_proc);
436 fdp = uio->uio_td->td_proc->p_fd;
984263bc
MD
437 error = 0;
438
439 fcnt = i - 2; /* The first two nodes are `.' and `..' */
440
709f67da 441 while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
984263bc
MD
442 switch (i) {
443 case 0: /* `.' */
709f67da
JS
444 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
445 1, "."))
446 goto done;
447 if (error)
448 return (error);
449 break;
984263bc 450 case 1: /* `..' */
709f67da
JS
451 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
452 2, ".."))
453 goto done;
454 if (error)
455 return (error);
984263bc
MD
456 break;
457 default:
709f67da
JS
458 if (fdp->fd_files[fcnt].fp == NULL) {
459 fcnt++;
460 continue;
461 }
462
463 namelen = snprintf(name, sizeof(name), "%d", fcnt);
464 if (vop_write_dirent(&error, uio, FD_ROOT + i,
465 DT_UNKNOWN, namelen, name))
984263bc 466 goto done;
709f67da
JS
467 if (error)
468 return (error);
984263bc
MD
469 break;
470 }
984263bc
MD
471 i++;
472 fcnt++;
473 }
474
475done:
709f67da
JS
476 if (i >= 2)
477 uio->uio_offset = fcnt + 2;
478 else
479 uio->uio_offset = i;
984263bc
MD
480 return (error);
481}
482
130da09f
CP
483/*
484 * fdesc_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
485 * struct thread *a_td)
486 */
984263bc 487static int
130da09f 488fdesc_poll(struct vop_poll_args *ap)
984263bc 489{
87de5057 490 return seltrue(0, ap->a_events, curthread);
984263bc
MD
491}
492
130da09f
CP
493/*
494 * fdesc_inactive(struct vnode *a_vp, struct thread *a_td)
495 */
984263bc 496static int
130da09f 497fdesc_inactive(struct vop_inactive_args *ap)
984263bc
MD
498{
499 struct vnode *vp = ap->a_vp;
500
501 /*
502 * Clear out the v_type field to avoid
503 * nasty things happening in vgone().
504 */
984263bc
MD
505 vp->v_type = VNON;
506 return (0);
507}
508
130da09f
CP
509/*
510 * fdesc_reclaim(struct vnode *a_vp)
511 */
984263bc 512static int
130da09f 513fdesc_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
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.
130da09f
CP
527 *
528 * fdesc_print(struct vnode *a_vp)
984263bc
MD
529 */
530/* ARGSUSED */
531static int
130da09f 532fdesc_print(struct vop_print_args *ap)
984263bc 533{
984263bc
MD
534 printf("tag VT_NON, fdesc vnode\n");
535 return (0);
536}
537
0961aa92 538struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
625ddaba
JS
539 { &vop_default_desc, vop_defaultop },
540 { &vop_access_desc, vop_null },
541 { &vop_getattr_desc, (vnodeopv_entry_t) fdesc_getattr },
542 { &vop_inactive_desc, (vnodeopv_entry_t) fdesc_inactive },
e62afb5f 543 { &vop_old_lookup_desc, (vnodeopv_entry_t) fdesc_lookup },
625ddaba
JS
544 { &vop_open_desc, (vnodeopv_entry_t) fdesc_open },
545 { &vop_pathconf_desc, (vnodeopv_entry_t) vop_stdpathconf },
546 { &vop_poll_desc, (vnodeopv_entry_t) fdesc_poll },
547 { &vop_print_desc, (vnodeopv_entry_t) fdesc_print },
548 { &vop_readdir_desc, (vnodeopv_entry_t) fdesc_readdir },
549 { &vop_reclaim_desc, (vnodeopv_entry_t) fdesc_reclaim },
550 { &vop_setattr_desc, (vnodeopv_entry_t) fdesc_setattr },
984263bc
MD
551 { NULL, NULL }
552};
984263bc 553