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 unsigned long version; /* NFS version */
88 int flags, /* MS_ flags */
89 fake, /* actually do the mount? */
90 child; /* forked bg child? */
94 * Obtain a retry timeout value based on the value of the "retry=" option.
96 * Returns a time_t timeout timestamp, in seconds.
98 static time_t nfs_parse_retry_option(struct mount_options *options,
99 unsigned int timeout_minutes)
103 switch (po_get_numeric(options, "retry", &tmp)) {
108 timeout_minutes = tmp;
113 nfs_error(_("%s: invalid retry timeout was specified; "
114 "using default timeout"), progname);
118 return time(NULL) + (time_t)(timeout_minutes * 60);
122 * Convert the passed-in sockaddr-style address to presentation
123 * format, then append an option of the form "keyword=address".
125 * Returns 1 if the option was appended successfully; otherwise zero.
127 static int nfs_append_generic_address_option(const struct sockaddr *sap,
128 const socklen_t salen,
130 struct mount_options *options)
132 char address[NI_MAXHOST];
133 char new_option[512];
136 if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
139 len = snprintf(new_option, sizeof(new_option), "%s=%s",
141 if (len < 0 || (size_t)len >= sizeof(new_option))
144 if (po_append(options, new_option) != PO_SUCCEEDED)
150 nfs_error(_("%s: failed to construct %s option"), progname, keyword);
155 * Append the 'addr=' option to the options string to pass a resolved
156 * server address to the kernel. After a successful mount, this address
157 * is also added to /etc/mtab for use when unmounting.
159 * If 'addr=' is already present, we strip it out. This prevents users
160 * from setting a bogus 'addr=' option themselves, and also allows bg
161 * retries to recompute the server's address, in case it has changed.
163 * Returns 1 if 'addr=' option appended successfully;
166 static int nfs_append_addr_option(const struct sockaddr *sap,
168 struct mount_options *options)
170 po_remove_all(options, "addr");
171 return nfs_append_generic_address_option(sap, salen, "addr", options);
175 * Called to discover our address and append an appropriate 'clientaddr='
176 * option to the options string.
178 * Returns 1 if 'clientaddr=' option created successfully or if
179 * 'clientaddr=' option is already present; otherwise zero.
181 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
183 struct mount_options *options)
185 struct sockaddr_storage dummy;
186 struct sockaddr *my_addr = (struct sockaddr *)&dummy;
187 socklen_t my_len = sizeof(dummy);
189 if (po_contains(options, "clientaddr") == PO_FOUND)
192 nfs_callback_address(sap, salen, my_addr, &my_len);
194 return nfs_append_generic_address_option(my_addr, my_len,
195 "clientaddr", options);
199 * Resolve the 'mounthost=' hostname and append a new option using
200 * the resulting address.
202 static int nfs_fix_mounthost_option(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, 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) == 0)
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, sap, &salen))
276 if (!nfs_nfs_version(mi->options, &mi->version))
278 if (strncmp(mi->type, "nfs4", 4) == 0)
281 if (mi->version == 4) {
282 if (!nfs_append_clientaddr_option(sap, salen, mi->options))
285 if (!nfs_fix_mounthost_option(mi->options))
287 if (!mi->fake && !nfs_verify_lock_option(mi->options))
291 if (!nfs_append_sloppy_option(mi->options))
294 if (!nfs_append_addr_option(sap, salen, mi->options))
298 * Update option string to be recorded in /etc/mnttab
300 if (po_join(mi->options, mi->extra_opts) == PO_FAILED)
307 * Get NFS/mnt server addresses from mount options
309 * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
310 * if all goes well; otherwise zero.
312 static int nfs_extract_server_addresses(struct mount_options *options,
313 struct sockaddr *nfs_saddr,
314 socklen_t *nfs_salen,
315 struct sockaddr *mnt_saddr,
316 socklen_t *mnt_salen)
320 option = po_get(options, "addr");
323 if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
326 option = po_get(options, "mountaddr");
327 if (option == NULL) {
328 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
329 *mnt_salen = *nfs_salen;
330 } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
336 static int nfs_construct_new_options(struct mount_options *options,
337 struct pmap *nfs_pmap,
338 struct pmap *mnt_pmap)
342 po_remove_all(options, "nfsprog");
343 po_remove_all(options, "mountprog");
345 po_remove_all(options, "v2");
346 po_remove_all(options, "v3");
347 po_remove_all(options, "vers");
348 po_remove_all(options, "nfsvers");
349 snprintf(new_option, sizeof(new_option) - 1,
350 "vers=%lu", nfs_pmap->pm_vers);
351 if (po_append(options, new_option) == PO_FAILED)
354 po_remove_all(options, "proto");
355 po_remove_all(options, "udp");
356 po_remove_all(options, "tcp");
357 switch (nfs_pmap->pm_prot) {
359 snprintf(new_option, sizeof(new_option) - 1,
361 if (po_append(options, new_option) == PO_FAILED)
365 snprintf(new_option, sizeof(new_option) - 1,
367 if (po_append(options, new_option) == PO_FAILED)
372 po_remove_all(options, "port");
373 if (nfs_pmap->pm_port != NFS_PORT) {
374 snprintf(new_option, sizeof(new_option) - 1,
375 "port=%lu", nfs_pmap->pm_port);
376 if (po_append(options, new_option) == PO_FAILED)
380 po_remove_all(options, "mountvers");
381 snprintf(new_option, sizeof(new_option) - 1,
382 "mountvers=%lu", mnt_pmap->pm_vers);
383 if (po_append(options, new_option) == PO_FAILED)
386 po_remove_all(options, "mountproto");
387 switch (mnt_pmap->pm_prot) {
389 snprintf(new_option, sizeof(new_option) - 1,
391 if (po_append(options, new_option) == PO_FAILED)
395 snprintf(new_option, sizeof(new_option) - 1,
397 if (po_append(options, new_option) == PO_FAILED)
402 po_remove_all(options, "mountport");
403 snprintf(new_option, sizeof(new_option) - 1,
404 "mountport=%lu", mnt_pmap->pm_port);
405 if (po_append(options, new_option) == PO_FAILED)
412 * Reconstruct the mount option string based on a portmapper probe
413 * of the server. Returns one if the server's portmapper returned
414 * something we can use, otherwise zero.
416 * To handle version and transport protocol fallback properly, we
417 * need to parse some of the mount options in order to set up a
418 * portmap probe. Mount options that nfs_rewrite_pmap_mount_options()
419 * doesn't recognize are left alone.
421 * Returns TRUE if rewriting was successful; otherwise
422 * FALSE is returned if some failure occurred.
425 nfs_rewrite_pmap_mount_options(struct mount_options *options)
427 struct sockaddr_storage nfs_address;
428 struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
429 socklen_t nfs_salen = sizeof(nfs_address);
430 struct pmap nfs_pmap;
431 struct sockaddr_storage mnt_address;
432 struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
433 socklen_t mnt_salen = sizeof(mnt_address);
434 struct pmap mnt_pmap;
438 * Skip option negotiation for proto=rdma mounts.
440 option = po_get(options, "proto");
441 if (option && strcmp(option, "rdma") == 0)
445 * Extract just the options needed to contact server.
446 * Bail now if any of these have bad values.
448 if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
449 mnt_saddr, &mnt_salen)) {
453 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
459 * The kernel NFS client doesn't support changing the RPC
460 * program number for these services, so force the value of
461 * these fields before probing the server's ports.
463 nfs_pmap.pm_prog = NFS_PROGRAM;
464 mnt_pmap.pm_prog = MOUNTPROG;
467 * If the server's rpcbind service isn't available, we can't
468 * negotiate. Bail now if we can't contact it.
470 if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
471 nfs_saddr, nfs_salen, &nfs_pmap)) {
476 if (!nfs_construct_new_options(options, &nfs_pmap, &mnt_pmap)) {
487 * Do the mount(2) system call.
489 * Returns TRUE if successful, otherwise FALSE.
490 * "errno" is set to reflect the individual error.
492 static int nfs_try_mount(struct nfsmount_info *mi)
494 char *options = NULL;
497 if (mi->version != 4) {
498 if (!nfs_rewrite_pmap_mount_options(mi->options))
502 if (po_join(mi->options, &options) == PO_FAILED) {
508 printf(_("%s: trying text-based options '%s'\n"),
514 result = mount(mi->spec, mi->node, mi->type,
515 mi->flags & ~(MS_USER|MS_USERS), options);
516 if (verbose && result) {
518 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
525 * Distinguish between permanent and temporary errors.
527 * Basically, we retry if communication with the server has
528 * failed so far, but fail immediately if there is a local
529 * error (like a bad mount option).
531 * ESTALE is also a temporary error because some servers
532 * return ESTALE when a share is temporarily offline.
534 * Returns 1 if we should fail immediately, or 0 if we
537 static int nfs_is_permanent_error(int error)
543 return 0; /* temporary */
545 return 1; /* permanent */
550 * Handle "foreground" NFS mounts.
552 * Retry the mount request for as long as the 'retry=' option says.
554 * Returns a valid mount command exit code.
556 static int nfsmount_fg(struct nfsmount_info *mi)
558 unsigned int secs = 1;
561 timeout = nfs_parse_retry_option(mi->options,
562 NFS_DEF_FG_TIMEOUT_MINUTES);
564 printf(_("%s: timeout set for %s"),
565 progname, ctime(&timeout));
568 if (nfs_try_mount(mi))
571 if (nfs_is_permanent_error(errno))
574 if (time(NULL) > timeout) {
579 if (errno != ETIMEDOUT) {
588 mount_error(mi->spec, mi->node, errno);
593 * Handle "background" NFS mount [first try]
595 * Returns a valid mount command exit code.
597 * EX_BG should cause the caller to fork and invoke nfsmount_child.
599 static int nfsmount_parent(struct nfsmount_info *mi)
601 if (nfs_try_mount(mi))
604 if (nfs_is_permanent_error(errno)) {
605 mount_error(mi->spec, mi->node, errno);
609 sys_mount_errors(mi->hostname, errno, 1, 1);
614 * Handle "background" NFS mount [retry daemon]
616 * Returns a valid mount command exit code: EX_SUCCESS if successful,
617 * EX_FAIL if a failure occurred. There's nothing to catch the
618 * error return, though, so we use sys_mount_errors to log the
621 static int nfsmount_child(struct nfsmount_info *mi)
623 unsigned int secs = 1;
626 timeout = nfs_parse_retry_option(mi->options,
627 NFS_DEF_BG_TIMEOUT_MINUTES);
636 if (nfs_try_mount(mi))
639 if (nfs_is_permanent_error(errno))
642 if (time(NULL) > timeout)
645 sys_mount_errors(mi->hostname, errno, 1, 1);
648 sys_mount_errors(mi->hostname, errno, 1, 0);
653 * Handle "background" NFS mount
655 * Returns a valid mount command exit code.
657 static int nfsmount_bg(struct nfsmount_info *mi)
660 return nfsmount_parent(mi);
662 return nfsmount_child(mi);
666 * Process mount options and try a mount system call.
668 * Returns a valid mount command exit code.
670 static const char *nfs_background_opttbl[] = {
676 static int nfsmount_start(struct nfsmount_info *mi)
678 if (!nfs_validate_options(mi))
681 if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
682 return nfsmount_bg(mi);
684 return nfsmount_fg(mi);
688 * nfsmount_string - Mount an NFS file system using C string options
689 * @spec: C string specifying remote share to mount ("hostname:path")
690 * @node: C string pathname of local mounted-on directory
691 * @type: C string that represents file system type ("nfs" or "nfs4")
692 * @flags: MS_ style mount flags
693 * @extra_opts: pointer to C string containing fs-specific mount options
694 * (input and output argument)
695 * @fake: flag indicating whether to carry out the whole operation
696 * @child: one if this is a mount daemon (bg)
698 int nfsmount_string(const char *spec, const char *node, const char *type,
699 int flags, char **extra_opts, int fake, int child)
701 struct nfsmount_info mi = {
705 .extra_opts = extra_opts,
710 int retval = EX_FAIL;
712 mi.options = po_split(*extra_opts);
714 retval = nfsmount_start(&mi);
715 po_destroy(mi.options);
717 nfs_error(_("%s: internal option parsing error"), progname);