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