]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/fs/fdescfs/fdesc_vnops.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / fs / fdescfs / 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  * 4. 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$
35  */
36
37 /*
38  * /dev/fd Filesystem
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.h>
44 #include <sys/conf.h>
45 #include <sys/dirent.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h> /* boottime */
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/malloc.h>
51 #include <sys/file.h>   /* Must come after sys/malloc.h */
52 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/proc.h>
55 #include <sys/stat.h>
56 #include <sys/vnode.h>
57
58 #include <fs/fdescfs/fdesc.h>
59
60 #define NFDCACHE 4
61 #define FD_NHASH(ix) \
62         (&fdhashtbl[(ix) & fdhash])
63 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
64 static u_long fdhash;
65
66 struct mtx fdesc_hashmtx;
67
68 static vop_getattr_t    fdesc_getattr;
69 static vop_lookup_t     fdesc_lookup;
70 static vop_open_t       fdesc_open;
71 static vop_readdir_t    fdesc_readdir;
72 static vop_reclaim_t    fdesc_reclaim;
73 static vop_setattr_t    fdesc_setattr;
74
75 static struct vop_vector fdesc_vnodeops = {
76         .vop_default =          &default_vnodeops,
77
78         .vop_access =           VOP_NULL,
79         .vop_getattr =          fdesc_getattr,
80         .vop_lookup =           fdesc_lookup,
81         .vop_open =             fdesc_open,
82         .vop_pathconf =         vop_stdpathconf,
83         .vop_readdir =          fdesc_readdir,
84         .vop_reclaim =          fdesc_reclaim,
85         .vop_setattr =          fdesc_setattr,
86 };
87
88 static void fdesc_insmntque_dtr(struct vnode *, void *);
89 static void fdesc_remove_entry(struct fdescnode *);
90
91 /*
92  * Initialise cache headers
93  */
94 int
95 fdesc_init(vfsp)
96         struct vfsconf *vfsp;
97 {
98
99         mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
100         fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
101         return (0);
102 }
103
104 /*
105  * Uninit ready for unload.
106  */
107 int
108 fdesc_uninit(vfsp)
109         struct vfsconf *vfsp;
110 {
111
112         hashdestroy(fdhashtbl, M_CACHE, fdhash);
113         mtx_destroy(&fdesc_hashmtx);
114         return (0);
115 }
116
117 /*
118  * If allocating vnode fails, call this.
119  */
120 static void
121 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
122 {
123
124         vgone(vp);
125         vput(vp);
126 }
127
128 /*
129  * Remove an entry from the hash if it exists.
130  */
131 static void
132 fdesc_remove_entry(struct fdescnode *fd)
133 {
134         struct fdhashhead *fc;
135         struct fdescnode *fd2;
136
137         fc = FD_NHASH(fd->fd_ix);
138         mtx_lock(&fdesc_hashmtx);
139         LIST_FOREACH(fd2, fc, fd_hash) {
140                 if (fd == fd2) {
141                         LIST_REMOVE(fd, fd_hash);
142                         break;
143                 }
144         }
145         mtx_unlock(&fdesc_hashmtx);
146 }
147
148 int
149 fdesc_allocvp(ftype, fd_fd, ix, mp, vpp)
150         fdntype ftype;
151         unsigned fd_fd;
152         int ix;
153         struct mount *mp;
154         struct vnode **vpp;
155 {
156         struct fdescmount *fmp;
157         struct fdhashhead *fc;
158         struct fdescnode *fd, *fd2;
159         struct vnode *vp, *vp2;
160         struct thread *td;
161         int error = 0;
162
163         td = curthread;
164         fc = FD_NHASH(ix);
165 loop:
166         mtx_lock(&fdesc_hashmtx);
167         /*
168          * If a forced unmount is progressing, we need to drop it. The flags are
169          * protected by the hashmtx.
170          */
171         fmp = (struct fdescmount *)mp->mnt_data;
172         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
173                 mtx_unlock(&fdesc_hashmtx);
174                 return (-1);
175         }
176
177         LIST_FOREACH(fd, fc, fd_hash) {
178                 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
179                         /* Get reference to vnode in case it's being free'd */
180                         vp = fd->fd_vnode;
181                         VI_LOCK(vp);
182                         mtx_unlock(&fdesc_hashmtx);
183                         if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
184                                 goto loop;
185                         *vpp = vp;
186                         return (0);
187                 }
188         }
189         mtx_unlock(&fdesc_hashmtx);
190
191         fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
192
193         error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
194         if (error) {
195                 free(fd, M_TEMP);
196                 return (error);
197         }
198         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
199         vp->v_data = fd;
200         fd->fd_vnode = vp;
201         fd->fd_type = ftype;
202         fd->fd_fd = fd_fd;
203         fd->fd_ix = ix;
204         error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
205         if (error != 0) {
206                 *vpp = NULLVP;
207                 return (error);
208         }
209
210         /* Make sure that someone didn't beat us when inserting the vnode. */
211         mtx_lock(&fdesc_hashmtx);
212         /*
213          * If a forced unmount is progressing, we need to drop it. The flags are
214          * protected by the hashmtx.
215          */
216         fmp = (struct fdescmount *)mp->mnt_data;
217         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
218                 mtx_unlock(&fdesc_hashmtx);
219                 vgone(vp);
220                 vput(vp);
221                 *vpp = NULLVP;
222                 return (-1);
223         }
224
225         LIST_FOREACH(fd2, fc, fd_hash) {
226                 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
227                         /* Get reference to vnode in case it's being free'd */
228                         vp2 = fd2->fd_vnode;
229                         VI_LOCK(vp2);
230                         mtx_unlock(&fdesc_hashmtx);
231                         error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
232                         /* Someone beat us, dec use count and wait for reclaim */
233                         vgone(vp);
234                         vput(vp);
235                         /* If we didn't get it, return no vnode. */
236                         if (error)
237                                 vp2 = NULLVP;
238                         *vpp = vp2;
239                         return (error);
240                 }
241         }
242
243         /* If we came here, we can insert it safely. */
244         LIST_INSERT_HEAD(fc, fd, fd_hash);
245         mtx_unlock(&fdesc_hashmtx);
246         *vpp = vp;
247         return (0);
248 }
249
250 struct fdesc_get_ino_args {
251         fdntype ftype;
252         unsigned fd_fd;
253         int ix;
254         struct file *fp;
255         struct thread *td;
256 };
257
258 static int
259 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
260     struct vnode **rvp)
261 {
262         struct fdesc_get_ino_args *a;
263         int error;
264
265         a = arg;
266         error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
267         fdrop(a->fp, a->td);
268         return (error);
269 }
270
271
272 /*
273  * vp is the current namei directory
274  * ndp is the name to locate in that directory...
275  */
276 static int
277 fdesc_lookup(ap)
278         struct vop_lookup_args /* {
279                 struct vnode * a_dvp;
280                 struct vnode ** a_vpp;
281                 struct componentname * a_cnp;
282         } */ *ap;
283 {
284         struct vnode **vpp = ap->a_vpp;
285         struct vnode *dvp = ap->a_dvp;
286         struct componentname *cnp = ap->a_cnp;
287         char *pname = cnp->cn_nameptr;
288         struct thread *td = cnp->cn_thread;
289         struct file *fp;
290         struct fdesc_get_ino_args arg;
291         int nlen = cnp->cn_namelen;
292         u_int fd, fd1;
293         int error;
294         struct vnode *fvp;
295
296         if ((cnp->cn_flags & ISLASTCN) &&
297             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
298                 error = EROFS;
299                 goto bad;
300         }
301
302         if (cnp->cn_namelen == 1 && *pname == '.') {
303                 *vpp = dvp;
304                 VREF(dvp);
305                 return (0);
306         }
307
308         if (VTOFDESC(dvp)->fd_type != Froot) {
309                 error = ENOTDIR;
310                 goto bad;
311         }
312
313         fd = 0;
314         /* the only time a leading 0 is acceptable is if it's "0" */
315         if (*pname == '0' && nlen != 1) {
316                 error = ENOENT;
317                 goto bad;
318         }
319         while (nlen--) {
320                 if (*pname < '0' || *pname > '9') {
321                         error = ENOENT;
322                         goto bad;
323                 }
324                 fd1 = 10 * fd + *pname++ - '0';
325                 if (fd1 < fd) {
326                         error = ENOENT;
327                         goto bad;
328                 }
329                 fd = fd1;
330         }
331
332         /*
333          * No rights to check since 'fp' isn't actually used.
334          */
335         if ((error = fget(td, fd, NULL, &fp)) != 0)
336                 goto bad;
337
338         /* Check if we're looking up ourselves. */
339         if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
340                 /*
341                  * In case we're holding the last reference to the file, the dvp
342                  * will be re-acquired.
343                  */
344                 vhold(dvp);
345                 VOP_UNLOCK(dvp, 0);
346                 fdrop(fp, td);
347
348                 /* Re-aquire the lock afterwards. */
349                 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
350                 vdrop(dvp);
351                 fvp = dvp;
352                 if ((dvp->v_iflag & VI_DOOMED) != 0)
353                         error = ENOENT;
354         } else {
355                 /*
356                  * Unlock our root node (dvp) when doing this, since we might
357                  * deadlock since the vnode might be locked by another thread
358                  * and the root vnode lock will be obtained afterwards (in case
359                  * we're looking up the fd of the root vnode), which will be the
360                  * opposite lock order. Vhold the root vnode first so we don't
361                  * lose it.
362                  */
363                 arg.ftype = Fdesc;
364                 arg.fd_fd = fd;
365                 arg.ix = FD_DESC + fd;
366                 arg.fp = fp;
367                 arg.td = td;
368                 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
369                     LK_EXCLUSIVE, &fvp);
370         }
371         
372         if (error)
373                 goto bad;
374         *vpp = fvp;
375         return (0);
376
377 bad:
378         *vpp = NULL;
379         return (error);
380 }
381
382 static int
383 fdesc_open(ap)
384         struct vop_open_args /* {
385                 struct vnode *a_vp;
386                 int  a_mode;
387                 struct ucred *a_cred;
388                 struct thread *a_td;
389         } */ *ap;
390 {
391         struct vnode *vp = ap->a_vp;
392
393         if (VTOFDESC(vp)->fd_type == Froot)
394                 return (0);
395
396         /*
397          * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
398          * descriptor being sought for duplication. The error return ensures
399          * that the vnode for this device will be released by vn_open. Open
400          * will detect this special error and take the actions in dupfdopen.
401          * Other callers of vn_open or VOP_OPEN will simply report the
402          * error.
403          */
404         ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd;       /* XXX */
405         return (ENODEV);
406 }
407
408 static int
409 fdesc_getattr(ap)
410         struct vop_getattr_args /* {
411                 struct vnode *a_vp;
412                 struct vattr *a_vap;
413                 struct ucred *a_cred;
414         } */ *ap;
415 {
416         struct vnode *vp = ap->a_vp;
417         struct vattr *vap = ap->a_vap;
418
419         vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
420         vap->va_fileid = VTOFDESC(vp)->fd_ix;
421         vap->va_uid = 0;
422         vap->va_gid = 0;
423         vap->va_blocksize = DEV_BSIZE;
424         vap->va_atime.tv_sec = boottime.tv_sec;
425         vap->va_atime.tv_nsec = 0;
426         vap->va_mtime = vap->va_atime;
427         vap->va_ctime = vap->va_mtime;
428         vap->va_gen = 0;
429         vap->va_flags = 0;
430         vap->va_bytes = 0;
431         vap->va_filerev = 0;
432
433         switch (VTOFDESC(vp)->fd_type) {
434         case Froot:
435                 vap->va_type = VDIR;
436                 vap->va_nlink = 2;
437                 vap->va_size = DEV_BSIZE;
438                 vap->va_rdev = NODEV;
439                 break;
440
441         case Fdesc:
442                 vap->va_type = VCHR;
443                 vap->va_nlink = 1;
444                 vap->va_size = 0;
445                 vap->va_rdev = makedev(0, vap->va_fileid);
446                 break;
447
448         default:
449                 panic("fdesc_getattr");
450                 break;
451         }
452
453         vp->v_type = vap->va_type;
454         return (0);
455 }
456
457 static int
458 fdesc_setattr(ap)
459         struct vop_setattr_args /* {
460                 struct vnode *a_vp;
461                 struct vattr *a_vap;
462                 struct ucred *a_cred;
463         } */ *ap;
464 {
465         struct vattr *vap = ap->a_vap;
466         struct vnode *vp;
467         struct mount *mp;
468         struct file *fp;
469         struct thread *td = curthread;
470         cap_rights_t rights;
471         unsigned fd;
472         int error;
473
474         /*
475          * Can't mess with the root vnode
476          */
477         if (VTOFDESC(ap->a_vp)->fd_type == Froot)
478                 return (EACCES);
479
480         fd = VTOFDESC(ap->a_vp)->fd_fd;
481
482         /*
483          * Allow setattr where there is an underlying vnode.
484          */
485         error = getvnode(td->td_proc->p_fd, fd,
486             cap_rights_init(&rights, CAP_EXTATTR_SET), &fp);
487         if (error) {
488                 /*
489                  * getvnode() returns EINVAL if the file descriptor is not
490                  * backed by a vnode.  Silently drop all changes except
491                  * chflags(2) in this case.
492                  */
493                 if (error == EINVAL) {
494                         if (vap->va_flags != VNOVAL)
495                                 error = EOPNOTSUPP;
496                         else
497                                 error = 0;
498                 }
499                 return (error);
500         }
501         vp = fp->f_vnode;
502         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
503                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
504                 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
505                 VOP_UNLOCK(vp, 0);
506                 vn_finished_write(mp);
507         }
508         fdrop(fp, td);
509         return (error);
510 }
511
512 #define UIO_MX 16
513
514 static int
515 fdesc_readdir(ap)
516         struct vop_readdir_args /* {
517                 struct vnode *a_vp;
518                 struct uio *a_uio;
519                 struct ucred *a_cred;
520                 int *a_eofflag;
521                 u_long *a_cookies;
522                 int a_ncookies;
523         } */ *ap;
524 {
525         struct uio *uio = ap->a_uio;
526         struct filedesc *fdp;
527         struct dirent d;
528         struct dirent *dp = &d;
529         int error, i, off, fcnt;
530
531         if (VTOFDESC(ap->a_vp)->fd_type != Froot)
532                 panic("fdesc_readdir: not dir");
533
534         if (ap->a_ncookies != NULL)
535                 *ap->a_ncookies = 0;
536
537         off = (int)uio->uio_offset;
538         if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
539             uio->uio_resid < UIO_MX)
540                 return (EINVAL);
541         i = (u_int)off / UIO_MX;
542         fdp = uio->uio_td->td_proc->p_fd;
543         error = 0;
544
545         fcnt = i - 2;           /* The first two nodes are `.' and `..' */
546
547         FILEDESC_SLOCK(fdp);
548         while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
549                 bzero((caddr_t)dp, UIO_MX);
550                 switch (i) {
551                 case 0: /* `.' */
552                 case 1: /* `..' */
553                         dp->d_fileno = i + FD_ROOT;
554                         dp->d_namlen = i + 1;
555                         dp->d_reclen = UIO_MX;
556                         bcopy("..", dp->d_name, dp->d_namlen);
557                         dp->d_name[i + 1] = '\0';
558                         dp->d_type = DT_DIR;
559                         break;
560                 default:
561                         if (fdp->fd_ofiles[fcnt].fde_file == NULL)
562                                 break;
563                         dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
564                         dp->d_reclen = UIO_MX;
565                         dp->d_type = DT_CHR;
566                         dp->d_fileno = i + FD_DESC;
567                         break;
568                 }
569                 if (dp->d_namlen != 0) {
570                         /*
571                          * And ship to userland
572                          */
573                         FILEDESC_SUNLOCK(fdp);
574                         error = uiomove(dp, UIO_MX, uio);
575                         if (error)
576                                 goto done;
577                         FILEDESC_SLOCK(fdp);
578                 }
579                 i++;
580                 fcnt++;
581         }
582         FILEDESC_SUNLOCK(fdp);
583
584 done:
585         uio->uio_offset = i * UIO_MX;
586         return (error);
587 }
588
589 static int
590 fdesc_reclaim(ap)
591         struct vop_reclaim_args /* {
592                 struct vnode *a_vp;
593         } */ *ap;
594 {
595         struct vnode *vp;
596         struct fdescnode *fd;
597
598         vp = ap->a_vp;
599         fd = VTOFDESC(vp);
600         fdesc_remove_entry(fd);
601         free(vp->v_data, M_TEMP);
602         vp->v_data = NULL;
603         return (0);
604 }