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 * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
196 * @spec: C string hostname:path specifying remoteshare to mount
197 * @node: C string pathname of local mounted on directory
198 * @flags: MS_ style flags
199 * @extra_opts: pointer to C string containing fs-specific mount options
200 * (possibly also a return argument)
201 * @fake: flag indicating whether to carry out the whole operation
202 * @child: one if this is a backgrounded mount
204 * XXX: need to handle bg, fg, and retry options.
206 int nfsmount_s(const char *spec, const char *node, int flags,
207 char **extra_opts, int fake, int child)
209 struct mount_options *options = NULL;
210 struct sockaddr_in saddr;
212 int err, retval = EX_FAIL;
214 if (!parse_devname(spec, &hostname))
216 err = fill_ipv4_sockaddr(hostname, &saddr);
221 options = po_split(*extra_opts);
223 nfs_error(_("%s: internal option parsing error"), progname);
227 if (!append_addr_option(&saddr, options))
230 if (po_join(options, extra_opts) == PO_FAILED) {
231 nfs_error(_("%s: internal option parsing error"), progname);
236 printf(_("%s: text-based options: '%s'\n"),
237 progname, *extra_opts);
240 if (mount(spec, node, "nfs",
241 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
242 mount_error(spec, node, errno);
255 * nfs4mount_s - Mount an NFSv4 file system using C string options
257 * @spec: C string hostname:path specifying remoteshare to mount
258 * @node: C string pathname of local mounted on directory
259 * @flags: MS_ style flags
260 * @extra_opts: pointer to C string containing fs-specific mount options
261 * (possibly also a return argument)
262 * @fake: flag indicating whether to carry out the whole operation
263 * @child: one if this is a backgrounded mount
265 * XXX: need to handle bg, fg, and retry options.
268 int nfs4mount_s(const char *spec, const char *node, int flags,
269 char **extra_opts, int fake, int child)
271 struct mount_options *options = NULL;
272 struct sockaddr_in saddr;
274 int err, retval = EX_FAIL;
276 if (!parse_devname(spec, &hostname))
278 err = fill_ipv4_sockaddr(hostname, &saddr);
283 options = po_split(*extra_opts);
285 nfs_error(_("%s: internal option parsing error"), progname);
289 if (!append_addr_option(&saddr, options))
292 if (!append_clientaddr_option(&saddr, options))
295 if (po_join(options, extra_opts) == PO_FAILED) {
296 nfs_error(_("%s: internal option parsing error"), progname);
301 printf(_("%s: text-based options: '%s'\n"),
302 progname, *extra_opts);
305 if (mount(spec, node, "nfs4",
306 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
307 mount_error(spec, node, errno);