]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
Now that only the Section names are case-insensitive
[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
92 /*
93  * Obtain a retry timeout value based on the value of the "retry=" option.
94  *
95  * Returns a time_t timeout timestamp, in seconds.
96  */
97 static time_t nfs_parse_retry_option(struct mount_options *options,
98                                      unsigned int timeout_minutes)
99 {
100         long tmp;
101
102         switch (po_get_numeric(options, "retry", &tmp)) {
103         case PO_NOT_FOUND:
104                 break;
105         case PO_FOUND:
106                 if (tmp >= 0) {
107                         timeout_minutes = tmp;
108                         break;
109                 }
110         case PO_BAD_VALUE:
111                 if (verbose)
112                         nfs_error(_("%s: invalid retry timeout was specified; "
113                                         "using default timeout"), progname);
114                 break;
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         int len;
134
135         if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
136                 goto out_err;
137
138         len = snprintf(new_option, sizeof(new_option), "%s=%s",
139                                                 keyword, address);
140         if (len < 0 || (size_t)len >= sizeof(new_option))
141                 goto out_err;
142
143         if (po_append(options, new_option) != PO_SUCCEEDED)
144                 goto out_err;
145
146         return 1;
147
148 out_err:
149         nfs_error(_("%s: failed to construct %s option"), progname, keyword);
150         return 0;
151 }
152
153 /*
154  * Append the 'addr=' option to the options string to pass a resolved
155  * server address to the kernel.  After a successful mount, this address
156  * is also added to /etc/mtab for use when unmounting.
157  *
158  * If 'addr=' is already present, we strip it out.  This prevents users
159  * from setting a bogus 'addr=' option themselves, and also allows bg
160  * retries to recompute the server's address, in case it has changed.
161  *
162  * Returns 1 if 'addr=' option appended successfully;
163  * otherwise zero.
164  */
165 static int nfs_append_addr_option(const struct sockaddr *sap,
166                                   socklen_t salen,
167                                   struct mount_options *options)
168 {
169         po_remove_all(options, "addr");
170         return nfs_append_generic_address_option(sap, salen, "addr", options);
171 }
172
173 /*
174  * Called to discover our address and append an appropriate 'clientaddr='
175  * option to the options string.
176  *
177  * Returns 1 if 'clientaddr=' option created successfully or if
178  * 'clientaddr=' option is already present; otherwise zero.
179  */
180 static int nfs_append_clientaddr_option(const struct sockaddr *sap,
181                                         socklen_t salen,
182                                         struct mount_options *options)
183 {
184         struct sockaddr_storage dummy;
185         struct sockaddr *my_addr = (struct sockaddr *)&dummy;
186         socklen_t my_len = sizeof(dummy);
187
188         if (po_contains(options, "clientaddr") == PO_FOUND)
189                 return 1;
190
191         nfs_callback_address(sap, salen, my_addr, &my_len);
192
193         return nfs_append_generic_address_option(my_addr, my_len,
194                                                         "clientaddr", options);
195 }
196
197 /*
198  * Resolve the 'mounthost=' hostname and append a new option using
199  * the resulting address.
200  */
201 static int nfs_fix_mounthost_option(struct mount_options *options)
202 {
203         struct sockaddr_storage dummy;
204         struct sockaddr *sap = (struct sockaddr *)&dummy;
205         socklen_t salen = sizeof(dummy);
206         char *mounthost;
207
208         mounthost = po_get(options, "mounthost");
209         if (!mounthost)
210                 return 1;
211
212         if (!nfs_name_to_address(mounthost, sap, &salen)) {
213                 nfs_error(_("%s: unable to determine mount server's address"),
214                                 progname);
215                 return 0;
216         }
217
218         return nfs_append_generic_address_option(sap, salen,
219                                                         "mountaddr", options);
220 }
221
222 /*
223  * Returns zero if the "lock" option is in effect, but statd
224  * can't be started.  Otherwise, returns 1.
225  */
226 static const char *nfs_lock_opttbl[] = {
227         "nolock",
228         "lock",
229         NULL,
230 };
231
232 static int nfs_verify_lock_option(struct mount_options *options)
233 {
234         if (po_rightmost(options, nfs_lock_opttbl) == 0)
235                 return 1;
236
237         if (!start_statd()) {
238                 nfs_error(_("%s: rpc.statd is not running but is "
239                             "required for remote locking."), progname);
240                 nfs_error(_("%s: Either use '-o nolock' to keep "
241                             "locks local, or start statd."), progname);
242                 return 0;
243         }
244
245         return 1;
246 }
247
248 static int nfs_append_sloppy_option(struct mount_options *options)
249 {
250         if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
251                 return 1;
252
253         if (po_append(options, "sloppy") == PO_FAILED)
254                 return 0;
255         return 1;
256 }
257
258 /*
259  * Set up mandatory NFS mount options.
260  *
261  * Returns 1 if successful; otherwise zero.
262  */
263 static int nfs_validate_options(struct nfsmount_info *mi)
264 {
265         struct sockaddr_storage dummy;
266         struct sockaddr *sap = (struct sockaddr *)&dummy;
267         socklen_t salen = sizeof(dummy);
268
269         if (!nfs_parse_devname(mi->spec, &mi->hostname, NULL))
270                 return 0;
271
272         if (!nfs_name_to_address(mi->hostname, sap, &salen))
273                 return 0;
274
275         if (strncmp(mi->type, "nfs4", 4) == 0) {
276                 if (!nfs_append_clientaddr_option(sap, salen, mi->options))
277                         return 0;
278         } else {
279                 if (!nfs_fix_mounthost_option(mi->options))
280                         return 0;
281                 if (!mi->fake && !nfs_verify_lock_option(mi->options))
282                         return 0;
283         }
284
285         if (!nfs_append_sloppy_option(mi->options))
286                 return 0;
287
288         if (!nfs_append_addr_option(sap, salen, mi->options))
289                 return 0;
290
291         /*
292          * Update option string to be recorded in /etc/mnttab
293          */
294         if (po_join(mi->options, mi->extra_opts) == PO_FAILED)
295                 return 0;
296
297         return 1;
298 }
299
300 /*
301  * Get NFS/mnt server addresses from mount options
302  *
303  * Returns 1 and fills in @nfs_saddr, @nfs_salen, @mnt_saddr, and @mnt_salen
304  * if all goes well; otherwise zero.
305  */
306 static int nfs_extract_server_addresses(struct mount_options *options,
307                                         struct sockaddr *nfs_saddr,
308                                         socklen_t *nfs_salen,
309                                         struct sockaddr *mnt_saddr,
310                                         socklen_t *mnt_salen)
311 {
312         char *option;
313
314         option = po_get(options, "addr");
315         if (option == NULL)
316                 return 0;
317         if (!nfs_string_to_sockaddr(option, nfs_saddr, nfs_salen))
318                 return 0;
319
320         option = po_get(options, "mountaddr");
321         if (option == NULL) {
322                 memcpy(mnt_saddr, nfs_saddr, *nfs_salen);
323                 *mnt_salen = *nfs_salen;
324         } else if (!nfs_string_to_sockaddr(option, mnt_saddr, mnt_salen))
325                 return 0;
326
327         return 1;
328 }
329
330 static int nfs_construct_new_options(struct mount_options *options,
331                                      struct pmap *nfs_pmap,
332                                      struct pmap *mnt_pmap)
333 {
334         char new_option[64];
335
336         po_remove_all(options, "nfsprog");
337         po_remove_all(options, "mountprog");
338
339         po_remove_all(options, "v2");
340         po_remove_all(options, "v3");
341         po_remove_all(options, "vers");
342         po_remove_all(options, "nfsvers");
343         snprintf(new_option, sizeof(new_option) - 1,
344                  "vers=%lu", nfs_pmap->pm_vers);
345         if (po_append(options, new_option) == PO_FAILED)
346                 return 0;
347
348         po_remove_all(options, "proto");
349         po_remove_all(options, "udp");
350         po_remove_all(options, "tcp");
351         switch (nfs_pmap->pm_prot) {
352         case IPPROTO_TCP:
353                 snprintf(new_option, sizeof(new_option) - 1,
354                          "proto=tcp");
355                 if (po_append(options, new_option) == PO_FAILED)
356                         return 0;
357                 break;
358         case IPPROTO_UDP:
359                 snprintf(new_option, sizeof(new_option) - 1,
360                          "proto=udp");
361                 if (po_append(options, new_option) == PO_FAILED)
362                         return 0;
363                 break;
364         }
365
366         po_remove_all(options, "port");
367         if (nfs_pmap->pm_port != NFS_PORT) {
368                 snprintf(new_option, sizeof(new_option) - 1,
369                          "port=%lu", nfs_pmap->pm_port);
370                 if (po_append(options, new_option) == PO_FAILED)
371                         return 0;
372         }
373
374         po_remove_all(options, "mountvers");
375         snprintf(new_option, sizeof(new_option) - 1,
376                  "mountvers=%lu", mnt_pmap->pm_vers);
377         if (po_append(options, new_option) == PO_FAILED)
378                 return 0;
379
380         po_remove_all(options, "mountproto");
381         switch (mnt_pmap->pm_prot) {
382         case IPPROTO_TCP:
383                 snprintf(new_option, sizeof(new_option) - 1,
384                          "mountproto=tcp");
385                 if (po_append(options, new_option) == PO_FAILED)
386                         return 0;
387                 break;
388         case IPPROTO_UDP:
389                 snprintf(new_option, sizeof(new_option) - 1,
390                          "mountproto=udp");
391                 if (po_append(options, new_option) == PO_FAILED)
392                         return 0;
393                 break;
394         }
395
396         po_remove_all(options, "mountport");
397         snprintf(new_option, sizeof(new_option) - 1,
398                  "mountport=%lu", mnt_pmap->pm_port);
399         if (po_append(options, new_option) == PO_FAILED)
400                 return 0;
401
402         return 1;
403 }
404
405 /*
406  * Reconstruct the mount option string based on a portmapper probe
407  * of the server.  Returns one if the server's portmapper returned
408  * something we can use, otherwise zero.
409  *
410  * To handle version and transport protocol fallback properly, we
411  * need to parse some of the mount options in order to set up a
412  * portmap probe.  Mount options that nfs_rewrite_pmap_mount_options()
413  * doesn't recognize are left alone.
414  *
415  * Returns TRUE if rewriting was successful; otherwise
416  * FALSE is returned if some failure occurred.
417  */
418 static int
419 nfs_rewrite_pmap_mount_options(struct mount_options *options)
420 {
421         struct sockaddr_storage nfs_address;
422         struct sockaddr *nfs_saddr = (struct sockaddr *)&nfs_address;
423         socklen_t nfs_salen = sizeof(nfs_address);
424         struct pmap nfs_pmap;
425         struct sockaddr_storage mnt_address;
426         struct sockaddr *mnt_saddr = (struct sockaddr *)&mnt_address;
427         socklen_t mnt_salen = sizeof(mnt_address);
428         struct pmap mnt_pmap;
429         char *option;
430
431         /*
432          * Skip option negotiation for proto=rdma mounts.
433          */
434         option = po_get(options, "proto");
435         if (option && strcmp(option, "rdma") == 0)
436                 goto out;
437
438         /*
439          * Extract just the options needed to contact server.
440          * Bail now if any of these have bad values.
441          */
442         if (!nfs_extract_server_addresses(options, nfs_saddr, &nfs_salen,
443                                                 mnt_saddr, &mnt_salen)) {
444                 errno = EINVAL;
445                 return 0;
446         }
447         if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap)) {
448                 errno = EINVAL;
449                 return 0;
450         }
451
452         /*
453          * The kernel NFS client doesn't support changing the RPC
454          * program number for these services, so force the value of
455          * these fields before probing the server's ports.
456          */
457         nfs_pmap.pm_prog = NFS_PROGRAM;
458         mnt_pmap.pm_prog = MOUNTPROG;
459
460         /*
461          * If the server's rpcbind service isn't available, we can't
462          * negotiate.  Bail now if we can't contact it.
463          */
464         if (!nfs_probe_bothports(mnt_saddr, mnt_salen, &mnt_pmap,
465                                  nfs_saddr, nfs_salen, &nfs_pmap)) {
466                 errno = ESPIPE;
467                 return 0;
468         }
469
470         if (!nfs_construct_new_options(options, &nfs_pmap, &mnt_pmap)) {
471                 errno = EINVAL;
472                 return 0;
473         }
474
475 out:
476         errno = 0;
477         return 1;
478 }
479
480 /*
481  * Do the mount(2) system call.
482  *
483  * Returns TRUE if successful, otherwise FALSE.
484  * "errno" is set to reflect the individual error.
485  */
486 static int nfs_try_mount(struct nfsmount_info *mi)
487 {
488         char *options = NULL;
489         int result;
490
491         if (strncmp(mi->type, "nfs4", 4) != 0) {
492                 if (!nfs_rewrite_pmap_mount_options(mi->options))
493                         return 0;
494         }
495
496         if (po_join(mi->options, &options) == PO_FAILED) {
497                 errno = EIO;
498                 return 0;
499         }
500
501         if (verbose)
502                 printf(_("%s: trying text-based options '%s'\n"),
503                         progname, options);
504
505         if (mi->fake)
506                 return 1;
507
508         result = mount(mi->spec, mi->node, mi->type,
509                         mi->flags & ~(MS_USER|MS_USERS), options);
510         if (verbose && result) {
511                 int save = errno;
512                 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
513                 errno = save;
514         }
515         return !result;
516 }
517
518 /*
519  * Distinguish between permanent and temporary errors.
520  *
521  * Basically, we retry if communication with the server has
522  * failed so far, but fail immediately if there is a local
523  * error (like a bad mount option).
524  *
525  * ESTALE is also a temporary error because some servers
526  * return ESTALE when a share is temporarily offline.
527  *
528  * Returns 1 if we should fail immediately, or 0 if we
529  * should retry.
530  */
531 static int nfs_is_permanent_error(int error)
532 {
533         switch (error) {
534         case ESTALE:
535         case ETIMEDOUT:
536         case ECONNREFUSED:
537                 return 0;       /* temporary */
538         default:
539                 return 1;       /* permanent */
540         }
541 }
542
543 /*
544  * Handle "foreground" NFS mounts.
545  *
546  * Retry the mount request for as long as the 'retry=' option says.
547  *
548  * Returns a valid mount command exit code.
549  */
550 static int nfsmount_fg(struct nfsmount_info *mi)
551 {
552         unsigned int secs = 1;
553         time_t timeout;
554
555         timeout = nfs_parse_retry_option(mi->options,
556                                          NFS_DEF_FG_TIMEOUT_MINUTES);
557         if (verbose)
558                 printf(_("%s: timeout set for %s"),
559                         progname, ctime(&timeout));
560
561         for (;;) {
562                 if (nfs_try_mount(mi))
563                         return EX_SUCCESS;
564
565                 if (nfs_is_permanent_error(errno))
566                         break;
567
568                 if (time(NULL) > timeout) {
569                         errno = ETIMEDOUT;
570                         break;
571                 }
572
573                 if (errno != ETIMEDOUT) {
574                         if (sleep(secs))
575                                 break;
576                         secs <<= 1;
577                         if (secs > 10)
578                                 secs = 10;
579                 }
580         };
581
582         mount_error(mi->spec, mi->node, errno);
583         return EX_FAIL;
584 }
585
586 /*
587  * Handle "background" NFS mount [first try]
588  *
589  * Returns a valid mount command exit code.
590  *
591  * EX_BG should cause the caller to fork and invoke nfsmount_child.
592  */
593 static int nfsmount_parent(struct nfsmount_info *mi)
594 {
595         if (nfs_try_mount(mi))
596                 return EX_SUCCESS;
597
598         if (nfs_is_permanent_error(errno)) {
599                 mount_error(mi->spec, mi->node, errno);
600                 return EX_FAIL;
601         }
602
603         sys_mount_errors(mi->hostname, errno, 1, 1);
604         return EX_BG;
605 }
606
607 /*
608  * Handle "background" NFS mount [retry daemon]
609  *
610  * Returns a valid mount command exit code: EX_SUCCESS if successful,
611  * EX_FAIL if a failure occurred.  There's nothing to catch the
612  * error return, though, so we use sys_mount_errors to log the
613  * failure.
614  */
615 static int nfsmount_child(struct nfsmount_info *mi)
616 {
617         unsigned int secs = 1;
618         time_t timeout;
619
620         timeout = nfs_parse_retry_option(mi->options,
621                                          NFS_DEF_BG_TIMEOUT_MINUTES);
622
623         for (;;) {
624                 if (sleep(secs))
625                         break;
626                 secs <<= 1;
627                 if (secs > 120)
628                         secs = 120;
629
630                 if (nfs_try_mount(mi))
631                         return EX_SUCCESS;
632
633                 if (nfs_is_permanent_error(errno))
634                         break;
635
636                 if (time(NULL) > timeout)
637                         break;
638
639                 sys_mount_errors(mi->hostname, errno, 1, 1);
640         };
641
642         sys_mount_errors(mi->hostname, errno, 1, 0);
643         return EX_FAIL;
644 }
645
646 /*
647  * Handle "background" NFS mount
648  *
649  * Returns a valid mount command exit code.
650  */
651 static int nfsmount_bg(struct nfsmount_info *mi)
652 {
653         if (!mi->child)
654                 return nfsmount_parent(mi);
655         else
656                 return nfsmount_child(mi);
657 }
658
659 /*
660  * Process mount options and try a mount system call.
661  *
662  * Returns a valid mount command exit code.
663  */
664 static const char *nfs_background_opttbl[] = {
665         "bg",
666         "fg",
667         NULL,
668 };
669
670 static int nfsmount_start(struct nfsmount_info *mi)
671 {
672         if (!nfs_validate_options(mi))
673                 return EX_FAIL;
674
675         if (po_rightmost(mi->options, nfs_background_opttbl) == 0)
676                 return nfsmount_bg(mi);
677         else
678                 return nfsmount_fg(mi);
679 }
680
681 /**
682  * nfsmount_string - Mount an NFS file system using C string options
683  * @spec: C string specifying remote share to mount ("hostname:path")
684  * @node: C string pathname of local mounted-on directory
685  * @type: C string that represents file system type ("nfs" or "nfs4")
686  * @flags: MS_ style mount flags
687  * @extra_opts: pointer to C string containing fs-specific mount options
688  *              (input and output argument)
689  * @fake: flag indicating whether to carry out the whole operation
690  * @child: one if this is a mount daemon (bg)
691  */
692 int nfsmount_string(const char *spec, const char *node, const char *type,
693                     int flags, char **extra_opts, int fake, int child)
694 {
695         struct nfsmount_info mi = {
696                 .spec           = spec,
697                 .node           = node,
698                 .type           = type,
699                 .extra_opts     = extra_opts,
700                 .flags          = flags,
701                 .fake           = fake,
702                 .child          = child,
703         };
704         int retval = EX_FAIL;
705
706         mi.options = po_split(*extra_opts);
707         if (mi.options) {
708                 retval = nfsmount_start(&mi);
709                 po_destroy(mi.options);
710         } else
711                 nfs_error(_("%s: internal option parsing error"), progname);
712
713         free(mi.hostname);
714         return retval;
715 }