]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fdescfs/fdesc_vnops.c
fdescfs: stop using insmntque1
[FreeBSD/FreeBSD.git] / sys / fs / fdescfs / fdesc_vnops.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)fdesc_vnops.c       8.9 (Berkeley) 1/21/94
35  *
36  * $FreeBSD$
37  */
38
39 /*
40  * /dev/fd Filesystem
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/capsicum.h>
46 #include <sys/conf.h>
47 #include <sys/dirent.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h> /* boottime */
50 #include <sys/lock.h>
51 #include <sys/mutex.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/namei.h>
56 #include <sys/proc.h>
57 #include <sys/stat.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/unistd.h>
60 #include <sys/vnode.h>
61
62 #include <fs/fdescfs/fdesc.h>
63
64 #define NFDCACHE 4
65 #define FD_NHASH(ix) \
66         (&fdhashtbl[(ix) & fdhash])
67 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
68 static u_long fdhash;
69
70 struct mtx fdesc_hashmtx;
71
72 static vop_getattr_t    fdesc_getattr;
73 static vop_lookup_t     fdesc_lookup;
74 static vop_open_t       fdesc_open;
75 static vop_pathconf_t   fdesc_pathconf;
76 static vop_readdir_t    fdesc_readdir;
77 static vop_readlink_t   fdesc_readlink;
78 static vop_reclaim_t    fdesc_reclaim;
79 static vop_setattr_t    fdesc_setattr;
80
81 static struct vop_vector fdesc_vnodeops = {
82         .vop_default =          &default_vnodeops,
83
84         .vop_access =           VOP_NULL,
85         .vop_getattr =          fdesc_getattr,
86         .vop_lookup =           fdesc_lookup,
87         .vop_open =             fdesc_open,
88         .vop_pathconf =         fdesc_pathconf,
89         .vop_readdir =          fdesc_readdir,
90         .vop_readlink =         fdesc_readlink,
91         .vop_reclaim =          fdesc_reclaim,
92         .vop_setattr =          fdesc_setattr,
93 };
94 VFS_VOP_VECTOR_REGISTER(fdesc_vnodeops);
95
96 static void fdesc_remove_entry(struct fdescnode *);
97
98 /*
99  * Initialise cache headers
100  */
101 int
102 fdesc_init(struct vfsconf *vfsp)
103 {
104
105         mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
106         fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
107         return (0);
108 }
109
110 /*
111  * Uninit ready for unload.
112  */
113 int
114 fdesc_uninit(struct vfsconf *vfsp)
115 {
116
117         hashdestroy(fdhashtbl, M_CACHE, fdhash);
118         mtx_destroy(&fdesc_hashmtx);
119         return (0);
120 }
121
122 /*
123  * Remove an entry from the hash if it exists.
124  */
125 static void
126 fdesc_remove_entry(struct fdescnode *fd)
127 {
128         struct fdhashhead *fc;
129         struct fdescnode *fd2;
130
131         fc = FD_NHASH(fd->fd_ix);
132         mtx_lock(&fdesc_hashmtx);
133         LIST_FOREACH(fd2, fc, fd_hash) {
134                 if (fd == fd2) {
135                         LIST_REMOVE(fd, fd_hash);
136                         break;
137                 }
138         }
139         mtx_unlock(&fdesc_hashmtx);
140 }
141
142 int
143 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
144     struct vnode **vpp)
145 {
146         struct fdescmount *fmp;
147         struct fdhashhead *fc;
148         struct fdescnode *fd, *fd2;
149         struct vnode *vp, *vp2;
150         int error;
151
152         fc = FD_NHASH(ix);
153 loop:
154         mtx_lock(&fdesc_hashmtx);
155         /*
156          * If a forced unmount is progressing, we need to drop it. The flags are
157          * protected by the hashmtx.
158          */
159         fmp = mp->mnt_data;
160         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
161                 mtx_unlock(&fdesc_hashmtx);
162                 return (-1);
163         }
164
165         LIST_FOREACH(fd, fc, fd_hash) {
166                 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
167                         /* Get reference to vnode in case it's being free'd */
168                         vp = fd->fd_vnode;
169                         VI_LOCK(vp);
170                         mtx_unlock(&fdesc_hashmtx);
171                         if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
172                                 goto loop;
173                         *vpp = vp;
174                         return (0);
175                 }
176         }
177         mtx_unlock(&fdesc_hashmtx);
178
179         fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
180
181         error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
182         if (error) {
183                 free(fd, M_TEMP);
184                 return (error);
185         }
186         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
187         vp->v_data = fd;
188         fd->fd_vnode = vp;
189         fd->fd_type = ftype;
190         fd->fd_fd = fd_fd;
191         fd->fd_ix = ix;
192         if (ftype == Fdesc && fmp->flags & FMNT_LINRDLNKF)
193                 vp->v_vflag |= VV_READLINK;
194         error = insmntque(vp, mp);
195         if (error != 0) {
196                 vgone(vp);
197                 vput(vp);
198                 *vpp = NULLVP;
199                 return (error);
200         }
201
202         /* Make sure that someone didn't beat us when inserting the vnode. */
203         mtx_lock(&fdesc_hashmtx);
204         /*
205          * If a forced unmount is progressing, we need to drop it. The flags are
206          * protected by the hashmtx.
207          */
208         fmp = mp->mnt_data;
209         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
210                 mtx_unlock(&fdesc_hashmtx);
211                 vgone(vp);
212                 vput(vp);
213                 *vpp = NULLVP;
214                 return (-1);
215         }
216
217         LIST_FOREACH(fd2, fc, fd_hash) {
218                 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
219                         /* Get reference to vnode in case it's being free'd */
220                         vp2 = fd2->fd_vnode;
221                         VI_LOCK(vp2);
222                         mtx_unlock(&fdesc_hashmtx);
223                         error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK);
224                         /* Someone beat us, dec use count and wait for reclaim */
225                         vgone(vp);
226                         vput(vp);
227                         /* If we didn't get it, return no vnode. */
228                         if (error)
229                                 vp2 = NULLVP;
230                         *vpp = vp2;
231                         return (error);
232                 }
233         }
234
235         /* If we came here, we can insert it safely. */
236         LIST_INSERT_HEAD(fc, fd, fd_hash);
237         mtx_unlock(&fdesc_hashmtx);
238         *vpp = vp;
239         return (0);
240 }
241
242 struct fdesc_get_ino_args {
243         fdntype ftype;
244         unsigned fd_fd;
245         int ix;
246         struct file *fp;
247         struct thread *td;
248 };
249
250 static int
251 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
252     struct vnode **rvp)
253 {
254         struct fdesc_get_ino_args *a;
255         struct fdescmount *fdm;
256         struct vnode *vp;
257         int error;
258
259         a = arg;
260         fdm = VFSTOFDESC(mp);
261         if ((fdm->flags & FMNT_NODUP) != 0 && a->fp->f_type == DTYPE_VNODE) {
262                 vp = a->fp->f_vnode;
263                 vget(vp, lkflags | LK_RETRY);
264                 *rvp = vp;
265                 error = 0;
266         } else {
267                 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
268         }
269         fdrop(a->fp, a->td);
270         return (error);
271 }
272
273 /*
274  * vp is the current namei directory
275  * ndp is the name to locate in that directory...
276  */
277 static int
278 fdesc_lookup(struct vop_lookup_args *ap)
279 {
280         struct vnode **vpp = ap->a_vpp;
281         struct vnode *dvp = ap->a_dvp;
282         struct componentname *cnp = ap->a_cnp;
283         char *pname = cnp->cn_nameptr;
284         struct thread *td = curthread;
285         struct file *fp;
286         struct fdesc_get_ino_args arg;
287         int nlen = cnp->cn_namelen;
288         u_int fd, fd1;
289         int error;
290         struct vnode *fvp;
291
292         if ((cnp->cn_flags & ISLASTCN) &&
293             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
294                 error = EROFS;
295                 goto bad;
296         }
297
298         if (cnp->cn_namelen == 1 && *pname == '.') {
299                 *vpp = dvp;
300                 VREF(dvp);
301                 return (0);
302         }
303
304         if (VTOFDESC(dvp)->fd_type != Froot) {
305                 error = ENOTDIR;
306                 goto bad;
307         }
308
309         fd = 0;
310         /* the only time a leading 0 is acceptable is if it's "0" */
311         if (*pname == '0' && nlen != 1) {
312                 error = ENOENT;
313                 goto bad;
314         }
315         while (nlen--) {
316                 if (*pname < '0' || *pname > '9') {
317                         error = ENOENT;
318                         goto bad;
319                 }
320                 fd1 = 10 * fd + *pname++ - '0';
321                 if (fd1 < fd) {
322                         error = ENOENT;
323                         goto bad;
324                 }
325                 fd = fd1;
326         }
327
328         /*
329          * No rights to check since 'fp' isn't actually used.
330          */
331         if ((error = fget(td, fd, &cap_no_rights, &fp)) != 0)
332                 goto bad;
333
334         /* Check if we're looking up ourselves. */
335         if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
336                 /*
337                  * In case we're holding the last reference to the file, the dvp
338                  * will be re-acquired.
339                  */
340                 vhold(dvp);
341                 VOP_UNLOCK(dvp);
342                 fdrop(fp, td);
343
344                 /* Re-aquire the lock afterwards. */
345                 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
346                 vdrop(dvp);
347                 fvp = dvp;
348                 if (VN_IS_DOOMED(dvp))
349                         error = ENOENT;
350         } else {
351                 /*
352                  * Unlock our root node (dvp) when doing this, since we might
353                  * deadlock since the vnode might be locked by another thread
354                  * and the root vnode lock will be obtained afterwards (in case
355                  * we're looking up the fd of the root vnode), which will be the
356                  * opposite lock order. Vhold the root vnode first so we don't
357                  * lose it.
358                  */
359                 arg.ftype = Fdesc;
360                 arg.fd_fd = fd;
361                 arg.ix = FD_DESC + fd;
362                 arg.fp = fp;
363                 arg.td = td;
364                 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
365                     LK_EXCLUSIVE, &fvp);
366         }
367
368         if (error)
369                 goto bad;
370         *vpp = fvp;
371         return (0);
372
373 bad:
374         *vpp = NULL;
375         return (error);
376 }
377
378 static int
379 fdesc_open(struct vop_open_args *ap)
380 {
381         struct vnode *vp = ap->a_vp;
382
383         if (VTOFDESC(vp)->fd_type == Froot)
384                 return (0);
385
386         /*
387          * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
388          * descriptor being sought for duplication. The error return ensures
389          * that the vnode for this device will be released by vn_open. Open
390          * will detect this special error and take the actions in dupfdopen.
391          * Other callers of vn_open or VOP_OPEN will simply report the
392          * error.
393          */
394         ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd;       /* XXX */
395         return (ENODEV);
396 }
397
398 static int
399 fdesc_pathconf(struct vop_pathconf_args *ap)
400 {
401         struct vnode *vp = ap->a_vp;
402         int error;
403
404         switch (ap->a_name) {
405         case _PC_NAME_MAX:
406                 *ap->a_retval = NAME_MAX;
407                 return (0);
408         case _PC_LINK_MAX:
409                 if (VTOFDESC(vp)->fd_type == Froot)
410                         *ap->a_retval = 2;
411                 else
412                         *ap->a_retval = 1;
413                 return (0);
414         default:
415                 if (VTOFDESC(vp)->fd_type == Froot)
416                         return (vop_stdpathconf(ap));
417                 vref(vp);
418                 VOP_UNLOCK(vp);
419                 error = kern_fpathconf(curthread, VTOFDESC(vp)->fd_fd,
420                     ap->a_name, ap->a_retval);
421                 vn_lock(vp, LK_SHARED | LK_RETRY);
422                 vunref(vp);
423                 return (error);
424         }
425 }
426
427 static int
428 fdesc_getattr(struct vop_getattr_args *ap)
429 {
430         struct vnode *vp = ap->a_vp;
431         struct vattr *vap = ap->a_vap;
432         struct timeval boottime;
433
434         getboottime(&boottime);
435         vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
436         vap->va_fileid = VTOFDESC(vp)->fd_ix;
437         vap->va_uid = 0;
438         vap->va_gid = 0;
439         vap->va_blocksize = DEV_BSIZE;
440         vap->va_atime.tv_sec = boottime.tv_sec;
441         vap->va_atime.tv_nsec = 0;
442         vap->va_mtime = vap->va_atime;
443         vap->va_ctime = vap->va_mtime;
444         vap->va_gen = 0;
445         vap->va_flags = 0;
446         vap->va_bytes = 0;
447         vap->va_filerev = 0;
448
449         switch (VTOFDESC(vp)->fd_type) {
450         case Froot:
451                 vap->va_type = VDIR;
452                 vap->va_nlink = 2;
453                 vap->va_size = DEV_BSIZE;
454                 vap->va_rdev = NODEV;
455                 break;
456
457         case Fdesc:
458                 vap->va_type = (vp->v_vflag & VV_READLINK) == 0 ? VCHR : VLNK;
459                 vap->va_nlink = 1;
460                 vap->va_size = 0;
461                 vap->va_rdev = makedev(0, vap->va_fileid);
462                 break;
463
464         default:
465                 panic("fdesc_getattr");
466                 break;
467         }
468
469         vp->v_type = vap->va_type;
470         return (0);
471 }
472
473 static int
474 fdesc_setattr(struct vop_setattr_args *ap)
475 {
476         struct vattr *vap = ap->a_vap;
477         struct vnode *vp;
478         struct mount *mp;
479         struct file *fp;
480         struct thread *td = curthread;
481         cap_rights_t rights;
482         unsigned fd;
483         int error;
484
485         /*
486          * Can't mess with the root vnode
487          */
488         if (VTOFDESC(ap->a_vp)->fd_type == Froot)
489                 return (EACCES);
490
491         fd = VTOFDESC(ap->a_vp)->fd_fd;
492
493         /*
494          * Allow setattr where there is an underlying vnode.
495          */
496         error = getvnode(td, fd,
497             cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
498         if (error) {
499                 /*
500                  * getvnode() returns EINVAL if the file descriptor is not
501                  * backed by a vnode.  Silently drop all changes except
502                  * chflags(2) in this case.
503                  */
504                 if (error == EINVAL) {
505                         if (vap->va_flags != VNOVAL)
506                                 error = EOPNOTSUPP;
507                         else
508                                 error = 0;
509                 }
510                 return (error);
511         }
512         vp = fp->f_vnode;
513         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
514                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
515                 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
516                 VOP_UNLOCK(vp);
517                 vn_finished_write(mp);
518         }
519         fdrop(fp, td);
520         return (error);
521 }
522
523 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
524
525 static int
526 fdesc_readdir(struct vop_readdir_args *ap)
527 {
528         struct fdescmount *fmp;
529         struct uio *uio = ap->a_uio;
530         struct filedesc *fdp;
531         struct dirent d;
532         struct dirent *dp = &d;
533         int error, i, off, fcnt;
534
535         if (VTOFDESC(ap->a_vp)->fd_type != Froot)
536                 panic("fdesc_readdir: not dir");
537
538         fmp = VFSTOFDESC(ap->a_vp->v_mount);
539         if (ap->a_ncookies != NULL)
540                 *ap->a_ncookies = 0;
541
542         off = (int)uio->uio_offset;
543         if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
544             uio->uio_resid < UIO_MX)
545                 return (EINVAL);
546         i = (u_int)off / UIO_MX;
547         fdp = uio->uio_td->td_proc->p_fd;
548         error = 0;
549
550         fcnt = i - 2;           /* The first two nodes are `.' and `..' */
551
552         FILEDESC_SLOCK(fdp);
553         while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
554                 bzero((caddr_t)dp, UIO_MX);
555                 switch (i) {
556                 case 0: /* `.' */
557                 case 1: /* `..' */
558                         dp->d_fileno = i + FD_ROOT;
559                         dp->d_namlen = i + 1;
560                         dp->d_reclen = UIO_MX;
561                         bcopy("..", dp->d_name, dp->d_namlen);
562                         dp->d_type = DT_DIR;
563                         dirent_terminate(dp);
564                         break;
565                 default:
566                         if (fdp->fd_ofiles[fcnt].fde_file == NULL)
567                                 break;
568                         dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
569                         dp->d_reclen = UIO_MX;
570                         dp->d_type = (fmp->flags & FMNT_LINRDLNKF) == 0 ?
571                             DT_CHR : DT_LNK;
572                         dp->d_fileno = i + FD_DESC;
573                         dirent_terminate(dp);
574                         break;
575                 }
576                 /* NOTE: d_off is the offset of the *next* entry. */
577                 dp->d_off = UIO_MX * (i + 1);
578                 if (dp->d_namlen != 0) {
579                         /*
580                          * And ship to userland
581                          */
582                         FILEDESC_SUNLOCK(fdp);
583                         error = uiomove(dp, UIO_MX, uio);
584                         if (error)
585                                 goto done;
586                         FILEDESC_SLOCK(fdp);
587                 }
588                 i++;
589                 fcnt++;
590         }
591         FILEDESC_SUNLOCK(fdp);
592
593 done:
594         uio->uio_offset = i * UIO_MX;
595         return (error);
596 }
597
598 static int
599 fdesc_reclaim(struct vop_reclaim_args *ap)
600 {
601         struct vnode *vp;
602         struct fdescnode *fd;
603
604         vp = ap->a_vp;
605         fd = VTOFDESC(vp);
606         fdesc_remove_entry(fd);
607         free(vp->v_data, M_TEMP);
608         vp->v_data = NULL;
609         return (0);
610 }
611
612 static int
613 fdesc_readlink(struct vop_readlink_args *va)
614 {
615         struct vnode *vp, *vn;
616         struct thread *td;
617         struct uio *uio;
618         struct file *fp;
619         char *freepath, *fullpath;
620         size_t pathlen;
621         int lockflags, fd_fd;
622         int error;
623
624         freepath = NULL;
625         vn = va->a_vp;
626         if (VTOFDESC(vn)->fd_type != Fdesc)
627                 panic("fdesc_readlink: not fdescfs link");
628         fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
629         lockflags = VOP_ISLOCKED(vn);
630         VOP_UNLOCK(vn);
631
632         td = curthread;
633         error = fget_cap(td, fd_fd, &cap_no_rights, &fp, NULL);
634         if (error != 0)
635                 goto out;
636
637         switch (fp->f_type) {
638         case DTYPE_VNODE:
639                 vp = fp->f_vnode;
640                 error = vn_fullpath(vp, &fullpath, &freepath);
641                 break;
642         default:
643                 fullpath = "anon_inode:[unknown]";
644                 break;
645         }
646         if (error == 0) {
647                 uio = va->a_uio;
648                 pathlen = strlen(fullpath);
649                 error = uiomove(fullpath, pathlen, uio);
650         }
651         if (freepath != NULL)
652                 free(freepath, M_TEMP);
653         fdrop(fp, td);
654
655 out:
656         vn_lock(vn, lockflags | LK_RETRY);
657         return (error);
658 }