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.
37 #include <sys/socket.h>
38 #include <sys/mount.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
45 #include "nfs_mount.h"
46 #include "mount_constants.h"
50 #include "parse_opt.h"
52 #include "parse_dev.h"
54 #ifdef HAVE_RPCSVC_NFS_PROT_H
55 #include <rpcsvc/nfs_prot.h>
57 #include <linux/nfs.h>
58 #define nfsstat nfs_stat
65 #ifndef NFS_MAXHOSTNAME
66 #define NFS_MAXHOSTNAME (255)
69 #ifndef NFS_MAXPATHNAME
70 #define NFS_MAXPATHNAME (1024)
73 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
74 #define NFS_DEF_FG_TIMEOUT_MINUTES (2u)
77 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
78 #define NFS_DEF_BG_TIMEOUT_MINUTES (10000u)
81 extern int nfs_mount_data_version;
82 extern char *progname;
86 struct nfsmount_info {
87 const char *spec, /* server:/path */
88 *node, /* mounted-on dir */
89 *type; /* "nfs" or "nfs4" */
90 char *hostname; /* server's hostname */
92 struct mount_options *options; /* parsed mount options */
93 char **extra_opts; /* string for /etc/mtab */
95 int flags, /* MS_ flags */
96 fake, /* actually do the mount? */
97 child; /* forked bg child? */
99 sa_family_t family; /* supported address family */
103 * Obtain a retry timeout value based on the value of the "retry=" option.
105 * Returns a time_t timeout timestamp, in seconds.
107 static time_t nfs_parse_retry_option(struct mount_options *options,
108 unsigned int timeout_minutes)
110 char *retry_option, *endptr;
112 retry_option = po_get(options, "retry");
117 tmp = strtol(retry_option, &endptr, 10);
118 if (errno == 0 && endptr != retry_option && tmp >= 0)
119 timeout_minutes = tmp;
121 nfs_error(_("%s: invalid retry timeout was specified; "
122 "using default timeout"), progname);
125 return time(NULL) + (time_t)(timeout_minutes * 60);
129 * Convert the passed-in sockaddr-style address to presentation
130 * format, then append an option of the form "keyword=address".
132 * Returns 1 if the option was appended successfully; otherwise zero.
134 static int nfs_append_generic_address_option(const struct sockaddr *sap,
135 const socklen_t salen,
137 struct mount_options *options)
139 char address[NI_MAXHOST];
140 char new_option[512];
142 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
145 if (snprintf(new_option, sizeof(new_option), "%s=%s",
146 keyword, address) >= sizeof(new_option))
149 if (po_append(options, new_option) != PO_SUCCEEDED)
155 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
160 * Append the 'addr=' option to the options string to pass a resolved
161 * server address to the kernel. After a successful mount, this address
162 * is also added to /etc/mtab for use when unmounting.
164 * If 'addr=' is already present, we strip it out. This prevents users
165 * from setting a bogus 'addr=' option themselves, and also allows bg
166 * retries to recompute the server's address, in case it has changed.
168 * Returns 1 if 'addr=' option appended successfully;
171 static int nfs_append_addr_option(const struct sockaddr *sap,
173 struct mount_options *options)
175 po_remove_all(options, "addr");
176 return nfs_append_generic_address_option(sap, salen, "addr", options);
180 * Called to discover our address and append an appropriate 'clientaddr='
181 * option to the options string.
183 * Returns 1 if 'clientaddr=' option created successfully or if
184 * 'clientaddr=' option is already present; otherwise zero.
186 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
188 struct mount_options *options)
190 struct sockaddr_storage dummy;
191 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
192 socklen_t my_len = sizeof(dummy);
194 if (po_contains(options, "clientaddr") == PO_FOUND)
197 nfs_callback_address(sap, salen, my_addr, &my_len);
199 return nfs_append_generic_address_option(my_addr, my_len,
200 "clientaddr", options);
204 * Resolve the 'mounthost=' hostname and append a new option using
205 * the resulting address.
207 static int nfs_fix_mounthost_option(const sa_family_t family,
208 struct mount_options *options)
210 struct sockaddr_storage dummy;
211 struct sockaddr *sap = (struct sockaddr *)&dummy;
212 socklen_t salen = sizeof(dummy);
215 mounthost = po_get(options, "mounthost");
219 if (!nfs_name_to_address(mounthost, family, sap, &salen)) {
220 nfs_error(_("%s: unable to determine mount server's address"),
225 return nfs_append_generic_address_option(sap, salen,
226 "mountaddr", options);
230 * Returns zero if the "lock" option is in effect, but statd
231 * can't be started. Otherwise, returns 1.
233 static int verify_lock_option(struct mount_options *options)
235 if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
238 if (!start_statd()) {
239 nfs_error(_("%s: rpc.statd is not running but is "
240 "required for remote locking."), progname);
241 nfs_error(_("%s: Either use '-o nolock' to keep "
242 "locks local, or start statd."), progname);
249 static int nfs_append_sloppy_option(struct mount_options *options)
251 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
254 if (po_append(options, "sloppy") == PO_FAILED)
260 * Set up mandatory NFS mount options.
262 * Returns 1 if successful; otherwise zero.
264 static int nfs_validate_options(struct nfsmount_info *mi)
266 struct sockaddr_storage dummy;
267 struct sockaddr *sap = (struct sockaddr *)&dummy;
268 socklen_t salen = sizeof(dummy);
270 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
273 if (!nfs_name_to_address(mi->hostname, mi->family, sap, &salen))
276 if (strncmp(mi->type, "nfs4", 4) == 0) {
277 if (!nfs_append_clientaddr_option(sap, salen, mi->options))
280 if (!nfs_fix_mounthost_option(mi->family, mi->options))
282 if (!mi->fake && !verify_lock_option(mi->options))
286 if (!nfs_append_sloppy_option(mi->options))
289 return nfs_append_addr_option(sap, salen, mi->options);
293 * Distinguish between permanent and temporary errors.
295 * Returns 0 if the passed-in error is temporary, thus the
296 * mount system call should be retried; returns one if the
297 * passed-in error is permanent, thus the mount system call
298 * should not be retried.
300 static int is_permanent_error(int error)
306 return 0; /* temporary */
308 return 1; /* permanent */
313 * Reconstruct the mount option string based on a portmapper probe
314 * of the server. Returns one if the server's portmapper returned
315 * something we can use, otherwise zero.
317 * To handle version and transport protocol fallback properly, we
318 * need to parse some of the mount options in order to set up a
319 * portmap probe. Mount options that rewrite_mount_options()
320 * doesn't recognize are left alone.
322 * Returns a new group of mount options if successful; otherwise
323 * NULL is returned if some failure occurred.
325 static struct mount_options *rewrite_mount_options(char *str)
327 struct mount_options *options;
328 char *option, new_option[64];
329 clnt_addr_t mnt_server = { };
330 clnt_addr_t nfs_server = { };
333 options = po_split(str);
340 option = po_get(options, "addr");
342 nfs_server.saddr.sin_family = AF_INET;
343 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
348 option = po_get(options, "mountaddr");
350 mnt_server.saddr.sin_family = AF_INET;
351 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
354 memcpy(&mnt_server.saddr, &nfs_server.saddr,
355 sizeof(mnt_server.saddr));
357 option = po_get(options, "mountport");
359 mnt_server.pmap.pm_port = atoi(option);
360 mnt_server.pmap.pm_prog = MOUNTPROG;
361 option = po_get(options, "mountvers");
363 mnt_server.pmap.pm_vers = atoi(option);
365 option = po_get(options, "port");
367 nfs_server.pmap.pm_port = atoi(option);
368 po_remove_all(options, "port");
370 nfs_server.pmap.pm_prog = NFS_PROGRAM;
372 option = po_get(options, "nfsvers");
374 nfs_server.pmap.pm_vers = atoi(option);
375 po_remove_all(options, "nfsvers");
377 option = po_get(options, "vers");
379 nfs_server.pmap.pm_vers = atoi(option);
380 po_remove_all(options, "vers");
382 option = po_get(options, "proto");
384 if (strcmp(option, "tcp") == 0) {
385 nfs_server.pmap.pm_prot = IPPROTO_TCP;
386 po_remove_all(options, "proto");
388 if (strcmp(option, "udp") == 0) {
389 nfs_server.pmap.pm_prot = IPPROTO_UDP;
390 po_remove_all(options, "proto");
393 p = po_rightmost(options, "tcp", "udp");
395 case PO_KEY2_RIGHTMOST:
396 nfs_server.pmap.pm_prot = IPPROTO_UDP;
398 case PO_KEY1_RIGHTMOST:
399 nfs_server.pmap.pm_prot = IPPROTO_TCP;
402 po_remove_all(options, "tcp");
403 po_remove_all(options, "udp");
405 if (!probe_bothports(&mnt_server, &nfs_server)) {
410 snprintf(new_option, sizeof(new_option) - 1,
411 "nfsvers=%lu", nfs_server.pmap.pm_vers);
412 if (po_append(options, new_option) == PO_FAILED)
415 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
416 snprintf(new_option, sizeof(new_option) - 1,
419 snprintf(new_option, sizeof(new_option) - 1,
421 if (po_append(options, new_option) == PO_FAILED)
424 if (nfs_server.pmap.pm_port != NFS_PORT) {
425 snprintf(new_option, sizeof(new_option) - 1,
426 "port=%lu", nfs_server.pmap.pm_port);
427 if (po_append(options, new_option) == PO_FAILED)
441 * Do the mount(2) system call.
443 * Returns 1 if successful, otherwise zero.
444 * "errno" is set to reflect the individual error.
446 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
451 result = mount(mi->spec, mi->node, type,
452 mi->flags & ~(MS_USER|MS_USERS), options);
453 if (verbose && result) {
455 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
462 * Retry an NFS mount that failed because the requested service isn't
463 * available on the server.
465 * Returns 1 if successful. Otherwise, returns zero.
466 * "errno" is set to reflect the individual error.
468 * Side effect: If the retry is successful, both 'options' and
469 * 'extra_opts' are updated to reflect the mount options that worked.
470 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
472 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
474 struct mount_options *retry_options;
475 char *retry_str = NULL;
476 char **extra_opts = mi->extra_opts;
478 retry_options = rewrite_mount_options(*extra_opts);
482 if (po_join(retry_options, &retry_str) == PO_FAILED) {
483 po_destroy(retry_options);
489 printf(_("%s: text-based options (retry): '%s'\n"),
490 progname, retry_str);
492 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
493 po_destroy(retry_options);
499 *extra_opts = retry_str;
500 po_replace(mi->options, retry_options);
505 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
506 * claims the requested service isn't supported on the server, probe
507 * the server to see what's supported, rewrite the mount options,
508 * and retry the request.
510 * Returns 1 if successful. Otherwise, returns zero.
511 * "errno" is set to reflect the individual error.
513 * Side effect: If the retry is successful, both 'options' and
514 * 'extra_opts' are updated to reflect the mount options that worked.
515 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
517 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
519 char **extra_opts = mi->extra_opts;
521 if (po_join(mi->options, extra_opts) == PO_FAILED) {
527 printf(_("%s: text-based options: '%s'\n"),
528 progname, *extra_opts);
533 if (nfs_sys_mount(mi, "nfs", *extra_opts))
537 * The kernel returns EOPNOTSUPP if the RPC bind failed,
538 * and EPROTONOSUPPORT if the version isn't supported.
540 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
543 return nfs_retry_nfs23mount(mi);
547 * Attempt an NFS v4 mount via a mount(2) system call.
549 * Returns 1 if successful. Otherwise, returns zero.
550 * "errno" is set to reflect the individual error.
552 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
554 char **extra_opts = mi->extra_opts;
556 if (po_join(mi->options, extra_opts) == PO_FAILED) {
562 printf(_("%s: text-based options: '%s'\n"),
563 progname, *extra_opts);
568 return nfs_sys_mount(mi, "nfs4", *extra_opts);
572 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
574 * Returns 1 if successful. Otherwise, returns zero.
575 * "errno" is set to reflect the individual error.
577 static int nfs_try_mount(struct nfsmount_info *mi)
579 if (strncmp(mi->type, "nfs4", 4) == 0)
580 return nfs_try_nfs4mount(mi);
582 return nfs_try_nfs23mount(mi);
586 * Handle "foreground" NFS mounts.
588 * Retry the mount request for as long as the 'retry=' option says.
590 * Returns a valid mount command exit code.
592 static int nfsmount_fg(struct nfsmount_info *mi)
594 unsigned int secs = 1;
597 timeout = nfs_parse_retry_option(mi->options,
598 NFS_DEF_FG_TIMEOUT_MINUTES);
600 printf(_("%s: timeout set for %s"),
601 progname, ctime(&timeout));
604 if (nfs_try_mount(mi))
607 if (is_permanent_error(errno))
610 if (time(NULL) > timeout) {
615 if (errno != ETIMEDOUT) {
624 mount_error(mi->spec, mi->node, errno);
629 * Handle "background" NFS mount [first try]
631 * Returns a valid mount command exit code.
633 * EX_BG should cause the caller to fork and invoke nfsmount_child.
635 static int nfsmount_parent(struct nfsmount_info *mi)
637 if (nfs_try_mount(mi))
640 if (is_permanent_error(errno)) {
641 mount_error(mi->spec, mi->node, errno);
645 sys_mount_errors(mi->hostname, errno, 1, 1);
650 * Handle "background" NFS mount [retry daemon]
652 * Returns a valid mount command exit code: EX_SUCCESS if successful,
653 * EX_FAIL if a failure occurred. There's nothing to catch the
654 * error return, though, so we use sys_mount_errors to log the
657 static int nfsmount_child(struct nfsmount_info *mi)
659 unsigned int secs = 1;
662 timeout = nfs_parse_retry_option(mi->options,
663 NFS_DEF_BG_TIMEOUT_MINUTES);
672 if (nfs_try_mount(mi))
675 if (is_permanent_error(errno))
678 if (time(NULL) > timeout)
681 sys_mount_errors(mi->hostname, errno, 1, 1);
684 sys_mount_errors(mi->hostname, errno, 1, 0);
689 * Handle "background" NFS mount
691 * Returns a valid mount command exit code.
693 static int nfsmount_bg(struct nfsmount_info *mi)
696 return nfsmount_parent(mi);
698 return nfsmount_child(mi);
702 * Process mount options and try a mount system call.
704 * Returns a valid mount command exit code.
706 static int nfsmount_start(struct nfsmount_info *mi)
708 if (!nfs_validate_options(mi))
711 if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
712 return nfsmount_bg(mi);
714 return nfsmount_fg(mi);
718 * nfsmount_string - Mount an NFS file system using C string options
719 * @spec: C string specifying remote share to mount ("hostname:path")
720 * @node: C string pathname of local mounted-on directory
721 * @type: C string that represents file system type ("nfs" or "nfs4")
722 * @flags: MS_ style mount flags
723 * @extra_opts: pointer to C string containing fs-specific mount options
724 * (input and output argument)
725 * @fake: flag indicating whether to carry out the whole operation
726 * @child: one if this is a mount daemon (bg)
728 int nfsmount_string(const char *spec, const char *node, const char *type,
729 int flags, char **extra_opts, int fake, int child)
731 struct nfsmount_info mi = {
735 .extra_opts = extra_opts,
739 #ifdef IPV6_SUPPORTED
740 .family = AF_UNSPEC, /* either IPv4 or v6 */
742 .family = AF_INET, /* only IPv4 */
745 int retval = EX_FAIL;
747 mi.options = po_split(*extra_opts);
749 retval = nfsmount_start(&mi);
750 po_destroy(mi.options);
752 nfs_error(_("%s: internal option parsing error"), progname);