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