2 * stropts.c -- NFS mount using C string to pass options to kernel
4 * Copyright (C) 2007 Oracle. All rights reserved.
5 * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 021110-1307, USA.
32 #include <sys/socket.h>
33 #include <sys/mount.h>
38 #include "nfs_mount.h"
39 #include "mount_constants.h"
43 #include "parse_opt.h"
45 #ifdef HAVE_RPCSVC_NFS_PROT_H
46 #include <rpcsvc/nfs_prot.h>
48 #include <linux/nfs.h>
49 #define nfsstat nfs_stat
56 #ifndef NFS_MAXHOSTNAME
57 #define NFS_MAXHOSTNAME (255)
60 #ifndef NFS_MAXPATHNAME
61 #define NFS_MAXPATHNAME (1024)
64 extern int nfs_mount_data_version;
65 extern char *progname;
68 static int parse_devname(const char *spec, char **hostname)
71 char *dev, *pathname, *s;
75 if (!(pathname = strchr(dev, ':'))) {
76 nfs_error(_("%s: remote share not in 'host:dir' format"),
84 * We don't need a copy of the pathname, but let's
85 * sanity check it anyway.
87 if (strlen(pathname) > NFS_MAXPATHNAME) {
88 nfs_error(_("%s: export pathname is too long"),
94 * Ignore all but first hostname in replicated mounts
95 * until they can be fully supported. (mack@sgi.com)
97 if ((s = strchr(dev, ','))) {
99 nfs_error(_("%s: warning: multiple hostnames not supported"),
101 nfs_error(_("%s: ignoring hostnames that follow the first one"),
104 *hostname = xstrdup(dev);
105 if (strlen(*hostname) > NFS_MAXHOSTNAME) {
106 nfs_error(_("%s: server hostname is too long"),
119 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
122 addr->sin_family = AF_INET;
124 if (inet_aton(hostname, &addr->sin_addr))
126 if ((hp = gethostbyname(hostname)) == NULL) {
127 nfs_error(_("%s: can't get address for %s\n"),
131 if (hp->h_length > sizeof(struct in_addr)) {
132 nfs_error(_("%s: got bad hp->h_length"), progname);
133 hp->h_length = sizeof(struct in_addr);
135 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
140 * Append the 'addr=' option to the options string to pass a resolved
141 * server address to the kernel. After a successful mount, this address
142 * is also added to /etc/mtab for use when unmounting.
144 * If 'addr=' is already present, we strip it out. This prevents users
145 * from setting a bogus 'addr=' option themselves, and also allows bg
146 * retries to recompute the server's address, in case it has changed.
148 * Returns 1 if 'addr=' option appended successfully;
151 static int append_addr_option(struct sockaddr_in *saddr,
152 struct mount_options *options)
156 po_remove_all(options, "addr");
158 snprintf(new_option, sizeof(new_option) - 1,
159 "addr=%s", inet_ntoa(saddr->sin_addr));
161 if (po_append(options, new_option) == PO_SUCCEEDED)
167 * Called to discover our address and append an appropriate 'clientaddr='
168 * option to the options string.
170 * Returns 1 if 'clientaddr=' option created successfully or if
171 * 'clientaddr=' option is already present; otherwise zero.
173 static int append_clientaddr_option(struct sockaddr_in *saddr,
174 struct mount_options *options)
176 struct sockaddr_in my_addr;
179 if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
182 if (!get_client_address(saddr, &my_addr))
185 snprintf(new_option, sizeof(new_option) - 1,
186 "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
188 if (po_append(options, new_option) == PO_SUCCEEDED)
194 * Resolve the 'mounthost=' hostname and append a new option using
195 * the resulting IPv4 address.
197 static int fix_mounthost_option(struct mount_options *options)
199 struct sockaddr_in maddr;
200 char *mounthost, new_option[32];
202 mounthost = po_get(options, "mounthost");
206 if (!fill_ipv4_sockaddr(mounthost, &maddr))
209 snprintf(new_option, sizeof(new_option) - 1,
210 "mountaddr=%s", inet_ntoa(maddr.sin_addr));
212 if (po_append(options, new_option) == PO_SUCCEEDED)
218 * Set up mandatory mount options.
220 * Returns 1 if successful; otherwise zero.
222 static int set_mandatory_options(const char *type,
223 struct sockaddr_in *saddr,
224 struct mount_options *options)
226 if (!append_addr_option(saddr, options))
229 if (strncmp(type, "nfs4", 4) == 0) {
230 if (!append_clientaddr_option(saddr, options))
233 if (!fix_mounthost_option(options))
241 * Attempt an NFSv2/3 mount via a mount(2) system call.
243 * Returns 1 if successful. Otherwise, returns zero.
244 * "errno" is set to reflect the individual error.
246 static int try_nfs23mount(const char *spec, const char *node,
247 int flags, struct mount_options *options,
248 int fake, char **extra_opts)
250 if (po_join(options, extra_opts) == PO_FAILED) {
256 printf(_("%s: text-based options: '%s'\n"),
257 progname, *extra_opts);
262 if (!mount(spec, node, "nfs",
263 flags & ~(MS_USER|MS_USERS), *extra_opts))
269 * Attempt an NFS v4 mount via a mount(2) system call.
271 * Returns 1 if successful. Otherwise, returns zero.
272 * "errno" is set to reflect the individual error.
274 static int try_nfs4mount(const char *spec, const char *node,
275 int flags, struct mount_options *options,
276 int fake, char **extra_opts)
278 if (po_join(options, extra_opts) == PO_FAILED) {
284 printf(_("%s: text-based options: '%s'\n"),
285 progname, *extra_opts);
290 if (!mount(spec, node, "nfs4",
291 flags & ~(MS_USER|MS_USERS), *extra_opts))
297 * Try the mount(2) system call.
299 * Returns 1 if successful. Otherwise, returns zero.
300 * "errno" is set to reflect the individual error.
302 static int try_mount(const char *spec, const char *node, const char *type,
303 int flags, struct mount_options *options, int fake,
306 if (strncmp(type, "nfs4", 4) == 0)
307 return try_nfs4mount(spec, node, flags,
308 options, fake, extra_opts);
310 return try_nfs23mount(spec, node, flags,
311 options, fake, extra_opts);
315 * nfsmount_string - Mount an NFS file system using C string options
316 * @spec: C string specifying remote share to mount ("hostname:path")
317 * @node: C string pathname of local mounted-on directory
318 * @type: C string that represents file system type ("nfs" or "nfs4")
319 * @flags: MS_ style mount flags
320 * @extra_opts: pointer to C string containing fs-specific mount options
321 * (input and output argument)
322 * @fake: flag indicating whether to carry out the whole operation
323 * @child: one if this is a mount daemon (bg)
325 int nfsmount_string(const char *spec, const char *node, const char *type,
326 int flags, char **extra_opts, int fake, int child)
328 struct mount_options *options = NULL;
329 struct sockaddr_in saddr;
331 int err, retval = EX_FAIL;
333 if (!parse_devname(spec, &hostname))
335 err = fill_ipv4_sockaddr(hostname, &saddr);
340 options = po_split(*extra_opts);
342 nfs_error(_("%s: internal option parsing error"), progname);
346 if (!set_mandatory_options(type, &saddr, options))
349 if (po_join(options, extra_opts) == PO_FAILED) {
350 nfs_error(_("%s: internal option parsing error"), progname);
355 printf(_("%s: text-based options: '%s'\n"),
356 progname, *extra_opts);
359 if (mount(spec, node, type,
360 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
361 mount_error(spec, node, errno);