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 * Called to resolve the 'mounthost=' hostname and append a new
195 * option using an IPv4 address.
197 static int fix_up_mounthost_opt(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 * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
220 * @spec: C string hostname:path specifying remoteshare to mount
221 * @node: C string pathname of local mounted on directory
222 * @flags: MS_ style flags
223 * @extra_opts: pointer to C string containing fs-specific mount options
224 * (possibly also a return argument)
225 * @fake: flag indicating whether to carry out the whole operation
226 * @child: one if this is a backgrounded mount
228 * XXX: need to handle bg, fg, and retry options.
230 int nfsmount_s(const char *spec, const char *node, int flags,
231 char **extra_opts, int fake, int child)
233 struct mount_options *options = NULL;
234 struct sockaddr_in saddr;
236 int err, retval = EX_FAIL;
238 if (!parse_devname(spec, &hostname))
240 err = fill_ipv4_sockaddr(hostname, &saddr);
245 options = po_split(*extra_opts);
247 nfs_error(_("%s: internal option parsing error"), progname);
251 if (!append_addr_option(&saddr, options))
254 if (!fix_up_mounthost_opt(options))
257 if (po_join(options, extra_opts) == PO_FAILED) {
258 nfs_error(_("%s: internal option parsing error"), progname);
263 printf(_("%s: text-based options: '%s'\n"),
264 progname, *extra_opts);
267 if (mount(spec, node, "nfs",
268 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
269 mount_error(spec, node, errno);
282 * nfs4mount_s - Mount an NFSv4 file system using C string options
284 * @spec: C string hostname:path specifying remoteshare to mount
285 * @node: C string pathname of local mounted on directory
286 * @flags: MS_ style flags
287 * @extra_opts: pointer to C string containing fs-specific mount options
288 * (possibly also a return argument)
289 * @fake: flag indicating whether to carry out the whole operation
290 * @child: one if this is a backgrounded mount
292 * XXX: need to handle bg, fg, and retry options.
295 int nfs4mount_s(const char *spec, const char *node, int flags,
296 char **extra_opts, int fake, int child)
298 struct mount_options *options = NULL;
299 struct sockaddr_in saddr;
301 int err, retval = EX_FAIL;
303 if (!parse_devname(spec, &hostname))
305 err = fill_ipv4_sockaddr(hostname, &saddr);
310 options = po_split(*extra_opts);
312 nfs_error(_("%s: internal option parsing error"), progname);
316 if (!append_addr_option(&saddr, options))
319 if (!append_clientaddr_option(&saddr, options))
322 if (po_join(options, extra_opts) == PO_FAILED) {
323 nfs_error(_("%s: internal option parsing error"), progname);
328 printf(_("%s: text-based options: '%s'\n"),
329 progname, *extra_opts);
332 if (mount(spec, node, "nfs4",
333 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
334 mount_error(spec, node, errno);