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.
33 #include <sys/socket.h>
34 #include <sys/mount.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
43 #include "mount_constants.h"
47 #include "parse_opt.h"
49 #include "parse_dev.h"
53 #define NFS_PROGRAM (100003)
57 #define NFS_PORT (2049)
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;
81 struct nfsmount_info {
82 const char *spec, /* server:/path */
83 *node, /* mounted-on dir */
84 *type; /* "nfs" or "nfs4" */
85 char *hostname; /* server's hostname */
86 struct addrinfo *address; /* server's addresses */
88 struct mount_options *options; /* parsed mount options */
89 char **extra_opts; /* string for /etc/mtab */
91 unsigned long version; /* NFS version */
92 int flags, /* MS_ flags */
93 fake, /* actually do the mount? */
94 child; /* forked bg child? */
98 static void nfs_default_version(struct nfsmount_info *mi);
100 static void nfs_default_version(struct nfsmount_info *mi)
102 extern unsigned long config_default_vers;
104 * Use the default value set in the config file when
105 * the version has not been explicitly set.
107 if (mi->version == 0 && config_default_vers) {
108 if (config_default_vers < 4)
109 mi->version = config_default_vers;
113 inline void nfs_default_version(__attribute__ ((unused)) struct nfsmount_info *mi) {}
114 #endif /* MOUNT_CONFIG */
117 * Obtain a retry timeout value based on the value of the "retry=" option.
119 * Returns a time_t timeout timestamp, in seconds.
121 static time_t nfs_parse_retry_option(struct mount_options *options,
122 const time_t default_timeout)
124 time_t timeout_minutes;
127 timeout_minutes = default_timeout;
128 switch (po_get_numeric(options, "retry", &tmp)) {
133 timeout_minutes = tmp;
139 nfs_error(_("%s: invalid retry timeout was specified; "
140 "using default timeout"), progname);
144 return time(NULL) + (timeout_minutes * 60);
148 * Convert the passed-in sockaddr-style address to presentation
149 * format, then append an option of the form "keyword=address".
151 * Returns 1 if the option was appended successfully; otherwise zero.
153 static int nfs_append_generic_address_option(const struct sockaddr *sap,
154 const socklen_t salen,
156 struct mount_options *options)
158 char address[NI_MAXHOST];
159 char new_option[512];
162 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
165 len = snprintf(new_option, sizeof(new_option), "%s=%s",
167 if (len < 0 || (size_t)len >= sizeof(new_option))
170 if (po_append(options, new_option) != PO_SUCCEEDED)
176 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
181 * Append the 'addr=' option to the options string to pass a resolved
182 * server address to the kernel. After a successful mount, this address
183 * is also added to /etc/mtab for use when unmounting.
185 * If 'addr=' is already present, we strip it out. This prevents users
186 * from setting a bogus 'addr=' option themselves, and also allows bg
187 * retries to recompute the server's address, in case it has changed.
189 * Returns 1 if 'addr=' option appended successfully;
192 static int nfs_append_addr_option(const struct sockaddr *sap,
194 struct mount_options *options)
196 po_remove_all(options, "addr");
197 return nfs_append_generic_address_option(sap, salen, "addr", options);
201 * Called to discover our address and append an appropriate 'clientaddr='
202 * option to the options string.
204 * Returns 1 if 'clientaddr=' option created successfully or if
205 * 'clientaddr=' option is already present; otherwise zero.
207 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
209 struct mount_options *options)
211 union nfs_sockaddr address;
212 struct sockaddr *my_addr = &address.sa;
213 socklen_t my_len = sizeof(address);
215 if (po_contains(options, "clientaddr") == PO_FOUND)
218 nfs_callback_address(sap, salen, my_addr, &my_len);
220 return nfs_append_generic_address_option(my_addr, my_len,
221 "clientaddr", options);
225 * Determine whether to append a 'mountaddr=' option. The option is needed if:
227 * 1. "mounthost=" was specified, or
228 * 2. The address families for proto= and mountproto= are different.
230 static int nfs_fix_mounthost_option(struct mount_options *options,
231 const char *nfs_hostname)
233 union nfs_sockaddr address;
234 struct sockaddr *sap = &address.sa;
235 socklen_t salen = sizeof(address);
236 sa_family_t nfs_family, mnt_family;
239 if (!nfs_nfs_proto_family(options, &nfs_family))
241 if (!nfs_mount_proto_family(options, &mnt_family))
244 mounthost = po_get(options, "mounthost");
245 if (mounthost == NULL) {
246 if (nfs_family == mnt_family)
248 mounthost = (char *)nfs_hostname;
251 if (!nfs_lookup(mounthost, mnt_family, sap, &salen)) {
252 nfs_error(_("%s: unable to determine mount server's address"),
257 return nfs_append_generic_address_option(sap, salen,
258 "mountaddr", options);
262 * Returns zero if the "lock" option is in effect, but statd
263 * can't be started. Otherwise, returns 1.
265 static const char *nfs_lock_opttbl[] = {
271 static int nfs_verify_lock_option(struct mount_options *options)
273 if (po_rightmost(options, nfs_lock_opttbl) == 0)
276 if (!start_statd()) {
277 nfs_error(_("%s: rpc.statd is not running but is "
278 "required for remote locking."), progname);
279 nfs_error(_("%s: Either use '-o nolock' to keep "
280 "locks local, or start statd."), progname);
287 static int nfs_append_sloppy_option(struct mount_options *options)
289 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
292 if (po_append(options, "sloppy") == PO_FAILED)
297 static int nfs_set_version(struct nfsmount_info *mi)
299 if (!nfs_nfs_version(mi->options, &mi->version))
302 if (strncmp(mi->type, "nfs4", 4) == 0)
306 * Before 2.6.32, the kernel NFS client didn't
307 * support "-t nfs vers=4" mounts, so NFS version
308 * 4 cannot be included when autonegotiating
309 * while running on those kernels.
311 if (mi->version == 0 &&
312 linux_version_code() <= MAKE_VERSION(2, 6, 31))
316 * If we still don't know, check for version-specific
319 if (mi->version == 0) {
320 if (po_contains(mi->options, "mounthost") ||
321 po_contains(mi->options, "mountaddr") ||
322 po_contains(mi->options, "mountvers") ||
323 po_contains(mi->options, "mountproto"))
328 * If enabled, see if the default version was
329 * set in the config file
331 nfs_default_version(mi);
337 * Set up mandatory non-version specific NFS mount options.
339 * Returns 1 if successful; otherwise zero.
341 static int nfs_validate_options(struct nfsmount_info *mi)
343 struct addrinfo hint = {
344 .ai_protocol = (int)IPPROTO_UDP,
349 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
352 if (!nfs_nfs_proto_family(mi->options, &family))
355 hint.ai_family = (int)family;
356 error = getaddrinfo(mi->hostname, NULL, &hint, &mi->address);
358 nfs_error(_("%s: Failed to resolve server %s: %s"),
359 progname, mi->hostname, gai_strerror(error));
364 if (!nfs_set_version(mi))
367 if (!nfs_append_sloppy_option(mi->options))
370 if (!nfs_append_addr_option(mi->address->ai_addr,
371 mi->address->ai_addrlen, mi->options))
378 * Get NFS/mnt server addresses from mount options
380 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
381 * if all goes well; otherwise zero.
383 static int nfs_extract_server_addresses(struct mount_options *options,
384 struct sockaddr *nfs_saddr,
385 socklen_t *nfs_salen,
386 struct sockaddr *mnt_saddr,
387 socklen_t *mnt_salen)
391 option = po_get(options, "addr");
394 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
397 option = po_get(options, "mountaddr");
398 if (option == NULL) {
399 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
400 *mnt_salen = *nfs_salen;
401 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
407 static int nfs_construct_new_options(struct mount_options *options,
408 struct sockaddr *nfs_saddr,
409 struct pmap *nfs_pmap,
410 struct sockaddr *mnt_saddr,
411 struct pmap *mnt_pmap)
416 po_remove_all(options, "nfsprog");
417 po_remove_all(options, "mountprog");
419 po_remove_all(options, "v2");
420 po_remove_all(options, "v3");
421 po_remove_all(options, "vers");
422 po_remove_all(options, "nfsvers");
423 snprintf(new_option, sizeof(new_option) - 1,
424 "vers=%lu", nfs_pmap->pm_vers);
425 if (po_append(options, new_option) == PO_FAILED)
428 po_remove_all(options, "proto");
429 po_remove_all(options, "udp");
430 po_remove_all(options, "tcp");
431 netid = nfs_get_netid(nfs_saddr->sa_family, nfs_pmap->pm_prot);
434 snprintf(new_option, sizeof(new_option) - 1,
437 if (po_append(options, new_option) == PO_FAILED)
440 po_remove_all(options, "port");
441 if (nfs_pmap->pm_port != NFS_PORT) {
442 snprintf(new_option, sizeof(new_option) - 1,
443 "port=%lu", nfs_pmap->pm_port);
444 if (po_append(options, new_option) == PO_FAILED)
448 po_remove_all(options, "mountvers");
449 snprintf(new_option, sizeof(new_option) - 1,
450 "mountvers=%lu", mnt_pmap->pm_vers);
451 if (po_append(options, new_option) == PO_FAILED)
454 po_remove_all(options, "mountproto");
455 netid = nfs_get_netid(mnt_saddr->sa_family, mnt_pmap->pm_prot);
458 snprintf(new_option, sizeof(new_option) - 1,
459 "mountproto=%s", netid);
461 if (po_append(options, new_option) == PO_FAILED)
464 po_remove_all(options, "mountport");
465 snprintf(new_option, sizeof(new_option) - 1,
466 "mountport=%lu", mnt_pmap->pm_port);
467 if (po_append(options, new_option) == PO_FAILED)
474 * Reconstruct the mount option string based on a portmapper probe
475 * of the server. Returns one if the server's portmapper returned
476 * something we can use, otherwise zero.
478 * To handle version and transport protocol fallback properly, we
479 * need to parse some of the mount options in order to set up a
480 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
481 * doesn't recognize are left alone.
483 * Returns TRUE if rewriting was successful; otherwise
484 * FALSE is returned if some failure occurred.
487 nfs_rewrite_pmap_mount_options(struct mount_options *options)
489 union nfs_sockaddr nfs_address;
490 struct sockaddr *nfs_saddr = &nfs_address.sa;
491 socklen_t nfs_salen = sizeof(nfs_address);
492 struct pmap nfs_pmap;
493 union nfs_sockaddr mnt_address;
494 struct sockaddr *mnt_saddr = &mnt_address.sa;
495 socklen_t mnt_salen = sizeof(mnt_address);
496 unsigned long protocol;
497 struct pmap mnt_pmap;
500 * Version and transport negotiation is not required
501 * and does not work for RDMA mounts.
503 if (!nfs_nfs_protocol(options, &protocol)) {
507 if (protocol == NFSPROTO_RDMA)
511 * Extract just the options needed to contact server.
512 * Bail now if any of these have bad values.
514 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
515 mnt_saddr, &mnt_salen)) {
519 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
525 * The kernel NFS client doesn't support changing the RPC
526 * program number for these services, so force the value of
527 * these fields before probing the server's ports.
529 nfs_pmap.pm_prog = NFS_PROGRAM;
530 mnt_pmap.pm_prog = MOUNTPROG;
533 * If the server's rpcbind service isn't available, we can't
534 * negotiate. Bail now if we can't contact it.
536 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
537 nfs_saddr, nfs_salen, &nfs_pmap)) {
539 if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
541 else if (rpc_createerr.cf_error.re_errno != 0)
542 errno = rpc_createerr.cf_error.re_errno;
546 if (!nfs_construct_new_options(options, nfs_saddr, &nfs_pmap,
547 mnt_saddr, &mnt_pmap)) {
548 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO)
549 errno = EPROTONOSUPPORT;
561 * Do the mount(2) system call.
563 * Returns TRUE if successful, otherwise FALSE.
564 * "errno" is set to reflect the individual error.
566 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
568 char *options = NULL;
574 if (po_join(opts, &options) == PO_FAILED) {
579 result = mount(mi->spec, mi->node, mi->type,
580 mi->flags & ~(MS_USER|MS_USERS), options);
583 if (verbose && result) {
585 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
591 static int nfs_do_mount_v3v2(struct nfsmount_info *mi,
592 struct sockaddr *sap, socklen_t salen)
594 struct mount_options *options = po_dup(mi->options);
602 if (!nfs_append_addr_option(sap, salen, options)) {
608 if (!nfs_fix_mounthost_option(options, mi->hostname)) {
613 if (!mi->fake && !nfs_verify_lock_option(options)) {
620 * Options we negotiate below may be stale by the time this
621 * file system is unmounted. In order to force umount.nfs
622 * to renegotiate with the server, only write the user-
623 * specified options, and not negotiated options, to /etc/mtab.
625 if (po_join(options, mi->extra_opts) == PO_FAILED) {
631 printf(_("%s: trying text-based options '%s'\n"),
632 progname, *mi->extra_opts);
634 if (!nfs_rewrite_pmap_mount_options(options))
637 result = nfs_sys_mount(mi, options);
645 * Attempt a "-t nfs vers=2" or "-t nfs vers=3" mount.
647 * Returns TRUE if successful, otherwise FALSE.
648 * "errno" is set to reflect the individual error.
650 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
655 for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
656 ret = nfs_do_mount_v3v2(mi, ai->ai_addr, ai->ai_addrlen);
672 static int nfs_do_mount_v4(struct nfsmount_info *mi,
673 struct sockaddr *sap, socklen_t salen)
675 struct mount_options *options = po_dup(mi->options);
683 if (mi->version == 0) {
684 if (po_contains(options, "mounthost") ||
685 po_contains(options, "mountaddr") ||
686 po_contains(options, "mountvers") ||
687 po_contains(options, "mountproto")) {
689 * Since these mountd options are set assume version 3
690 * is wanted so error out with EPROTONOSUPPORT so the
691 * protocol negation starts with v3.
693 errno = EPROTONOSUPPORT;
696 if (po_append(options, "vers=4") == PO_FAILED) {
702 if (!nfs_append_addr_option(sap, salen, options)) {
707 if (!nfs_append_clientaddr_option(sap, salen, options)) {
713 * Update option string to be recorded in /etc/mtab.
715 if (po_join(options, mi->extra_opts) == PO_FAILED) {
721 printf(_("%s: trying text-based options '%s'\n"),
722 progname, *mi->extra_opts);
724 result = nfs_sys_mount(mi, options);
732 * Attempt a "-t nfs -o vers=4" or "-t nfs4" mount.
734 * Returns TRUE if successful, otherwise FALSE.
735 * "errno" is set to reflect the individual error.
737 static int nfs_try_mount_v4(struct nfsmount_info *mi)
742 for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
743 ret = nfs_do_mount_v4(mi, ai->ai_addr, ai->ai_addrlen);
759 * Handle NFS version and transport protocol
762 * When no version or protocol is specified on the
763 * command line, mount.nfs negotiates with the server
764 * to determine appropriate settings for the new
767 * Returns TRUE if successful, otherwise FALSE.
768 * "errno" is set to reflect the individual error.
770 static int nfs_autonegotiate(struct nfsmount_info *mi)
774 result = nfs_try_mount_v4(mi);
779 case EPROTONOSUPPORT:
780 /* A clear indication that the server or our
781 * client does not support NFS version 4. */
784 /* Legacy Linux servers don't export an NFS
785 * version 4 pseudoroot. */
788 /* Linux servers prior to 2.6.25 may return
789 * EPERM when NFS version 4 is not supported. */
796 return nfs_try_mount_v3v2(mi);
800 * This is a single pass through the fg/bg loop.
802 * Returns TRUE if successful, otherwise FALSE.
803 * "errno" is set to reflect the individual error.
805 static int nfs_try_mount(struct nfsmount_info *mi)
809 switch (mi->version) {
811 result = nfs_autonegotiate(mi);
815 result = nfs_try_mount_v3v2(mi);
818 result = nfs_try_mount_v4(mi);
828 * Distinguish between permanent and temporary errors.
830 * Basically, we retry if communication with the server has
831 * failed so far, but fail immediately if there is a local
832 * error (like a bad mount option).
834 * ESTALE is also a temporary error because some servers
835 * return ESTALE when a share is temporarily offline.
837 * Returns 1 if we should fail immediately, or 0 if we
840 static int nfs_is_permanent_error(int error)
847 return 0; /* temporary */
849 return 1; /* permanent */
854 * Handle "foreground" NFS mounts.
856 * Retry the mount request for as long as the 'retry=' option says.
858 * Returns a valid mount command exit code.
860 static int nfsmount_fg(struct nfsmount_info *mi)
862 unsigned int secs = 1;
865 timeout = nfs_parse_retry_option(mi->options,
866 NFS_DEF_FG_TIMEOUT_MINUTES);
868 printf(_("%s: timeout set for %s"),
869 progname, ctime(&timeout));
872 if (nfs_try_mount(mi))
875 if (nfs_is_permanent_error(errno))
878 if (time(NULL) > timeout) {
883 if (errno != ETIMEDOUT) {
892 mount_error(mi->spec, mi->node, errno);
897 * Handle "background" NFS mount [first try]
899 * Returns a valid mount command exit code.
901 * EX_BG should cause the caller to fork and invoke nfsmount_child.
903 static int nfsmount_parent(struct nfsmount_info *mi)
905 if (nfs_try_mount(mi))
908 if (nfs_is_permanent_error(errno)) {
909 mount_error(mi->spec, mi->node, errno);
913 sys_mount_errors(mi->hostname, errno, 1, 1);
918 * Handle "background" NFS mount [retry daemon]
920 * Returns a valid mount command exit code: EX_SUCCESS if successful,
921 * EX_FAIL if a failure occurred. There's nothing to catch the
922 * error return, though, so we use sys_mount_errors to log the
925 static int nfsmount_child(struct nfsmount_info *mi)
927 unsigned int secs = 1;
930 timeout = nfs_parse_retry_option(mi->options,
931 NFS_DEF_BG_TIMEOUT_MINUTES);
940 if (nfs_try_mount(mi))
943 if (nfs_is_permanent_error(errno))
946 if (time(NULL) > timeout)
949 sys_mount_errors(mi->hostname, errno, 1, 1);
952 sys_mount_errors(mi->hostname, errno, 1, 0);
957 * Handle "background" NFS mount
959 * Returns a valid mount command exit code.
961 static int nfsmount_bg(struct nfsmount_info *mi)
964 return nfsmount_parent(mi);
966 return nfsmount_child(mi);
970 * Usually all that is needed for an NFS remount is to change
971 * generic mount options like "sync" or "ro". These generic
972 * options are controlled by mi->flags, not by text-based
973 * options, and no contact with the server is needed.
975 * Take care with the /etc/mtab entry for this mount; just
976 * calling update_mtab() will change an "-t nfs -o vers=4"
977 * mount to an "-t nfs -o remount" mount, and that will
978 * confuse umount.nfs.
980 * Returns a valid mount command exit code.
982 static int nfs_remount(struct nfsmount_info *mi)
984 if (nfs_sys_mount(mi, mi->options))
990 * Process mount options and try a mount system call.
992 * Returns a valid mount command exit code.
994 static const char *nfs_background_opttbl[] = {
1000 static int nfsmount_start(struct nfsmount_info *mi)
1002 if (!nfs_validate_options(mi))
1006 * Avoid retry and negotiation logic when remounting
1008 if (mi->flags & MS_REMOUNT)
1009 return nfs_remount(mi);
1011 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
1012 return nfsmount_bg(mi);
1014 return nfsmount_fg(mi);
1018 * nfsmount_string - Mount an NFS file system using C string options
1019 * @spec: C string specifying remote share to mount ("hostname:path")
1020 * @node: C string pathname of local mounted-on directory
1021 * @type: C string that represents file system type ("nfs" or "nfs4")
1022 * @flags: MS_ style mount flags
1023 * @extra_opts: pointer to C string containing fs-specific mount options
1024 * (input and output argument)
1025 * @fake: flag indicating whether to carry out the whole operation
1026 * @child: one if this is a mount daemon (bg)
1028 * Returns a valid mount command exit code.
1030 int nfsmount_string(const char *spec, const char *node, const char *type,
1031 int flags, char **extra_opts, int fake, int child)
1033 struct nfsmount_info mi = {
1038 .extra_opts = extra_opts,
1043 int retval = EX_FAIL;
1045 mi.options = po_split(*extra_opts);
1047 retval = nfsmount_start(&mi);
1048 po_destroy(mi.options);
1050 nfs_error(_("%s: internal option parsing error"), progname);
1052 freeaddrinfo(mi.address);