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