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"
50 #ifdef HAVE_RPCSVC_NFS_PROT_H
51 #include <rpcsvc/nfs_prot.h>
53 #include <linux/nfs.h>
54 #define nfsstat nfs_stat
61 #ifndef NFS_MAXHOSTNAME
62 #define NFS_MAXHOSTNAME (255)
65 #ifndef NFS_MAXPATHNAME
66 #define NFS_MAXPATHNAME (1024)
69 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
70 #define NFS_DEF_FG_TIMEOUT_MINUTES (2u)
73 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
74 #define NFS_DEF_BG_TIMEOUT_MINUTES (10000u)
77 extern int nfs_mount_data_version;
78 extern char *progname;
82 struct nfsmount_info {
83 const char *spec, /* server:/path */
84 *node, /* mounted-on dir */
85 *type; /* "nfs" or "nfs4" */
86 char *hostname; /* server's hostname */
88 struct mount_options *options; /* parsed mount options */
89 char **extra_opts; /* string for /etc/mtab */
91 int flags, /* MS_ flags */
92 fake, /* actually do the mount? */
93 child; /* forked bg child? */
96 static int nfs_parse_devname(struct nfsmount_info *mi)
99 char *dev, *pathname, *s;
101 dev = xstrdup(mi->spec);
103 if (!(pathname = strchr(dev, ':'))) {
104 nfs_error(_("%s: remote share not in 'host:dir' format"),
112 * We don't need a copy of the pathname, but let's
113 * sanity check it anyway.
115 if (strlen(pathname) > NFS_MAXPATHNAME) {
116 nfs_error(_("%s: export pathname is too long"),
122 * Ignore all but first hostname in replicated mounts
123 * until they can be fully supported. (mack@sgi.com)
125 if ((s = strchr(dev, ','))) {
127 nfs_error(_("%s: warning: multiple hostnames not supported"),
129 nfs_error(_("%s: ignoring hostnames that follow the first one"),
132 mi->hostname = xstrdup(dev);
133 if (strlen(mi->hostname) > NFS_MAXHOSTNAME) {
134 nfs_error(_("%s: server hostname is too long"),
148 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
151 addr->sin_family = AF_INET;
153 if (inet_aton(hostname, &addr->sin_addr))
155 if ((hp = gethostbyname(hostname)) == NULL) {
156 nfs_error(_("%s: can't get address for %s\n"),
160 if (hp->h_length > sizeof(struct in_addr)) {
161 nfs_error(_("%s: got bad hp->h_length"), progname);
162 hp->h_length = sizeof(struct in_addr);
164 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
169 * Obtain a retry timeout value based on the value of the "retry=" option.
171 * Returns a time_t timeout timestamp, in seconds.
173 static time_t nfs_parse_retry_option(struct mount_options *options,
174 unsigned int timeout_minutes)
176 char *retry_option, *endptr;
178 retry_option = po_get(options, "retry");
183 tmp = strtol(retry_option, &endptr, 10);
184 if (errno == 0 && endptr != retry_option && tmp >= 0)
185 timeout_minutes = tmp;
187 nfs_error(_("%s: invalid retry timeout was specified; "
188 "using default timeout"), progname);
191 return time(NULL) + (time_t)(timeout_minutes * 60);
195 * Append the 'addr=' option to the options string to pass a resolved
196 * server address to the kernel. After a successful mount, this address
197 * is also added to /etc/mtab for use when unmounting.
199 * If 'addr=' is already present, we strip it out. This prevents users
200 * from setting a bogus 'addr=' option themselves, and also allows bg
201 * retries to recompute the server's address, in case it has changed.
203 * Returns 1 if 'addr=' option appended successfully;
206 static int append_addr_option(struct sockaddr_in *saddr,
207 struct mount_options *options)
211 po_remove_all(options, "addr");
213 snprintf(new_option, sizeof(new_option) - 1,
214 "addr=%s", inet_ntoa(saddr->sin_addr));
216 if (po_append(options, new_option) == PO_SUCCEEDED)
222 * Called to discover our address and append an appropriate 'clientaddr='
223 * option to the options string.
225 * Returns 1 if 'clientaddr=' option created successfully or if
226 * 'clientaddr=' option is already present; otherwise zero.
228 static int append_clientaddr_option(struct sockaddr_in *saddr,
229 struct mount_options *options)
231 struct sockaddr_in my_addr;
234 if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
237 if (!get_client_address(saddr, &my_addr))
240 snprintf(new_option, sizeof(new_option) - 1,
241 "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
243 if (po_append(options, new_option) == PO_SUCCEEDED)
249 * Resolve the 'mounthost=' hostname and append a new option using
250 * the resulting IPv4 address.
252 static int fix_mounthost_option(struct mount_options *options)
254 struct sockaddr_in maddr;
255 char *mounthost, new_option[32];
257 mounthost = po_get(options, "mounthost");
261 if (!fill_ipv4_sockaddr(mounthost, &maddr))
264 snprintf(new_option, sizeof(new_option) - 1,
265 "mountaddr=%s", inet_ntoa(maddr.sin_addr));
267 if (po_append(options, new_option) == PO_SUCCEEDED)
273 * Returns zero if the "lock" option is in effect, but statd
274 * can't be started. Otherwise, returns 1.
276 static int verify_lock_option(struct mount_options *options)
278 if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
281 if (!start_statd()) {
282 nfs_error(_("%s: rpc.statd is not running but is "
283 "required for remote locking."), progname);
284 nfs_error(_("%s: Either use '-o nolock' to keep "
285 "locks local, or start statd."), progname);
292 static int nfs_append_sloppy_option(struct mount_options *options)
294 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
297 if (po_append(options, "sloppy") == PO_FAILED)
303 * Set up mandatory NFS mount options.
305 * Returns 1 if successful; otherwise zero.
307 static int nfs_validate_options(struct nfsmount_info *mi)
309 struct sockaddr_in saddr;
311 if (!fill_ipv4_sockaddr(mi->hostname, &saddr))
314 if (strncmp(mi->type, "nfs4", 4) == 0) {
315 if (!append_clientaddr_option(&saddr, mi->options))
318 if (!fix_mounthost_option(mi->options))
320 if (!mi->fake && !verify_lock_option(mi->options))
324 if (!nfs_append_sloppy_option(mi->options))
327 if (!append_addr_option(&saddr, mi->options))
334 * Distinguish between permanent and temporary errors.
336 * Returns 0 if the passed-in error is temporary, thus the
337 * mount system call should be retried; returns one if the
338 * passed-in error is permanent, thus the mount system call
339 * should not be retried.
341 static int is_permanent_error(int error)
347 return 0; /* temporary */
349 return 1; /* permanent */
354 * Reconstruct the mount option string based on a portmapper probe
355 * of the server. Returns one if the server's portmapper returned
356 * something we can use, otherwise zero.
358 * To handle version and transport protocol fallback properly, we
359 * need to parse some of the mount options in order to set up a
360 * portmap probe. Mount options that rewrite_mount_options()
361 * doesn't recognize are left alone.
363 * Returns a new group of mount options if successful; otherwise
364 * NULL is returned if some failure occurred.
366 static struct mount_options *rewrite_mount_options(char *str)
368 struct mount_options *options;
369 char *option, new_option[64];
370 clnt_addr_t mnt_server = { };
371 clnt_addr_t nfs_server = { };
374 options = po_split(str);
381 option = po_get(options, "addr");
383 nfs_server.saddr.sin_family = AF_INET;
384 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
389 option = po_get(options, "mountaddr");
391 mnt_server.saddr.sin_family = AF_INET;
392 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
395 memcpy(&mnt_server.saddr, &nfs_server.saddr,
396 sizeof(mnt_server.saddr));
398 option = po_get(options, "mountport");
400 mnt_server.pmap.pm_port = atoi(option);
401 mnt_server.pmap.pm_prog = MOUNTPROG;
402 option = po_get(options, "mountvers");
404 mnt_server.pmap.pm_vers = atoi(option);
406 option = po_get(options, "port");
408 nfs_server.pmap.pm_port = atoi(option);
409 po_remove_all(options, "port");
411 nfs_server.pmap.pm_prog = NFS_PROGRAM;
413 option = po_get(options, "nfsvers");
415 nfs_server.pmap.pm_vers = atoi(option);
416 po_remove_all(options, "nfsvers");
418 option = po_get(options, "vers");
420 nfs_server.pmap.pm_vers = atoi(option);
421 po_remove_all(options, "vers");
423 option = po_get(options, "proto");
425 if (strcmp(option, "tcp") == 0) {
426 nfs_server.pmap.pm_prot = IPPROTO_TCP;
427 po_remove_all(options, "proto");
429 if (strcmp(option, "udp") == 0) {
430 nfs_server.pmap.pm_prot = IPPROTO_UDP;
431 po_remove_all(options, "proto");
434 p = po_rightmost(options, "tcp", "udp");
436 case PO_KEY2_RIGHTMOST:
437 nfs_server.pmap.pm_prot = IPPROTO_UDP;
439 case PO_KEY1_RIGHTMOST:
440 nfs_server.pmap.pm_prot = IPPROTO_TCP;
443 po_remove_all(options, "tcp");
444 po_remove_all(options, "udp");
446 if (!probe_bothports(&mnt_server, &nfs_server)) {
451 snprintf(new_option, sizeof(new_option) - 1,
452 "nfsvers=%lu", nfs_server.pmap.pm_vers);
453 if (po_append(options, new_option) == PO_FAILED)
456 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
457 snprintf(new_option, sizeof(new_option) - 1,
460 snprintf(new_option, sizeof(new_option) - 1,
462 if (po_append(options, new_option) == PO_FAILED)
465 if (nfs_server.pmap.pm_port != NFS_PORT) {
466 snprintf(new_option, sizeof(new_option) - 1,
467 "port=%lu", nfs_server.pmap.pm_port);
468 if (po_append(options, new_option) == PO_FAILED)
482 * Do the mount(2) system call.
484 * Returns 1 if successful, otherwise zero.
485 * "errno" is set to reflect the individual error.
487 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
492 result = mount(mi->spec, mi->node, type,
493 mi->flags & ~(MS_USER|MS_USERS), options);
494 if (verbose && result) {
496 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
503 * Retry an NFS mount that failed because the requested service isn't
504 * available on the server.
506 * Returns 1 if successful. Otherwise, returns zero.
507 * "errno" is set to reflect the individual error.
509 * Side effect: If the retry is successful, both 'options' and
510 * 'extra_opts' are updated to reflect the mount options that worked.
511 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
513 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
515 struct mount_options *retry_options;
516 char *retry_str = NULL;
517 char **extra_opts = mi->extra_opts;
519 retry_options = rewrite_mount_options(*extra_opts);
523 if (po_join(retry_options, &retry_str) == PO_FAILED) {
524 po_destroy(retry_options);
530 printf(_("%s: text-based options (retry): '%s'\n"),
531 progname, retry_str);
533 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
534 po_destroy(retry_options);
540 *extra_opts = retry_str;
541 po_replace(mi->options, retry_options);
546 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
547 * claims the requested service isn't supported on the server, probe
548 * the server to see what's supported, rewrite the mount options,
549 * and retry the request.
551 * Returns 1 if successful. Otherwise, returns zero.
552 * "errno" is set to reflect the individual error.
554 * Side effect: If the retry is successful, both 'options' and
555 * 'extra_opts' are updated to reflect the mount options that worked.
556 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
558 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
560 char **extra_opts = mi->extra_opts;
562 if (po_join(mi->options, extra_opts) == PO_FAILED) {
568 printf(_("%s: text-based options: '%s'\n"),
569 progname, *extra_opts);
574 if (nfs_sys_mount(mi, "nfs", *extra_opts))
578 * The kernel returns EOPNOTSUPP if the RPC bind failed,
579 * and EPROTONOSUPPORT if the version isn't supported.
581 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
584 return nfs_retry_nfs23mount(mi);
588 * Attempt an NFS v4 mount via a mount(2) system call.
590 * Returns 1 if successful. Otherwise, returns zero.
591 * "errno" is set to reflect the individual error.
593 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
595 char **extra_opts = mi->extra_opts;
597 if (po_join(mi->options, extra_opts) == PO_FAILED) {
603 printf(_("%s: text-based options: '%s'\n"),
604 progname, *extra_opts);
609 return nfs_sys_mount(mi, "nfs4", *extra_opts);
613 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
615 * Returns 1 if successful. Otherwise, returns zero.
616 * "errno" is set to reflect the individual error.
618 static int nfs_try_mount(struct nfsmount_info *mi)
620 if (strncmp(mi->type, "nfs4", 4) == 0)
621 return nfs_try_nfs4mount(mi);
623 return nfs_try_nfs23mount(mi);
627 * Handle "foreground" NFS mounts.
629 * Retry the mount request for as long as the 'retry=' option says.
631 * Returns a valid mount command exit code.
633 static int nfsmount_fg(struct nfsmount_info *mi)
635 unsigned int secs = 1;
638 timeout = nfs_parse_retry_option(mi->options,
639 NFS_DEF_FG_TIMEOUT_MINUTES);
641 printf(_("%s: timeout set for %s"),
642 progname, ctime(&timeout));
645 if (nfs_try_mount(mi))
648 if (is_permanent_error(errno))
651 if (time(NULL) > timeout) {
656 if (errno != ETIMEDOUT) {
665 mount_error(mi->spec, mi->node, errno);
670 * Handle "background" NFS mount [first try]
672 * Returns a valid mount command exit code.
674 * EX_BG should cause the caller to fork and invoke nfsmount_child.
676 static int nfsmount_parent(struct nfsmount_info *mi)
678 if (nfs_try_mount(mi))
681 if (is_permanent_error(errno)) {
682 mount_error(mi->spec, mi->node, errno);
686 sys_mount_errors(mi->hostname, errno, 1, 1);
691 * Handle "background" NFS mount [retry daemon]
693 * Returns a valid mount command exit code: EX_SUCCESS if successful,
694 * EX_FAIL if a failure occurred. There's nothing to catch the
695 * error return, though, so we use sys_mount_errors to log the
698 static int nfsmount_child(struct nfsmount_info *mi)
700 unsigned int secs = 1;
703 timeout = nfs_parse_retry_option(mi->options,
704 NFS_DEF_BG_TIMEOUT_MINUTES);
713 if (nfs_try_mount(mi))
716 if (is_permanent_error(errno))
719 if (time(NULL) > timeout)
722 sys_mount_errors(mi->hostname, errno, 1, 1);
725 sys_mount_errors(mi->hostname, errno, 1, 0);
730 * Handle "background" NFS mount
732 * Returns a valid mount command exit code.
734 static int nfsmount_bg(struct nfsmount_info *mi)
737 return nfsmount_parent(mi);
739 return nfsmount_child(mi);
743 * Process mount options and try a mount system call.
745 * Returns a valid mount command exit code.
747 static int nfsmount_start(struct nfsmount_info *mi)
749 if (!nfs_validate_options(mi))
752 if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
753 return nfsmount_bg(mi);
755 return nfsmount_fg(mi);
759 * nfsmount_string - Mount an NFS file system using C string options
760 * @spec: C string specifying remote share to mount ("hostname:path")
761 * @node: C string pathname of local mounted-on directory
762 * @type: C string that represents file system type ("nfs" or "nfs4")
763 * @flags: MS_ style mount flags
764 * @extra_opts: pointer to C string containing fs-specific mount options
765 * (input and output argument)
766 * @fake: flag indicating whether to carry out the whole operation
767 * @child: one if this is a mount daemon (bg)
769 int nfsmount_string(const char *spec, const char *node, const char *type,
770 int flags, char **extra_opts, int fake, int child)
772 struct nfsmount_info mi = {
776 .extra_opts = extra_opts,
781 int retval = EX_FAIL;
783 if (!nfs_parse_devname(&mi))
786 mi.options = po_split(*extra_opts);
788 retval = nfsmount_start(&mi);
789 po_destroy(mi.options);
791 nfs_error(_("%s: internal option parsing error"), progname);