]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
9695c739c4f53844a65119b3dd049faca2b304ee
[nfs-utils.git] / utils / mount / stropts.c
1 /*
2  * stropts.c -- NFS mount using C string to pass options to kernel
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <unistd.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <time.h>
32
33 #include <sys/socket.h>
34 #include <sys/mount.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include "sockaddr.h"
39 #include "xcommon.h"
40 #include "mount.h"
41 #include "nls.h"
42 #include "nfsrpc.h"
43 #include "mount_constants.h"
44 #include "stropts.h"
45 #include "error.h"
46 #include "network.h"
47 #include "parse_opt.h"
48 #include "version.h"
49 #include "parse_dev.h"
50 #include "conffile.h"
51
52 #ifndef HAVE_DECL_AI_ADDRCONFIG
53 #define AI_ADDRCONFIG   0
54 #endif
55
56 #ifndef NFS_PROGRAM
57 #define NFS_PROGRAM     (100003)
58 #endif
59
60 #ifndef NFS_PORT
61 #define NFS_PORT        (2049)
62 #endif
63
64 #ifndef NFS_MAXHOSTNAME
65 #define NFS_MAXHOSTNAME         (255)
66 #endif
67
68 #ifndef NFS_MAXPATHNAME
69 #define NFS_MAXPATHNAME         (1024)
70 #endif
71
72 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
73 #define NFS_DEF_FG_TIMEOUT_MINUTES      (2u)
74 #endif
75
76 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
77 #define NFS_DEF_BG_TIMEOUT_MINUTES      (10000u)
78 #endif
79
80 extern int nfs_mount_data_version;
81 extern char *progname;
82 extern int verbose;
83 extern int sloppy;
84
85 struct nfsmount_info {
86         const char              *spec,          /* server:/path */
87                                 *node,          /* mounted-on dir */
88                                 *type;          /* "nfs" or "nfs4" */
89         char                    *hostname;      /* server's hostname */
90         struct addrinfo         *address;       /* server's addresses */
91
92         struct mount_options    *options;       /* parsed mount options */
93         char                    **extra_opts;   /* string for /etc/mtab */
94
95         unsigned long           version;        /* NFS version */
96         int                     flags,          /* MS_ flags */
97                                 fake,           /* actually do the mount? */
98                                 child;          /* forked bg child? */
99 };
100
101 #ifdef MOUNT_CONFIG
102 static void nfs_default_version(struct nfsmount_info *mi);
103
104 static void nfs_default_version(struct nfsmount_info *mi)
105 {
106         extern unsigned long config_default_vers;
107         /*
108          * Use the default value set in the config file when
109          * the version has not been explicitly set.
110          */
111         if (mi->version == 0 && config_default_vers) {
112                 if (config_default_vers < 4)
113                         mi->version = config_default_vers;
114         }
115 }
116 #else
117 inline void nfs_default_version(struct nfsmount_info *mi) {}
118 #endif /* MOUNT_CONFIG */
119
120 /*
121  * Obtain a retry timeout value based on the value of the "retry=" option.
122  *
123  * Returns a time_t timeout timestamp, in seconds.
124  */
125 static time_t nfs_parse_retry_option(struct mount_options *options,
126                                      unsigned int timeout_minutes)
127 {
128         long tmp;
129
130         switch (po_get_numeric(options, "retry", &tmp)) {
131         case PO_NOT_FOUND:
132                 break;
133         case PO_FOUND:
134                 if (tmp >= 0) {
135                         timeout_minutes = tmp;
136                         break;
137                 }
138         case PO_BAD_VALUE:
139                 if (verbose)
140                         nfs_error(_("%s: invalid retry timeout was specified; "
141                                         "using default timeout"), progname);
142                 break;
143         }
144
145         return time(NULL) + (time_t)(timeout_minutes * 60);
146 }
147
148 /*
149  * Convert the passed-in sockaddr-style address to presentation
150  * format, then append an option of the form "keyword=address".
151  *
152  * Returns 1 if the option was appended successfully; otherwise zero.
153  */
154 static int nfs_append_generic_address_option(const struct sockaddr *sap,
155                                              const socklen_t salen,
156                                              const char *keyword,
157                                              struct mount_options *options)
158 {
159         char address[NI_MAXHOST];
160         char new_option[512];
161         int len;
162
163         if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
164                 goto out_err;
165
166         len = snprintf(new_option, sizeof(new_option), "%s=%s",
167                                                 keyword, address);
168         if (len < 0 || (size_t)len >= sizeof(new_option))
169                 goto out_err;
170
171         if (po_append(options, new_option) != PO_SUCCEEDED)
172                 goto out_err;
173
174         return 1;
175
176 out_err:
177         nfs_error(_("%s: failed to construct %s option"), progname, keyword);
178         return 0;
179 }
180
181 /*
182  * Append the 'addr=' option to the options string to pass a resolved
183  * server address to the kernel.  After a successful mount, this address
184  * is also added to /etc/mtab for use when unmounting.
185  *
186  * If 'addr=' is already present, we strip it out.  This prevents users
187  * from setting a bogus 'addr=' option themselves, and also allows bg
188  * retries to recompute the server's address, in case it has changed.
189  *
190  * Returns 1 if 'addr=' option appended successfully;
191  * otherwise zero.
192  */
193 static int nfs_append_addr_option(const struct sockaddr *sap,
194                                   socklen_t salen,
195                                   struct mount_options *options)
196 {
197         po_remove_all(options, "addr");
198         return nfs_append_generic_address_option(sap, salen, "addr", options);
199 }
200
201 /*
202  * Called to discover our address and append an appropriate 'clientaddr='
203  * option to the options string.
204  *
205  * Returns 1 if 'clientaddr=' option created successfully or if
206  * 'clientaddr=' option is already present; otherwise zero.
207  */
208 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
209                                         socklen_t salen,
210                                         struct mount_options *options)
211 {
212         union nfs_sockaddr address;
213         struct sockaddr *my_addr = &address.sa;
214         socklen_t my_len = sizeof(address);
215
216         if (po_contains(options, "clientaddr") == PO_FOUND)
217                 return 1;
218
219         nfs_callback_address(sap, salen, my_addr, &my_len);
220
221         return nfs_append_generic_address_option(my_addr, my_len,
222                                                         "clientaddr", options);
223 }
224
225 /*
226  * Determine whether to append a 'mountaddr=' option.  The option is needed if:
227  *
228  *   1. "mounthost=" was specified, or
229  *   2. The address families for proto= and mountproto= are different.
230  */
231 static int nfs_fix_mounthost_option(struct mount_options *options,
232                 const char *nfs_hostname)
233 {
234         union nfs_sockaddr address;
235         struct sockaddr *sap = &address.sa;
236         socklen_t salen = sizeof(address);
237         sa_family_t nfs_family, mnt_family;
238         char *mounthost;
239
240         if (!nfs_nfs_proto_family(options, &nfs_family))
241                 return 0;
242         if (!nfs_mount_proto_family(options, &mnt_family))
243                 return 0;
244
245         mounthost = po_get(options, "mounthost");
246         if (mounthost == NULL) {
247                 if (nfs_family == mnt_family)
248                         return 1;
249                 mounthost = (char *)nfs_hostname;
250         }
251
252         if (!nfs_lookup(mounthost, mnt_family, sap, &salen)) {
253                 nfs_error(_("%s: unable to determine mount server's address"),
254                                 progname);
255                 return 0;
256         }
257
258         return nfs_append_generic_address_option(sap, salen,
259                                                         "mountaddr", options);
260 }
261
262 /*
263  * Returns zero if the "lock" option is in effect, but statd
264  * can't be started.  Otherwise, returns 1.
265  */
266 static const char *nfs_lock_opttbl[] = {
267         "nolock",
268         "lock",
269         NULL,
270 };
271
272 static int nfs_verify_lock_option(struct mount_options *options)
273 {
274         if (po_rightmost(options, nfs_lock_opttbl) == 0)
275                 return 1;
276
277         if (!start_statd()) {
278                 nfs_error(_("%s: rpc.statd is not running but is "
279                             "required for remote locking."), progname);
280                 nfs_error(_("%s: Either use '-o nolock' to keep "
281                             "locks local, or start statd."), progname);
282                 return 0;
283         }
284
285         return 1;
286 }
287
288 static int nfs_append_sloppy_option(struct mount_options *options)
289 {
290         if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
291                 return 1;
292
293         if (po_append(options, "sloppy") == PO_FAILED)
294                 return 0;
295         return 1;
296 }
297
298 static int nfs_set_version(struct nfsmount_info *mi)
299 {
300         if (!nfs_nfs_version(mi->options, &mi->version))
301                 return 0;
302
303         if (strncmp(mi->type, "nfs4", 4) == 0)
304                 mi->version = 4;
305         else {
306                 unsigned long protocol;
307                 if (!nfs_nfs_protocol(mi->options, &protocol))
308                         return 0;
309                 if (protocol == NFSPROTO_RDMA)
310                         mi->version = 3;
311         }
312
313         /*
314          * If we still don't know, check for version-specific
315          * mount options.
316          */
317         if (mi->version == 0) {
318                 if (po_contains(mi->options, "mounthost") ||
319                     po_contains(mi->options, "mountaddr") ||
320                     po_contains(mi->options, "mountvers") ||
321                     po_contains(mi->options, "mountproto"))
322                         mi->version = 3;
323         }
324
325         /*
326          * If enabled, see if the default version was
327          * set in the config file
328          */
329         nfs_default_version(mi);
330         
331         return 1;
332 }
333
334 /*
335  * Set up mandatory non-version specific NFS mount options.
336  *
337  * Returns 1 if successful; otherwise zero.
338  */
339 static int nfs_validate_options(struct nfsmount_info *mi)
340 {
341         struct addrinfo hint = {
342                 .ai_protocol    = (int)IPPROTO_UDP,
343                 .ai_flags       = AI_ADDRCONFIG,
344         };
345         sa_family_t family;
346         int error;
347
348         if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
349                 return 0;
350
351         if (!nfs_nfs_proto_family(mi->options, &family))
352                 return 0;
353
354         hint.ai_family = (int)family;
355         error = getaddrinfo(mi->hostname, NULL, &hint, &mi->address);
356         if (error != 0) {
357                 nfs_error(_("%s: Failed to resolve server %s: %s"),
358                         progname, mi->hostname, gai_strerror(error));
359                 mi->address = NULL;
360                 return 0;
361         }
362
363         if (!nfs_set_version(mi))
364                 return 0;
365
366         if (!nfs_append_sloppy_option(mi->options))
367                 return 0;
368
369         if (!nfs_append_addr_option(mi->address->ai_addr,
370                                         mi->address->ai_addrlen, mi->options))
371                 return 0;
372
373         return 1;
374 }
375
376 /*
377  * Get NFS/mnt server addresses from mount options
378  *
379  * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
380  * if all goes well; otherwise zero.
381  */
382 static int nfs_extract_server_addresses(struct mount_options *options,
383                                         struct sockaddr *nfs_saddr,
384                                         socklen_t *nfs_salen,
385                                         struct sockaddr *mnt_saddr,
386                                         socklen_t *mnt_salen)
387 {
388         char *option;
389
390         option = po_get(options, "addr");
391         if (option == NULL)
392                 return 0;
393         if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
394                 return 0;
395
396         option = po_get(options, "mountaddr");
397         if (option == NULL) {
398                 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
399                 *mnt_salen = *nfs_salen;
400         } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
401                 return 0;
402
403         return 1;
404 }
405
406 static int nfs_construct_new_options(struct mount_options *options,
407                                      struct sockaddr *nfs_saddr,
408                                      struct pmap *nfs_pmap,
409                                      struct sockaddr *mnt_saddr,
410                                      struct pmap *mnt_pmap)
411 {
412         char new_option[64];
413         char *netid;
414
415         po_remove_all(options, "nfsprog");
416         po_remove_all(options, "mountprog");
417
418         po_remove_all(options, "v2");
419         po_remove_all(options, "v3");
420         po_remove_all(options, "vers");
421         po_remove_all(options, "nfsvers");
422         snprintf(new_option, sizeof(new_option) - 1,
423                  "vers=%lu", nfs_pmap->pm_vers);
424         if (po_append(options, new_option) == PO_FAILED)
425                 return 0;
426
427         po_remove_all(options, "proto");
428         po_remove_all(options, "udp");
429         po_remove_all(options, "tcp");
430         netid = nfs_get_netid(nfs_saddr->sa_family, nfs_pmap->pm_prot);
431         if (netid == NULL)
432                 return 0;
433         snprintf(new_option, sizeof(new_option) - 1,
434                          "proto=%s", netid);
435         free(netid);
436         if (po_append(options, new_option) == PO_FAILED)
437                 return 0;
438
439         po_remove_all(options, "port");
440         if (nfs_pmap->pm_port != NFS_PORT) {
441                 snprintf(new_option, sizeof(new_option) - 1,
442                          "port=%lu", nfs_pmap->pm_port);
443                 if (po_append(options, new_option) == PO_FAILED)
444                         return 0;
445         }
446
447         po_remove_all(options, "mountvers");
448         snprintf(new_option, sizeof(new_option) - 1,
449                  "mountvers=%lu", mnt_pmap->pm_vers);
450         if (po_append(options, new_option) == PO_FAILED)
451                 return 0;
452
453         po_remove_all(options, "mountproto");
454         netid = nfs_get_netid(mnt_saddr->sa_family, mnt_pmap->pm_prot);
455         if (netid == NULL)
456                 return 0;
457         snprintf(new_option, sizeof(new_option) - 1,
458                          "mountproto=%s", netid);
459         free(netid);
460         if (po_append(options, new_option) == PO_FAILED)
461                 return 0;
462
463         po_remove_all(options, "mountport");
464         snprintf(new_option, sizeof(new_option) - 1,
465                  "mountport=%lu", mnt_pmap->pm_port);
466         if (po_append(options, new_option) == PO_FAILED)
467                 return 0;
468
469         return 1;
470 }
471
472 /*
473  * Reconstruct the mount option string based on a portmapper probe
474  * of the server.  Returns one if the server's portmapper returned
475  * something we can use, otherwise zero.
476  *
477  * To handle version and transport protocol fallback properly, we
478  * need to parse some of the mount options in order to set up a
479  * portmap probe.  Mount options that nfs_rewrite_pmap_mount_options()
480  * doesn't recognize are left alone.
481  *
482  * Returns TRUE if rewriting was successful; otherwise
483  * FALSE is returned if some failure occurred.
484  */
485 static int
486 nfs_rewrite_pmap_mount_options(struct mount_options *options)
487 {
488         union nfs_sockaddr nfs_address;
489         struct sockaddr *nfs_saddr = &nfs_address.sa;
490         socklen_t nfs_salen = sizeof(nfs_address);
491         struct pmap nfs_pmap;
492         union nfs_sockaddr mnt_address;
493         struct sockaddr *mnt_saddr = &mnt_address.sa;
494         socklen_t mnt_salen = sizeof(mnt_address);
495         unsigned long protocol;
496         struct pmap mnt_pmap;
497         char *option;
498
499         /*
500          * Version and transport negotiation is not required
501          * and does not work for RDMA mounts.
502          */
503         if (!nfs_nfs_protocol(options, &protocol)) {
504                 errno = EINVAL;
505                 return 0;
506         }
507         if (protocol == NFSPROTO_RDMA)
508                 goto out;
509
510         /*
511          * Extract just the options needed to contact server.
512          * Bail now if any of these have bad values.
513          */
514         if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
515                                                 mnt_saddr, &mnt_salen)) {
516                 errno = EINVAL;
517                 return 0;
518         }
519         if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
520                 errno = EINVAL;
521                 return 0;
522         }
523
524         /*
525          * The kernel NFS client doesn't support changing the RPC
526          * program number for these services, so force the value of
527          * these fields before probing the server's ports.
528          */
529         nfs_pmap.pm_prog = NFS_PROGRAM;
530         mnt_pmap.pm_prog = MOUNTPROG;
531
532         /*
533          * If the server's rpcbind service isn't available, we can't
534          * negotiate.  Bail now if we can't contact it.
535          */
536         if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
537                                  nfs_saddr, nfs_salen, &nfs_pmap)) {
538                 errno = ESPIPE;
539                 if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
540                         errno = EOPNOTSUPP;
541                 else if (rpc_createerr.cf_error.re_errno != 0)
542                         errno = rpc_createerr.cf_error.re_errno;
543                 return 0;
544         }
545
546         if (!nfs_construct_new_options(options, nfs_saddr, &nfs_pmap,
547                                         mnt_saddr, &mnt_pmap)) {
548                 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO)
549                         errno = EPROTONOSUPPORT;
550                 else
551                         errno = EINVAL;
552                 return 0;
553         }
554
555 out:
556         errno = 0;
557         return 1;
558 }
559
560 /*
561  * Do the mount(2) system call.
562  *
563  * Returns TRUE if successful, otherwise FALSE.
564  * "errno" is set to reflect the individual error.
565  */
566 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
567 {
568         char *options = NULL;
569         int result;
570
571         if (po_join(opts, &options) == PO_FAILED) {
572                 errno = EIO;
573                 return 0;
574         }
575
576         if (mi->fake)
577                 return 1;
578
579         result = mount(mi->spec, mi->node, mi->type,
580                         mi->flags & ~(MS_USER|MS_USERS), options);
581         if (verbose && result) {
582                 int save = errno;
583                 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
584                 errno = save;
585         }
586         return !result;
587 }
588
589 static int nfs_do_mount_v3v2(struct nfsmount_info *mi,
590                 struct sockaddr *sap, socklen_t salen)
591 {
592         struct mount_options *options = po_dup(mi->options);
593         int result = 0;
594
595         if (!options) {
596                 errno = ENOMEM;
597                 return result;
598         }
599         errno = 0;
600         if (!nfs_append_addr_option(sap, salen, options)) {
601                 if (errno == 0)
602                         errno = EINVAL;
603                 goto out_fail;
604         }
605
606         if (!nfs_fix_mounthost_option(options, mi->hostname)) {
607                 if (errno == 0)
608                         errno = EINVAL;
609                 goto out_fail;
610         }
611         if (!mi->fake && !nfs_verify_lock_option(options)) {
612                 if (errno == 0)
613                         errno = EINVAL;
614                 goto out_fail;
615         }
616
617         /*
618          * Options we negotiate below may be stale by the time this
619          * file system is unmounted.  In order to force umount.nfs
620          * to renegotiate with the server, only write the user-
621          * specified options, and not negotiated options, to /etc/mtab.
622          */
623         if (po_join(options, mi->extra_opts) == PO_FAILED) {
624                 errno = ENOMEM;
625                 goto out_fail;
626         }
627
628         if (verbose)
629                 printf(_("%s: trying text-based options '%s'\n"),
630                         progname, *mi->extra_opts);
631
632         if (!nfs_rewrite_pmap_mount_options(options))
633                 goto out_fail;
634
635         result = nfs_sys_mount(mi, options);
636
637 out_fail:
638         po_destroy(options);
639         return result;
640 }
641
642 /*
643  * Attempt a "-t nfs vers=2" or "-t nfs vers=3" mount.
644  *
645  * Returns TRUE if successful, otherwise FALSE.
646  * "errno" is set to reflect the individual error.
647  */
648 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
649 {
650         struct addrinfo *ai;
651         int ret;
652
653         for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
654                 ret = nfs_do_mount_v3v2(mi, ai->ai_addr, ai->ai_addrlen);
655                 if (ret != 0)
656                         return ret;
657
658                 switch (errno) {
659                 case ECONNREFUSED:
660                 case EOPNOTSUPP:
661                 case EHOSTUNREACH:
662                         continue;
663                 default:
664                         break;
665                 }
666         }
667         return ret;
668 }
669
670 static int nfs_do_mount_v4(struct nfsmount_info *mi,
671                 struct sockaddr *sap, socklen_t salen)
672 {
673         struct mount_options *options = po_dup(mi->options);
674         int result = 0;
675
676         if (!options) {
677                 errno = ENOMEM;
678                 return result;
679         }
680
681         if (mi->version == 0) {
682                 if (po_contains(options, "mounthost") ||
683                         po_contains(options, "mountaddr") ||
684                         po_contains(options, "mountvers") ||
685                         po_contains(options, "mountproto")) {
686                 /*
687                  * Since these mountd options are set assume version 3
688                  * is wanted so error out with EPROTONOSUPPORT so the
689                  * protocol negation starts with v3.
690                  */
691                         errno = EPROTONOSUPPORT;
692                         goto out_fail;
693                 }
694                 if (po_append(options, "vers=4") == PO_FAILED) {
695                         errno = EINVAL;
696                         goto out_fail;
697                 }
698         }
699
700         if (!nfs_append_addr_option(sap, salen, options)) {
701                 errno = EINVAL;
702                 goto out_fail;
703         }
704
705         if (!nfs_append_clientaddr_option(sap, salen, options)) {
706                 errno = EINVAL;
707                 goto out_fail;
708         }
709
710         /*
711          * Update option string to be recorded in /etc/mtab.
712          */
713         if (po_join(options, mi->extra_opts) == PO_FAILED) {
714                 errno = ENOMEM;
715                 goto out_fail;
716         }
717
718         if (verbose)
719                 printf(_("%s: trying text-based options '%s'\n"),
720                         progname, *mi->extra_opts);
721
722         result = nfs_sys_mount(mi, options);
723
724 out_fail:
725         po_destroy(options);
726         return result;
727 }
728
729 /*
730  * Attempt a "-t nfs -o vers=4" or "-t nfs4" mount.
731  *
732  * Returns TRUE if successful, otherwise FALSE.
733  * "errno" is set to reflect the individual error.
734  */
735 static int nfs_try_mount_v4(struct nfsmount_info *mi)
736 {
737         struct addrinfo *ai;
738         int ret;
739
740         for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
741                 ret = nfs_do_mount_v4(mi, ai->ai_addr, ai->ai_addrlen);
742                 if (ret != 0)
743                         return ret;
744
745                 switch (errno) {
746                 case ECONNREFUSED:
747                 case EHOSTUNREACH:
748                         continue;
749                 default:
750                         break;
751                 }
752         }
753         return ret;
754 }
755
756 /*
757  * This is a single pass through the fg/bg loop.
758  *
759  * Returns TRUE if successful, otherwise FALSE.
760  * "errno" is set to reflect the individual error.
761  */
762 static int nfs_try_mount(struct nfsmount_info *mi)
763 {
764         int result = 0;
765
766         switch (mi->version) {
767         case 0:
768                 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
769                         errno = 0;
770                         result = nfs_try_mount_v4(mi);
771                         if (errno != EPROTONOSUPPORT) {
772                                 /* 
773                                  * To deal with legacy Linux servers that don't
774                                  * automatically export a pseudo root, retry
775                                  * ENOENT errors using version 3. And for
776                                  * Linux servers prior to 2.6.25, retry EPERM
777                                  */
778                                 if (errno != ENOENT && errno != EPERM)
779                                         break;
780                         }
781                 }
782         case 2:
783         case 3:
784                 result = nfs_try_mount_v3v2(mi);
785                 break;
786         case 4:
787                 result = nfs_try_mount_v4(mi);
788                 break;
789         default:
790                 errno = EIO;
791         }
792
793         return result;
794 }
795
796 /*
797  * Distinguish between permanent and temporary errors.
798  *
799  * Basically, we retry if communication with the server has
800  * failed so far, but fail immediately if there is a local
801  * error (like a bad mount option).
802  *
803  * ESTALE is also a temporary error because some servers
804  * return ESTALE when a share is temporarily offline.
805  *
806  * Returns 1 if we should fail immediately, or 0 if we
807  * should retry.
808  */
809 static int nfs_is_permanent_error(int error)
810 {
811         switch (error) {
812         case ESTALE:
813         case ETIMEDOUT:
814         case ECONNREFUSED:
815         case EHOSTUNREACH:
816                 return 0;       /* temporary */
817         default:
818                 return 1;       /* permanent */
819         }
820 }
821
822 /*
823  * Handle "foreground" NFS mounts.
824  *
825  * Retry the mount request for as long as the 'retry=' option says.
826  *
827  * Returns a valid mount command exit code.
828  */
829 static int nfsmount_fg(struct nfsmount_info *mi)
830 {
831         unsigned int secs = 1;
832         time_t timeout;
833
834         timeout = nfs_parse_retry_option(mi->options,
835                                          NFS_DEF_FG_TIMEOUT_MINUTES);
836         if (verbose)
837                 printf(_("%s: timeout set for %s"),
838                         progname, ctime(&timeout));
839
840         for (;;) {
841                 if (nfs_try_mount(mi))
842                         return EX_SUCCESS;
843
844                 if (nfs_is_permanent_error(errno))
845                         break;
846
847                 if (time(NULL) > timeout) {
848                         errno = ETIMEDOUT;
849                         break;
850                 }
851
852                 if (errno != ETIMEDOUT) {
853                         if (sleep(secs))
854                                 break;
855                         secs <<= 1;
856                         if (secs > 10)
857                                 secs = 10;
858                 }
859         };
860
861         mount_error(mi->spec, mi->node, errno);
862         return EX_FAIL;
863 }
864
865 /*
866  * Handle "background" NFS mount [first try]
867  *
868  * Returns a valid mount command exit code.
869  *
870  * EX_BG should cause the caller to fork and invoke nfsmount_child.
871  */
872 static int nfsmount_parent(struct nfsmount_info *mi)
873 {
874         if (nfs_try_mount(mi))
875                 return EX_SUCCESS;
876
877         if (nfs_is_permanent_error(errno)) {
878                 mount_error(mi->spec, mi->node, errno);
879                 return EX_FAIL;
880         }
881
882         sys_mount_errors(mi->hostname, errno, 1, 1);
883         return EX_BG;
884 }
885
886 /*
887  * Handle "background" NFS mount [retry daemon]
888  *
889  * Returns a valid mount command exit code: EX_SUCCESS if successful,
890  * EX_FAIL if a failure occurred.  There's nothing to catch the
891  * error return, though, so we use sys_mount_errors to log the
892  * failure.
893  */
894 static int nfsmount_child(struct nfsmount_info *mi)
895 {
896         unsigned int secs = 1;
897         time_t timeout;
898
899         timeout = nfs_parse_retry_option(mi->options,
900                                          NFS_DEF_BG_TIMEOUT_MINUTES);
901
902         for (;;) {
903                 if (sleep(secs))
904                         break;
905                 secs <<= 1;
906                 if (secs > 120)
907                         secs = 120;
908
909                 if (nfs_try_mount(mi))
910                         return EX_SUCCESS;
911
912                 if (nfs_is_permanent_error(errno))
913                         break;
914
915                 if (time(NULL) > timeout)
916                         break;
917
918                 sys_mount_errors(mi->hostname, errno, 1, 1);
919         };
920
921         sys_mount_errors(mi->hostname, errno, 1, 0);
922         return EX_FAIL;
923 }
924
925 /*
926  * Handle "background" NFS mount
927  *
928  * Returns a valid mount command exit code.
929  */
930 static int nfsmount_bg(struct nfsmount_info *mi)
931 {
932         if (!mi->child)
933                 return nfsmount_parent(mi);
934         else
935                 return nfsmount_child(mi);
936 }
937
938 /*
939  * Process mount options and try a mount system call.
940  *
941  * Returns a valid mount command exit code.
942  */
943 static const char *nfs_background_opttbl[] = {
944         "bg",
945         "fg",
946         NULL,
947 };
948
949 static int nfsmount_start(struct nfsmount_info *mi)
950 {
951         if (!nfs_validate_options(mi))
952                 return EX_FAIL;
953
954         if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
955                 return nfsmount_bg(mi);
956         else
957                 return nfsmount_fg(mi);
958 }
959
960 /**
961  * nfsmount_string - Mount an NFS file system using C string options
962  * @spec: C string specifying remote share to mount ("hostname:path")
963  * @node: C string pathname of local mounted-on directory
964  * @type: C string that represents file system type ("nfs" or "nfs4")
965  * @flags: MS_ style mount flags
966  * @extra_opts: pointer to C string containing fs-specific mount options
967  *              (input and output argument)
968  * @fake: flag indicating whether to carry out the whole operation
969  * @child: one if this is a mount daemon (bg)
970  */
971 int nfsmount_string(const char *spec, const char *node, const char *type,
972                     int flags, char **extra_opts, int fake, int child)
973 {
974         struct nfsmount_info mi = {
975                 .spec           = spec,
976                 .node           = node,
977                 .address        = NULL,
978                 .type           = type,
979                 .extra_opts     = extra_opts,
980                 .flags          = flags,
981                 .fake           = fake,
982                 .child          = child,
983         };
984         int retval = EX_FAIL;
985
986         mi.options = po_split(*extra_opts);
987         if (mi.options) {
988                 retval = nfsmount_start(&mi);
989                 po_destroy(mi.options);
990         } else
991                 nfs_error(_("%s: internal option parsing error"), progname);
992
993         freeaddrinfo(mi.address);
994         free(mi.hostname);
995         return retval;
996 }