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 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
69 #define NFS_DEF_FG_TIMEOUT_MINUTES (2u)
72 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
73 #define NFS_DEF_BG_TIMEOUT_MINUTES (10000u)
76 extern int nfs_mount_data_version;
77 extern char *progname;
80 struct nfsmount_info {
81 const char *spec, /* server:/path */
82 *node, /* mounted-on dir */
83 *type; /* "nfs" or "nfs4" */
84 char *hostname; /* server's hostname */
86 struct mount_options *options; /* parsed mount options */
87 char **extra_opts; /* string for /etc/mtab */
89 int flags, /* MS_ flags */
90 fake, /* actually do the mount? */
91 child; /* forked bg child? */
94 static int nfs_parse_devname(struct nfsmount_info *mi)
97 char *dev, *pathname, *s;
99 dev = xstrdup(mi->spec);
101 if (!(pathname = strchr(dev, ':'))) {
102 nfs_error(_("%s: remote share not in 'host:dir' format"),
110 * We don't need a copy of the pathname, but let's
111 * sanity check it anyway.
113 if (strlen(pathname) > NFS_MAXPATHNAME) {
114 nfs_error(_("%s: export pathname is too long"),
120 * Ignore all but first hostname in replicated mounts
121 * until they can be fully supported. (mack@sgi.com)
123 if ((s = strchr(dev, ','))) {
125 nfs_error(_("%s: warning: multiple hostnames not supported"),
127 nfs_error(_("%s: ignoring hostnames that follow the first one"),
130 mi->hostname = xstrdup(dev);
131 if (strlen(mi->hostname) > NFS_MAXHOSTNAME) {
132 nfs_error(_("%s: server hostname is too long"),
146 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
149 addr->sin_family = AF_INET;
151 if (inet_aton(hostname, &addr->sin_addr))
153 if ((hp = gethostbyname(hostname)) == NULL) {
154 nfs_error(_("%s: can't get address for %s\n"),
158 if (hp->h_length > sizeof(struct in_addr)) {
159 nfs_error(_("%s: got bad hp->h_length"), progname);
160 hp->h_length = sizeof(struct in_addr);
162 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
167 * Obtain a retry timeout value based on the value of the "retry=" option.
169 * Returns a time_t timeout timestamp, in seconds.
171 static time_t nfs_parse_retry_option(struct mount_options *options,
172 unsigned int timeout_minutes)
174 char *retry_option, *endptr;
176 retry_option = po_get(options, "retry");
181 tmp = strtol(retry_option, &endptr, 10);
182 if (errno == 0 && endptr != retry_option && tmp >= 0)
183 timeout_minutes = tmp;
185 nfs_error(_("%s: invalid retry timeout was specified; "
186 "using default timeout"), progname);
189 return time(NULL) + (time_t)(timeout_minutes * 60);
193 * Append the 'addr=' option to the options string to pass a resolved
194 * server address to the kernel. After a successful mount, this address
195 * is also added to /etc/mtab for use when unmounting.
197 * If 'addr=' is already present, we strip it out. This prevents users
198 * from setting a bogus 'addr=' option themselves, and also allows bg
199 * retries to recompute the server's address, in case it has changed.
201 * Returns 1 if 'addr=' option appended successfully;
204 static int append_addr_option(struct sockaddr_in *saddr,
205 struct mount_options *options)
209 po_remove_all(options, "addr");
211 snprintf(new_option, sizeof(new_option) - 1,
212 "addr=%s", inet_ntoa(saddr->sin_addr));
214 if (po_append(options, new_option) == PO_SUCCEEDED)
220 * Called to discover our address and append an appropriate 'clientaddr='
221 * option to the options string.
223 * Returns 1 if 'clientaddr=' option created successfully or if
224 * 'clientaddr=' option is already present; otherwise zero.
226 static int append_clientaddr_option(struct sockaddr_in *saddr,
227 struct mount_options *options)
229 struct sockaddr_in my_addr;
232 if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
235 if (!get_client_address(saddr, &my_addr))
238 snprintf(new_option, sizeof(new_option) - 1,
239 "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
241 if (po_append(options, new_option) == PO_SUCCEEDED)
247 * Resolve the 'mounthost=' hostname and append a new option using
248 * the resulting IPv4 address.
250 static int fix_mounthost_option(struct mount_options *options)
252 struct sockaddr_in maddr;
253 char *mounthost, new_option[32];
255 mounthost = po_get(options, "mounthost");
259 if (!fill_ipv4_sockaddr(mounthost, &maddr))
262 snprintf(new_option, sizeof(new_option) - 1,
263 "mountaddr=%s", inet_ntoa(maddr.sin_addr));
265 if (po_append(options, new_option) == PO_SUCCEEDED)
271 * Returns zero if the "lock" option is in effect, but statd
272 * can't be started. Otherwise, returns 1.
274 static int verify_lock_option(struct mount_options *options)
276 if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
279 if (!start_statd()) {
280 nfs_error(_("%s: rpc.statd is not running but is "
281 "required for remote locking."), progname);
282 nfs_error(_("%s: Either use '-o nolock' to keep "
283 "locks local, or start statd."), progname);
291 * Set up mandatory NFS mount options.
293 * Returns 1 if successful; otherwise zero.
295 static int nfs_validate_options(struct nfsmount_info *mi)
297 struct sockaddr_in saddr;
299 if (!fill_ipv4_sockaddr(mi->hostname, &saddr))
302 if (strncmp(mi->type, "nfs4", 4) == 0) {
303 if (!append_clientaddr_option(&saddr, mi->options))
306 if (!fix_mounthost_option(mi->options))
308 if (!mi->fake && !verify_lock_option(mi->options))
312 if (!append_addr_option(&saddr, mi->options))
319 * Distinguish between permanent and temporary errors.
321 * Returns 0 if the passed-in error is temporary, thus the
322 * mount system call should be retried; returns one if the
323 * passed-in error is permanent, thus the mount system call
324 * should not be retried.
326 static int is_permanent_error(int error)
332 return 0; /* temporary */
334 return 1; /* permanent */
339 * Reconstruct the mount option string based on a portmapper probe
340 * of the server. Returns one if the server's portmapper returned
341 * something we can use, otherwise zero.
343 * To handle version and transport protocol fallback properly, we
344 * need to parse some of the mount options in order to set up a
345 * portmap probe. Mount options that rewrite_mount_options()
346 * doesn't recognize are left alone.
348 * Returns a new group of mount options if successful; otherwise
349 * NULL is returned if some failure occurred.
351 static struct mount_options *rewrite_mount_options(char *str)
353 struct mount_options *options;
354 char *option, new_option[64];
355 clnt_addr_t mnt_server = { };
356 clnt_addr_t nfs_server = { };
359 options = po_split(str);
363 option = po_get(options, "addr");
365 nfs_server.saddr.sin_family = AF_INET;
366 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
371 option = po_get(options, "mountaddr");
373 mnt_server.saddr.sin_family = AF_INET;
374 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
377 memcpy(&mnt_server.saddr, &nfs_server.saddr,
378 sizeof(mnt_server.saddr));
380 option = po_get(options, "mountport");
382 mnt_server.pmap.pm_port = atoi(option);
383 mnt_server.pmap.pm_prog = MOUNTPROG;
384 option = po_get(options, "mountvers");
386 mnt_server.pmap.pm_vers = atoi(option);
388 option = po_get(options, "port");
390 nfs_server.pmap.pm_port = atoi(option);
391 po_remove_all(options, "port");
393 nfs_server.pmap.pm_prog = NFS_PROGRAM;
395 option = po_get(options, "nfsvers");
397 nfs_server.pmap.pm_vers = atoi(option);
398 po_remove_all(options, "nfsvers");
400 option = po_get(options, "vers");
402 nfs_server.pmap.pm_vers = atoi(option);
403 po_remove_all(options, "vers");
405 option = po_get(options, "proto");
407 if (strcmp(option, "tcp") == 0) {
408 nfs_server.pmap.pm_prot = IPPROTO_TCP;
409 po_remove_all(options, "proto");
411 if (strcmp(option, "udp") == 0) {
412 nfs_server.pmap.pm_prot = IPPROTO_UDP;
413 po_remove_all(options, "proto");
416 p = po_rightmost(options, "tcp", "udp");
418 case PO_KEY2_RIGHTMOST:
419 nfs_server.pmap.pm_prot = IPPROTO_UDP;
421 case PO_KEY1_RIGHTMOST:
422 nfs_server.pmap.pm_prot = IPPROTO_TCP;
425 po_remove_all(options, "tcp");
426 po_remove_all(options, "udp");
428 if (!probe_bothports(&mnt_server, &nfs_server)) {
429 rpc_mount_errors("rpcbind", 0, 0);
433 snprintf(new_option, sizeof(new_option) - 1,
434 "nfsvers=%lu", nfs_server.pmap.pm_vers);
435 if (po_append(options, new_option) == PO_FAILED)
438 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
439 snprintf(new_option, sizeof(new_option) - 1,
442 snprintf(new_option, sizeof(new_option) - 1,
444 if (po_append(options, new_option) == PO_FAILED)
447 if (nfs_server.pmap.pm_port != NFS_PORT) {
448 snprintf(new_option, sizeof(new_option) - 1,
449 "port=%lu", nfs_server.pmap.pm_port);
450 if (po_append(options, new_option) == PO_FAILED)
463 * Do the mount(2) system call.
465 * Returns 1 if successful, otherwise zero.
466 * "errno" is set to reflect the individual error.
468 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
473 result = mount(mi->spec, mi->node, type,
474 mi->flags & ~(MS_USER|MS_USERS), options);
475 if (verbose && result) {
477 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
484 * Retry an NFS mount that failed because the requested service isn't
485 * available on the server.
487 * Returns 1 if successful. Otherwise, returns zero.
488 * "errno" is set to reflect the individual error.
490 * Side effect: If the retry is successful, both 'options' and
491 * 'extra_opts' are updated to reflect the mount options that worked.
492 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
494 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
496 struct mount_options *retry_options;
497 char *retry_str = NULL;
498 char **extra_opts = mi->extra_opts;
500 retry_options = rewrite_mount_options(*extra_opts);
501 if (!retry_options) {
506 if (po_join(retry_options, &retry_str) == PO_FAILED) {
507 po_destroy(retry_options);
513 printf(_("%s: text-based options (retry): '%s'\n"),
514 progname, retry_str);
516 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
517 po_destroy(retry_options);
523 *extra_opts = retry_str;
524 po_replace(mi->options, retry_options);
529 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
530 * claims the requested service isn't supported on the server, probe
531 * the server to see what's supported, rewrite the mount options,
532 * and retry the request.
534 * Returns 1 if successful. Otherwise, returns zero.
535 * "errno" is set to reflect the individual error.
537 * Side effect: If the retry is successful, both 'options' and
538 * 'extra_opts' are updated to reflect the mount options that worked.
539 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
541 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
543 char **extra_opts = mi->extra_opts;
545 if (po_join(mi->options, extra_opts) == PO_FAILED) {
551 printf(_("%s: text-based options: '%s'\n"),
552 progname, *extra_opts);
557 if (nfs_sys_mount(mi, "nfs", *extra_opts))
561 * The kernel returns EOPNOTSUPP if the RPC bind failed,
562 * and EPROTONOSUPPORT if the version isn't supported.
564 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
567 return nfs_retry_nfs23mount(mi);
571 * Attempt an NFS v4 mount via a mount(2) system call.
573 * Returns 1 if successful. Otherwise, returns zero.
574 * "errno" is set to reflect the individual error.
576 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
578 char **extra_opts = mi->extra_opts;
580 if (po_join(mi->options, extra_opts) == PO_FAILED) {
586 printf(_("%s: text-based options: '%s'\n"),
587 progname, *extra_opts);
592 return nfs_sys_mount(mi, "nfs4", *extra_opts);
596 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
598 * Returns 1 if successful. Otherwise, returns zero.
599 * "errno" is set to reflect the individual error.
601 static int nfs_try_mount(struct nfsmount_info *mi)
603 if (strncmp(mi->type, "nfs4", 4) == 0)
604 return nfs_try_nfs4mount(mi);
606 return nfs_try_nfs23mount(mi);
610 * Handle "foreground" NFS mounts.
612 * Retry the mount request for as long as the 'retry=' option says.
614 * Returns a valid mount command exit code.
616 static int nfsmount_fg(struct nfsmount_info *mi)
618 unsigned int secs = 1;
621 timeout = nfs_parse_retry_option(mi->options,
622 NFS_DEF_FG_TIMEOUT_MINUTES);
624 printf(_("%s: timeout set for %s"),
625 progname, ctime(&timeout));
628 if (nfs_try_mount(mi))
631 if (is_permanent_error(errno))
634 if (time(NULL) > timeout) {
639 if (errno != ETIMEDOUT) {
648 mount_error(mi->spec, mi->node, errno);
653 * Handle "background" NFS mount [first try]
655 * Returns a valid mount command exit code.
657 * EX_BG should cause the caller to fork and invoke nfsmount_child.
659 static int nfsmount_parent(struct nfsmount_info *mi)
661 if (nfs_try_mount(mi))
664 if (is_permanent_error(errno)) {
665 mount_error(mi->spec, mi->node, errno);
669 sys_mount_errors(mi->hostname, errno, 1, 1);
674 * Handle "background" NFS mount [retry daemon]
676 * Returns a valid mount command exit code: EX_SUCCESS if successful,
677 * EX_FAIL if a failure occurred. There's nothing to catch the
678 * error return, though, so we use sys_mount_errors to log the
681 static int nfsmount_child(struct nfsmount_info *mi)
683 unsigned int secs = 1;
686 timeout = nfs_parse_retry_option(mi->options,
687 NFS_DEF_BG_TIMEOUT_MINUTES);
696 if (nfs_try_mount(mi))
699 if (is_permanent_error(errno))
702 if (time(NULL) > timeout)
705 sys_mount_errors(mi->hostname, errno, 1, 1);
708 sys_mount_errors(mi->hostname, errno, 1, 0);
713 * Handle "background" NFS mount
715 * Returns a valid mount command exit code.
717 static int nfsmount_bg(struct nfsmount_info *mi)
720 return nfsmount_parent(mi);
722 return nfsmount_child(mi);
726 * Process mount options and try a mount system call.
728 * Returns a valid mount command exit code.
730 static int nfsmount_start(struct nfsmount_info *mi)
732 if (!nfs_validate_options(mi))
735 if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
736 return nfsmount_bg(mi);
738 return nfsmount_fg(mi);
742 * nfsmount_string - Mount an NFS file system using C string options
743 * @spec: C string specifying remote share to mount ("hostname:path")
744 * @node: C string pathname of local mounted-on directory
745 * @type: C string that represents file system type ("nfs" or "nfs4")
746 * @flags: MS_ style mount flags
747 * @extra_opts: pointer to C string containing fs-specific mount options
748 * (input and output argument)
749 * @fake: flag indicating whether to carry out the whole operation
750 * @child: one if this is a mount daemon (bg)
752 int nfsmount_string(const char *spec, const char *node, const char *type,
753 int flags, char **extra_opts, int fake, int child)
755 struct nfsmount_info mi = {
759 .extra_opts = extra_opts,
764 int retval = EX_FAIL;
766 if (!nfs_parse_devname(&mi))
769 mi.options = po_split(*extra_opts);
771 retval = nfsmount_start(&mi);
772 po_destroy(mi.options);
774 nfs_error(_("%s: internal option parsing error"), progname);