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.
36 #include <sys/socket.h>
37 #include <sys/mount.h>
42 #include "nfs_mount.h"
43 #include "mount_constants.h"
47 #include "parse_opt.h"
49 #ifdef HAVE_RPCSVC_NFS_PROT_H
50 #include <rpcsvc/nfs_prot.h>
52 #include <linux/nfs.h>
53 #define nfsstat nfs_stat
60 #ifndef NFS_MAXHOSTNAME
61 #define NFS_MAXHOSTNAME (255)
64 #ifndef NFS_MAXPATHNAME
65 #define NFS_MAXPATHNAME (1024)
68 extern int nfs_mount_data_version;
69 extern char *progname;
72 static int parse_devname(const char *spec, char **hostname)
75 char *dev, *pathname, *s;
79 if (!(pathname = strchr(dev, ':'))) {
80 nfs_error(_("%s: remote share not in 'host:dir' format"),
88 * We don't need a copy of the pathname, but let's
89 * sanity check it anyway.
91 if (strlen(pathname) > NFS_MAXPATHNAME) {
92 nfs_error(_("%s: export pathname is too long"),
98 * Ignore all but first hostname in replicated mounts
99 * until they can be fully supported. (mack@sgi.com)
101 if ((s = strchr(dev, ','))) {
103 nfs_error(_("%s: warning: multiple hostnames not supported"),
105 nfs_error(_("%s: ignoring hostnames that follow the first one"),
108 *hostname = xstrdup(dev);
109 if (strlen(*hostname) > NFS_MAXHOSTNAME) {
110 nfs_error(_("%s: server hostname is too long"),
123 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
126 addr->sin_family = AF_INET;
128 if (inet_aton(hostname, &addr->sin_addr))
130 if ((hp = gethostbyname(hostname)) == NULL) {
131 nfs_error(_("%s: can't get address for %s\n"),
135 if (hp->h_length > sizeof(struct in_addr)) {
136 nfs_error(_("%s: got bad hp->h_length"), progname);
137 hp->h_length = sizeof(struct in_addr);
139 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
144 * Append the 'addr=' option to the options string to pass a resolved
145 * server address to the kernel. After a successful mount, this address
146 * is also added to /etc/mtab for use when unmounting.
148 * If 'addr=' is already present, we strip it out. This prevents users
149 * from setting a bogus 'addr=' option themselves, and also allows bg
150 * retries to recompute the server's address, in case it has changed.
152 * Returns 1 if 'addr=' option appended successfully;
155 static int append_addr_option(struct sockaddr_in *saddr,
156 struct mount_options *options)
160 po_remove_all(options, "addr");
162 snprintf(new_option, sizeof(new_option) - 1,
163 "addr=%s", inet_ntoa(saddr->sin_addr));
165 if (po_append(options, new_option) == PO_SUCCEEDED)
171 * Called to discover our address and append an appropriate 'clientaddr='
172 * option to the options string.
174 * Returns 1 if 'clientaddr=' option created successfully or if
175 * 'clientaddr=' option is already present; otherwise zero.
177 static int append_clientaddr_option(struct sockaddr_in *saddr,
178 struct mount_options *options)
180 struct sockaddr_in my_addr;
183 if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
186 if (!get_client_address(saddr, &my_addr))
189 snprintf(new_option, sizeof(new_option) - 1,
190 "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
192 if (po_append(options, new_option) == PO_SUCCEEDED)
198 * Resolve the 'mounthost=' hostname and append a new option using
199 * the resulting IPv4 address.
201 static int fix_mounthost_option(struct mount_options *options)
203 struct sockaddr_in maddr;
204 char *mounthost, new_option[32];
206 mounthost = po_get(options, "mounthost");
210 if (!fill_ipv4_sockaddr(mounthost, &maddr))
213 snprintf(new_option, sizeof(new_option) - 1,
214 "mountaddr=%s", inet_ntoa(maddr.sin_addr));
216 if (po_append(options, new_option) == PO_SUCCEEDED)
222 * Set up mandatory mount options.
224 * Returns 1 if successful; otherwise zero.
226 static int set_mandatory_options(const char *type,
227 struct sockaddr_in *saddr,
228 struct mount_options *options)
230 if (!append_addr_option(saddr, options))
233 if (strncmp(type, "nfs4", 4) == 0) {
234 if (!append_clientaddr_option(saddr, options))
237 if (!fix_mounthost_option(options))
245 * Distinguish between permanent and temporary errors.
247 * Returns 0 if the passed-in error is temporary, thus the
248 * mount system call should be retried; returns one if the
249 * passed-in error is permanent, thus the mount system call
250 * should not be retried.
252 static int is_permanent_error(int error)
258 return 0; /* temporary */
260 return 1; /* permanent */
265 * Reconstruct the mount option string based on a portmapper probe
266 * of the server. Returns one if the server's portmapper returned
267 * something we can use, otherwise zero.
269 * To handle version and transport protocol fallback properly, we
270 * need to parse some of the mount options in order to set up a
271 * portmap probe. Mount options that rewrite_mount_options()
272 * doesn't recognize are left alone.
274 * Returns a new group of mount options if successful; otherwise
275 * NULL is returned if some failure occurred.
277 static struct mount_options *rewrite_mount_options(char *str)
279 struct mount_options *options;
280 char *option, new_option[64];
281 clnt_addr_t mnt_server = { };
282 clnt_addr_t nfs_server = { };
285 options = po_split(str);
289 option = po_get(options, "addr");
291 nfs_server.saddr.sin_family = AF_INET;
292 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
297 option = po_get(options, "mountaddr");
299 mnt_server.saddr.sin_family = AF_INET;
300 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
303 memcpy(&mnt_server.saddr, &nfs_server.saddr,
304 sizeof(mnt_server.saddr));
306 option = po_get(options, "mountport");
308 mnt_server.pmap.pm_port = atoi(option);
309 mnt_server.pmap.pm_prog = MOUNTPROG;
310 option = po_get(options, "mountvers");
312 mnt_server.pmap.pm_vers = atoi(option);
314 option = po_get(options, "port");
316 nfs_server.pmap.pm_port = atoi(option);
317 po_remove_all(options, "port");
319 nfs_server.pmap.pm_prog = NFS_PROGRAM;
321 option = po_get(options, "nfsvers");
323 nfs_server.pmap.pm_vers = atoi(option);
324 po_remove_all(options, "nfsvers");
326 option = po_get(options, "vers");
328 nfs_server.pmap.pm_vers = atoi(option);
329 po_remove_all(options, "vers");
331 option = po_get(options, "proto");
333 if (strcmp(option, "tcp") == 0) {
334 nfs_server.pmap.pm_prot = IPPROTO_TCP;
335 po_remove_all(options, "proto");
337 if (strcmp(option, "udp") == 0) {
338 nfs_server.pmap.pm_prot = IPPROTO_UDP;
339 po_remove_all(options, "proto");
342 p = po_rightmost(options, "tcp", "udp");
344 case PO_KEY2_RIGHTMOST:
345 nfs_server.pmap.pm_prot = IPPROTO_UDP;
347 case PO_KEY1_RIGHTMOST:
348 nfs_server.pmap.pm_prot = IPPROTO_TCP;
351 po_remove_all(options, "tcp");
352 po_remove_all(options, "udp");
354 if (!probe_bothports(&mnt_server, &nfs_server)) {
355 rpc_mount_errors("rpcbind", 0, 0);
359 snprintf(new_option, sizeof(new_option) - 1,
360 "nfsvers=%lu", nfs_server.pmap.pm_vers);
361 if (po_append(options, new_option) == PO_FAILED)
364 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
365 snprintf(new_option, sizeof(new_option) - 1,
368 snprintf(new_option, sizeof(new_option) - 1,
370 if (po_append(options, new_option) == PO_FAILED)
373 if (nfs_server.pmap.pm_port != NFS_PORT) {
374 snprintf(new_option, sizeof(new_option) - 1,
375 "port=%lu", nfs_server.pmap.pm_port);
376 if (po_append(options, new_option) == PO_FAILED)
389 * Retry an NFS mount that failed because the requested service isn't
390 * available on the server.
392 * Returns 1 if successful. Otherwise, returns zero.
393 * "errno" is set to reflect the individual error.
395 * Side effect: If the retry is successful, both 'options' and
396 * 'extra_opts' are updated to reflect the mount options that worked.
397 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
399 static int retry_nfsmount(const char *spec, const char *node,
400 int flags, struct mount_options *options,
401 int fake, char **extra_opts)
403 struct mount_options *retry_options;
404 char *retry_str = NULL;
406 retry_options = rewrite_mount_options(*extra_opts);
407 if (!retry_options) {
412 if (po_join(retry_options, &retry_str) == PO_FAILED) {
413 po_destroy(retry_options);
419 printf(_("%s: text-based options (retry): '%s'\n"),
420 progname, retry_str);
422 if (!mount(spec, node, "nfs",
423 flags & ~(MS_USER|MS_USERS), retry_str)) {
425 *extra_opts = retry_str;
426 po_replace(options, retry_options);
430 po_destroy(retry_options);
436 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
437 * claims the requested service isn't supported on the server, probe
438 * the server to see what's supported, rewrite the mount options,
439 * and retry the request.
441 * Returns 1 if successful. Otherwise, returns zero.
442 * "errno" is set to reflect the individual error.
444 * Side effect: If the retry is successful, both 'options' and
445 * 'extra_opts' are updated to reflect the mount options that worked.
446 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
448 static int try_nfs23mount(const char *spec, const char *node,
449 int flags, struct mount_options *options,
450 int fake, char **extra_opts)
452 if (po_join(options, extra_opts) == PO_FAILED) {
458 printf(_("%s: text-based options: '%s'\n"),
459 progname, *extra_opts);
464 if (!mount(spec, node, "nfs",
465 flags & ~(MS_USER|MS_USERS), *extra_opts))
469 * The kernel returns EOPNOTSUPP if the RPC bind failed,
470 * and EPROTONOSUPPORT if the version isn't supported.
472 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
475 return retry_nfsmount(spec, node, flags, options, fake, extra_opts);
479 * Attempt an NFS v4 mount via a mount(2) system call.
481 * Returns 1 if successful. Otherwise, returns zero.
482 * "errno" is set to reflect the individual error.
484 static int try_nfs4mount(const char *spec, const char *node,
485 int flags, struct mount_options *options,
486 int fake, char **extra_opts)
488 if (po_join(options, extra_opts) == PO_FAILED) {
494 printf(_("%s: text-based options: '%s'\n"),
495 progname, *extra_opts);
500 if (!mount(spec, node, "nfs4",
501 flags & ~(MS_USER|MS_USERS), *extra_opts))
507 * Try the mount(2) system call.
509 * Returns 1 if successful. Otherwise, returns zero.
510 * "errno" is set to reflect the individual error.
512 static int try_mount(const char *spec, const char *node, const char *type,
513 int flags, struct mount_options *options, int fake,
516 if (strncmp(type, "nfs4", 4) == 0)
517 return try_nfs4mount(spec, node, flags,
518 options, fake, extra_opts);
520 return try_nfs23mount(spec, node, flags,
521 options, fake, extra_opts);
525 * Handle "foreground" NFS mounts.
527 * Retry the mount request for as long as the 'retry=' option says.
529 * Returns a valid mount command exit code.
531 static int nfsmount_fg(const char *spec, const char *node,
532 const char *type, int flags,
533 struct mount_options *options, int fake,
536 unsigned int secs = 1;
537 time_t timeout = time(NULL);
540 timeout += 60 * 2; /* default: 2 minutes */
541 retry = po_get(options, "retry");
543 timeout += 60 * atoi(retry);
546 printf(_("%s: timeout set for %s"),
547 progname, ctime(&timeout));
550 if (try_mount(spec, node, type, flags,
551 options, fake, extra_opts))
554 if (is_permanent_error(errno))
557 if (time(NULL) > timeout) {
562 if (errno != ETIMEDOUT) {
571 mount_error(spec, node, errno);
576 * Handle "background" NFS mount [first try]
578 * Returns a valid mount command exit code.
580 * EX_BG should cause the caller to fork and invoke nfsmount_child.
582 static int nfsmount_parent(const char *spec, const char *node,
583 const char *type, char *hostname, int flags,
584 struct mount_options *options,
585 int fake, char **extra_opts)
587 if (try_mount(spec, node, type, flags, options,
591 if (is_permanent_error(errno)) {
592 mount_error(spec, node, errno);
596 sys_mount_errors(hostname, errno, 1, 1);
601 * Handle "background" NFS mount [retry daemon]
603 * Returns a valid mount command exit code: EX_SUCCESS if successful,
604 * EX_FAIL if a failure occurred. There's nothing to catch the
605 * error return, though, so we use sys_mount_errors to log the
608 static int nfsmount_child(const char *spec, const char *node,
609 const char *type, char *hostname, int flags,
610 struct mount_options *options,
611 int fake, char **extra_opts)
613 unsigned int secs = 1;
614 time_t timeout = time(NULL);
617 timeout += 60 * 10000; /* default: 10,000 minutes */
618 retry = po_get(options, "retry");
620 timeout += 60 * atoi(retry);
629 if (try_mount(spec, node, type, flags, options,
633 if (is_permanent_error(errno))
636 if (time(NULL) > timeout)
639 sys_mount_errors(hostname, errno, 1, 1);
642 sys_mount_errors(hostname, errno, 1, 0);
647 * Handle "background" NFS mount
649 * Returns a valid mount command exit code.
651 static int nfsmount_bg(const char *spec, const char *node,
652 const char *type, char *hostname, int flags,
653 struct mount_options *options,
654 int fake, int child, char **extra_opts)
657 return nfsmount_parent(spec, node, type, hostname, flags,
658 options, fake, extra_opts);
660 return nfsmount_child(spec, node, type, hostname, flags,
661 options, fake, extra_opts);
665 * nfsmount_string - Mount an NFS file system using C string options
666 * @spec: C string specifying remote share to mount ("hostname:path")
667 * @node: C string pathname of local mounted-on directory
668 * @type: C string that represents file system type ("nfs" or "nfs4")
669 * @flags: MS_ style mount flags
670 * @extra_opts: pointer to C string containing fs-specific mount options
671 * (input and output argument)
672 * @fake: flag indicating whether to carry out the whole operation
673 * @child: one if this is a mount daemon (bg)
675 int nfsmount_string(const char *spec, const char *node, const char *type,
676 int flags, char **extra_opts, int fake, int child)
678 struct mount_options *options = NULL;
679 struct sockaddr_in saddr;
681 int retval = EX_FAIL;
683 if (!parse_devname(spec, &hostname))
685 if (!fill_ipv4_sockaddr(hostname, &saddr))
688 options = po_split(*extra_opts);
690 nfs_error(_("%s: internal option parsing error"), progname);
694 if (!set_mandatory_options(type, &saddr, options))
697 if (po_rightmost(options, "bg", "fg") == PO_KEY1_RIGHTMOST)
698 retval = nfsmount_bg(spec, node, type, hostname, flags,
699 options, fake, child, extra_opts);
701 retval = nfsmount_fg(spec, node, type, flags, options,