138494Sobrien/*
2310490Scy * Copyright (c) 1997-2014 Erez Zadok
338494Sobrien * Copyright (c) 1990 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1990 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
19310490Scy * 3. Neither the name of the University nor the names of its contributors
2038494Sobrien *    may be used to endorse or promote products derived from this software
2138494Sobrien *    without specific prior written permission.
2238494Sobrien *
2338494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2438494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2538494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2638494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2738494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2838494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2938494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3038494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3138494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3238494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3338494Sobrien * SUCH DAMAGE.
3438494Sobrien *
3538494Sobrien *
36174294Sobrien * File: am-utils/amd/amfs_auto.c
3738494Sobrien *
3838494Sobrien */
3938494Sobrien
4038494Sobrien/*
4138494Sobrien * Automount file system
4238494Sobrien */
4338494Sobrien
4438494Sobrien#ifdef HAVE_CONFIG_H
4538494Sobrien# include <config.h>
4638494Sobrien#endif /* HAVE_CONFIG_H */
4738494Sobrien#include <am_defs.h>
4838494Sobrien#include <amd.h>
4938494Sobrien
5038494Sobrien/****************************************************************************
5138494Sobrien *** MACROS                                                               ***
5238494Sobrien ****************************************************************************/
5338494Sobrien
5438494Sobrien
5538494Sobrien/****************************************************************************
5638494Sobrien *** STRUCTURES                                                           ***
5738494Sobrien ****************************************************************************/
5838494Sobrien
5938494Sobrien
6038494Sobrien/****************************************************************************
6138494Sobrien *** FORWARD DEFINITIONS                                                  ***
6238494Sobrien ****************************************************************************/
63174294Sobrienstatic int amfs_auto_mount(am_node *mp, mntfs *mf);
6438494Sobrien
6538494Sobrien
6638494Sobrien/****************************************************************************
6738494Sobrien *** OPS STRUCTURES                                                       ***
6838494Sobrien ****************************************************************************/
6938494Sobrienam_ops amfs_auto_ops =
7038494Sobrien{
7138494Sobrien  "auto",
72174294Sobrien  amfs_generic_match,
7338494Sobrien  0,				/* amfs_auto_init */
7438494Sobrien  amfs_auto_mount,
75174294Sobrien  amfs_generic_umount,
76174294Sobrien  amfs_generic_lookup_child,
77174294Sobrien  amfs_generic_mount_child,
78174294Sobrien  amfs_generic_readdir,
7938494Sobrien  0,				/* amfs_auto_readlink */
80174294Sobrien  amfs_generic_mounted,
81174294Sobrien  0,				/* amfs_auto_umounted */
82174294Sobrien  amfs_generic_find_srvr,
83174294Sobrien  0,				/* amfs_auto_get_wchan */
84174294Sobrien  FS_AMQINFO | FS_DIRECTORY,
85174294Sobrien#ifdef HAVE_FS_AUTOFS
86174294Sobrien  AUTOFS_AUTO_FS_FLAGS,
87174294Sobrien#endif /* HAVE_FS_AUTOFS */
8838494Sobrien};
8938494Sobrien
9038494Sobrien
9138494Sobrien/****************************************************************************
9238494Sobrien *** FUNCTIONS                                                             ***
9338494Sobrien ****************************************************************************/
9438494Sobrien/*
9538494Sobrien * Mount a sub-mount
9638494Sobrien */
9738494Sobrienstatic int
98174294Sobrienamfs_auto_mount(am_node *mp, mntfs *mf)
9938494Sobrien{
100310490Scy  if (mp->am_parent == NULL)
101310490Scy    return EINVAL;
10238494Sobrien  /*
10338494Sobrien   * Pseudo-directories are used to provide some structure
10438494Sobrien   * to the automounted directories instead
10538494Sobrien   * of putting them all in the top-level automount directory.
10638494Sobrien   *
10738494Sobrien   * Here, just increment the parent's link count.
10838494Sobrien   */
10938494Sobrien  mp->am_parent->am_fattr.na_nlink++;
11038494Sobrien
11138494Sobrien  /*
11238494Sobrien   * Info field of . means use parent's info field.
11338494Sobrien   * Historical - not documented.
11438494Sobrien   */
11538494Sobrien  if (mf->mf_info[0] == '.' && mf->mf_info[1] == '\0')
116310490Scy    mf->mf_info = strealloc(mf->mf_info, mp->am_parent->am_al->al_mnt->mf_info);
11738494Sobrien
11838494Sobrien  /*
11938494Sobrien   * Compute prefix:
12038494Sobrien   *
12138494Sobrien   * If there is an option prefix then use that else
12238494Sobrien   * If the parent had a prefix then use that with name
12338494Sobrien   *      of this node appended else
12438494Sobrien   * Use the name of this node.
12538494Sobrien   *
12638494Sobrien   * That means if you want no prefix you must say so
12738494Sobrien   * in the map.
12838494Sobrien   */
12938494Sobrien  if (mf->mf_fo->opt_pref) {
13038494Sobrien    /* allow pref:=null to set a real null prefix */
13138494Sobrien    if (STREQ(mf->mf_fo->opt_pref, "null")) {
132310490Scy      mp->am_pref = xstrdup("");
13338494Sobrien    } else {
13438494Sobrien      /*
13538494Sobrien       * the prefix specified as an option
13638494Sobrien       */
137310490Scy      mp->am_pref = xstrdup(mf->mf_fo->opt_pref);
13838494Sobrien    }
13938494Sobrien  } else {
14038494Sobrien    /*
14138494Sobrien     * else the parent's prefix
14238494Sobrien     * followed by the name
14338494Sobrien     * followed by /
14438494Sobrien     */
14538494Sobrien    char *ppref = mp->am_parent->am_pref;
14638494Sobrien    if (ppref == 0)
14738494Sobrien      ppref = "";
148310490Scy    mp->am_pref = str3cat((char *) NULL, ppref, mp->am_name, "/");
14938494Sobrien  }
15038494Sobrien
151174294Sobrien#ifdef HAVE_FS_AUTOFS
152174294Sobrien  if (mf->mf_flags & MFF_IS_AUTOFS) {
153174294Sobrien    char opts[SIZEOF_OPTS];
154174294Sobrien    int error;
15538494Sobrien
156174294Sobrien    autofs_get_opts(opts, sizeof(opts), mp->am_autofs_fh);
15738494Sobrien
158174294Sobrien    /* now do the mount */
159174294Sobrien    error = amfs_mount(mp, mf, opts);
160174294Sobrien    if (error) {
161174294Sobrien      errno = error;
162174294Sobrien      plog(XLOG_FATAL, "amfs_auto_mount: amfs_mount failed: %m");
163174294Sobrien      return error;
16438494Sobrien    }
16538494Sobrien  }
166174294Sobrien#endif /* HAVE_FS_AUTOFS */
16738494Sobrien
16838494Sobrien  /*
169174294Sobrien   * Attach a map cache
17038494Sobrien   */
171174294Sobrien  amfs_mkcacheref(mf);
17238494Sobrien
17338494Sobrien  return 0;
17438494Sobrien}
175