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)
281 static int nfs_set_version(struct nfsmount_info *mi)
283 if (!nfs_nfs_version(mi->options, &mi->version))
286 if (strncmp(mi->type, "nfs4", 4) == 0)
289 char *option = po_get(mi->options, "proto");
290 if (option && strcmp(option, "rdma") == 0)
295 * If we still don't know, check for version-specific
298 if (mi->version == 0) {
299 if (po_contains(mi->options, "mounthost") ||
300 po_contains(mi->options, "mountaddr") ||
301 po_contains(mi->options, "mountvers") ||
302 po_contains(mi->options, "mountproto"))
307 * If enabled, see if the default version was
308 * set in the config file
310 nfs_default_version(mi);
316 * Set up mandatory non-version specific NFS mount options.
318 * Returns 1 if successful; otherwise zero.
320 static int nfs_validate_options(struct nfsmount_info *mi)
322 struct sockaddr *sap = (struct sockaddr *)&mi->address;
324 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
327 mi->salen = sizeof(mi->address);
328 if (!nfs_name_to_address(mi->hostname, sap, &mi->salen))
331 if (!nfs_set_version(mi))
334 if (!nfs_append_sloppy_option(mi->options))
337 if (!nfs_append_addr_option(sap, mi->salen, mi->options))
344 * Get NFS/mnt server addresses from mount options
346 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
347 * if all goes well; otherwise zero.
349 static int nfs_extract_server_addresses(struct mount_options *options,
350 struct sockaddr *nfs_saddr,
351 socklen_t *nfs_salen,
352 struct sockaddr *mnt_saddr,
353 socklen_t *mnt_salen)
357 option = po_get(options, "addr");
360 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
363 option = po_get(options, "mountaddr");
364 if (option == NULL) {
365 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
366 *mnt_salen = *nfs_salen;
367 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
373 static int nfs_construct_new_options(struct mount_options *options,
374 struct pmap *nfs_pmap,
375 struct pmap *mnt_pmap)
379 po_remove_all(options, "nfsprog");
380 po_remove_all(options, "mountprog");
382 po_remove_all(options, "v2");
383 po_remove_all(options, "v3");
384 po_remove_all(options, "vers");
385 po_remove_all(options, "nfsvers");
386 snprintf(new_option, sizeof(new_option) - 1,
387 "vers=%lu", nfs_pmap->pm_vers);
388 if (po_append(options, new_option) == PO_FAILED)
391 po_remove_all(options, "proto");
392 po_remove_all(options, "udp");
393 po_remove_all(options, "tcp");
394 switch (nfs_pmap->pm_prot) {
396 snprintf(new_option, sizeof(new_option) - 1,
398 if (po_append(options, new_option) == PO_FAILED)
402 snprintf(new_option, sizeof(new_option) - 1,
404 if (po_append(options, new_option) == PO_FAILED)
409 po_remove_all(options, "port");
410 if (nfs_pmap->pm_port != NFS_PORT) {
411 snprintf(new_option, sizeof(new_option) - 1,
412 "port=%lu", nfs_pmap->pm_port);
413 if (po_append(options, new_option) == PO_FAILED)
417 po_remove_all(options, "mountvers");
418 snprintf(new_option, sizeof(new_option) - 1,
419 "mountvers=%lu", mnt_pmap->pm_vers);
420 if (po_append(options, new_option) == PO_FAILED)
423 po_remove_all(options, "mountproto");
424 switch (mnt_pmap->pm_prot) {
426 snprintf(new_option, sizeof(new_option) - 1,
428 if (po_append(options, new_option) == PO_FAILED)
432 snprintf(new_option, sizeof(new_option) - 1,
434 if (po_append(options, new_option) == PO_FAILED)
439 po_remove_all(options, "mountport");
440 snprintf(new_option, sizeof(new_option) - 1,
441 "mountport=%lu", mnt_pmap->pm_port);
442 if (po_append(options, new_option) == PO_FAILED)
449 * Reconstruct the mount option string based on a portmapper probe
450 * of the server. Returns one if the server's portmapper returned
451 * something we can use, otherwise zero.
453 * To handle version and transport protocol fallback properly, we
454 * need to parse some of the mount options in order to set up a
455 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
456 * doesn't recognize are left alone.
458 * Returns TRUE if rewriting was successful; otherwise
459 * FALSE is returned if some failure occurred.
462 nfs_rewrite_pmap_mount_options(struct mount_options *options)
464 struct sockaddr_storage nfs_address;
465 struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
466 socklen_t nfs_salen = sizeof(nfs_address);
467 struct pmap nfs_pmap;
468 struct sockaddr_storage mnt_address;
469 struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
470 socklen_t mnt_salen = sizeof(mnt_address);
471 struct pmap mnt_pmap;
475 * Skip option negotiation for proto=rdma mounts.
477 option = po_get(options, "proto");
478 if (option && strcmp(option, "rdma") == 0)
482 * Extract just the options needed to contact server.
483 * Bail now if any of these have bad values.
485 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
486 mnt_saddr, &mnt_salen)) {
490 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
496 * The kernel NFS client doesn't support changing the RPC
497 * program number for these services, so force the value of
498 * these fields before probing the server's ports.
500 nfs_pmap.pm_prog = NFS_PROGRAM;
501 mnt_pmap.pm_prog = MOUNTPROG;
504 * If the server's rpcbind service isn't available, we can't
505 * negotiate. Bail now if we can't contact it.
507 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
508 nfs_saddr, nfs_salen, &nfs_pmap)) {
513 if (!nfs_construct_new_options(options, &nfs_pmap, &mnt_pmap)) {
524 * Do the mount(2) system call.
526 * Returns TRUE if successful, otherwise FALSE.
527 * "errno" is set to reflect the individual error.
529 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
531 char *options = NULL;
534 if (po_join(opts, &options) == PO_FAILED) {
540 printf(_("%s: trying text-based options '%s'\n"),
546 result = mount(mi->spec, mi->node, mi->type,
547 mi->flags & ~(MS_USER|MS_USERS), options);
548 if (verbose && result) {
550 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
557 * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
559 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
561 struct mount_options *options = po_dup(mi->options);
569 if (!nfs_fix_mounthost_option(options)) {
573 if (!mi->fake && !nfs_verify_lock_option(options)) {
579 * Options we negotiate below may be stale by the time this
580 * file system is unmounted. In order to force umount.nfs
581 * to renegotiate with the server, only write the user-
582 * specified options, and not negotiated options, to /etc/mtab.
584 if (po_join(options, mi->extra_opts) == PO_FAILED) {
589 if (!nfs_rewrite_pmap_mount_options(options))
592 result = nfs_sys_mount(mi, options);
600 * For "-t nfs -o vers=4" or "-t nfs4" mounts.
602 static int nfs_try_mount_v4(struct nfsmount_info *mi)
604 struct sockaddr *sap = (struct sockaddr *)&mi->address;
605 struct mount_options *options = po_dup(mi->options);
613 if (mi->version == 0) {
614 if (po_append(options, "vers=4") == PO_FAILED) {
620 if (!nfs_append_clientaddr_option(sap, mi->salen, options)) {
626 * Update option string to be recorded in /etc/mtab.
628 if (po_join(options, mi->extra_opts) == PO_FAILED) {
633 result = nfs_sys_mount(mi, options);
641 * This is a single pass through the fg/bg loop.
643 * Returns TRUE if successful, otherwise FALSE.
644 * "errno" is set to reflect the individual error.
646 static int nfs_try_mount(struct nfsmount_info *mi)
650 switch (mi->version) {
652 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
654 result = nfs_try_mount_v4(mi);
655 if (errno != EPROTONOSUPPORT) {
657 * To deal with legacy Linux servers that don't
658 * automatically export a pseudo root, retry
659 * ENOENT errors using version 3
667 result = nfs_try_mount_v3v2(mi);
670 result = nfs_try_mount_v4(mi);
680 * Distinguish between permanent and temporary errors.
682 * Basically, we retry if communication with the server has
683 * failed so far, but fail immediately if there is a local
684 * error (like a bad mount option).
686 * ESTALE is also a temporary error because some servers
687 * return ESTALE when a share is temporarily offline.
689 * Returns 1 if we should fail immediately, or 0 if we
692 static int nfs_is_permanent_error(int error)
698 return 0; /* temporary */
700 return 1; /* permanent */
705 * Handle "foreground" NFS mounts.
707 * Retry the mount request for as long as the 'retry=' option says.
709 * Returns a valid mount command exit code.
711 static int nfsmount_fg(struct nfsmount_info *mi)
713 unsigned int secs = 1;
716 timeout = nfs_parse_retry_option(mi->options,
717 NFS_DEF_FG_TIMEOUT_MINUTES);
719 printf(_("%s: timeout set for %s"),
720 progname, ctime(&timeout));
723 if (nfs_try_mount(mi))
726 if (nfs_is_permanent_error(errno))
729 if (time(NULL) > timeout) {
734 if (errno != ETIMEDOUT) {
743 mount_error(mi->spec, mi->node, errno);
748 * Handle "background" NFS mount [first try]
750 * Returns a valid mount command exit code.
752 * EX_BG should cause the caller to fork and invoke nfsmount_child.
754 static int nfsmount_parent(struct nfsmount_info *mi)
756 if (nfs_try_mount(mi))
759 if (nfs_is_permanent_error(errno)) {
760 mount_error(mi->spec, mi->node, errno);
764 sys_mount_errors(mi->hostname, errno, 1, 1);
769 * Handle "background" NFS mount [retry daemon]
771 * Returns a valid mount command exit code: EX_SUCCESS if successful,
772 * EX_FAIL if a failure occurred. There's nothing to catch the
773 * error return, though, so we use sys_mount_errors to log the
776 static int nfsmount_child(struct nfsmount_info *mi)
778 unsigned int secs = 1;
781 timeout = nfs_parse_retry_option(mi->options,
782 NFS_DEF_BG_TIMEOUT_MINUTES);
791 if (nfs_try_mount(mi))
794 if (nfs_is_permanent_error(errno))
797 if (time(NULL) > timeout)
800 sys_mount_errors(mi->hostname, errno, 1, 1);
803 sys_mount_errors(mi->hostname, errno, 1, 0);
808 * Handle "background" NFS mount
810 * Returns a valid mount command exit code.
812 static int nfsmount_bg(struct nfsmount_info *mi)
815 return nfsmount_parent(mi);
817 return nfsmount_child(mi);
821 * Process mount options and try a mount system call.
823 * Returns a valid mount command exit code.
825 static const char *nfs_background_opttbl[] = {
831 static int nfsmount_start(struct nfsmount_info *mi)
833 if (!nfs_validate_options(mi))
836 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
837 return nfsmount_bg(mi);
839 return nfsmount_fg(mi);
843 * nfsmount_string - Mount an NFS file system using C string options
844 * @spec: C string specifying remote share to mount ("hostname:path")
845 * @node: C string pathname of local mounted-on directory
846 * @type: C string that represents file system type ("nfs" or "nfs4")
847 * @flags: MS_ style mount flags
848 * @extra_opts: pointer to C string containing fs-specific mount options
849 * (input and output argument)
850 * @fake: flag indicating whether to carry out the whole operation
851 * @child: one if this is a mount daemon (bg)
853 int nfsmount_string(const char *spec, const char *node, const char *type,
854 int flags, char **extra_opts, int fake, int child)
856 struct nfsmount_info mi = {
860 .extra_opts = extra_opts,
865 int retval = EX_FAIL;
867 mi.options = po_split(*extra_opts);
869 retval = nfsmount_start(&mi);
870 po_destroy(mi.options);
872 nfs_error(_("%s: internal option parsing error"), progname);