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