]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
Recently #include directives for autoconf's config.h file were added in
[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  * Set up mandatory mount options.
223  *
224  * Returns 1 if successful; otherwise zero.
225  */
226 static int set_mandatory_options(const char *type,
227                                  struct sockaddr_in *saddr,
228                                  struct mount_options *options)
229 {
230         if (!append_addr_option(saddr, options))
231                 return 0;
232
233         if (strncmp(type, "nfs4", 4) == 0) {
234                 if (!append_clientaddr_option(saddr, options))
235                         return 0;
236         } else {
237                 if (!fix_mounthost_option(options))
238                         return 0;
239         }
240
241         return 1;
242 }
243
244 /*
245  * Distinguish between permanent and temporary errors.
246  *
247  * Returns 0 if the passed-in error is temporary, thus the
248  * mount system call should be retried; returns one if the
249  * passed-in error is permanent, thus the mount system call
250  * should not be retried.
251  */
252 static int is_permanent_error(int error)
253 {
254         switch (error) {
255         case EACCES:
256         case ESTALE:
257         case ETIMEDOUT:
258         case ECONNREFUSED:
259                 return 0;       /* temporary */
260         default:
261                 return 1;       /* permanent */
262         }
263 }
264
265 /*
266  * Reconstruct the mount option string based on a portmapper probe
267  * of the server.  Returns one if the server's portmapper returned
268  * something we can use, otherwise zero.
269  *
270  * To handle version and transport protocol fallback properly, we
271  * need to parse some of the mount options in order to set up a
272  * portmap probe.  Mount options that rewrite_mount_options()
273  * doesn't recognize are left alone.
274  *
275  * Returns a new group of mount options if successful; otherwise
276  * NULL is returned if some failure occurred.
277  */
278 static struct mount_options *rewrite_mount_options(char *str)
279 {
280         struct mount_options *options;
281         char *option, new_option[64];
282         clnt_addr_t mnt_server = { };
283         clnt_addr_t nfs_server = { };
284         int p;
285
286         options = po_split(str);
287         if (!options)
288                 return NULL;
289
290         option = po_get(options, "addr");
291         if (option) {
292                 nfs_server.saddr.sin_family = AF_INET;
293                 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
294                         goto err;
295         } else
296                 goto err;
297
298         option = po_get(options, "mountaddr");
299         if (option) {
300                 mnt_server.saddr.sin_family = AF_INET;
301                 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
302                         goto err;
303         } else
304                 memcpy(&mnt_server.saddr, &nfs_server.saddr,
305                                 sizeof(mnt_server.saddr));
306
307         option = po_get(options, "mountport");
308         if (option)
309                 mnt_server.pmap.pm_port = atoi(option);
310         mnt_server.pmap.pm_prog = MOUNTPROG;
311         option = po_get(options, "mountvers");
312         if (option)
313                 mnt_server.pmap.pm_vers = atoi(option);
314
315         option = po_get(options, "port");
316         if (option) {
317                 nfs_server.pmap.pm_port = atoi(option);
318                 po_remove_all(options, "port");
319         }
320         nfs_server.pmap.pm_prog = NFS_PROGRAM;
321
322         option = po_get(options, "nfsvers");
323         if (option) {
324                 nfs_server.pmap.pm_vers = atoi(option);
325                 po_remove_all(options, "nfsvers");
326         }
327         option = po_get(options, "vers");
328         if (option) {
329                 nfs_server.pmap.pm_vers = atoi(option);
330                 po_remove_all(options, "vers");
331         }
332         option = po_get(options, "proto");
333         if (option) {
334                 if (strcmp(option, "tcp") == 0) {
335                         nfs_server.pmap.pm_prot = IPPROTO_TCP;
336                         po_remove_all(options, "proto");
337                 }
338                 if (strcmp(option, "udp") == 0) {
339                         nfs_server.pmap.pm_prot = IPPROTO_UDP;
340                         po_remove_all(options, "proto");
341                 }
342         }
343         p = po_rightmost(options, "tcp", "udp");
344         switch (p) {
345         case PO_KEY2_RIGHTMOST:
346                 nfs_server.pmap.pm_prot = IPPROTO_UDP;
347                 break;
348         case PO_KEY1_RIGHTMOST:
349                 nfs_server.pmap.pm_prot = IPPROTO_TCP;
350                 break;
351         }
352         po_remove_all(options, "tcp");
353         po_remove_all(options, "udp");
354
355         if (!probe_bothports(&mnt_server, &nfs_server)) {
356                 rpc_mount_errors("rpcbind", 0, 0);
357                 goto err;
358         }
359
360         snprintf(new_option, sizeof(new_option) - 1,
361                  "nfsvers=%lu", nfs_server.pmap.pm_vers);
362         if (po_append(options, new_option) == PO_FAILED)
363                 goto err;
364
365         if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
366                 snprintf(new_option, sizeof(new_option) - 1,
367                          "proto=tcp");
368         else
369                 snprintf(new_option, sizeof(new_option) - 1,
370                          "proto=udp");
371         if (po_append(options, new_option) == PO_FAILED)
372                 goto err;
373
374         if (nfs_server.pmap.pm_port != NFS_PORT) {
375                 snprintf(new_option, sizeof(new_option) - 1,
376                          "port=%lu", nfs_server.pmap.pm_port);
377                 if (po_append(options, new_option) == PO_FAILED)
378                         goto err;
379
380         }
381
382         return options;
383
384 err:
385         po_destroy(options);
386         return NULL;
387 }
388
389 /*
390  * Retry an NFS mount that failed because the requested service isn't
391  * available on the server.
392  *
393  * Returns 1 if successful.  Otherwise, returns zero.
394  * "errno" is set to reflect the individual error.
395  *
396  * Side effect: If the retry is successful, both 'options' and
397  * 'extra_opts' are updated to reflect the mount options that worked.
398  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
399  */
400 static int retry_nfsmount(const char *spec, const char *node,
401                         int flags, struct mount_options *options,
402                         int fake, char **extra_opts)
403 {
404         struct mount_options *retry_options;
405         char *retry_str = NULL;
406
407         retry_options = rewrite_mount_options(*extra_opts);
408         if (!retry_options) {
409                 errno = EIO;
410                 return 0;
411         }
412
413         if (po_join(retry_options, &retry_str) == PO_FAILED) {
414                 po_destroy(retry_options);
415                 errno = EIO;
416                 return 0;
417         }
418
419         if (verbose)
420                 printf(_("%s: text-based options (retry): '%s'\n"),
421                         progname, retry_str);
422
423         if (!mount(spec, node, "nfs",
424                                 flags & ~(MS_USER|MS_USERS), retry_str)) {
425                 free(*extra_opts);
426                 *extra_opts = retry_str;
427                 po_replace(options, retry_options);
428                 return 1;
429         }
430
431         po_destroy(retry_options);
432         free(retry_str);
433         return 0;
434 }
435
436 /*
437  * Attempt an NFSv2/3 mount via a mount(2) system call.  If the kernel
438  * claims the requested service isn't supported on the server, probe
439  * the server to see what's supported, rewrite the mount options,
440  * and retry the request.
441  *
442  * Returns 1 if successful.  Otherwise, returns zero.
443  * "errno" is set to reflect the individual error.
444  *
445  * Side effect: If the retry is successful, both 'options' and
446  * 'extra_opts' are updated to reflect the mount options that worked.
447  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
448  */
449 static int try_nfs23mount(const char *spec, const char *node,
450                           int flags, struct mount_options *options,
451                           int fake, char **extra_opts)
452 {
453         if (po_join(options, extra_opts) == PO_FAILED) {
454                 errno = EIO;
455                 return 0;
456         }
457
458         if (verbose)
459                 printf(_("%s: text-based options: '%s'\n"),
460                         progname, *extra_opts);
461
462         if (fake)
463                 return 1;
464
465         if (!mount(spec, node, "nfs",
466                                 flags & ~(MS_USER|MS_USERS), *extra_opts))
467                 return 1;
468
469         /*
470          * The kernel returns EOPNOTSUPP if the RPC bind failed,
471          * and EPROTONOSUPPORT if the version isn't supported.
472          */
473         if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
474                 return 0;
475
476         return retry_nfsmount(spec, node, flags, options, fake, extra_opts);
477 }
478
479 /*
480  * Attempt an NFS v4 mount via a mount(2) system call.
481  *
482  * Returns 1 if successful.  Otherwise, returns zero.
483  * "errno" is set to reflect the individual error.
484  */
485 static int try_nfs4mount(const char *spec, const char *node,
486                          int flags, struct mount_options *options,
487                          int fake, char **extra_opts)
488 {
489         if (po_join(options, extra_opts) == PO_FAILED) {
490                 errno = EIO;
491                 return 0;
492         }
493
494         if (verbose)
495                 printf(_("%s: text-based options: '%s'\n"),
496                         progname, *extra_opts);
497
498         if (fake)
499                 return 1;
500
501         if (!mount(spec, node, "nfs4",
502                                 flags & ~(MS_USER|MS_USERS), *extra_opts))
503                 return 1;
504         return 0;
505 }
506
507 /*
508  * Try the mount(2) system call.
509  *
510  * Returns 1 if successful.  Otherwise, returns zero.
511  * "errno" is set to reflect the individual error.
512  */
513 static int try_mount(const char *spec, const char *node, const char *type,
514                      int flags, struct mount_options *options, int fake,
515                      char **extra_opts)
516 {
517         if (strncmp(type, "nfs4", 4) == 0)
518                 return try_nfs4mount(spec, node, flags,
519                                         options, fake, extra_opts);
520         else
521                 return try_nfs23mount(spec, node, flags,
522                                         options, fake, extra_opts);
523 }
524
525 /*
526  * Handle "foreground" NFS mounts.
527  *
528  * Retry the mount request for as long as the 'retry=' option says.
529  *
530  * Returns a valid mount command exit code.
531  */
532 static int nfsmount_fg(const char *spec, const char *node,
533                        const char *type, int flags,
534                        struct mount_options *options, int fake,
535                        char **extra_opts)
536 {
537         unsigned int secs = 1;
538         time_t timeout = time(NULL);
539         char *retry;
540
541         timeout += 60 * 2;              /* default: 2 minutes */
542         retry = po_get(options, "retry");
543         if (retry)
544                 timeout += 60 * atoi(retry);
545
546         if (verbose)
547                 printf(_("%s: timeout set for %s"),
548                         progname, ctime(&timeout));
549
550         for (;;) {
551                 if (try_mount(spec, node, type, flags,
552                                         options, fake, extra_opts))
553                         return EX_SUCCESS;
554
555                 if (is_permanent_error(errno))
556                         break;
557
558                 if (time(NULL) > timeout) {
559                         errno = ETIMEDOUT;
560                         break;
561                 }
562
563                 if (errno != ETIMEDOUT) {
564                         if (sleep(secs))
565                                 break;
566                         secs <<= 1;
567                         if (secs > 10)
568                                 secs = 10;
569                 }
570         };
571
572         mount_error(spec, node, errno);
573         return EX_FAIL;
574 }
575
576 /*
577  * Handle "background" NFS mount [first try]
578  *
579  * Returns a valid mount command exit code.
580  *
581  * EX_BG should cause the caller to fork and invoke nfsmount_child.
582  */
583 static int nfsmount_parent(const char *spec, const char *node,
584                            const char *type, char *hostname, int flags,
585                            struct mount_options *options,
586                            int fake, char **extra_opts)
587 {
588         if (try_mount(spec, node, type, flags, options,
589                                         fake, extra_opts))
590                 return EX_SUCCESS;
591
592         if (is_permanent_error(errno)) {
593                 mount_error(spec, node, errno);
594                 return EX_FAIL;
595         }
596
597         sys_mount_errors(hostname, errno, 1, 1);
598         return EX_BG;
599 }
600
601 /*
602  * Handle "background" NFS mount [retry daemon]
603  *
604  * Returns a valid mount command exit code: EX_SUCCESS if successful,
605  * EX_FAIL if a failure occurred.  There's nothing to catch the
606  * error return, though, so we use sys_mount_errors to log the
607  * failure.
608  */
609 static int nfsmount_child(const char *spec, const char *node,
610                           const char *type, char *hostname, int flags,
611                           struct mount_options *options,
612                           int fake, char **extra_opts)
613 {
614         unsigned int secs = 1;
615         time_t timeout = time(NULL);
616         char *retry;
617
618         timeout += 60 * 10000;          /* default: 10,000 minutes */
619         retry = po_get(options, "retry");
620         if (retry)
621                 timeout += 60 * atoi(retry);
622
623         for (;;) {
624                 if (sleep(secs))
625                         break;
626                 secs <<= 1;
627                 if (secs > 120)
628                         secs = 120;
629
630                 if (try_mount(spec, node, type, flags, options,
631                                                         fake, extra_opts))
632                         return EX_SUCCESS;
633
634                 if (is_permanent_error(errno))
635                         break;
636
637                 if (time(NULL) > timeout)
638                         break;
639
640                 sys_mount_errors(hostname, errno, 1, 1);
641         };
642
643         sys_mount_errors(hostname, errno, 1, 0);
644         return EX_FAIL;
645 }
646
647 /*
648  * Handle "background" NFS mount
649  *
650  * Returns a valid mount command exit code.
651  */
652 static int nfsmount_bg(const char *spec, const char *node,
653                           const char *type, char *hostname, int flags,
654                           struct mount_options *options,
655                           int fake, int child, char **extra_opts)
656 {
657         if (!child)
658                 return nfsmount_parent(spec, node, type, hostname, flags,
659                                         options, fake, extra_opts);
660         else
661                 return nfsmount_child(spec, node, type, hostname, flags,
662                                         options, fake, extra_opts);
663 }
664
665 /**
666  * nfsmount_string - Mount an NFS file system using C string options
667  * @spec: C string specifying remote share to mount ("hostname:path")
668  * @node: C string pathname of local mounted-on directory
669  * @type: C string that represents file system type ("nfs" or "nfs4")
670  * @flags: MS_ style mount flags
671  * @extra_opts: pointer to C string containing fs-specific mount options
672  *              (input and output argument)
673  * @fake: flag indicating whether to carry out the whole operation
674  * @child: one if this is a mount daemon (bg)
675  */
676 int nfsmount_string(const char *spec, const char *node, const char *type,
677                     int flags, char **extra_opts, int fake, int child)
678 {
679         struct mount_options *options = NULL;
680         struct sockaddr_in saddr;
681         char *hostname;
682         int retval = EX_FAIL;
683
684         if (!parse_devname(spec, &hostname))
685                 return retval;
686         if (!fill_ipv4_sockaddr(hostname, &saddr))
687                 goto fail;
688
689         options = po_split(*extra_opts);
690         if (!options) {
691                 nfs_error(_("%s: internal option parsing error"), progname);
692                 goto fail;
693         }
694
695         if (!set_mandatory_options(type, &saddr, options))
696                 goto out;
697
698         if (po_rightmost(options, "bg", "fg") == PO_KEY1_RIGHTMOST)
699                 retval = nfsmount_bg(spec, node, type, hostname, flags,
700                                         options, fake, child, extra_opts);
701         else
702                 retval = nfsmount_fg(spec, node, type, flags, options,
703                                                         fake, extra_opts);
704
705 out:
706         po_destroy(options);
707 fail:
708         free(hostname);
709         return retval;
710 }