1*31bdb48aSchristos /* $NetBSD: amfs_nfsl.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */
2a53f50b9Schristos
3a53f50b9Schristos /*
4*31bdb48aSchristos * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos * Copyright (c) 1990 Jan-Simon Pendry
6a53f50b9Schristos * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7a53f50b9Schristos * Copyright (c) 1990 The Regents of the University of California.
8a53f50b9Schristos * All rights reserved.
9a53f50b9Schristos *
10a53f50b9Schristos * This code is derived from software contributed to Berkeley by
11a53f50b9Schristos * Jan-Simon Pendry at Imperial College, London.
12a53f50b9Schristos *
13a53f50b9Schristos * Redistribution and use in source and binary forms, with or without
14a53f50b9Schristos * modification, are permitted provided that the following conditions
15a53f50b9Schristos * are met:
16a53f50b9Schristos * 1. Redistributions of source code must retain the above copyright
17a53f50b9Schristos * notice, this list of conditions and the following disclaimer.
18a53f50b9Schristos * 2. Redistributions in binary form must reproduce the above copyright
19a53f50b9Schristos * notice, this list of conditions and the following disclaimer in the
20a53f50b9Schristos * documentation and/or other materials provided with the distribution.
21*31bdb48aSchristos * 3. Neither the name of the University nor the names of its contributors
22a53f50b9Schristos * may be used to endorse or promote products derived from this software
23a53f50b9Schristos * without specific prior written permission.
24a53f50b9Schristos *
25a53f50b9Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26a53f50b9Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27a53f50b9Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28a53f50b9Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29a53f50b9Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30a53f50b9Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31a53f50b9Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32a53f50b9Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33a53f50b9Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34a53f50b9Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35a53f50b9Schristos * SUCH DAMAGE.
36a53f50b9Schristos *
37a53f50b9Schristos *
38a53f50b9Schristos * File: am-utils/amd/amfs_nfsl.c
39a53f50b9Schristos *
40a53f50b9Schristos */
41a53f50b9Schristos
42a53f50b9Schristos /*
43a53f50b9Schristos * NFSL: Network file system with local existence check. If the local
44a53f50b9Schristos * path denoted by $rfs exists, it behaves as type:=link.
45a53f50b9Schristos *
46a53f50b9Schristos * Example:
47a53f50b9Schristos * pkg type:=nfsl;rhost:=jonny;rfs:=/n/johnny/src/pkg;fs:=${rfs}
48a53f50b9Schristos */
49a53f50b9Schristos
50a53f50b9Schristos #ifdef HAVE_CONFIG_H
51a53f50b9Schristos # include <config.h>
52a53f50b9Schristos #endif /* HAVE_CONFIG_H */
53a53f50b9Schristos #include <am_defs.h>
54a53f50b9Schristos #include <amd.h>
55a53f50b9Schristos
56a53f50b9Schristos
57a53f50b9Schristos /* forward declarations */
58a53f50b9Schristos static char *amfs_nfsl_match(am_opts *fo);
59a53f50b9Schristos static int amfs_nfsl_init(mntfs *mf);
60a53f50b9Schristos static int amfs_nfsl_mount(am_node *mp, mntfs *mf);
61a53f50b9Schristos static int amfs_nfsl_umount(am_node *mp, mntfs *mf);
62a53f50b9Schristos static void amfs_nfsl_umounted(mntfs *mf);
63a53f50b9Schristos static fserver *amfs_nfsl_ffserver(mntfs *mf);
64a53f50b9Schristos
65a53f50b9Schristos /*
66a53f50b9Schristos * NFS-Link operations
67a53f50b9Schristos */
68a53f50b9Schristos am_ops amfs_nfsl_ops =
69a53f50b9Schristos {
70a53f50b9Schristos "nfsl",
71a53f50b9Schristos amfs_nfsl_match,
72a53f50b9Schristos amfs_nfsl_init,
73a53f50b9Schristos amfs_nfsl_mount,
74a53f50b9Schristos amfs_nfsl_umount,
75a53f50b9Schristos amfs_error_lookup_child,
76a53f50b9Schristos amfs_error_mount_child,
77a53f50b9Schristos amfs_error_readdir,
78a53f50b9Schristos 0, /* amfs_nfsl_readlink */
79a53f50b9Schristos 0, /* amfs_nfsl_mounted */
80a53f50b9Schristos amfs_nfsl_umounted,
81a53f50b9Schristos amfs_nfsl_ffserver,
82a53f50b9Schristos 0, /* amfs_nfsl_get_wchan */
83a53f50b9Schristos FS_MKMNT | FS_BACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
84a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
85a53f50b9Schristos AUTOFS_NFSL_FS_FLAGS,
86a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
87a53f50b9Schristos };
88a53f50b9Schristos
89a53f50b9Schristos
90a53f50b9Schristos /*
91a53f50b9Schristos * Check that f/s has all needed fields.
92a53f50b9Schristos * Returns: matched string if found, NULL otherwise.
93a53f50b9Schristos */
94a53f50b9Schristos static char *
amfs_nfsl_match(am_opts * fo)95a53f50b9Schristos amfs_nfsl_match(am_opts *fo)
96a53f50b9Schristos {
97a53f50b9Schristos char *cp;
98a53f50b9Schristos char *ho = fo->opt_rhost;
99a53f50b9Schristos char *retval;
100a53f50b9Schristos struct stat stb;
101a53f50b9Schristos
1024bcd344eSchristos if (fo->opt_sublink && fo->opt_sublink[0])
103a53f50b9Schristos cp = fo->opt_sublink;
104a53f50b9Schristos else
105a53f50b9Schristos cp = fo->opt_fs;
106a53f50b9Schristos
107a53f50b9Schristos if (!cp || !ho) {
108a53f50b9Schristos plog(XLOG_USER, "amfs_nfsl: host $fs and $rhost must be specified");
109a53f50b9Schristos return NULL;
110a53f50b9Schristos }
111a53f50b9Schristos
112a53f50b9Schristos /*
113a53f50b9Schristos * If this host is not the same as $rhost, or if link does not exist,
114a53f50b9Schristos * call nfs_ops.fs_match().
115a53f50b9Schristos * If link value exists (or same host), call amfs_link_ops.fs_match().
116a53f50b9Schristos */
117*31bdb48aSchristos if (!STRCEQ(ho, am_get_hostname()) && !STRCEQ(ho, hostd)) {
118*31bdb48aSchristos plog(XLOG_INFO, "amfs_nfsl: \"%s\" is not the local host \"%s\", "
119*31bdb48aSchristos "or \"%s\" using type:=nfs", ho, am_get_hostname(), hostd);
120a53f50b9Schristos retval = nfs_ops.fs_match(fo);
121a53f50b9Schristos } else if (lstat(cp, &stb) < 0) {
122a53f50b9Schristos plog(XLOG_INFO, "amfs_nfsl: \"%s\" does not exist, using type:=nfs", cp);
123a53f50b9Schristos retval = nfs_ops.fs_match(fo);
124a53f50b9Schristos } else {
125a53f50b9Schristos plog(XLOG_INFO, "amfs_nfsl: \"%s\" exists, using type:=link", cp);
126a53f50b9Schristos retval = amfs_link_ops.fs_match(fo);
127a53f50b9Schristos }
128a53f50b9Schristos return retval;
129a53f50b9Schristos }
130a53f50b9Schristos
131a53f50b9Schristos
132a53f50b9Schristos /*
133a53f50b9Schristos * Initialize.
134a53f50b9Schristos * Returns: 0 if OK, non-zero (errno) if failed.
135a53f50b9Schristos */
136a53f50b9Schristos static int
amfs_nfsl_init(mntfs * mf)137a53f50b9Schristos amfs_nfsl_init(mntfs *mf)
138a53f50b9Schristos {
139a53f50b9Schristos int ret = 0;
140a53f50b9Schristos if (mf->mf_flags & MFF_NFSLINK) {
141a53f50b9Schristos if (amfs_link_ops.fs_init)
142a53f50b9Schristos ret = amfs_link_ops.fs_init(mf);
143a53f50b9Schristos } else {
144a53f50b9Schristos if (nfs_ops.fs_init)
145a53f50b9Schristos ret = nfs_ops.fs_init(mf);
146a53f50b9Schristos }
147a53f50b9Schristos return ret;
148a53f50b9Schristos }
149a53f50b9Schristos
150a53f50b9Schristos
151a53f50b9Schristos /*
152a53f50b9Schristos * Mount vfs.
153a53f50b9Schristos * Returns: 0 if OK, non-zero (errno) if failed.
154a53f50b9Schristos */
155a53f50b9Schristos static int
amfs_nfsl_mount(am_node * mp,mntfs * mf)156a53f50b9Schristos amfs_nfsl_mount(am_node *mp, mntfs *mf)
157a53f50b9Schristos {
158a53f50b9Schristos int ret = 0;
159a53f50b9Schristos if (mf->mf_flags & MFF_NFSLINK) {
160a53f50b9Schristos if (amfs_link_ops.mount_fs)
161a53f50b9Schristos ret = amfs_link_ops.mount_fs(mp, mf);
162a53f50b9Schristos } else {
163a53f50b9Schristos if (nfs_ops.mount_fs)
164a53f50b9Schristos ret = nfs_ops.mount_fs(mp, mf);
165a53f50b9Schristos }
166a53f50b9Schristos return ret;
167a53f50b9Schristos }
168a53f50b9Schristos
169a53f50b9Schristos
170a53f50b9Schristos /*
171a53f50b9Schristos * Unmount VFS.
172a53f50b9Schristos * Returns: 0 if OK, non-zero (errno) if failed.
173a53f50b9Schristos */
174a53f50b9Schristos static int
amfs_nfsl_umount(am_node * mp,mntfs * mf)175a53f50b9Schristos amfs_nfsl_umount(am_node *mp, mntfs *mf)
176a53f50b9Schristos {
177a53f50b9Schristos int ret = 0;
178a53f50b9Schristos if (mf->mf_flags & MFF_NFSLINK) {
179a53f50b9Schristos if (amfs_link_ops.umount_fs)
180a53f50b9Schristos ret = amfs_link_ops.umount_fs(mp, mf);
181a53f50b9Schristos } else {
182a53f50b9Schristos if (nfs_ops.umount_fs)
183a53f50b9Schristos ret = nfs_ops.umount_fs(mp, mf);
184a53f50b9Schristos }
185a53f50b9Schristos return ret;
186a53f50b9Schristos }
187a53f50b9Schristos
188a53f50b9Schristos
189a53f50b9Schristos /*
190a53f50b9Schristos * Async unmount callback function.
191a53f50b9Schristos * After the base umount() succeeds, we may want to take extra actions,
192a53f50b9Schristos * such as informing remote mount daemons that we've unmounted them.
193a53f50b9Schristos * See amfs_auto_umounted(), host_umounted(), nfs_umounted().
194a53f50b9Schristos */
195a53f50b9Schristos static void
amfs_nfsl_umounted(mntfs * mf)196a53f50b9Schristos amfs_nfsl_umounted(mntfs *mf)
197a53f50b9Schristos {
198a53f50b9Schristos if (mf->mf_flags & MFF_NFSLINK) {
199a53f50b9Schristos if (amfs_link_ops.umounted)
200a53f50b9Schristos amfs_link_ops.umounted(mf);
201a53f50b9Schristos } else {
202a53f50b9Schristos if (nfs_ops.umounted)
203a53f50b9Schristos nfs_ops.umounted(mf);
204a53f50b9Schristos }
205a53f50b9Schristos }
206a53f50b9Schristos
207a53f50b9Schristos
208a53f50b9Schristos /*
209a53f50b9Schristos * Find a file server.
210a53f50b9Schristos * Returns: fserver of found server, or NULL if not found.
211a53f50b9Schristos */
212a53f50b9Schristos static fserver *
amfs_nfsl_ffserver(mntfs * mf)213a53f50b9Schristos amfs_nfsl_ffserver(mntfs *mf)
214a53f50b9Schristos {
215*31bdb48aSchristos char *cp, *ho;
216a53f50b9Schristos struct stat stb;
217a53f50b9Schristos
218*31bdb48aSchristos if (mf->mf_fo == NULL) {
219*31bdb48aSchristos plog(XLOG_ERROR, "%s: NULL mf_fo", __func__);
220*31bdb48aSchristos return NULL;
221*31bdb48aSchristos }
222*31bdb48aSchristos ho = mf->mf_fo->opt_rhost;
223*31bdb48aSchristos
2244bcd344eSchristos if (mf->mf_fo->opt_sublink && mf->mf_fo->opt_sublink[0])
225a53f50b9Schristos cp = mf->mf_fo->opt_sublink;
226a53f50b9Schristos else
227a53f50b9Schristos cp = mf->mf_fo->opt_fs;
228a53f50b9Schristos
229a53f50b9Schristos /*
230a53f50b9Schristos * If this host is not the same as $rhost, or if link does not exist,
231a53f50b9Schristos * call amfs_link_ops.ffserver().
232a53f50b9Schristos * If link value exists (or same host), then call ops_nfs.ffserver().
233a53f50b9Schristos */
234*31bdb48aSchristos if ((!STRCEQ(ho, am_get_hostname()) &&
235*31bdb48aSchristos !STRCEQ(ho, hostd)) || lstat(cp, &stb) < 0) {
236a53f50b9Schristos return nfs_ops.ffserver(mf);
237a53f50b9Schristos } else {
238a53f50b9Schristos mf->mf_flags |= MFF_NFSLINK;
239a53f50b9Schristos /* remove the FS_MKMNT flag, we don't want amd touching the mountpoint */
240a53f50b9Schristos mf->mf_fsflags &= ~FS_MKMNT;
241a53f50b9Schristos return amfs_link_ops.ffserver(mf);
242a53f50b9Schristos }
243a53f50b9Schristos }
244