1 1.1 christos /* $NetBSD: amfs_toplvl.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1.1.3 christos * Copyright (c) 1997-2014 Erez Zadok 5 1.1 christos * Copyright (c) 1990 Jan-Simon Pendry 6 1.1 christos * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 7 1.1 christos * Copyright (c) 1990 The Regents of the University of California. 8 1.1 christos * All rights reserved. 9 1.1 christos * 10 1.1 christos * This code is derived from software contributed to Berkeley by 11 1.1 christos * Jan-Simon Pendry at Imperial College, London. 12 1.1 christos * 13 1.1 christos * Redistribution and use in source and binary forms, with or without 14 1.1 christos * modification, are permitted provided that the following conditions 15 1.1 christos * are met: 16 1.1 christos * 1. Redistributions of source code must retain the above copyright 17 1.1 christos * notice, this list of conditions and the following disclaimer. 18 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 19 1.1 christos * notice, this list of conditions and the following disclaimer in the 20 1.1 christos * documentation and/or other materials provided with the distribution. 21 1.1.1.3 christos * 3. Neither the name of the University nor the names of its contributors 22 1.1 christos * may be used to endorse or promote products derived from this software 23 1.1 christos * without specific prior written permission. 24 1.1 christos * 25 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.1 christos * SUCH DAMAGE. 36 1.1 christos * 37 1.1 christos * 38 1.1 christos * File: am-utils/amd/amfs_toplvl.c 39 1.1 christos * 40 1.1 christos */ 41 1.1 christos 42 1.1 christos /* 43 1.1 christos * Top-level file system 44 1.1 christos */ 45 1.1 christos 46 1.1 christos #ifdef HAVE_CONFIG_H 47 1.1 christos # include <config.h> 48 1.1 christos #endif /* HAVE_CONFIG_H */ 49 1.1 christos #include <am_defs.h> 50 1.1 christos #include <amd.h> 51 1.1 christos 52 1.1 christos /**************************************************************************** 53 1.1 christos *** FORWARD DEFINITIONS *** 54 1.1 christos ****************************************************************************/ 55 1.1 christos static int amfs_toplvl_init(mntfs *mf); 56 1.1 christos 57 1.1 christos /**************************************************************************** 58 1.1 christos *** OPS STRUCTURES *** 59 1.1 christos ****************************************************************************/ 60 1.1 christos am_ops amfs_toplvl_ops = 61 1.1 christos { 62 1.1 christos "toplvl", 63 1.1 christos amfs_generic_match, 64 1.1 christos amfs_toplvl_init, /* amfs_toplvl_init */ 65 1.1 christos amfs_toplvl_mount, 66 1.1 christos amfs_toplvl_umount, 67 1.1 christos amfs_generic_lookup_child, 68 1.1 christos amfs_generic_mount_child, 69 1.1 christos amfs_generic_readdir, 70 1.1 christos 0, /* amfs_toplvl_readlink */ 71 1.1 christos amfs_generic_mounted, 72 1.1 christos 0, /* amfs_toplvl_umounted */ 73 1.1 christos amfs_generic_find_srvr, 74 1.1 christos 0, /* amfs_toplvl_get_wchan */ 75 1.1 christos FS_MKMNT | FS_NOTIMEOUT | FS_BACKGROUND | 76 1.1 christos FS_AMQINFO | FS_DIRECTORY, /* nfs_fs_flags */ 77 1.1 christos #ifdef HAVE_FS_AUTOFS 78 1.1 christos AUTOFS_TOPLVL_FS_FLAGS, 79 1.1 christos #endif /* HAVE_FS_AUTOFS */ 80 1.1 christos }; 81 1.1 christos 82 1.1 christos 83 1.1 christos /**************************************************************************** 84 1.1 christos *** FUNCTIONS *** 85 1.1 christos ****************************************************************************/ 86 1.1 christos 87 1.1 christos static void 88 1.1 christos set_auto_attrcache_timeout(char *preopts, char *opts, size_t l) 89 1.1 christos { 90 1.1 christos 91 1.1 christos #ifdef MNTTAB_OPT_NOAC 92 1.1 christos /* 93 1.1 christos * Don't cache attributes - they are changing under the kernel's feet. 94 1.1 christos * For example, IRIX5.2 will dispense with nfs lookup calls and hand stale 95 1.1 christos * filehandles to getattr unless we disable attribute caching on the 96 1.1 christos * automount points. 97 1.1 christos */ 98 1.1 christos if (gopt.auto_attrcache == 0) { 99 1.1 christos xsnprintf(preopts, l, ",%s", MNTTAB_OPT_NOAC); 100 1.1 christos xstrlcat(opts, preopts, l); 101 1.1 christos } 102 1.1 christos #endif /* MNTTAB_OPT_NOAC */ 103 1.1 christos 104 1.1 christos /* 105 1.1 christos * XXX: note that setting these to 0 in the past resulted in an error on 106 1.1 christos * some systems, which is why it's better to use "noac" if possible. For 107 1.1 christos * now, we're setting everything possible, but if this will cause trouble, 108 1.1 christos * then we'll have to condition the remainder of this on OPT_NOAC. 109 1.1 christos */ 110 1.1 christos #ifdef MNTTAB_OPT_ACTIMEO 111 1.1 christos xsnprintf(preopts, l, ",%s=%d", MNTTAB_OPT_ACTIMEO, gopt.auto_attrcache); 112 1.1 christos xstrlcat(opts, preopts, l); 113 1.1 christos #else /* MNTTAB_OPT_ACTIMEO */ 114 1.1 christos # ifdef MNTTAB_OPT_ACDIRMIN 115 1.1 christos xsnprintf(preopts, l, ",%s=%d", MNTTAB_OPT_ACTDIRMIN, gopt.auto_attrcache); 116 1.1 christos xstrlcat(opts, preopts, l); 117 1.1 christos # endif /* MNTTAB_OPT_ACDIRMIN */ 118 1.1 christos # ifdef MNTTAB_OPT_ACDIRMAX 119 1.1 christos xsnprintf(preopts, l, ",%s=%d", MNTTAB_OPT_ACTDIRMAX, gopt.auto_attrcache); 120 1.1 christos xstrlcat(opts, preopts, l); 121 1.1 christos # endif /* MNTTAB_OPT_ACDIRMAX */ 122 1.1 christos # ifdef MNTTAB_OPT_ACREGMIN 123 1.1 christos xsnprintf(preopts, l, ",%s=%d", MNTTAB_OPT_ACTREGMIN, gopt.auto_attrcache); 124 1.1 christos xstrlcat(opts, preopts, l); 125 1.1 christos # endif /* MNTTAB_OPT_ACREGMIN */ 126 1.1 christos # ifdef MNTTAB_OPT_ACREGMAX 127 1.1 christos xsnprintf(preopts, l, ",%s=%d", MNTTAB_OPT_ACTREGMAX, gopt.auto_attrcache); 128 1.1 christos xstrlcat(opts, preopts, l); 129 1.1 christos # endif /* MNTTAB_OPT_ACREGMAX */ 130 1.1 christos #endif /* MNTTAB_OPT_ACTIMEO */ 131 1.1 christos } 132 1.1 christos 133 1.1 christos 134 1.1 christos /* 135 1.1 christos * Initialize a top-level mount. In our case, if the user asked for 136 1.1 christos * forced_unmounts, and the OS supports it, then we try forced/lazy unmounts 137 1.1 christos * on any previous toplvl mounts. This is useful if a previous Amd died and 138 1.1 christos * left behind toplvl mount points (this Amd will clean them up). 139 1.1 christos * 140 1.1 christos * WARNING: Don't use forced/lazy unmounts if you have another valid Amd 141 1.1 christos * running, because this code WILL force those valid toplvl mount points to 142 1.1 christos * be detached as well! 143 1.1 christos */ 144 1.1 christos static int 145 1.1 christos amfs_toplvl_init(mntfs *mf) 146 1.1 christos { 147 1.1 christos int error = 0; 148 1.1 christos 149 1.1 christos #if (defined(MNT2_GEN_OPT_FORCE) || defined(MNT2_GEN_OPT_DETACH)) && (defined(HAVE_UVMOUNT) || defined(HAVE_UMOUNT2)) 150 1.1 christos if (gopt.flags & CFM_FORCED_UNMOUNTS) { 151 1.1 christos plog(XLOG_INFO, "amfs_toplvl_init: trying forced/lazy unmount of %s", 152 1.1 christos mf->mf_mount); 153 1.1 christos error = umount2_fs(mf->mf_mount, AMU_UMOUNT_FORCE | AMU_UMOUNT_DETACH); 154 1.1 christos if (error) 155 1.1 christos plog(XLOG_INFO, "amfs_toplvl_init: forced/lazy unmount failed: %m"); 156 1.1 christos else 157 1.1 christos dlog("amfs_toplvl_init: forced/lazy unmount succeeded"); 158 1.1 christos } 159 1.1 christos #endif /* (MNT2_GEN_OPT_FORCE || MNT2_GEN_OPT_DETACH) && (HAVE_UVMOUNT || HAVE_UMOUNT2) */ 160 1.1 christos return error; 161 1.1 christos } 162 1.1 christos 163 1.1 christos 164 1.1 christos /* 165 1.1 christos * Mount the top-level 166 1.1 christos */ 167 1.1 christos int 168 1.1 christos amfs_toplvl_mount(am_node *mp, mntfs *mf) 169 1.1 christos { 170 1.1 christos struct stat stb; 171 1.1 christos char opts[SIZEOF_OPTS], preopts[SIZEOF_OPTS], toplvl_opts[40]; 172 1.1 christos int error; 173 1.1 christos 174 1.1 christos /* 175 1.1 christos * Mounting the automounter. 176 1.1 christos * Make sure the mount directory exists, construct 177 1.1 christos * the mount options and call the mount_amfs_toplvl routine. 178 1.1 christos */ 179 1.1 christos 180 1.1 christos if (stat(mp->am_path, &stb) < 0) { 181 1.1 christos return errno; 182 1.1 christos } else if ((stb.st_mode & S_IFMT) != S_IFDIR) { 183 1.1 christos plog(XLOG_WARNING, "%s is not a directory", mp->am_path); 184 1.1 christos return ENOTDIR; 185 1.1 christos } 186 1.1 christos 187 1.1 christos /* 188 1.1 christos * Construct some mount options: 189 1.1 christos * 190 1.1 christos * Tack on magic map=<mapname> option in mtab to emulate 191 1.1 christos * SunOS automounter behavior. 192 1.1 christos */ 193 1.1 christos 194 1.1 christos #ifdef HAVE_FS_AUTOFS 195 1.1 christos if (mf->mf_flags & MFF_IS_AUTOFS) { 196 1.1 christos autofs_get_opts(opts, sizeof(opts), mp->am_autofs_fh); 197 1.1 christos } else 198 1.1 christos #endif /* HAVE_FS_AUTOFS */ 199 1.1 christos { 200 1.1 christos preopts[0] = '\0'; 201 1.1 christos #ifdef MNTTAB_OPT_INTR 202 1.1 christos xstrlcat(preopts, MNTTAB_OPT_INTR, sizeof(preopts)); 203 1.1 christos xstrlcat(preopts, ",", sizeof(preopts)); 204 1.1 christos #endif /* MNTTAB_OPT_INTR */ 205 1.1 christos #ifdef MNTTAB_OPT_IGNORE 206 1.1 christos xstrlcat(preopts, MNTTAB_OPT_IGNORE, sizeof(preopts)); 207 1.1 christos xstrlcat(preopts, ",", sizeof(preopts)); 208 1.1 christos #endif /* MNTTAB_OPT_IGNORE */ 209 1.1 christos /* write most of the initial options + preopts */ 210 1.1 christos xsnprintf(opts, sizeof(opts), "%s%s,%s=%d,%s,map=%s", 211 1.1 christos preopts, 212 1.1 christos MNTTAB_OPT_RW, 213 1.1 christos MNTTAB_OPT_PORT, nfs_port, 214 1.1 christos mf->mf_ops->fs_type, mf->mf_info); 215 1.1 christos 216 1.1 christos /* process toplvl timeo/retrans options, if any */ 217 1.1 christos if (gopt.amfs_auto_timeo[AMU_TYPE_TOPLVL] > 0) { 218 1.1 christos xsnprintf(toplvl_opts, sizeof(toplvl_opts), ",%s=%d", 219 1.1 christos MNTTAB_OPT_TIMEO, gopt.amfs_auto_timeo[AMU_TYPE_TOPLVL]); 220 1.1 christos xstrlcat(opts, toplvl_opts, sizeof(opts)); 221 1.1 christos } 222 1.1 christos if (gopt.amfs_auto_retrans[AMU_TYPE_TOPLVL] > 0) { 223 1.1 christos xsnprintf(toplvl_opts, sizeof(toplvl_opts), ",%s=%d", 224 1.1 christos MNTTAB_OPT_RETRANS, gopt.amfs_auto_retrans[AMU_TYPE_TOPLVL]); 225 1.1 christos xstrlcat(opts, toplvl_opts, sizeof(opts)); 226 1.1 christos } 227 1.1 christos 228 1.1 christos #ifdef MNTTAB_OPT_NOLOCK 229 1.1 christos xstrlcat(opts, ",", sizeof(opts)); 230 1.1 christos xstrlcat(opts, MNTTAB_OPT_NOLOCK, sizeof(opts)); 231 1.1 christos #endif /* MNTTAB_OPT_NOLOCK */ 232 1.1 christos 233 1.1 christos #ifdef MNTTAB_OPT_NOAC 234 1.1 christos if (gopt.auto_attrcache == 0) { 235 1.1 christos xstrlcat(opts, ",", sizeof(opts)); 236 1.1 christos xstrlcat(opts, MNTTAB_OPT_NOAC, sizeof(opts)); 237 1.1 christos } else 238 1.1 christos #endif /* MNTTAB_OPT_NOAC */ 239 1.1 christos set_auto_attrcache_timeout(preopts, opts, sizeof(preopts)); 240 1.1 christos } 241 1.1 christos 242 1.1 christos /* now do the mount */ 243 1.1 christos error = amfs_mount(mp, mf, opts); 244 1.1 christos if (error) { 245 1.1 christos errno = error; 246 1.1 christos plog(XLOG_FATAL, "amfs_toplvl_mount: amfs_mount failed: %m"); 247 1.1 christos return error; 248 1.1 christos } 249 1.1 christos return 0; 250 1.1 christos } 251 1.1 christos 252 1.1 christos 253 1.1 christos /* 254 1.1 christos * Unmount a top-level automount node 255 1.1 christos */ 256 1.1 christos int 257 1.1 christos amfs_toplvl_umount(am_node *mp, mntfs *mf) 258 1.1 christos { 259 1.1 christos struct stat stb; 260 1.1 christos int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0; 261 1.1 christos int error; 262 1.1 christos int count = 0; /* how many times did we try to unmount? */ 263 1.1 christos 264 1.1 christos again: 265 1.1 christos /* 266 1.1 christos * The lstat is needed if this mount is type=direct. 267 1.1 christos * When that happens, the kernel cache gets confused 268 1.1 christos * between the underlying type (dir) and the mounted 269 1.1 christos * type (link) and so needs to be re-synced before 270 1.1 christos * the unmount. This is all because the unmount system 271 1.1 christos * call follows links and so can't actually unmount 272 1.1 christos * a link (stupid!). It was noted that doing an ls -ld 273 1.1 christos * of the mount point to see why things were not working 274 1.1 christos * actually fixed the problem - so simulate an ls -ld here. 275 1.1 christos */ 276 1.1 christos if (lstat(mp->am_path, &stb) < 0) { 277 1.1 christos error = errno; 278 1.1 christos dlog("lstat(%s): %m", mp->am_path); 279 1.1 christos goto out; 280 1.1 christos } 281 1.1 christos if ((stb.st_mode & S_IFMT) != S_IFDIR) { 282 1.1 christos plog(XLOG_ERROR, "amfs_toplvl_umount: %s is not a directory, aborting.", mp->am_path); 283 1.1 christos error = ENOTDIR; 284 1.1 christos goto out; 285 1.1 christos } 286 1.1 christos 287 1.1 christos error = UMOUNT_FS(mp->am_path, mnttab_file_name, unmount_flags); 288 1.1 christos if (error == EBUSY) { 289 1.1 christos #ifdef HAVE_FS_AUTOFS 290 1.1 christos /* 291 1.1 christos * autofs mounts are "in place", so it is possible 292 1.1 christos * that we can't just unmount our mount points and go away. 293 1.1 christos * If that's the case, just give up. 294 1.1 christos */ 295 1.1 christos if (mf->mf_flags & MFF_IS_AUTOFS) 296 1.1 christos return error; 297 1.1 christos #endif /* HAVE_FS_AUTOFS */ 298 1.1 christos plog(XLOG_WARNING, "amfs_toplvl_unmount retrying %s in 1s", mp->am_path); 299 1.1 christos count++; 300 1.1 christos sleep(1); 301 1.1 christos /* 302 1.1 christos * If user wants forced/lazy unmount semantics, then set those flags, 303 1.1 christos * but only after we've tried normal lstat/umount a few times -- 304 1.1 christos * otherwise forced unmounts may hang this very same Amd (by preventing 305 1.1 christos * it from achieving a clean unmount). 306 1.1 christos */ 307 1.1 christos if (gopt.flags & CFM_FORCED_UNMOUNTS) { 308 1.1 christos if (count == 5) { /* after 5 seconds, try MNT_FORCE */ 309 1.1 christos dlog("enabling forced unmounts for toplvl node %s", mp->am_path); 310 1.1 christos unmount_flags |= AMU_UMOUNT_FORCE; 311 1.1 christos } 312 1.1 christos if (count == 10) { /* after 10 seconds, try MNT_DETACH */ 313 1.1 christos dlog("enabling detached unmounts for toplvl node %s", mp->am_path); 314 1.1 christos unmount_flags |= AMU_UMOUNT_DETACH; 315 1.1 christos } 316 1.1 christos } 317 1.1 christos goto again; 318 1.1 christos } 319 1.1 christos out: 320 1.1 christos return error; 321 1.1 christos } 322