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"
52 #ifndef HAVE_DECL_AI_ADDRCONFIG
53 #define AI_ADDRCONFIG 0
57 #define NFS_PROGRAM (100003)
61 #define NFS_PORT (2049)
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 */
90 struct addrinfo *address; /* server's addresses */
92 struct mount_options *options; /* parsed mount options */
93 char **extra_opts; /* string for /etc/mtab */
95 unsigned long version; /* NFS version */
96 int flags, /* MS_ flags */
97 fake, /* actually do the mount? */
98 child; /* forked bg child? */
102 static void nfs_default_version(struct nfsmount_info *mi);
104 static void nfs_default_version(struct nfsmount_info *mi)
106 extern unsigned long config_default_vers;
108 * Use the default value set in the config file when
109 * the version has not been explicitly set.
111 if (mi->version == 0 && config_default_vers) {
112 if (config_default_vers < 4)
113 mi->version = config_default_vers;
117 inline void nfs_default_version(struct nfsmount_info *mi) {}
118 #endif /* MOUNT_CONFIG */
121 * Obtain a retry timeout value based on the value of the "retry=" option.
123 * Returns a time_t timeout timestamp, in seconds.
125 static time_t nfs_parse_retry_option(struct mount_options *options,
126 unsigned int timeout_minutes)
130 switch (po_get_numeric(options, "retry", &tmp)) {
135 timeout_minutes = tmp;
140 nfs_error(_("%s: invalid retry timeout was specified; "
141 "using default timeout"), progname);
145 return time(NULL) + (time_t)(timeout_minutes * 60);
149 * Convert the passed-in sockaddr-style address to presentation
150 * format, then append an option of the form "keyword=address".
152 * Returns 1 if the option was appended successfully; otherwise zero.
154 static int nfs_append_generic_address_option(const struct sockaddr *sap,
155 const socklen_t salen,
157 struct mount_options *options)
159 char address[NI_MAXHOST];
160 char new_option[512];
163 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
166 len = snprintf(new_option, sizeof(new_option), "%s=%s",
168 if (len < 0 || (size_t)len >= sizeof(new_option))
171 if (po_append(options, new_option) != PO_SUCCEEDED)
177 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
182 * Append the 'addr=' option to the options string to pass a resolved
183 * server address to the kernel. After a successful mount, this address
184 * is also added to /etc/mtab for use when unmounting.
186 * If 'addr=' is already present, we strip it out. This prevents users
187 * from setting a bogus 'addr=' option themselves, and also allows bg
188 * retries to recompute the server's address, in case it has changed.
190 * Returns 1 if 'addr=' option appended successfully;
193 static int nfs_append_addr_option(const struct sockaddr *sap,
195 struct mount_options *options)
197 po_remove_all(options, "addr");
198 return nfs_append_generic_address_option(sap, salen, "addr", options);
202 * Called to discover our address and append an appropriate 'clientaddr='
203 * option to the options string.
205 * Returns 1 if 'clientaddr=' option created successfully or if
206 * 'clientaddr=' option is already present; otherwise zero.
208 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
210 struct mount_options *options)
212 union nfs_sockaddr address;
213 struct sockaddr *my_addr = &address.sa;
214 socklen_t my_len = sizeof(address);
216 if (po_contains(options, "clientaddr") == PO_FOUND)
219 nfs_callback_address(sap, salen, my_addr, &my_len);
221 return nfs_append_generic_address_option(my_addr, my_len,
222 "clientaddr", options);
226 * Determine whether to append a 'mountaddr=' option. The option is needed if:
228 * 1. "mounthost=" was specified, or
229 * 2. The address families for proto= and mountproto= are different.
231 static int nfs_fix_mounthost_option(struct mount_options *options,
232 const char *nfs_hostname)
234 union nfs_sockaddr address;
235 struct sockaddr *sap = &address.sa;
236 socklen_t salen = sizeof(address);
237 sa_family_t nfs_family, mnt_family;
240 if (!nfs_nfs_proto_family(options, &nfs_family))
242 if (!nfs_mount_proto_family(options, &mnt_family))
245 mounthost = po_get(options, "mounthost");
246 if (mounthost == NULL) {
247 if (nfs_family == mnt_family)
249 mounthost = (char *)nfs_hostname;
252 if (!nfs_lookup(mounthost, mnt_family, sap, &salen)) {
253 nfs_error(_("%s: unable to determine mount server's address"),
258 return nfs_append_generic_address_option(sap, salen,
259 "mountaddr", options);
263 * Returns zero if the "lock" option is in effect, but statd
264 * can't be started. Otherwise, returns 1.
266 static const char *nfs_lock_opttbl[] = {
272 static int nfs_verify_lock_option(struct mount_options *options)
274 if (po_rightmost(options, nfs_lock_opttbl) == 0)
277 if (!start_statd()) {
278 nfs_error(_("%s: rpc.statd is not running but is "
279 "required for remote locking."), progname);
280 nfs_error(_("%s: Either use '-o nolock' to keep "
281 "locks local, or start statd."), progname);
288 static int nfs_append_sloppy_option(struct mount_options *options)
290 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
293 if (po_append(options, "sloppy") == PO_FAILED)
298 static int nfs_set_version(struct nfsmount_info *mi)
300 if (!nfs_nfs_version(mi->options, &mi->version))
303 if (strncmp(mi->type, "nfs4", 4) == 0)
306 char *option = po_get(mi->options, "proto");
307 if (option && strcmp(option, "rdma") == 0)
312 * If we still don't know, check for version-specific
315 if (mi->version == 0) {
316 if (po_contains(mi->options, "mounthost") ||
317 po_contains(mi->options, "mountaddr") ||
318 po_contains(mi->options, "mountvers") ||
319 po_contains(mi->options, "mountproto"))
324 * If enabled, see if the default version was
325 * set in the config file
327 nfs_default_version(mi);
333 * Set up mandatory non-version specific NFS mount options.
335 * Returns 1 if successful; otherwise zero.
337 static int nfs_validate_options(struct nfsmount_info *mi)
339 struct addrinfo hint = {
340 .ai_protocol = (int)IPPROTO_UDP,
341 .ai_flags = AI_ADDRCONFIG,
346 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
349 if (!nfs_nfs_proto_family(mi->options, &family))
352 hint.ai_family = (int)family;
353 error = getaddrinfo(mi->hostname, NULL, &hint, &mi->address);
355 nfs_error(_("%s: Failed to resolve server %s: %s"),
356 progname, mi->hostname, gai_strerror(error));
361 if (!nfs_set_version(mi))
364 if (!nfs_append_sloppy_option(mi->options))
367 if (!nfs_append_addr_option(mi->address->ai_addr,
368 mi->address->ai_addrlen, mi->options))
375 * Get NFS/mnt server addresses from mount options
377 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
378 * if all goes well; otherwise zero.
380 static int nfs_extract_server_addresses(struct mount_options *options,
381 struct sockaddr *nfs_saddr,
382 socklen_t *nfs_salen,
383 struct sockaddr *mnt_saddr,
384 socklen_t *mnt_salen)
388 option = po_get(options, "addr");
391 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
394 option = po_get(options, "mountaddr");
395 if (option == NULL) {
396 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
397 *mnt_salen = *nfs_salen;
398 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
404 static int nfs_construct_new_options(struct mount_options *options,
405 struct sockaddr *nfs_saddr,
406 struct pmap *nfs_pmap,
407 struct sockaddr *mnt_saddr,
408 struct pmap *mnt_pmap)
413 po_remove_all(options, "nfsprog");
414 po_remove_all(options, "mountprog");
416 po_remove_all(options, "v2");
417 po_remove_all(options, "v3");
418 po_remove_all(options, "vers");
419 po_remove_all(options, "nfsvers");
420 snprintf(new_option, sizeof(new_option) - 1,
421 "vers=%lu", nfs_pmap->pm_vers);
422 if (po_append(options, new_option) == PO_FAILED)
425 po_remove_all(options, "proto");
426 po_remove_all(options, "udp");
427 po_remove_all(options, "tcp");
428 netid = nfs_get_netid(nfs_saddr->sa_family, nfs_pmap->pm_prot);
431 snprintf(new_option, sizeof(new_option) - 1,
434 if (po_append(options, new_option) == PO_FAILED)
437 po_remove_all(options, "port");
438 if (nfs_pmap->pm_port != NFS_PORT) {
439 snprintf(new_option, sizeof(new_option) - 1,
440 "port=%lu", nfs_pmap->pm_port);
441 if (po_append(options, new_option) == PO_FAILED)
445 po_remove_all(options, "mountvers");
446 snprintf(new_option, sizeof(new_option) - 1,
447 "mountvers=%lu", mnt_pmap->pm_vers);
448 if (po_append(options, new_option) == PO_FAILED)
451 po_remove_all(options, "mountproto");
452 netid = nfs_get_netid(mnt_saddr->sa_family, mnt_pmap->pm_prot);
455 snprintf(new_option, sizeof(new_option) - 1,
456 "mountproto=%s", netid);
458 if (po_append(options, new_option) == PO_FAILED)
461 po_remove_all(options, "mountport");
462 snprintf(new_option, sizeof(new_option) - 1,
463 "mountport=%lu", mnt_pmap->pm_port);
464 if (po_append(options, new_option) == PO_FAILED)
471 * Reconstruct the mount option string based on a portmapper probe
472 * of the server. Returns one if the server's portmapper returned
473 * something we can use, otherwise zero.
475 * To handle version and transport protocol fallback properly, we
476 * need to parse some of the mount options in order to set up a
477 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
478 * doesn't recognize are left alone.
480 * Returns TRUE if rewriting was successful; otherwise
481 * FALSE is returned if some failure occurred.
484 nfs_rewrite_pmap_mount_options(struct mount_options *options)
486 union nfs_sockaddr nfs_address;
487 struct sockaddr *nfs_saddr = &nfs_address.sa;
488 socklen_t nfs_salen = sizeof(nfs_address);
489 struct pmap nfs_pmap;
490 union nfs_sockaddr mnt_address;
491 struct sockaddr *mnt_saddr = &mnt_address.sa;
492 socklen_t mnt_salen = sizeof(mnt_address);
493 struct pmap mnt_pmap;
497 * Skip option negotiation for proto=rdma mounts.
499 option = po_get(options, "proto");
500 if (option && strcmp(option, "rdma") == 0)
504 * Extract just the options needed to contact server.
505 * Bail now if any of these have bad values.
507 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
508 mnt_saddr, &mnt_salen)) {
512 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
518 * The kernel NFS client doesn't support changing the RPC
519 * program number for these services, so force the value of
520 * these fields before probing the server's ports.
522 nfs_pmap.pm_prog = NFS_PROGRAM;
523 mnt_pmap.pm_prog = MOUNTPROG;
526 * If the server's rpcbind service isn't available, we can't
527 * negotiate. Bail now if we can't contact it.
529 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
530 nfs_saddr, nfs_salen, &nfs_pmap)) {
532 if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
534 else if (rpc_createerr.cf_error.re_errno != 0)
535 errno = rpc_createerr.cf_error.re_errno;
539 if (!nfs_construct_new_options(options, nfs_saddr, &nfs_pmap,
540 mnt_saddr, &mnt_pmap)) {
551 * Do the mount(2) system call.
553 * Returns TRUE if successful, otherwise FALSE.
554 * "errno" is set to reflect the individual error.
556 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
558 char *options = NULL;
561 if (po_join(opts, &options) == PO_FAILED) {
567 printf(_("%s: trying text-based options '%s'\n"),
573 result = mount(mi->spec, mi->node, mi->type,
574 mi->flags & ~(MS_USER|MS_USERS), options);
575 if (verbose && result) {
577 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
584 * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
586 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
588 struct mount_options *options = po_dup(mi->options);
596 if (!nfs_fix_mounthost_option(options, mi->hostname)) {
600 if (!mi->fake && !nfs_verify_lock_option(options)) {
606 * Options we negotiate below may be stale by the time this
607 * file system is unmounted. In order to force umount.nfs
608 * to renegotiate with the server, only write the user-
609 * specified options, and not negotiated options, to /etc/mtab.
611 if (po_join(options, mi->extra_opts) == PO_FAILED) {
616 if (!nfs_rewrite_pmap_mount_options(options))
619 result = nfs_sys_mount(mi, options);
627 * For "-t nfs -o vers=4" or "-t nfs4" mounts.
629 static int nfs_try_mount_v4(struct nfsmount_info *mi)
631 struct addrinfo *ai = mi->address;
632 struct mount_options *options = po_dup(mi->options);
640 if (mi->version == 0) {
641 if (po_contains(options, "mounthost") ||
642 po_contains(options, "mountaddr") ||
643 po_contains(options, "mountvers") ||
644 po_contains(options, "mountproto")) {
646 * Since these mountd options are set assume version 3
647 * is wanted so error out with EPROTONOSUPPORT so the
648 * protocol negation starts with v3.
650 errno = EPROTONOSUPPORT;
653 if (po_append(options, "vers=4") == PO_FAILED) {
659 if (!nfs_append_clientaddr_option(ai->ai_addr, ai->ai_addrlen, options)) {
665 * Update option string to be recorded in /etc/mtab.
667 if (po_join(options, mi->extra_opts) == PO_FAILED) {
672 result = nfs_sys_mount(mi, options);
680 * This is a single pass through the fg/bg loop.
682 * Returns TRUE if successful, otherwise FALSE.
683 * "errno" is set to reflect the individual error.
685 static int nfs_try_mount(struct nfsmount_info *mi)
689 switch (mi->version) {
691 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
693 result = nfs_try_mount_v4(mi);
694 if (errno != EPROTONOSUPPORT) {
696 * To deal with legacy Linux servers that don't
697 * automatically export a pseudo root, retry
698 * ENOENT errors using version 3. And for
699 * Linux servers prior to 2.6.25, retry EPERM
701 if (errno != ENOENT && errno != EPERM)
707 result = nfs_try_mount_v3v2(mi);
710 result = nfs_try_mount_v4(mi);
720 * Distinguish between permanent and temporary errors.
722 * Basically, we retry if communication with the server has
723 * failed so far, but fail immediately if there is a local
724 * error (like a bad mount option).
726 * ESTALE is also a temporary error because some servers
727 * return ESTALE when a share is temporarily offline.
729 * Returns 1 if we should fail immediately, or 0 if we
732 static int nfs_is_permanent_error(int error)
738 return 0; /* temporary */
740 return 1; /* permanent */
745 * Handle "foreground" NFS mounts.
747 * Retry the mount request for as long as the 'retry=' option says.
749 * Returns a valid mount command exit code.
751 static int nfsmount_fg(struct nfsmount_info *mi)
753 unsigned int secs = 1;
756 timeout = nfs_parse_retry_option(mi->options,
757 NFS_DEF_FG_TIMEOUT_MINUTES);
759 printf(_("%s: timeout set for %s"),
760 progname, ctime(&timeout));
763 if (nfs_try_mount(mi))
766 if (nfs_is_permanent_error(errno))
769 if (time(NULL) > timeout) {
774 if (errno != ETIMEDOUT) {
783 mount_error(mi->spec, mi->node, errno);
788 * Handle "background" NFS mount [first try]
790 * Returns a valid mount command exit code.
792 * EX_BG should cause the caller to fork and invoke nfsmount_child.
794 static int nfsmount_parent(struct nfsmount_info *mi)
796 if (nfs_try_mount(mi))
799 if (nfs_is_permanent_error(errno)) {
800 mount_error(mi->spec, mi->node, errno);
804 sys_mount_errors(mi->hostname, errno, 1, 1);
809 * Handle "background" NFS mount [retry daemon]
811 * Returns a valid mount command exit code: EX_SUCCESS if successful,
812 * EX_FAIL if a failure occurred. There's nothing to catch the
813 * error return, though, so we use sys_mount_errors to log the
816 static int nfsmount_child(struct nfsmount_info *mi)
818 unsigned int secs = 1;
821 timeout = nfs_parse_retry_option(mi->options,
822 NFS_DEF_BG_TIMEOUT_MINUTES);
831 if (nfs_try_mount(mi))
834 if (nfs_is_permanent_error(errno))
837 if (time(NULL) > timeout)
840 sys_mount_errors(mi->hostname, errno, 1, 1);
843 sys_mount_errors(mi->hostname, errno, 1, 0);
848 * Handle "background" NFS mount
850 * Returns a valid mount command exit code.
852 static int nfsmount_bg(struct nfsmount_info *mi)
855 return nfsmount_parent(mi);
857 return nfsmount_child(mi);
861 * Process mount options and try a mount system call.
863 * Returns a valid mount command exit code.
865 static const char *nfs_background_opttbl[] = {
871 static int nfsmount_start(struct nfsmount_info *mi)
873 if (!nfs_validate_options(mi))
876 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
877 return nfsmount_bg(mi);
879 return nfsmount_fg(mi);
883 * nfsmount_string - Mount an NFS file system using C string options
884 * @spec: C string specifying remote share to mount ("hostname:path")
885 * @node: C string pathname of local mounted-on directory
886 * @type: C string that represents file system type ("nfs" or "nfs4")
887 * @flags: MS_ style mount flags
888 * @extra_opts: pointer to C string containing fs-specific mount options
889 * (input and output argument)
890 * @fake: flag indicating whether to carry out the whole operation
891 * @child: one if this is a mount daemon (bg)
893 int nfsmount_string(const char *spec, const char *node, const char *type,
894 int flags, char **extra_opts, int fake, int child)
896 struct nfsmount_info mi = {
901 .extra_opts = extra_opts,
906 int retval = EX_FAIL;
908 mi.options = po_split(*extra_opts);
910 retval = nfsmount_start(&mi);
911 po_destroy(mi.options);
913 nfs_error(_("%s: internal option parsing error"), progname);
915 freeaddrinfo(mi.address);