]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/fs/autofs/autofs_vfsops.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / fs / autofs / autofs_vfsops.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/systm.h>
36 #include <sys/conf.h>
37 #include <sys/condvar.h>
38 #include <sys/ioccom.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/mount.h>
42 #include <sys/sx.h>
43 #include <sys/taskqueue.h>
44 #include <sys/vnode.h>
45
46 #include <fs/autofs/autofs.h>
47
48 static const char *autofs_opts[] = {
49         "from", "master_options", "master_prefix", NULL
50 };
51
52 extern struct autofs_softc      *autofs_softc;
53
54 static int
55 autofs_mount(struct mount *mp)
56 {
57         struct autofs_mount *amp;
58         char *from, *fspath, *options, *prefix;
59         int error;
60
61         if (vfs_filteropt(mp->mnt_optnew, autofs_opts))
62                 return (EINVAL);
63
64         if (mp->mnt_flag & MNT_UPDATE) {
65                 autofs_flush(VFSTOAUTOFS(mp));
66                 return (0);
67         }
68
69         if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
70                 return (EINVAL);
71
72         if (vfs_getopt(mp->mnt_optnew, "fspath", (void **)&fspath, NULL))
73                 return (EINVAL);
74
75         if (vfs_getopt(mp->mnt_optnew, "master_options", (void **)&options, NULL))
76                 return (EINVAL);
77
78         if (vfs_getopt(mp->mnt_optnew, "master_prefix", (void **)&prefix, NULL))
79                 return (EINVAL);
80
81         amp = malloc(sizeof(*amp), M_AUTOFS, M_WAITOK | M_ZERO);
82         mp->mnt_data = amp;
83         amp->am_mp = mp;
84         strlcpy(amp->am_from, from, sizeof(amp->am_from));
85         strlcpy(amp->am_mountpoint, fspath, sizeof(amp->am_mountpoint));
86         strlcpy(amp->am_options, options, sizeof(amp->am_options));
87         strlcpy(amp->am_prefix, prefix, sizeof(amp->am_prefix));
88         sx_init(&amp->am_lock, "autofslk");
89         amp->am_last_fileno = 1;
90
91         vfs_getnewfsid(mp);
92
93         MNT_ILOCK(mp);
94         mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
95         MNT_IUNLOCK(mp);
96
97         AUTOFS_XLOCK(amp);
98         error = autofs_node_new(NULL, amp, ".", -1, &amp->am_root);
99         if (error != 0) {
100                 AUTOFS_XUNLOCK(amp);
101                 free(amp, M_AUTOFS);
102                 return (error);
103         }
104         AUTOFS_XUNLOCK(amp);
105
106         vfs_mountedfrom(mp, from);
107
108         return (0);
109 }
110
111 static int
112 autofs_unmount(struct mount *mp, int mntflags)
113 {
114         struct autofs_mount *amp;
115         struct autofs_node *anp;
116         struct autofs_request *ar;
117         int error, flags;
118         bool found;
119
120         amp = VFSTOAUTOFS(mp);
121
122         flags = 0;
123         if (mntflags & MNT_FORCE)
124                 flags |= FORCECLOSE;
125         error = vflush(mp, 0, flags, curthread);
126         if (error != 0) {
127                 AUTOFS_WARN("vflush failed with error %d", error);
128                 return (error);
129         }
130
131         /*
132          * All vnodes are gone, and new one will not appear - so,
133          * no new triggerings.  We can iterate over outstanding
134          * autofs_requests and terminate them.
135          */
136         for (;;) {
137                 found = false;
138                 sx_xlock(&autofs_softc->sc_lock);
139                 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
140                         if (ar->ar_mount != amp)
141                                 continue;
142                         ar->ar_error = ENXIO;
143                         ar->ar_done = true;
144                         ar->ar_in_progress = false;
145                         found = true;
146                 }
147                 sx_xunlock(&autofs_softc->sc_lock);
148                 if (found == false)
149                         break;
150
151                 cv_broadcast(&autofs_softc->sc_cv);
152                 pause("autofs_umount", 1);
153         }
154
155         AUTOFS_XLOCK(amp);
156
157         /*
158          * Not terribly efficient, but at least not recursive.
159          */
160         while (!TAILQ_EMPTY(&amp->am_root->an_children)) {
161                 anp = TAILQ_FIRST(&amp->am_root->an_children);
162                 while (!TAILQ_EMPTY(&anp->an_children))
163                         anp = TAILQ_FIRST(&anp->an_children);
164                 autofs_node_delete(anp);
165         }
166         autofs_node_delete(amp->am_root);
167
168         mp->mnt_data = NULL;
169         AUTOFS_XUNLOCK(amp);
170
171         sx_destroy(&amp->am_lock);
172
173         free(amp, M_AUTOFS);
174
175         return (0);
176 }
177
178 static int
179 autofs_root(struct mount *mp, int flags, struct vnode **vpp)
180 {
181         struct autofs_mount *amp;
182         int error;
183
184         amp = VFSTOAUTOFS(mp);
185
186         error = autofs_node_vn(amp->am_root, mp, flags, vpp);
187
188         return (error);
189 }
190
191 static int
192 autofs_statfs(struct mount *mp, struct statfs *sbp)
193 {
194
195         sbp->f_bsize = 512;
196         sbp->f_iosize = 0;
197         sbp->f_blocks = 0;
198         sbp->f_bfree = 0;
199         sbp->f_bavail = 0;
200         sbp->f_files = 0;
201         sbp->f_ffree = 0;
202
203         return (0);
204 }
205
206 static struct vfsops autofs_vfsops = {
207         .vfs_fhtovp =           NULL, /* XXX */
208         .vfs_mount =            autofs_mount,
209         .vfs_unmount =          autofs_unmount,
210         .vfs_root =             autofs_root,
211         .vfs_statfs =           autofs_statfs,
212         .vfs_init =             autofs_init,
213         .vfs_uninit =           autofs_uninit,
214 };
215
216 VFS_SET(autofs_vfsops, autofs, VFCF_SYNTHETIC | VFCF_NETWORK);
217 MODULE_VERSION(autofs, 1);