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;
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 */
85 struct sockaddr_storage address; /* server's address */
86 socklen_t salen; /* size of server's address */
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(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 unsigned int timeout_minutes)
126 switch (po_get_numeric(options, "retry", &tmp)) {
131 timeout_minutes = tmp;
136 nfs_error(_("%s: invalid retry timeout was specified; "
137 "using default timeout"), progname);
141 return time(NULL) + (time_t)(timeout_minutes * 60);
145 * Convert the passed-in sockaddr-style address to presentation
146 * format, then append an option of the form "keyword=address".
148 * Returns 1 if the option was appended successfully; otherwise zero.
150 static int nfs_append_generic_address_option(const struct sockaddr *sap,
151 const socklen_t salen,
153 struct mount_options *options)
155 char address[NI_MAXHOST];
156 char new_option[512];
159 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
162 len = snprintf(new_option, sizeof(new_option), "%s=%s",
164 if (len < 0 || (size_t)len >= sizeof(new_option))
167 if (po_append(options, new_option) != PO_SUCCEEDED)
173 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
178 * Append the 'addr=' option to the options string to pass a resolved
179 * server address to the kernel. After a successful mount, this address
180 * is also added to /etc/mtab for use when unmounting.
182 * If 'addr=' is already present, we strip it out. This prevents users
183 * from setting a bogus 'addr=' option themselves, and also allows bg
184 * retries to recompute the server's address, in case it has changed.
186 * Returns 1 if 'addr=' option appended successfully;
189 static int nfs_append_addr_option(const struct sockaddr *sap,
191 struct mount_options *options)
193 po_remove_all(options, "addr");
194 return nfs_append_generic_address_option(sap, salen, "addr", options);
198 * Called to discover our address and append an appropriate 'clientaddr='
199 * option to the options string.
201 * Returns 1 if 'clientaddr=' option created successfully or if
202 * 'clientaddr=' option is already present; otherwise zero.
204 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
206 struct mount_options *options)
208 struct sockaddr_storage dummy;
209 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
210 socklen_t my_len = sizeof(dummy);
212 if (po_contains(options, "clientaddr") == PO_FOUND)
215 nfs_callback_address(sap, salen, my_addr, &my_len);
217 return nfs_append_generic_address_option(my_addr, my_len,
218 "clientaddr", options);
222 * Resolve the 'mounthost=' hostname and append a new option using
223 * the resulting address.
225 static int nfs_fix_mounthost_option(struct mount_options *options)
227 struct sockaddr_storage dummy;
228 struct sockaddr *sap = (struct sockaddr *)&dummy;
229 socklen_t salen = sizeof(dummy);
232 mounthost = po_get(options, "mounthost");
236 if (!nfs_name_to_address(mounthost, sap, &salen)) {
237 nfs_error(_("%s: unable to determine mount server's address"),
242 return nfs_append_generic_address_option(sap, salen,
243 "mountaddr", options);
247 * Returns zero if the "lock" option is in effect, but statd
248 * can't be started. Otherwise, returns 1.
250 static const char *nfs_lock_opttbl[] = {
256 static int nfs_verify_lock_option(struct mount_options *options)
258 if (po_rightmost(options, nfs_lock_opttbl) == 0)
261 if (!start_statd()) {
262 nfs_error(_("%s: rpc.statd is not running but is "
263 "required for remote locking."), progname);
264 nfs_error(_("%s: Either use '-o nolock' to keep "
265 "locks local, or start statd."), progname);
272 static int nfs_append_sloppy_option(struct mount_options *options)
274 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
277 if (po_append(options, "sloppy") == PO_FAILED)
282 static int nfs_set_version(struct nfsmount_info *mi)
284 if (!nfs_nfs_version(mi->options, &mi->version))
287 if (strncmp(mi->type, "nfs4", 4) == 0)
290 char *option = po_get(mi->options, "proto");
291 if (option && strcmp(option, "rdma") == 0)
296 * If we still don't know, check for version-specific
299 if (mi->version == 0) {
300 if (po_contains(mi->options, "mounthost") ||
301 po_contains(mi->options, "mountaddr") ||
302 po_contains(mi->options, "mountvers") ||
303 po_contains(mi->options, "mountproto"))
308 * If enabled, see if the default version was
309 * set in the config file
311 nfs_default_version(mi);
317 * Set up mandatory non-version specific NFS mount options.
319 * Returns 1 if successful; otherwise zero.
321 static int nfs_validate_options(struct nfsmount_info *mi)
323 struct sockaddr *sap = (struct sockaddr *)&mi->address;
325 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
328 mi->salen = sizeof(mi->address);
329 if (!nfs_name_to_address(mi->hostname, sap, &mi->salen))
332 if (!nfs_set_version(mi))
335 if (!nfs_append_sloppy_option(mi->options))
338 if (!nfs_append_addr_option(sap, mi->salen, mi->options))
345 * Get NFS/mnt server addresses from mount options
347 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
348 * if all goes well; otherwise zero.
350 static int nfs_extract_server_addresses(struct mount_options *options,
351 struct sockaddr *nfs_saddr,
352 socklen_t *nfs_salen,
353 struct sockaddr *mnt_saddr,
354 socklen_t *mnt_salen)
358 option = po_get(options, "addr");
361 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
364 option = po_get(options, "mountaddr");
365 if (option == NULL) {
366 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
367 *mnt_salen = *nfs_salen;
368 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
374 static int nfs_construct_new_options(struct mount_options *options,
375 struct sockaddr *nfs_saddr,
376 struct pmap *nfs_pmap,
377 struct sockaddr *mnt_saddr,
378 struct pmap *mnt_pmap)
383 po_remove_all(options, "nfsprog");
384 po_remove_all(options, "mountprog");
386 po_remove_all(options, "v2");
387 po_remove_all(options, "v3");
388 po_remove_all(options, "vers");
389 po_remove_all(options, "nfsvers");
390 snprintf(new_option, sizeof(new_option) - 1,
391 "vers=%lu", nfs_pmap->pm_vers);
392 if (po_append(options, new_option) == PO_FAILED)
395 po_remove_all(options, "proto");
396 po_remove_all(options, "udp");
397 po_remove_all(options, "tcp");
398 netid = nfs_get_netid(nfs_saddr->sa_family, nfs_pmap->pm_prot);
401 snprintf(new_option, sizeof(new_option) - 1,
404 if (po_append(options, new_option) == PO_FAILED)
407 po_remove_all(options, "port");
408 if (nfs_pmap->pm_port != NFS_PORT) {
409 snprintf(new_option, sizeof(new_option) - 1,
410 "port=%lu", nfs_pmap->pm_port);
411 if (po_append(options, new_option) == PO_FAILED)
415 po_remove_all(options, "mountvers");
416 snprintf(new_option, sizeof(new_option) - 1,
417 "mountvers=%lu", mnt_pmap->pm_vers);
418 if (po_append(options, new_option) == PO_FAILED)
421 po_remove_all(options, "mountproto");
422 netid = nfs_get_netid(mnt_saddr->sa_family, mnt_pmap->pm_prot);
425 snprintf(new_option, sizeof(new_option) - 1,
426 "mountproto=%s", netid);
428 if (po_append(options, new_option) == PO_FAILED)
431 po_remove_all(options, "mountport");
432 snprintf(new_option, sizeof(new_option) - 1,
433 "mountport=%lu", mnt_pmap->pm_port);
434 if (po_append(options, new_option) == PO_FAILED)
441 * Reconstruct the mount option string based on a portmapper probe
442 * of the server. Returns one if the server's portmapper returned
443 * something we can use, otherwise zero.
445 * To handle version and transport protocol fallback properly, we
446 * need to parse some of the mount options in order to set up a
447 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
448 * doesn't recognize are left alone.
450 * Returns TRUE if rewriting was successful; otherwise
451 * FALSE is returned if some failure occurred.
454 nfs_rewrite_pmap_mount_options(struct mount_options *options)
456 struct sockaddr_storage nfs_address;
457 struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
458 socklen_t nfs_salen = sizeof(nfs_address);
459 struct pmap nfs_pmap;
460 struct sockaddr_storage mnt_address;
461 struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
462 socklen_t mnt_salen = sizeof(mnt_address);
463 struct pmap mnt_pmap;
467 * Skip option negotiation for proto=rdma mounts.
469 option = po_get(options, "proto");
470 if (option && strcmp(option, "rdma") == 0)
474 * Extract just the options needed to contact server.
475 * Bail now if any of these have bad values.
477 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
478 mnt_saddr, &mnt_salen)) {
482 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
488 * The kernel NFS client doesn't support changing the RPC
489 * program number for these services, so force the value of
490 * these fields before probing the server's ports.
492 nfs_pmap.pm_prog = NFS_PROGRAM;
493 mnt_pmap.pm_prog = MOUNTPROG;
496 * If the server's rpcbind service isn't available, we can't
497 * negotiate. Bail now if we can't contact it.
499 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
500 nfs_saddr, nfs_salen, &nfs_pmap)) {
505 if (!nfs_construct_new_options(options, nfs_saddr, &nfs_pmap,
506 mnt_saddr, &mnt_pmap)) {
517 * Do the mount(2) system call.
519 * Returns TRUE if successful, otherwise FALSE.
520 * "errno" is set to reflect the individual error.
522 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
524 char *options = NULL;
527 if (po_join(opts, &options) == PO_FAILED) {
533 printf(_("%s: trying text-based options '%s'\n"),
539 result = mount(mi->spec, mi->node, mi->type,
540 mi->flags & ~(MS_USER|MS_USERS), options);
541 if (verbose && result) {
543 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
550 * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
552 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
554 struct mount_options *options = po_dup(mi->options);
562 if (!nfs_fix_mounthost_option(options)) {
566 if (!mi->fake && !nfs_verify_lock_option(options)) {
572 * Options we negotiate below may be stale by the time this
573 * file system is unmounted. In order to force umount.nfs
574 * to renegotiate with the server, only write the user-
575 * specified options, and not negotiated options, to /etc/mtab.
577 if (po_join(options, mi->extra_opts) == PO_FAILED) {
582 if (!nfs_rewrite_pmap_mount_options(options))
585 result = nfs_sys_mount(mi, options);
593 * For "-t nfs -o vers=4" or "-t nfs4" mounts.
595 static int nfs_try_mount_v4(struct nfsmount_info *mi)
597 struct sockaddr *sap = (struct sockaddr *)&mi->address;
598 struct mount_options *options = po_dup(mi->options);
606 if (mi->version == 0) {
607 if (po_contains(options, "mounthost") ||
608 po_contains(options, "mountaddr") ||
609 po_contains(options, "mountvers") ||
610 po_contains(options, "mountproto")) {
612 * Since these mountd options are set assume version 3
613 * is wanted so error out with EPROTONOSUPPORT so the
614 * protocol negation starts with v3.
616 errno = EPROTONOSUPPORT;
619 if (po_append(options, "vers=4") == PO_FAILED) {
625 if (!nfs_append_clientaddr_option(sap, mi->salen, options)) {
631 * Update option string to be recorded in /etc/mtab.
633 if (po_join(options, mi->extra_opts) == PO_FAILED) {
638 result = nfs_sys_mount(mi, options);
646 * This is a single pass through the fg/bg loop.
648 * Returns TRUE if successful, otherwise FALSE.
649 * "errno" is set to reflect the individual error.
651 static int nfs_try_mount(struct nfsmount_info *mi)
655 switch (mi->version) {
657 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
659 result = nfs_try_mount_v4(mi);
660 if (errno != EPROTONOSUPPORT) {
662 * To deal with legacy Linux servers that don't
663 * automatically export a pseudo root, retry
664 * ENOENT errors using version 3. And for
665 * Linux servers prior to 2.6.25, retry EPERM
667 if (errno != ENOENT && errno != EPERM)
673 result = nfs_try_mount_v3v2(mi);
676 result = nfs_try_mount_v4(mi);
686 * Distinguish between permanent and temporary errors.
688 * Basically, we retry if communication with the server has
689 * failed so far, but fail immediately if there is a local
690 * error (like a bad mount option).
692 * ESTALE is also a temporary error because some servers
693 * return ESTALE when a share is temporarily offline.
695 * Returns 1 if we should fail immediately, or 0 if we
698 static int nfs_is_permanent_error(int error)
704 return 0; /* temporary */
706 return 1; /* permanent */
711 * Handle "foreground" NFS mounts.
713 * Retry the mount request for as long as the 'retry=' option says.
715 * Returns a valid mount command exit code.
717 static int nfsmount_fg(struct nfsmount_info *mi)
719 unsigned int secs = 1;
722 timeout = nfs_parse_retry_option(mi->options,
723 NFS_DEF_FG_TIMEOUT_MINUTES);
725 printf(_("%s: timeout set for %s"),
726 progname, ctime(&timeout));
729 if (nfs_try_mount(mi))
732 if (nfs_is_permanent_error(errno))
735 if (time(NULL) > timeout) {
740 if (errno != ETIMEDOUT) {
749 mount_error(mi->spec, mi->node, errno);
754 * Handle "background" NFS mount [first try]
756 * Returns a valid mount command exit code.
758 * EX_BG should cause the caller to fork and invoke nfsmount_child.
760 static int nfsmount_parent(struct nfsmount_info *mi)
762 if (nfs_try_mount(mi))
765 if (nfs_is_permanent_error(errno)) {
766 mount_error(mi->spec, mi->node, errno);
770 sys_mount_errors(mi->hostname, errno, 1, 1);
775 * Handle "background" NFS mount [retry daemon]
777 * Returns a valid mount command exit code: EX_SUCCESS if successful,
778 * EX_FAIL if a failure occurred. There's nothing to catch the
779 * error return, though, so we use sys_mount_errors to log the
782 static int nfsmount_child(struct nfsmount_info *mi)
784 unsigned int secs = 1;
787 timeout = nfs_parse_retry_option(mi->options,
788 NFS_DEF_BG_TIMEOUT_MINUTES);
797 if (nfs_try_mount(mi))
800 if (nfs_is_permanent_error(errno))
803 if (time(NULL) > timeout)
806 sys_mount_errors(mi->hostname, errno, 1, 1);
809 sys_mount_errors(mi->hostname, errno, 1, 0);
814 * Handle "background" NFS mount
816 * Returns a valid mount command exit code.
818 static int nfsmount_bg(struct nfsmount_info *mi)
821 return nfsmount_parent(mi);
823 return nfsmount_child(mi);
827 * Process mount options and try a mount system call.
829 * Returns a valid mount command exit code.
831 static const char *nfs_background_opttbl[] = {
837 static int nfsmount_start(struct nfsmount_info *mi)
839 if (!nfs_validate_options(mi))
842 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
843 return nfsmount_bg(mi);
845 return nfsmount_fg(mi);
849 * nfsmount_string - Mount an NFS file system using C string options
850 * @spec: C string specifying remote share to mount ("hostname:path")
851 * @node: C string pathname of local mounted-on directory
852 * @type: C string that represents file system type ("nfs" or "nfs4")
853 * @flags: MS_ style mount flags
854 * @extra_opts: pointer to C string containing fs-specific mount options
855 * (input and output argument)
856 * @fake: flag indicating whether to carry out the whole operation
857 * @child: one if this is a mount daemon (bg)
859 int nfsmount_string(const char *spec, const char *node, const char *type,
860 int flags, char **extra_opts, int fake, int child)
862 struct nfsmount_info mi = {
866 .extra_opts = extra_opts,
871 int retval = EX_FAIL;
873 mi.options = po_split(*extra_opts);
875 retval = nfsmount_start(&mi);
876 po_destroy(mi.options);
878 nfs_error(_("%s: internal option parsing error"), progname);