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