138494Sobrien/*
2174294Sobrien * Copyright (c) 1997-2006 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.
1938494Sobrien * 3. All advertising materials mentioning features or use of this software
2042629Sobrien *    must display the following acknowledgment:
2138494Sobrien *      This product includes software developed by the University of
2238494Sobrien *      California, Berkeley and its contributors.
2338494Sobrien * 4. Neither the name of the University nor the names of its contributors
2438494Sobrien *    may be used to endorse or promote products derived from this software
2538494Sobrien *    without specific prior written permission.
2638494Sobrien *
2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3038494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3738494Sobrien * SUCH DAMAGE.
3838494Sobrien *
3938494Sobrien *
40174294Sobrien * File: am-utils/amd/amfs_union.c
4138494Sobrien *
4238494Sobrien */
4338494Sobrien
4438494Sobrien/*
4538494Sobrien * Union automounter file system
4638494Sobrien */
4738494Sobrien
4838494Sobrien#ifdef HAVE_CONFIG_H
4938494Sobrien# include <config.h>
5038494Sobrien#endif /* HAVE_CONFIG_H */
5138494Sobrien#include <am_defs.h>
5238494Sobrien#include <amd.h>
5338494Sobrien
5438494Sobrien/****************************************************************************
5538494Sobrien *** FORWARD DEFINITIONS                                                  ***
5638494Sobrien ****************************************************************************/
57174294Sobrienstatic int create_amfs_union_node(char *dir, opaque_t arg);
5838494Sobrienstatic void amfs_union_mounted(mntfs *mf);
5938494Sobrien
6038494Sobrien
6138494Sobrien/****************************************************************************
6238494Sobrien *** OPS STRUCTURES                                                       ***
6338494Sobrien ****************************************************************************/
6438494Sobrienam_ops amfs_union_ops =
6538494Sobrien{
6638494Sobrien  "union",
67174294Sobrien  amfs_generic_match,
68174294Sobrien  0,				/* amfs_union_init */
6938494Sobrien  amfs_toplvl_mount,
7038494Sobrien  amfs_toplvl_umount,
71174294Sobrien  amfs_generic_lookup_child,
72174294Sobrien  amfs_generic_mount_child,
73174294Sobrien  amfs_generic_readdir,
74174294Sobrien  0,				/* amfs_union_readlink */
7538494Sobrien  amfs_union_mounted,
76174294Sobrien  0,				/* amfs_union_umounted */
77174294Sobrien  amfs_generic_find_srvr,
78174294Sobrien  0,				/* amfs_union_get_wchan */
79174294Sobrien  FS_MKMNT | FS_NOTIMEOUT | FS_BACKGROUND | FS_AMQINFO | FS_DIRECTORY,
80174294Sobrien#ifdef HAVE_FS_AUTOFS
81174294Sobrien  AUTOFS_UNION_FS_FLAGS,
82174294Sobrien#endif /* HAVE_FS_AUTOFS */
8338494Sobrien};
8438494Sobrien
8538494Sobrien
8638494Sobrien/*
8738494Sobrien * Create a reference to a union'ed entry
8838494Sobrien * XXX: this function may not be used anywhere...
8938494Sobrien */
9038494Sobrienstatic int
91174294Sobriencreate_amfs_union_node(char *dir, opaque_t arg)
9238494Sobrien{
9338494Sobrien  if (!STREQ(dir, "/defaults")) {
9438494Sobrien    int error = 0;
95174294Sobrien    am_node *am;
96174294Sobrien    am = amfs_generic_lookup_child(arg, dir, &error, VLOOK_CREATE);
97174294Sobrien    if (am && error < 0)
98174294Sobrien      am = amfs_generic_mount_child(am, &error);
9938494Sobrien    if (error > 0) {
10038494Sobrien      errno = error;		/* XXX */
10182794Sobrien      plog(XLOG_ERROR, "unionfs: could not mount %s: %m", dir);
10238494Sobrien    }
10338494Sobrien    return error;
10438494Sobrien  }
10538494Sobrien  return 0;
10638494Sobrien}
10738494Sobrien
10838494Sobrien
10938494Sobrienstatic void
11038494Sobrienamfs_union_mounted(mntfs *mf)
11138494Sobrien{
112174294Sobrien  int index;
113174294Sobrien  am_node *mp;
11438494Sobrien
115174294Sobrien  amfs_mkcacheref(mf);
11638494Sobrien
11738494Sobrien  /*
11838494Sobrien   * Having made the union mount point,
11938494Sobrien   * populate all the entries...
12038494Sobrien   */
121174294Sobrien  for (mp = get_first_exported_ap(&index);
122174294Sobrien       mp;
123174294Sobrien       mp = get_next_exported_ap(&index)) {
124174294Sobrien    if (mp->am_mnt == mf) {
12538494Sobrien      /* return value from create_amfs_union_node is ignored by mapc_keyiter */
12638494Sobrien      (void) mapc_keyiter((mnt_map *) mp->am_mnt->mf_private,
127174294Sobrien			  create_amfs_union_node,
12838494Sobrien			  mp);
12938494Sobrien      break;
13038494Sobrien    }
13138494Sobrien  }
13238494Sobrien}
133