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