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 */
84 struct mount_options *options; /* parsed mount options */
85 char **extra_opts; /* string for /etc/mtab */
87 int flags, /* MS_ flags */
88 fake, /* actually do the mount? */
89 child; /* forked bg child? */
91 sa_family_t family; /* supported address family */
95 * Obtain a retry timeout value based on the value of the "retry=" option.
97 * Returns a time_t timeout timestamp, in seconds.
99 static time_t nfs_parse_retry_option(struct mount_options *options,
100 unsigned int timeout_minutes)
102 char *retry_option, *endptr;
104 retry_option = po_get(options, "retry");
109 tmp = strtol(retry_option, &endptr, 10);
110 if (errno == 0 && endptr != retry_option && tmp >= 0)
111 timeout_minutes = tmp;
113 nfs_error(_("%s: invalid retry timeout was specified; "
114 "using default timeout"), progname);
117 return time(NULL) + (time_t)(timeout_minutes * 60);
121 * Convert the passed-in sockaddr-style address to presentation
122 * format, then append an option of the form "keyword=address".
124 * Returns 1 if the option was appended successfully; otherwise zero.
126 static int nfs_append_generic_address_option(const struct sockaddr *sap,
127 const socklen_t salen,
129 struct mount_options *options)
131 char address[NI_MAXHOST];
132 char new_option[512];
134 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
137 if (snprintf(new_option, sizeof(new_option), "%s=%s",
138 keyword, address) >= sizeof(new_option))
141 if (po_append(options, new_option) != PO_SUCCEEDED)
147 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
152 * Append the 'addr=' option to the options string to pass a resolved
153 * server address to the kernel. After a successful mount, this address
154 * is also added to /etc/mtab for use when unmounting.
156 * If 'addr=' is already present, we strip it out. This prevents users
157 * from setting a bogus 'addr=' option themselves, and also allows bg
158 * retries to recompute the server's address, in case it has changed.
160 * Returns 1 if 'addr=' option appended successfully;
163 static int nfs_append_addr_option(const struct sockaddr *sap,
165 struct mount_options *options)
167 po_remove_all(options, "addr");
168 return nfs_append_generic_address_option(sap, salen, "addr", options);
172 * Called to discover our address and append an appropriate 'clientaddr='
173 * option to the options string.
175 * Returns 1 if 'clientaddr=' option created successfully or if
176 * 'clientaddr=' option is already present; otherwise zero.
178 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
180 struct mount_options *options)
182 struct sockaddr_storage dummy;
183 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
184 socklen_t my_len = sizeof(dummy);
186 if (po_contains(options, "clientaddr") == PO_FOUND)
189 nfs_callback_address(sap, salen, my_addr, &my_len);
191 return nfs_append_generic_address_option(my_addr, my_len,
192 "clientaddr", options);
196 * Resolve the 'mounthost=' hostname and append a new option using
197 * the resulting address.
199 static int nfs_fix_mounthost_option(const sa_family_t family,
200 struct mount_options *options)
202 struct sockaddr_storage dummy;
203 struct sockaddr *sap = (struct sockaddr *)&dummy;
204 socklen_t salen = sizeof(dummy);
207 mounthost = po_get(options, "mounthost");
211 if (!nfs_name_to_address(mounthost, family, sap, &salen)) {
212 nfs_error(_("%s: unable to determine mount server's address"),
217 return nfs_append_generic_address_option(sap, salen,
218 "mountaddr", options);
222 * Returns zero if the "lock" option is in effect, but statd
223 * can't be started. Otherwise, returns 1.
225 static int nfs_verify_lock_option(struct mount_options *options)
227 if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
230 if (!start_statd()) {
231 nfs_error(_("%s: rpc.statd is not running but is "
232 "required for remote locking."), progname);
233 nfs_error(_("%s: Either use '-o nolock' to keep "
234 "locks local, or start statd."), progname);
241 static int nfs_append_sloppy_option(struct mount_options *options)
243 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
246 if (po_append(options, "sloppy") == PO_FAILED)
252 * Set up mandatory NFS mount options.
254 * Returns 1 if successful; otherwise zero.
256 static int nfs_validate_options(struct nfsmount_info *mi)
258 struct sockaddr_storage dummy;
259 struct sockaddr *sap = (struct sockaddr *)&dummy;
260 socklen_t salen = sizeof(dummy);
262 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
265 if (!nfs_name_to_address(mi->hostname, mi->family, sap, &salen))
268 if (strncmp(mi->type, "nfs4", 4) == 0) {
269 if (!nfs_append_clientaddr_option(sap, salen, mi->options))
272 if (!nfs_fix_mounthost_option(mi->family, mi->options))
274 if (!mi->fake && !nfs_verify_lock_option(mi->options))
278 if (!nfs_append_sloppy_option(mi->options))
281 return nfs_append_addr_option(sap, salen, mi->options);
285 * Distinguish between permanent and temporary errors.
287 * Returns 0 if the passed-in error is temporary, thus the
288 * mount system call should be retried; returns one if the
289 * passed-in error is permanent, thus the mount system call
290 * should not be retried.
292 static int nfs_is_permanent_error(int error)
298 return 0; /* temporary */
300 return 1; /* permanent */
305 * Reconstruct the mount option string based on a portmapper probe
306 * of the server. Returns one if the server's portmapper returned
307 * something we can use, otherwise zero.
309 * To handle version and transport protocol fallback properly, we
310 * need to parse some of the mount options in order to set up a
311 * portmap probe. Mount options that nfs_rewrite_mount_options()
312 * doesn't recognize are left alone.
314 * Returns a new group of mount options if successful; otherwise
315 * NULL is returned if some failure occurred.
317 static struct mount_options *nfs_rewrite_mount_options(char *str)
319 struct mount_options *options;
320 char *option, new_option[64];
321 clnt_addr_t mnt_server = { };
322 clnt_addr_t nfs_server = { };
325 options = po_split(str);
332 option = po_get(options, "addr");
334 nfs_server.saddr.sin_family = AF_INET;
335 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
340 option = po_get(options, "mountaddr");
342 mnt_server.saddr.sin_family = AF_INET;
343 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
346 memcpy(&mnt_server.saddr, &nfs_server.saddr,
347 sizeof(mnt_server.saddr));
349 option = po_get(options, "mountport");
351 mnt_server.pmap.pm_port = atoi(option);
352 mnt_server.pmap.pm_prog = MOUNTPROG;
353 option = po_get(options, "mountvers");
355 mnt_server.pmap.pm_vers = atoi(option);
356 option = po_get(options, "mountproto");
358 if (strcmp(option, "tcp") == 0) {
359 mnt_server.pmap.pm_prot = IPPROTO_TCP;
360 po_remove_all(options, "mountproto");
362 if (strcmp(option, "udp") == 0) {
363 mnt_server.pmap.pm_prot = IPPROTO_UDP;
364 po_remove_all(options, "mountproto");
368 option = po_get(options, "port");
370 nfs_server.pmap.pm_port = atoi(option);
371 po_remove_all(options, "port");
373 nfs_server.pmap.pm_prog = NFS_PROGRAM;
375 option = po_get(options, "nfsvers");
377 nfs_server.pmap.pm_vers = atoi(option);
378 po_remove_all(options, "nfsvers");
380 option = po_get(options, "vers");
382 nfs_server.pmap.pm_vers = atoi(option);
383 po_remove_all(options, "vers");
385 option = po_get(options, "proto");
387 if (strcmp(option, "tcp") == 0) {
388 nfs_server.pmap.pm_prot = IPPROTO_TCP;
389 po_remove_all(options, "proto");
391 if (strcmp(option, "udp") == 0) {
392 nfs_server.pmap.pm_prot = IPPROTO_UDP;
393 po_remove_all(options, "proto");
396 p = po_rightmost(options, "tcp", "udp");
398 case PO_KEY2_RIGHTMOST:
399 nfs_server.pmap.pm_prot = IPPROTO_UDP;
401 case PO_KEY1_RIGHTMOST:
402 nfs_server.pmap.pm_prot = IPPROTO_TCP;
405 po_remove_all(options, "tcp");
406 po_remove_all(options, "udp");
408 if (!probe_bothports(&mnt_server, &nfs_server)) {
413 snprintf(new_option, sizeof(new_option) - 1,
414 "nfsvers=%lu", nfs_server.pmap.pm_vers);
415 if (po_append(options, new_option) == PO_FAILED)
418 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
419 snprintf(new_option, sizeof(new_option) - 1,
422 snprintf(new_option, sizeof(new_option) - 1,
424 if (po_append(options, new_option) == PO_FAILED)
427 if (nfs_server.pmap.pm_port != NFS_PORT) {
428 snprintf(new_option, sizeof(new_option) - 1,
429 "port=%lu", nfs_server.pmap.pm_port);
430 if (po_append(options, new_option) == PO_FAILED)
435 if (mnt_server.pmap.pm_prot == IPPROTO_TCP)
436 snprintf(new_option, sizeof(new_option) - 1,
439 snprintf(new_option, sizeof(new_option) - 1,
441 if (po_append(options, new_option) == PO_FAILED)
444 snprintf(new_option, sizeof(new_option) - 1,
445 "mountport=%lu", mnt_server.pmap.pm_port);
446 if (po_append(options, new_option) == PO_FAILED)
458 * Do the mount(2) system call.
460 * Returns 1 if successful, otherwise zero.
461 * "errno" is set to reflect the individual error.
463 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
468 result = mount(mi->spec, mi->node, type,
469 mi->flags & ~(MS_USER|MS_USERS), options);
470 if (verbose && result) {
472 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
479 * Retry an NFS mount that failed because the requested service isn't
480 * available on the server.
482 * Returns 1 if successful. Otherwise, returns zero.
483 * "errno" is set to reflect the individual error.
485 * Side effect: If the retry is successful, both 'options' and
486 * 'extra_opts' are updated to reflect the mount options that worked.
487 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
489 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
491 struct mount_options *retry_options;
492 char *retry_str = NULL;
493 char **extra_opts = mi->extra_opts;
495 retry_options = nfs_rewrite_mount_options(*extra_opts);
499 if (po_join(retry_options, &retry_str) == PO_FAILED) {
500 po_destroy(retry_options);
506 printf(_("%s: text-based options (retry): '%s'\n"),
507 progname, retry_str);
509 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
510 po_destroy(retry_options);
516 *extra_opts = retry_str;
517 po_replace(mi->options, retry_options);
522 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
523 * claims the requested service isn't supported on the server, probe
524 * the server to see what's supported, rewrite the mount options,
525 * and retry the request.
527 * Returns 1 if successful. Otherwise, returns zero.
528 * "errno" is set to reflect the individual error.
530 * Side effect: If the retry is successful, both 'options' and
531 * 'extra_opts' are updated to reflect the mount options that worked.
532 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
534 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
536 char **extra_opts = mi->extra_opts;
538 if (po_join(mi->options, extra_opts) == PO_FAILED) {
544 printf(_("%s: text-based options: '%s'\n"),
545 progname, *extra_opts);
550 if (nfs_sys_mount(mi, "nfs", *extra_opts))
554 * The kernel returns EOPNOTSUPP if the RPC bind failed,
555 * and EPROTONOSUPPORT if the version isn't supported.
557 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
560 return nfs_retry_nfs23mount(mi);
564 * Attempt an NFS v4 mount via a mount(2) system call.
566 * Returns 1 if successful. Otherwise, returns zero.
567 * "errno" is set to reflect the individual error.
569 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
571 char **extra_opts = mi->extra_opts;
573 if (po_join(mi->options, extra_opts) == PO_FAILED) {
579 printf(_("%s: text-based options: '%s'\n"),
580 progname, *extra_opts);
585 return nfs_sys_mount(mi, "nfs4", *extra_opts);
589 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
591 * Returns 1 if successful. Otherwise, returns zero.
592 * "errno" is set to reflect the individual error.
594 static int nfs_try_mount(struct nfsmount_info *mi)
596 if (strncmp(mi->type, "nfs4", 4) == 0)
597 return nfs_try_nfs4mount(mi);
599 return nfs_try_nfs23mount(mi);
603 * Handle "foreground" NFS mounts.
605 * Retry the mount request for as long as the 'retry=' option says.
607 * Returns a valid mount command exit code.
609 static int nfsmount_fg(struct nfsmount_info *mi)
611 unsigned int secs = 1;
614 timeout = nfs_parse_retry_option(mi->options,
615 NFS_DEF_FG_TIMEOUT_MINUTES);
617 printf(_("%s: timeout set for %s"),
618 progname, ctime(&timeout));
621 if (nfs_try_mount(mi))
624 if (nfs_is_permanent_error(errno))
627 if (time(NULL) > timeout) {
632 if (errno != ETIMEDOUT) {
641 mount_error(mi->spec, mi->node, errno);
646 * Handle "background" NFS mount [first try]
648 * Returns a valid mount command exit code.
650 * EX_BG should cause the caller to fork and invoke nfsmount_child.
652 static int nfsmount_parent(struct nfsmount_info *mi)
654 if (nfs_try_mount(mi))
657 if (nfs_is_permanent_error(errno)) {
658 mount_error(mi->spec, mi->node, errno);
662 sys_mount_errors(mi->hostname, errno, 1, 1);
667 * Handle "background" NFS mount [retry daemon]
669 * Returns a valid mount command exit code: EX_SUCCESS if successful,
670 * EX_FAIL if a failure occurred. There's nothing to catch the
671 * error return, though, so we use sys_mount_errors to log the
674 static int nfsmount_child(struct nfsmount_info *mi)
676 unsigned int secs = 1;
679 timeout = nfs_parse_retry_option(mi->options,
680 NFS_DEF_BG_TIMEOUT_MINUTES);
689 if (nfs_try_mount(mi))
692 if (nfs_is_permanent_error(errno))
695 if (time(NULL) > timeout)
698 sys_mount_errors(mi->hostname, errno, 1, 1);
701 sys_mount_errors(mi->hostname, errno, 1, 0);
706 * Handle "background" NFS mount
708 * Returns a valid mount command exit code.
710 static int nfsmount_bg(struct nfsmount_info *mi)
713 return nfsmount_parent(mi);
715 return nfsmount_child(mi);
719 * Process mount options and try a mount system call.
721 * Returns a valid mount command exit code.
723 static int nfsmount_start(struct nfsmount_info *mi)
725 if (!nfs_validate_options(mi))
728 if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
729 return nfsmount_bg(mi);
731 return nfsmount_fg(mi);
735 * nfsmount_string - Mount an NFS file system using C string options
736 * @spec: C string specifying remote share to mount ("hostname:path")
737 * @node: C string pathname of local mounted-on directory
738 * @type: C string that represents file system type ("nfs" or "nfs4")
739 * @flags: MS_ style mount flags
740 * @extra_opts: pointer to C string containing fs-specific mount options
741 * (input and output argument)
742 * @fake: flag indicating whether to carry out the whole operation
743 * @child: one if this is a mount daemon (bg)
745 int nfsmount_string(const char *spec, const char *node, const char *type,
746 int flags, char **extra_opts, int fake, int child)
748 struct nfsmount_info mi = {
752 .extra_opts = extra_opts,
756 #ifdef IPV6_SUPPORTED
757 .family = AF_UNSPEC, /* either IPv4 or v6 */
759 .family = AF_INET, /* only IPv4 */
762 int retval = EX_FAIL;
764 mi.options = po_split(*extra_opts);
766 retval = nfsmount_start(&mi);
767 po_destroy(mi.options);
769 nfs_error(_("%s: internal option parsing error"), progname);