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