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