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 unsigned long protocol;
307 if (!nfs_nfs_protocol(mi->options, &protocol))
309 if (protocol == NFSPROTO_RDMA)
314 * If we still don't know, check for version-specific
317 if (mi->version == 0) {
318 if (po_contains(mi->options, "mounthost") ||
319 po_contains(mi->options, "mountaddr") ||
320 po_contains(mi->options, "mountvers") ||
321 po_contains(mi->options, "mountproto"))
326 * If enabled, see if the default version was
327 * set in the config file
329 nfs_default_version(mi);
335 * Set up mandatory non-version specific NFS mount options.
337 * Returns 1 if successful; otherwise zero.
339 static int nfs_validate_options(struct nfsmount_info *mi)
341 struct addrinfo hint = {
342 .ai_protocol = (int)IPPROTO_UDP,
343 .ai_flags = AI_ADDRCONFIG,
348 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
351 if (!nfs_nfs_proto_family(mi->options, &family))
354 hint.ai_family = (int)family;
355 error = getaddrinfo(mi->hostname, NULL, &hint, &mi->address);
357 nfs_error(_("%s: Failed to resolve server %s: %s"),
358 progname, mi->hostname, gai_strerror(error));
363 if (!nfs_set_version(mi))
366 if (!nfs_append_sloppy_option(mi->options))
369 if (!nfs_append_addr_option(mi->address->ai_addr,
370 mi->address->ai_addrlen, mi->options))
377 * Get NFS/mnt server addresses from mount options
379 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
380 * if all goes well; otherwise zero.
382 static int nfs_extract_server_addresses(struct mount_options *options,
383 struct sockaddr *nfs_saddr,
384 socklen_t *nfs_salen,
385 struct sockaddr *mnt_saddr,
386 socklen_t *mnt_salen)
390 option = po_get(options, "addr");
393 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
396 option = po_get(options, "mountaddr");
397 if (option == NULL) {
398 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
399 *mnt_salen = *nfs_salen;
400 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
406 static int nfs_construct_new_options(struct mount_options *options,
407 struct sockaddr *nfs_saddr,
408 struct pmap *nfs_pmap,
409 struct sockaddr *mnt_saddr,
410 struct pmap *mnt_pmap)
415 po_remove_all(options, "nfsprog");
416 po_remove_all(options, "mountprog");
418 po_remove_all(options, "v2");
419 po_remove_all(options, "v3");
420 po_remove_all(options, "vers");
421 po_remove_all(options, "nfsvers");
422 snprintf(new_option, sizeof(new_option) - 1,
423 "vers=%lu", nfs_pmap->pm_vers);
424 if (po_append(options, new_option) == PO_FAILED)
427 po_remove_all(options, "proto");
428 po_remove_all(options, "udp");
429 po_remove_all(options, "tcp");
430 netid = nfs_get_netid(nfs_saddr->sa_family, nfs_pmap->pm_prot);
433 snprintf(new_option, sizeof(new_option) - 1,
436 if (po_append(options, new_option) == PO_FAILED)
439 po_remove_all(options, "port");
440 if (nfs_pmap->pm_port != NFS_PORT) {
441 snprintf(new_option, sizeof(new_option) - 1,
442 "port=%lu", nfs_pmap->pm_port);
443 if (po_append(options, new_option) == PO_FAILED)
447 po_remove_all(options, "mountvers");
448 snprintf(new_option, sizeof(new_option) - 1,
449 "mountvers=%lu", mnt_pmap->pm_vers);
450 if (po_append(options, new_option) == PO_FAILED)
453 po_remove_all(options, "mountproto");
454 netid = nfs_get_netid(mnt_saddr->sa_family, mnt_pmap->pm_prot);
457 snprintf(new_option, sizeof(new_option) - 1,
458 "mountproto=%s", netid);
460 if (po_append(options, new_option) == PO_FAILED)
463 po_remove_all(options, "mountport");
464 snprintf(new_option, sizeof(new_option) - 1,
465 "mountport=%lu", mnt_pmap->pm_port);
466 if (po_append(options, new_option) == PO_FAILED)
473 * Reconstruct the mount option string based on a portmapper probe
474 * of the server. Returns one if the server's portmapper returned
475 * something we can use, otherwise zero.
477 * To handle version and transport protocol fallback properly, we
478 * need to parse some of the mount options in order to set up a
479 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
480 * doesn't recognize are left alone.
482 * Returns TRUE if rewriting was successful; otherwise
483 * FALSE is returned if some failure occurred.
486 nfs_rewrite_pmap_mount_options(struct mount_options *options)
488 union nfs_sockaddr nfs_address;
489 struct sockaddr *nfs_saddr = &nfs_address.sa;
490 socklen_t nfs_salen = sizeof(nfs_address);
491 struct pmap nfs_pmap;
492 union nfs_sockaddr mnt_address;
493 struct sockaddr *mnt_saddr = &mnt_address.sa;
494 socklen_t mnt_salen = sizeof(mnt_address);
495 unsigned long protocol;
496 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;
571 if (po_join(opts, &options) == PO_FAILED) {
579 result = mount(mi->spec, mi->node, mi->type,
580 mi->flags & ~(MS_USER|MS_USERS), options);
581 if (verbose && result) {
583 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
589 static int nfs_do_mount_v3v2(struct nfsmount_info *mi,
590 struct sockaddr *sap, socklen_t salen)
592 struct mount_options *options = po_dup(mi->options);
600 if (!nfs_append_addr_option(sap, salen, options)) {
606 if (!nfs_fix_mounthost_option(options, mi->hostname)) {
611 if (!mi->fake && !nfs_verify_lock_option(options)) {
618 * Options we negotiate below may be stale by the time this
619 * file system is unmounted. In order to force umount.nfs
620 * to renegotiate with the server, only write the user-
621 * specified options, and not negotiated options, to /etc/mtab.
623 if (po_join(options, mi->extra_opts) == PO_FAILED) {
629 printf(_("%s: trying text-based options '%s'\n"),
630 progname, *mi->extra_opts);
632 if (!nfs_rewrite_pmap_mount_options(options))
635 result = nfs_sys_mount(mi, options);
643 * Attempt a "-t nfs vers=2" or "-t nfs vers=3" mount.
645 * Returns TRUE if successful, otherwise FALSE.
646 * "errno" is set to reflect the individual error.
648 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
653 for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
654 ret = nfs_do_mount_v3v2(mi, ai->ai_addr, ai->ai_addrlen);
670 static int nfs_do_mount_v4(struct nfsmount_info *mi,
671 struct sockaddr *sap, socklen_t salen)
673 struct mount_options *options = po_dup(mi->options);
681 if (mi->version == 0) {
682 if (po_contains(options, "mounthost") ||
683 po_contains(options, "mountaddr") ||
684 po_contains(options, "mountvers") ||
685 po_contains(options, "mountproto")) {
687 * Since these mountd options are set assume version 3
688 * is wanted so error out with EPROTONOSUPPORT so the
689 * protocol negation starts with v3.
691 errno = EPROTONOSUPPORT;
694 if (po_append(options, "vers=4") == PO_FAILED) {
700 if (!nfs_append_addr_option(sap, salen, options)) {
705 if (!nfs_append_clientaddr_option(sap, salen, options)) {
711 * Update option string to be recorded in /etc/mtab.
713 if (po_join(options, mi->extra_opts) == PO_FAILED) {
719 printf(_("%s: trying text-based options '%s'\n"),
720 progname, *mi->extra_opts);
722 result = nfs_sys_mount(mi, options);
730 * Attempt a "-t nfs -o vers=4" or "-t nfs4" mount.
732 * Returns TRUE if successful, otherwise FALSE.
733 * "errno" is set to reflect the individual error.
735 static int nfs_try_mount_v4(struct nfsmount_info *mi)
740 for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
741 ret = nfs_do_mount_v4(mi, ai->ai_addr, ai->ai_addrlen);
757 * This is a single pass through the fg/bg loop.
759 * Returns TRUE if successful, otherwise FALSE.
760 * "errno" is set to reflect the individual error.
762 static int nfs_try_mount(struct nfsmount_info *mi)
766 switch (mi->version) {
768 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
770 result = nfs_try_mount_v4(mi);
771 if (errno != EPROTONOSUPPORT) {
773 * To deal with legacy Linux servers that don't
774 * automatically export a pseudo root, retry
775 * ENOENT errors using version 3. And for
776 * Linux servers prior to 2.6.25, retry EPERM
778 if (errno != ENOENT && errno != EPERM)
784 result = nfs_try_mount_v3v2(mi);
787 result = nfs_try_mount_v4(mi);
797 * Distinguish between permanent and temporary errors.
799 * Basically, we retry if communication with the server has
800 * failed so far, but fail immediately if there is a local
801 * error (like a bad mount option).
803 * ESTALE is also a temporary error because some servers
804 * return ESTALE when a share is temporarily offline.
806 * Returns 1 if we should fail immediately, or 0 if we
809 static int nfs_is_permanent_error(int error)
816 return 0; /* temporary */
818 return 1; /* permanent */
823 * Handle "foreground" NFS mounts.
825 * Retry the mount request for as long as the 'retry=' option says.
827 * Returns a valid mount command exit code.
829 static int nfsmount_fg(struct nfsmount_info *mi)
831 unsigned int secs = 1;
834 timeout = nfs_parse_retry_option(mi->options,
835 NFS_DEF_FG_TIMEOUT_MINUTES);
837 printf(_("%s: timeout set for %s"),
838 progname, ctime(&timeout));
841 if (nfs_try_mount(mi))
844 if (nfs_is_permanent_error(errno))
847 if (time(NULL) > timeout) {
852 if (errno != ETIMEDOUT) {
861 mount_error(mi->spec, mi->node, errno);
866 * Handle "background" NFS mount [first try]
868 * Returns a valid mount command exit code.
870 * EX_BG should cause the caller to fork and invoke nfsmount_child.
872 static int nfsmount_parent(struct nfsmount_info *mi)
874 if (nfs_try_mount(mi))
877 if (nfs_is_permanent_error(errno)) {
878 mount_error(mi->spec, mi->node, errno);
882 sys_mount_errors(mi->hostname, errno, 1, 1);
887 * Handle "background" NFS mount [retry daemon]
889 * Returns a valid mount command exit code: EX_SUCCESS if successful,
890 * EX_FAIL if a failure occurred. There's nothing to catch the
891 * error return, though, so we use sys_mount_errors to log the
894 static int nfsmount_child(struct nfsmount_info *mi)
896 unsigned int secs = 1;
899 timeout = nfs_parse_retry_option(mi->options,
900 NFS_DEF_BG_TIMEOUT_MINUTES);
909 if (nfs_try_mount(mi))
912 if (nfs_is_permanent_error(errno))
915 if (time(NULL) > timeout)
918 sys_mount_errors(mi->hostname, errno, 1, 1);
921 sys_mount_errors(mi->hostname, errno, 1, 0);
926 * Handle "background" NFS mount
928 * Returns a valid mount command exit code.
930 static int nfsmount_bg(struct nfsmount_info *mi)
933 return nfsmount_parent(mi);
935 return nfsmount_child(mi);
939 * Process mount options and try a mount system call.
941 * Returns a valid mount command exit code.
943 static const char *nfs_background_opttbl[] = {
949 static int nfsmount_start(struct nfsmount_info *mi)
951 if (!nfs_validate_options(mi))
954 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
955 return nfsmount_bg(mi);
957 return nfsmount_fg(mi);
961 * nfsmount_string - Mount an NFS file system using C string options
962 * @spec: C string specifying remote share to mount ("hostname:path")
963 * @node: C string pathname of local mounted-on directory
964 * @type: C string that represents file system type ("nfs" or "nfs4")
965 * @flags: MS_ style mount flags
966 * @extra_opts: pointer to C string containing fs-specific mount options
967 * (input and output argument)
968 * @fake: flag indicating whether to carry out the whole operation
969 * @child: one if this is a mount daemon (bg)
971 int nfsmount_string(const char *spec, const char *node, const char *type,
972 int flags, char **extra_opts, int fake, int child)
974 struct nfsmount_info mi = {
979 .extra_opts = extra_opts,
984 int retval = EX_FAIL;
986 mi.options = po_split(*extra_opts);
988 retval = nfsmount_start(&mi);
989 po_destroy(mi.options);
991 nfs_error(_("%s: internal option parsing error"), progname);
993 freeaddrinfo(mi.address);