]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/autofs/autofs_vnops.c
Fix autofs triggering problem. Assume you have an NFS server,
[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/systm.h>
45 #include <sys/taskqueue.h>
46 #include <sys/vnode.h>
47 #include <machine/atomic.h>
48 #include <vm/uma.h>
49
50 #include <fs/autofs/autofs.h>
51
52 static int      autofs_trigger_vn(struct vnode *vp, const char *path,
53                     int pathlen, struct vnode **newvp);
54
55 extern struct autofs_softc      *autofs_softc;
56
57 static int
58 autofs_access(struct vop_access_args *ap)
59 {
60
61         /*
62          * Nothing to do here; the only kind of access control
63          * needed is in autofs_mkdir().
64          */
65
66         return (0);
67 }
68
69 static int
70 autofs_getattr(struct vop_getattr_args *ap)
71 {
72         struct vnode *vp, *newvp;
73         struct autofs_node *anp;
74         struct mount *mp;
75         struct vattr *vap;
76         int error;
77
78         vp = ap->a_vp;
79         anp = vp->v_data;
80         mp = vp->v_mount;
81         vap = ap->a_vap;
82
83         KASSERT(ap->a_vp->v_type == VDIR, ("!VDIR"));
84
85         /*
86          * The reason we must do this is that some tree-walking software,
87          * namely fts(3), assumes that stat(".") results will not change
88          * between chdir("subdir") and chdir(".."), and fails with ENOENT
89          * otherwise.
90          */
91         if (autofs_mount_on_stat && autofs_cached(anp, NULL, 0) == false &&
92             autofs_ignore_thread(curthread) == false) {
93                 error = autofs_trigger_vn(vp, "", 0, &newvp);
94                 if (error != 0)
95                         return (error);
96
97                 if (newvp != NULL) {
98                         error = VOP_GETATTR(newvp, ap->a_vap,
99                             ap->a_cred);
100                         vput(newvp);
101                         return (error);
102                 }
103         }
104
105         vap->va_type = VDIR;
106         vap->va_mode = 0755;
107         vap->va_nlink = 3; /* XXX */
108         vap->va_uid = 0;
109         vap->va_gid = 0;
110         vap->va_rdev = NODEV;
111         vap->va_fsid = mp->mnt_stat.f_fsid.val[0];
112         vap->va_fileid = anp->an_fileno;
113         vap->va_size = 512; /* XXX */
114         vap->va_blocksize = 512;
115         vap->va_mtime = anp->an_ctime;
116         vap->va_atime = anp->an_ctime;
117         vap->va_ctime = anp->an_ctime;
118         vap->va_birthtime = anp->an_ctime;
119         vap->va_gen = 0;
120         vap->va_flags = 0;
121         vap->va_rdev = 0;
122         vap->va_bytes = 512; /* XXX */
123         vap->va_filerev = 0;
124         vap->va_spare = 0;
125
126         return (0);
127 }
128
129 /*
130  * Unlock the vnode, request automountd(8) action, and then lock it back.
131  * If anything got mounted on top of the vnode, return the new filesystem's
132  * root vnode in 'newvp', locked.
133  */
134 static int
135 autofs_trigger_vn(struct vnode *vp, const char *path, int pathlen,
136     struct vnode **newvp)
137 {
138         struct autofs_node *anp;
139         struct autofs_mount *amp;
140         int error, lock_flags;
141
142         anp = vp->v_data;
143         amp = VFSTOAUTOFS(vp->v_mount);
144
145         /*
146          * Release the vnode lock, so that other operations, in partcular
147          * mounting a filesystem on top of it, can proceed.  Increase use
148          * count, to prevent the vnode from being deallocated and to prevent
149          * filesystem from being unmounted.
150          */
151         lock_flags = VOP_ISLOCKED(vp);
152         vref(vp);
153         VOP_UNLOCK(vp, 0);
154
155         sx_xlock(&autofs_softc->sc_lock);
156
157         /*
158          * XXX: Workaround for mounting the same thing multiple times; revisit.
159          */
160         if (vp->v_mountedhere != NULL) {
161                 error = 0;
162                 goto mounted;
163         }
164
165         error = autofs_trigger(anp, path, pathlen);
166 mounted:
167         sx_xunlock(&autofs_softc->sc_lock);
168         vn_lock(vp, lock_flags | LK_RETRY);
169         vunref(vp);
170         if ((vp->v_iflag & VI_DOOMED) != 0) {
171                 AUTOFS_DEBUG("VI_DOOMED");
172                 return (ENOENT);
173         }
174
175         if (error != 0)
176                 return (error);
177
178         if (vp->v_mountedhere == NULL) {
179                 *newvp = NULL;
180                 return (0);
181         } else {
182                 /*
183                  * If the operation that succeeded was mount, then mark
184                  * the node as non-cached.  Otherwise, if someone unmounts
185                  * the filesystem before the cache times out, we will fail
186                  * to trigger.
187                  */
188                 anp->an_cached = false;
189         }
190
191         error = VFS_ROOT(vp->v_mountedhere, lock_flags, newvp);
192         if (error != 0) {
193                 AUTOFS_WARN("VFS_ROOT() failed with error %d", error);
194                 return (error);
195         }
196
197         return (0);
198 }
199
200 static int
201 autofs_vget_callback(struct mount *mp, void *arg, int flags,
202     struct vnode **vpp)
203 {
204
205
206         return (autofs_node_vn(arg, mp, flags, vpp));
207 }
208
209 static int
210 autofs_lookup(struct vop_lookup_args *ap)
211 {
212         struct vnode *dvp, *newvp, **vpp;
213         struct mount *mp;
214         struct autofs_mount *amp;
215         struct autofs_node *anp, *child;
216         struct componentname *cnp;
217         int error;
218
219         dvp = ap->a_dvp;
220         vpp = ap->a_vpp;
221         mp = dvp->v_mount;
222         amp = VFSTOAUTOFS(mp);
223         anp = dvp->v_data;
224         cnp = ap->a_cnp;
225
226         if (cnp->cn_flags & ISDOTDOT) {
227                 KASSERT(anp->an_parent != NULL, ("NULL parent"));
228                 /*
229                  * Note that in this case, dvp is the child vnode, and we
230                  * are looking up the parent vnode - exactly reverse from
231                  * normal operation.  Unlocking dvp requires some rather
232                  * tricky unlock/relock dance to prevent mp from being freed;
233                  * use vn_vget_ino_gen() which takes care of all that.
234                  */
235                 error = vn_vget_ino_gen(dvp, autofs_vget_callback,
236                     anp->an_parent, cnp->cn_lkflags, vpp);
237                 if (error != 0) {
238                         AUTOFS_WARN("vn_vget_ino_gen() failed with error %d",
239                             error);
240                         return (error);
241                 }
242                 return (error);
243         }
244
245         if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
246                 vref(dvp);
247                 *vpp = dvp;
248
249                 return (0);
250         }
251
252         if (autofs_cached(anp, cnp->cn_nameptr, cnp->cn_namelen) == false &&
253             autofs_ignore_thread(cnp->cn_thread) == false) {
254                 error = autofs_trigger_vn(dvp,
255                     cnp->cn_nameptr, cnp->cn_namelen, &newvp);
256                 if (error != 0)
257                         return (error);
258
259                 if (newvp != NULL) {
260                         /*
261                          * The target filesystem got automounted.
262                          * Let the lookup(9) go around with the same
263                          * path component.
264                          */
265                         vput(newvp);
266                         return (ERELOOKUP);
267                 }
268         }
269
270         AUTOFS_SLOCK(amp);
271         error = autofs_node_find(anp, cnp->cn_nameptr, cnp->cn_namelen, &child);
272         if (error != 0) {
273                 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
274                         AUTOFS_SUNLOCK(amp);
275                         return (EJUSTRETURN);
276                 }
277
278                 AUTOFS_SUNLOCK(amp);
279                 return (ENOENT);
280         }
281
282         /*
283          * XXX: Dropping the node here is ok, because we never remove nodes.
284          */
285         AUTOFS_SUNLOCK(amp);
286
287         error = autofs_node_vn(child, mp, cnp->cn_lkflags, vpp);
288         if (error != 0) {
289                 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE)
290                         return (EJUSTRETURN);
291
292                 return (error);
293         }
294
295         return (0);
296 }
297
298 static int
299 autofs_mkdir(struct vop_mkdir_args *ap)
300 {
301         struct vnode *vp;
302         struct autofs_node *anp;
303         struct autofs_mount *amp;
304         struct autofs_node *child;
305         int error;
306
307         vp = ap->a_dvp;
308         anp = vp->v_data;
309         amp = VFSTOAUTOFS(vp->v_mount);
310
311         /*
312          * Do not allow mkdir() if the calling thread is not
313          * automountd(8) descendant.
314          */
315         if (autofs_ignore_thread(curthread) == false)
316                 return (EPERM);
317
318         AUTOFS_XLOCK(amp);
319         error = autofs_node_new(anp, amp, ap->a_cnp->cn_nameptr,
320             ap->a_cnp->cn_namelen, &child);
321         if (error != 0) {
322                 AUTOFS_XUNLOCK(amp);
323                 return (error);
324         }
325         AUTOFS_XUNLOCK(amp);
326
327         error = autofs_node_vn(child, vp->v_mount, LK_EXCLUSIVE, ap->a_vpp);
328
329         return (error);
330 }
331
332 static int
333 autofs_readdir_one(struct uio *uio, const char *name, int fileno)
334 {
335         struct dirent dirent;
336         int error, i;
337
338         memset(&dirent, 0, sizeof(dirent));
339         dirent.d_type = DT_DIR;
340         dirent.d_reclen = AUTOFS_DELEN;
341         dirent.d_fileno = fileno;
342         /* PFS_DELEN was picked to fit PFS_NAMLEN */
343         for (i = 0; i < AUTOFS_NAMELEN - 1 && name[i] != '\0'; ++i)
344                 dirent.d_name[i] = name[i];
345         dirent.d_name[i] = 0;
346         dirent.d_namlen = i;
347
348         error = uiomove(&dirent, AUTOFS_DELEN, uio);
349         return (error);
350 }
351
352 static int
353 autofs_readdir(struct vop_readdir_args *ap)
354 {
355         struct vnode *vp, *newvp;
356         struct autofs_mount *amp;
357         struct autofs_node *anp, *child;
358         struct uio *uio;
359         off_t offset;
360         int error, i, resid;
361
362         vp = ap->a_vp;
363         amp = VFSTOAUTOFS(vp->v_mount);
364         anp = vp->v_data;
365         uio = ap->a_uio;
366
367         KASSERT(vp->v_type == VDIR, ("!VDIR"));
368
369         if (autofs_cached(anp, NULL, 0) == false &&
370             autofs_ignore_thread(curthread) == false) {
371                 error = autofs_trigger_vn(vp, "", 0, &newvp);
372                 if (error != 0)
373                         return (error);
374
375                 if (newvp != NULL) {
376                         error = VOP_READDIR(newvp, ap->a_uio, ap->a_cred,
377                             ap->a_eofflag, ap->a_ncookies, ap->a_cookies);
378                         vput(newvp);
379                         return (error);
380                 }
381         }
382
383         /* only allow reading entire entries */
384         offset = uio->uio_offset;
385         resid = uio->uio_resid;
386         if (offset < 0 || offset % AUTOFS_DELEN != 0 ||
387             (resid && resid < AUTOFS_DELEN))
388                 return (EINVAL);
389         if (resid == 0)
390                 return (0);
391
392         if (ap->a_eofflag != NULL)
393                 *ap->a_eofflag = TRUE;
394
395         if (offset == 0 && resid >= AUTOFS_DELEN) {
396                 error = autofs_readdir_one(uio, ".", anp->an_fileno);
397                 if (error != 0)
398                         return (error);
399                 offset += AUTOFS_DELEN;
400                 resid -= AUTOFS_DELEN;
401         }
402
403         if (offset == AUTOFS_DELEN && resid >= AUTOFS_DELEN) {
404                 if (anp->an_parent == NULL) {
405                         /*
406                          * XXX: Right?
407                          */
408                         error = autofs_readdir_one(uio, "..", anp->an_fileno);
409                 } else {
410                         error = autofs_readdir_one(uio, "..",
411                             anp->an_parent->an_fileno);
412                 }
413                 if (error != 0)
414                         return (error);
415                 offset += AUTOFS_DELEN;
416                 resid -= AUTOFS_DELEN;
417         }
418
419         i = 2; /* Account for "." and "..". */
420         AUTOFS_SLOCK(amp);
421         TAILQ_FOREACH(child, &anp->an_children, an_next) {
422                 if (resid < AUTOFS_DELEN) {
423                         if (ap->a_eofflag != NULL)
424                                 *ap->a_eofflag = 0;
425                         break;
426                 }
427
428                 /*
429                  * Skip entries returned by previous call to getdents().
430                  */
431                 i++;
432                 if (i * AUTOFS_DELEN <= offset)
433                         continue;
434
435                 error = autofs_readdir_one(uio, child->an_name,
436                     child->an_fileno);
437                 if (error != 0) {
438                         AUTOFS_SUNLOCK(amp);
439                         return (error);
440                 }
441                 offset += AUTOFS_DELEN;
442                 resid -= AUTOFS_DELEN;
443         }
444
445         AUTOFS_SUNLOCK(amp);
446         return (0);
447 }
448
449 static int
450 autofs_reclaim(struct vop_reclaim_args *ap)
451 {
452         struct vnode *vp;
453         struct autofs_node *anp;
454
455         vp = ap->a_vp;
456         anp = vp->v_data;
457
458         /*
459          * We do not free autofs_node here; instead we are
460          * destroying them in autofs_node_delete().
461          */
462         sx_xlock(&anp->an_vnode_lock);
463         anp->an_vnode = NULL;
464         vp->v_data = NULL;
465         sx_xunlock(&anp->an_vnode_lock);
466
467         return (0);
468 }
469
470 struct vop_vector autofs_vnodeops = {
471         .vop_default =          &default_vnodeops,
472
473         .vop_access =           autofs_access,
474         .vop_lookup =           autofs_lookup,
475         .vop_create =           VOP_EOPNOTSUPP,
476         .vop_getattr =          autofs_getattr,
477         .vop_link =             VOP_EOPNOTSUPP,
478         .vop_mkdir =            autofs_mkdir,
479         .vop_mknod =            VOP_EOPNOTSUPP,
480         .vop_read =             VOP_EOPNOTSUPP,
481         .vop_readdir =          autofs_readdir,
482         .vop_remove =           VOP_EOPNOTSUPP,
483         .vop_rename =           VOP_EOPNOTSUPP,
484         .vop_rmdir =            VOP_EOPNOTSUPP,
485         .vop_setattr =          VOP_EOPNOTSUPP,
486         .vop_symlink =          VOP_EOPNOTSUPP,
487         .vop_write =            VOP_EOPNOTSUPP,
488         .vop_reclaim =          autofs_reclaim,
489 };
490
491 int
492 autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
493     const char *name, int namelen, struct autofs_node **anpp)
494 {
495         struct autofs_node *anp;
496
497         if (parent != NULL) {
498                 AUTOFS_ASSERT_XLOCKED(parent->an_mount);
499
500                 KASSERT(autofs_node_find(parent, name, namelen, NULL) == ENOENT,
501                     ("node \"%s\" already exists", name));
502         }
503
504         anp = uma_zalloc(autofs_node_zone, M_WAITOK | M_ZERO);
505         if (namelen >= 0)
506                 anp->an_name = strndup(name, namelen, M_AUTOFS);
507         else
508                 anp->an_name = strdup(name, M_AUTOFS);
509         anp->an_fileno = atomic_fetchadd_int(&amp->am_last_fileno, 1);
510         callout_init(&anp->an_callout, 1);
511         /*
512          * The reason for SX_NOWITNESS here is that witness(4)
513          * cannot tell vnodes apart, so the following perfectly
514          * valid lock order...
515          *
516          * vnode lock A -> autofsvlk B -> vnode lock B
517          *
518          * ... gets reported as a LOR.
519          */
520         sx_init_flags(&anp->an_vnode_lock, "autofsvlk", SX_NOWITNESS);
521         getnanotime(&anp->an_ctime);
522         anp->an_parent = parent;
523         anp->an_mount = amp;
524         if (parent != NULL)
525                 TAILQ_INSERT_TAIL(&parent->an_children, anp, an_next);
526         TAILQ_INIT(&anp->an_children);
527
528         *anpp = anp;
529         return (0);
530 }
531
532 int
533 autofs_node_find(struct autofs_node *parent, const char *name,
534     int namelen, struct autofs_node **anpp)
535 {
536         struct autofs_node *anp;
537
538         AUTOFS_ASSERT_LOCKED(parent->an_mount);
539
540         TAILQ_FOREACH(anp, &parent->an_children, an_next) {
541                 if (namelen >= 0) {
542                         if (strlen(anp->an_name) != namelen)
543                                 continue;
544                         if (strncmp(anp->an_name, name, namelen) != 0)
545                                 continue;
546                 } else {
547                         if (strcmp(anp->an_name, name) != 0)
548                                 continue;
549                 }
550
551                 if (anpp != NULL)
552                         *anpp = anp;
553                 return (0);
554         }
555
556         return (ENOENT);
557 }
558
559 void
560 autofs_node_delete(struct autofs_node *anp)
561 {
562         struct autofs_node *parent;
563
564         AUTOFS_ASSERT_XLOCKED(anp->an_mount);
565         KASSERT(TAILQ_EMPTY(&anp->an_children), ("have children"));
566
567         callout_drain(&anp->an_callout);
568
569         parent = anp->an_parent;
570         if (parent != NULL)
571                 TAILQ_REMOVE(&parent->an_children, anp, an_next);
572         sx_destroy(&anp->an_vnode_lock);
573         free(anp->an_name, M_AUTOFS);
574         uma_zfree(autofs_node_zone, anp);
575 }
576
577 int
578 autofs_node_vn(struct autofs_node *anp, struct mount *mp, int flags,
579     struct vnode **vpp)
580 {
581         struct vnode *vp;
582         int error;
583
584         AUTOFS_ASSERT_UNLOCKED(anp->an_mount);
585
586         sx_xlock(&anp->an_vnode_lock);
587
588         vp = anp->an_vnode;
589         if (vp != NULL) {
590                 error = vget(vp, flags | LK_RETRY, curthread);
591                 if (error != 0) {
592                         AUTOFS_WARN("vget failed with error %d", error);
593                         sx_xunlock(&anp->an_vnode_lock);
594                         return (error);
595                 }
596                 if (vp->v_iflag & VI_DOOMED) {
597                         /*
598                          * We got forcibly unmounted.
599                          */
600                         AUTOFS_DEBUG("doomed vnode");
601                         sx_xunlock(&anp->an_vnode_lock);
602                         vput(vp);
603
604                         return (ENOENT);
605                 }
606
607                 *vpp = vp;
608                 sx_xunlock(&anp->an_vnode_lock);
609                 return (0);
610         }
611
612         error = getnewvnode("autofs", mp, &autofs_vnodeops, &vp);
613         if (error != 0) {
614                 sx_xunlock(&anp->an_vnode_lock);
615                 return (error);
616         }
617
618         error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
619         if (error != 0) {
620                 sx_xunlock(&anp->an_vnode_lock);
621                 vdrop(vp);
622                 return (error);
623         }
624
625         vp->v_type = VDIR;
626         if (anp->an_parent == NULL)
627                 vp->v_vflag |= VV_ROOT;
628         vp->v_data = anp;
629
630         VN_LOCK_ASHARE(vp);
631
632         error = insmntque(vp, mp);
633         if (error != 0) {
634                 AUTOFS_WARN("insmntque() failed with error %d", error);
635                 sx_xunlock(&anp->an_vnode_lock);
636                 return (error);
637         }
638
639         KASSERT(anp->an_vnode == NULL, ("lost race"));
640         anp->an_vnode = vp;
641
642         sx_xunlock(&anp->an_vnode_lock);
643
644         *vpp = vp;
645         return (0);
646 }