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