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? */
99 static int nfs_parse_devname(struct nfsmount_info *mi)
102 char *dev, *pathname, *s;
104 dev = xstrdup(mi->spec);
106 if (!(pathname = strchr(dev, ':'))) {
107 nfs_error(_("%s: remote share not in 'host:dir' format"),
115 * We don't need a copy of the pathname, but let's
116 * sanity check it anyway.
118 if (strlen(pathname) > NFS_MAXPATHNAME) {
119 nfs_error(_("%s: export pathname is too long"),
125 * Ignore all but first hostname in replicated mounts
126 * until they can be fully supported. (mack@sgi.com)
128 if ((s = strchr(dev, ','))) {
130 nfs_error(_("%s: warning: multiple hostnames not supported"),
132 nfs_error(_("%s: ignoring hostnames that follow the first one"),
135 mi->hostname = xstrdup(dev);
136 if (strlen(mi->hostname) > NFS_MAXHOSTNAME) {
137 nfs_error(_("%s: server hostname is too long"),
151 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
154 addr->sin_family = AF_INET;
156 if (inet_aton(hostname, &addr->sin_addr))
158 if ((hp = gethostbyname(hostname)) == NULL) {
159 nfs_error(_("%s: can't get address for %s\n"),
163 if (hp->h_length > sizeof(struct in_addr)) {
164 nfs_error(_("%s: got bad hp->h_length"), progname);
165 hp->h_length = sizeof(struct in_addr);
167 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
172 * Obtain a retry timeout value based on the value of the "retry=" option.
174 * Returns a time_t timeout timestamp, in seconds.
176 static time_t nfs_parse_retry_option(struct mount_options *options,
177 unsigned int timeout_minutes)
179 char *retry_option, *endptr;
181 retry_option = po_get(options, "retry");
186 tmp = strtol(retry_option, &endptr, 10);
187 if (errno == 0 && endptr != retry_option && tmp >= 0)
188 timeout_minutes = tmp;
190 nfs_error(_("%s: invalid retry timeout was specified; "
191 "using default timeout"), progname);
194 return time(NULL) + (time_t)(timeout_minutes * 60);
198 * Convert the passed-in sockaddr-style address to presentation
199 * format, then append an option of the form "keyword=address".
201 * Returns 1 if the option was appended successfully; otherwise zero.
203 static int nfs_append_generic_address_option(const struct sockaddr *sap,
204 const socklen_t salen,
206 struct mount_options *options)
208 char address[NI_MAXHOST];
209 char new_option[512];
211 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
214 if (snprintf(new_option, sizeof(new_option), "%s=%s",
215 keyword, address) >= sizeof(new_option))
218 if (po_append(options, new_option) != PO_SUCCEEDED)
224 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
229 * Append the 'addr=' option to the options string to pass a resolved
230 * server address to the kernel. After a successful mount, this address
231 * is also added to /etc/mtab for use when unmounting.
233 * If 'addr=' is already present, we strip it out. This prevents users
234 * from setting a bogus 'addr=' option themselves, and also allows bg
235 * retries to recompute the server's address, in case it has changed.
237 * Returns 1 if 'addr=' option appended successfully;
240 static int nfs_append_addr_option(const struct sockaddr *sap,
242 struct mount_options *options)
244 po_remove_all(options, "addr");
245 return nfs_append_generic_address_option(sap, salen, "addr", options);
249 * Called to discover our address and append an appropriate 'clientaddr='
250 * option to the options string.
252 * Returns 1 if 'clientaddr=' option created successfully or if
253 * 'clientaddr=' option is already present; otherwise zero.
255 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
257 struct mount_options *options)
259 struct sockaddr_storage dummy;
260 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
261 socklen_t my_len = sizeof(dummy);
263 if (po_contains(options, "clientaddr") == PO_FOUND)
266 nfs_callback_address(sap, salen, my_addr, &my_len);
268 return nfs_append_generic_address_option(my_addr, my_len,
269 "clientaddr", options);
273 * Resolve the 'mounthost=' hostname and append a new option using
274 * the resulting IPv4 address.
276 static int fix_mounthost_option(struct mount_options *options)
278 struct sockaddr_in maddr;
279 char *mounthost, new_option[32];
281 mounthost = po_get(options, "mounthost");
285 if (!fill_ipv4_sockaddr(mounthost, &maddr))
288 snprintf(new_option, sizeof(new_option) - 1,
289 "mountaddr=%s", inet_ntoa(maddr.sin_addr));
291 if (po_append(options, new_option) == PO_SUCCEEDED)
297 * Returns zero if the "lock" option is in effect, but statd
298 * can't be started. Otherwise, returns 1.
300 static int verify_lock_option(struct mount_options *options)
302 if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
305 if (!start_statd()) {
306 nfs_error(_("%s: rpc.statd is not running but is "
307 "required for remote locking."), progname);
308 nfs_error(_("%s: Either use '-o nolock' to keep "
309 "locks local, or start statd."), progname);
316 static int nfs_append_sloppy_option(struct mount_options *options)
318 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
321 if (po_append(options, "sloppy") == PO_FAILED)
327 * Set up mandatory NFS mount options.
329 * Returns 1 if successful; otherwise zero.
331 static int nfs_validate_options(struct nfsmount_info *mi)
333 struct sockaddr_in saddr;
335 if (!fill_ipv4_sockaddr(mi->hostname, &saddr))
338 if (strncmp(mi->type, "nfs4", 4) == 0) {
339 if (!nfs_append_clientaddr_option((struct sockaddr *)&saddr,
340 sizeof(saddr), mi->options))
343 if (!fix_mounthost_option(mi->options))
345 if (!mi->fake && !verify_lock_option(mi->options))
349 if (!nfs_append_sloppy_option(mi->options))
352 return nfs_append_addr_option((struct sockaddr *)&saddr,
353 sizeof(saddr), mi->options);
357 * Distinguish between permanent and temporary errors.
359 * Returns 0 if the passed-in error is temporary, thus the
360 * mount system call should be retried; returns one if the
361 * passed-in error is permanent, thus the mount system call
362 * should not be retried.
364 static int is_permanent_error(int error)
370 return 0; /* temporary */
372 return 1; /* permanent */
377 * Reconstruct the mount option string based on a portmapper probe
378 * of the server. Returns one if the server's portmapper returned
379 * something we can use, otherwise zero.
381 * To handle version and transport protocol fallback properly, we
382 * need to parse some of the mount options in order to set up a
383 * portmap probe. Mount options that rewrite_mount_options()
384 * doesn't recognize are left alone.
386 * Returns a new group of mount options if successful; otherwise
387 * NULL is returned if some failure occurred.
389 static struct mount_options *rewrite_mount_options(char *str)
391 struct mount_options *options;
392 char *option, new_option[64];
393 clnt_addr_t mnt_server = { };
394 clnt_addr_t nfs_server = { };
397 options = po_split(str);
404 option = po_get(options, "addr");
406 nfs_server.saddr.sin_family = AF_INET;
407 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
412 option = po_get(options, "mountaddr");
414 mnt_server.saddr.sin_family = AF_INET;
415 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
418 memcpy(&mnt_server.saddr, &nfs_server.saddr,
419 sizeof(mnt_server.saddr));
421 option = po_get(options, "mountport");
423 mnt_server.pmap.pm_port = atoi(option);
424 mnt_server.pmap.pm_prog = MOUNTPROG;
425 option = po_get(options, "mountvers");
427 mnt_server.pmap.pm_vers = atoi(option);
429 option = po_get(options, "port");
431 nfs_server.pmap.pm_port = atoi(option);
432 po_remove_all(options, "port");
434 nfs_server.pmap.pm_prog = NFS_PROGRAM;
436 option = po_get(options, "nfsvers");
438 nfs_server.pmap.pm_vers = atoi(option);
439 po_remove_all(options, "nfsvers");
441 option = po_get(options, "vers");
443 nfs_server.pmap.pm_vers = atoi(option);
444 po_remove_all(options, "vers");
446 option = po_get(options, "proto");
448 if (strcmp(option, "tcp") == 0) {
449 nfs_server.pmap.pm_prot = IPPROTO_TCP;
450 po_remove_all(options, "proto");
452 if (strcmp(option, "udp") == 0) {
453 nfs_server.pmap.pm_prot = IPPROTO_UDP;
454 po_remove_all(options, "proto");
457 p = po_rightmost(options, "tcp", "udp");
459 case PO_KEY2_RIGHTMOST:
460 nfs_server.pmap.pm_prot = IPPROTO_UDP;
462 case PO_KEY1_RIGHTMOST:
463 nfs_server.pmap.pm_prot = IPPROTO_TCP;
466 po_remove_all(options, "tcp");
467 po_remove_all(options, "udp");
469 if (!probe_bothports(&mnt_server, &nfs_server)) {
474 snprintf(new_option, sizeof(new_option) - 1,
475 "nfsvers=%lu", nfs_server.pmap.pm_vers);
476 if (po_append(options, new_option) == PO_FAILED)
479 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
480 snprintf(new_option, sizeof(new_option) - 1,
483 snprintf(new_option, sizeof(new_option) - 1,
485 if (po_append(options, new_option) == PO_FAILED)
488 if (nfs_server.pmap.pm_port != NFS_PORT) {
489 snprintf(new_option, sizeof(new_option) - 1,
490 "port=%lu", nfs_server.pmap.pm_port);
491 if (po_append(options, new_option) == PO_FAILED)
505 * Do the mount(2) system call.
507 * Returns 1 if successful, otherwise zero.
508 * "errno" is set to reflect the individual error.
510 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
515 result = mount(mi->spec, mi->node, type,
516 mi->flags & ~(MS_USER|MS_USERS), options);
517 if (verbose && result) {
519 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
526 * Retry an NFS mount that failed because the requested service isn't
527 * available on the server.
529 * Returns 1 if successful. Otherwise, returns zero.
530 * "errno" is set to reflect the individual error.
532 * Side effect: If the retry is successful, both 'options' and
533 * 'extra_opts' are updated to reflect the mount options that worked.
534 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
536 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
538 struct mount_options *retry_options;
539 char *retry_str = NULL;
540 char **extra_opts = mi->extra_opts;
542 retry_options = rewrite_mount_options(*extra_opts);
546 if (po_join(retry_options, &retry_str) == PO_FAILED) {
547 po_destroy(retry_options);
553 printf(_("%s: text-based options (retry): '%s'\n"),
554 progname, retry_str);
556 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
557 po_destroy(retry_options);
563 *extra_opts = retry_str;
564 po_replace(mi->options, retry_options);
569 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
570 * claims the requested service isn't supported on the server, probe
571 * the server to see what's supported, rewrite the mount options,
572 * and retry the request.
574 * Returns 1 if successful. Otherwise, returns zero.
575 * "errno" is set to reflect the individual error.
577 * Side effect: If the retry is successful, both 'options' and
578 * 'extra_opts' are updated to reflect the mount options that worked.
579 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
581 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
583 char **extra_opts = mi->extra_opts;
585 if (po_join(mi->options, extra_opts) == PO_FAILED) {
591 printf(_("%s: text-based options: '%s'\n"),
592 progname, *extra_opts);
597 if (nfs_sys_mount(mi, "nfs", *extra_opts))
601 * The kernel returns EOPNOTSUPP if the RPC bind failed,
602 * and EPROTONOSUPPORT if the version isn't supported.
604 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
607 return nfs_retry_nfs23mount(mi);
611 * Attempt an NFS v4 mount via a mount(2) system call.
613 * Returns 1 if successful. Otherwise, returns zero.
614 * "errno" is set to reflect the individual error.
616 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
618 char **extra_opts = mi->extra_opts;
620 if (po_join(mi->options, extra_opts) == PO_FAILED) {
626 printf(_("%s: text-based options: '%s'\n"),
627 progname, *extra_opts);
632 return nfs_sys_mount(mi, "nfs4", *extra_opts);
636 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
638 * Returns 1 if successful. Otherwise, returns zero.
639 * "errno" is set to reflect the individual error.
641 static int nfs_try_mount(struct nfsmount_info *mi)
643 if (strncmp(mi->type, "nfs4", 4) == 0)
644 return nfs_try_nfs4mount(mi);
646 return nfs_try_nfs23mount(mi);
650 * Handle "foreground" NFS mounts.
652 * Retry the mount request for as long as the 'retry=' option says.
654 * Returns a valid mount command exit code.
656 static int nfsmount_fg(struct nfsmount_info *mi)
658 unsigned int secs = 1;
661 timeout = nfs_parse_retry_option(mi->options,
662 NFS_DEF_FG_TIMEOUT_MINUTES);
664 printf(_("%s: timeout set for %s"),
665 progname, ctime(&timeout));
668 if (nfs_try_mount(mi))
671 if (is_permanent_error(errno))
674 if (time(NULL) > timeout) {
679 if (errno != ETIMEDOUT) {
688 mount_error(mi->spec, mi->node, errno);
693 * Handle "background" NFS mount [first try]
695 * Returns a valid mount command exit code.
697 * EX_BG should cause the caller to fork and invoke nfsmount_child.
699 static int nfsmount_parent(struct nfsmount_info *mi)
701 if (nfs_try_mount(mi))
704 if (is_permanent_error(errno)) {
705 mount_error(mi->spec, mi->node, errno);
709 sys_mount_errors(mi->hostname, errno, 1, 1);
714 * Handle "background" NFS mount [retry daemon]
716 * Returns a valid mount command exit code: EX_SUCCESS if successful,
717 * EX_FAIL if a failure occurred. There's nothing to catch the
718 * error return, though, so we use sys_mount_errors to log the
721 static int nfsmount_child(struct nfsmount_info *mi)
723 unsigned int secs = 1;
726 timeout = nfs_parse_retry_option(mi->options,
727 NFS_DEF_BG_TIMEOUT_MINUTES);
736 if (nfs_try_mount(mi))
739 if (is_permanent_error(errno))
742 if (time(NULL) > timeout)
745 sys_mount_errors(mi->hostname, errno, 1, 1);
748 sys_mount_errors(mi->hostname, errno, 1, 0);
753 * Handle "background" NFS mount
755 * Returns a valid mount command exit code.
757 static int nfsmount_bg(struct nfsmount_info *mi)
760 return nfsmount_parent(mi);
762 return nfsmount_child(mi);
766 * Process mount options and try a mount system call.
768 * Returns a valid mount command exit code.
770 static int nfsmount_start(struct nfsmount_info *mi)
772 if (!nfs_validate_options(mi))
775 if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
776 return nfsmount_bg(mi);
778 return nfsmount_fg(mi);
782 * nfsmount_string - Mount an NFS file system using C string options
783 * @spec: C string specifying remote share to mount ("hostname:path")
784 * @node: C string pathname of local mounted-on directory
785 * @type: C string that represents file system type ("nfs" or "nfs4")
786 * @flags: MS_ style mount flags
787 * @extra_opts: pointer to C string containing fs-specific mount options
788 * (input and output argument)
789 * @fake: flag indicating whether to carry out the whole operation
790 * @child: one if this is a mount daemon (bg)
792 int nfsmount_string(const char *spec, const char *node, const char *type,
793 int flags, char **extra_opts, int fake, int child)
795 struct nfsmount_info mi = {
799 .extra_opts = extra_opts,
804 int retval = EX_FAIL;
806 if (!nfs_parse_devname(&mi))
809 mi.options = po_split(*extra_opts);
811 retval = nfsmount_start(&mi);
812 po_destroy(mi.options);
814 nfs_error(_("%s: internal option parsing error"), progname);