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.
32 #include <sys/socket.h>
33 #include <sys/mount.h>
38 #include "nfs_mount.h"
39 #include "mount_constants.h"
43 #include "parse_opt.h"
45 #ifdef HAVE_RPCSVC_NFS_PROT_H
46 #include <rpcsvc/nfs_prot.h>
48 #include <linux/nfs.h>
49 #define nfsstat nfs_stat
56 #ifndef NFS_MAXHOSTNAME
57 #define NFS_MAXHOSTNAME (255)
60 #ifndef NFS_MAXPATHNAME
61 #define NFS_MAXPATHNAME (1024)
64 extern int nfs_mount_data_version;
65 extern char *progname;
68 static int parse_devname(const char *spec, char **hostname)
71 char *dev, *pathname, *s;
75 if (!(pathname = strchr(dev, ':'))) {
76 nfs_error(_("%s: remote share not in 'host:dir' format"),
84 * We don't need a copy of the pathname, but let's
85 * sanity check it anyway.
87 if (strlen(pathname) > NFS_MAXPATHNAME) {
88 nfs_error(_("%s: export pathname is too long"),
94 * Ignore all but first hostname in replicated mounts
95 * until they can be fully supported. (mack@sgi.com)
97 if ((s = strchr(dev, ','))) {
99 nfs_error(_("%s: warning: multiple hostnames not supported"),
101 nfs_error(_("%s: ignoring hostnames that follow the first one"),
104 *hostname = xstrdup(dev);
105 if (strlen(*hostname) > NFS_MAXHOSTNAME) {
106 nfs_error(_("%s: server hostname is too long"),
119 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
122 addr->sin_family = AF_INET;
124 if (inet_aton(hostname, &addr->sin_addr))
126 if ((hp = gethostbyname(hostname)) == NULL) {
127 nfs_error(_("%s: can't get address for %s\n"),
131 if (hp->h_length > sizeof(struct in_addr)) {
132 nfs_error(_("%s: got bad hp->h_length"), progname);
133 hp->h_length = sizeof(struct in_addr);
135 memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
140 * Append the 'addr=' option to the options string to pass a resolved
141 * server address to the kernel. After a successful mount, this address
142 * is also added to /etc/mtab for use when unmounting.
144 * If 'addr=' is already present, we strip it out. This prevents users
145 * from setting a bogus 'addr=' option themselves, and also allows bg
146 * retries to recompute the server's address, in case it has changed.
148 * Returns 1 if 'addr=' option appended successfully;
151 static int append_addr_option(struct sockaddr_in *saddr,
152 struct mount_options *options)
156 po_remove_all(options, "addr");
158 snprintf(new_option, sizeof(new_option) - 1,
159 "addr=%s", inet_ntoa(saddr->sin_addr));
161 if (po_append(options, new_option) == PO_SUCCEEDED)
167 * Called to discover our address and append an appropriate 'clientaddr='
168 * option to the options string.
170 * Returns 1 if 'clientaddr=' option created successfully or if
171 * 'clientaddr=' option is already present; otherwise zero.
173 static int append_clientaddr_option(struct sockaddr_in *saddr,
174 struct mount_options *options)
176 struct sockaddr_in my_addr;
179 if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
182 if (!get_client_address(saddr, &my_addr))
185 snprintf(new_option, sizeof(new_option) - 1,
186 "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
188 if (po_append(options, new_option) == PO_SUCCEEDED)
194 * Resolve the 'mounthost=' hostname and append a new option using
195 * the resulting IPv4 address.
197 static int fix_mounthost_option(struct mount_options *options)
199 struct sockaddr_in maddr;
200 char *mounthost, new_option[32];
202 mounthost = po_get(options, "mounthost");
206 if (!fill_ipv4_sockaddr(mounthost, &maddr))
209 snprintf(new_option, sizeof(new_option) - 1,
210 "mountaddr=%s", inet_ntoa(maddr.sin_addr));
212 if (po_append(options, new_option) == PO_SUCCEEDED)
218 * Set up mandatory mount options.
220 * Returns 1 if successful; otherwise zero.
222 static int set_mandatory_options(const char *type,
223 struct sockaddr_in *saddr,
224 struct mount_options *options)
226 if (!append_addr_option(saddr, options))
229 if (strncmp(type, "nfs4", 4) == 0) {
230 if (!append_clientaddr_option(saddr, options))
233 if (!fix_mounthost_option(options))
241 * Distinguish between permanent and temporary errors.
243 * Returns 0 if the passed-in error is temporary, thus the
244 * mount system call should be retried; returns one if the
245 * passed-in error is permanent, thus the mount system call
246 * should not be retried.
248 static int is_permanent_error(int error)
255 return 0; /* temporary */
257 return 1; /* permanent */
262 * Reconstruct the mount option string based on a portmapper probe
263 * of the server. Returns one if the server's portmapper returned
264 * something we can use, otherwise zero.
266 * To handle version and transport protocol fallback properly, we
267 * need to parse some of the mount options in order to set up a
268 * portmap probe. Mount options that rewrite_mount_options()
269 * doesn't recognize are left alone.
271 * Returns a new group of mount options if successful; otherwise
272 * NULL is returned if some failure occurred.
274 static struct mount_options *rewrite_mount_options(char *str)
276 struct mount_options *options;
277 char *option, new_option[64];
278 clnt_addr_t mnt_server = { };
279 clnt_addr_t nfs_server = { };
282 options = po_split(str);
286 option = po_get(options, "addr");
288 nfs_server.saddr.sin_family = AF_INET;
289 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
294 option = po_get(options, "mountaddr");
296 mnt_server.saddr.sin_family = AF_INET;
297 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
300 memcpy(&mnt_server.saddr, &nfs_server.saddr,
301 sizeof(mnt_server.saddr));
303 option = po_get(options, "mountport");
305 mnt_server.pmap.pm_port = atoi(option);
306 mnt_server.pmap.pm_prog = MOUNTPROG;
307 option = po_get(options, "mountvers");
309 mnt_server.pmap.pm_vers = atoi(option);
311 option = po_get(options, "port");
313 nfs_server.pmap.pm_port = atoi(option);
314 po_remove_all(options, "port");
316 nfs_server.pmap.pm_prog = NFS_PROGRAM;
318 option = po_get(options, "nfsvers");
320 nfs_server.pmap.pm_vers = atoi(option);
321 po_remove_all(options, "nfsvers");
323 option = po_get(options, "vers");
325 nfs_server.pmap.pm_vers = atoi(option);
326 po_remove_all(options, "vers");
328 option = po_get(options, "proto");
330 if (strcmp(option, "tcp") == 0) {
331 nfs_server.pmap.pm_prot = IPPROTO_TCP;
332 po_remove_all(options, "proto");
334 if (strcmp(option, "udp") == 0) {
335 nfs_server.pmap.pm_prot = IPPROTO_UDP;
336 po_remove_all(options, "proto");
339 p = po_rightmost(options, "tcp", "udp");
341 case PO_KEY2_RIGHTMOST:
342 nfs_server.pmap.pm_prot = IPPROTO_UDP;
344 case PO_KEY1_RIGHTMOST:
345 nfs_server.pmap.pm_prot = IPPROTO_TCP;
348 po_remove_all(options, "tcp");
349 po_remove_all(options, "udp");
351 if (!probe_bothports(&mnt_server, &nfs_server)) {
352 rpc_mount_errors("rpcbind", 0, 0);
356 snprintf(new_option, sizeof(new_option) - 1,
357 "nfsvers=%lu", nfs_server.pmap.pm_vers);
358 if (po_append(options, new_option) == PO_FAILED)
361 if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
362 snprintf(new_option, sizeof(new_option) - 1,
365 snprintf(new_option, sizeof(new_option) - 1,
367 if (po_append(options, new_option) == PO_FAILED)
370 if (nfs_server.pmap.pm_port != NFS_PORT) {
371 snprintf(new_option, sizeof(new_option) - 1,
372 "port=%lu", nfs_server.pmap.pm_port);
373 if (po_append(options, new_option) == PO_FAILED)
386 * Retry an NFS mount that failed because the requested service isn't
387 * available on the server.
389 * Returns 1 if successful. Otherwise, returns zero.
390 * "errno" is set to reflect the individual error.
392 * Side effect: If the retry is successful, both 'options' and
393 * 'extra_opts' are updated to reflect the mount options that worked.
394 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
396 static int retry_nfsmount(const char *spec, const char *node,
397 int flags, struct mount_options *options,
398 int fake, char **extra_opts)
400 struct mount_options *retry_options;
401 char *retry_str = NULL;
403 retry_options = rewrite_mount_options(*extra_opts);
404 if (!retry_options) {
409 if (po_join(retry_options, &retry_str) == PO_FAILED) {
410 po_destroy(retry_options);
416 printf(_("%s: text-based options (retry): '%s'\n"),
417 progname, retry_str);
419 if (!mount(spec, node, "nfs",
420 flags & ~(MS_USER|MS_USERS), retry_str)) {
422 *extra_opts = retry_str;
423 po_replace(options, retry_options);
427 po_destroy(retry_options);
433 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
434 * claims the requested service isn't supported on the server, probe
435 * the server to see what's supported, rewrite the mount options,
436 * and retry the request.
438 * Returns 1 if successful. Otherwise, returns zero.
439 * "errno" is set to reflect the individual error.
441 * Side effect: If the retry is successful, both 'options' and
442 * 'extra_opts' are updated to reflect the mount options that worked.
443 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
445 static int try_nfs23mount(const char *spec, const char *node,
446 int flags, struct mount_options *options,
447 int fake, char **extra_opts)
449 if (po_join(options, extra_opts) == PO_FAILED) {
455 printf(_("%s: text-based options: '%s'\n"),
456 progname, *extra_opts);
461 if (!mount(spec, node, "nfs",
462 flags & ~(MS_USER|MS_USERS), *extra_opts))
466 * The kernel returns EOPNOTSUPP if the RPC bind failed,
467 * and EPROTONOSUPPORT if the version isn't supported.
469 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
472 return retry_nfsmount(spec, node, flags, options, fake, extra_opts);
476 * Attempt an NFS v4 mount via a mount(2) system call.
478 * Returns 1 if successful. Otherwise, returns zero.
479 * "errno" is set to reflect the individual error.
481 static int try_nfs4mount(const char *spec, const char *node,
482 int flags, struct mount_options *options,
483 int fake, char **extra_opts)
485 if (po_join(options, extra_opts) == PO_FAILED) {
491 printf(_("%s: text-based options: '%s'\n"),
492 progname, *extra_opts);
497 if (!mount(spec, node, "nfs4",
498 flags & ~(MS_USER|MS_USERS), *extra_opts))
504 * Try the mount(2) system call.
506 * Returns 1 if successful. Otherwise, returns zero.
507 * "errno" is set to reflect the individual error.
509 static int try_mount(const char *spec, const char *node, const char *type,
510 int flags, struct mount_options *options, int fake,
513 if (strncmp(type, "nfs4", 4) == 0)
514 return try_nfs4mount(spec, node, flags,
515 options, fake, extra_opts);
517 return try_nfs23mount(spec, node, flags,
518 options, fake, extra_opts);
522 * Handle "foreground" NFS mounts.
524 * Retry the mount request for as long as the 'retry=' option says.
526 * Returns a valid mount command exit code.
528 static int nfsmount_fg(const char *spec, const char *node,
529 const char *type, int flags,
530 struct mount_options *options, int fake,
533 unsigned int secs = 1;
534 time_t timeout = time(NULL);
537 timeout += 60 * 2; /* default: 2 minutes */
538 retry = po_get(options, "retry");
540 timeout += 60 * atoi(retry);
543 printf(_("%s: timeout set for %s"),
544 progname, ctime(&timeout));
547 if (try_mount(spec, node, type, flags,
548 options, fake, extra_opts))
551 if (is_permanent_error(errno))
554 if (time(NULL) > timeout) {
559 if (errno != ETIMEDOUT) {
568 mount_error(spec, node, errno);
573 * Handle "background" NFS mount [first try]
575 * Returns a valid mount command exit code.
577 * EX_BG should cause the caller to fork and invoke nfsmount_child.
579 static int nfsmount_parent(const char *spec, const char *node,
580 const char *type, char *hostname, int flags,
581 struct mount_options *options,
582 int fake, char **extra_opts)
584 if (try_mount(spec, node, type, flags, options,
588 if (is_permanent_error(errno)) {
589 mount_error(spec, node, errno);
593 sys_mount_errors(hostname, errno, 1, 1);
598 * Handle "background" NFS mount [retry daemon]
600 * Returns a valid mount command exit code: EX_SUCCESS if successful,
601 * EX_FAIL if a failure occurred. There's nothing to catch the
602 * error return, though, so we use sys_mount_errors to log the
605 static int nfsmount_child(const char *spec, const char *node,
606 const char *type, char *hostname, int flags,
607 struct mount_options *options,
608 int fake, char **extra_opts)
610 unsigned int secs = 1;
611 time_t timeout = time(NULL);
614 timeout += 60 * 10000; /* default: 10,000 minutes */
615 retry = po_get(options, "retry");
617 timeout += 60 * atoi(retry);
626 if (try_mount(spec, node, type, flags, options,
630 if (is_permanent_error(errno))
633 if (time(NULL) > timeout)
636 sys_mount_errors(hostname, errno, 1, 1);
639 sys_mount_errors(hostname, errno, 1, 0);
644 * Handle "background" NFS mount
646 * Returns a valid mount command exit code.
648 static int nfsmount_bg(const char *spec, const char *node,
649 const char *type, char *hostname, int flags,
650 struct mount_options *options,
651 int fake, int child, char **extra_opts)
654 return nfsmount_parent(spec, node, type, hostname, flags,
655 options, fake, extra_opts);
657 return nfsmount_child(spec, node, type, hostname, flags,
658 options, fake, extra_opts);
662 * nfsmount_string - Mount an NFS file system using C string options
663 * @spec: C string specifying remote share to mount ("hostname:path")
664 * @node: C string pathname of local mounted-on directory
665 * @type: C string that represents file system type ("nfs" or "nfs4")
666 * @flags: MS_ style mount flags
667 * @extra_opts: pointer to C string containing fs-specific mount options
668 * (input and output argument)
669 * @fake: flag indicating whether to carry out the whole operation
670 * @child: one if this is a mount daemon (bg)
672 int nfsmount_string(const char *spec, const char *node, const char *type,
673 int flags, char **extra_opts, int fake, int child)
675 struct mount_options *options = NULL;
676 struct sockaddr_in saddr;
678 int retval = EX_FAIL;
680 if (!parse_devname(spec, &hostname))
682 if (!fill_ipv4_sockaddr(hostname, &saddr))
685 options = po_split(*extra_opts);
687 nfs_error(_("%s: internal option parsing error"), progname);
691 if (!set_mandatory_options(type, &saddr, options))
694 if (po_rightmost(options, "bg", "fg") == PO_KEY1_RIGHTMOST)
695 retval = nfsmount_bg(spec, node, type, hostname, flags,
696 options, fake, child, extra_opts);
698 retval = nfsmount_fg(spec, node, type, flags, options,