]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
There are three helpers that convert sockaddr-style addresses to text
[nfs-utils.git] / utils / mount / stropts.c
1 /*
2  * stropts.c -- NFS mount using C string to pass options to kernel
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 021110-1307, USA.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <time.h>
36
37 #include <sys/socket.h>
38 #include <sys/mount.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41
42 #include "xcommon.h"
43 #include "mount.h"
44 #include "nls.h"
45 #include "nfs_mount.h"
46 #include "mount_constants.h"
47 #include "stropts.h"
48 #include "error.h"
49 #include "network.h"
50 #include "parse_opt.h"
51 #include "version.h"
52
53 #ifdef HAVE_RPCSVC_NFS_PROT_H
54 #include <rpcsvc/nfs_prot.h>
55 #else
56 #include <linux/nfs.h>
57 #define nfsstat nfs_stat
58 #endif
59
60 #ifndef NFS_PORT
61 #define NFS_PORT 2049
62 #endif
63
64 #ifndef NFS_MAXHOSTNAME
65 #define NFS_MAXHOSTNAME         (255)
66 #endif
67
68 #ifndef NFS_MAXPATHNAME
69 #define NFS_MAXPATHNAME         (1024)
70 #endif
71
72 #ifndef NFS_DEF_FG_TIMEOUT_MINUTES
73 #define NFS_DEF_FG_TIMEOUT_MINUTES      (2u)
74 #endif
75
76 #ifndef NFS_DEF_BG_TIMEOUT_MINUTES
77 #define NFS_DEF_BG_TIMEOUT_MINUTES      (10000u)
78 #endif
79
80 extern int nfs_mount_data_version;
81 extern char *progname;
82 extern int verbose;
83 extern int sloppy;
84
85 struct nfsmount_info {
86         const char              *spec,          /* server:/path */
87                                 *node,          /* mounted-on dir */
88                                 *type;          /* "nfs" or "nfs4" */
89         char                    *hostname;      /* server's hostname */
90
91         struct mount_options    *options;       /* parsed mount options */
92         char                    **extra_opts;   /* string for /etc/mtab */
93
94         int                     flags,          /* MS_ flags */
95                                 fake,           /* actually do the mount? */
96                                 child;          /* forked bg child? */
97 };
98
99 static int nfs_parse_devname(struct nfsmount_info *mi)
100 {
101         int ret = 0;
102         char *dev, *pathname, *s;
103
104         dev = xstrdup(mi->spec);
105
106         if (!(pathname = strchr(dev, ':'))) {
107                 nfs_error(_("%s: remote share not in 'host:dir' format"),
108                                 progname);
109                 goto out;
110         }
111         *pathname = '\0';
112         pathname++;
113
114         /*
115          * We don't need a copy of the pathname, but let's
116          * sanity check it anyway.
117          */
118         if (strlen(pathname) > NFS_MAXPATHNAME) {
119                 nfs_error(_("%s: export pathname is too long"),
120                                 progname);
121                 goto out;
122         }
123
124         /*
125          * Ignore all but first hostname in replicated mounts
126          * until they can be fully supported. (mack@sgi.com)
127          */
128         if ((s = strchr(dev, ','))) {
129                 *s = '\0';
130                 nfs_error(_("%s: warning: multiple hostnames not supported"),
131                                 progname);
132                 nfs_error(_("%s: ignoring hostnames that follow the first one"),
133                                 progname);
134         }
135         mi->hostname = xstrdup(dev);
136         if (strlen(mi->hostname) > NFS_MAXHOSTNAME) {
137                 nfs_error(_("%s: server hostname is too long"),
138                                 progname);
139                 free(mi->hostname);
140                 mi->hostname = NULL;
141                 goto out;
142         }
143
144         ret = 1;
145
146 out:
147         free(dev);
148         return ret;
149 }
150
151 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
152 {
153         struct hostent *hp;
154         addr->sin_family = AF_INET;
155
156         if (inet_aton(hostname, &addr->sin_addr))
157                 return 1;
158         if ((hp = gethostbyname(hostname)) == NULL) {
159                 nfs_error(_("%s: can't get address for %s\n"),
160                                 progname, hostname);
161                 return 0;
162         }
163         if (hp->h_length > sizeof(struct in_addr)) {
164                 nfs_error(_("%s: got bad hp->h_length"), progname);
165                 hp->h_length = sizeof(struct in_addr);
166         }
167         memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
168         return 1;
169 }
170
171 /*
172  * Obtain a retry timeout value based on the value of the "retry=" option.
173  *
174  * Returns a time_t timeout timestamp, in seconds.
175  */
176 static time_t nfs_parse_retry_option(struct mount_options *options,
177                                      unsigned int timeout_minutes)
178 {
179         char *retry_option, *endptr;
180
181         retry_option = po_get(options, "retry");
182         if (retry_option) {
183                 long tmp;
184
185                 errno = 0;
186                 tmp = strtol(retry_option, &endptr, 10);
187                 if (errno == 0 && endptr != retry_option && tmp >= 0)
188                         timeout_minutes = tmp;
189                 else if (verbose)
190                         nfs_error(_("%s: invalid retry timeout was specified; "
191                                         "using default timeout"), progname);
192         }
193
194         return time(NULL) + (time_t)(timeout_minutes * 60);
195 }
196
197 /*
198  * Convert the passed-in sockaddr-style address to presentation
199  * format, then append an option of the form "keyword=address".
200  *
201  * Returns 1 if the option was appended successfully; otherwise zero.
202  */
203 static int nfs_append_generic_address_option(const struct sockaddr *sap,
204                                              const socklen_t salen,
205                                              const char *keyword,
206                                              struct mount_options *options)
207 {
208         char address[NI_MAXHOST];
209         char new_option[512];
210
211         if (!nfs_present_sockaddr(sap, salen, address, sizeof(address)))
212                 goto out_err;
213
214         if (snprintf(new_option, sizeof(new_option), "%s=%s",
215                                         keyword, address) >= sizeof(new_option))
216                 goto out_err;
217
218         if (po_append(options, new_option) != PO_SUCCEEDED)
219                 goto out_err;
220
221         return 1;
222
223 out_err:
224         nfs_error(_("%s: failed to construct %s option"), progname, keyword);
225         return 0;
226 }
227
228 /*
229  * Append the 'addr=' option to the options string to pass a resolved
230  * server address to the kernel.  After a successful mount, this address
231  * is also added to /etc/mtab for use when unmounting.
232  *
233  * If 'addr=' is already present, we strip it out.  This prevents users
234  * from setting a bogus 'addr=' option themselves, and also allows bg
235  * retries to recompute the server's address, in case it has changed.
236  *
237  * Returns 1 if 'addr=' option appended successfully;
238  * otherwise zero.
239  */
240 static int append_addr_option(struct sockaddr_in *saddr,
241                            struct mount_options *options)
242 {
243         char new_option[24];
244
245         po_remove_all(options, "addr");
246
247         snprintf(new_option, sizeof(new_option) - 1,
248                         "addr=%s", inet_ntoa(saddr->sin_addr));
249
250         if (po_append(options, new_option) == PO_SUCCEEDED)
251                 return 1;
252         return 0;
253 }
254
255 /*
256  * Called to discover our address and append an appropriate 'clientaddr='
257  * option to the options string.
258  *
259  * Returns 1 if 'clientaddr=' option created successfully or if
260  * 'clientaddr=' option is already present; otherwise zero.
261  */
262 static int append_clientaddr_option(struct sockaddr_in *saddr,
263                                     struct mount_options *options)
264 {
265         struct sockaddr_in my_addr;
266         char new_option[32];
267
268         if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
269                 return 1;
270
271         if (!get_client_address(saddr, &my_addr))
272                 return 0;
273
274         snprintf(new_option, sizeof(new_option) - 1,
275                         "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
276
277         if (po_append(options, new_option) == PO_SUCCEEDED)
278                 return 1;
279         return 0;
280 }
281
282 /*
283  * Resolve the 'mounthost=' hostname and append a new option using
284  * the resulting IPv4 address.
285  */
286 static int fix_mounthost_option(struct mount_options *options)
287 {
288         struct sockaddr_in maddr;
289         char *mounthost, new_option[32];
290
291         mounthost = po_get(options, "mounthost");
292         if (!mounthost)
293                 return 1;
294
295         if (!fill_ipv4_sockaddr(mounthost, &maddr))
296                 return 0;
297
298         snprintf(new_option, sizeof(new_option) - 1,
299                         "mountaddr=%s", inet_ntoa(maddr.sin_addr));
300
301         if (po_append(options, new_option) == PO_SUCCEEDED)
302                 return 1;
303         return 0;
304 }
305
306 /*
307  * Returns zero if the "lock" option is in effect, but statd
308  * can't be started.  Otherwise, returns 1.
309  */
310 static int verify_lock_option(struct mount_options *options)
311 {
312         if (po_rightmost(options, "nolock", "lock") == PO_KEY1_RIGHTMOST)
313                 return 1;
314
315         if (!start_statd()) {
316                 nfs_error(_("%s: rpc.statd is not running but is "
317                             "required for remote locking."), progname);
318                 nfs_error(_("%s: Either use '-o nolock' to keep "
319                             "locks local, or start statd."), progname);
320                 return 0;
321         }
322
323         return 1;
324 }
325
326 static int nfs_append_sloppy_option(struct mount_options *options)
327 {
328         if (!sloppy || linux_version_code() < MAKE_VERSION(2, 6, 27))
329                 return 1;
330
331         if (po_append(options, "sloppy") == PO_FAILED)
332                 return 0;
333         return 1;
334 }
335
336 /*
337  * Set up mandatory NFS mount options.
338  *
339  * Returns 1 if successful; otherwise zero.
340  */
341 static int nfs_validate_options(struct nfsmount_info *mi)
342 {
343         struct sockaddr_in saddr;
344
345         if (!fill_ipv4_sockaddr(mi->hostname, &saddr))
346                 return 0;
347
348         if (strncmp(mi->type, "nfs4", 4) == 0) {
349                 if (!append_clientaddr_option(&saddr, mi->options))
350                         return 0;
351         } else {
352                 if (!fix_mounthost_option(mi->options))
353                         return 0;
354                 if (!mi->fake && !verify_lock_option(mi->options))
355                         return 0;
356         }
357
358         if (!nfs_append_sloppy_option(mi->options))
359                 return 0;
360
361         if (!append_addr_option(&saddr, mi->options))
362                 return 0;
363
364         return 1;
365 }
366
367 /*
368  * Distinguish between permanent and temporary errors.
369  *
370  * Returns 0 if the passed-in error is temporary, thus the
371  * mount system call should be retried; returns one if the
372  * passed-in error is permanent, thus the mount system call
373  * should not be retried.
374  */
375 static int is_permanent_error(int error)
376 {
377         switch (error) {
378         case ESTALE:
379         case ETIMEDOUT:
380         case ECONNREFUSED:
381                 return 0;       /* temporary */
382         default:
383                 return 1;       /* permanent */
384         }
385 }
386
387 /*
388  * Reconstruct the mount option string based on a portmapper probe
389  * of the server.  Returns one if the server's portmapper returned
390  * something we can use, otherwise zero.
391  *
392  * To handle version and transport protocol fallback properly, we
393  * need to parse some of the mount options in order to set up a
394  * portmap probe.  Mount options that rewrite_mount_options()
395  * doesn't recognize are left alone.
396  *
397  * Returns a new group of mount options if successful; otherwise
398  * NULL is returned if some failure occurred.
399  */
400 static struct mount_options *rewrite_mount_options(char *str)
401 {
402         struct mount_options *options;
403         char *option, new_option[64];
404         clnt_addr_t mnt_server = { };
405         clnt_addr_t nfs_server = { };
406         int p;
407
408         options = po_split(str);
409         if (!options) {
410                 errno = EFAULT;
411                 return NULL;
412         }
413
414         errno = EINVAL;
415         option = po_get(options, "addr");
416         if (option) {
417                 nfs_server.saddr.sin_family = AF_INET;
418                 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
419                         goto err;
420         } else
421                 goto err;
422
423         option = po_get(options, "mountaddr");
424         if (option) {
425                 mnt_server.saddr.sin_family = AF_INET;
426                 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
427                         goto err;
428         } else
429                 memcpy(&mnt_server.saddr, &nfs_server.saddr,
430                                 sizeof(mnt_server.saddr));
431
432         option = po_get(options, "mountport");
433         if (option)
434                 mnt_server.pmap.pm_port = atoi(option);
435         mnt_server.pmap.pm_prog = MOUNTPROG;
436         option = po_get(options, "mountvers");
437         if (option)
438                 mnt_server.pmap.pm_vers = atoi(option);
439
440         option = po_get(options, "port");
441         if (option) {
442                 nfs_server.pmap.pm_port = atoi(option);
443                 po_remove_all(options, "port");
444         }
445         nfs_server.pmap.pm_prog = NFS_PROGRAM;
446
447         option = po_get(options, "nfsvers");
448         if (option) {
449                 nfs_server.pmap.pm_vers = atoi(option);
450                 po_remove_all(options, "nfsvers");
451         }
452         option = po_get(options, "vers");
453         if (option) {
454                 nfs_server.pmap.pm_vers = atoi(option);
455                 po_remove_all(options, "vers");
456         }
457         option = po_get(options, "proto");
458         if (option) {
459                 if (strcmp(option, "tcp") == 0) {
460                         nfs_server.pmap.pm_prot = IPPROTO_TCP;
461                         po_remove_all(options, "proto");
462                 }
463                 if (strcmp(option, "udp") == 0) {
464                         nfs_server.pmap.pm_prot = IPPROTO_UDP;
465                         po_remove_all(options, "proto");
466                 }
467         }
468         p = po_rightmost(options, "tcp", "udp");
469         switch (p) {
470         case PO_KEY2_RIGHTMOST:
471                 nfs_server.pmap.pm_prot = IPPROTO_UDP;
472                 break;
473         case PO_KEY1_RIGHTMOST:
474                 nfs_server.pmap.pm_prot = IPPROTO_TCP;
475                 break;
476         }
477         po_remove_all(options, "tcp");
478         po_remove_all(options, "udp");
479
480         if (!probe_bothports(&mnt_server, &nfs_server)) {
481                 errno = ESPIPE;
482                 goto err;
483         }
484
485         snprintf(new_option, sizeof(new_option) - 1,
486                  "nfsvers=%lu", nfs_server.pmap.pm_vers);
487         if (po_append(options, new_option) == PO_FAILED)
488                 goto err;
489
490         if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
491                 snprintf(new_option, sizeof(new_option) - 1,
492                          "proto=tcp");
493         else
494                 snprintf(new_option, sizeof(new_option) - 1,
495                          "proto=udp");
496         if (po_append(options, new_option) == PO_FAILED)
497                 goto err;
498
499         if (nfs_server.pmap.pm_port != NFS_PORT) {
500                 snprintf(new_option, sizeof(new_option) - 1,
501                          "port=%lu", nfs_server.pmap.pm_port);
502                 if (po_append(options, new_option) == PO_FAILED)
503                         goto err;
504
505         }
506
507         errno = 0;
508         return options;
509
510 err:
511         po_destroy(options);
512         return NULL;
513 }
514
515 /*
516  * Do the mount(2) system call.
517  *
518  * Returns 1 if successful, otherwise zero.
519  * "errno" is set to reflect the individual error.
520  */
521 static int nfs_sys_mount(const struct nfsmount_info *mi, const char *type,
522                          const char *options)
523 {
524         int result;
525
526         result = mount(mi->spec, mi->node, type,
527                                 mi->flags & ~(MS_USER|MS_USERS), options);
528         if (verbose && result) {
529                 int save = errno;
530                 nfs_error(_("%s: mount(2): %s"), progname, strerror(save));
531                 errno = save;
532         }
533         return !result;
534 }
535
536 /*
537  * Retry an NFS mount that failed because the requested service isn't
538  * available on the server.
539  *
540  * Returns 1 if successful.  Otherwise, returns zero.
541  * "errno" is set to reflect the individual error.
542  *
543  * Side effect: If the retry is successful, both 'options' and
544  * 'extra_opts' are updated to reflect the mount options that worked.
545  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
546  */
547 static int nfs_retry_nfs23mount(struct nfsmount_info *mi)
548 {
549         struct mount_options *retry_options;
550         char *retry_str = NULL;
551         char **extra_opts = mi->extra_opts;
552
553         retry_options = rewrite_mount_options(*extra_opts);
554         if (!retry_options)
555                 return 0;
556
557         if (po_join(retry_options, &retry_str) == PO_FAILED) {
558                 po_destroy(retry_options);
559                 errno = EIO;
560                 return 0;
561         }
562
563         if (verbose)
564                 printf(_("%s: text-based options (retry): '%s'\n"),
565                         progname, retry_str);
566
567         if (!nfs_sys_mount(mi, "nfs", retry_str)) {
568                 po_destroy(retry_options);
569                 free(retry_str);
570                 return 0;
571         }
572
573         free(*extra_opts);
574         *extra_opts = retry_str;
575         po_replace(mi->options, retry_options);
576         return 1;
577 }
578
579 /*
580  * Attempt an NFSv2/3 mount via a mount(2) system call.  If the kernel
581  * claims the requested service isn't supported on the server, probe
582  * the server to see what's supported, rewrite the mount options,
583  * and retry the request.
584  *
585  * Returns 1 if successful.  Otherwise, returns zero.
586  * "errno" is set to reflect the individual error.
587  *
588  * Side effect: If the retry is successful, both 'options' and
589  * 'extra_opts' are updated to reflect the mount options that worked.
590  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
591  */
592 static int nfs_try_nfs23mount(struct nfsmount_info *mi)
593 {
594         char **extra_opts = mi->extra_opts;
595
596         if (po_join(mi->options, extra_opts) == PO_FAILED) {
597                 errno = EIO;
598                 return 0;
599         }
600
601         if (verbose)
602                 printf(_("%s: text-based options: '%s'\n"),
603                         progname, *extra_opts);
604
605         if (mi->fake)
606                 return 1;
607
608         if (nfs_sys_mount(mi, "nfs", *extra_opts))
609                 return 1;
610
611         /*
612          * The kernel returns EOPNOTSUPP if the RPC bind failed,
613          * and EPROTONOSUPPORT if the version isn't supported.
614          */
615         if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
616                 return 0;
617
618         return nfs_retry_nfs23mount(mi);
619 }
620
621 /*
622  * Attempt an NFS v4 mount via a mount(2) system call.
623  *
624  * Returns 1 if successful.  Otherwise, returns zero.
625  * "errno" is set to reflect the individual error.
626  */
627 static int nfs_try_nfs4mount(struct nfsmount_info *mi)
628 {
629         char **extra_opts = mi->extra_opts;
630
631         if (po_join(mi->options, extra_opts) == PO_FAILED) {
632                 errno = EIO;
633                 return 0;
634         }
635
636         if (verbose)
637                 printf(_("%s: text-based options: '%s'\n"),
638                         progname, *extra_opts);
639
640         if (mi->fake)
641                 return 1;
642
643         return nfs_sys_mount(mi, "nfs4", *extra_opts);
644 }
645
646 /*
647  * Perform either an NFSv2/3 mount, or an NFSv4 mount system call.
648  *
649  * Returns 1 if successful.  Otherwise, returns zero.
650  * "errno" is set to reflect the individual error.
651  */
652 static int nfs_try_mount(struct nfsmount_info *mi)
653 {
654         if (strncmp(mi->type, "nfs4", 4) == 0)
655                 return nfs_try_nfs4mount(mi);
656         else
657                 return nfs_try_nfs23mount(mi);
658 }
659
660 /*
661  * Handle "foreground" NFS mounts.
662  *
663  * Retry the mount request for as long as the 'retry=' option says.
664  *
665  * Returns a valid mount command exit code.
666  */
667 static int nfsmount_fg(struct nfsmount_info *mi)
668 {
669         unsigned int secs = 1;
670         time_t timeout;
671
672         timeout = nfs_parse_retry_option(mi->options,
673                                          NFS_DEF_FG_TIMEOUT_MINUTES);
674         if (verbose)
675                 printf(_("%s: timeout set for %s"),
676                         progname, ctime(&timeout));
677
678         for (;;) {
679                 if (nfs_try_mount(mi))
680                         return EX_SUCCESS;
681
682                 if (is_permanent_error(errno))
683                         break;
684
685                 if (time(NULL) > timeout) {
686                         errno = ETIMEDOUT;
687                         break;
688                 }
689
690                 if (errno != ETIMEDOUT) {
691                         if (sleep(secs))
692                                 break;
693                         secs <<= 1;
694                         if (secs > 10)
695                                 secs = 10;
696                 }
697         };
698
699         mount_error(mi->spec, mi->node, errno);
700         return EX_FAIL;
701 }
702
703 /*
704  * Handle "background" NFS mount [first try]
705  *
706  * Returns a valid mount command exit code.
707  *
708  * EX_BG should cause the caller to fork and invoke nfsmount_child.
709  */
710 static int nfsmount_parent(struct nfsmount_info *mi)
711 {
712         if (nfs_try_mount(mi))
713                 return EX_SUCCESS;
714
715         if (is_permanent_error(errno)) {
716                 mount_error(mi->spec, mi->node, errno);
717                 return EX_FAIL;
718         }
719
720         sys_mount_errors(mi->hostname, errno, 1, 1);
721         return EX_BG;
722 }
723
724 /*
725  * Handle "background" NFS mount [retry daemon]
726  *
727  * Returns a valid mount command exit code: EX_SUCCESS if successful,
728  * EX_FAIL if a failure occurred.  There's nothing to catch the
729  * error return, though, so we use sys_mount_errors to log the
730  * failure.
731  */
732 static int nfsmount_child(struct nfsmount_info *mi)
733 {
734         unsigned int secs = 1;
735         time_t timeout;
736
737         timeout = nfs_parse_retry_option(mi->options,
738                                          NFS_DEF_BG_TIMEOUT_MINUTES);
739
740         for (;;) {
741                 if (sleep(secs))
742                         break;
743                 secs <<= 1;
744                 if (secs > 120)
745                         secs = 120;
746
747                 if (nfs_try_mount(mi))
748                         return EX_SUCCESS;
749
750                 if (is_permanent_error(errno))
751                         break;
752
753                 if (time(NULL) > timeout)
754                         break;
755
756                 sys_mount_errors(mi->hostname, errno, 1, 1);
757         };
758
759         sys_mount_errors(mi->hostname, errno, 1, 0);
760         return EX_FAIL;
761 }
762
763 /*
764  * Handle "background" NFS mount
765  *
766  * Returns a valid mount command exit code.
767  */
768 static int nfsmount_bg(struct nfsmount_info *mi)
769 {
770         if (!mi->child)
771                 return nfsmount_parent(mi);
772         else
773                 return nfsmount_child(mi);
774 }
775
776 /*
777  * Process mount options and try a mount system call.
778  *
779  * Returns a valid mount command exit code.
780  */
781 static int nfsmount_start(struct nfsmount_info *mi)
782 {
783         if (!nfs_validate_options(mi))
784                 return EX_FAIL;
785
786         if (po_rightmost(mi->options, "bg", "fg") == PO_KEY1_RIGHTMOST)
787                 return nfsmount_bg(mi);
788         else
789                 return nfsmount_fg(mi);
790 }
791
792 /**
793  * nfsmount_string - Mount an NFS file system using C string options
794  * @spec: C string specifying remote share to mount ("hostname:path")
795  * @node: C string pathname of local mounted-on directory
796  * @type: C string that represents file system type ("nfs" or "nfs4")
797  * @flags: MS_ style mount flags
798  * @extra_opts: pointer to C string containing fs-specific mount options
799  *              (input and output argument)
800  * @fake: flag indicating whether to carry out the whole operation
801  * @child: one if this is a mount daemon (bg)
802  */
803 int nfsmount_string(const char *spec, const char *node, const char *type,
804                     int flags, char **extra_opts, int fake, int child)
805 {
806         struct nfsmount_info mi = {
807                 .spec           = spec,
808                 .node           = node,
809                 .type           = type,
810                 .extra_opts     = extra_opts,
811                 .flags          = flags,
812                 .fake           = fake,
813                 .child          = child,
814         };
815         int retval = EX_FAIL;
816
817         if (!nfs_parse_devname(&mi))
818                 return retval;
819
820         mi.options = po_split(*extra_opts);
821         if (mi.options) {
822                 retval = nfsmount_start(&mi);
823                 po_destroy(mi.options);
824         } else
825                 nfs_error(_("%s: internal option parsing error"), progname);
826
827         free(mi.hostname);
828         return retval;
829 }