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 * Obtain a retry timeout value based on the value of the "retry=" option.
99 * Returns a time_t timeout timestamp, in seconds.
101 static time_t nfs_parse_retry_option(struct mount_options *options,
102 unsigned int timeout_minutes)
106 switch (po_get_numeric(options, "retry", &tmp)) {
111 timeout_minutes = tmp;
116 nfs_error(_("%s: invalid retry timeout was specified; "
117 "using default timeout"), progname);
121 return time(NULL) + (time_t)(timeout_minutes * 60);
125 * Convert the passed-in sockaddr-style address to presentation
126 * format, then append an option of the form "keyword=address".
128 * Returns 1 if the option was appended successfully; otherwise zero.
130 static int nfs_append_generic_address_option(const struct sockaddr *sap,
131 const socklen_t salen,
133 struct mount_options *options)
135 char address[NI_MAXHOST];
136 char new_option[512];
139 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
142 len = snprintf(new_option, sizeof(new_option), "%s=%s",
144 if (len < 0 || (size_t)len >= sizeof(new_option))
147 if (po_append(options, new_option) != PO_SUCCEEDED)
153 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
158 * Append the 'addr=' option to the options string to pass a resolved
159 * server address to the kernel. After a successful mount, this address
160 * is also added to /etc/mtab for use when unmounting.
162 * If 'addr=' is already present, we strip it out. This prevents users
163 * from setting a bogus 'addr=' option themselves, and also allows bg
164 * retries to recompute the server's address, in case it has changed.
166 * Returns 1 if 'addr=' option appended successfully;
169 static int nfs_append_addr_option(const struct sockaddr *sap,
171 struct mount_options *options)
173 po_remove_all(options, "addr");
174 return nfs_append_generic_address_option(sap, salen, "addr", options);
178 * Called to discover our address and append an appropriate 'clientaddr='
179 * option to the options string.
181 * Returns 1 if 'clientaddr=' option created successfully or if
182 * 'clientaddr=' option is already present; otherwise zero.
184 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
186 struct mount_options *options)
188 struct sockaddr_storage dummy;
189 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
190 socklen_t my_len = sizeof(dummy);
192 if (po_contains(options, "clientaddr") == PO_FOUND)
195 nfs_callback_address(sap, salen, my_addr, &my_len);
197 return nfs_append_generic_address_option(my_addr, my_len,
198 "clientaddr", options);
202 * Resolve the 'mounthost=' hostname and append a new option using
203 * the resulting address.
205 static int nfs_fix_mounthost_option(struct mount_options *options)
207 struct sockaddr_storage dummy;
208 struct sockaddr *sap = (struct sockaddr *)&dummy;
209 socklen_t salen = sizeof(dummy);
212 mounthost = po_get(options, "mounthost");
216 if (!nfs_name_to_address(mounthost, sap, &salen)) {
217 nfs_error(_("%s: unable to determine mount server's address"),
222 return nfs_append_generic_address_option(sap, salen,
223 "mountaddr", options);
227 * Returns zero if the "lock" option is in effect, but statd
228 * can't be started. Otherwise, returns 1.
230 static const char *nfs_lock_opttbl[] = {
236 static int nfs_verify_lock_option(struct mount_options *options)
238 if (po_rightmost(options, nfs_lock_opttbl) == 0)
241 if (!start_statd()) {
242 nfs_error(_("%s: rpc.statd is not running but is "
243 "required for remote locking."), progname);
244 nfs_error(_("%s: Either use '-o nolock' to keep "
245 "locks local, or start statd."), progname);
252 static int nfs_append_sloppy_option(struct mount_options *options)
254 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
257 if (po_append(options, "sloppy") == PO_FAILED)
263 * Set up mandatory non-version specific NFS mount options.
265 * Returns 1 if successful; otherwise zero.
267 static int nfs_validate_options(struct nfsmount_info *mi)
269 struct sockaddr *sap = (struct sockaddr *)&mi->address;
271 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
274 mi->salen = sizeof(mi->address);
275 if (!nfs_name_to_address(mi->hostname, sap, &mi->salen))
278 if (!nfs_nfs_version(mi->options, &mi->version))
280 if (strncmp(mi->type, "nfs4", 4) == 0)
283 char *option = po_get(mi->options, "proto");
284 if (option && strcmp(option, "rdma") == 0)
288 * Use the default value set in the config file when
289 * the version has not been explicitly set.
291 if (mi->version == 0 && config_default_vers) {
292 if (config_default_vers < 4)
293 mi->version = config_default_vers;
296 if (!nfs_append_sloppy_option(mi->options))
299 if (!nfs_append_addr_option(sap, mi->salen, mi->options))
306 * Get NFS/mnt server addresses from mount options
308 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
309 * if all goes well; otherwise zero.
311 static int nfs_extract_server_addresses(struct mount_options *options,
312 struct sockaddr *nfs_saddr,
313 socklen_t *nfs_salen,
314 struct sockaddr *mnt_saddr,
315 socklen_t *mnt_salen)
319 option = po_get(options, "addr");
322 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
325 option = po_get(options, "mountaddr");
326 if (option == NULL) {
327 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
328 *mnt_salen = *nfs_salen;
329 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
335 static int nfs_construct_new_options(struct mount_options *options,
336 struct pmap *nfs_pmap,
337 struct pmap *mnt_pmap)
341 po_remove_all(options, "nfsprog");
342 po_remove_all(options, "mountprog");
344 po_remove_all(options, "v2");
345 po_remove_all(options, "v3");
346 po_remove_all(options, "vers");
347 po_remove_all(options, "nfsvers");
348 snprintf(new_option, sizeof(new_option) - 1,
349 "vers=%lu", nfs_pmap->pm_vers);
350 if (po_append(options, new_option) == PO_FAILED)
353 po_remove_all(options, "proto");
354 po_remove_all(options, "udp");
355 po_remove_all(options, "tcp");
356 switch (nfs_pmap->pm_prot) {
358 snprintf(new_option, sizeof(new_option) - 1,
360 if (po_append(options, new_option) == PO_FAILED)
364 snprintf(new_option, sizeof(new_option) - 1,
366 if (po_append(options, new_option) == PO_FAILED)
371 po_remove_all(options, "port");
372 if (nfs_pmap->pm_port != NFS_PORT) {
373 snprintf(new_option, sizeof(new_option) - 1,
374 "port=%lu", nfs_pmap->pm_port);
375 if (po_append(options, new_option) == PO_FAILED)
379 po_remove_all(options, "mountvers");
380 snprintf(new_option, sizeof(new_option) - 1,
381 "mountvers=%lu", mnt_pmap->pm_vers);
382 if (po_append(options, new_option) == PO_FAILED)
385 po_remove_all(options, "mountproto");
386 switch (mnt_pmap->pm_prot) {
388 snprintf(new_option, sizeof(new_option) - 1,
390 if (po_append(options, new_option) == PO_FAILED)
394 snprintf(new_option, sizeof(new_option) - 1,
396 if (po_append(options, new_option) == PO_FAILED)
401 po_remove_all(options, "mountport");
402 snprintf(new_option, sizeof(new_option) - 1,
403 "mountport=%lu", mnt_pmap->pm_port);
404 if (po_append(options, new_option) == PO_FAILED)
411 * Reconstruct the mount option string based on a portmapper probe
412 * of the server. Returns one if the server's portmapper returned
413 * something we can use, otherwise zero.
415 * To handle version and transport protocol fallback properly, we
416 * need to parse some of the mount options in order to set up a
417 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
418 * doesn't recognize are left alone.
420 * Returns TRUE if rewriting was successful; otherwise
421 * FALSE is returned if some failure occurred.
424 nfs_rewrite_pmap_mount_options(struct mount_options *options)
426 struct sockaddr_storage nfs_address;
427 struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
428 socklen_t nfs_salen = sizeof(nfs_address);
429 struct pmap nfs_pmap;
430 struct sockaddr_storage mnt_address;
431 struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
432 socklen_t mnt_salen = sizeof(mnt_address);
433 struct pmap mnt_pmap;
437 * Skip option negotiation for proto=rdma mounts.
439 option = po_get(options, "proto");
440 if (option && strcmp(option, "rdma") == 0)
444 * Extract just the options needed to contact server.
445 * Bail now if any of these have bad values.
447 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
448 mnt_saddr, &mnt_salen)) {
452 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
458 * The kernel NFS client doesn't support changing the RPC
459 * program number for these services, so force the value of
460 * these fields before probing the server's ports.
462 nfs_pmap.pm_prog = NFS_PROGRAM;
463 mnt_pmap.pm_prog = MOUNTPROG;
466 * If the server's rpcbind service isn't available, we can't
467 * negotiate. Bail now if we can't contact it.
469 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
470 nfs_saddr, nfs_salen, &nfs_pmap)) {
475 if (!nfs_construct_new_options(options, &nfs_pmap, &mnt_pmap)) {
486 * Do the mount(2) system call.
488 * Returns TRUE if successful, otherwise FALSE.
489 * "errno" is set to reflect the individual error.
491 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
493 char *options = NULL;
496 if (po_join(opts, &options) == PO_FAILED) {
502 printf(_("%s: trying text-based options '%s'\n"),
508 result = mount(mi->spec, mi->node, mi->type,
509 mi->flags & ~(MS_USER|MS_USERS), options);
510 if (verbose && result) {
512 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
519 * For "-t nfs vers=2" or "-t nfs vers=3" mounts.
521 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
523 struct mount_options *options = po_dup(mi->options);
531 if (!nfs_fix_mounthost_option(options)) {
535 if (!mi->fake && !nfs_verify_lock_option(options)) {
541 * Options we negotiate below may be stale by the time this
542 * file system is unmounted. In order to force umount.nfs
543 * to renegotiate with the server, only write the user-
544 * specified options, and not negotiated options, to /etc/mtab.
546 if (po_join(options, mi->extra_opts) == PO_FAILED) {
551 if (!nfs_rewrite_pmap_mount_options(options))
554 result = nfs_sys_mount(mi, options);
562 * For "-t nfs -o vers=4" or "-t nfs4" mounts.
564 static int nfs_try_mount_v4(struct nfsmount_info *mi)
566 struct sockaddr *sap = (struct sockaddr *)&mi->address;
567 struct mount_options *options = po_dup(mi->options);
575 if (mi->version == 0) {
576 if (po_append(options, "vers=4") == PO_FAILED) {
582 if (!nfs_append_clientaddr_option(sap, mi->salen, options)) {
587 * Update option string to be recorded in /etc/mtab.
589 if (po_join(options, mi->extra_opts) == PO_FAILED) {
594 result = nfs_sys_mount(mi, options);
602 * This is a single pass through the fg/bg loop.
604 * Returns TRUE if successful, otherwise FALSE.
605 * "errno" is set to reflect the individual error.
607 static int nfs_try_mount(struct nfsmount_info *mi)
611 switch (mi->version) {
613 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
615 result = nfs_try_mount_v4(mi);
616 if (errno != EPROTONOSUPPORT)
621 result = nfs_try_mount_v3v2(mi);
624 result = nfs_try_mount_v4(mi);
634 * Distinguish between permanent and temporary errors.
636 * Basically, we retry if communication with the server has
637 * failed so far, but fail immediately if there is a local
638 * error (like a bad mount option).
640 * ESTALE is also a temporary error because some servers
641 * return ESTALE when a share is temporarily offline.
643 * Returns 1 if we should fail immediately, or 0 if we
646 static int nfs_is_permanent_error(int error)
652 return 0; /* temporary */
654 return 1; /* permanent */
659 * Handle "foreground" NFS mounts.
661 * Retry the mount request for as long as the 'retry=' option says.
663 * Returns a valid mount command exit code.
665 static int nfsmount_fg(struct nfsmount_info *mi)
667 unsigned int secs = 1;
670 timeout = nfs_parse_retry_option(mi->options,
671 NFS_DEF_FG_TIMEOUT_MINUTES);
673 printf(_("%s: timeout set for %s"),
674 progname, ctime(&timeout));
677 if (nfs_try_mount(mi))
680 if (nfs_is_permanent_error(errno))
683 if (time(NULL) > timeout) {
688 if (errno != ETIMEDOUT) {
697 mount_error(mi->spec, mi->node, errno);
702 * Handle "background" NFS mount [first try]
704 * Returns a valid mount command exit code.
706 * EX_BG should cause the caller to fork and invoke nfsmount_child.
708 static int nfsmount_parent(struct nfsmount_info *mi)
710 if (nfs_try_mount(mi))
713 if (nfs_is_permanent_error(errno)) {
714 mount_error(mi->spec, mi->node, errno);
718 sys_mount_errors(mi->hostname, errno, 1, 1);
723 * Handle "background" NFS mount [retry daemon]
725 * Returns a valid mount command exit code: EX_SUCCESS if successful,
726 * EX_FAIL if a failure occurred. There's nothing to catch the
727 * error return, though, so we use sys_mount_errors to log the
730 static int nfsmount_child(struct nfsmount_info *mi)
732 unsigned int secs = 1;
735 timeout = nfs_parse_retry_option(mi->options,
736 NFS_DEF_BG_TIMEOUT_MINUTES);
745 if (nfs_try_mount(mi))
748 if (nfs_is_permanent_error(errno))
751 if (time(NULL) > timeout)
754 sys_mount_errors(mi->hostname, errno, 1, 1);
757 sys_mount_errors(mi->hostname, errno, 1, 0);
762 * Handle "background" NFS mount
764 * Returns a valid mount command exit code.
766 static int nfsmount_bg(struct nfsmount_info *mi)
769 return nfsmount_parent(mi);
771 return nfsmount_child(mi);
775 * Process mount options and try a mount system call.
777 * Returns a valid mount command exit code.
779 static const char *nfs_background_opttbl[] = {
785 static int nfsmount_start(struct nfsmount_info *mi)
787 if (!nfs_validate_options(mi))
790 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
791 return nfsmount_bg(mi);
793 return nfsmount_fg(mi);
797 * nfsmount_string - Mount an NFS file system using C string options
798 * @spec: C string specifying remote share to mount ("hostname:path")
799 * @node: C string pathname of local mounted-on directory
800 * @type: C string that represents file system type ("nfs" or "nfs4")
801 * @flags: MS_ style mount flags
802 * @extra_opts: pointer to C string containing fs-specific mount options
803 * (input and output argument)
804 * @fake: flag indicating whether to carry out the whole operation
805 * @child: one if this is a mount daemon (bg)
807 int nfsmount_string(const char *spec, const char *node, const char *type,
808 int flags, char **extra_opts, int fake, int child)
810 struct nfsmount_info mi = {
814 .extra_opts = extra_opts,
819 int retval = EX_FAIL;
821 mi.options = po_split(*extra_opts);
823 retval = nfsmount_start(&mi);
824 po_destroy(mi.options);
826 nfs_error(_("%s: internal option parsing error"), progname);