]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
mount.nfs: silently fails when the network protocol is not found
[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                 char *option = po_get(mi->options, "proto");
307                 if (option && strcmp(option, "rdma") == 0)
308                         mi->version = 3;
309         }
310
311         /*
312          * If we still don't know, check for version-specific
313          * mount options.
314          */
315         if (mi->version == 0) {
316                 if (po_contains(mi->options, "mounthost") ||
317                     po_contains(mi->options, "mountaddr") ||
318                     po_contains(mi->options, "mountvers") ||
319                     po_contains(mi->options, "mountproto"))
320                         mi->version = 3;
321         }
322
323         /*
324          * If enabled, see if the default version was
325          * set in the config file
326          */
327         nfs_default_version(mi);
328         
329         return 1;
330 }
331
332 /*
333  * Set up mandatory non-version specific NFS mount options.
334  *
335  * Returns 1 if successful; otherwise zero.
336  */
337 static int nfs_validate_options(struct nfsmount_info *mi)
338 {
339         struct addrinfo hint = {
340                 .ai_protocol    = (int)IPPROTO_UDP,
341                 .ai_flags       = AI_ADDRCONFIG,
342         };
343         sa_family_t family;
344         int error;
345
346         if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
347                 return 0;
348
349         if (!nfs_nfs_proto_family(mi->options, &family))
350                 return 0;
351
352         hint.ai_family = (int)family;
353         error = getaddrinfo(mi->hostname, NULL, &hint, &mi->address);
354         if (error != 0) {
355                 nfs_error(_("%s: Failed to resolve server %s: %s"),
356                         progname, mi->hostname, gai_strerror(error));
357                 mi->address = NULL;
358                 return 0;
359         }
360
361         if (!nfs_set_version(mi))
362                 return 0;
363
364         if (!nfs_append_sloppy_option(mi->options))
365                 return 0;
366
367         if (!nfs_append_addr_option(mi->address->ai_addr,
368                                         mi->address->ai_addrlen, mi->options))
369                 return 0;
370
371         return 1;
372 }
373
374 /*
375  * Get NFS/mnt server addresses from mount options
376  *
377  * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
378  * if all goes well; otherwise zero.
379  */
380 static int nfs_extract_server_addresses(struct mount_options *options,
381                                         struct sockaddr *nfs_saddr,
382                                         socklen_t *nfs_salen,
383                                         struct sockaddr *mnt_saddr,
384                                         socklen_t *mnt_salen)
385 {
386         char *option;
387
388         option = po_get(options, "addr");
389         if (option == NULL)
390                 return 0;
391         if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
392                 return 0;
393
394         option = po_get(options, "mountaddr");
395         if (option == NULL) {
396                 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
397                 *mnt_salen = *nfs_salen;
398         } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
399                 return 0;
400
401         return 1;
402 }
403
404 static int nfs_construct_new_options(struct mount_options *options,
405                                      struct sockaddr *nfs_saddr,
406                                      struct pmap *nfs_pmap,
407                                      struct sockaddr *mnt_saddr,
408                                      struct pmap *mnt_pmap)
409 {
410         char new_option[64];
411         char *netid;
412
413         po_remove_all(options, "nfsprog");
414         po_remove_all(options, "mountprog");
415
416         po_remove_all(options, "v2");
417         po_remove_all(options, "v3");
418         po_remove_all(options, "vers");
419         po_remove_all(options, "nfsvers");
420         snprintf(new_option, sizeof(new_option) - 1,
421                  "vers=%lu", nfs_pmap->pm_vers);
422         if (po_append(options, new_option) == PO_FAILED)
423                 return 0;
424
425         po_remove_all(options, "proto");
426         po_remove_all(options, "udp");
427         po_remove_all(options, "tcp");
428         netid = nfs_get_netid(nfs_saddr->sa_family, nfs_pmap->pm_prot);
429         if (netid == NULL)
430                 return 0;
431         snprintf(new_option, sizeof(new_option) - 1,
432                          "proto=%s", netid);
433         free(netid);
434         if (po_append(options, new_option) == PO_FAILED)
435                 return 0;
436
437         po_remove_all(options, "port");
438         if (nfs_pmap->pm_port != NFS_PORT) {
439                 snprintf(new_option, sizeof(new_option) - 1,
440                          "port=%lu", nfs_pmap->pm_port);
441                 if (po_append(options, new_option) == PO_FAILED)
442                         return 0;
443         }
444
445         po_remove_all(options, "mountvers");
446         snprintf(new_option, sizeof(new_option) - 1,
447                  "mountvers=%lu", mnt_pmap->pm_vers);
448         if (po_append(options, new_option) == PO_FAILED)
449                 return 0;
450
451         po_remove_all(options, "mountproto");
452         netid = nfs_get_netid(mnt_saddr->sa_family, mnt_pmap->pm_prot);
453         if (netid == NULL)
454                 return 0;
455         snprintf(new_option, sizeof(new_option) - 1,
456                          "mountproto=%s", netid);
457         free(netid);
458         if (po_append(options, new_option) == PO_FAILED)
459                 return 0;
460
461         po_remove_all(options, "mountport");
462         snprintf(new_option, sizeof(new_option) - 1,
463                  "mountport=%lu", mnt_pmap->pm_port);
464         if (po_append(options, new_option) == PO_FAILED)
465                 return 0;
466
467         return 1;
468 }
469
470 /*
471  * Reconstruct the mount option string based on a portmapper probe
472  * of the server.  Returns one if the server's portmapper returned
473  * something we can use, otherwise zero.
474  *
475  * To handle version and transport protocol fallback properly, we
476  * need to parse some of the mount options in order to set up a
477  * portmap probe.  Mount options that nfs_rewrite_pmap_mount_options()
478  * doesn't recognize are left alone.
479  *
480  * Returns TRUE if rewriting was successful; otherwise
481  * FALSE is returned if some failure occurred.
482  */
483 static int
484 nfs_rewrite_pmap_mount_options(struct mount_options *options)
485 {
486         union nfs_sockaddr nfs_address;
487         struct sockaddr *nfs_saddr = &nfs_address.sa;
488         socklen_t nfs_salen = sizeof(nfs_address);
489         struct pmap nfs_pmap;
490         union nfs_sockaddr mnt_address;
491         struct sockaddr *mnt_saddr = &mnt_address.sa;
492         socklen_t mnt_salen = sizeof(mnt_address);
493         struct pmap mnt_pmap;
494         char *option;
495
496         /*
497          * Skip option negotiation for proto=rdma mounts.
498          */
499         option = po_get(options, "proto");
500         if (option && strcmp(option, "rdma") == 0)
501                 goto out;
502
503         /*
504          * Extract just the options needed to contact server.
505          * Bail now if any of these have bad values.
506          */
507         if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
508                                                 mnt_saddr, &mnt_salen)) {
509                 errno = EINVAL;
510                 return 0;
511         }
512         if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
513                 errno = EINVAL;
514                 return 0;
515         }
516
517         /*
518          * The kernel NFS client doesn't support changing the RPC
519          * program number for these services, so force the value of
520          * these fields before probing the server's ports.
521          */
522         nfs_pmap.pm_prog = NFS_PROGRAM;
523         mnt_pmap.pm_prog = MOUNTPROG;
524
525         /*
526          * If the server's rpcbind service isn't available, we can't
527          * negotiate.  Bail now if we can't contact it.
528          */
529         if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
530                                  nfs_saddr, nfs_salen, &nfs_pmap)) {
531                 errno = ESPIPE;
532                 if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
533                         errno = EOPNOTSUPP;
534                 else if (rpc_createerr.cf_error.re_errno != 0)
535                         errno = rpc_createerr.cf_error.re_errno;
536                 return 0;
537         }
538
539         if (!nfs_construct_new_options(options, nfs_saddr, &nfs_pmap,
540                                         mnt_saddr, &mnt_pmap)) {
541                 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO)
542                         errno = EPROTONOSUPPORT;
543                 else
544                         errno = EINVAL;
545                 return 0;
546         }
547
548 out:
549         errno = 0;
550         return 1;
551 }
552
553 /*
554  * Do the mount(2) system call.
555  *
556  * Returns TRUE if successful, otherwise FALSE.
557  * "errno" is set to reflect the individual error.
558  */
559 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
560 {
561         char *options = NULL;
562         int result;
563
564         if (po_join(opts, &options) == PO_FAILED) {
565                 errno = EIO;
566                 return 0;
567         }
568
569         if (mi->fake)
570                 return 1;
571
572         result = mount(mi->spec, mi->node, mi->type,
573                         mi->flags & ~(MS_USER|MS_USERS), options);
574         if (verbose && result) {
575                 int save = errno;
576                 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
577                 errno = save;
578         }
579         return !result;
580 }
581
582 static int nfs_do_mount_v3v2(struct nfsmount_info *mi,
583                 struct sockaddr *sap, socklen_t salen)
584 {
585         struct mount_options *options = po_dup(mi->options);
586         int result = 0;
587
588         if (!options) {
589                 errno = ENOMEM;
590                 return result;
591         }
592         errno = 0;
593         if (!nfs_append_addr_option(sap, salen, options)) {
594                 if (errno == 0)
595                         errno = EINVAL;
596                 goto out_fail;
597         }
598
599         if (!nfs_fix_mounthost_option(options, mi->hostname)) {
600                 if (errno == 0)
601                         errno = EINVAL;
602                 goto out_fail;
603         }
604         if (!mi->fake && !nfs_verify_lock_option(options)) {
605                 if (errno == 0)
606                         errno = EINVAL;
607                 goto out_fail;
608         }
609
610         /*
611          * Options we negotiate below may be stale by the time this
612          * file system is unmounted.  In order to force umount.nfs
613          * to renegotiate with the server, only write the user-
614          * specified options, and not negotiated options, to /etc/mtab.
615          */
616         if (po_join(options, mi->extra_opts) == PO_FAILED) {
617                 errno = ENOMEM;
618                 goto out_fail;
619         }
620
621         if (verbose)
622                 printf(_("%s: trying text-based options '%s'\n"),
623                         progname, *mi->extra_opts);
624
625         if (!nfs_rewrite_pmap_mount_options(options))
626                 goto out_fail;
627
628         result = nfs_sys_mount(mi, options);
629
630 out_fail:
631         po_destroy(options);
632         return result;
633 }
634
635 /*
636  * Attempt a "-t nfs vers=2" or "-t nfs vers=3" mount.
637  *
638  * Returns TRUE if successful, otherwise FALSE.
639  * "errno" is set to reflect the individual error.
640  */
641 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
642 {
643         struct addrinfo *ai;
644         int ret;
645
646         for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
647                 ret = nfs_do_mount_v3v2(mi, ai->ai_addr, ai->ai_addrlen);
648                 if (ret != 0)
649                         return ret;
650
651                 switch (errno) {
652                 case ECONNREFUSED:
653                 case EOPNOTSUPP:
654                 case EHOSTUNREACH:
655                         continue;
656                 default:
657                         break;
658                 }
659         }
660         return ret;
661 }
662
663 static int nfs_do_mount_v4(struct nfsmount_info *mi,
664                 struct sockaddr *sap, socklen_t salen)
665 {
666         struct mount_options *options = po_dup(mi->options);
667         int result = 0;
668
669         if (!options) {
670                 errno = ENOMEM;
671                 return result;
672         }
673
674         if (mi->version == 0) {
675                 if (po_contains(options, "mounthost") ||
676                         po_contains(options, "mountaddr") ||
677                         po_contains(options, "mountvers") ||
678                         po_contains(options, "mountproto")) {
679                 /*
680                  * Since these mountd options are set assume version 3
681                  * is wanted so error out with EPROTONOSUPPORT so the
682                  * protocol negation starts with v3.
683                  */
684                         errno = EPROTONOSUPPORT;
685                         goto out_fail;
686                 }
687                 if (po_append(options, "vers=4") == PO_FAILED) {
688                         errno = EINVAL;
689                         goto out_fail;
690                 }
691         }
692
693         if (!nfs_append_addr_option(sap, salen, options)) {
694                 errno = EINVAL;
695                 goto out_fail;
696         }
697
698         if (!nfs_append_clientaddr_option(sap, salen, options)) {
699                 errno = EINVAL;
700                 goto out_fail;
701         }
702
703         /*
704          * Update option string to be recorded in /etc/mtab.
705          */
706         if (po_join(options, mi->extra_opts) == PO_FAILED) {
707                 errno = ENOMEM;
708                 goto out_fail;
709         }
710
711         if (verbose)
712                 printf(_("%s: trying text-based options '%s'\n"),
713                         progname, *mi->extra_opts);
714
715         result = nfs_sys_mount(mi, options);
716
717 out_fail:
718         po_destroy(options);
719         return result;
720 }
721
722 /*
723  * Attempt a "-t nfs -o vers=4" or "-t nfs4" mount.
724  *
725  * Returns TRUE if successful, otherwise FALSE.
726  * "errno" is set to reflect the individual error.
727  */
728 static int nfs_try_mount_v4(struct nfsmount_info *mi)
729 {
730         struct addrinfo *ai;
731         int ret;
732
733         for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
734                 ret = nfs_do_mount_v4(mi, ai->ai_addr, ai->ai_addrlen);
735                 if (ret != 0)
736                         return ret;
737
738                 switch (errno) {
739                 case ECONNREFUSED:
740                 case EHOSTUNREACH:
741                         continue;
742                 default:
743                         break;
744                 }
745         }
746         return ret;
747 }
748
749 /*
750  * This is a single pass through the fg/bg loop.
751  *
752  * Returns TRUE if successful, otherwise FALSE.
753  * "errno" is set to reflect the individual error.
754  */
755 static int nfs_try_mount(struct nfsmount_info *mi)
756 {
757         int result = 0;
758
759         switch (mi->version) {
760         case 0:
761                 if (linux_version_code() > MAKE_VERSION(2, 6, 31)) {
762                         errno = 0;
763                         result = nfs_try_mount_v4(mi);
764                         if (errno != EPROTONOSUPPORT) {
765                                 /* 
766                                  * To deal with legacy Linux servers that don't
767                                  * automatically export a pseudo root, retry
768                                  * ENOENT errors using version 3. And for
769                                  * Linux servers prior to 2.6.25, retry EPERM
770                                  */
771                                 if (errno != ENOENT && errno != EPERM)
772                                         break;
773                         }
774                 }
775         case 2:
776         case 3:
777                 result = nfs_try_mount_v3v2(mi);
778                 break;
779         case 4:
780                 result = nfs_try_mount_v4(mi);
781                 break;
782         default:
783                 errno = EIO;
784         }
785
786         return result;
787 }
788
789 /*
790  * Distinguish between permanent and temporary errors.
791  *
792  * Basically, we retry if communication with the server has
793  * failed so far, but fail immediately if there is a local
794  * error (like a bad mount option).
795  *
796  * ESTALE is also a temporary error because some servers
797  * return ESTALE when a share is temporarily offline.
798  *
799  * Returns 1 if we should fail immediately, or 0 if we
800  * should retry.
801  */
802 static int nfs_is_permanent_error(int error)
803 {
804         switch (error) {
805         case ESTALE:
806         case ETIMEDOUT:
807         case ECONNREFUSED:
808         case EHOSTUNREACH:
809                 return 0;       /* temporary */
810         default:
811                 return 1;       /* permanent */
812         }
813 }
814
815 /*
816  * Handle "foreground" NFS mounts.
817  *
818  * Retry the mount request for as long as the 'retry=' option says.
819  *
820  * Returns a valid mount command exit code.
821  */
822 static int nfsmount_fg(struct nfsmount_info *mi)
823 {
824         unsigned int secs = 1;
825         time_t timeout;
826
827         timeout = nfs_parse_retry_option(mi->options,
828                                          NFS_DEF_FG_TIMEOUT_MINUTES);
829         if (verbose)
830                 printf(_("%s: timeout set for %s"),
831                         progname, ctime(&timeout));
832
833         for (;;) {
834                 if (nfs_try_mount(mi))
835                         return EX_SUCCESS;
836
837                 if (nfs_is_permanent_error(errno))
838                         break;
839
840                 if (time(NULL) > timeout) {
841                         errno = ETIMEDOUT;
842                         break;
843                 }
844
845                 if (errno != ETIMEDOUT) {
846                         if (sleep(secs))
847                                 break;
848                         secs <<= 1;
849                         if (secs > 10)
850                                 secs = 10;
851                 }
852         };
853
854         mount_error(mi->spec, mi->node, errno);
855         return EX_FAIL;
856 }
857
858 /*
859  * Handle "background" NFS mount [first try]
860  *
861  * Returns a valid mount command exit code.
862  *
863  * EX_BG should cause the caller to fork and invoke nfsmount_child.
864  */
865 static int nfsmount_parent(struct nfsmount_info *mi)
866 {
867         if (nfs_try_mount(mi))
868                 return EX_SUCCESS;
869
870         if (nfs_is_permanent_error(errno)) {
871                 mount_error(mi->spec, mi->node, errno);
872                 return EX_FAIL;
873         }
874
875         sys_mount_errors(mi->hostname, errno, 1, 1);
876         return EX_BG;
877 }
878
879 /*
880  * Handle "background" NFS mount [retry daemon]
881  *
882  * Returns a valid mount command exit code: EX_SUCCESS if successful,
883  * EX_FAIL if a failure occurred.  There's nothing to catch the
884  * error return, though, so we use sys_mount_errors to log the
885  * failure.
886  */
887 static int nfsmount_child(struct nfsmount_info *mi)
888 {
889         unsigned int secs = 1;
890         time_t timeout;
891
892         timeout = nfs_parse_retry_option(mi->options,
893                                          NFS_DEF_BG_TIMEOUT_MINUTES);
894
895         for (;;) {
896                 if (sleep(secs))
897                         break;
898                 secs <<= 1;
899                 if (secs > 120)
900                         secs = 120;
901
902                 if (nfs_try_mount(mi))
903                         return EX_SUCCESS;
904
905                 if (nfs_is_permanent_error(errno))
906                         break;
907
908                 if (time(NULL) > timeout)
909                         break;
910
911                 sys_mount_errors(mi->hostname, errno, 1, 1);
912         };
913
914         sys_mount_errors(mi->hostname, errno, 1, 0);
915         return EX_FAIL;
916 }
917
918 /*
919  * Handle "background" NFS mount
920  *
921  * Returns a valid mount command exit code.
922  */
923 static int nfsmount_bg(struct nfsmount_info *mi)
924 {
925         if (!mi->child)
926                 return nfsmount_parent(mi);
927         else
928                 return nfsmount_child(mi);
929 }
930
931 /*
932  * Process mount options and try a mount system call.
933  *
934  * Returns a valid mount command exit code.
935  */
936 static const char *nfs_background_opttbl[] = {
937         "bg",
938         "fg",
939         NULL,
940 };
941
942 static int nfsmount_start(struct nfsmount_info *mi)
943 {
944         if (!nfs_validate_options(mi))
945                 return EX_FAIL;
946
947         if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
948                 return nfsmount_bg(mi);
949         else
950                 return nfsmount_fg(mi);
951 }
952
953 /**
954  * nfsmount_string - Mount an NFS file system using C string options
955  * @spec: C string specifying remote share to mount ("hostname:path")
956  * @node: C string pathname of local mounted-on directory
957  * @type: C string that represents file system type ("nfs" or "nfs4")
958  * @flags: MS_ style mount flags
959  * @extra_opts: pointer to C string containing fs-specific mount options
960  *              (input and output argument)
961  * @fake: flag indicating whether to carry out the whole operation
962  * @child: one if this is a mount daemon (bg)
963  */
964 int nfsmount_string(const char *spec, const char *node, const char *type,
965                     int flags, char **extra_opts, int fake, int child)
966 {
967         struct nfsmount_info mi = {
968                 .spec           = spec,
969                 .node           = node,
970                 .address        = NULL,
971                 .type           = type,
972                 .extra_opts     = extra_opts,
973                 .flags          = flags,
974                 .fake           = fake,
975                 .child          = child,
976         };
977         int retval = EX_FAIL;
978
979         mi.options = po_split(*extra_opts);
980         if (mi.options) {
981                 retval = nfsmount_start(&mi);
982                 po_destroy(mi.options);
983         } else
984                 nfs_error(_("%s: internal option parsing error"), progname);
985
986         freeaddrinfo(mi.address);
987         free(mi.hostname);
988         return retval;
989 }