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? */
93 * Obtain a retry timeout value based on the value of the "retry=" option.
95 * Returns a time_t timeout timestamp, in seconds.
97 static time_t nfs_parse_retry_option(struct mount_options *options,
98 unsigned int timeout_minutes)
102 switch (po_get_numeric(options, "retry", &tmp)) {
107 timeout_minutes = tmp;
112 nfs_error(_("%s: invalid retry timeout was specified; "
113 "using default timeout"), progname);
117 return time(NULL) + (time_t)(timeout_minutes * 60);
121 * Convert the passed-in sockaddr-style address to presentation
122 * format, then append an option of the form "keyword=address".
124 * Returns 1 if the option was appended successfully; otherwise zero.
126 static int nfs_append_generic_address_option(const struct sockaddr *sap,
127 const socklen_t salen,
129 struct mount_options *options)
131 char address[NI_MAXHOST];
132 char new_option[512];
134 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
137 if (snprintf(new_option, sizeof(new_option), "%s=%s",
138 keyword, address) >= sizeof(new_option))
141 if (po_append(options, new_option) != PO_SUCCEEDED)
147 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
152 * Append the 'addr=' option to the options string to pass a resolved
153 * server address to the kernel. After a successful mount, this address
154 * is also added to /etc/mtab for use when unmounting.
156 * If 'addr=' is already present, we strip it out. This prevents users
157 * from setting a bogus 'addr=' option themselves, and also allows bg
158 * retries to recompute the server's address, in case it has changed.
160 * Returns 1 if 'addr=' option appended successfully;
163 static int nfs_append_addr_option(const struct sockaddr *sap,
165 struct mount_options *options)
167 po_remove_all(options, "addr");
168 return nfs_append_generic_address_option(sap, salen, "addr", options);
172 * Called to discover our address and append an appropriate 'clientaddr='
173 * option to the options string.
175 * Returns 1 if 'clientaddr=' option created successfully or if
176 * 'clientaddr=' option is already present; otherwise zero.
178 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
180 struct mount_options *options)
182 struct sockaddr_storage dummy;
183 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
184 socklen_t my_len = sizeof(dummy);
186 if (po_contains(options, "clientaddr") == PO_FOUND)
189 nfs_callback_address(sap, salen, my_addr, &my_len);
191 return nfs_append_generic_address_option(my_addr, my_len,
192 "clientaddr", options);
196 * Resolve the 'mounthost=' hostname and append a new option using
197 * the resulting address.
199 static int nfs_fix_mounthost_option(struct mount_options *options)
201 struct sockaddr_storage dummy;
202 struct sockaddr *sap = (struct sockaddr *)&dummy;
203 socklen_t salen = sizeof(dummy);
206 mounthost = po_get(options, "mounthost");
210 if (!nfs_name_to_address(mounthost, sap, &salen)) {
211 nfs_error(_("%s: unable to determine mount server's address"),
216 return nfs_append_generic_address_option(sap, salen,
217 "mountaddr", options);
221 * Returns zero if the "lock" option is in effect, but statd
222 * can't be started. Otherwise, returns 1.
224 static const char *nfs_lock_opttbl[] = {
230 static int nfs_verify_lock_option(struct mount_options *options)
232 if (po_rightmost(options, nfs_lock_opttbl) == 0)
235 if (!start_statd()) {
236 nfs_error(_("%s: rpc.statd is not running but is "
237 "required for remote locking."), progname);
238 nfs_error(_("%s: Either use '-o nolock' to keep "
239 "locks local, or start statd."), progname);
246 static int nfs_append_sloppy_option(struct mount_options *options)
248 if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
251 if (po_append(options, "sloppy") == PO_FAILED)
257 * Set up mandatory NFS mount options.
259 * Returns 1 if successful; otherwise zero.
261 static int nfs_validate_options(struct nfsmount_info *mi)
263 struct sockaddr_storage dummy;
264 struct sockaddr *sap = (struct sockaddr *)&dummy;
265 socklen_t salen = sizeof(dummy);
267 if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
270 if (!nfs_name_to_address(mi->hostname, sap, &salen))
273 if (strncmp(mi->type, "nfs4", 4) == 0) {
274 if (!nfs_append_clientaddr_option(sap, salen, mi->options))
277 if (!nfs_fix_mounthost_option(mi->options))
279 if (!mi->fake && !nfs_verify_lock_option(mi->options))
283 if (!nfs_append_sloppy_option(mi->options))
286 return nfs_append_addr_option(sap, salen, mi->options);
290 * Distinguish between permanent and temporary errors.
292 * Returns 0 if the passed-in error is temporary, thus the
293 * mount system call should be retried; returns one if the
294 * passed-in error is permanent, thus the mount system call
295 * should not be retried.
297 static int nfs_is_permanent_error(int error)
303 return 0; /* temporary */
305 return 1; /* permanent */
310 * Get NFS/mnt server addresses from mount options
312 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
313 * if all goes well; otherwise zero.
315 static int nfs_extract_server_addresses(struct mount_options *options,
316 struct sockaddr *nfs_saddr,
317 socklen_t *nfs_salen,
318 struct sockaddr *mnt_saddr,
319 socklen_t *mnt_salen)
323 option = po_get(options, "addr");
326 if (!nfs_string_to_sockaddr(option, strlen(option),
327 nfs_saddr, nfs_salen))
330 option = po_get(options, "mountaddr");
331 if (option == NULL) {
332 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
333 *mnt_salen = *nfs_salen;
334 } else if (!nfs_string_to_sockaddr(option, strlen(option),
335 mnt_saddr, mnt_salen))
341 static int nfs_construct_new_options(struct mount_options *options,
342 struct pmap *nfs_pmap,
343 struct pmap *mnt_pmap)
347 po_remove_all(options, "nfsprog");
348 po_remove_all(options, "mountprog");
350 po_remove_all(options, "v2");
351 po_remove_all(options, "v3");
352 po_remove_all(options, "vers");
353 po_remove_all(options, "nfsvers");
354 snprintf(new_option, sizeof(new_option) - 1,
355 "vers=%lu", nfs_pmap->pm_vers);
356 if (po_append(options, new_option) == PO_FAILED)
359 po_remove_all(options, "proto");
360 po_remove_all(options, "udp");
361 po_remove_all(options, "tcp");
362 switch (nfs_pmap->pm_prot) {
364 snprintf(new_option, sizeof(new_option) - 1,
366 if (po_append(options, new_option) == PO_FAILED)
370 snprintf(new_option, sizeof(new_option) - 1,
372 if (po_append(options, new_option) == PO_FAILED)
377 po_remove_all(options, "port");
378 if (nfs_pmap->pm_port != NFS_PORT) {
379 snprintf(new_option, sizeof(new_option) - 1,
380 "port=%lu", nfs_pmap->pm_port);
381 if (po_append(options, new_option) == PO_FAILED)
385 po_remove_all(options, "mountvers");
386 snprintf(new_option, sizeof(new_option) - 1,
387 "mountvers=%lu", mnt_pmap->pm_vers);
388 if (po_append(options, new_option) == PO_FAILED)
391 po_remove_all(options, "mountproto");
392 switch (mnt_pmap->pm_prot) {
394 snprintf(new_option, sizeof(new_option) - 1,
396 if (po_append(options, new_option) == PO_FAILED)
400 snprintf(new_option, sizeof(new_option) - 1,
402 if (po_append(options, new_option) == PO_FAILED)
407 po_remove_all(options, "mountport");
408 snprintf(new_option, sizeof(new_option) - 1,
409 "mountport=%lu", mnt_pmap->pm_port);
410 if (po_append(options, new_option) == PO_FAILED)
417 * Reconstruct the mount option string based on a portmapper probe
418 * of the server. Returns one if the server's portmapper returned
419 * something we can use, otherwise zero.
421 * To handle version and transport protocol fallback properly, we
422 * need to parse some of the mount options in order to set up a
423 * portmap probe. Mount options that nfs_rewrite_mount_options()
424 * doesn't recognize are left alone.
426 * Returns a new group of mount options if successful; otherwise
427 * NULL is returned if some failure occurred.
429 static struct mount_options *nfs_rewrite_mount_options(char *str)
431 struct mount_options *options;
432 struct sockaddr_storage nfs_address;
433 struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
435 struct pmap nfs_pmap;
436 struct sockaddr_storage mnt_address;
437 struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
439 struct pmap mnt_pmap;
441 options = po_split(str);
447 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
448 mnt_saddr, &mnt_salen)) {
453 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
458 /* The kernel NFS client doesn't support changing the RPC program
459 * number for these services, so reset these fields before probing
460 * the server's ports. */
461 nfs_pmap.pm_prog = NFS_PROGRAM;
462 mnt_pmap.pm_prog = MOUNTPROG;
464 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
465 nfs_saddr, nfs_salen, &nfs_pmap)) {
470 if (!nfs_construct_new_options(options, &nfs_pmap, &mnt_pmap)) {
484 * Do the mount(2) system call.
486 * Returns 1 if successful, otherwise zero.
487 * "errno" is set to reflect the individual error.
489 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
494 result = mount(mi->spec, mi->node, type,
495 mi->flags & ~(MS_USER|MS_USERS), options);
496 if (verbose && result) {
498 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
505 * Retry an NFS mount that failed because the requested service isn't
506 * available on the server.
508 * Returns 1 if successful. Otherwise, returns zero.
509 * "errno" is set to reflect the individual error.
511 * Side effect: If the retry is successful, both 'options' and
512 * 'extra_opts' are updated to reflect the mount options that worked.
513 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
515 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
517 struct mount_options *retry_options;
518 char *retry_str = NULL;
519 char **extra_opts = mi->extra_opts;
521 retry_options = nfs_rewrite_mount_options(*extra_opts);
525 if (po_join(retry_options, &retry_str) == PO_FAILED) {
526 po_destroy(retry_options);
532 printf(_("%s: text-based options (retry): '%s'\n"),
533 progname, retry_str);
535 if (!nfs_sys_mount(mi, "nfs", retry_str)) {
536 po_destroy(retry_options);
542 *extra_opts = retry_str;
543 po_replace(mi->options, retry_options);
548 * Attempt an NFSv2/3 mount via a mount(2) system call. If the kernel
549 * claims the requested service isn't supported on the server, probe
550 * the server to see what's supported, rewrite the mount options,
551 * and retry the request.
553 * Returns 1 if successful. Otherwise, returns zero.
554 * "errno" is set to reflect the individual error.
556 * Side effect: If the retry is successful, both 'options' and
557 * 'extra_opts' are updated to reflect the mount options that worked.
558 * If the retry fails, 'options' and 'extra_opts' are left unchanged.
560 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
562 char **extra_opts = mi->extra_opts;
564 if (po_join(mi->options, extra_opts) == PO_FAILED) {
570 printf(_("%s: text-based options: '%s'\n"),
571 progname, *extra_opts);
576 if (nfs_sys_mount(mi, "nfs", *extra_opts))
580 * The kernel returns EOPNOTSUPP if the RPC bind failed,
581 * and EPROTONOSUPPORT if the version isn't supported.
583 if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
586 return nfs_retry_nfs23mount(mi);
590 * Attempt an NFS v4 mount via a mount(2) system call.
592 * Returns 1 if successful. Otherwise, returns zero.
593 * "errno" is set to reflect the individual error.
595 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
597 char **extra_opts = mi->extra_opts;
599 if (po_join(mi->options, extra_opts) == PO_FAILED) {
605 printf(_("%s: text-based options: '%s'\n"),
606 progname, *extra_opts);
611 return nfs_sys_mount(mi, "nfs4", *extra_opts);
615 * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
617 * Returns 1 if successful. Otherwise, returns zero.
618 * "errno" is set to reflect the individual error.
620 static int nfs_try_mount(struct nfsmount_info *mi)
622 if (strncmp(mi->type, "nfs4", 4) == 0)
623 return nfs_try_nfs4mount(mi);
625 return nfs_try_nfs23mount(mi);
629 * Handle "foreground" NFS mounts.
631 * Retry the mount request for as long as the 'retry=' option says.
633 * Returns a valid mount command exit code.
635 static int nfsmount_fg(struct nfsmount_info *mi)
637 unsigned int secs = 1;
640 timeout = nfs_parse_retry_option(mi->options,
641 NFS_DEF_FG_TIMEOUT_MINUTES);
643 printf(_("%s: timeout set for %s"),
644 progname, ctime(&timeout));
647 if (nfs_try_mount(mi))
650 if (nfs_is_permanent_error(errno))
653 if (time(NULL) > timeout) {
658 if (errno != ETIMEDOUT) {
667 mount_error(mi->spec, mi->node, errno);
672 * Handle "background" NFS mount [first try]
674 * Returns a valid mount command exit code.
676 * EX_BG should cause the caller to fork and invoke nfsmount_child.
678 static int nfsmount_parent(struct nfsmount_info *mi)
680 if (nfs_try_mount(mi))
683 if (nfs_is_permanent_error(errno)) {
684 mount_error(mi->spec, mi->node, errno);
688 sys_mount_errors(mi->hostname, errno, 1, 1);
693 * Handle "background" NFS mount [retry daemon]
695 * Returns a valid mount command exit code: EX_SUCCESS if successful,
696 * EX_FAIL if a failure occurred. There's nothing to catch the
697 * error return, though, so we use sys_mount_errors to log the
700 static int nfsmount_child(struct nfsmount_info *mi)
702 unsigned int secs = 1;
705 timeout = nfs_parse_retry_option(mi->options,
706 NFS_DEF_BG_TIMEOUT_MINUTES);
715 if (nfs_try_mount(mi))
718 if (nfs_is_permanent_error(errno))
721 if (time(NULL) > timeout)
724 sys_mount_errors(mi->hostname, errno, 1, 1);
727 sys_mount_errors(mi->hostname, errno, 1, 0);
732 * Handle "background" NFS mount
734 * Returns a valid mount command exit code.
736 static int nfsmount_bg(struct nfsmount_info *mi)
739 return nfsmount_parent(mi);
741 return nfsmount_child(mi);
745 * Process mount options and try a mount system call.
747 * Returns a valid mount command exit code.
749 static const char *nfs_background_opttbl[] = {
755 static int nfsmount_start(struct nfsmount_info *mi)
757 if (!nfs_validate_options(mi))
760 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
761 return nfsmount_bg(mi);
763 return nfsmount_fg(mi);
767 * nfsmount_string - Mount an NFS file system using C string options
768 * @spec: C string specifying remote share to mount ("hostname:path")
769 * @node: C string pathname of local mounted-on directory
770 * @type: C string that represents file system type ("nfs" or "nfs4")
771 * @flags: MS_ style mount flags
772 * @extra_opts: pointer to C string containing fs-specific mount options
773 * (input and output argument)
774 * @fake: flag indicating whether to carry out the whole operation
775 * @child: one if this is a mount daemon (bg)
777 int nfsmount_string(const char *spec, const char *node, const char *type,
778 int flags, char **extra_opts, int fake, int child)
780 struct nfsmount_info mi = {
784 .extra_opts = extra_opts,
789 int retval = EX_FAIL;
791 mi.options = po_split(*extra_opts);
793 retval = nfsmount_start(&mi);
794 po_destroy(mi.options);
796 nfs_error(_("%s: internal option parsing error"), progname);