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