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"
44 #ifdef HAVE_RPCSVC_NFS_PROT_H
45 #include <rpcsvc/nfs_prot.h>
47 #include <linux/nfs.h>
48 #define nfsstat nfs_stat
55 #ifndef NFS_MAXHOSTNAME
56 #define NFS_MAXHOSTNAME (255)
59 #ifndef NFS_MAXPATHNAME
60 #define NFS_MAXPATHNAME (1024)
63 extern int nfs_mount_data_version;
64 extern char *progname;
67 static int retry_opt = 10000; /* 10,000 minutes ~= 1 week */
68 static int bg_opt = 0;
69 static int addr_opt = 0;
70 static int ca_opt = 0;
72 static int parse_devname(const char *spec, char **hostname)
75 char *dev, *pathname, *s;
79 if (!(pathname = strchr(dev, ':'))) {
80 nfs_error(_("%s: remote share not in 'host:dir' format"),
88 * We don't need a copy of the pathname, but let's
89 * sanity check it anyway.
91 if (strlen(pathname) > NFS_MAXPATHNAME) {
92 nfs_error(_("%s: export pathname is too long"),
98 * Ignore all but first hostname in replicated mounts
99 * until they can be fully supported. (mack@sgi.com)
101 if ((s = strchr(dev, ','))) {
103 nfs_error(_("%s: warning: multiple hostnames not supported"),
105 nfs_error(_("%s: ignoring hostnames that follow the first one"),
108 *hostname = xstrdup(dev);
109 if (strlen(*hostname) > NFS_MAXHOSTNAME) {
110 nfs_error(_("%s: server hostname is too long"),
123 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
126 addr->sin_family = AF_INET;
128 if (inet_aton(hostname, &addr->sin_addr))
130 if ((hp = gethostbyname(hostname)) == NULL) {
131 nfs_error(_("%s: can't get address for %s\n"),
135 if (hp->h_length > sizeof(struct in_addr)) {
136 nfs_error(_("%s: got bad hp->h_length"), progname);
137 hp->h_length = sizeof(struct in_addr);
139 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
144 * Walk through our mount options string, and indicate the presence
145 * of 'bg', 'retry=', 'addr=', and 'clientaddr='.
147 static void extract_interesting_options(char *opts)
152 opts = xstrdup(opts);
154 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ",")) {
155 if ((opteq = strchr(opt, '='))) {
156 val = atoi(opteq + 1);
158 if (strcmp(opt, "bg") == 0)
160 else if (strcmp(opt, "retry") == 0)
162 else if (strcmp(opt, "addr") == 0)
164 else if (strcmp(opt, "clientaddr") == 0)
167 if (strcmp(opt, "bg") == 0)
176 * Append the 'addr=' option to the options string. The server
177 * address is added to /etc/mtab for use when unmounting.
179 * Returns 1 if 'addr=' option created successfully;
182 static int append_addr_opt(struct sockaddr_in *saddr, char **extra_opts)
184 static char new_opts[1024];
187 s = inet_ntoa(saddr->sin_addr);
188 old_opts = *extra_opts;
191 if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
192 nfs_error(_("%s: too many mount options\n"),
196 snprintf(new_opts, sizeof(new_opts), "%s%saddr=%s",
197 old_opts, *old_opts ? "," : "", s);
198 *extra_opts = xstrdup(new_opts);
204 * Called if no 'clientaddr=' option was specified in the options string
205 * to discover our address and append an appropriate 'clientaddr=' option
206 * to the options string.
208 * Returns 1 if 'clientaddr=' option created successfully;
211 static int append_clientaddr_opt(struct sockaddr_in *saddr, char **extra_opts)
213 static char new_opts[2048], cbuf[256];
214 struct sockaddr_in my_addr;
216 if (!get_client_address(saddr, &my_addr))
219 if (strlen(*extra_opts) + 30 >= sizeof(new_opts)) {
220 nfs_error(_("%s: too many mount options"),
225 strcat(new_opts, *extra_opts);
227 snprintf(cbuf, sizeof(cbuf) - 1, "%sclientaddr=%s",
228 *extra_opts ? "," : "", inet_ntoa(my_addr.sin_addr));
230 strcat(new_opts, cbuf);
232 *extra_opts = xstrdup(new_opts);
238 * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
240 * @spec: C string hostname:path specifying remoteshare to mount
241 * @node: C string pathname of local mounted on directory
242 * @flags: MS_ style flags
243 * @extra_opts: pointer to C string containing fs-specific mount options
244 * (possibly also a return argument)
245 * @fake: flag indicating whether to carry out the whole operation
246 * @bg: one if this is a backgrounded mount attempt
248 * XXX: need to handle bg, fg, and retry options.
250 int nfsmount_s(const char *spec, const char *node, int flags,
251 char **extra_opts, int fake, int bg)
253 struct sockaddr_in saddr;
257 if (!parse_devname(spec, &hostname))
259 err = fill_ipv4_sockaddr(hostname, &saddr);
264 extract_interesting_options(*extra_opts);
266 if (!bg && addr_opt) {
267 nfs_error(_("%s: Illegal option: 'addr='"), progname);
271 if (!append_addr_opt(&saddr, extra_opts))
275 printf(_("%s: text-based options: '%s'\n"),
276 progname, *extra_opts);
279 if (mount(spec, node, "nfs",
280 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
281 mount_error(spec, node, errno);
290 * nfs4mount_s - Mount an NFSv4 file system using C string options
292 * @spec: C string hostname:path specifying remoteshare to mount
293 * @node: C string pathname of local mounted on directory
294 * @flags: MS_ style flags
295 * @extra_opts: pointer to C string containing fs-specific mount options
296 * (possibly also a return argument)
297 * @fake: flag indicating whether to carry out the whole operation
298 * @child: one if this is a backgrounded mount
300 * XXX: need to handle bg, fg, and retry options.
303 int nfs4mount_s(const char *spec, const char *node, int flags,
304 char **extra_opts, int fake, int child)
306 struct sockaddr_in saddr;
310 if (!parse_devname(spec, &hostname))
312 err = fill_ipv4_sockaddr(hostname, &saddr);
317 extract_interesting_options(*extra_opts);
320 nfs_error(_("%s: Illegal option: 'addr='"), progname);
324 if (!append_addr_opt(&saddr, extra_opts))
327 if (!ca_opt && !append_clientaddr_opt(&saddr, extra_opts))
331 printf(_("%s: text-based options: '%s'\n"),
332 progname, *extra_opts);
335 if (mount(spec, node, "nfs4",
336 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
337 mount_error(spec, node, errno);