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