Update the pciconf(8) database.
[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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)fdesc_vnops.c       8.9 (Berkeley) 1/21/94
33  *
34  * $FreeBSD: src/sys/miscfs/fdesc/fdesc_vnops.c,v 1.47.2.1 2001/10/22 22:49:26 chris Exp $
35  */
36
37 /*
38  * /dev/fd Filesystem
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/dirent.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h> /* boottime */
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/file.h>   /* Must come after sys/malloc.h */
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/namei.h>
53 #include <sys/socket.h>
54 #include <sys/stat.h>
55 #include <sys/vnode.h>
56 #include <sys/file2.h>
57
58 #include <machine/limits.h>
59
60 #include "fdesc.h"
61
62 #define FDL_WANT        0x01
63 #define FDL_LOCKED      0x02
64 static int fdcache_lock;
65
66 #define NFDCACHE 4
67 #define FD_NHASH(ix) \
68         (&fdhashtbl[(ix) & fdhash])
69 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
70 static u_long fdhash;
71
72 static int      fdesc_getattr (struct vop_getattr_args *ap);
73 static int      fdesc_inactive (struct vop_inactive_args *ap);
74 static int      fdesc_lookup (struct vop_old_lookup_args *ap);
75 static int      fdesc_open (struct vop_open_args *ap);
76 static int      fdesc_print (struct vop_print_args *ap);
77 static int      fdesc_readdir (struct vop_readdir_args *ap);
78 static int      fdesc_reclaim (struct vop_reclaim_args *ap);
79 static int      fdesc_setattr (struct vop_setattr_args *ap);
80
81 /*
82  * Initialise cache headers
83  */
84 int
85 fdesc_init(struct vfsconf *vfsp)
86 {
87
88         fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
89         return (0);
90 }
91
92 int
93 fdesc_uninit(struct vfsconf *vfsp)
94 {
95         if (fdhashtbl)
96                 hashdestroy(fdhashtbl, M_CACHE, fdhash);
97         return (0);
98 }
99 int
100 fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
101 {
102         struct fdhashhead *fc;
103         struct fdescnode *fd;
104         int error = 0;
105
106         fc = FD_NHASH(ix);
107 loop:
108         LIST_FOREACH(fd, fc, fd_hash) {
109                 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
110                         if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
111                                 goto loop;
112                         *vpp = fd->fd_vnode;
113                         return (error);
114                 }
115         }
116
117         /*
118          * otherwise lock the array while we call getnewvnode
119          * since that can block.
120          */
121         if (fdcache_lock & FDL_LOCKED) {
122                 fdcache_lock |= FDL_WANT;
123                 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
124                 goto loop;
125         }
126         fdcache_lock |= FDL_LOCKED;
127
128         /*
129          * Do the MALLOC before the getnewvnode since doing so afterward
130          * might cause a bogus v_data pointer to get dereferenced
131          * elsewhere if MALLOC should block.
132          */
133         fd = kmalloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
134
135         error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
136         if (error) {
137                 kfree(fd, M_TEMP);
138                 goto out;
139         }
140         (*vpp)->v_data = fd;
141         fd->fd_vnode = *vpp;
142         fd->fd_type = ftype;
143         fd->fd_fd = -1;
144         fd->fd_ix = ix;
145         LIST_INSERT_HEAD(fc, fd, fd_hash);
146         vx_unlock(*vpp);
147
148 out:
149         fdcache_lock &= ~FDL_LOCKED;
150
151         if (fdcache_lock & FDL_WANT) {
152                 fdcache_lock &= ~FDL_WANT;
153                 wakeup((caddr_t) &fdcache_lock);
154         }
155
156         return (error);
157 }
158
159 /*
160  * vp is the current namei directory
161  * ndp is the name to locate in that directory...
162  *
163  * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
164  *              struct componentname *a_cnp)
165  */
166 static int
167 fdesc_lookup(struct vop_old_lookup_args *ap)
168 {
169         struct componentname *cnp = ap->a_cnp;
170         struct thread *td = cnp->cn_td;
171         struct proc *p = td->td_proc;
172         struct vnode **vpp = ap->a_vpp;
173         struct vnode *dvp = ap->a_dvp;
174         char *pname = cnp->cn_nameptr;
175         int nlen = cnp->cn_namelen;
176         int nfiles;
177         u_int fd;
178         int error;
179         struct vnode *fvp;
180
181         KKASSERT(p);
182         nfiles = p->p_fd->fd_nfiles;
183         if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
184                 error = EROFS;
185                 goto bad;
186         }
187
188         vn_unlock(dvp);
189         if (cnp->cn_namelen == 1 && *pname == '.') {
190                 *vpp = dvp;
191                 vref(dvp);      
192                 vn_lock(dvp, LK_SHARED | LK_RETRY);
193                 return (0);
194         }
195
196         if (VTOFDESC(dvp)->fd_type != Froot) {
197                 error = ENOTDIR;
198                 goto bad;
199         }
200
201         fd = 0;
202         /* the only time a leading 0 is acceptable is if it's "0" */
203         if (*pname == '0' && nlen != 1) {
204                 error = ENOENT;
205                 goto bad;
206         }
207         while (nlen--) {
208                 if (*pname < '0' || *pname > '9') {
209                         error = ENOENT;
210                         goto bad;
211                 }
212                 fd = 10 * fd + *pname++ - '0';
213         }
214
215         if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
216                 error = EBADF;
217                 goto bad;
218         }
219
220         error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
221         if (error)
222                 goto bad;
223         VTOFDESC(fvp)->fd_fd = fd;
224         vn_lock(fvp, LK_SHARED | LK_RETRY);
225         *vpp = fvp;
226         return (0);
227
228 bad:
229         vn_lock(dvp, LK_SHARED | LK_RETRY);
230         *vpp = NULL;
231         return (error);
232 }
233
234 /*
235  * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
236  *            struct file *a_fp)
237  */
238 static int
239 fdesc_open(struct vop_open_args *ap)
240 {
241         struct vnode *vp = ap->a_vp;
242         struct lwp *lp = curthread->td_lwp;
243
244         KKASSERT(lp);
245
246         if (VTOFDESC(vp)->fd_type == Froot)
247                 return (vop_stdopen(ap));
248
249         /*
250          * XXX Kludge: set lp->lwp_dupfd to contain the value of the the file
251          * descriptor being sought for duplication. The error return ensures
252          * that the vnode for this device will be released by vn_open. Open
253          * will detect this special error and take the actions in dupfdopen.
254          * Other callers of vn_open or VOP_OPEN will simply report the
255          * error.
256          */
257         lp->lwp_dupfd = VTOFDESC(vp)->fd_fd;    /* XXX */
258         return (ENODEV);
259 }
260
261 /*
262  * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
263  */
264 static int
265 fdesc_getattr(struct vop_getattr_args *ap)
266 {
267         struct proc *p = curproc;
268         struct vnode *vp = ap->a_vp;
269         struct vattr *vap = ap->a_vap;
270         struct file *fp;
271         struct stat stb;
272         u_int fd;
273         int error = 0;
274
275         KKASSERT(p);
276
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_rmajor = VNOVAL;
296                 vap->va_rminor = VNOVAL;
297                 vap->va_bytes = 0;
298                 break;
299
300         case Fdesc:
301                 fd = VTOFDESC(vp)->fd_fd;
302
303                 fp = holdfp(p->p_fd, fd, -1);
304                 if (fp == NULL)
305                         return (EBADF);
306
307                 bzero(&stb, sizeof(stb));
308                 error = fo_stat(fp, &stb, curproc->p_ucred);
309                 fdrop(fp);
310
311                 if (error == 0) {
312                         VATTR_NULL(vap);
313                         vap->va_type = IFTOVT(stb.st_mode);
314                         vap->va_mode = stb.st_mode;
315 #define FDRX (VREAD|VEXEC)
316                         if (vap->va_type == VDIR)
317                                 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
318 #undef FDRX
319                         vap->va_nlink = 1;
320                         vap->va_flags = 0;
321                         vap->va_bytes = stb.st_blocks * stb.st_blksize;
322                         vap->va_fileid = VTOFDESC(vp)->fd_ix;
323                         vap->va_size = stb.st_size;
324                         vap->va_blocksize = stb.st_blksize;
325                         vap->va_rmajor = umajor(stb.st_rdev);
326                         vap->va_rminor = uminor(stb.st_rdev);
327
328                         /*
329                          * If no time data is provided, use the current time.
330                          */
331                         if (stb.st_atimespec.tv_sec == 0 &&
332                             stb.st_atimespec.tv_nsec == 0)
333                                 nanotime(&stb.st_atimespec);
334
335                         if (stb.st_ctimespec.tv_sec == 0 &&
336                             stb.st_ctimespec.tv_nsec == 0)
337                                 nanotime(&stb.st_ctimespec);
338
339                         if (stb.st_mtimespec.tv_sec == 0 &&
340                             stb.st_mtimespec.tv_nsec == 0)
341                                 nanotime(&stb.st_mtimespec);
342
343                         vap->va_atime = stb.st_atimespec;
344                         vap->va_mtime = stb.st_mtimespec;
345                         vap->va_ctime = stb.st_ctimespec;
346                         vap->va_uid = stb.st_uid;
347                         vap->va_gid = stb.st_gid;
348                 }
349                 break;
350
351         default:
352                 panic("fdesc_getattr");
353                 break;
354         }
355
356         if (error == 0)
357                 vp->v_type = vap->va_type;
358         return (error);
359 }
360
361 /*
362  * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
363  *               struct ucred *a_cred)
364  */
365 static int
366 fdesc_setattr(struct vop_setattr_args *ap)
367 {
368         struct proc *p = curproc;
369         struct vattr *vap = ap->a_vap;
370         struct file *fp;
371         unsigned fd;
372         int error;
373
374         /*
375          * Can't mess with the root vnode
376          */
377         if (VTOFDESC(ap->a_vp)->fd_type == Froot)
378                 return (EACCES);
379
380         fd = VTOFDESC(ap->a_vp)->fd_fd;
381         KKASSERT(p);
382
383         /*
384          * Allow setattr where there is an underlying vnode.
385          */
386         error = holdvnode(p->p_fd, fd, &fp);
387         if (error) {
388                 /*
389                  * holdvnode() returns EINVAL if the file descriptor is not
390                  * backed by a vnode.  Silently drop all changes except
391                  * chflags(2) in this case.
392                  */
393                 if (error == EINVAL) {
394                         if (vap->va_flags != VNOVAL)
395                                 error = EOPNOTSUPP;
396                         else
397                                 error = 0;
398                 }
399         } else {
400                 fdrop(fp);
401         }
402         return (error);
403 }
404
405 #define UIO_MX 16
406
407 /*
408  * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
409  *               int *a_eofflag, off_t *a_cookies, int a_ncookies)
410  */
411 static int
412 fdesc_readdir(struct vop_readdir_args *ap)
413 {
414         struct uio *uio = ap->a_uio;
415         struct filedesc *fdp;
416         int error, i, fcnt;
417         size_t namelen;
418         char name[20]; /* enough for %d */
419
420         /*
421          * We don't allow exporting fdesc mounts, and currently local
422          * requests do not need cookies.
423          */
424         if (ap->a_ncookies)
425                 panic("fdesc_readdir: not hungry");
426
427         if (VTOFDESC(ap->a_vp)->fd_type != Froot)
428                 panic("fdesc_readdir: not dir");
429
430         if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
431                 return(EINVAL);
432         i = (int)uio->uio_offset;
433         KKASSERT(uio->uio_td->td_proc);
434         fdp = uio->uio_td->td_proc->p_fd;
435         error = 0;
436
437         fcnt = i - 2;           /* The first two nodes are `.' and `..' */
438
439         while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
440                 switch (i) {
441                 case 0: /* `.' */
442                         if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
443                                          1, "."))
444                                 goto done;
445                         if (error)
446                                 return (error);
447                         break;
448                 case 1: /* `..' */
449                         if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
450                                              2, ".."))
451                                 goto done;
452                         if (error)
453                                 return (error);
454                         break;
455                 default:
456                         if (fdp->fd_files[fcnt].fp == NULL) {
457                                 fcnt++;
458                                 continue;
459                         }
460
461                         namelen = ksnprintf(name, sizeof(name), "%d", fcnt);
462                         if (vop_write_dirent(&error, uio, FD_ROOT + i,
463                                              DT_UNKNOWN, namelen, name))
464                                 goto done;
465                         if (error)
466                                 return (error);
467                         break;
468                 }
469                 i++;
470                 fcnt++;
471         }
472
473 done:
474         if (i >= 2)
475                 uio->uio_offset = fcnt + 2;
476         else
477                 uio->uio_offset = i;
478         return (error);
479 }
480
481 /*
482  * fdesc_inactive(struct vnode *a_vp)
483  */
484 static int
485 fdesc_inactive(struct vop_inactive_args *ap)
486 {
487         struct vnode *vp = ap->a_vp;
488
489         /*
490          * Clear out the v_type field to avoid
491          * nasty things happening in vgone().
492          */
493         vp->v_type = VNON;
494         return (0);
495 }
496
497 /*
498  * fdesc_reclaim(struct vnode *a_vp)
499  */
500 static int
501 fdesc_reclaim(struct vop_reclaim_args *ap)
502 {
503         struct vnode *vp = ap->a_vp;
504         struct fdescnode *fd = VTOFDESC(vp);
505
506         LIST_REMOVE(fd, fd_hash);
507         kfree(vp->v_data, M_TEMP);
508         vp->v_data = 0;
509
510         return (0);
511 }
512
513 /*
514  * Print out the contents of a /dev/fd vnode.
515  *
516  * fdesc_print(struct vnode *a_vp)
517  */
518 /* ARGSUSED */
519 static int
520 fdesc_print(struct vop_print_args *ap)
521 {
522         kprintf("tag VT_NON, fdesc vnode\n");
523         return (0);
524 }
525
526 struct vop_ops fdesc_vnode_vops = {
527         .vop_default =          vop_defaultop,
528         .vop_access =           (void *)vop_null,
529         .vop_getattr =          fdesc_getattr,
530         .vop_inactive =         fdesc_inactive,
531         .vop_old_lookup =       fdesc_lookup,
532         .vop_open =             fdesc_open,
533         .vop_pathconf =         vop_stdpathconf,
534         .vop_print =            fdesc_print,
535         .vop_readdir =          fdesc_readdir,
536         .vop_reclaim =          fdesc_reclaim,
537         .vop_setattr =          fdesc_setattr
538 };
539