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