]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
a51cf0b720fbfee20d085c959911c5bf91d30826
[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 <ctype.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <time.h>
36
37 #include <sys/socket.h>
38 #include <sys/mount.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41
42 #include "xcommon.h"
43 #include "mount.h"
44 #include "nls.h"
45 #include "nfs_mount.h"
46 #include "mount_constants.h"
47 #include "stropts.h"
48 #include "error.h"
49 #include "network.h"
50 #include "parse_opt.h"
51 #include "version.h"
52 #include "parse_dev.h"
53
54 #ifdef HAVE_RPCSVC_NFS_PROT_H
55 #include <rpcsvc/nfs_prot.h>
56 #else
57 #include <linux/nfs.h>
58 #define nfsstat nfs_stat
59 #endif
60
61 #ifndef NFS_PORT
62 #define NFS_PORT 2049
63 #endif
64
65 #ifndef NFS_MAXHOSTNAME
66 #define NFS_MAXHOSTNAME         (255)
67 #endif
68
69 #ifndef NFS_MAXPATHNAME
70 #define NFS_MAXPATHNAME         (1024)
71 #endif
72
73 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
74 #define NFS_DEF_FG_TIMEOUT_MINUTES      (2u)
75 #endif
76
77 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
78 #define NFS_DEF_BG_TIMEOUT_MINUTES      (10000u)
79 #endif
80
81 extern int nfs_mount_data_version;
82 extern char *progname;
83 extern int verbose;
84 extern int sloppy;
85
86 struct nfsmount_info {
87         const char              *spec,          /* server:/path */
88                                 *node,          /* mounted-on dir */
89                                 *type;          /* "nfs" or "nfs4" */
90         char                    *hostname;      /* server's hostname */
91
92         struct mount_options    *options;       /* parsed mount options */
93         char                    **extra_opts;   /* string for /etc/mtab */
94
95         int                     flags,          /* MS_ flags */
96                                 fake,           /* actually do the mount? */
97                                 child;          /* forked bg child? */
98
99         sa_family_t             family;         /* supported address family */
100 };
101
102 /*
103  * Obtain a retry timeout value based on the value of the "retry=" option.
104  *
105  * Returns a time_t timeout timestamp, in seconds.
106  */
107 static time_t nfs_parse_retry_option(struct mount_options *options,
108                                      unsigned int timeout_minutes)
109 {
110         char *retry_option, *endptr;
111
112         retry_option = po_get(options, "retry");
113         if (retry_option) {
114                 long tmp;
115
116                 errno = 0;
117                 tmp = strtol(retry_option, &endptr, 10);
118                 if (errno == 0 && endptr != retry_option && tmp >= 0)
119                         timeout_minutes = tmp;
120                 else if (verbose)
121                         nfs_error(_("%s: invalid retry timeout was specified; "
122                                         "using default timeout"), progname);
123         }
124
125         return time(NULL) + (time_t)(timeout_minutes * 60);
126 }
127
128 /*
129  * Convert the passed-in sockaddr-style address to presentation
130  * format, then append an option of the form "keyword=address".
131  *
132  * Returns 1 if the option was appended successfully; otherwise zero.
133  */
134 static int nfs_append_generic_address_option(const struct sockaddr *sap,
135                                              const socklen_t salen,
136                                              const char *keyword,
137                                              struct mount_options *options)
138 {
139         char address[NI_MAXHOST];
140         char new_option[512];
141
142         if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
143                 goto out_err;
144
145         if (snprintf(new_option, sizeof(new_option), "%s=%s",
146                                         keyword, address) >= sizeof(new_option))
147                 goto out_err;
148
149         if (po_append(options, new_option) != PO_SUCCEEDED)
150                 goto out_err;
151
152         return 1;
153
154 out_err:
155         nfs_error(_("%s: failed to construct %s option"), progname, keyword);
156         return 0;
157 }
158
159 /*
160  * Append the 'addr=' option to the options string to pass a resolved
161  * server address to the kernel.  After a successful mount, this address
162  * is also added to /etc/mtab for use when unmounting.
163  *
164  * If 'addr=' is already present, we strip it out.  This prevents users
165  * from setting a bogus 'addr=' option themselves, and also allows bg
166  * retries to recompute the server's address, in case it has changed.
167  *
168  * Returns 1 if 'addr=' option appended successfully;
169  * otherwise zero.
170  */
171 static int nfs_append_addr_option(const struct sockaddr *sap,
172                                   socklen_t salen,
173                                   struct mount_options *options)
174 {
175         po_remove_all(options, "addr");
176         return nfs_append_generic_address_option(sap, salen, "addr", options);
177 }
178
179 /*
180  * Called to discover our address and append an appropriate 'clientaddr='
181  * option to the options string.
182  *
183  * Returns 1 if 'clientaddr=' option created successfully or if
184  * 'clientaddr=' option is already present; otherwise zero.
185  */
186 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
187                                         socklen_t salen,
188                                         struct mount_options *options)
189 {
190         struct sockaddr_storage dummy;
191         struct sockaddr *my_addr = (struct sockaddr *)&dummy;
192         socklen_t my_len = sizeof(dummy);
193
194         if (po_contains(options, "clientaddr") == PO_FOUND)
195                 return 1;
196
197         nfs_callback_address(sap, salen, my_addr, &my_len);
198
199         return nfs_append_generic_address_option(my_addr, my_len,
200                                                         "clientaddr", options);
201 }
202
203 /*
204  * Resolve the 'mounthost=' hostname and append a new option using
205  * the resulting address.
206  */
207 static int nfs_fix_mounthost_option(const sa_family_t family,
208                                     struct mount_options *options)
209 {
210         struct sockaddr_storage dummy;
211         struct sockaddr *sap = (struct sockaddr *)&dummy;
212         socklen_t salen = sizeof(dummy);
213         char *mounthost;
214
215         mounthost = po_get(options, "mounthost");
216         if (!mounthost)
217                 return 1;
218
219         if (!nfs_name_to_address(mounthost, family, sap, &salen)) {
220                 nfs_error(_("%s: unable to determine mount server's address"),
221                                 progname);
222                 return 0;
223         }
224
225         return nfs_append_generic_address_option(sap, salen,
226                                                         "mountaddr", options);
227 }
228
229 /*
230  * Returns zero if the "lock" option is in effect, but statd
231  * can't be started.  Otherwise, returns 1.
232  */
233 static int verify_lock_option(struct mount_options *options)
234 {
235         if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
236                 return 1;
237
238         if (!start_statd()) {
239                 nfs_error(_("%s: rpc.statd is not running but is "
240                             "required for remote locking."), progname);
241                 nfs_error(_("%s: Either use '-o nolock' to keep "
242                             "locks local, or start statd."), progname);
243                 return 0;
244         }
245
246         return 1;
247 }
248
249 static int nfs_append_sloppy_option(struct mount_options *options)
250 {
251         if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
252                 return 1;
253
254         if (po_append(options, "sloppy") == PO_FAILED)
255                 return 0;
256         return 1;
257 }
258
259 /*
260  * Set up mandatory NFS mount options.
261  *
262  * Returns 1 if successful; otherwise zero.
263  */
264 static int nfs_validate_options(struct nfsmount_info *mi)
265 {
266         struct sockaddr_storage dummy;
267         struct sockaddr *sap = (struct sockaddr *)&dummy;
268         socklen_t salen = sizeof(dummy);
269
270         if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
271                 return 0;
272
273         if (!nfs_name_to_address(mi->hostname, mi->family, sap, &salen))
274                 return 0;
275
276         if (strncmp(mi->type, "nfs4", 4) == 0) {
277                 if (!nfs_append_clientaddr_option(sap, salen, mi->options))
278                         return 0;
279         } else {
280                 if (!nfs_fix_mounthost_option(mi->family, mi->options))
281                         return 0;
282                 if (!mi->fake && !verify_lock_option(mi->options))
283                         return 0;
284         }
285
286         if (!nfs_append_sloppy_option(mi->options))
287                 return 0;
288
289         return nfs_append_addr_option(sap, salen, mi->options);
290 }
291
292 /*
293  * Distinguish between permanent and temporary errors.
294  *
295  * Returns 0 if the passed-in error is temporary, thus the
296  * mount system call should be retried; returns one if the
297  * passed-in error is permanent, thus the mount system call
298  * should not be retried.
299  */
300 static int is_permanent_error(int error)
301 {
302         switch (error) {
303         case ESTALE:
304         case ETIMEDOUT:
305         case ECONNREFUSED:
306                 return 0;       /* temporary */
307         default:
308                 return 1;       /* permanent */
309         }
310 }
311
312 /*
313  * Reconstruct the mount option string based on a portmapper probe
314  * of the server.  Returns one if the server's portmapper returned
315  * something we can use, otherwise zero.
316  *
317  * To handle version and transport protocol fallback properly, we
318  * need to parse some of the mount options in order to set up a
319  * portmap probe.  Mount options that rewrite_mount_options()
320  * doesn't recognize are left alone.
321  *
322  * Returns a new group of mount options if successful; otherwise
323  * NULL is returned if some failure occurred.
324  */
325 static struct mount_options *rewrite_mount_options(char *str)
326 {
327         struct mount_options *options;
328         char *option, new_option[64];
329         clnt_addr_t mnt_server = { };
330         clnt_addr_t nfs_server = { };
331         int p;
332
333         options = po_split(str);
334         if (!options) {
335                 errno = EFAULT;
336                 return NULL;
337         }
338
339         errno = EINVAL;
340         option = po_get(options, "addr");
341         if (option) {
342                 nfs_server.saddr.sin_family = AF_INET;
343                 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
344                         goto err;
345         } else
346                 goto err;
347
348         option = po_get(options, "mountaddr");
349         if (option) {
350                 mnt_server.saddr.sin_family = AF_INET;
351                 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
352                         goto err;
353         } else
354                 memcpy(&mnt_server.saddr, &nfs_server.saddr,
355                                 sizeof(mnt_server.saddr));
356
357         option = po_get(options, "mountport");
358         if (option)
359                 mnt_server.pmap.pm_port = atoi(option);
360         mnt_server.pmap.pm_prog = MOUNTPROG;
361         option = po_get(options, "mountvers");
362         if (option)
363                 mnt_server.pmap.pm_vers = atoi(option);
364
365         option = po_get(options, "port");
366         if (option) {
367                 nfs_server.pmap.pm_port = atoi(option);
368                 po_remove_all(options, "port");
369         }
370         nfs_server.pmap.pm_prog = NFS_PROGRAM;
371
372         option = po_get(options, "nfsvers");
373         if (option) {
374                 nfs_server.pmap.pm_vers = atoi(option);
375                 po_remove_all(options, "nfsvers");
376         }
377         option = po_get(options, "vers");
378         if (option) {
379                 nfs_server.pmap.pm_vers = atoi(option);
380                 po_remove_all(options, "vers");
381         }
382         option = po_get(options, "proto");
383         if (option) {
384                 if (strcmp(option, "tcp") == 0) {
385                         nfs_server.pmap.pm_prot = IPPROTO_TCP;
386                         po_remove_all(options, "proto");
387                 }
388                 if (strcmp(option, "udp") == 0) {
389                         nfs_server.pmap.pm_prot = IPPROTO_UDP;
390                         po_remove_all(options, "proto");
391                 }
392         }
393         p = po_rightmost(options, "tcp", "udp");
394         switch (p) {
395         case PO_KEY2_RIGHTMOST:
396                 nfs_server.pmap.pm_prot = IPPROTO_UDP;
397                 break;
398         case PO_KEY1_RIGHTMOST:
399                 nfs_server.pmap.pm_prot = IPPROTO_TCP;
400                 break;
401         }
402         po_remove_all(options, "tcp");
403         po_remove_all(options, "udp");
404
405         if (!probe_bothports(&mnt_server, &nfs_server)) {
406                 errno = ESPIPE;
407                 goto err;
408         }
409
410         snprintf(new_option, sizeof(new_option) - 1,
411                  "nfsvers=%lu", nfs_server.pmap.pm_vers);
412         if (po_append(options, new_option) == PO_FAILED)
413                 goto err;
414
415         if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
416                 snprintf(new_option, sizeof(new_option) - 1,
417                          "proto=tcp");
418         else
419                 snprintf(new_option, sizeof(new_option) - 1,
420                          "proto=udp");
421         if (po_append(options, new_option) == PO_FAILED)
422                 goto err;
423
424         if (nfs_server.pmap.pm_port != NFS_PORT) {
425                 snprintf(new_option, sizeof(new_option) - 1,
426                          "port=%lu", nfs_server.pmap.pm_port);
427                 if (po_append(options, new_option) == PO_FAILED)
428                         goto err;
429
430         }
431
432         errno = 0;
433         return options;
434
435 err:
436         po_destroy(options);
437         return NULL;
438 }
439
440 /*
441  * Do the mount(2) system call.
442  *
443  * Returns 1 if successful, otherwise zero.
444  * "errno" is set to reflect the individual error.
445  */
446 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
447                          const char *options)
448 {
449         int result;
450
451         result = mount(mi->spec, mi->node, type,
452                                 mi->flags & ~(MS_USER|MS_USERS), options);
453         if (verbose && result) {
454                 int save = errno;
455                 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
456                 errno = save;
457         }
458         return !result;
459 }
460
461 /*
462  * Retry an NFS mount that failed because the requested service isn't
463  * available on the server.
464  *
465  * Returns 1 if successful.  Otherwise, returns zero.
466  * "errno" is set to reflect the individual error.
467  *
468  * Side effect: If the retry is successful, both 'options' and
469  * 'extra_opts' are updated to reflect the mount options that worked.
470  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
471  */
472 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
473 {
474         struct mount_options *retry_options;
475         char *retry_str = NULL;
476         char **extra_opts = mi->extra_opts;
477
478         retry_options = rewrite_mount_options(*extra_opts);
479         if (!retry_options)
480                 return 0;
481
482         if (po_join(retry_options, &retry_str) == PO_FAILED) {
483                 po_destroy(retry_options);
484                 errno = EIO;
485                 return 0;
486         }
487
488         if (verbose)
489                 printf(_("%s: text-based options (retry): '%s'\n"),
490                         progname, retry_str);
491
492         if (!nfs_sys_mount(mi, "nfs", retry_str)) {
493                 po_destroy(retry_options);
494                 free(retry_str);
495                 return 0;
496         }
497
498         free(*extra_opts);
499         *extra_opts = retry_str;
500         po_replace(mi->options, retry_options);
501         return 1;
502 }
503
504 /*
505  * Attempt an NFSv2/3 mount via a mount(2) system call.  If the kernel
506  * claims the requested service isn't supported on the server, probe
507  * the server to see what's supported, rewrite the mount options,
508  * and retry the request.
509  *
510  * Returns 1 if successful.  Otherwise, returns zero.
511  * "errno" is set to reflect the individual error.
512  *
513  * Side effect: If the retry is successful, both 'options' and
514  * 'extra_opts' are updated to reflect the mount options that worked.
515  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
516  */
517 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
518 {
519         char **extra_opts = mi->extra_opts;
520
521         if (po_join(mi->options, extra_opts) == PO_FAILED) {
522                 errno = EIO;
523                 return 0;
524         }
525
526         if (verbose)
527                 printf(_("%s: text-based options: '%s'\n"),
528                         progname, *extra_opts);
529
530         if (mi->fake)
531                 return 1;
532
533         if (nfs_sys_mount(mi, "nfs", *extra_opts))
534                 return 1;
535
536         /*
537          * The kernel returns EOPNOTSUPP if the RPC bind failed,
538          * and EPROTONOSUPPORT if the version isn't supported.
539          */
540         if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
541                 return 0;
542
543         return nfs_retry_nfs23mount(mi);
544 }
545
546 /*
547  * Attempt an NFS v4 mount via a mount(2) system call.
548  *
549  * Returns 1 if successful.  Otherwise, returns zero.
550  * "errno" is set to reflect the individual error.
551  */
552 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
553 {
554         char **extra_opts = mi->extra_opts;
555
556         if (po_join(mi->options, extra_opts) == PO_FAILED) {
557                 errno = EIO;
558                 return 0;
559         }
560
561         if (verbose)
562                 printf(_("%s: text-based options: '%s'\n"),
563                         progname, *extra_opts);
564
565         if (mi->fake)
566                 return 1;
567
568         return nfs_sys_mount(mi, "nfs4", *extra_opts);
569 }
570
571 /*
572  * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
573  *
574  * Returns 1 if successful.  Otherwise, returns zero.
575  * "errno" is set to reflect the individual error.
576  */
577 static int nfs_try_mount(struct nfsmount_info *mi)
578 {
579         if (strncmp(mi->type, "nfs4", 4) == 0)
580                 return nfs_try_nfs4mount(mi);
581         else
582                 return nfs_try_nfs23mount(mi);
583 }
584
585 /*
586  * Handle "foreground" NFS mounts.
587  *
588  * Retry the mount request for as long as the 'retry=' option says.
589  *
590  * Returns a valid mount command exit code.
591  */
592 static int nfsmount_fg(struct nfsmount_info *mi)
593 {
594         unsigned int secs = 1;
595         time_t timeout;
596
597         timeout = nfs_parse_retry_option(mi->options,
598                                          NFS_DEF_FG_TIMEOUT_MINUTES);
599         if (verbose)
600                 printf(_("%s: timeout set for %s"),
601                         progname, ctime(&timeout));
602
603         for (;;) {
604                 if (nfs_try_mount(mi))
605                         return EX_SUCCESS;
606
607                 if (is_permanent_error(errno))
608                         break;
609
610                 if (time(NULL) > timeout) {
611                         errno = ETIMEDOUT;
612                         break;
613                 }
614
615                 if (errno != ETIMEDOUT) {
616                         if (sleep(secs))
617                                 break;
618                         secs <<= 1;
619                         if (secs > 10)
620                                 secs = 10;
621                 }
622         };
623
624         mount_error(mi->spec, mi->node, errno);
625         return EX_FAIL;
626 }
627
628 /*
629  * Handle "background" NFS mount [first try]
630  *
631  * Returns a valid mount command exit code.
632  *
633  * EX_BG should cause the caller to fork and invoke nfsmount_child.
634  */
635 static int nfsmount_parent(struct nfsmount_info *mi)
636 {
637         if (nfs_try_mount(mi))
638                 return EX_SUCCESS;
639
640         if (is_permanent_error(errno)) {
641                 mount_error(mi->spec, mi->node, errno);
642                 return EX_FAIL;
643         }
644
645         sys_mount_errors(mi->hostname, errno, 1, 1);
646         return EX_BG;
647 }
648
649 /*
650  * Handle "background" NFS mount [retry daemon]
651  *
652  * Returns a valid mount command exit code: EX_SUCCESS if successful,
653  * EX_FAIL if a failure occurred.  There's nothing to catch the
654  * error return, though, so we use sys_mount_errors to log the
655  * failure.
656  */
657 static int nfsmount_child(struct nfsmount_info *mi)
658 {
659         unsigned int secs = 1;
660         time_t timeout;
661
662         timeout = nfs_parse_retry_option(mi->options,
663                                          NFS_DEF_BG_TIMEOUT_MINUTES);
664
665         for (;;) {
666                 if (sleep(secs))
667                         break;
668                 secs <<= 1;
669                 if (secs > 120)
670                         secs = 120;
671
672                 if (nfs_try_mount(mi))
673                         return EX_SUCCESS;
674
675                 if (is_permanent_error(errno))
676                         break;
677
678                 if (time(NULL) > timeout)
679                         break;
680
681                 sys_mount_errors(mi->hostname, errno, 1, 1);
682         };
683
684         sys_mount_errors(mi->hostname, errno, 1, 0);
685         return EX_FAIL;
686 }
687
688 /*
689  * Handle "background" NFS mount
690  *
691  * Returns a valid mount command exit code.
692  */
693 static int nfsmount_bg(struct nfsmount_info *mi)
694 {
695         if (!mi->child)
696                 return nfsmount_parent(mi);
697         else
698                 return nfsmount_child(mi);
699 }
700
701 /*
702  * Process mount options and try a mount system call.
703  *
704  * Returns a valid mount command exit code.
705  */
706 static int nfsmount_start(struct nfsmount_info *mi)
707 {
708         if (!nfs_validate_options(mi))
709                 return EX_FAIL;
710
711         if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
712                 return nfsmount_bg(mi);
713         else
714                 return nfsmount_fg(mi);
715 }
716
717 /**
718  * nfsmount_string - Mount an NFS file system using C string options
719  * @spec: C string specifying remote share to mount ("hostname:path")
720  * @node: C string pathname of local mounted-on directory
721  * @type: C string that represents file system type ("nfs" or "nfs4")
722  * @flags: MS_ style mount flags
723  * @extra_opts: pointer to C string containing fs-specific mount options
724  *              (input and output argument)
725  * @fake: flag indicating whether to carry out the whole operation
726  * @child: one if this is a mount daemon (bg)
727  */
728 int nfsmount_string(const char *spec, const char *node, const char *type,
729                     int flags, char **extra_opts, int fake, int child)
730 {
731         struct nfsmount_info mi = {
732                 .spec           = spec,
733                 .node           = node,
734                 .type           = type,
735                 .extra_opts     = extra_opts,
736                 .flags          = flags,
737                 .fake           = fake,
738                 .child          = child,
739 #ifdef IPV6_SUPPORTED
740                 .family         = AF_UNSPEC,    /* either IPv4 or v6 */
741 #else
742                 .family         = AF_INET,      /* only IPv4 */
743 #endif
744         };
745         int retval = EX_FAIL;
746
747         mi.options = po_split(*extra_opts);
748         if (mi.options) {
749                 retval = nfsmount_start(&mi);
750                 po_destroy(mi.options);
751         } else
752                 nfs_error(_("%s: internal option parsing error"), progname);
753
754         free(mi.hostname);
755         return retval;
756 }