]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
nfs-utils: Remove all uses of AI_ADDRCONFIG
[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 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(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                                      unsigned int timeout_minutes)
123 {
124         long tmp;
125
126         switch (po_get_numeric(options, "retry", &tmp)) {
127         case PO_NOT_FOUND:
128                 break;
129         case PO_FOUND:
130                 if (tmp >= 0) {
131                         timeout_minutes = tmp;
132                         break;
133                 }
134         case PO_BAD_VALUE:
135                 if (verbose)
136                         nfs_error(_("%s: invalid retry timeout was specified; "
137                                         "using default timeout"), progname);
138                 break;
139         }
140
141         return time(NULL) + (time_t)(timeout_minutes * 60);
142 }
143
144 /*
145  * Convert the passed-in sockaddr-style address to presentation
146  * format, then append an option of the form "keyword=address".
147  *
148  * Returns 1 if the option was appended successfully; otherwise zero.
149  */
150 static int nfs_append_generic_address_option(const struct sockaddr *sap,
151                                              const socklen_t salen,
152                                              const char *keyword,
153                                              struct mount_options *options)
154 {
155         char address[NI_MAXHOST];
156         char new_option[512];
157         int len;
158
159         if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
160                 goto out_err;
161
162         len = snprintf(new_option, sizeof(new_option), "%s=%s",
163                                                 keyword, address);
164         if (len < 0 || (size_t)len >= sizeof(new_option))
165                 goto out_err;
166
167         if (po_append(options, new_option) != PO_SUCCEEDED)
168                 goto out_err;
169
170         return 1;
171
172 out_err:
173         nfs_error(_("%s: failed to construct %s option"), progname, keyword);
174         return 0;
175 }
176
177 /*
178  * Append the 'addr=' option to the options string to pass a resolved
179  * server address to the kernel.  After a successful mount, this address
180  * is also added to /etc/mtab for use when unmounting.
181  *
182  * If 'addr=' is already present, we strip it out.  This prevents users
183  * from setting a bogus 'addr=' option themselves, and also allows bg
184  * retries to recompute the server's address, in case it has changed.
185  *
186  * Returns 1 if 'addr=' option appended successfully;
187  * otherwise zero.
188  */
189 static int nfs_append_addr_option(const struct sockaddr *sap,
190                                   socklen_t salen,
191                                   struct mount_options *options)
192 {
193         po_remove_all(options, "addr");
194         return nfs_append_generic_address_option(sap, salen, "addr", options);
195 }
196
197 /*
198  * Called to discover our address and append an appropriate 'clientaddr='
199  * option to the options string.
200  *
201  * Returns 1 if 'clientaddr=' option created successfully or if
202  * 'clientaddr=' option is already present; otherwise zero.
203  */
204 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
205                                         socklen_t salen,
206                                         struct mount_options *options)
207 {
208         union nfs_sockaddr address;
209         struct sockaddr *my_addr = &address.sa;
210         socklen_t my_len = sizeof(address);
211
212         if (po_contains(options, "clientaddr") == PO_FOUND)
213                 return 1;
214
215         nfs_callback_address(sap, salen, my_addr, &my_len);
216
217         return nfs_append_generic_address_option(my_addr, my_len,
218                                                         "clientaddr", options);
219 }
220
221 /*
222  * Determine whether to append a 'mountaddr=' option.  The option is needed if:
223  *
224  *   1. "mounthost=" was specified, or
225  *   2. The address families for proto= and mountproto= are different.
226  */
227 static int nfs_fix_mounthost_option(struct mount_options *options,
228                 const char *nfs_hostname)
229 {
230         union nfs_sockaddr address;
231         struct sockaddr *sap = &address.sa;
232         socklen_t salen = sizeof(address);
233         sa_family_t nfs_family, mnt_family;
234         char *mounthost;
235
236         if (!nfs_nfs_proto_family(options, &nfs_family))
237                 return 0;
238         if (!nfs_mount_proto_family(options, &mnt_family))
239                 return 0;
240
241         mounthost = po_get(options, "mounthost");
242         if (mounthost == NULL) {
243                 if (nfs_family == mnt_family)
244                         return 1;
245                 mounthost = (char *)nfs_hostname;
246         }
247
248         if (!nfs_lookup(mounthost, mnt_family, sap, &salen)) {
249                 nfs_error(_("%s: unable to determine mount server's address"),
250                                 progname);
251                 return 0;
252         }
253
254         return nfs_append_generic_address_option(sap, salen,
255                                                         "mountaddr", options);
256 }
257
258 /*
259  * Returns zero if the "lock" option is in effect, but statd
260  * can't be started.  Otherwise, returns 1.
261  */
262 static const char *nfs_lock_opttbl[] = {
263         "nolock",
264         "lock",
265         NULL,
266 };
267
268 static int nfs_verify_lock_option(struct mount_options *options)
269 {
270         if (po_rightmost(options, nfs_lock_opttbl) == 0)
271                 return 1;
272
273         if (!start_statd()) {
274                 nfs_error(_("%s: rpc.statd is not running but is "
275                             "required for remote locking."), progname);
276                 nfs_error(_("%s: Either use '-o nolock' to keep "
277                             "locks local, or start statd."), progname);
278                 return 0;
279         }
280
281         return 1;
282 }
283
284 static int nfs_append_sloppy_option(struct mount_options *options)
285 {
286         if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
287                 return 1;
288
289         if (po_append(options, "sloppy") == PO_FAILED)
290                 return 0;
291         return 1;
292 }
293
294 static int nfs_set_version(struct nfsmount_info *mi)
295 {
296         if (!nfs_nfs_version(mi->options, &mi->version))
297                 return 0;
298
299         if (strncmp(mi->type, "nfs4", 4) == 0)
300                 mi->version = 4;
301
302         /*
303          * Before 2.6.32, the kernel NFS client didn't
304          * support "-t nfs vers=4" mounts, so NFS version
305          * 4 cannot be included when autonegotiating
306          * while running on those kernels.
307          */
308         if (mi->version == 0 &&
309             linux_version_code() <= MAKE_VERSION(2, 6, 31))
310                 mi->version = 3;
311
312         /*
313          * If we still don't know, check for version-specific
314          * mount options.
315          */
316         if (mi->version == 0) {
317                 if (po_contains(mi->options, "mounthost") ||
318                     po_contains(mi->options, "mountaddr") ||
319                     po_contains(mi->options, "mountvers") ||
320                     po_contains(mi->options, "mountproto"))
321                         mi->version = 3;
322         }
323
324         /*
325          * If enabled, see if the default version was
326          * set in the config file
327          */
328         nfs_default_version(mi);
329         
330         return 1;
331 }
332
333 /*
334  * Set up mandatory non-version specific NFS mount options.
335  *
336  * Returns 1 if successful; otherwise zero.
337  */
338 static int nfs_validate_options(struct nfsmount_info *mi)
339 {
340         struct addrinfo hint = {
341                 .ai_protocol    = (int)IPPROTO_UDP,
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         unsigned long protocol;
494         struct pmap mnt_pmap;
495
496         /*
497          * Version and transport negotiation is not required
498          * and does not work for RDMA mounts.
499          */
500         if (!nfs_nfs_protocol(options, &protocol)) {
501                 errno = EINVAL;
502                 return 0;
503         }
504         if (protocol == NFSPROTO_RDMA)
505                 goto out;
506
507         /*
508          * Extract just the options needed to contact server.
509          * Bail now if any of these have bad values.
510          */
511         if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
512                                                 mnt_saddr, &mnt_salen)) {
513                 errno = EINVAL;
514                 return 0;
515         }
516         if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
517                 errno = EINVAL;
518                 return 0;
519         }
520
521         /*
522          * The kernel NFS client doesn't support changing the RPC
523          * program number for these services, so force the value of
524          * these fields before probing the server's ports.
525          */
526         nfs_pmap.pm_prog = NFS_PROGRAM;
527         mnt_pmap.pm_prog = MOUNTPROG;
528
529         /*
530          * If the server's rpcbind service isn't available, we can't
531          * negotiate.  Bail now if we can't contact it.
532          */
533         if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
534                                  nfs_saddr, nfs_salen, &nfs_pmap)) {
535                 errno = ESPIPE;
536                 if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
537                         errno = EOPNOTSUPP;
538                 else if (rpc_createerr.cf_error.re_errno != 0)
539                         errno = rpc_createerr.cf_error.re_errno;
540                 return 0;
541         }
542
543         if (!nfs_construct_new_options(options, nfs_saddr, &nfs_pmap,
544                                         mnt_saddr, &mnt_pmap)) {
545                 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO)
546                         errno = EPROTONOSUPPORT;
547                 else
548                         errno = EINVAL;
549                 return 0;
550         }
551
552 out:
553         errno = 0;
554         return 1;
555 }
556
557 /*
558  * Do the mount(2) system call.
559  *
560  * Returns TRUE if successful, otherwise FALSE.
561  * "errno" is set to reflect the individual error.
562  */
563 static int nfs_sys_mount(struct nfsmount_info *mi, struct mount_options *opts)
564 {
565         char *options = NULL;
566         int result;
567
568         if (po_join(opts, &options) == PO_FAILED) {
569                 errno = EIO;
570                 return 0;
571         }
572
573         if (mi->fake)
574                 return 1;
575
576         result = mount(mi->spec, mi->node, mi->type,
577                         mi->flags & ~(MS_USER|MS_USERS), options);
578         if (verbose && result) {
579                 int save = errno;
580                 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
581                 errno = save;
582         }
583         return !result;
584 }
585
586 static int nfs_do_mount_v3v2(struct nfsmount_info *mi,
587                 struct sockaddr *sap, socklen_t salen)
588 {
589         struct mount_options *options = po_dup(mi->options);
590         int result = 0;
591
592         if (!options) {
593                 errno = ENOMEM;
594                 return result;
595         }
596         errno = 0;
597         if (!nfs_append_addr_option(sap, salen, options)) {
598                 if (errno == 0)
599                         errno = EINVAL;
600                 goto out_fail;
601         }
602
603         if (!nfs_fix_mounthost_option(options, mi->hostname)) {
604                 if (errno == 0)
605                         errno = EINVAL;
606                 goto out_fail;
607         }
608         if (!mi->fake && !nfs_verify_lock_option(options)) {
609                 if (errno == 0)
610                         errno = EINVAL;
611                 goto out_fail;
612         }
613
614         /*
615          * Options we negotiate below may be stale by the time this
616          * file system is unmounted.  In order to force umount.nfs
617          * to renegotiate with the server, only write the user-
618          * specified options, and not negotiated options, to /etc/mtab.
619          */
620         if (po_join(options, mi->extra_opts) == PO_FAILED) {
621                 errno = ENOMEM;
622                 goto out_fail;
623         }
624
625         if (verbose)
626                 printf(_("%s: trying text-based options '%s'\n"),
627                         progname, *mi->extra_opts);
628
629         if (!nfs_rewrite_pmap_mount_options(options))
630                 goto out_fail;
631
632         result = nfs_sys_mount(mi, options);
633
634 out_fail:
635         po_destroy(options);
636         return result;
637 }
638
639 /*
640  * Attempt a "-t nfs vers=2" or "-t nfs vers=3" mount.
641  *
642  * Returns TRUE if successful, otherwise FALSE.
643  * "errno" is set to reflect the individual error.
644  */
645 static int nfs_try_mount_v3v2(struct nfsmount_info *mi)
646 {
647         struct addrinfo *ai;
648         int ret;
649
650         for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
651                 ret = nfs_do_mount_v3v2(mi, ai->ai_addr, ai->ai_addrlen);
652                 if (ret != 0)
653                         return ret;
654
655                 switch (errno) {
656                 case ECONNREFUSED:
657                 case EOPNOTSUPP:
658                 case EHOSTUNREACH:
659                         continue;
660                 default:
661                         break;
662                 }
663         }
664         return ret;
665 }
666
667 static int nfs_do_mount_v4(struct nfsmount_info *mi,
668                 struct sockaddr *sap, socklen_t salen)
669 {
670         struct mount_options *options = po_dup(mi->options);
671         int result = 0;
672
673         if (!options) {
674                 errno = ENOMEM;
675                 return result;
676         }
677
678         if (mi->version == 0) {
679                 if (po_contains(options, "mounthost") ||
680                         po_contains(options, "mountaddr") ||
681                         po_contains(options, "mountvers") ||
682                         po_contains(options, "mountproto")) {
683                 /*
684                  * Since these mountd options are set assume version 3
685                  * is wanted so error out with EPROTONOSUPPORT so the
686                  * protocol negation starts with v3.
687                  */
688                         errno = EPROTONOSUPPORT;
689                         goto out_fail;
690                 }
691                 if (po_append(options, "vers=4") == PO_FAILED) {
692                         errno = EINVAL;
693                         goto out_fail;
694                 }
695         }
696
697         if (!nfs_append_addr_option(sap, salen, options)) {
698                 errno = EINVAL;
699                 goto out_fail;
700         }
701
702         if (!nfs_append_clientaddr_option(sap, salen, options)) {
703                 errno = EINVAL;
704                 goto out_fail;
705         }
706
707         /*
708          * Update option string to be recorded in /etc/mtab.
709          */
710         if (po_join(options, mi->extra_opts) == PO_FAILED) {
711                 errno = ENOMEM;
712                 goto out_fail;
713         }
714
715         if (verbose)
716                 printf(_("%s: trying text-based options '%s'\n"),
717                         progname, *mi->extra_opts);
718
719         result = nfs_sys_mount(mi, options);
720
721 out_fail:
722         po_destroy(options);
723         return result;
724 }
725
726 /*
727  * Attempt a "-t nfs -o vers=4" or "-t nfs4" mount.
728  *
729  * Returns TRUE if successful, otherwise FALSE.
730  * "errno" is set to reflect the individual error.
731  */
732 static int nfs_try_mount_v4(struct nfsmount_info *mi)
733 {
734         struct addrinfo *ai;
735         int ret;
736
737         for (ai = mi->address; ai != NULL; ai = ai->ai_next) {
738                 ret = nfs_do_mount_v4(mi, ai->ai_addr, ai->ai_addrlen);
739                 if (ret != 0)
740                         return ret;
741
742                 switch (errno) {
743                 case ECONNREFUSED:
744                 case EHOSTUNREACH:
745                         continue;
746                 default:
747                         break;
748                 }
749         }
750         return ret;
751 }
752
753 /*
754  * Handle NFS version and transport protocol
755  * autonegotiation.
756  *
757  * When no version or protocol is specified on the
758  * command line, mount.nfs negotiates with the server
759  * to determine appropriate settings for the new
760  * mount point.
761  *
762  * Returns TRUE if successful, otherwise FALSE.
763  * "errno" is set to reflect the individual error.
764  */
765 static int nfs_autonegotiate(struct nfsmount_info *mi)
766 {
767         int result;
768
769         result = nfs_try_mount_v4(mi);
770         if (result)
771                 return result;
772                 
773         switch (errno) {
774         case EPROTONOSUPPORT:
775                 /* A clear indication that the server or our
776                  * client does not support NFS version 4. */
777                 goto fall_back;
778         case ENOENT:
779                 /* Legacy Linux servers don't export an NFS
780                  * version 4 pseudoroot. */
781                 goto fall_back;
782         case EPERM:
783                 /* Linux servers prior to 2.6.25 may return
784                  * EPERM when NFS version 4 is not supported. */
785                 goto fall_back;
786         default:
787                 return result;
788         }
789
790 fall_back:
791         return nfs_try_mount_v3v2(mi);
792 }
793
794 /*
795  * This is a single pass through the fg/bg loop.
796  *
797  * Returns TRUE if successful, otherwise FALSE.
798  * "errno" is set to reflect the individual error.
799  */
800 static int nfs_try_mount(struct nfsmount_info *mi)
801 {
802         int result = 0;
803
804         switch (mi->version) {
805         case 0:
806                 result = nfs_autonegotiate(mi);
807                 break;
808         case 2:
809         case 3:
810                 result = nfs_try_mount_v3v2(mi);
811                 break;
812         case 4:
813                 result = nfs_try_mount_v4(mi);
814                 break;
815         default:
816                 errno = EIO;
817         }
818
819         return result;
820 }
821
822 /*
823  * Distinguish between permanent and temporary errors.
824  *
825  * Basically, we retry if communication with the server has
826  * failed so far, but fail immediately if there is a local
827  * error (like a bad mount option).
828  *
829  * ESTALE is also a temporary error because some servers
830  * return ESTALE when a share is temporarily offline.
831  *
832  * Returns 1 if we should fail immediately, or 0 if we
833  * should retry.
834  */
835 static int nfs_is_permanent_error(int error)
836 {
837         switch (error) {
838         case ESTALE:
839         case ETIMEDOUT:
840         case ECONNREFUSED:
841         case EHOSTUNREACH:
842                 return 0;       /* temporary */
843         default:
844                 return 1;       /* permanent */
845         }
846 }
847
848 /*
849  * Handle "foreground" NFS mounts.
850  *
851  * Retry the mount request for as long as the 'retry=' option says.
852  *
853  * Returns a valid mount command exit code.
854  */
855 static int nfsmount_fg(struct nfsmount_info *mi)
856 {
857         unsigned int secs = 1;
858         time_t timeout;
859
860         timeout = nfs_parse_retry_option(mi->options,
861                                          NFS_DEF_FG_TIMEOUT_MINUTES);
862         if (verbose)
863                 printf(_("%s: timeout set for %s"),
864                         progname, ctime(&timeout));
865
866         for (;;) {
867                 if (nfs_try_mount(mi))
868                         return EX_SUCCESS;
869
870                 if (nfs_is_permanent_error(errno))
871                         break;
872
873                 if (time(NULL) > timeout) {
874                         errno = ETIMEDOUT;
875                         break;
876                 }
877
878                 if (errno != ETIMEDOUT) {
879                         if (sleep(secs))
880                                 break;
881                         secs <<= 1;
882                         if (secs > 10)
883                                 secs = 10;
884                 }
885         };
886
887         mount_error(mi->spec, mi->node, errno);
888         return EX_FAIL;
889 }
890
891 /*
892  * Handle "background" NFS mount [first try]
893  *
894  * Returns a valid mount command exit code.
895  *
896  * EX_BG should cause the caller to fork and invoke nfsmount_child.
897  */
898 static int nfsmount_parent(struct nfsmount_info *mi)
899 {
900         if (nfs_try_mount(mi))
901                 return EX_SUCCESS;
902
903         if (nfs_is_permanent_error(errno)) {
904                 mount_error(mi->spec, mi->node, errno);
905                 return EX_FAIL;
906         }
907
908         sys_mount_errors(mi->hostname, errno, 1, 1);
909         return EX_BG;
910 }
911
912 /*
913  * Handle "background" NFS mount [retry daemon]
914  *
915  * Returns a valid mount command exit code: EX_SUCCESS if successful,
916  * EX_FAIL if a failure occurred.  There's nothing to catch the
917  * error return, though, so we use sys_mount_errors to log the
918  * failure.
919  */
920 static int nfsmount_child(struct nfsmount_info *mi)
921 {
922         unsigned int secs = 1;
923         time_t timeout;
924
925         timeout = nfs_parse_retry_option(mi->options,
926                                          NFS_DEF_BG_TIMEOUT_MINUTES);
927
928         for (;;) {
929                 if (sleep(secs))
930                         break;
931                 secs <<= 1;
932                 if (secs > 120)
933                         secs = 120;
934
935                 if (nfs_try_mount(mi))
936                         return EX_SUCCESS;
937
938                 if (nfs_is_permanent_error(errno))
939                         break;
940
941                 if (time(NULL) > timeout)
942                         break;
943
944                 sys_mount_errors(mi->hostname, errno, 1, 1);
945         };
946
947         sys_mount_errors(mi->hostname, errno, 1, 0);
948         return EX_FAIL;
949 }
950
951 /*
952  * Handle "background" NFS mount
953  *
954  * Returns a valid mount command exit code.
955  */
956 static int nfsmount_bg(struct nfsmount_info *mi)
957 {
958         if (!mi->child)
959                 return nfsmount_parent(mi);
960         else
961                 return nfsmount_child(mi);
962 }
963
964 /*
965  * Usually all that is needed for an NFS remount is to change
966  * generic mount options like "sync" or "ro".  These generic
967  * options are controlled by mi->flags, not by text-based
968  * options, and no contact with the server is needed.
969  *
970  * Take care with the /etc/mtab entry for this mount; just
971  * calling update_mtab() will change an "-t nfs -o vers=4"
972  * mount to an "-t nfs -o remount" mount, and that will
973  * confuse umount.nfs.
974  *
975  * Returns a valid mount command exit code.
976  */
977 static int nfs_remount(struct nfsmount_info *mi)
978 {
979         if (nfs_sys_mount(mi, mi->options))
980                 return EX_SUCCESS;
981         return EX_FAIL;
982 }
983
984 /*
985  * Process mount options and try a mount system call.
986  *
987  * Returns a valid mount command exit code.
988  */
989 static const char *nfs_background_opttbl[] = {
990         "bg",
991         "fg",
992         NULL,
993 };
994
995 static int nfsmount_start(struct nfsmount_info *mi)
996 {
997         if (!nfs_validate_options(mi))
998                 return EX_FAIL;
999
1000         /*
1001          * Avoid retry and negotiation logic when remounting
1002          */
1003         if (mi->flags & MS_REMOUNT)
1004                 return nfs_remount(mi);
1005
1006         if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
1007                 return nfsmount_bg(mi);
1008         else
1009                 return nfsmount_fg(mi);
1010 }
1011
1012 /**
1013  * nfsmount_string - Mount an NFS file system using C string options
1014  * @spec: C string specifying remote share to mount ("hostname:path")
1015  * @node: C string pathname of local mounted-on directory
1016  * @type: C string that represents file system type ("nfs" or "nfs4")
1017  * @flags: MS_ style mount flags
1018  * @extra_opts: pointer to C string containing fs-specific mount options
1019  *              (input and output argument)
1020  * @fake: flag indicating whether to carry out the whole operation
1021  * @child: one if this is a mount daemon (bg)
1022  *
1023  * Returns a valid mount command exit code.
1024  */
1025 int nfsmount_string(const char *spec, const char *node, const char *type,
1026                     int flags, char **extra_opts, int fake, int child)
1027 {
1028         struct nfsmount_info mi = {
1029                 .spec           = spec,
1030                 .node           = node,
1031                 .address        = NULL,
1032                 .type           = type,
1033                 .extra_opts     = extra_opts,
1034                 .flags          = flags,
1035                 .fake           = fake,
1036                 .child          = child,
1037         };
1038         int retval = EX_FAIL;
1039
1040         mi.options = po_split(*extra_opts);
1041         if (mi.options) {
1042                 retval = nfsmount_start(&mi);
1043                 po_destroy(mi.options);
1044         } else
1045                 nfs_error(_("%s: internal option parsing error"), progname);
1046
1047         freeaddrinfo(mi.address);
1048         free(mi.hostname);
1049         return retval;
1050 }