kernel: Use hashdestroy() to free hash tables allocated with hashinit().
[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  */
40
41 /*
42  * /dev/fd Filesystem
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/dirent.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h> /* boottime */
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/file.h>   /* Must come after sys/malloc.h */
54 #include <sys/mount.h>
55 #include <sys/proc.h>
56 #include <sys/namei.h>
57 #include <sys/socket.h>
58 #include <sys/stat.h>
59 #include <sys/vnode.h>
60 #include <sys/file2.h>
61
62 #include <machine/limits.h>
63
64 #include "fdesc.h"
65
66 #define FDL_WANT        0x01
67 #define FDL_LOCKED      0x02
68 static int fdcache_lock;
69
70 #define NFDCACHE 4
71 #define FD_NHASH(ix) \
72         (&fdhashtbl[(ix) & fdhash])
73 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
74 static u_long fdhash;
75
76 static int      fdesc_getattr (struct vop_getattr_args *ap);
77 static int      fdesc_inactive (struct vop_inactive_args *ap);
78 static int      fdesc_lookup (struct vop_old_lookup_args *ap);
79 static int      fdesc_open (struct vop_open_args *ap);
80 static int      fdesc_print (struct vop_print_args *ap);
81 static int      fdesc_readdir (struct vop_readdir_args *ap);
82 static int      fdesc_reclaim (struct vop_reclaim_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_uninit(struct vfsconf *vfsp)
98 {
99         if (fdhashtbl)
100                 hashdestroy(fdhashtbl, M_CACHE, fdhash);
101         return (0);
102 }
103 int
104 fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
105 {
106         struct fdhashhead *fc;
107         struct fdescnode *fd;
108         int error = 0;
109
110         fc = FD_NHASH(ix);
111 loop:
112         LIST_FOREACH(fd, fc, fd_hash) {
113                 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
114                         if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
115                                 goto loop;
116                         *vpp = fd->fd_vnode;
117                         return (error);
118                 }
119         }
120
121         /*
122          * otherwise lock the array while we call getnewvnode
123          * since that can block.
124          */
125         if (fdcache_lock & FDL_LOCKED) {
126                 fdcache_lock |= FDL_WANT;
127                 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
128                 goto loop;
129         }
130         fdcache_lock |= FDL_LOCKED;
131
132         /*
133          * Do the MALLOC before the getnewvnode since doing so afterward
134          * might cause a bogus v_data pointer to get dereferenced
135          * elsewhere if MALLOC should block.
136          */
137         fd = kmalloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
138
139         error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
140         if (error) {
141                 kfree(fd, M_TEMP);
142                 goto out;
143         }
144         (*vpp)->v_data = fd;
145         fd->fd_vnode = *vpp;
146         fd->fd_type = ftype;
147         fd->fd_fd = -1;
148         fd->fd_ix = ix;
149         LIST_INSERT_HEAD(fc, fd, fd_hash);
150         vx_unlock(*vpp);
151
152 out:
153         fdcache_lock &= ~FDL_LOCKED;
154
155         if (fdcache_lock & FDL_WANT) {
156                 fdcache_lock &= ~FDL_WANT;
157                 wakeup((caddr_t) &fdcache_lock);
158         }
159
160         return (error);
161 }
162
163 /*
164  * vp is the current namei directory
165  * ndp is the name to locate in that directory...
166  *
167  * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
168  *              struct componentname *a_cnp)
169  */
170 static int
171 fdesc_lookup(struct vop_old_lookup_args *ap)
172 {
173         struct componentname *cnp = ap->a_cnp;
174         struct thread *td = cnp->cn_td;
175         struct proc *p = td->td_proc;
176         struct vnode **vpp = ap->a_vpp;
177         struct vnode *dvp = ap->a_dvp;
178         char *pname = cnp->cn_nameptr;
179         int nlen = cnp->cn_namelen;
180         int nfiles;
181         u_int fd;
182         int error;
183         struct vnode *fvp;
184
185         KKASSERT(p);
186         nfiles = p->p_fd->fd_nfiles;
187         if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
188                 error = EROFS;
189                 goto bad;
190         }
191
192         vn_unlock(dvp);
193         if (cnp->cn_namelen == 1 && *pname == '.') {
194                 *vpp = dvp;
195                 vref(dvp);      
196                 vn_lock(dvp, LK_SHARED | LK_RETRY);
197                 return (0);
198         }
199
200         if (VTOFDESC(dvp)->fd_type != Froot) {
201                 error = ENOTDIR;
202                 goto bad;
203         }
204
205         fd = 0;
206         /* the only time a leading 0 is acceptable is if it's "0" */
207         if (*pname == '0' && nlen != 1) {
208                 error = ENOENT;
209                 goto bad;
210         }
211         while (nlen--) {
212                 if (*pname < '0' || *pname > '9') {
213                         error = ENOENT;
214                         goto bad;
215                 }
216                 fd = 10 * fd + *pname++ - '0';
217         }
218
219         if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
220                 error = EBADF;
221                 goto bad;
222         }
223
224         error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
225         if (error)
226                 goto bad;
227         VTOFDESC(fvp)->fd_fd = fd;
228         vn_lock(fvp, LK_SHARED | LK_RETRY);
229         *vpp = fvp;
230         return (0);
231
232 bad:
233         vn_lock(dvp, LK_SHARED | LK_RETRY);
234         *vpp = NULL;
235         return (error);
236 }
237
238 /*
239  * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
240  *            struct file *a_fp)
241  */
242 static int
243 fdesc_open(struct vop_open_args *ap)
244 {
245         struct vnode *vp = ap->a_vp;
246         struct lwp *lp = curthread->td_lwp;
247
248         KKASSERT(lp);
249
250         if (VTOFDESC(vp)->fd_type == Froot)
251                 return (vop_stdopen(ap));
252
253         /*
254          * XXX Kludge: set lp->lwp_dupfd to contain the value of the the file
255          * descriptor being sought for duplication. The error return ensures
256          * that the vnode for this device will be released by vn_open. Open
257          * will detect this special error and take the actions in dupfdopen.
258          * Other callers of vn_open or VOP_OPEN will simply report the
259          * error.
260          */
261         lp->lwp_dupfd = VTOFDESC(vp)->fd_fd;    /* XXX */
262         return (ENODEV);
263 }
264
265 /*
266  * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
267  */
268 static int
269 fdesc_getattr(struct vop_getattr_args *ap)
270 {
271         struct proc *p = curproc;
272         struct vnode *vp = ap->a_vp;
273         struct vattr *vap = ap->a_vap;
274         struct file *fp;
275         struct stat stb;
276         u_int fd;
277         int error = 0;
278
279         KKASSERT(p);
280
281         switch (VTOFDESC(vp)->fd_type) {
282         case Froot:
283                 VATTR_NULL(vap);
284
285                 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
286                 vap->va_type = VDIR;
287                 vap->va_nlink = 2;
288                 vap->va_size = DEV_BSIZE;
289                 vap->va_fileid = VTOFDESC(vp)->fd_ix;
290                 vap->va_uid = 0;
291                 vap->va_gid = 0;
292                 vap->va_blocksize = DEV_BSIZE;
293                 vap->va_atime.tv_sec = boottime.tv_sec;
294                 vap->va_atime.tv_nsec = 0;
295                 vap->va_mtime = vap->va_atime;
296                 vap->va_ctime = vap->va_mtime;
297                 vap->va_gen = 0;
298                 vap->va_flags = 0;
299                 vap->va_rmajor = VNOVAL;
300                 vap->va_rminor = VNOVAL;
301                 vap->va_bytes = 0;
302                 break;
303
304         case Fdesc:
305                 fd = VTOFDESC(vp)->fd_fd;
306
307                 fp = holdfp(p->p_fd, fd, -1);
308                 if (fp == NULL)
309                         return (EBADF);
310
311                 bzero(&stb, sizeof(stb));
312                 error = fo_stat(fp, &stb, curproc->p_ucred);
313                 fdrop(fp);
314
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_rmajor = umajor(stb.st_rdev);
330                         vap->va_rminor = uminor(stb.st_rdev);
331
332                         /*
333                          * If no time data is provided, use the current time.
334                          */
335                         if (stb.st_atimespec.tv_sec == 0 &&
336                             stb.st_atimespec.tv_nsec == 0)
337                                 nanotime(&stb.st_atimespec);
338
339                         if (stb.st_ctimespec.tv_sec == 0 &&
340                             stb.st_ctimespec.tv_nsec == 0)
341                                 nanotime(&stb.st_ctimespec);
342
343                         if (stb.st_mtimespec.tv_sec == 0 &&
344                             stb.st_mtimespec.tv_nsec == 0)
345                                 nanotime(&stb.st_mtimespec);
346
347                         vap->va_atime = stb.st_atimespec;
348                         vap->va_mtime = stb.st_mtimespec;
349                         vap->va_ctime = stb.st_ctimespec;
350                         vap->va_uid = stb.st_uid;
351                         vap->va_gid = stb.st_gid;
352                 }
353                 break;
354
355         default:
356                 panic("fdesc_getattr");
357                 break;
358         }
359
360         if (error == 0)
361                 vp->v_type = vap->va_type;
362         return (error);
363 }
364
365 /*
366  * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
367  *               struct ucred *a_cred)
368  */
369 static int
370 fdesc_setattr(struct vop_setattr_args *ap)
371 {
372         struct proc *p = curproc;
373         struct vattr *vap = ap->a_vap;
374         struct file *fp;
375         unsigned fd;
376         int error;
377
378         /*
379          * Can't mess with the root vnode
380          */
381         if (VTOFDESC(ap->a_vp)->fd_type == Froot)
382                 return (EACCES);
383
384         fd = VTOFDESC(ap->a_vp)->fd_fd;
385         KKASSERT(p);
386
387         /*
388          * Allow setattr where there is an underlying vnode.
389          */
390         error = holdvnode(p->p_fd, fd, &fp);
391         if (error) {
392                 /*
393                  * holdvnode() returns EINVAL if the file descriptor is not
394                  * backed by a vnode.  Silently drop all changes except
395                  * chflags(2) in this case.
396                  */
397                 if (error == EINVAL) {
398                         if (vap->va_flags != VNOVAL)
399                                 error = EOPNOTSUPP;
400                         else
401                                 error = 0;
402                 }
403         } else {
404                 fdrop(fp);
405         }
406         return (error);
407 }
408
409 #define UIO_MX 16
410
411 /*
412  * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
413  *               int *a_eofflag, off_t *a_cookies, int a_ncookies)
414  */
415 static int
416 fdesc_readdir(struct vop_readdir_args *ap)
417 {
418         struct uio *uio = ap->a_uio;
419         struct filedesc *fdp;
420         int error, i, fcnt;
421         size_t namelen;
422         char name[20]; /* enough for %d */
423
424         /*
425          * We don't allow exporting fdesc mounts, and currently local
426          * requests do not need cookies.
427          */
428         if (ap->a_ncookies)
429                 panic("fdesc_readdir: not hungry");
430
431         if (VTOFDESC(ap->a_vp)->fd_type != Froot)
432                 panic("fdesc_readdir: not dir");
433
434         if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
435                 return(EINVAL);
436         i = (int)uio->uio_offset;
437         KKASSERT(uio->uio_td->td_proc);
438         fdp = uio->uio_td->td_proc->p_fd;
439         error = 0;
440
441         fcnt = i - 2;           /* The first two nodes are `.' and `..' */
442
443         while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
444                 switch (i) {
445                 case 0: /* `.' */
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;
452                 case 1: /* `..' */
453                         if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
454                                              2, ".."))
455                                 goto done;
456                         if (error)
457                                 return (error);
458                         break;
459                 default:
460                         if (fdp->fd_files[fcnt].fp == NULL) {
461                                 fcnt++;
462                                 continue;
463                         }
464
465                         namelen = ksnprintf(name, sizeof(name), "%d", fcnt);
466                         if (vop_write_dirent(&error, uio, FD_ROOT + i,
467                                              DT_UNKNOWN, namelen, name))
468                                 goto done;
469                         if (error)
470                                 return (error);
471                         break;
472                 }
473                 i++;
474                 fcnt++;
475         }
476
477 done:
478         if (i >= 2)
479                 uio->uio_offset = fcnt + 2;
480         else
481                 uio->uio_offset = i;
482         return (error);
483 }
484
485 /*
486  * fdesc_inactive(struct vnode *a_vp)
487  */
488 static int
489 fdesc_inactive(struct vop_inactive_args *ap)
490 {
491         struct vnode *vp = ap->a_vp;
492
493         /*
494          * Clear out the v_type field to avoid
495          * nasty things happening in vgone().
496          */
497         vp->v_type = VNON;
498         return (0);
499 }
500
501 /*
502  * fdesc_reclaim(struct vnode *a_vp)
503  */
504 static int
505 fdesc_reclaim(struct vop_reclaim_args *ap)
506 {
507         struct vnode *vp = ap->a_vp;
508         struct fdescnode *fd = VTOFDESC(vp);
509
510         LIST_REMOVE(fd, fd_hash);
511         kfree(vp->v_data, M_TEMP);
512         vp->v_data = 0;
513
514         return (0);
515 }
516
517 /*
518  * Print out the contents of a /dev/fd vnode.
519  *
520  * fdesc_print(struct vnode *a_vp)
521  */
522 /* ARGSUSED */
523 static int
524 fdesc_print(struct vop_print_args *ap)
525 {
526         kprintf("tag VT_NON, fdesc vnode\n");
527         return (0);
528 }
529
530 struct vop_ops fdesc_vnode_vops = {
531         .vop_default =          vop_defaultop,
532         .vop_access =           (void *)vop_null,
533         .vop_getattr =          fdesc_getattr,
534         .vop_inactive =         fdesc_inactive,
535         .vop_old_lookup =       fdesc_lookup,
536         .vop_open =             fdesc_open,
537         .vop_pathconf =         vop_stdpathconf,
538         .vop_print =            fdesc_print,
539         .vop_readdir =          fdesc_readdir,
540         .vop_reclaim =          fdesc_reclaim,
541         .vop_setattr =          fdesc_setattr
542 };
543