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>
41 #include "mount_constants.h"
45 #include "parse_opt.h"
47 #include "parse_dev.h"
51 #define NFS_PROGRAM (100003)
55 #define NFS_PORT (2049)
58 #ifndef NFS_MAXHOSTNAME
59 #define NFS_MAXHOSTNAME (255)
62 #ifndef NFS_MAXPATHNAME
63 #define NFS_MAXPATHNAME (1024)
66 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
67 #define NFS_DEF_FG_TIMEOUT_MINUTES (2u)
70 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
71 #define NFS_DEF_BG_TIMEOUT_MINUTES (10000u)
74 extern int nfs_mount_data_version;
75 extern char *progname;
79 struct nfsmount_info {
80 const char *spec, /* server:/path */
81 *node, /* mounted-on dir */
82 *type; /* "nfs" or "nfs4" */
83 char *hostname; /* server's hostname */
84 struct sockaddr_storage address; /* server's address */
85 socklen_t salen; /* size of server's address */
87 struct mount_options *options; /* parsed mount options */
88 char **extra_opts; /* string for /etc/mtab */
90 unsigned long version; /* NFS version */
91 int flags, /* MS_ flags */
92 fake, /* actually do the mount? */
93 child; /* forked bg child? */
97 static void nfs_default_version(struct nfsmount_info *mi);
99 static void nfs_default_version(struct nfsmount_info *mi)
101 extern unsigned long config_default_vers;
103 * Use the default value set in the config file when
104 * the version has not been explicitly set.
106 if (mi->version == 0 && config_default_vers) {
107 if (config_default_vers < 4)
108 mi->version = config_default_vers;
112 inline void nfs_default_version(struct nfsmount_info *mi) {}
113 #endif /* MOUNT_CONFIG */
116 * Obtain a retry timeout value based on the value of the "retry=" option.
118 * Returns a time_t timeout timestamp, in seconds.
120 static time_t nfs_parse_retry_option(struct mount_options *options,
121 unsigned int timeout_minutes)
125 switch (po_get_numeric(options, "retry", &tmp)) {
130 timeout_minutes = tmp;
135 nfs_error(_("%s: invalid retry timeout was specified; "
136 "using default timeout"), progname);
140 return time(NULL) + (time_t)(timeout_minutes * 60);
144 * Convert the passed-in sockaddr-style address to presentation
145 * format, then append an option of the form "keyword=address".
147 * Returns 1 if the option was appended successfully; otherwise zero.
149 static int nfs_append_generic_address_option(const struct sockaddr *sap,
150 const socklen_t salen,
152 struct mount_options *options)
154 char address[NI_MAXHOST];
155 char new_option[512];
158 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
161 len = snprintf(new_option, sizeof(new_option), "%s=%s",
163 if (len < 0 || (size_t)len >= sizeof(new_option))
166 if (po_append(options, new_option) != PO_SUCCEEDED)
172 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
177 * Append the 'addr=' option to the options string to pass a resolved
178 * server address to the kernel. After a successful mount, this address
179 * is also added to /etc/mtab for use when unmounting.
181 * If 'addr=' is already present, we strip it out. This prevents users
182 * from setting a bogus 'addr=' option themselves, and also allows bg
183 * retries to recompute the server's address, in case it has changed.
185 * Returns 1 if 'addr=' option appended successfully;
188 static int nfs_append_addr_option(const struct sockaddr *sap,
190 struct mount_options *options)
192 po_remove_all(options, "addr");
193 return nfs_append_generic_address_option(sap, salen, "addr", options);
197 * Called to discover our address and append an appropriate 'clientaddr='
198 * option to the options string.
200 * Returns 1 if 'clientaddr=' option created successfully or if
201 * 'clientaddr=' option is already present; otherwise zero.
203 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
205 struct mount_options *options)
207 struct sockaddr_storage dummy;
208 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
209 socklen_t my_len = sizeof(dummy);
211 if (po_contains(options, "clientaddr") == PO_FOUND)
214 nfs_callback_address(sap, salen, my_addr, &my_len);
216 return nfs_append_generic_address_option(my_addr, my_len,
217 "clientaddr", options);
221 * Resolve the 'mounthost=' hostname and append a new option using
222 * the resulting address.
224 static int nfs_fix_mounthost_option(struct mount_options *options)
226 struct sockaddr_storage dummy;
227 struct sockaddr *sap = (struct sockaddr *)&dummy;
228 socklen_t salen = sizeof(dummy);
231 mounthost = po_get(options, "mounthost");
235 if (!nfs_name_to_address(mounthost, sap, &salen)) {
236 nfs_error(_("%s: unable to determine mount server's address"),
241 return nfs_append_generic_address_option(sap, salen,
242 "mountaddr", options);
246 * Returns zero if the "lock" option is in effect, but statd
247 * can't be started. Otherwise, returns 1.
249 static const char *nfs_lock_opttbl[] = {
255 static int nfs_verify_lock_option(struct mount_options *options)
257 if (po_rightmost(options, nfs_lock_opttbl) == 0)
260 if (!start_statd()) {
261 nfs_error(_("%s: rpc.statd is not running but is "
262 "required for remote locking."), progname);
263 nfs_error(_("%s: Either use '-o nolock' to keep "
264 "locks local, or start statd."), progname);
271 static int nfs_append_sloppy_option(struct mount_options *options)
273 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
276 if (po_append(options, "sloppy") == PO_FAILED)
282 * Set up mandatory non-version specific NFS mount options.
284 * Returns 1 if successful; otherwise zero.
286 static int nfs_validate_options(struct nfsmount_info *mi)
288 struct sockaddr *sap = (struct sockaddr *)&mi->address;
290 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
293 mi->salen = sizeof(mi->address);
294 if (!nfs_name_to_address(mi->hostname, sap, &mi->salen))
297 if (!nfs_nfs_version(mi->options, &mi->version))
299 if (strncmp(mi->type, "nfs4", 4) == 0)
302 char *option = po_get(mi->options, "proto");
303 if (option && strcmp(option, "rdma") == 0)
308 * If enabled, see if the default version was
309 * set in the config file
311 nfs_default_version(mi);
313 if (!nfs_append_sloppy_option(mi->options))
316 if (!nfs_append_addr_option(sap, mi->salen, mi->options))
323 * Get NFS/mnt server addresses from mount options
325 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
326 * if all goes well; otherwise zero.
328 static int nfs_extract_server_addresses(struct mount_options *options,
329 struct sockaddr *nfs_saddr,
330 socklen_t *nfs_salen,
331 struct sockaddr *mnt_saddr,
332 socklen_t *mnt_salen)
336 option = po_get(options, "addr");
339 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
342 option = po_get(options, "mountaddr");
343 if (option == NULL) {
344 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
345 *mnt_salen = *nfs_salen;
346 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
352 static int nfs_construct_new_options(struct mount_options *options,
353 struct pmap *nfs_pmap,
354 struct pmap *mnt_pmap)
358 po_remove_all(options, "nfsprog");
359 po_remove_all(options, "mountprog");
361 po_remove_all(options, "v2");
362 po_remove_all(options, "v3");
363 po_remove_all(options, "vers");
364 po_remove_all(options, "nfsvers");
365 snprintf(new_option, sizeof(new_option) - 1,
366 "vers=%lu", nfs_pmap->pm_vers);
367 if (po_append(options, new_option) == PO_FAILED)
370 po_remove_all(options, "proto");
371 po_remove_all(options, "udp");
372 po_remove_all(options, "tcp");
373 switch (nfs_pmap->pm_prot) {
375 snprintf(new_option, sizeof(new_option) - 1,
377 if (po_append(options, new_option) == PO_FAILED)
381 snprintf(new_option, sizeof(new_option) - 1,
383 if (po_append(options, new_option) == PO_FAILED)
388 po_remove_all(options, "port");
389 if (nfs_pmap->pm_port != NFS_PORT) {
390 snprintf(new_option, sizeof(new_option) - 1,
391 "port=%lu", nfs_pmap->pm_port);
392 if (po_append(options, new_option) == PO_FAILED)
396 po_remove_all(options, "mountvers");
397 snprintf(new_option, sizeof(new_option) - 1,
398 "mountvers=%lu", mnt_pmap->pm_vers);
399 if (po_append(options, new_option) == PO_FAILED)
402 po_remove_all(options, "mountproto");
403 switch (mnt_pmap->pm_prot) {
405 snprintf(new_option, sizeof(new_option) - 1,
407 if (po_append(options, new_option) == PO_FAILED)
411 snprintf(new_option, sizeof(new_option) - 1,
413 if (po_append(options, new_option) == PO_FAILED)
418 po_remove_all(options, "mountport");
419 snprintf(new_option, sizeof(new_option) - 1,
420 "mountport=%lu", mnt_pmap->pm_port);
421 if (po_append(options, new_option) == PO_FAILED)
428 * Reconstruct the mount option string based on a portmapper probe
429 * of the server. Returns one if the server's portmapper returned
430 * something we can use, otherwise zero.
432 * To handle version and transport protocol fallback properly, we
433 * need to parse some of the mount options in order to set up a
434 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
435 * doesn't recognize are left alone.
437 * Returns TRUE if rewriting was successful; otherwise
438 * FALSE is returned if some failure occurred.
441 nfs_rewrite_pmap_mount_options(struct mount_options *options)
443 struct sockaddr_storage nfs_address;
444 struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
445 socklen_t nfs_salen = sizeof(nfs_address);
446 struct pmap nfs_pmap;
447 struct sockaddr_storage mnt_address;
448 struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
449 socklen_t mnt_salen = sizeof(mnt_address);
450 struct pmap mnt_pmap;
454 * Skip option negotiation for proto=rdma mounts.
456 option = po_get(options, "proto");
457 if (option && strcmp(option, "rdma") == 0)
461 * Extract just the options needed to contact server.
462 * Bail now if any of these have bad values.
464 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
465 mnt_saddr, &mnt_salen)) {
469 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
475 * The kernel NFS client doesn't support changing the RPC
476 * program number for these services, so force the value of
477 * these fields before probing the server's ports.
479 nfs_pmap.pm_prog = NFS_PROGRAM;
480 mnt_pmap.pm_prog = MOUNTPROG;
483 * If the server's rpcbind service isn't available, we can't
484 * negotiate. Bail now if we can't contact it.
486 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
487 nfs_saddr, nfs_salen, &nfs_pmap)) {
492 if (!nfs_construct_new_options(options, &nfs_pmap, &mnt_pmap)) {
503 * Do the mount(2) system call.
505 * Returns TRUE if successful, otherwise FALSE.
506 * "errno" is set to reflect the individual error.
508 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
510 char *options = NULL;
513 if (po_join(opts, &options) == PO_FAILED) {
519 printf(_("%s: trying text-based options '%s'\n"),
525 result = mount(mi->spec, mi->node, mi->type,
526 mi->flags & ~(MS_USER|MS_USERS), options);
527 if (verbose && result) {
529 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
536 * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
538 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
540 struct mount_options *options = po_dup(mi->options);
548 if (!nfs_fix_mounthost_option(options)) {
552 if (!mi->fake && !nfs_verify_lock_option(options)) {
558 * Options we negotiate below may be stale by the time this
559 * file system is unmounted. In order to force umount.nfs
560 * to renegotiate with the server, only write the user-
561 * specified options, and not negotiated options, to /etc/mtab.
563 if (po_join(options, mi->extra_opts) == PO_FAILED) {
568 if (!nfs_rewrite_pmap_mount_options(options))
571 result = nfs_sys_mount(mi, options);
579 * For "-t nfs -o vers=4" or "-t nfs4" mounts.
581 static int nfs_try_mount_v4(struct nfsmount_info *mi)
583 struct sockaddr *sap = (struct sockaddr *)&mi->address;
584 struct mount_options *options = po_dup(mi->options);
592 if (mi->version == 0) {
593 if (po_append(options, "vers=4") == PO_FAILED) {
599 if (!nfs_append_clientaddr_option(sap, mi->salen, options)) {
604 * Update option string to be recorded in /etc/mtab.
606 if (po_join(options, mi->extra_opts) == PO_FAILED) {
611 result = nfs_sys_mount(mi, options);
619 * This is a single pass through the fg/bg loop.
621 * Returns TRUE if successful, otherwise FALSE.
622 * "errno" is set to reflect the individual error.
624 static int nfs_try_mount(struct nfsmount_info *mi)
628 switch (mi->version) {
630 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
632 result = nfs_try_mount_v4(mi);
633 if (errno != EPROTONOSUPPORT) {
635 * To deal with legacy Linux servers that don't
636 * automatically export a pseudo root, retry
637 * ENOENT errors using version 3
645 result = nfs_try_mount_v3v2(mi);
648 result = nfs_try_mount_v4(mi);
658 * Distinguish between permanent and temporary errors.
660 * Basically, we retry if communication with the server has
661 * failed so far, but fail immediately if there is a local
662 * error (like a bad mount option).
664 * ESTALE is also a temporary error because some servers
665 * return ESTALE when a share is temporarily offline.
667 * Returns 1 if we should fail immediately, or 0 if we
670 static int nfs_is_permanent_error(int error)
676 return 0; /* temporary */
678 return 1; /* permanent */
683 * Handle "foreground" NFS mounts.
685 * Retry the mount request for as long as the 'retry=' option says.
687 * Returns a valid mount command exit code.
689 static int nfsmount_fg(struct nfsmount_info *mi)
691 unsigned int secs = 1;
694 timeout = nfs_parse_retry_option(mi->options,
695 NFS_DEF_FG_TIMEOUT_MINUTES);
697 printf(_("%s: timeout set for %s"),
698 progname, ctime(&timeout));
701 if (nfs_try_mount(mi))
704 if (nfs_is_permanent_error(errno))
707 if (time(NULL) > timeout) {
712 if (errno != ETIMEDOUT) {
721 mount_error(mi->spec, mi->node, errno);
726 * Handle "background" NFS mount [first try]
728 * Returns a valid mount command exit code.
730 * EX_BG should cause the caller to fork and invoke nfsmount_child.
732 static int nfsmount_parent(struct nfsmount_info *mi)
734 if (nfs_try_mount(mi))
737 if (nfs_is_permanent_error(errno)) {
738 mount_error(mi->spec, mi->node, errno);
742 sys_mount_errors(mi->hostname, errno, 1, 1);
747 * Handle "background" NFS mount [retry daemon]
749 * Returns a valid mount command exit code: EX_SUCCESS if successful,
750 * EX_FAIL if a failure occurred. There's nothing to catch the
751 * error return, though, so we use sys_mount_errors to log the
754 static int nfsmount_child(struct nfsmount_info *mi)
756 unsigned int secs = 1;
759 timeout = nfs_parse_retry_option(mi->options,
760 NFS_DEF_BG_TIMEOUT_MINUTES);
769 if (nfs_try_mount(mi))
772 if (nfs_is_permanent_error(errno))
775 if (time(NULL) > timeout)
778 sys_mount_errors(mi->hostname, errno, 1, 1);
781 sys_mount_errors(mi->hostname, errno, 1, 0);
786 * Handle "background" NFS mount
788 * Returns a valid mount command exit code.
790 static int nfsmount_bg(struct nfsmount_info *mi)
793 return nfsmount_parent(mi);
795 return nfsmount_child(mi);
799 * Process mount options and try a mount system call.
801 * Returns a valid mount command exit code.
803 static const char *nfs_background_opttbl[] = {
809 static int nfsmount_start(struct nfsmount_info *mi)
811 if (!nfs_validate_options(mi))
814 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
815 return nfsmount_bg(mi);
817 return nfsmount_fg(mi);
821 * nfsmount_string - Mount an NFS file system using C string options
822 * @spec: C string specifying remote share to mount ("hostname:path")
823 * @node: C string pathname of local mounted-on directory
824 * @type: C string that represents file system type ("nfs" or "nfs4")
825 * @flags: MS_ style mount flags
826 * @extra_opts: pointer to C string containing fs-specific mount options
827 * (input and output argument)
828 * @fake: flag indicating whether to carry out the whole operation
829 * @child: one if this is a mount daemon (bg)
831 int nfsmount_string(const char *spec, const char *node, const char *type,
832 int flags, char **extra_opts, int fake, int child)
834 struct nfsmount_info mi = {
838 .extra_opts = extra_opts,
843 int retval = EX_FAIL;
845 mi.options = po_split(*extra_opts);
847 retval = nfsmount_start(&mi);
848 po_destroy(mi.options);
850 nfs_error(_("%s: internal option parsing error"), progname);