2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/amd/amfs_nfsx.c
45 * NFS hierarchical mounts
52 #endif /* HAVE_CONFIG_H */
57 * The rfs field contains a list of mounts to be done from
60 typedef struct amfs_nfsx_mnt {
66 int nx_c; /* Number of elements in nx_v */
67 amfs_nfsx_mnt *nx_v; /* Underlying mounts */
68 amfs_nfsx_mnt *nx_try;
72 /* forward definitions */
73 static char *amfs_nfsx_match(am_opts *fo);
74 static int amfs_nfsx_mount(am_node *am, mntfs *mf);
75 static int amfs_nfsx_umount(am_node *am, mntfs *mf);
76 static int amfs_nfsx_init(mntfs *mf);
81 am_ops amfs_nfsx_ops =
88 amfs_error_lookup_child,
89 amfs_error_mount_child,
91 0, /* amfs_nfsx_readlink */
92 0, /* amfs_nfsx_mounted */
93 0, /* amfs_nfsx_umounted */
94 find_nfs_srvr, /* XXX */
95 0, /* amfs_nfsx_get_wchan */
96 /* FS_UBACKGROUND| */ FS_AMQINFO, /* nfs_fs_flags */
99 #endif /* HAVE_FS_AUTOFS */
104 amfs_nfsx_match(am_opts *fo)
111 plog(XLOG_USER, "amfs_nfsx: no remote filesystem specified");
115 if (!fo->opt_rhost) {
116 plog(XLOG_USER, "amfs_nfsx: no remote host specified");
120 /* set default sublink */
121 if (fo->opt_sublink == 0) {
122 ptr = strchr(fo->opt_rfs, ',');
123 if (ptr && ptr > (fo->opt_rfs + 1))
124 fo->opt_sublink = strnsave(fo->opt_rfs + 1, ptr - fo->opt_rfs - 1);
128 * Remove trailing ",..." from ${fs}
129 * After deslashifying, overwrite the end of ${fs} with "/"
130 * to make sure it is unique.
132 if ((ptr = strchr(fo->opt_fs, ',')))
134 deslashify(fo->opt_fs);
137 * Bump string length to allow trailing /
139 len = strlen(fo->opt_fs);
140 fo->opt_fs = xrealloc(fo->opt_fs, len + 1 + 1);
141 ptr = fo->opt_fs + len;
150 * Determine magic cookie to put in mtab
152 xmtab = str3cat((char *) 0, fo->opt_rhost, ":", fo->opt_rfs);
153 dlog("NFSX: mounting remote server \"%s\", remote fs \"%s\" on \"%s\"",
154 fo->opt_rhost, fo->opt_rfs, fo->opt_fs);
161 amfs_nfsx_prfree(opaque_t vp)
163 struct amfs_nfsx *nx = (struct amfs_nfsx *) vp;
166 for (i = 0; i < nx->nx_c; i++) {
167 mntfs *m = nx->nx_v[i].n_mnt;
178 amfs_nfsx_init(mntfs *mf)
181 * mf_info has the form:
182 * host:/prefix/path,sub,sub,sub
186 struct amfs_nfsx *nx;
187 int asked_for_wakeup = 0;
189 nx = (struct amfs_nfsx *) mf->mf_private;
198 info = strdup(mf->mf_info);
199 host = strchr(info, ':');
208 * Split the prefix off from the suffices
210 ivec = strsplit(pref, ',', '\'');
215 for (i = 0; ivec[i]; i++)
218 nx = ALLOC(struct amfs_nfsx);
219 mf->mf_private = (opaque_t) nx;
220 mf->mf_prfree = amfs_nfsx_prfree;
222 nx->nx_c = i - 1; /* i-1 because we don't want the prefix */
223 nx->nx_v = (amfs_nfsx_mnt *) xmalloc(nx->nx_c * sizeof(amfs_nfsx_mnt));
228 char *fs = mf->mf_fo->opt_fs;
230 for (i = 0; i < nx->nx_c; i++) {
231 char *path = ivec[i + 1];
232 rfs = str3cat(rfs, pref, "/", path);
234 * Determine the mount point.
235 * If this is the root, then don't remove
236 * the trailing slash to avoid mntfs name clashes.
238 mp = str3cat(mp, fs, "/", rfs);
242 * Determine the mount info
244 xinfo = str3cat(xinfo, host, *path == '/' ? "" : "/", path);
245 normalize_slash(xinfo);
248 dlog("amfs_nfsx: init mount for %s on %s", xinfo, mp);
249 nx->nx_v[i].n_error = -1;
250 nx->nx_v[i].n_mnt = find_mntfs(&nfs_ops, mf->mf_fo, mp, xinfo, "", mf->mf_mopts, mf->mf_remopts);
251 /* propagate the on_autofs flag */
252 nx->nx_v[i].n_mnt->mf_flags |= mf->mf_flags & MFF_ON_AUTOFS;
271 * Iterate through the mntfs's and call
272 * the underlying init routine on each
276 for (i = 0; i < nx->nx_c; i++) {
277 amfs_nfsx_mnt *n = &nx->nx_v[i];
280 if (m->mf_ops->fs_init && !(mf->mf_flags & MFF_RESTART))
281 error = m->mf_ops->fs_init(m);
283 * if you just "return error" here, you will have made a failure
284 * in any submounts to fail the whole group. There was old unused code
290 else if (error < 0) {
292 if (!asked_for_wakeup) {
293 asked_for_wakeup = 1;
294 sched_task(wakeup_task, (opaque_t) mf, get_mntfs_wchan(m));
304 amfs_nfsx_cont(int rc, int term, opaque_t arg)
306 mntfs *mf = (mntfs *) arg;
307 struct amfs_nfsx *nx = (struct amfs_nfsx *) mf->mf_private;
308 am_node *mp = nx->nx_mp;
309 amfs_nfsx_mnt *n = nx->nx_try;
311 n->n_mnt->mf_flags &= ~(MFF_ERROR | MFF_MOUNTING);
312 mf->mf_flags &= ~MFF_ERROR;
315 * Wakeup anything waiting for this mount
317 wakeup(get_mntfs_wchan(n->n_mnt));
322 * Not sure what to do for an error code.
324 plog(XLOG_ERROR, "mount for %s got signal %d", n->n_mnt->mf_mount, term);
328 * Check for exit status
330 errno = rc; /* XXX */
331 plog(XLOG_ERROR, "%s: mount (amfs_nfsx_cont): %m", n->n_mnt->mf_mount);
334 free_mntfs(n->n_mnt);
335 n->n_mnt = new_mntfs();
336 n->n_mnt->mf_error = n->n_error;
337 n->n_mnt->mf_flags |= MFF_ERROR;
342 mf_mounted(n->n_mnt, FALSE); /* FALSE => don't free the n_mnt->am_opts */
347 * Do the remaining bits
349 if (amfs_nfsx_mount(mp, mf) >= 0)
350 wakeup(get_mntfs_wchan(mf));
355 try_amfs_nfsx_mount(opaque_t mv)
357 mntfs *mf = (mntfs *) mv;
358 struct amfs_nfsx *nx = (struct amfs_nfsx *) mf->mf_private;
359 am_node *mp = nx->nx_mp;
362 error = mf->mf_ops->mount_fs(mp, mf);
369 amfs_nfsx_remount(am_node *am, mntfs *mf, int fg)
371 struct amfs_nfsx *nx = (struct amfs_nfsx *) mf->mf_private;
375 /* Save the am_node pointer for later use */
379 * Iterate through the mntfs's and mount each filesystem
380 * which is not yet mounted.
382 for (n = nx->nx_v; n < nx->nx_v + nx->nx_c; n++) {
385 if (m->mf_flags & MFF_MOUNTING)
388 if (m->mf_flags & MFF_MOUNTED) {
389 mf_mounted(m, FALSE); /* FALSE => don't free the m->am_opts */
390 n->n_error = glob_error = 0;
394 if (n->n_error < 0) {
395 /* Create the mountpoint, if and as required */
396 if (!(m->mf_flags & MFF_MKMNT) && m->mf_fsflags & FS_MKMNT) {
397 if (!mkdirs(m->mf_mount, 0555))
398 m->mf_flags |= MFF_MKMNT;
401 dlog("calling underlying mount on %s", m->mf_mount);
402 if (!fg && foreground && (m->mf_fsflags & FS_MBACKGROUND)) {
403 m->mf_flags |= MFF_MOUNTING;
404 dlog("backgrounding mount of \"%s\"", m->mf_info);
406 run_task(try_amfs_nfsx_mount, (opaque_t) m, amfs_nfsx_cont, (opaque_t) mf);
410 dlog("foreground mount of \"%s\" ...", mf->mf_info);
411 n->n_error = m->mf_ops->mount_fs(am, m);
415 dlog("underlying fmount of %s failed: %s", m->mf_mount, strerror(n->n_error));
417 if (n->n_error == 0) {
419 } else if (glob_error < 0) {
420 glob_error = n->n_error;
425 return glob_error < 0 ? 0 : glob_error;
430 amfs_nfsx_mount(am_node *am, mntfs *mf)
432 return amfs_nfsx_remount(am, mf, FALSE);
437 * Unmount an NFS hierarchy.
438 * Note that this is called in the foreground
439 * and so may hang under extremely rare conditions.
442 amfs_nfsx_umount(am_node *am, mntfs *mf)
444 struct amfs_nfsx *nx = (struct amfs_nfsx *) mf->mf_private;
449 * Iterate in reverse through the mntfs's and unmount each filesystem
452 for (n = nx->nx_v + nx->nx_c - 1; n >= nx->nx_v; --n) {
455 * If this node has not been messed with
456 * and there has been no error so far
457 * then try and unmount.
458 * If an error had occurred then zero
459 * the error code so that the remount
460 * only tries to unmount those nodes
461 * which had been successfully unmounted.
463 if (n->n_error == 0) {
464 dlog("calling underlying fumount on %s", m->mf_mount);
465 n->n_error = m->mf_ops->umount_fs(am, m);
467 glob_error = n->n_error;
471 * Make sure remount gets this node
479 * If any unmounts failed then remount the
483 glob_error = amfs_nfsx_remount(am, mf, TRUE);
485 errno = glob_error; /* XXX */
486 plog(XLOG_USER, "amfs_nfsx: remount of %s failed: %m", mf->mf_mount);
491 * Remove all the mount points
493 for (n = nx->nx_v; n < nx->nx_v + nx->nx_c; n++) {
495 dlog("calling underlying umounted on %s", m->mf_mount);
496 if (m->mf_ops->umounted)
497 m->mf_ops->umounted(m);
499 if (n->n_error < 0) {
500 if (m->mf_fsflags & FS_MKMNT) {
501 (void) rmdirs(m->mf_mount);
502 m->mf_flags &= ~MFF_MKMNT;