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