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 */
102 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
105 addr->sin_family = AF_INET;
107 if (inet_aton(hostname, &addr->sin_addr))
109 if ((hp = gethostbyname(hostname)) == NULL) {
110 nfs_error(_("%s: can't get address for %s\n"),
114 if (hp->h_length > sizeof(struct in_addr)) {
115 nfs_error(_("%s: got bad hp->h_length"), progname);
116 hp->h_length = sizeof(struct in_addr);
118 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
123 * Obtain a retry timeout value based on the value of the "retry=" option.
125 * Returns a time_t timeout timestamp, in seconds.
127 static time_t nfs_parse_retry_option(struct mount_options *options,
128 unsigned int timeout_minutes)
130 char *retry_option, *endptr;
132 retry_option = po_get(options, "retry");
137 tmp = strtol(retry_option, &endptr, 10);
138 if (errno == 0 && endptr != retry_option && tmp >= 0)
139 timeout_minutes = tmp;
141 nfs_error(_("%s: invalid retry timeout was specified; "
142 "using default timeout"), progname);
145 return time(NULL) + (time_t)(timeout_minutes * 60);
149 * Convert the passed-in sockaddr-style address to presentation
150 * format, then append an option of the form "keyword=address".
152 * Returns 1 if the option was appended successfully; otherwise zero.
154 static int nfs_append_generic_address_option(const struct sockaddr *sap,
155 const socklen_t salen,
157 struct mount_options *options)
159 char address[NI_MAXHOST];
160 char new_option[512];
162 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
165 if (snprintf(new_option, sizeof(new_option), "%s=%s",
166 keyword, address) >= sizeof(new_option))
169 if (po_append(options, new_option) != PO_SUCCEEDED)
175 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
180 * Append the 'addr=' option to the options string to pass a resolved
181 * server address to the kernel. After a successful mount, this address
182 * is also added to /etc/mtab for use when unmounting.
184 * If 'addr=' is already present, we strip it out. This prevents users
185 * from setting a bogus 'addr=' option themselves, and also allows bg
186 * retries to recompute the server's address, in case it has changed.
188 * Returns 1 if 'addr=' option appended successfully;
191 static int nfs_append_addr_option(const struct sockaddr *sap,
193 struct mount_options *options)
195 po_remove_all(options, "addr");
196 return nfs_append_generic_address_option(sap, salen, "addr", options);
200 * Called to discover our address and append an appropriate 'clientaddr='
201 * option to the options string.
203 * Returns 1 if 'clientaddr=' option created successfully or if
204 * 'clientaddr=' option is already present; otherwise zero.
206 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
208 struct mount_options *options)
210 struct sockaddr_storage dummy;
211 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
212 socklen_t my_len = sizeof(dummy);
214 if (po_contains(options, "clientaddr") == PO_FOUND)
217 nfs_callback_address(sap, salen, my_addr, &my_len);
219 return nfs_append_generic_address_option(my_addr, my_len,
220 "clientaddr", options);
224 * Resolve the 'mounthost=' hostname and append a new option using
225 * the resulting address.
227 static int nfs_fix_mounthost_option(const sa_family_t family,
228 struct mount_options *options)
230 struct sockaddr_storage dummy;
231 struct sockaddr *sap = (struct sockaddr *)&dummy;
232 socklen_t salen = sizeof(dummy);
235 mounthost = po_get(options, "mounthost");
239 if (!nfs_name_to_address(mounthost, family, sap, &salen)) {
240 nfs_error(_("%s: unable to determine mount server's address"),
245 return nfs_append_generic_address_option(sap, salen,
246 "mountaddr", options);
250 * Returns zero if the "lock" option is in effect, but statd
251 * can't be started. Otherwise, returns 1.
253 static int verify_lock_option(struct mount_options *options)
255 if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
258 if (!start_statd()) {
259 nfs_error(_("%s: rpc.statd is not running but is "
260 "required for remote locking."), progname);
261 nfs_error(_("%s: Either use '-o nolock' to keep "
262 "locks local, or start statd."), progname);
269 static int nfs_append_sloppy_option(struct mount_options *options)
271 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
274 if (po_append(options, "sloppy") == PO_FAILED)
280 * Set up mandatory NFS mount options.
282 * Returns 1 if successful; otherwise zero.
284 static int nfs_validate_options(struct nfsmount_info *mi)
286 struct sockaddr_storage dummy;
287 struct sockaddr *sap = (struct sockaddr *)&dummy;
288 socklen_t salen = sizeof(dummy);
290 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
293 if (!nfs_name_to_address(mi->hostname, mi->family, sap, &salen))
296 if (strncmp(mi->type, "nfs4", 4) == 0) {
297 if (!nfs_append_clientaddr_option(sap, salen, mi->options))
300 if (!nfs_fix_mounthost_option(mi->family, mi->options))
302 if (!mi->fake && !verify_lock_option(mi->options))
306 if (!nfs_append_sloppy_option(mi->options))
309 return nfs_append_addr_option(sap, salen, mi->options);
313 * Distinguish between permanent and temporary errors.
315 * Returns 0 if the passed-in error is temporary, thus the
316 * mount system call should be retried; returns one if the
317 * passed-in error is permanent, thus the mount system call
318 * should not be retried.
320 static int is_permanent_error(int error)
326 return 0; /* temporary */
328 return 1; /* permanent */
333 * Reconstruct the mount option string based on a portmapper probe
334 * of the server. Returns one if the server's portmapper returned
335 * something we can use, otherwise zero.
337 * To handle version and transport protocol fallback properly, we
338 * need to parse some of the mount options in order to set up a
339 * portmap probe. Mount options that rewrite_mount_options()
340 * doesn't recognize are left alone.
342 * Returns a new group of mount options if successful; otherwise
343 * NULL is returned if some failure occurred.
345 static struct mount_options *rewrite_mount_options(char *str)
347 struct mount_options *options;
348 char *option, new_option[64];
349 clnt_addr_t mnt_server = { };
350 clnt_addr_t nfs_server = { };
353 options = po_split(str);
360 option = po_get(options, "addr");
362 nfs_server.saddr.sin_family = AF_INET;
363 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
368 option = po_get(options, "mountaddr");
370 mnt_server.saddr.sin_family = AF_INET;
371 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
374 memcpy(&mnt_server.saddr, &nfs_server.saddr,
375 sizeof(mnt_server.saddr));
377 option = po_get(options, "mountport");
379 mnt_server.pmap.pm_port = atoi(option);
380 mnt_server.pmap.pm_prog = MOUNTPROG;
381 option = po_get(options, "mountvers");
383 mnt_server.pmap.pm_vers = atoi(option);
385 option = po_get(options, "port");
387 nfs_server.pmap.pm_port = atoi(option);
388 po_remove_all(options, "port");
390 nfs_server.pmap.pm_prog = NFS_PROGRAM;
392 option = po_get(options, "nfsvers");
394 nfs_server.pmap.pm_vers = atoi(option);
395 po_remove_all(options, "nfsvers");
397 option = po_get(options, "vers");
399 nfs_server.pmap.pm_vers = atoi(option);
400 po_remove_all(options, "vers");
402 option = po_get(options, "proto");
404 if (strcmp(option, "tcp") == 0) {
405 nfs_server.pmap.pm_prot = IPPROTO_TCP;
406 po_remove_all(options, "proto");
408 if (strcmp(option, "udp") == 0) {
409 nfs_server.pmap.pm_prot = IPPROTO_UDP;
410 po_remove_all(options, "proto");
413 p = po_rightmost(options, "tcp", "udp");
415 case PO_KEY2_RIGHTMOST:
416 nfs_server.pmap.pm_prot = IPPROTO_UDP;
418 case PO_KEY1_RIGHTMOST:
419 nfs_server.pmap.pm_prot = IPPROTO_TCP;
422 po_remove_all(options, "tcp");
423 po_remove_all(options, "udp");
425 if (!probe_bothports(&mnt_server, &nfs_server)) {
430 snprintf(new_option, sizeof(new_option) - 1,
431 "nfsvers=%lu", nfs_server.pmap.pm_vers);
432 if (po_append(options, new_option) == PO_FAILED)
435 if (nfs_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 if (nfs_server.pmap.pm_port != NFS_PORT) {
445 snprintf(new_option, sizeof(new_option) - 1,
446 "port=%lu", nfs_server.pmap.pm_port);
447 if (po_append(options, new_option) == PO_FAILED)
461 * Do the mount(2) system call.
463 * Returns 1 if successful, otherwise zero.
464 * "errno" is set to reflect the individual error.
466 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
471 result = mount(mi->spec, mi->node, type,
472 mi->flags & ~(MS_USER|MS_USERS), options);
473 if (verbose && result) {
475 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
482 * Retry an NFS mount that failed because the requested service isn't
483 * available on the server.
485 * Returns 1 if successful. Otherwise, returns zero.
486 * "errno" is set to reflect the individual error.
488 * Side effect: If the retry is successful, both 'options' and
489 * 'extra_opts' are updated to reflect the mount options that worked.
490 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
492 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
494 struct mount_options *retry_options;
495 char *retry_str = NULL;
496 char **extra_opts = mi->extra_opts;
498 retry_options = rewrite_mount_options(*extra_opts);
502 if (po_join(retry_options, &retry_str) == PO_FAILED) {
503 po_destroy(retry_options);
509 printf(_("%s: text-based options (retry): '%s'\n"),
510 progname, retry_str);
512 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
513 po_destroy(retry_options);
519 *extra_opts = retry_str;
520 po_replace(mi->options, retry_options);
525 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
526 * claims the requested service isn't supported on the server, probe
527 * the server to see what's supported, rewrite the mount options,
528 * and retry the request.
530 * Returns 1 if successful. Otherwise, returns zero.
531 * "errno" is set to reflect the individual error.
533 * Side effect: If the retry is successful, both 'options' and
534 * 'extra_opts' are updated to reflect the mount options that worked.
535 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
537 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
539 char **extra_opts = mi->extra_opts;
541 if (po_join(mi->options, extra_opts) == PO_FAILED) {
547 printf(_("%s: text-based options: '%s'\n"),
548 progname, *extra_opts);
553 if (nfs_sys_mount(mi, "nfs", *extra_opts))
557 * The kernel returns EOPNOTSUPP if the RPC bind failed,
558 * and EPROTONOSUPPORT if the version isn't supported.
560 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
563 return nfs_retry_nfs23mount(mi);
567 * Attempt an NFS v4 mount via a mount(2) system call.
569 * Returns 1 if successful. Otherwise, returns zero.
570 * "errno" is set to reflect the individual error.
572 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
574 char **extra_opts = mi->extra_opts;
576 if (po_join(mi->options, extra_opts) == PO_FAILED) {
582 printf(_("%s: text-based options: '%s'\n"),
583 progname, *extra_opts);
588 return nfs_sys_mount(mi, "nfs4", *extra_opts);
592 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
594 * Returns 1 if successful. Otherwise, returns zero.
595 * "errno" is set to reflect the individual error.
597 static int nfs_try_mount(struct nfsmount_info *mi)
599 if (strncmp(mi->type, "nfs4", 4) == 0)
600 return nfs_try_nfs4mount(mi);
602 return nfs_try_nfs23mount(mi);
606 * Handle "foreground" NFS mounts.
608 * Retry the mount request for as long as the 'retry=' option says.
610 * Returns a valid mount command exit code.
612 static int nfsmount_fg(struct nfsmount_info *mi)
614 unsigned int secs = 1;
617 timeout = nfs_parse_retry_option(mi->options,
618 NFS_DEF_FG_TIMEOUT_MINUTES);
620 printf(_("%s: timeout set for %s"),
621 progname, ctime(&timeout));
624 if (nfs_try_mount(mi))
627 if (is_permanent_error(errno))
630 if (time(NULL) > timeout) {
635 if (errno != ETIMEDOUT) {
644 mount_error(mi->spec, mi->node, errno);
649 * Handle "background" NFS mount [first try]
651 * Returns a valid mount command exit code.
653 * EX_BG should cause the caller to fork and invoke nfsmount_child.
655 static int nfsmount_parent(struct nfsmount_info *mi)
657 if (nfs_try_mount(mi))
660 if (is_permanent_error(errno)) {
661 mount_error(mi->spec, mi->node, errno);
665 sys_mount_errors(mi->hostname, errno, 1, 1);
670 * Handle "background" NFS mount [retry daemon]
672 * Returns a valid mount command exit code: EX_SUCCESS if successful,
673 * EX_FAIL if a failure occurred. There's nothing to catch the
674 * error return, though, so we use sys_mount_errors to log the
677 static int nfsmount_child(struct nfsmount_info *mi)
679 unsigned int secs = 1;
682 timeout = nfs_parse_retry_option(mi->options,
683 NFS_DEF_BG_TIMEOUT_MINUTES);
692 if (nfs_try_mount(mi))
695 if (is_permanent_error(errno))
698 if (time(NULL) > timeout)
701 sys_mount_errors(mi->hostname, errno, 1, 1);
704 sys_mount_errors(mi->hostname, errno, 1, 0);
709 * Handle "background" NFS mount
711 * Returns a valid mount command exit code.
713 static int nfsmount_bg(struct nfsmount_info *mi)
716 return nfsmount_parent(mi);
718 return nfsmount_child(mi);
722 * Process mount options and try a mount system call.
724 * Returns a valid mount command exit code.
726 static int nfsmount_start(struct nfsmount_info *mi)
728 if (!nfs_validate_options(mi))
731 if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
732 return nfsmount_bg(mi);
734 return nfsmount_fg(mi);
738 * nfsmount_string - Mount an NFS file system using C string options
739 * @spec: C string specifying remote share to mount ("hostname:path")
740 * @node: C string pathname of local mounted-on directory
741 * @type: C string that represents file system type ("nfs" or "nfs4")
742 * @flags: MS_ style mount flags
743 * @extra_opts: pointer to C string containing fs-specific mount options
744 * (input and output argument)
745 * @fake: flag indicating whether to carry out the whole operation
746 * @child: one if this is a mount daemon (bg)
748 int nfsmount_string(const char *spec, const char *node, const char *type,
749 int flags, char **extra_opts, int fake, int child)
751 struct nfsmount_info mi = {
755 .extra_opts = extra_opts,
759 #ifdef IPV6_SUPPORTED
760 .family = AF_UNSPEC, /* either IPv4 or v6 */
762 .family = AF_INET, /* only IPv4 */
765 int retval = EX_FAIL;
767 mi.options = po_split(*extra_opts);
769 retval = nfsmount_start(&mi);
770 po_destroy(mi.options);
772 nfs_error(_("%s: internal option parsing error"), progname);