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"
50 #define NFS_PROGRAM (100003)
54 #define NFS_PORT (2049)
57 #ifndef NFS_MAXHOSTNAME
58 #define NFS_MAXHOSTNAME (255)
61 #ifndef NFS_MAXPATHNAME
62 #define NFS_MAXPATHNAME (1024)
65 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
66 #define NFS_DEF_FG_TIMEOUT_MINUTES (2u)
69 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
70 #define NFS_DEF_BG_TIMEOUT_MINUTES (10000u)
73 extern int nfs_mount_data_version;
74 extern char *progname;
78 struct nfsmount_info {
79 const char *spec, /* server:/path */
80 *node, /* mounted-on dir */
81 *type; /* "nfs" or "nfs4" */
82 char *hostname; /* server's hostname */
83 struct sockaddr_storage address; /* server's address */
84 socklen_t salen; /* size of server's address */
86 struct mount_options *options; /* parsed mount options */
87 char **extra_opts; /* string for /etc/mtab */
89 unsigned long version; /* NFS version */
90 int flags, /* MS_ flags */
91 fake, /* actually do the mount? */
92 child; /* forked bg child? */
96 * Obtain a retry timeout value based on the value of the "retry=" option.
98 * Returns a time_t timeout timestamp, in seconds.
100 static time_t nfs_parse_retry_option(struct mount_options *options,
101 unsigned int timeout_minutes)
105 switch (po_get_numeric(options, "retry", &tmp)) {
110 timeout_minutes = tmp;
115 nfs_error(_("%s: invalid retry timeout was specified; "
116 "using default timeout"), progname);
120 return time(NULL) + (time_t)(timeout_minutes * 60);
124 * Convert the passed-in sockaddr-style address to presentation
125 * format, then append an option of the form "keyword=address".
127 * Returns 1 if the option was appended successfully; otherwise zero.
129 static int nfs_append_generic_address_option(const struct sockaddr *sap,
130 const socklen_t salen,
132 struct mount_options *options)
134 char address[NI_MAXHOST];
135 char new_option[512];
138 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
141 len = snprintf(new_option, sizeof(new_option), "%s=%s",
143 if (len < 0 || (size_t)len >= sizeof(new_option))
146 if (po_append(options, new_option) != PO_SUCCEEDED)
152 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
157 * Append the 'addr=' option to the options string to pass a resolved
158 * server address to the kernel. After a successful mount, this address
159 * is also added to /etc/mtab for use when unmounting.
161 * If 'addr=' is already present, we strip it out. This prevents users
162 * from setting a bogus 'addr=' option themselves, and also allows bg
163 * retries to recompute the server's address, in case it has changed.
165 * Returns 1 if 'addr=' option appended successfully;
168 static int nfs_append_addr_option(const struct sockaddr *sap,
170 struct mount_options *options)
172 po_remove_all(options, "addr");
173 return nfs_append_generic_address_option(sap, salen, "addr", options);
177 * Called to discover our address and append an appropriate 'clientaddr='
178 * option to the options string.
180 * Returns 1 if 'clientaddr=' option created successfully or if
181 * 'clientaddr=' option is already present; otherwise zero.
183 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
185 struct mount_options *options)
187 struct sockaddr_storage dummy;
188 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
189 socklen_t my_len = sizeof(dummy);
191 if (po_contains(options, "clientaddr") == PO_FOUND)
194 nfs_callback_address(sap, salen, my_addr, &my_len);
196 return nfs_append_generic_address_option(my_addr, my_len,
197 "clientaddr", options);
201 * Resolve the 'mounthost=' hostname and append a new option using
202 * the resulting address.
204 static int nfs_fix_mounthost_option(struct mount_options *options)
206 struct sockaddr_storage dummy;
207 struct sockaddr *sap = (struct sockaddr *)&dummy;
208 socklen_t salen = sizeof(dummy);
211 mounthost = po_get(options, "mounthost");
215 if (!nfs_name_to_address(mounthost, sap, &salen)) {
216 nfs_error(_("%s: unable to determine mount server's address"),
221 return nfs_append_generic_address_option(sap, salen,
222 "mountaddr", options);
226 * Returns zero if the "lock" option is in effect, but statd
227 * can't be started. Otherwise, returns 1.
229 static const char *nfs_lock_opttbl[] = {
235 static int nfs_verify_lock_option(struct mount_options *options)
237 if (po_rightmost(options, nfs_lock_opttbl) == 0)
240 if (!start_statd()) {
241 nfs_error(_("%s: rpc.statd is not running but is "
242 "required for remote locking."), progname);
243 nfs_error(_("%s: Either use '-o nolock' to keep "
244 "locks local, or start statd."), progname);
251 static int nfs_append_sloppy_option(struct mount_options *options)
253 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
256 if (po_append(options, "sloppy") == PO_FAILED)
262 * Set up mandatory non-version specific NFS mount options.
264 * Returns 1 if successful; otherwise zero.
266 static int nfs_validate_options(struct nfsmount_info *mi)
268 struct sockaddr *sap = (struct sockaddr *)&mi->address;
270 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
273 mi->salen = sizeof(mi->address);
274 if (!nfs_name_to_address(mi->hostname, sap, &mi->salen))
277 if (!nfs_nfs_version(mi->options, &mi->version))
279 if (strncmp(mi->type, "nfs4", 4) == 0)
282 char *option = po_get(mi->options, "proto");
283 if (option && strcmp(option, "rdma") == 0)
287 if (!nfs_append_sloppy_option(mi->options))
290 if (!nfs_append_addr_option(sap, mi->salen, mi->options))
297 * Get NFS/mnt server addresses from mount options
299 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
300 * if all goes well; otherwise zero.
302 static int nfs_extract_server_addresses(struct mount_options *options,
303 struct sockaddr *nfs_saddr,
304 socklen_t *nfs_salen,
305 struct sockaddr *mnt_saddr,
306 socklen_t *mnt_salen)
310 option = po_get(options, "addr");
313 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
316 option = po_get(options, "mountaddr");
317 if (option == NULL) {
318 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
319 *mnt_salen = *nfs_salen;
320 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
326 static int nfs_construct_new_options(struct mount_options *options,
327 struct pmap *nfs_pmap,
328 struct pmap *mnt_pmap)
332 po_remove_all(options, "nfsprog");
333 po_remove_all(options, "mountprog");
335 po_remove_all(options, "v2");
336 po_remove_all(options, "v3");
337 po_remove_all(options, "vers");
338 po_remove_all(options, "nfsvers");
339 snprintf(new_option, sizeof(new_option) - 1,
340 "vers=%lu", nfs_pmap->pm_vers);
341 if (po_append(options, new_option) == PO_FAILED)
344 po_remove_all(options, "proto");
345 po_remove_all(options, "udp");
346 po_remove_all(options, "tcp");
347 switch (nfs_pmap->pm_prot) {
349 snprintf(new_option, sizeof(new_option) - 1,
351 if (po_append(options, new_option) == PO_FAILED)
355 snprintf(new_option, sizeof(new_option) - 1,
357 if (po_append(options, new_option) == PO_FAILED)
362 po_remove_all(options, "port");
363 if (nfs_pmap->pm_port != NFS_PORT) {
364 snprintf(new_option, sizeof(new_option) - 1,
365 "port=%lu", nfs_pmap->pm_port);
366 if (po_append(options, new_option) == PO_FAILED)
370 po_remove_all(options, "mountvers");
371 snprintf(new_option, sizeof(new_option) - 1,
372 "mountvers=%lu", mnt_pmap->pm_vers);
373 if (po_append(options, new_option) == PO_FAILED)
376 po_remove_all(options, "mountproto");
377 switch (mnt_pmap->pm_prot) {
379 snprintf(new_option, sizeof(new_option) - 1,
381 if (po_append(options, new_option) == PO_FAILED)
385 snprintf(new_option, sizeof(new_option) - 1,
387 if (po_append(options, new_option) == PO_FAILED)
392 po_remove_all(options, "mountport");
393 snprintf(new_option, sizeof(new_option) - 1,
394 "mountport=%lu", mnt_pmap->pm_port);
395 if (po_append(options, new_option) == PO_FAILED)
402 * Reconstruct the mount option string based on a portmapper probe
403 * of the server. Returns one if the server's portmapper returned
404 * something we can use, otherwise zero.
406 * To handle version and transport protocol fallback properly, we
407 * need to parse some of the mount options in order to set up a
408 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
409 * doesn't recognize are left alone.
411 * Returns TRUE if rewriting was successful; otherwise
412 * FALSE is returned if some failure occurred.
415 nfs_rewrite_pmap_mount_options(struct mount_options *options)
417 struct sockaddr_storage nfs_address;
418 struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
419 socklen_t nfs_salen = sizeof(nfs_address);
420 struct pmap nfs_pmap;
421 struct sockaddr_storage mnt_address;
422 struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
423 socklen_t mnt_salen = sizeof(mnt_address);
424 struct pmap mnt_pmap;
428 * Skip option negotiation for proto=rdma mounts.
430 option = po_get(options, "proto");
431 if (option && strcmp(option, "rdma") == 0)
435 * Extract just the options needed to contact server.
436 * Bail now if any of these have bad values.
438 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
439 mnt_saddr, &mnt_salen)) {
443 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
449 * The kernel NFS client doesn't support changing the RPC
450 * program number for these services, so force the value of
451 * these fields before probing the server's ports.
453 nfs_pmap.pm_prog = NFS_PROGRAM;
454 mnt_pmap.pm_prog = MOUNTPROG;
457 * If the server's rpcbind service isn't available, we can't
458 * negotiate. Bail now if we can't contact it.
460 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
461 nfs_saddr, nfs_salen, &nfs_pmap)) {
466 if (!nfs_construct_new_options(options, &nfs_pmap, &mnt_pmap)) {
477 * Do the mount(2) system call.
479 * Returns TRUE if successful, otherwise FALSE.
480 * "errno" is set to reflect the individual error.
482 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
484 char *options = NULL;
487 if (po_join(opts, &options) == PO_FAILED) {
493 printf(_("%s: trying text-based options '%s'\n"),
499 result = mount(mi->spec, mi->node, mi->type,
500 mi->flags & ~(MS_USER|MS_USERS), options);
501 if (verbose && result) {
503 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
510 * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
512 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
514 struct mount_options *options = po_dup(mi->options);
522 if (!nfs_fix_mounthost_option(options)) {
526 if (!mi->fake && !nfs_verify_lock_option(options)) {
532 * Options we negotiate below may be stale by the time this
533 * file system is unmounted. In order to force umount.nfs
534 * to renegotiate with the server, only write the user-
535 * specified options, and not negotiated options, to /etc/mtab.
537 if (po_join(options, mi->extra_opts) == PO_FAILED) {
542 if (!nfs_rewrite_pmap_mount_options(options))
545 result = nfs_sys_mount(mi, options);
553 * For "-t nfs -o vers=4" or "-t nfs4" mounts.
555 static int nfs_try_mount_v4(struct nfsmount_info *mi)
557 struct sockaddr *sap = (struct sockaddr *)&mi->address;
558 struct mount_options *options = po_dup(mi->options);
566 if (mi->version == 0) {
567 if (po_append(options, "vers=4") == PO_FAILED) {
573 if (!nfs_append_clientaddr_option(sap, mi->salen, options)) {
578 * Update option string to be recorded in /etc/mtab.
580 if (po_join(options, mi->extra_opts) == PO_FAILED) {
585 result = nfs_sys_mount(mi, options);
593 * This is a single pass through the fg/bg loop.
595 * Returns TRUE if successful, otherwise FALSE.
596 * "errno" is set to reflect the individual error.
598 static int nfs_try_mount(struct nfsmount_info *mi)
602 switch (mi->version) {
604 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
606 result = nfs_try_mount_v4(mi);
607 if (errno != EPROTONOSUPPORT)
612 result = nfs_try_mount_v3v2(mi);
615 result = nfs_try_mount_v4(mi);
625 * Distinguish between permanent and temporary errors.
627 * Basically, we retry if communication with the server has
628 * failed so far, but fail immediately if there is a local
629 * error (like a bad mount option).
631 * ESTALE is also a temporary error because some servers
632 * return ESTALE when a share is temporarily offline.
634 * Returns 1 if we should fail immediately, or 0 if we
637 static int nfs_is_permanent_error(int error)
643 return 0; /* temporary */
645 return 1; /* permanent */
650 * Handle "foreground" NFS mounts.
652 * Retry the mount request for as long as the 'retry=' option says.
654 * Returns a valid mount command exit code.
656 static int nfsmount_fg(struct nfsmount_info *mi)
658 unsigned int secs = 1;
661 timeout = nfs_parse_retry_option(mi->options,
662 NFS_DEF_FG_TIMEOUT_MINUTES);
664 printf(_("%s: timeout set for %s"),
665 progname, ctime(&timeout));
668 if (nfs_try_mount(mi))
671 if (nfs_is_permanent_error(errno))
674 if (time(NULL) > timeout) {
679 if (errno != ETIMEDOUT) {
688 mount_error(mi->spec, mi->node, errno);
693 * Handle "background" NFS mount [first try]
695 * Returns a valid mount command exit code.
697 * EX_BG should cause the caller to fork and invoke nfsmount_child.
699 static int nfsmount_parent(struct nfsmount_info *mi)
701 if (nfs_try_mount(mi))
704 if (nfs_is_permanent_error(errno)) {
705 mount_error(mi->spec, mi->node, errno);
709 sys_mount_errors(mi->hostname, errno, 1, 1);
714 * Handle "background" NFS mount [retry daemon]
716 * Returns a valid mount command exit code: EX_SUCCESS if successful,
717 * EX_FAIL if a failure occurred. There's nothing to catch the
718 * error return, though, so we use sys_mount_errors to log the
721 static int nfsmount_child(struct nfsmount_info *mi)
723 unsigned int secs = 1;
726 timeout = nfs_parse_retry_option(mi->options,
727 NFS_DEF_BG_TIMEOUT_MINUTES);
736 if (nfs_try_mount(mi))
739 if (nfs_is_permanent_error(errno))
742 if (time(NULL) > timeout)
745 sys_mount_errors(mi->hostname, errno, 1, 1);
748 sys_mount_errors(mi->hostname, errno, 1, 0);
753 * Handle "background" NFS mount
755 * Returns a valid mount command exit code.
757 static int nfsmount_bg(struct nfsmount_info *mi)
760 return nfsmount_parent(mi);
762 return nfsmount_child(mi);
766 * Process mount options and try a mount system call.
768 * Returns a valid mount command exit code.
770 static const char *nfs_background_opttbl[] = {
776 static int nfsmount_start(struct nfsmount_info *mi)
778 if (!nfs_validate_options(mi))
781 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
782 return nfsmount_bg(mi);
784 return nfsmount_fg(mi);
788 * nfsmount_string - Mount an NFS file system using C string options
789 * @spec: C string specifying remote share to mount ("hostname:path")
790 * @node: C string pathname of local mounted-on directory
791 * @type: C string that represents file system type ("nfs" or "nfs4")
792 * @flags: MS_ style mount flags
793 * @extra_opts: pointer to C string containing fs-specific mount options
794 * (input and output argument)
795 * @fake: flag indicating whether to carry out the whole operation
796 * @child: one if this is a mount daemon (bg)
798 int nfsmount_string(const char *spec, const char *node, const char *type,
799 int flags, char **extra_opts, int fake, int child)
801 struct nfsmount_info mi = {
805 .extra_opts = extra_opts,
810 int retval = EX_FAIL;
812 mi.options = po_split(*extra_opts);
814 retval = nfsmount_start(&mi);
815 po_destroy(mi.options);
817 nfs_error(_("%s: internal option parsing error"), progname);