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>
42 #include "mount_constants.h"
46 #include "parse_opt.h"
48 #include "parse_dev.h"
52 #define NFS_PROGRAM (100003)
56 #define NFS_PORT (2049)
59 #ifndef NFS_MAXHOSTNAME
60 #define NFS_MAXHOSTNAME (255)
63 #ifndef NFS_MAXPATHNAME
64 #define NFS_MAXPATHNAME (1024)
67 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
68 #define NFS_DEF_FG_TIMEOUT_MINUTES (2u)
71 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
72 #define NFS_DEF_BG_TIMEOUT_MINUTES (10000u)
75 extern int nfs_mount_data_version;
76 extern char *progname;
82 struct sockaddr_in s4;
83 struct sockaddr_in6 s6;
86 struct nfsmount_info {
87 const char *spec, /* server:/path */
88 *node, /* mounted-on dir */
89 *type; /* "nfs" or "nfs4" */
90 char *hostname; /* server's hostname */
91 union nfs_sockaddr address;
92 socklen_t salen; /* size of server's address */
94 struct mount_options *options; /* parsed mount options */
95 char **extra_opts; /* string for /etc/mtab */
97 unsigned long version; /* NFS version */
98 int flags, /* MS_ flags */
99 fake, /* actually do the mount? */
100 child; /* forked bg child? */
104 static void nfs_default_version(struct nfsmount_info *mi);
106 static void nfs_default_version(struct nfsmount_info *mi)
108 extern unsigned long config_default_vers;
110 * Use the default value set in the config file when
111 * the version has not been explicitly set.
113 if (mi->version == 0 && config_default_vers) {
114 if (config_default_vers < 4)
115 mi->version = config_default_vers;
119 inline void nfs_default_version(struct nfsmount_info *mi) {}
120 #endif /* MOUNT_CONFIG */
123 * Obtain a retry timeout value based on the value of the "retry=" option.
125 * Returns a time_t timeout timestamp, in seconds.
127 static time_t nfs_parse_retry_option(struct mount_options *options,
128 unsigned int timeout_minutes)
132 switch (po_get_numeric(options, "retry", &tmp)) {
137 timeout_minutes = tmp;
142 nfs_error(_("%s: invalid retry timeout was specified; "
143 "using default timeout"), progname);
147 return time(NULL) + (time_t)(timeout_minutes * 60);
151 * Convert the passed-in sockaddr-style address to presentation
152 * format, then append an option of the form "keyword=address".
154 * Returns 1 if the option was appended successfully; otherwise zero.
156 static int nfs_append_generic_address_option(const struct sockaddr *sap,
157 const socklen_t salen,
159 struct mount_options *options)
161 char address[NI_MAXHOST];
162 char new_option[512];
165 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
168 len = snprintf(new_option, sizeof(new_option), "%s=%s",
170 if (len < 0 || (size_t)len >= sizeof(new_option))
173 if (po_append(options, new_option) != PO_SUCCEEDED)
179 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
184 * Append the 'addr=' option to the options string to pass a resolved
185 * server address to the kernel. After a successful mount, this address
186 * is also added to /etc/mtab for use when unmounting.
188 * If 'addr=' is already present, we strip it out. This prevents users
189 * from setting a bogus 'addr=' option themselves, and also allows bg
190 * retries to recompute the server's address, in case it has changed.
192 * Returns 1 if 'addr=' option appended successfully;
195 static int nfs_append_addr_option(const struct sockaddr *sap,
197 struct mount_options *options)
199 po_remove_all(options, "addr");
200 return nfs_append_generic_address_option(sap, salen, "addr", options);
204 * Called to discover our address and append an appropriate 'clientaddr='
205 * option to the options string.
207 * Returns 1 if 'clientaddr=' option created successfully or if
208 * 'clientaddr=' option is already present; otherwise zero.
210 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
212 struct mount_options *options)
214 union nfs_sockaddr address;
215 struct sockaddr *my_addr = &address.sa;
216 socklen_t my_len = sizeof(address);
218 if (po_contains(options, "clientaddr") == PO_FOUND)
221 nfs_callback_address(sap, salen, my_addr, &my_len);
223 return nfs_append_generic_address_option(my_addr, my_len,
224 "clientaddr", options);
228 * Determine whether to append a 'mountaddr=' option. The option is needed if:
230 * 1. "mounthost=" was specified, or
231 * 2. The address families for proto= and mountproto= are different.
233 static int nfs_fix_mounthost_option(struct mount_options *options,
234 const char *nfs_hostname)
236 union nfs_sockaddr address;
237 struct sockaddr *sap = &address.sa;
238 socklen_t salen = sizeof(address);
239 sa_family_t nfs_family, mnt_family;
242 if (!nfs_nfs_proto_family(options, &nfs_family))
244 if (!nfs_mount_proto_family(options, &mnt_family))
247 mounthost = po_get(options, "mounthost");
248 if (mounthost == NULL) {
249 if (nfs_family == mnt_family)
251 mounthost = (char *)nfs_hostname;
254 if (!nfs_lookup(mounthost, mnt_family, sap, &salen)) {
255 nfs_error(_("%s: unable to determine mount server's address"),
260 return nfs_append_generic_address_option(sap, salen,
261 "mountaddr", options);
265 * Returns zero if the "lock" option is in effect, but statd
266 * can't be started. Otherwise, returns 1.
268 static const char *nfs_lock_opttbl[] = {
274 static int nfs_verify_lock_option(struct mount_options *options)
276 if (po_rightmost(options, nfs_lock_opttbl) == 0)
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);
290 static int nfs_append_sloppy_option(struct mount_options *options)
292 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
295 if (po_append(options, "sloppy") == PO_FAILED)
300 static int nfs_set_version(struct nfsmount_info *mi)
302 if (!nfs_nfs_version(mi->options, &mi->version))
305 if (strncmp(mi->type, "nfs4", 4) == 0)
308 char *option = po_get(mi->options, "proto");
309 if (option && strcmp(option, "rdma") == 0)
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 sockaddr *sap = &mi->address.sa;
344 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
347 if (!nfs_nfs_proto_family(mi->options, &family))
349 mi->salen = sizeof(mi->address);
350 if (!nfs_lookup(mi->hostname, family, sap, &mi->salen))
353 if (!nfs_set_version(mi))
356 if (!nfs_append_sloppy_option(mi->options))
359 if (!nfs_append_addr_option(sap, mi->salen, mi->options))
366 * Get NFS/mnt server addresses from mount options
368 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
369 * if all goes well; otherwise zero.
371 static int nfs_extract_server_addresses(struct mount_options *options,
372 struct sockaddr *nfs_saddr,
373 socklen_t *nfs_salen,
374 struct sockaddr *mnt_saddr,
375 socklen_t *mnt_salen)
379 option = po_get(options, "addr");
382 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
385 option = po_get(options, "mountaddr");
386 if (option == NULL) {
387 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
388 *mnt_salen = *nfs_salen;
389 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
395 static int nfs_construct_new_options(struct mount_options *options,
396 struct sockaddr *nfs_saddr,
397 struct pmap *nfs_pmap,
398 struct sockaddr *mnt_saddr,
399 struct pmap *mnt_pmap)
404 po_remove_all(options, "nfsprog");
405 po_remove_all(options, "mountprog");
407 po_remove_all(options, "v2");
408 po_remove_all(options, "v3");
409 po_remove_all(options, "vers");
410 po_remove_all(options, "nfsvers");
411 snprintf(new_option, sizeof(new_option) - 1,
412 "vers=%lu", nfs_pmap->pm_vers);
413 if (po_append(options, new_option) == PO_FAILED)
416 po_remove_all(options, "proto");
417 po_remove_all(options, "udp");
418 po_remove_all(options, "tcp");
419 netid = nfs_get_netid(nfs_saddr->sa_family, nfs_pmap->pm_prot);
422 snprintf(new_option, sizeof(new_option) - 1,
425 if (po_append(options, new_option) == PO_FAILED)
428 po_remove_all(options, "port");
429 if (nfs_pmap->pm_port != NFS_PORT) {
430 snprintf(new_option, sizeof(new_option) - 1,
431 "port=%lu", nfs_pmap->pm_port);
432 if (po_append(options, new_option) == PO_FAILED)
436 po_remove_all(options, "mountvers");
437 snprintf(new_option, sizeof(new_option) - 1,
438 "mountvers=%lu", mnt_pmap->pm_vers);
439 if (po_append(options, new_option) == PO_FAILED)
442 po_remove_all(options, "mountproto");
443 netid = nfs_get_netid(mnt_saddr->sa_family, mnt_pmap->pm_prot);
446 snprintf(new_option, sizeof(new_option) - 1,
447 "mountproto=%s", netid);
449 if (po_append(options, new_option) == PO_FAILED)
452 po_remove_all(options, "mountport");
453 snprintf(new_option, sizeof(new_option) - 1,
454 "mountport=%lu", mnt_pmap->pm_port);
455 if (po_append(options, new_option) == PO_FAILED)
462 * Reconstruct the mount option string based on a portmapper probe
463 * of the server. Returns one if the server's portmapper returned
464 * something we can use, otherwise zero.
466 * To handle version and transport protocol fallback properly, we
467 * need to parse some of the mount options in order to set up a
468 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
469 * doesn't recognize are left alone.
471 * Returns TRUE if rewriting was successful; otherwise
472 * FALSE is returned if some failure occurred.
475 nfs_rewrite_pmap_mount_options(struct mount_options *options)
477 union nfs_sockaddr nfs_address;
478 struct sockaddr *nfs_saddr = &nfs_address.sa;
479 socklen_t nfs_salen = sizeof(nfs_address);
480 struct pmap nfs_pmap;
481 union nfs_sockaddr mnt_address;
482 struct sockaddr *mnt_saddr = &mnt_address.sa;
483 socklen_t mnt_salen = sizeof(mnt_address);
484 struct pmap mnt_pmap;
488 * Skip option negotiation for proto=rdma mounts.
490 option = po_get(options, "proto");
491 if (option && strcmp(option, "rdma") == 0)
495 * Extract just the options needed to contact server.
496 * Bail now if any of these have bad values.
498 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
499 mnt_saddr, &mnt_salen)) {
503 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
509 * The kernel NFS client doesn't support changing the RPC
510 * program number for these services, so force the value of
511 * these fields before probing the server's ports.
513 nfs_pmap.pm_prog = NFS_PROGRAM;
514 mnt_pmap.pm_prog = MOUNTPROG;
517 * If the server's rpcbind service isn't available, we can't
518 * negotiate. Bail now if we can't contact it.
520 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
521 nfs_saddr, nfs_salen, &nfs_pmap)) {
526 if (!nfs_construct_new_options(options, nfs_saddr, &nfs_pmap,
527 mnt_saddr, &mnt_pmap)) {
538 * Do the mount(2) system call.
540 * Returns TRUE if successful, otherwise FALSE.
541 * "errno" is set to reflect the individual error.
543 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
545 char *options = NULL;
548 if (po_join(opts, &options) == PO_FAILED) {
554 printf(_("%s: trying text-based options '%s'\n"),
560 result = mount(mi->spec, mi->node, mi->type,
561 mi->flags & ~(MS_USER|MS_USERS), options);
562 if (verbose && result) {
564 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
571 * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
573 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
575 struct mount_options *options = po_dup(mi->options);
583 if (!nfs_fix_mounthost_option(options, mi->hostname)) {
587 if (!mi->fake && !nfs_verify_lock_option(options)) {
593 * Options we negotiate below may be stale by the time this
594 * file system is unmounted. In order to force umount.nfs
595 * to renegotiate with the server, only write the user-
596 * specified options, and not negotiated options, to /etc/mtab.
598 if (po_join(options, mi->extra_opts) == PO_FAILED) {
603 if (!nfs_rewrite_pmap_mount_options(options))
606 result = nfs_sys_mount(mi, options);
614 * For "-t nfs -o vers=4" or "-t nfs4" mounts.
616 static int nfs_try_mount_v4(struct nfsmount_info *mi)
618 struct sockaddr *sap = &mi->address.sa;
619 struct mount_options *options = po_dup(mi->options);
627 if (mi->version == 0) {
628 if (po_contains(options, "mounthost") ||
629 po_contains(options, "mountaddr") ||
630 po_contains(options, "mountvers") ||
631 po_contains(options, "mountproto")) {
633 * Since these mountd options are set assume version 3
634 * is wanted so error out with EPROTONOSUPPORT so the
635 * protocol negation starts with v3.
637 errno = EPROTONOSUPPORT;
640 if (po_append(options, "vers=4") == PO_FAILED) {
646 if (!nfs_append_clientaddr_option(sap, mi->salen, options)) {
652 * Update option string to be recorded in /etc/mtab.
654 if (po_join(options, mi->extra_opts) == PO_FAILED) {
659 result = nfs_sys_mount(mi, options);
667 * This is a single pass through the fg/bg loop.
669 * Returns TRUE if successful, otherwise FALSE.
670 * "errno" is set to reflect the individual error.
672 static int nfs_try_mount(struct nfsmount_info *mi)
676 switch (mi->version) {
678 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
680 result = nfs_try_mount_v4(mi);
681 if (errno != EPROTONOSUPPORT) {
683 * To deal with legacy Linux servers that don't
684 * automatically export a pseudo root, retry
685 * ENOENT errors using version 3. And for
686 * Linux servers prior to 2.6.25, retry EPERM
688 if (errno != ENOENT && errno != EPERM)
694 result = nfs_try_mount_v3v2(mi);
697 result = nfs_try_mount_v4(mi);
707 * Distinguish between permanent and temporary errors.
709 * Basically, we retry if communication with the server has
710 * failed so far, but fail immediately if there is a local
711 * error (like a bad mount option).
713 * ESTALE is also a temporary error because some servers
714 * return ESTALE when a share is temporarily offline.
716 * Returns 1 if we should fail immediately, or 0 if we
719 static int nfs_is_permanent_error(int error)
725 return 0; /* temporary */
727 return 1; /* permanent */
732 * Handle "foreground" NFS mounts.
734 * Retry the mount request for as long as the 'retry=' option says.
736 * Returns a valid mount command exit code.
738 static int nfsmount_fg(struct nfsmount_info *mi)
740 unsigned int secs = 1;
743 timeout = nfs_parse_retry_option(mi->options,
744 NFS_DEF_FG_TIMEOUT_MINUTES);
746 printf(_("%s: timeout set for %s"),
747 progname, ctime(&timeout));
750 if (nfs_try_mount(mi))
753 if (nfs_is_permanent_error(errno))
756 if (time(NULL) > timeout) {
761 if (errno != ETIMEDOUT) {
770 mount_error(mi->spec, mi->node, errno);
775 * Handle "background" NFS mount [first try]
777 * Returns a valid mount command exit code.
779 * EX_BG should cause the caller to fork and invoke nfsmount_child.
781 static int nfsmount_parent(struct nfsmount_info *mi)
783 if (nfs_try_mount(mi))
786 if (nfs_is_permanent_error(errno)) {
787 mount_error(mi->spec, mi->node, errno);
791 sys_mount_errors(mi->hostname, errno, 1, 1);
796 * Handle "background" NFS mount [retry daemon]
798 * Returns a valid mount command exit code: EX_SUCCESS if successful,
799 * EX_FAIL if a failure occurred. There's nothing to catch the
800 * error return, though, so we use sys_mount_errors to log the
803 static int nfsmount_child(struct nfsmount_info *mi)
805 unsigned int secs = 1;
808 timeout = nfs_parse_retry_option(mi->options,
809 NFS_DEF_BG_TIMEOUT_MINUTES);
818 if (nfs_try_mount(mi))
821 if (nfs_is_permanent_error(errno))
824 if (time(NULL) > timeout)
827 sys_mount_errors(mi->hostname, errno, 1, 1);
830 sys_mount_errors(mi->hostname, errno, 1, 0);
835 * Handle "background" NFS mount
837 * Returns a valid mount command exit code.
839 static int nfsmount_bg(struct nfsmount_info *mi)
842 return nfsmount_parent(mi);
844 return nfsmount_child(mi);
848 * Process mount options and try a mount system call.
850 * Returns a valid mount command exit code.
852 static const char *nfs_background_opttbl[] = {
858 static int nfsmount_start(struct nfsmount_info *mi)
860 if (!nfs_validate_options(mi))
863 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
864 return nfsmount_bg(mi);
866 return nfsmount_fg(mi);
870 * nfsmount_string - Mount an NFS file system using C string options
871 * @spec: C string specifying remote share to mount ("hostname:path")
872 * @node: C string pathname of local mounted-on directory
873 * @type: C string that represents file system type ("nfs" or "nfs4")
874 * @flags: MS_ style mount flags
875 * @extra_opts: pointer to C string containing fs-specific mount options
876 * (input and output argument)
877 * @fake: flag indicating whether to carry out the whole operation
878 * @child: one if this is a mount daemon (bg)
880 int nfsmount_string(const char *spec, const char *node, const char *type,
881 int flags, char **extra_opts, int fake, int child)
883 struct nfsmount_info mi = {
887 .extra_opts = extra_opts,
892 int retval = EX_FAIL;
894 mi.options = po_split(*extra_opts);
896 retval = nfsmount_start(&mi);
897 po_destroy(mi.options);
899 nfs_error(_("%s: internal option parsing error"), progname);