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 retry_opt = 10000; /* 10,000 minutes ~= 1 week */
69 static int bg_opt = 0;
70 static int addr_opt = 0;
71 static int ca_opt = 0;
73 static int parse_devname(const char *spec, char **hostname)
76 char *dev, *pathname, *s;
80 if (!(pathname = strchr(dev, ':'))) {
81 nfs_error(_("%s: remote share not in 'host:dir' format"),
89 * We don't need a copy of the pathname, but let's
90 * sanity check it anyway.
92 if (strlen(pathname) > NFS_MAXPATHNAME) {
93 nfs_error(_("%s: export pathname is too long"),
99 * Ignore all but first hostname in replicated mounts
100 * until they can be fully supported. (mack@sgi.com)
102 if ((s = strchr(dev, ','))) {
104 nfs_error(_("%s: warning: multiple hostnames not supported"),
106 nfs_error(_("%s: ignoring hostnames that follow the first one"),
109 *hostname = xstrdup(dev);
110 if (strlen(*hostname) > NFS_MAXHOSTNAME) {
111 nfs_error(_("%s: server hostname is too long"),
124 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
127 addr->sin_family = AF_INET;
129 if (inet_aton(hostname, &addr->sin_addr))
131 if ((hp = gethostbyname(hostname)) == NULL) {
132 nfs_error(_("%s: can't get address for %s\n"),
136 if (hp->h_length > sizeof(struct in_addr)) {
137 nfs_error(_("%s: got bad hp->h_length"), progname);
138 hp->h_length = sizeof(struct in_addr);
140 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
145 * Walk through our mount options string, and indicate the presence
146 * of 'bg', 'retry=', 'addr=', and 'clientaddr='.
148 static void extract_interesting_options(char *opts)
153 opts = xstrdup(opts);
155 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ",")) {
156 if ((opteq = strchr(opt, '='))) {
157 val = atoi(opteq + 1);
159 if (strcmp(opt, "bg") == 0)
161 else if (strcmp(opt, "retry") == 0)
163 else if (strcmp(opt, "addr") == 0)
165 else if (strcmp(opt, "clientaddr") == 0)
168 if (strcmp(opt, "bg") == 0)
177 * Append the 'addr=' option to the options string. The server
178 * address is added to /etc/mtab for use when unmounting.
180 * Returns 1 if 'addr=' option created successfully;
183 static int append_addr_opt(struct sockaddr_in *saddr, char **extra_opts)
185 static char new_opts[1024];
188 s = inet_ntoa(saddr->sin_addr);
189 old_opts = *extra_opts;
192 if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
193 nfs_error(_("%s: too many mount options\n"),
197 snprintf(new_opts, sizeof(new_opts), "%s%saddr=%s",
198 old_opts, *old_opts ? "," : "", s);
199 *extra_opts = xstrdup(new_opts);
205 * Called if no 'clientaddr=' option was specified in the options string
206 * to discover our address and append an appropriate 'clientaddr=' option
207 * to the options string.
209 * Returns 1 if 'clientaddr=' option created successfully;
212 static int append_clientaddr_opt(struct sockaddr_in *saddr, char **extra_opts)
214 static char new_opts[2048], cbuf[256];
215 struct sockaddr_in my_addr;
217 if (!get_client_address(saddr, &my_addr))
220 if (strlen(*extra_opts) + 30 >= sizeof(new_opts)) {
221 nfs_error(_("%s: too many mount options"),
226 strcat(new_opts, *extra_opts);
228 snprintf(cbuf, sizeof(cbuf) - 1, "%sclientaddr=%s",
229 *extra_opts ? "," : "", inet_ntoa(my_addr.sin_addr));
231 strcat(new_opts, cbuf);
233 *extra_opts = xstrdup(new_opts);
239 * Append the 'addr=' option to the options string to pass a resolved
240 * server address to the kernel. After a successful mount, this address
241 * is also added to /etc/mtab for use when unmounting.
243 * If 'addr=' is already present, we strip it out. This prevents users
244 * from setting a bogus 'addr=' option themselves, and also allows bg
245 * retries to recompute the server's address, in case it has changed.
247 * Returns 1 if 'addr=' option appended successfully;
250 static int append_addr_option(struct sockaddr_in *saddr,
251 struct mount_options *options)
255 po_remove_all(options, "addr");
257 snprintf(new_option, sizeof(new_option) - 1,
258 "addr=%s", inet_ntoa(saddr->sin_addr));
260 if (po_append(options, new_option) == PO_SUCCEEDED)
266 * Called to discover our address and append an appropriate 'clientaddr='
267 * option to the options string.
269 * Returns 1 if 'clientaddr=' option created successfully or if
270 * 'clientaddr=' option is already present; otherwise zero.
272 static int append_clientaddr_option(struct sockaddr_in *saddr,
273 struct mount_options *options)
275 struct sockaddr_in my_addr;
278 if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
281 if (!get_client_address(saddr, &my_addr))
284 snprintf(new_option, sizeof(new_option) - 1,
285 "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
287 if (po_append(options, new_option) == PO_SUCCEEDED)
293 * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
295 * @spec: C string hostname:path specifying remoteshare to mount
296 * @node: C string pathname of local mounted on directory
297 * @flags: MS_ style flags
298 * @extra_opts: pointer to C string containing fs-specific mount options
299 * (possibly also a return argument)
300 * @fake: flag indicating whether to carry out the whole operation
301 * @child: one if this is a backgrounded mount
303 * XXX: need to handle bg, fg, and retry options.
305 int nfsmount_s(const char *spec, const char *node, int flags,
306 char **extra_opts, int fake, int child)
308 struct mount_options *options = NULL;
309 struct sockaddr_in saddr;
311 int err, retval = EX_FAIL;
313 if (!parse_devname(spec, &hostname))
315 err = fill_ipv4_sockaddr(hostname, &saddr);
320 extract_interesting_options(*extra_opts);
322 options = po_split(*extra_opts);
324 nfs_error(_("%s: internal option parsing error"), progname);
328 if (!child && addr_opt) {
329 nfs_error(_("%s: Illegal option: 'addr='"), progname);
333 if (!append_addr_opt(&saddr, extra_opts))
336 if (po_join(options, extra_opts) == PO_FAILED) {
337 nfs_error(_("%s: internal option parsing error"), progname);
342 printf(_("%s: text-based options: '%s'\n"),
343 progname, *extra_opts);
346 if (mount(spec, node, "nfs",
347 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
348 mount_error(spec, node, errno);
361 * nfs4mount_s - Mount an NFSv4 file system using C string options
363 * @spec: C string hostname:path specifying remoteshare to mount
364 * @node: C string pathname of local mounted on directory
365 * @flags: MS_ style flags
366 * @extra_opts: pointer to C string containing fs-specific mount options
367 * (possibly also a return argument)
368 * @fake: flag indicating whether to carry out the whole operation
369 * @child: one if this is a backgrounded mount
371 * XXX: need to handle bg, fg, and retry options.
374 int nfs4mount_s(const char *spec, const char *node, int flags,
375 char **extra_opts, int fake, int child)
377 struct mount_options *options = NULL;
378 struct sockaddr_in saddr;
380 int err, retval = EX_FAIL;
382 if (!parse_devname(spec, &hostname))
384 err = fill_ipv4_sockaddr(hostname, &saddr);
389 extract_interesting_options(*extra_opts);
391 options = po_split(*extra_opts);
393 nfs_error(_("%s: internal option parsing error"), progname);
398 nfs_error(_("%s: Illegal option: 'addr='"), progname);
402 if (!append_addr_opt(&saddr, extra_opts))
405 if (!ca_opt && !append_clientaddr_opt(&saddr, extra_opts))
408 if (po_join(options, extra_opts) == PO_FAILED) {
409 nfs_error(_("%s: internal option parsing error"), progname);
414 printf(_("%s: text-based options: '%s'\n"),
415 progname, *extra_opts);
418 if (mount(spec, node, "nfs4",
419 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
420 mount_error(spec, node, errno);