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)
104 switch (po_get_numeric(options, "retry", &tmp)) {
109 timeout_minutes = tmp;
114 nfs_error(_("%s: invalid retry timeout was specified; "
115 "using default timeout"), progname);
119 return time(NULL) + (time_t)(timeout_minutes * 60);
123 * Convert the passed-in sockaddr-style address to presentation
124 * format, then append an option of the form "keyword=address".
126 * Returns 1 if the option was appended successfully; otherwise zero.
128 static int nfs_append_generic_address_option(const struct sockaddr *sap,
129 const socklen_t salen,
131 struct mount_options *options)
133 char address[NI_MAXHOST];
134 char new_option[512];
136 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
139 if (snprintf(new_option, sizeof(new_option), "%s=%s",
140 keyword, address) >= sizeof(new_option))
143 if (po_append(options, new_option) != PO_SUCCEEDED)
149 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
154 * Append the 'addr=' option to the options string to pass a resolved
155 * server address to the kernel. After a successful mount, this address
156 * is also added to /etc/mtab for use when unmounting.
158 * If 'addr=' is already present, we strip it out. This prevents users
159 * from setting a bogus 'addr=' option themselves, and also allows bg
160 * retries to recompute the server's address, in case it has changed.
162 * Returns 1 if 'addr=' option appended successfully;
165 static int nfs_append_addr_option(const struct sockaddr *sap,
167 struct mount_options *options)
169 po_remove_all(options, "addr");
170 return nfs_append_generic_address_option(sap, salen, "addr", options);
174 * Called to discover our address and append an appropriate 'clientaddr='
175 * option to the options string.
177 * Returns 1 if 'clientaddr=' option created successfully or if
178 * 'clientaddr=' option is already present; otherwise zero.
180 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
182 struct mount_options *options)
184 struct sockaddr_storage dummy;
185 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
186 socklen_t my_len = sizeof(dummy);
188 if (po_contains(options, "clientaddr") == PO_FOUND)
191 nfs_callback_address(sap, salen, my_addr, &my_len);
193 return nfs_append_generic_address_option(my_addr, my_len,
194 "clientaddr", options);
198 * Resolve the 'mounthost=' hostname and append a new option using
199 * the resulting address.
201 static int nfs_fix_mounthost_option(const sa_family_t family,
202 struct mount_options *options)
204 struct sockaddr_storage dummy;
205 struct sockaddr *sap = (struct sockaddr *)&dummy;
206 socklen_t salen = sizeof(dummy);
209 mounthost = po_get(options, "mounthost");
213 if (!nfs_name_to_address(mounthost, family, sap, &salen)) {
214 nfs_error(_("%s: unable to determine mount server's address"),
219 return nfs_append_generic_address_option(sap, salen,
220 "mountaddr", options);
224 * Returns zero if the "lock" option is in effect, but statd
225 * can't be started. Otherwise, returns 1.
227 static const char *nfs_lock_opttbl[] = {
233 static int nfs_verify_lock_option(struct mount_options *options)
235 if (po_rightmost(options, nfs_lock_opttbl) == 1)
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 && !nfs_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 nfs_is_permanent_error(int error)
306 return 0; /* temporary */
308 return 1; /* permanent */
312 static int nfs_construct_new_options(struct mount_options *options,
313 struct pmap *nfs_pmap,
314 struct pmap *mnt_pmap)
318 po_remove_all(options, "nfsprog");
319 po_remove_all(options, "mountprog");
321 po_remove_all(options, "v2");
322 po_remove_all(options, "v3");
323 po_remove_all(options, "vers");
324 po_remove_all(options, "nfsvers");
325 snprintf(new_option, sizeof(new_option) - 1,
326 "vers=%lu", nfs_pmap->pm_vers);
327 if (po_append(options, new_option) == PO_FAILED)
330 po_remove_all(options, "proto");
331 po_remove_all(options, "udp");
332 po_remove_all(options, "tcp");
333 switch (nfs_pmap->pm_prot) {
335 snprintf(new_option, sizeof(new_option) - 1,
337 if (po_append(options, new_option) == PO_FAILED)
341 snprintf(new_option, sizeof(new_option) - 1,
343 if (po_append(options, new_option) == PO_FAILED)
348 po_remove_all(options, "port");
349 if (nfs_pmap->pm_port != NFS_PORT) {
350 snprintf(new_option, sizeof(new_option) - 1,
351 "port=%lu", nfs_pmap->pm_port);
352 if (po_append(options, new_option) == PO_FAILED)
356 po_remove_all(options, "mountvers");
357 snprintf(new_option, sizeof(new_option) - 1,
358 "mountvers=%lu", mnt_pmap->pm_vers);
359 if (po_append(options, new_option) == PO_FAILED)
362 po_remove_all(options, "mountproto");
363 switch (mnt_pmap->pm_prot) {
365 snprintf(new_option, sizeof(new_option) - 1,
367 if (po_append(options, new_option) == PO_FAILED)
371 snprintf(new_option, sizeof(new_option) - 1,
373 if (po_append(options, new_option) == PO_FAILED)
378 po_remove_all(options, "mountport");
379 snprintf(new_option, sizeof(new_option) - 1,
380 "mountport=%lu", mnt_pmap->pm_port);
381 if (po_append(options, new_option) == PO_FAILED)
388 * Reconstruct the mount option string based on a portmapper probe
389 * of the server. Returns one if the server's portmapper returned
390 * something we can use, otherwise zero.
392 * To handle version and transport protocol fallback properly, we
393 * need to parse some of the mount options in order to set up a
394 * portmap probe. Mount options that nfs_rewrite_mount_options()
395 * doesn't recognize are left alone.
397 * Returns a new group of mount options if successful; otherwise
398 * NULL is returned if some failure occurred.
400 static struct mount_options *nfs_rewrite_mount_options(char *str)
402 struct mount_options *options;
404 clnt_addr_t mnt_server = { };
405 clnt_addr_t nfs_server = { };
407 options = po_split(str);
414 option = po_get(options, "addr");
416 nfs_server.saddr.sin_family = AF_INET;
417 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
422 option = po_get(options, "mountaddr");
424 mnt_server.saddr.sin_family = AF_INET;
425 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
428 memcpy(&mnt_server.saddr, &nfs_server.saddr,
429 sizeof(mnt_server.saddr));
431 nfs_options2pmap(options, &nfs_server.pmap, &mnt_server.pmap);
433 /* The kernel NFS client doesn't support changing the RPC program
434 * number for these services, so reset these fields before probing
435 * the server's ports. */
436 nfs_server.pmap.pm_prog = NFS_PROGRAM;
437 mnt_server.pmap.pm_prog = MOUNTPROG;
439 if (!probe_bothports(&mnt_server, &nfs_server)) {
444 if (!nfs_construct_new_options(options,
445 &nfs_server.pmap, &mnt_server.pmap)) {
459 * Do the mount(2) system call.
461 * Returns 1 if successful, otherwise zero.
462 * "errno" is set to reflect the individual error.
464 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
469 result = mount(mi->spec, mi->node, type,
470 mi->flags & ~(MS_USER|MS_USERS), options);
471 if (verbose && result) {
473 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
480 * Retry an NFS mount that failed because the requested service isn't
481 * available on the server.
483 * Returns 1 if successful. Otherwise, returns zero.
484 * "errno" is set to reflect the individual error.
486 * Side effect: If the retry is successful, both 'options' and
487 * 'extra_opts' are updated to reflect the mount options that worked.
488 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
490 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
492 struct mount_options *retry_options;
493 char *retry_str = NULL;
494 char **extra_opts = mi->extra_opts;
496 retry_options = nfs_rewrite_mount_options(*extra_opts);
500 if (po_join(retry_options, &retry_str) == PO_FAILED) {
501 po_destroy(retry_options);
507 printf(_("%s: text-based options (retry): '%s'\n"),
508 progname, retry_str);
510 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
511 po_destroy(retry_options);
517 *extra_opts = retry_str;
518 po_replace(mi->options, retry_options);
523 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
524 * claims the requested service isn't supported on the server, probe
525 * the server to see what's supported, rewrite the mount options,
526 * and retry the request.
528 * Returns 1 if successful. Otherwise, returns zero.
529 * "errno" is set to reflect the individual error.
531 * Side effect: If the retry is successful, both 'options' and
532 * 'extra_opts' are updated to reflect the mount options that worked.
533 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
535 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
537 char **extra_opts = mi->extra_opts;
539 if (po_join(mi->options, extra_opts) == PO_FAILED) {
545 printf(_("%s: text-based options: '%s'\n"),
546 progname, *extra_opts);
551 if (nfs_sys_mount(mi, "nfs", *extra_opts))
555 * The kernel returns EOPNOTSUPP if the RPC bind failed,
556 * and EPROTONOSUPPORT if the version isn't supported.
558 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
561 return nfs_retry_nfs23mount(mi);
565 * Attempt an NFS v4 mount via a mount(2) system call.
567 * Returns 1 if successful. Otherwise, returns zero.
568 * "errno" is set to reflect the individual error.
570 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
572 char **extra_opts = mi->extra_opts;
574 if (po_join(mi->options, extra_opts) == PO_FAILED) {
580 printf(_("%s: text-based options: '%s'\n"),
581 progname, *extra_opts);
586 return nfs_sys_mount(mi, "nfs4", *extra_opts);
590 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
592 * Returns 1 if successful. Otherwise, returns zero.
593 * "errno" is set to reflect the individual error.
595 static int nfs_try_mount(struct nfsmount_info *mi)
597 if (strncmp(mi->type, "nfs4", 4) == 0)
598 return nfs_try_nfs4mount(mi);
600 return nfs_try_nfs23mount(mi);
604 * Handle "foreground" NFS mounts.
606 * Retry the mount request for as long as the 'retry=' option says.
608 * Returns a valid mount command exit code.
610 static int nfsmount_fg(struct nfsmount_info *mi)
612 unsigned int secs = 1;
615 timeout = nfs_parse_retry_option(mi->options,
616 NFS_DEF_FG_TIMEOUT_MINUTES);
618 printf(_("%s: timeout set for %s"),
619 progname, ctime(&timeout));
622 if (nfs_try_mount(mi))
625 if (nfs_is_permanent_error(errno))
628 if (time(NULL) > timeout) {
633 if (errno != ETIMEDOUT) {
642 mount_error(mi->spec, mi->node, errno);
647 * Handle "background" NFS mount [first try]
649 * Returns a valid mount command exit code.
651 * EX_BG should cause the caller to fork and invoke nfsmount_child.
653 static int nfsmount_parent(struct nfsmount_info *mi)
655 if (nfs_try_mount(mi))
658 if (nfs_is_permanent_error(errno)) {
659 mount_error(mi->spec, mi->node, errno);
663 sys_mount_errors(mi->hostname, errno, 1, 1);
668 * Handle "background" NFS mount [retry daemon]
670 * Returns a valid mount command exit code: EX_SUCCESS if successful,
671 * EX_FAIL if a failure occurred. There's nothing to catch the
672 * error return, though, so we use sys_mount_errors to log the
675 static int nfsmount_child(struct nfsmount_info *mi)
677 unsigned int secs = 1;
680 timeout = nfs_parse_retry_option(mi->options,
681 NFS_DEF_BG_TIMEOUT_MINUTES);
690 if (nfs_try_mount(mi))
693 if (nfs_is_permanent_error(errno))
696 if (time(NULL) > timeout)
699 sys_mount_errors(mi->hostname, errno, 1, 1);
702 sys_mount_errors(mi->hostname, errno, 1, 0);
707 * Handle "background" NFS mount
709 * Returns a valid mount command exit code.
711 static int nfsmount_bg(struct nfsmount_info *mi)
714 return nfsmount_parent(mi);
716 return nfsmount_child(mi);
720 * Process mount options and try a mount system call.
722 * Returns a valid mount command exit code.
724 static const char *nfs_background_opttbl[] = {
730 static int nfsmount_start(struct nfsmount_info *mi)
732 if (!nfs_validate_options(mi))
735 if (po_rightmost(mi->options, nfs_background_opttbl) == 1)
736 return nfsmount_bg(mi);
738 return nfsmount_fg(mi);
742 * nfsmount_string - Mount an NFS file system using C string options
743 * @spec: C string specifying remote share to mount ("hostname:path")
744 * @node: C string pathname of local mounted-on directory
745 * @type: C string that represents file system type ("nfs" or "nfs4")
746 * @flags: MS_ style mount flags
747 * @extra_opts: pointer to C string containing fs-specific mount options
748 * (input and output argument)
749 * @fake: flag indicating whether to carry out the whole operation
750 * @child: one if this is a mount daemon (bg)
752 int nfsmount_string(const char *spec, const char *node, const char *type,
753 int flags, char **extra_opts, int fake, int child)
755 struct nfsmount_info mi = {
759 .extra_opts = extra_opts,
763 #ifdef IPV6_SUPPORTED
764 .family = AF_UNSPEC, /* either IPv4 or v6 */
766 .family = AF_INET, /* only IPv4 */
769 int retval = EX_FAIL;
771 mi.options = po_split(*extra_opts);
773 retval = nfsmount_start(&mi);
774 po_destroy(mi.options);
776 nfs_error(_("%s: internal option parsing error"), progname);