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.
36 #include <sys/socket.h>
37 #include <sys/mount.h>
42 #include "nfs_mount.h"
43 #include "mount_constants.h"
47 #include "parse_opt.h"
49 #ifdef HAVE_RPCSVC_NFS_PROT_H
50 #include <rpcsvc/nfs_prot.h>
52 #include <linux/nfs.h>
53 #define nfsstat nfs_stat
60 #ifndef NFS_MAXHOSTNAME
61 #define NFS_MAXHOSTNAME (255)
64 #ifndef NFS_MAXPATHNAME
65 #define NFS_MAXPATHNAME (1024)
68 extern int nfs_mount_data_version;
69 extern char *progname;
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 * Append the 'addr=' option to the options string to pass a resolved
145 * server address to the kernel. After a successful mount, this address
146 * is also added to /etc/mtab for use when unmounting.
148 * If 'addr=' is already present, we strip it out. This prevents users
149 * from setting a bogus 'addr=' option themselves, and also allows bg
150 * retries to recompute the server's address, in case it has changed.
152 * Returns 1 if 'addr=' option appended successfully;
155 static int append_addr_option(struct sockaddr_in *saddr,
156 struct mount_options *options)
160 po_remove_all(options, "addr");
162 snprintf(new_option, sizeof(new_option) - 1,
163 "addr=%s", inet_ntoa(saddr->sin_addr));
165 if (po_append(options, new_option) == PO_SUCCEEDED)
171 * Called to discover our address and append an appropriate 'clientaddr='
172 * option to the options string.
174 * Returns 1 if 'clientaddr=' option created successfully or if
175 * 'clientaddr=' option is already present; otherwise zero.
177 static int append_clientaddr_option(struct sockaddr_in *saddr,
178 struct mount_options *options)
180 struct sockaddr_in my_addr;
183 if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
186 if (!get_client_address(saddr, &my_addr))
189 snprintf(new_option, sizeof(new_option) - 1,
190 "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
192 if (po_append(options, new_option) == PO_SUCCEEDED)
198 * Resolve the 'mounthost=' hostname and append a new option using
199 * the resulting IPv4 address.
201 static int fix_mounthost_option(struct mount_options *options)
203 struct sockaddr_in maddr;
204 char *mounthost, new_option[32];
206 mounthost = po_get(options, "mounthost");
210 if (!fill_ipv4_sockaddr(mounthost, &maddr))
213 snprintf(new_option, sizeof(new_option) - 1,
214 "mountaddr=%s", inet_ntoa(maddr.sin_addr));
216 if (po_append(options, new_option) == PO_SUCCEEDED)
222 * Set up mandatory mount options.
224 * Returns 1 if successful; otherwise zero.
226 static int set_mandatory_options(const char *type,
227 struct sockaddr_in *saddr,
228 struct mount_options *options)
230 if (!append_addr_option(saddr, options))
233 if (strncmp(type, "nfs4", 4) == 0) {
234 if (!append_clientaddr_option(saddr, options))
237 if (!fix_mounthost_option(options))
245 * Distinguish between permanent and temporary errors.
247 * Returns 0 if the passed-in error is temporary, thus the
248 * mount system call should be retried; returns one if the
249 * passed-in error is permanent, thus the mount system call
250 * should not be retried.
252 static int is_permanent_error(int error)
259 return 0; /* temporary */
261 return 1; /* permanent */
266 * Reconstruct the mount option string based on a portmapper probe
267 * of the server. Returns one if the server's portmapper returned
268 * something we can use, otherwise zero.
270 * To handle version and transport protocol fallback properly, we
271 * need to parse some of the mount options in order to set up a
272 * portmap probe. Mount options that rewrite_mount_options()
273 * doesn't recognize are left alone.
275 * Returns a new group of mount options if successful; otherwise
276 * NULL is returned if some failure occurred.
278 static struct mount_options *rewrite_mount_options(char *str)
280 struct mount_options *options;
281 char *option, new_option[64];
282 clnt_addr_t mnt_server = { };
283 clnt_addr_t nfs_server = { };
286 options = po_split(str);
290 option = po_get(options, "addr");
292 nfs_server.saddr.sin_family = AF_INET;
293 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
298 option = po_get(options, "mountaddr");
300 mnt_server.saddr.sin_family = AF_INET;
301 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
304 memcpy(&mnt_server.saddr, &nfs_server.saddr,
305 sizeof(mnt_server.saddr));
307 option = po_get(options, "mountport");
309 mnt_server.pmap.pm_port = atoi(option);
310 mnt_server.pmap.pm_prog = MOUNTPROG;
311 option = po_get(options, "mountvers");
313 mnt_server.pmap.pm_vers = atoi(option);
315 option = po_get(options, "port");
317 nfs_server.pmap.pm_port = atoi(option);
318 po_remove_all(options, "port");
320 nfs_server.pmap.pm_prog = NFS_PROGRAM;
322 option = po_get(options, "nfsvers");
324 nfs_server.pmap.pm_vers = atoi(option);
325 po_remove_all(options, "nfsvers");
327 option = po_get(options, "vers");
329 nfs_server.pmap.pm_vers = atoi(option);
330 po_remove_all(options, "vers");
332 option = po_get(options, "proto");
334 if (strcmp(option, "tcp") == 0) {
335 nfs_server.pmap.pm_prot = IPPROTO_TCP;
336 po_remove_all(options, "proto");
338 if (strcmp(option, "udp") == 0) {
339 nfs_server.pmap.pm_prot = IPPROTO_UDP;
340 po_remove_all(options, "proto");
343 p = po_rightmost(options, "tcp", "udp");
345 case PO_KEY2_RIGHTMOST:
346 nfs_server.pmap.pm_prot = IPPROTO_UDP;
348 case PO_KEY1_RIGHTMOST:
349 nfs_server.pmap.pm_prot = IPPROTO_TCP;
352 po_remove_all(options, "tcp");
353 po_remove_all(options, "udp");
355 if (!probe_bothports(&mnt_server, &nfs_server)) {
356 rpc_mount_errors("rpcbind", 0, 0);
360 snprintf(new_option, sizeof(new_option) - 1,
361 "nfsvers=%lu", nfs_server.pmap.pm_vers);
362 if (po_append(options, new_option) == PO_FAILED)
365 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
366 snprintf(new_option, sizeof(new_option) - 1,
369 snprintf(new_option, sizeof(new_option) - 1,
371 if (po_append(options, new_option) == PO_FAILED)
374 if (nfs_server.pmap.pm_port != NFS_PORT) {
375 snprintf(new_option, sizeof(new_option) - 1,
376 "port=%lu", nfs_server.pmap.pm_port);
377 if (po_append(options, new_option) == PO_FAILED)
390 * Retry an NFS mount that failed because the requested service isn't
391 * available on the server.
393 * Returns 1 if successful. Otherwise, returns zero.
394 * "errno" is set to reflect the individual error.
396 * Side effect: If the retry is successful, both 'options' and
397 * 'extra_opts' are updated to reflect the mount options that worked.
398 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
400 static int retry_nfsmount(const char *spec, const char *node,
401 int flags, struct mount_options *options,
402 int fake, char **extra_opts)
404 struct mount_options *retry_options;
405 char *retry_str = NULL;
407 retry_options = rewrite_mount_options(*extra_opts);
408 if (!retry_options) {
413 if (po_join(retry_options, &retry_str) == PO_FAILED) {
414 po_destroy(retry_options);
420 printf(_("%s: text-based options (retry): '%s'\n"),
421 progname, retry_str);
423 if (!mount(spec, node, "nfs",
424 flags & ~(MS_USER|MS_USERS), retry_str)) {
426 *extra_opts = retry_str;
427 po_replace(options, retry_options);
431 po_destroy(retry_options);
437 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
438 * claims the requested service isn't supported on the server, probe
439 * the server to see what's supported, rewrite the mount options,
440 * and retry the request.
442 * Returns 1 if successful. Otherwise, returns zero.
443 * "errno" is set to reflect the individual error.
445 * Side effect: If the retry is successful, both 'options' and
446 * 'extra_opts' are updated to reflect the mount options that worked.
447 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
449 static int try_nfs23mount(const char *spec, const char *node,
450 int flags, struct mount_options *options,
451 int fake, char **extra_opts)
453 if (po_join(options, extra_opts) == PO_FAILED) {
459 printf(_("%s: text-based options: '%s'\n"),
460 progname, *extra_opts);
465 if (!mount(spec, node, "nfs",
466 flags & ~(MS_USER|MS_USERS), *extra_opts))
470 * The kernel returns EOPNOTSUPP if the RPC bind failed,
471 * and EPROTONOSUPPORT if the version isn't supported.
473 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
476 return retry_nfsmount(spec, node, flags, options, fake, extra_opts);
480 * Attempt an NFS v4 mount via a mount(2) system call.
482 * Returns 1 if successful. Otherwise, returns zero.
483 * "errno" is set to reflect the individual error.
485 static int try_nfs4mount(const char *spec, const char *node,
486 int flags, struct mount_options *options,
487 int fake, char **extra_opts)
489 if (po_join(options, extra_opts) == PO_FAILED) {
495 printf(_("%s: text-based options: '%s'\n"),
496 progname, *extra_opts);
501 if (!mount(spec, node, "nfs4",
502 flags & ~(MS_USER|MS_USERS), *extra_opts))
508 * Try the mount(2) system call.
510 * Returns 1 if successful. Otherwise, returns zero.
511 * "errno" is set to reflect the individual error.
513 static int try_mount(const char *spec, const char *node, const char *type,
514 int flags, struct mount_options *options, int fake,
517 if (strncmp(type, "nfs4", 4) == 0)
518 return try_nfs4mount(spec, node, flags,
519 options, fake, extra_opts);
521 return try_nfs23mount(spec, node, flags,
522 options, fake, extra_opts);
526 * Handle "foreground" NFS mounts.
528 * Retry the mount request for as long as the 'retry=' option says.
530 * Returns a valid mount command exit code.
532 static int nfsmount_fg(const char *spec, const char *node,
533 const char *type, int flags,
534 struct mount_options *options, int fake,
537 unsigned int secs = 1;
538 time_t timeout = time(NULL);
541 timeout += 60 * 2; /* default: 2 minutes */
542 retry = po_get(options, "retry");
544 timeout += 60 * atoi(retry);
547 printf(_("%s: timeout set for %s"),
548 progname, ctime(&timeout));
551 if (try_mount(spec, node, type, flags,
552 options, fake, extra_opts))
555 if (is_permanent_error(errno))
558 if (time(NULL) > timeout) {
563 if (errno != ETIMEDOUT) {
572 mount_error(spec, node, errno);
577 * Handle "background" NFS mount [first try]
579 * Returns a valid mount command exit code.
581 * EX_BG should cause the caller to fork and invoke nfsmount_child.
583 static int nfsmount_parent(const char *spec, const char *node,
584 const char *type, char *hostname, int flags,
585 struct mount_options *options,
586 int fake, char **extra_opts)
588 if (try_mount(spec, node, type, flags, options,
592 if (is_permanent_error(errno)) {
593 mount_error(spec, node, errno);
597 sys_mount_errors(hostname, errno, 1, 1);
602 * Handle "background" NFS mount [retry daemon]
604 * Returns a valid mount command exit code: EX_SUCCESS if successful,
605 * EX_FAIL if a failure occurred. There's nothing to catch the
606 * error return, though, so we use sys_mount_errors to log the
609 static int nfsmount_child(const char *spec, const char *node,
610 const char *type, char *hostname, int flags,
611 struct mount_options *options,
612 int fake, char **extra_opts)
614 unsigned int secs = 1;
615 time_t timeout = time(NULL);
618 timeout += 60 * 10000; /* default: 10,000 minutes */
619 retry = po_get(options, "retry");
621 timeout += 60 * atoi(retry);
630 if (try_mount(spec, node, type, flags, options,
634 if (is_permanent_error(errno))
637 if (time(NULL) > timeout)
640 sys_mount_errors(hostname, errno, 1, 1);
643 sys_mount_errors(hostname, errno, 1, 0);
648 * Handle "background" NFS mount
650 * Returns a valid mount command exit code.
652 static int nfsmount_bg(const char *spec, const char *node,
653 const char *type, char *hostname, int flags,
654 struct mount_options *options,
655 int fake, int child, char **extra_opts)
658 return nfsmount_parent(spec, node, type, hostname, flags,
659 options, fake, extra_opts);
661 return nfsmount_child(spec, node, type, hostname, flags,
662 options, fake, extra_opts);
666 * nfsmount_string - Mount an NFS file system using C string options
667 * @spec: C string specifying remote share to mount ("hostname:path")
668 * @node: C string pathname of local mounted-on directory
669 * @type: C string that represents file system type ("nfs" or "nfs4")
670 * @flags: MS_ style mount flags
671 * @extra_opts: pointer to C string containing fs-specific mount options
672 * (input and output argument)
673 * @fake: flag indicating whether to carry out the whole operation
674 * @child: one if this is a mount daemon (bg)
676 int nfsmount_string(const char *spec, const char *node, const char *type,
677 int flags, char **extra_opts, int fake, int child)
679 struct mount_options *options = NULL;
680 struct sockaddr_in saddr;
682 int retval = EX_FAIL;
684 if (!parse_devname(spec, &hostname))
686 if (!fill_ipv4_sockaddr(hostname, &saddr))
689 options = po_split(*extra_opts);
691 nfs_error(_("%s: internal option parsing error"), progname);
695 if (!set_mandatory_options(type, &saddr, options))
698 if (po_rightmost(options, "bg", "fg") == PO_KEY1_RIGHTMOST)
699 retval = nfsmount_bg(spec, node, type, hostname, flags,
700 options, fake, child, extra_opts);
702 retval = nfsmount_fg(spec, node, type, flags, options,