]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/autofs/autofs_vnops.c
Import NetBSD's blacklist source from vendor tree
[FreeBSD/FreeBSD.git] / sys / fs / autofs / autofs_vnops.c
1 /*-
2  * Copyright (c) 2014 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/condvar.h>
37 #include <sys/dirent.h>
38 #include <sys/fcntl.h>
39 #include <sys/lock.h>
40 #include <sys/mount.h>
41 #include <sys/mutex.h>
42 #include <sys/namei.h>
43 #include <sys/signalvar.h>
44 #include <sys/stat.h>
45 #include <sys/systm.h>
46 #include <sys/taskqueue.h>
47 #include <sys/tree.h>
48 #include <sys/vnode.h>
49 #include <machine/atomic.h>
50 #include <vm/uma.h>
51
52 #include <fs/autofs/autofs.h>
53
54 static int      autofs_trigger_vn(struct vnode *vp, const char *path,
55                     int pathlen, struct vnode **newvp);
56
57 extern struct autofs_softc      *autofs_softc;
58
59 static int
60 autofs_access(struct vop_access_args *ap)
61 {
62
63         /*
64          * Nothing to do here; the only kind of access control
65          * needed is in autofs_mkdir().
66          */
67
68         return (0);
69 }
70
71 static int
72 autofs_getattr(struct vop_getattr_args *ap)
73 {
74         struct vnode *vp, *newvp;
75         struct autofs_node *anp;
76         struct mount *mp;
77         struct vattr *vap;
78         int error;
79
80         vp = ap->a_vp;
81         anp = vp->v_data;
82         mp = vp->v_mount;
83         vap = ap->a_vap;
84
85         KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
86
87         /*
88          * The reason we must do this is that some tree-walking software,
89          * namely fts(3), assumes that stat(".") results will not change
90          * between chdir("subdir") and chdir(".."), and fails with ENOENT
91          * otherwise.
92          */
93         if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
94             autofs_ignore_thread(curthread) == false) {
95                 error = autofs_trigger_vn(vp, "", 0, &newvp);
96                 if (error != 0)
97                         return (error);
98
99                 if (newvp != NULL) {
100                         error = VOP_GETATTR(newvp, ap->a_vap,
101                             ap->a_cred);
102                         vput(newvp);
103                         return (error);
104                 }
105         }
106
107         vap->va_type = VDIR;
108         vap->va_mode = 0755;
109         vap->va_nlink = 3; /* XXX */
110         vap->va_uid = 0;
111         vap->va_gid = 0;
112         vap->va_rdev = NODEV;
113         vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
114         vap->va_fileid = anp->an_fileno;
115         vap->va_size = S_BLKSIZE;
116         vap->va_blocksize = S_BLKSIZE;
117         vap->va_mtime = anp->an_ctime;
118         vap->va_atime = anp->an_ctime;
119         vap->va_ctime = anp->an_ctime;
120         vap->va_birthtime = anp->an_ctime;
121         vap->va_gen = 0;
122         vap->va_flags = 0;
123         vap->va_rdev = 0;
124         vap->va_bytes = S_BLKSIZE;
125         vap->va_filerev = 0;
126         vap->va_spare = 0;
127
128         return (0);
129 }
130
131 /*
132  * Unlock the vnode, request automountd(8) action, and then lock it back.
133  * If anything got mounted on top of the vnode, return the new filesystem's
134  * root vnode in 'newvp', locked.
135  */
136 static int
137 autofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
138     struct vnode **newvp)
139 {
140         struct autofs_node *anp;
141         struct autofs_mount *amp;
142         int error, lock_flags;
143
144         anp = vp->v_data;
145         amp = VFSTOAUTOFS(vp->v_mount);
146
147         /*
148          * Release the vnode lock, so that other operations, in partcular
149          * mounting a filesystem on top of it, can proceed.  Increase use
150          * count, to prevent the vnode from being deallocated and to prevent
151          * filesystem from being unmounted.
152          */
153         lock_flags = VOP_ISLOCKED(vp);
154         vref(vp);
155         VOP_UNLOCK(vp, 0);
156
157         sx_xlock(&autofs_softc->sc_lock);
158
159         /*
160          * XXX: Workaround for mounting the same thing multiple times; revisit.
161          */
162         if (vp->v_mountedhere != NULL) {
163                 error = 0;
164                 goto mounted;
165         }
166
167         error = autofs_trigger(anp, path, pathlen);
168 mounted:
169         sx_xunlock(&autofs_softc->sc_lock);
170         vn_lock(vp, lock_flags | LK_RETRY);
171         vunref(vp);
172         if ((vp->v_iflag & VI_DOOMED) != 0) {
173                 AUTOFS_DEBUG("VI_DOOMED");
174                 return (ENOENT);
175         }
176
177         if (error != 0)
178                 return (error);
179
180         if (vp->v_mountedhere == NULL) {
181                 *newvp = NULL;
182                 return (0);
183         } else {
184                 /*
185                  * If the operation that succeeded was mount, then mark
186                  * the node as non-cached.  Otherwise, if someone unmounts
187                  * the filesystem before the cache times out, we will fail
188                  * to trigger.
189                  */
190                 anp->an_cached = false;
191         }
192
193         error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
194         if (error != 0) {
195                 AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
196                 return (error);
197         }
198
199         return (0);
200 }
201
202 static int
203 autofs_vget_callback(struct mount *mp, void *arg, int flags,
204     struct vnode **vpp)
205 {
206
207
208         return (autofs_node_vn(arg, mp, flags, vpp));
209 }
210
211 static int
212 autofs_lookup(struct vop_lookup_args *ap)
213 {
214         struct vnode *dvp, *newvp, **vpp;
215         struct mount *mp;
216         struct autofs_mount *amp;
217         struct autofs_node *anp, *child;
218         struct componentname *cnp;
219         int error;
220
221         dvp = ap->a_dvp;
222         vpp = ap->a_vpp;
223         mp = dvp->v_mount;
224         amp = VFSTOAUTOFS(mp);
225         anp = dvp->v_data;
226         cnp = ap->a_cnp;
227
228         if (cnp->cn_flags & ISDOTDOT) {
229                 KASSERT(anp->an_parent != NULL, ("NULL parent"));
230                 /*
231                  * Note that in this case, dvp is the child vnode, and we
232                  * are looking up the parent vnode - exactly reverse from
233                  * normal operation.  Unlocking dvp requires some rather
234                  * tricky unlock/relock dance to prevent mp from being freed;
235                  * use vn_vget_ino_gen() which takes care of all that.
236                  */
237                 error = vn_vget_ino_gen(dvp, autofs_vget_callback,
238                     anp->an_parent, cnp->cn_lkflags, vpp);
239                 if (error != 0) {
240                         AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
241                             error);
242                         return (error);
243                 }
244                 return (error);
245         }
246
247         if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
248                 vref(dvp);
249                 *vpp = dvp;
250
251                 return (0);
252         }
253
254         if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
255             autofs_ignore_thread(cnp->cn_thread) == false) {
256                 error = autofs_trigger_vn(dvp,
257                     cnp->cn_nameptr, cnp->cn_namelen, &newvp);
258                 if (error != 0)
259                         return (error);
260
261                 if (newvp != NULL) {
262                         /*
263                          * The target filesystem got automounted.
264                          * Let the lookup(9) go around with the same
265                          * path component.
266                          */
267                         vput(newvp);
268                         return (ERELOOKUP);
269                 }
270         }
271
272         AUTOFS_SLOCK(amp);
273         error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
274         if (error != 0) {
275                 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
276                         AUTOFS_SUNLOCK(amp);
277                         return (EJUSTRETURN);
278                 }
279
280                 AUTOFS_SUNLOCK(amp);
281                 return (ENOENT);
282         }
283
284         /*
285          * XXX: Dropping the node here is ok, because we never remove nodes.
286          */
287         AUTOFS_SUNLOCK(amp);
288
289         error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp);
290         if (error != 0) {
291                 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
292                         return (EJUSTRETURN);
293
294                 return (error);
295         }
296
297         return (0);
298 }
299
300 static int
301 autofs_mkdir(struct vop_mkdir_args *ap)
302 {
303         struct vnode *vp;
304         struct autofs_node *anp;
305         struct autofs_mount *amp;
306         struct autofs_node *child;
307         int error;
308
309         vp = ap->a_dvp;
310         anp = vp->v_data;
311         amp = VFSTOAUTOFS(vp->v_mount);
312
313         /*
314          * Do not allow mkdir() if the calling thread is not
315          * automountd(8) descendant.
316          */
317         if (autofs_ignore_thread(curthread) == false)
318                 return (EPERM);
319
320         AUTOFS_XLOCK(amp);
321         error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
322             ap->a_cnp->cn_namelen, &child);
323         if (error != 0) {
324                 AUTOFS_XUNLOCK(amp);
325                 return (error);
326         }
327         AUTOFS_XUNLOCK(amp);
328
329         error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp);
330
331         return (error);
332 }
333
334 /*
335  * Write out a single 'struct dirent', based on 'name' and 'fileno' arguments.
336  */
337 static int
338 autofs_readdir_one(struct uio *uio, const char *name, int fileno,
339     size_t *reclenp)
340 {
341         struct dirent dirent;
342         size_t namlen, padded_namlen, reclen;
343         int error;
344
345         namlen = strlen(name);
346         padded_namlen = roundup2(namlen + 1, __alignof(struct dirent));
347         KASSERT(padded_namlen <= MAXNAMLEN, ("%zd > MAXNAMLEN", padded_namlen));
348         reclen = offsetof(struct dirent, d_name) + padded_namlen;
349
350         if (reclenp != NULL)
351                 *reclenp = reclen;
352
353         if (uio == NULL)
354                 return (0);
355
356         if (uio->uio_resid < reclen)
357                 return (EINVAL);
358
359         dirent.d_fileno = fileno;
360         dirent.d_reclen = reclen;
361         dirent.d_type = DT_DIR;
362         dirent.d_namlen = namlen;
363         memcpy(dirent.d_name, name, namlen);
364         memset(dirent.d_name + namlen, 0, padded_namlen - namlen);
365         error = uiomove(&dirent, reclen, uio);
366
367         return (error);
368 }
369
370 static size_t
371 autofs_dirent_reclen(const char *name)
372 {
373         size_t reclen;
374
375         (void)autofs_readdir_one(NULL, name, -1, &reclen);
376
377         return (reclen);
378 }
379
380 static int
381 autofs_readdir(struct vop_readdir_args *ap)
382 {
383         struct vnode *vp, *newvp;
384         struct autofs_mount *amp;
385         struct autofs_node *anp, *child;
386         struct uio *uio;
387         size_t reclen, reclens;
388         ssize_t initial_resid;
389         int error;
390
391         vp = ap->a_vp;
392         amp = VFSTOAUTOFS(vp->v_mount);
393         anp = vp->v_data;
394         uio = ap->a_uio;
395         initial_resid = ap->a_uio->uio_resid;
396
397         KASSERT(vp->v_type == VDIR, ("!VDIR"));
398
399         if (autofs_cached(anp, NULL, 0) == false &&
400             autofs_ignore_thread(curthread) == false) {
401                 error = autofs_trigger_vn(vp, "", 0, &newvp);
402                 if (error != 0)
403                         return (error);
404
405                 if (newvp != NULL) {
406                         error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
407                             ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
408                         vput(newvp);
409                         return (error);
410                 }
411         }
412
413         if (uio->uio_offset < 0)
414                 return (EINVAL);
415
416         if (ap->a_eofflag != NULL)
417                 *ap->a_eofflag = FALSE;
418
419         /*
420          * Write out the directory entry for ".".  This is conditional
421          * on the current offset into the directory; same applies to the
422          * other two cases below.
423          */
424         if (uio->uio_offset == 0) {
425                 error = autofs_readdir_one(uio, ".", anp->an_fileno, &reclen);
426                 if (error != 0)
427                         goto out;
428         }
429         reclens = autofs_dirent_reclen(".");
430
431         /*
432          * Write out the directory entry for "..".
433          */
434         if (uio->uio_offset <= reclens) {
435                 if (uio->uio_offset != reclens)
436                         return (EINVAL);
437                 if (anp->an_parent == NULL) {
438                         error = autofs_readdir_one(uio, "..",
439                             anp->an_fileno, &reclen);
440                 } else {
441                         error = autofs_readdir_one(uio, "..",
442                             anp->an_parent->an_fileno, &reclen);
443                 }
444                 if (error != 0)
445                         goto out;
446         }
447
448         reclens += autofs_dirent_reclen("..");
449
450         /*
451          * Write out the directory entries for subdirectories.
452          */
453         AUTOFS_SLOCK(amp);
454         RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
455                 /*
456                  * Check the offset to skip entries returned by previous
457                  * calls to getdents().
458                  */
459                 if (uio->uio_offset > reclens) {
460                         reclens += autofs_dirent_reclen(child->an_name);
461                         continue;
462                 }
463
464                 /*
465                  * Prevent seeking into the middle of dirent.
466                  */
467                 if (uio->uio_offset != reclens) {
468                         AUTOFS_SUNLOCK(amp);
469                         return (EINVAL);
470                 }
471
472                 error = autofs_readdir_one(uio, child->an_name,
473                     child->an_fileno, &reclen);
474                 reclens += reclen;
475                 if (error != 0) {
476                         AUTOFS_SUNLOCK(amp);
477                         goto out;
478                 }
479         }
480         AUTOFS_SUNLOCK(amp);
481
482         if (ap->a_eofflag != NULL)
483                 *ap->a_eofflag = TRUE;
484
485         return (0);
486
487 out:
488         /*
489          * Return error if the initial buffer was too small to do anything.
490          */
491         if (uio->uio_resid == initial_resid)
492                 return (error);
493
494         /*
495          * Don't return an error if we managed to copy out some entries.
496          */
497         if (uio->uio_resid < reclen)
498                 return (0);
499
500         return (error);
501 }
502
503 static int
504 autofs_reclaim(struct vop_reclaim_args *ap)
505 {
506         struct vnode *vp;
507         struct autofs_node *anp;
508
509         vp = ap->a_vp;
510         anp = vp->v_data;
511
512         /*
513          * We do not free autofs_node here; instead we are
514          * destroying them in autofs_node_delete().
515          */
516         sx_xlock(&anp->an_vnode_lock);
517         anp->an_vnode = NULL;
518         vp->v_data = NULL;
519         sx_xunlock(&anp->an_vnode_lock);
520
521         return (0);
522 }
523
524 struct vop_vector autofs_vnodeops = {
525         .vop_default =          &default_vnodeops,
526
527         .vop_access =           autofs_access,
528         .vop_lookup =           autofs_lookup,
529         .vop_create =           VOP_EOPNOTSUPP,
530         .vop_getattr =          autofs_getattr,
531         .vop_link =             VOP_EOPNOTSUPP,
532         .vop_mkdir =            autofs_mkdir,
533         .vop_mknod =            VOP_EOPNOTSUPP,
534         .vop_read =             VOP_EOPNOTSUPP,
535         .vop_readdir =          autofs_readdir,
536         .vop_remove =           VOP_EOPNOTSUPP,
537         .vop_rename =           VOP_EOPNOTSUPP,
538         .vop_rmdir =            VOP_EOPNOTSUPP,
539         .vop_setattr =          VOP_EOPNOTSUPP,
540         .vop_symlink =          VOP_EOPNOTSUPP,
541         .vop_write =            VOP_EOPNOTSUPP,
542         .vop_reclaim =          autofs_reclaim,
543 };
544
545 int
546 autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
547     const char *name, int namelen, struct autofs_node **anpp)
548 {
549         struct autofs_node *anp;
550
551         if (parent != NULL) {
552                 AUTOFS_ASSERT_XLOCKED(parent->an_mount);
553
554                 KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT,
555                     ("node \"%s\" already exists", name));
556         }
557
558         anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
559         if (namelen >= 0)
560                 anp->an_name = strndup(name, namelen, M_AUTOFS);
561         else
562                 anp->an_name = strdup(name, M_AUTOFS);
563         anp->an_fileno = atomic_fetchadd_int(&amp->am_last_fileno, 1);
564         callout_init(&anp->an_callout, 1);
565         /*
566          * The reason for SX_NOWITNESS here is that witness(4)
567          * cannot tell vnodes apart, so the following perfectly
568          * valid lock order...
569          *
570          * vnode lock A -> autofsvlk B -> vnode lock B
571          *
572          * ... gets reported as a LOR.
573          */
574         sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
575         getnanotime(&anp->an_ctime);
576         anp->an_parent = parent;
577         anp->an_mount = amp;
578         if (parent != NULL)
579                 RB_INSERT(autofs_node_tree, &parent->an_children, anp);
580         RB_INIT(&anp->an_children);
581
582         *anpp = anp;
583         return (0);
584 }
585
586 int
587 autofs_node_find(struct autofs_node *parent, const char *name,
588     int namelen, struct autofs_node **anpp)
589 {
590         struct autofs_node *anp, find;
591         int error;
592
593         AUTOFS_ASSERT_LOCKED(parent->an_mount);
594
595         if (namelen >= 0)
596                 find.an_name = strndup(name, namelen, M_AUTOFS);
597         else
598                 find.an_name = strdup(name, M_AUTOFS);
599
600         anp = RB_FIND(autofs_node_tree, &parent->an_children, &find);
601         if (anp != NULL) {
602                 error = 0;
603                 if (anpp != NULL)
604                         *anpp = anp;
605         } else {
606                 error = ENOENT;
607         }
608
609         free(find.an_name, M_AUTOFS);
610
611         return (error);
612 }
613
614 void
615 autofs_node_delete(struct autofs_node *anp)
616 {
617         struct autofs_node *parent;
618
619         AUTOFS_ASSERT_XLOCKED(anp->an_mount);
620         KASSERT(RB_EMPTY(&anp->an_children), ("have children"));
621
622         callout_drain(&anp->an_callout);
623
624         parent = anp->an_parent;
625         if (parent != NULL)
626                 RB_REMOVE(autofs_node_tree, &parent->an_children, anp);
627         sx_destroy(&anp->an_vnode_lock);
628         free(anp->an_name, M_AUTOFS);
629         uma_zfree(autofs_node_zone, anp);
630 }
631
632 int
633 autofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags,
634     struct vnode **vpp)
635 {
636         struct vnode *vp;
637         int error;
638
639         AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
640
641         sx_xlock(&anp->an_vnode_lock);
642
643         vp = anp->an_vnode;
644         if (vp != NULL) {
645                 error = vget(vp, flags | LK_RETRY, curthread);
646                 if (error != 0) {
647                         AUTOFS_WARN("vget failed with error %d", error);
648                         sx_xunlock(&anp->an_vnode_lock);
649                         return (error);
650                 }
651                 if (vp->v_iflag & VI_DOOMED) {
652                         /*
653                          * We got forcibly unmounted.
654                          */
655                         AUTOFS_DEBUG("doomed vnode");
656                         sx_xunlock(&anp->an_vnode_lock);
657                         vput(vp);
658
659                         return (ENOENT);
660                 }
661
662                 *vpp = vp;
663                 sx_xunlock(&anp->an_vnode_lock);
664                 return (0);
665         }
666
667         error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
668         if (error != 0) {
669                 sx_xunlock(&anp->an_vnode_lock);
670                 return (error);
671         }
672
673         error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
674         if (error != 0) {
675                 sx_xunlock(&anp->an_vnode_lock);
676                 vdrop(vp);
677                 return (error);
678         }
679
680         vp->v_type = VDIR;
681         if (anp->an_parent == NULL)
682                 vp->v_vflag |= VV_ROOT;
683         vp->v_data = anp;
684
685         VN_LOCK_ASHARE(vp);
686
687         error = insmntque(vp, mp);
688         if (error != 0) {
689                 AUTOFS_DEBUG("insmntque() failed with error %d", error);
690                 sx_xunlock(&anp->an_vnode_lock);
691                 return (error);
692         }
693
694         KASSERT(anp->an_vnode == NULL, ("lost race"));
695         anp->an_vnode = vp;
696
697         sx_xunlock(&anp->an_vnode_lock);
698
699         *vpp = vp;
700         return (0);
701 }