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.
37 #include <sys/socket.h>
38 #include <sys/mount.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 #include "nfs_mount.h"
46 #include "mount_constants.h"
50 #include "parse_opt.h"
53 #ifdef HAVE_RPCSVC_NFS_PROT_H
54 #include <rpcsvc/nfs_prot.h>
56 #include <linux/nfs.h>
57 #define nfsstat nfs_stat
64 #ifndef NFS_MAXHOSTNAME
65 #define NFS_MAXHOSTNAME (255)
68 #ifndef NFS_MAXPATHNAME
69 #define NFS_MAXPATHNAME (1024)
72 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
73 #define NFS_DEF_FG_TIMEOUT_MINUTES (2u)
76 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
77 #define NFS_DEF_BG_TIMEOUT_MINUTES (10000u)
80 extern int nfs_mount_data_version;
81 extern char *progname;
85 struct nfsmount_info {
86 const char *spec, /* server:/path */
87 *node, /* mounted-on dir */
88 *type; /* "nfs" or "nfs4" */
89 char *hostname; /* server's hostname */
91 struct mount_options *options; /* parsed mount options */
92 char **extra_opts; /* string for /etc/mtab */
94 int flags, /* MS_ flags */
95 fake, /* actually do the mount? */
96 child; /* forked bg child? */
98 sa_family_t family; /* supported address family */
101 static int nfs_parse_devname(struct nfsmount_info *mi)
104 char *dev, *pathname, *s;
106 dev = xstrdup(mi->spec);
108 if (!(pathname = strchr(dev, ':'))) {
109 nfs_error(_("%s: remote share not in 'host:dir' format"),
117 * We don't need a copy of the pathname, but let's
118 * sanity check it anyway.
120 if (strlen(pathname) > NFS_MAXPATHNAME) {
121 nfs_error(_("%s: export pathname is too long"),
127 * Ignore all but first hostname in replicated mounts
128 * until they can be fully supported. (mack@sgi.com)
130 if ((s = strchr(dev, ','))) {
132 nfs_error(_("%s: warning: multiple hostnames not supported"),
134 nfs_error(_("%s: ignoring hostnames that follow the first one"),
137 mi->hostname = xstrdup(dev);
138 if (strlen(mi->hostname) > NFS_MAXHOSTNAME) {
139 nfs_error(_("%s: server hostname is too long"),
153 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
156 addr->sin_family = AF_INET;
158 if (inet_aton(hostname, &addr->sin_addr))
160 if ((hp = gethostbyname(hostname)) == NULL) {
161 nfs_error(_("%s: can't get address for %s\n"),
165 if (hp->h_length > sizeof(struct in_addr)) {
166 nfs_error(_("%s: got bad hp->h_length"), progname);
167 hp->h_length = sizeof(struct in_addr);
169 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
174 * Obtain a retry timeout value based on the value of the "retry=" option.
176 * Returns a time_t timeout timestamp, in seconds.
178 static time_t nfs_parse_retry_option(struct mount_options *options,
179 unsigned int timeout_minutes)
181 char *retry_option, *endptr;
183 retry_option = po_get(options, "retry");
188 tmp = strtol(retry_option, &endptr, 10);
189 if (errno == 0 && endptr != retry_option && tmp >= 0)
190 timeout_minutes = tmp;
192 nfs_error(_("%s: invalid retry timeout was specified; "
193 "using default timeout"), progname);
196 return time(NULL) + (time_t)(timeout_minutes * 60);
200 * Convert the passed-in sockaddr-style address to presentation
201 * format, then append an option of the form "keyword=address".
203 * Returns 1 if the option was appended successfully; otherwise zero.
205 static int nfs_append_generic_address_option(const struct sockaddr *sap,
206 const socklen_t salen,
208 struct mount_options *options)
210 char address[NI_MAXHOST];
211 char new_option[512];
213 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
216 if (snprintf(new_option, sizeof(new_option), "%s=%s",
217 keyword, address) >= sizeof(new_option))
220 if (po_append(options, new_option) != PO_SUCCEEDED)
226 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
231 * Append the 'addr=' option to the options string to pass a resolved
232 * server address to the kernel. After a successful mount, this address
233 * is also added to /etc/mtab for use when unmounting.
235 * If 'addr=' is already present, we strip it out. This prevents users
236 * from setting a bogus 'addr=' option themselves, and also allows bg
237 * retries to recompute the server's address, in case it has changed.
239 * Returns 1 if 'addr=' option appended successfully;
242 static int nfs_append_addr_option(const struct sockaddr *sap,
244 struct mount_options *options)
246 po_remove_all(options, "addr");
247 return nfs_append_generic_address_option(sap, salen, "addr", options);
251 * Called to discover our address and append an appropriate 'clientaddr='
252 * option to the options string.
254 * Returns 1 if 'clientaddr=' option created successfully or if
255 * 'clientaddr=' option is already present; otherwise zero.
257 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
259 struct mount_options *options)
261 struct sockaddr_storage dummy;
262 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
263 socklen_t my_len = sizeof(dummy);
265 if (po_contains(options, "clientaddr") == PO_FOUND)
268 nfs_callback_address(sap, salen, my_addr, &my_len);
270 return nfs_append_generic_address_option(my_addr, my_len,
271 "clientaddr", options);
275 * Resolve the 'mounthost=' hostname and append a new option using
276 * the resulting address.
278 static int nfs_fix_mounthost_option(const sa_family_t family,
279 struct mount_options *options)
281 struct sockaddr_storage dummy;
282 struct sockaddr *sap = (struct sockaddr *)&dummy;
283 socklen_t salen = sizeof(dummy);
286 mounthost = po_get(options, "mounthost");
290 if (!nfs_name_to_address(mounthost, family, sap, &salen)) {
291 nfs_error(_("%s: unable to determine mount server's address"),
296 return nfs_append_generic_address_option(sap, salen,
297 "mountaddr", options);
301 * Returns zero if the "lock" option is in effect, but statd
302 * can't be started. Otherwise, returns 1.
304 static int verify_lock_option(struct mount_options *options)
306 if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
309 if (!start_statd()) {
310 nfs_error(_("%s: rpc.statd is not running but is "
311 "required for remote locking."), progname);
312 nfs_error(_("%s: Either use '-o nolock' to keep "
313 "locks local, or start statd."), progname);
320 static int nfs_append_sloppy_option(struct mount_options *options)
322 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
325 if (po_append(options, "sloppy") == PO_FAILED)
331 * Set up mandatory NFS mount options.
333 * Returns 1 if successful; otherwise zero.
335 static int nfs_validate_options(struct nfsmount_info *mi)
337 struct sockaddr_in saddr;
339 if (!fill_ipv4_sockaddr(mi->hostname, &saddr))
342 if (strncmp(mi->type, "nfs4", 4) == 0) {
343 if (!nfs_append_clientaddr_option((struct sockaddr *)&saddr,
344 sizeof(saddr), mi->options))
347 if (!nfs_fix_mounthost_option(mi->family, mi->options))
349 if (!mi->fake && !verify_lock_option(mi->options))
353 if (!nfs_append_sloppy_option(mi->options))
356 return nfs_append_addr_option((struct sockaddr *)&saddr,
357 sizeof(saddr), mi->options);
361 * Distinguish between permanent and temporary errors.
363 * Returns 0 if the passed-in error is temporary, thus the
364 * mount system call should be retried; returns one if the
365 * passed-in error is permanent, thus the mount system call
366 * should not be retried.
368 static int is_permanent_error(int error)
374 return 0; /* temporary */
376 return 1; /* permanent */
381 * Reconstruct the mount option string based on a portmapper probe
382 * of the server. Returns one if the server's portmapper returned
383 * something we can use, otherwise zero.
385 * To handle version and transport protocol fallback properly, we
386 * need to parse some of the mount options in order to set up a
387 * portmap probe. Mount options that rewrite_mount_options()
388 * doesn't recognize are left alone.
390 * Returns a new group of mount options if successful; otherwise
391 * NULL is returned if some failure occurred.
393 static struct mount_options *rewrite_mount_options(char *str)
395 struct mount_options *options;
396 char *option, new_option[64];
397 clnt_addr_t mnt_server = { };
398 clnt_addr_t nfs_server = { };
401 options = po_split(str);
408 option = po_get(options, "addr");
410 nfs_server.saddr.sin_family = AF_INET;
411 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
416 option = po_get(options, "mountaddr");
418 mnt_server.saddr.sin_family = AF_INET;
419 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
422 memcpy(&mnt_server.saddr, &nfs_server.saddr,
423 sizeof(mnt_server.saddr));
425 option = po_get(options, "mountport");
427 mnt_server.pmap.pm_port = atoi(option);
428 mnt_server.pmap.pm_prog = MOUNTPROG;
429 option = po_get(options, "mountvers");
431 mnt_server.pmap.pm_vers = atoi(option);
433 option = po_get(options, "port");
435 nfs_server.pmap.pm_port = atoi(option);
436 po_remove_all(options, "port");
438 nfs_server.pmap.pm_prog = NFS_PROGRAM;
440 option = po_get(options, "nfsvers");
442 nfs_server.pmap.pm_vers = atoi(option);
443 po_remove_all(options, "nfsvers");
445 option = po_get(options, "vers");
447 nfs_server.pmap.pm_vers = atoi(option);
448 po_remove_all(options, "vers");
450 option = po_get(options, "proto");
452 if (strcmp(option, "tcp") == 0) {
453 nfs_server.pmap.pm_prot = IPPROTO_TCP;
454 po_remove_all(options, "proto");
456 if (strcmp(option, "udp") == 0) {
457 nfs_server.pmap.pm_prot = IPPROTO_UDP;
458 po_remove_all(options, "proto");
461 p = po_rightmost(options, "tcp", "udp");
463 case PO_KEY2_RIGHTMOST:
464 nfs_server.pmap.pm_prot = IPPROTO_UDP;
466 case PO_KEY1_RIGHTMOST:
467 nfs_server.pmap.pm_prot = IPPROTO_TCP;
470 po_remove_all(options, "tcp");
471 po_remove_all(options, "udp");
473 if (!probe_bothports(&mnt_server, &nfs_server)) {
478 snprintf(new_option, sizeof(new_option) - 1,
479 "nfsvers=%lu", nfs_server.pmap.pm_vers);
480 if (po_append(options, new_option) == PO_FAILED)
483 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
484 snprintf(new_option, sizeof(new_option) - 1,
487 snprintf(new_option, sizeof(new_option) - 1,
489 if (po_append(options, new_option) == PO_FAILED)
492 if (nfs_server.pmap.pm_port != NFS_PORT) {
493 snprintf(new_option, sizeof(new_option) - 1,
494 "port=%lu", nfs_server.pmap.pm_port);
495 if (po_append(options, new_option) == PO_FAILED)
509 * Do the mount(2) system call.
511 * Returns 1 if successful, otherwise zero.
512 * "errno" is set to reflect the individual error.
514 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
519 result = mount(mi->spec, mi->node, type,
520 mi->flags & ~(MS_USER|MS_USERS), options);
521 if (verbose && result) {
523 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
530 * Retry an NFS mount that failed because the requested service isn't
531 * available on the server.
533 * Returns 1 if successful. Otherwise, returns zero.
534 * "errno" is set to reflect the individual error.
536 * Side effect: If the retry is successful, both 'options' and
537 * 'extra_opts' are updated to reflect the mount options that worked.
538 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
540 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
542 struct mount_options *retry_options;
543 char *retry_str = NULL;
544 char **extra_opts = mi->extra_opts;
546 retry_options = rewrite_mount_options(*extra_opts);
550 if (po_join(retry_options, &retry_str) == PO_FAILED) {
551 po_destroy(retry_options);
557 printf(_("%s: text-based options (retry): '%s'\n"),
558 progname, retry_str);
560 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
561 po_destroy(retry_options);
567 *extra_opts = retry_str;
568 po_replace(mi->options, retry_options);
573 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
574 * claims the requested service isn't supported on the server, probe
575 * the server to see what's supported, rewrite the mount options,
576 * and retry the request.
578 * Returns 1 if successful. Otherwise, returns zero.
579 * "errno" is set to reflect the individual error.
581 * Side effect: If the retry is successful, both 'options' and
582 * 'extra_opts' are updated to reflect the mount options that worked.
583 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
585 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
587 char **extra_opts = mi->extra_opts;
589 if (po_join(mi->options, extra_opts) == PO_FAILED) {
595 printf(_("%s: text-based options: '%s'\n"),
596 progname, *extra_opts);
601 if (nfs_sys_mount(mi, "nfs", *extra_opts))
605 * The kernel returns EOPNOTSUPP if the RPC bind failed,
606 * and EPROTONOSUPPORT if the version isn't supported.
608 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
611 return nfs_retry_nfs23mount(mi);
615 * Attempt an NFS v4 mount via a mount(2) system call.
617 * Returns 1 if successful. Otherwise, returns zero.
618 * "errno" is set to reflect the individual error.
620 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
622 char **extra_opts = mi->extra_opts;
624 if (po_join(mi->options, extra_opts) == PO_FAILED) {
630 printf(_("%s: text-based options: '%s'\n"),
631 progname, *extra_opts);
636 return nfs_sys_mount(mi, "nfs4", *extra_opts);
640 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
642 * Returns 1 if successful. Otherwise, returns zero.
643 * "errno" is set to reflect the individual error.
645 static int nfs_try_mount(struct nfsmount_info *mi)
647 if (strncmp(mi->type, "nfs4", 4) == 0)
648 return nfs_try_nfs4mount(mi);
650 return nfs_try_nfs23mount(mi);
654 * Handle "foreground" NFS mounts.
656 * Retry the mount request for as long as the 'retry=' option says.
658 * Returns a valid mount command exit code.
660 static int nfsmount_fg(struct nfsmount_info *mi)
662 unsigned int secs = 1;
665 timeout = nfs_parse_retry_option(mi->options,
666 NFS_DEF_FG_TIMEOUT_MINUTES);
668 printf(_("%s: timeout set for %s"),
669 progname, ctime(&timeout));
672 if (nfs_try_mount(mi))
675 if (is_permanent_error(errno))
678 if (time(NULL) > timeout) {
683 if (errno != ETIMEDOUT) {
692 mount_error(mi->spec, mi->node, errno);
697 * Handle "background" NFS mount [first try]
699 * Returns a valid mount command exit code.
701 * EX_BG should cause the caller to fork and invoke nfsmount_child.
703 static int nfsmount_parent(struct nfsmount_info *mi)
705 if (nfs_try_mount(mi))
708 if (is_permanent_error(errno)) {
709 mount_error(mi->spec, mi->node, errno);
713 sys_mount_errors(mi->hostname, errno, 1, 1);
718 * Handle "background" NFS mount [retry daemon]
720 * Returns a valid mount command exit code: EX_SUCCESS if successful,
721 * EX_FAIL if a failure occurred. There's nothing to catch the
722 * error return, though, so we use sys_mount_errors to log the
725 static int nfsmount_child(struct nfsmount_info *mi)
727 unsigned int secs = 1;
730 timeout = nfs_parse_retry_option(mi->options,
731 NFS_DEF_BG_TIMEOUT_MINUTES);
740 if (nfs_try_mount(mi))
743 if (is_permanent_error(errno))
746 if (time(NULL) > timeout)
749 sys_mount_errors(mi->hostname, errno, 1, 1);
752 sys_mount_errors(mi->hostname, errno, 1, 0);
757 * Handle "background" NFS mount
759 * Returns a valid mount command exit code.
761 static int nfsmount_bg(struct nfsmount_info *mi)
764 return nfsmount_parent(mi);
766 return nfsmount_child(mi);
770 * Process mount options and try a mount system call.
772 * Returns a valid mount command exit code.
774 static int nfsmount_start(struct nfsmount_info *mi)
776 if (!nfs_validate_options(mi))
779 if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
780 return nfsmount_bg(mi);
782 return nfsmount_fg(mi);
786 * nfsmount_string - Mount an NFS file system using C string options
787 * @spec: C string specifying remote share to mount ("hostname:path")
788 * @node: C string pathname of local mounted-on directory
789 * @type: C string that represents file system type ("nfs" or "nfs4")
790 * @flags: MS_ style mount flags
791 * @extra_opts: pointer to C string containing fs-specific mount options
792 * (input and output argument)
793 * @fake: flag indicating whether to carry out the whole operation
794 * @child: one if this is a mount daemon (bg)
796 int nfsmount_string(const char *spec, const char *node, const char *type,
797 int flags, char **extra_opts, int fake, int child)
799 struct nfsmount_info mi = {
803 .extra_opts = extra_opts,
807 #ifdef IPV6_SUPPORTED
808 .family = AF_UNSPEC, /* either IPv4 or v6 */
810 .family = AF_INET, /* only IPv4 */
813 int retval = EX_FAIL;
815 if (!nfs_parse_devname(&mi))
818 mi.options = po_split(*extra_opts);
820 retval = nfsmount_start(&mi);
821 po_destroy(mi.options);
823 nfs_error(_("%s: internal option parsing error"), progname);