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