amfs_auto.c revision 225736
1/*
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.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
26 *
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
37 * SUCH DAMAGE.
38 *
39 *
40 * File: am-utils/amd/amfs_auto.c
41 *
42 */
43
44/*
45 * Automount file system
46 */
47
48#ifdef HAVE_CONFIG_H
49# include <config.h>
50#endif /* HAVE_CONFIG_H */
51#include <am_defs.h>
52#include <amd.h>
53
54/****************************************************************************
55 *** MACROS                                                               ***
56 ****************************************************************************/
57
58
59/****************************************************************************
60 *** STRUCTURES                                                           ***
61 ****************************************************************************/
62
63
64/****************************************************************************
65 *** FORWARD DEFINITIONS                                                  ***
66 ****************************************************************************/
67static int amfs_auto_mount(am_node *mp, mntfs *mf);
68
69
70/****************************************************************************
71 *** OPS STRUCTURES                                                       ***
72 ****************************************************************************/
73am_ops amfs_auto_ops =
74{
75  "auto",
76  amfs_generic_match,
77  0,				/* amfs_auto_init */
78  amfs_auto_mount,
79  amfs_generic_umount,
80  amfs_generic_lookup_child,
81  amfs_generic_mount_child,
82  amfs_generic_readdir,
83  0,				/* amfs_auto_readlink */
84  amfs_generic_mounted,
85  0,				/* amfs_auto_umounted */
86  amfs_generic_find_srvr,
87  0,				/* amfs_auto_get_wchan */
88  FS_AMQINFO | FS_DIRECTORY,
89#ifdef HAVE_FS_AUTOFS
90  AUTOFS_AUTO_FS_FLAGS,
91#endif /* HAVE_FS_AUTOFS */
92};
93
94
95/****************************************************************************
96 *** FUNCTIONS                                                             ***
97 ****************************************************************************/
98/*
99 * Mount a sub-mount
100 */
101static int
102amfs_auto_mount(am_node *mp, mntfs *mf)
103{
104  /*
105   * Pseudo-directories are used to provide some structure
106   * to the automounted directories instead
107   * of putting them all in the top-level automount directory.
108   *
109   * Here, just increment the parent's link count.
110   */
111  mp->am_parent->am_fattr.na_nlink++;
112
113  /*
114   * Info field of . means use parent's info field.
115   * Historical - not documented.
116   */
117  if (mf->mf_info[0] == '.' && mf->mf_info[1] == '\0')
118    mf->mf_info = strealloc(mf->mf_info, mp->am_parent->am_mnt->mf_info);
119
120  /*
121   * Compute prefix:
122   *
123   * If there is an option prefix then use that else
124   * If the parent had a prefix then use that with name
125   *      of this node appended else
126   * Use the name of this node.
127   *
128   * That means if you want no prefix you must say so
129   * in the map.
130   */
131  if (mf->mf_fo->opt_pref) {
132    /* allow pref:=null to set a real null prefix */
133    if (STREQ(mf->mf_fo->opt_pref, "null")) {
134      mp->am_pref = strdup("");
135    } else {
136      /*
137       * the prefix specified as an option
138       */
139      mp->am_pref = strdup(mf->mf_fo->opt_pref);
140    }
141  } else {
142    /*
143     * else the parent's prefix
144     * followed by the name
145     * followed by /
146     */
147    char *ppref = mp->am_parent->am_pref;
148    if (ppref == 0)
149      ppref = "";
150    mp->am_pref = str3cat((char *) 0, ppref, mp->am_name, "/");
151  }
152
153#ifdef HAVE_FS_AUTOFS
154  if (mf->mf_flags & MFF_IS_AUTOFS) {
155    char opts[SIZEOF_OPTS];
156    int error;
157
158    autofs_get_opts(opts, sizeof(opts), mp->am_autofs_fh);
159
160    /* now do the mount */
161    error = amfs_mount(mp, mf, opts);
162    if (error) {
163      errno = error;
164      plog(XLOG_FATAL, "amfs_auto_mount: amfs_mount failed: %m");
165      return error;
166    }
167  }
168#endif /* HAVE_FS_AUTOFS */
169
170  /*
171   * Attach a map cache
172   */
173  amfs_mkcacheref(mf);
174
175  return 0;
176}
177