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