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