Support -s option to ignore unknown options (sloppy)
[nfs-utils.git] / utils / mount / nfsmount.c
1 /*
2  * nfsmount.c -- Linux NFS mount
3  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Wed Feb  8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
16  * numbers to be specified on the command line.
17  *
18  * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
19  * Omit the call to connect() for Linux version 1.3.11 or later.
20  *
21  * Wed Oct  1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
22  * Implemented the "bg", "fg" and "retry" mount options for NFS.
23  *
24  * 1999-02-22 Arkadiusz Mi¬∂kiewicz <misiek@pld.ORG.PL>
25  * - added Native Language Support
26  * 
27  * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
28  * plus NFSv3 stuff.
29  *
30  * 2006-06-06 Amit Gud <agud@redhat.com>
31  * - Moved with modifcations to nfs-utils/utils/mount from util-linux/mount.
32  */
33
34 /*
35  * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
36  */
37
38 #include <ctype.h>
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <netdb.h>
45 #include <time.h>
46 #include <rpc/rpc.h>
47 #include <rpc/pmap_prot.h>
48 #include <rpc/pmap_clnt.h>
49 #include <sys/socket.h>
50 #include <sys/time.h>
51 #include <sys/utsname.h>
52 #include <sys/stat.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <mntent.h>
56 #include <sys/mount.h>
57 #include <paths.h>
58 #include <syslog.h>
59
60 #include "conn.h"
61 #include "xcommon.h"
62 #include "mount.h"
63 #include "nfsumount.h"
64 #include "nfs_mount.h"
65 #include "mount_constants.h"
66 #include "nls.h"
67
68 #ifdef HAVE_RPCSVC_NFS_PROT_H
69 #include <rpcsvc/nfs_prot.h>
70 #else
71 #include <linux/nfs.h>
72 #define nfsstat nfs_stat
73 #endif
74
75 #ifndef NFS_PORT
76 #define NFS_PORT 2049
77 #endif
78 #ifndef NFS_FHSIZE
79 #define NFS_FHSIZE 32
80 #endif
81
82 static char *nfs_strerror(int stat);
83
84 #define MAKE_VERSION(p,q,r)     (65536*(p) + 256*(q) + (r))
85 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
86 #define MAX_MNTPROT ((nfs_mount_version >= 4) ? 3 : 2)
87 #define HAVE_RELIABLE_TCP (nfs_mount_version >= 4)
88
89 #ifndef HAVE_INET_ATON
90 #define inet_aton(a,b) (0)
91 #endif
92
93 typedef dirpath mnt2arg_t;
94 typedef dirpath mnt3arg_t;
95 typedef dirpath mntarg_t;
96
97 typedef struct fhstatus  mnt2res_t;
98 typedef struct mountres3 mnt3res_t;
99 typedef union {
100         mnt2res_t nfsv2;
101         mnt3res_t nfsv3;
102 } mntres_t;
103
104 static char errbuf[BUFSIZ];
105 static char *erreob = &errbuf[BUFSIZ];
106 extern int verbose;
107 extern int sloppy;
108
109 /* Convert RPC errors into strings */
110 int rpc_strerror(int);
111 int rpc_strerror(int spos)
112 {
113         int cf_stat = rpc_createerr.cf_stat; 
114         int pos=0, cf_errno = rpc_createerr.cf_error.re_errno;
115         char *ptr, *estr = clnt_sperrno(cf_stat);
116         char *tmp;
117
118         if (estr) {
119                 if ((ptr = index(estr, ':')))
120                         estr = ++ptr;
121
122                 tmp = &errbuf[spos];
123                 if (cf_stat == RPC_SYSTEMERROR)
124                         pos = snprintf(tmp, (erreob - tmp), 
125                                 "System Error: %s", strerror(cf_errno));
126                 else
127                         pos = snprintf(tmp, (erreob - tmp), "RPC Error:%s", estr);
128         }
129         return (pos);
130 }
131 void mount_errors(char *, int, int);
132 void mount_errors(char *server, int will_retry, int bg)
133 {
134         int pos = 0;
135         char *tmp;
136         static int onlyonce = 0;
137
138         tmp = &errbuf[pos];
139         if (bg) 
140                 pos = snprintf(tmp, (erreob - tmp), 
141                         "mount to NFS server '%s' failed: ", server);
142         else
143                 pos = snprintf(tmp, (erreob - tmp), 
144                         "mount: mount to NFS server '%s' failed: ", server);
145
146         tmp = &errbuf[pos];
147         if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
148                 pos = snprintf(tmp, (erreob - tmp), "timed out %s", 
149                         will_retry ? "(retrying)" : "(giving up)");
150         } else {
151                 pos += rpc_strerror(pos);
152                 tmp = &errbuf[pos];
153                 if (bg) {
154                         pos = snprintf(tmp, (erreob - tmp), " %s",
155                                 will_retry ? "(retrying)" : "(giving up)");
156                 }
157         }
158         if (bg) {
159                 if (onlyonce++ < 1)
160                         openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
161                 syslog(LOG_ERR, "%s.", errbuf);
162         } else
163                 fprintf(stderr, "%s.\n", errbuf);
164 }
165
166 /* Define the order in which to probe for UDP/TCP services */
167 enum plist {
168         use_tcp = 0,
169         udp_tcp,
170         udp_only,
171 };
172 static const u_int *
173 proto_probelist(enum plist list)
174 {
175         static const u_int probe_udp_tcp[] = { IPPROTO_UDP, IPPROTO_TCP, 0 };
176         static const u_int probe_both[] = { IPPROTO_TCP, IPPROTO_UDP, 0 };
177         static const u_int probe_udponly[] = { IPPROTO_UDP, 0 };
178
179         if (list == use_tcp)
180                 return probe_both;
181         if (list == udp_tcp)
182                 return probe_udp_tcp;
183         return probe_udponly;
184 }
185
186 /* Define the order in which NFS versions are probed on portmapper */
187 static const u_long *
188 nfs_probelist(const int vers)
189 {
190         static const u_long nfs2_probe[] = { 2, 0};
191         static const u_long nfs3_probe[] = { 3, 2, 0};
192         switch (vers) {
193         case 3:
194                 return nfs3_probe;
195         default:
196                 return nfs2_probe;
197         }
198 }
199
200 /* Define the order in which Mountd versions are probed on portmapper */
201 static const u_long *
202 mnt_probelist(const int vers)
203 {
204         static const u_long mnt1_probe[] = { 1, 2, 0 };
205         static const u_long mnt3_probe[] = { 3, 1, 2, 0 };
206         switch (vers) {
207         case 3:
208                 return mnt3_probe;
209         default:
210                 return mnt1_probe;
211         }
212 }
213
214 static int
215 linux_version_code(void) {
216         struct utsname my_utsname;
217         int p, q, r;
218
219         if (uname(&my_utsname) == 0) {
220                 p = atoi(strtok(my_utsname.release, "."));
221                 q = atoi(strtok(NULL, "."));
222                 r = atoi(strtok(NULL, "."));
223                 return MAKE_VERSION(p,q,r);
224         }
225         return 0;
226 }
227
228 /*
229  * Unfortunately, the kernel prints annoying console messages
230  * in case of an unexpected nfs mount version (instead of
231  * just returning some error).  Therefore we'll have to try
232  * and figure out what version the kernel expects.
233  *
234  * Variables:
235  *      NFS_MOUNT_VERSION: these nfsmount sources at compile time
236  *      nfs_mount_version: version this source and running kernel can handle
237  */
238 int nfs_mount_version = NFS_MOUNT_VERSION;
239
240 int
241 find_kernel_nfs_mount_version(void) {
242         static int kernel_version = -1;
243         int mnt_version = NFS_MOUNT_VERSION;
244
245         if (kernel_version == -1)
246                 kernel_version = linux_version_code();
247
248         if (kernel_version) {
249              if (kernel_version < MAKE_VERSION(2,1,32))
250                   mnt_version = 1;
251              else if (kernel_version < MAKE_VERSION(2,2,18))
252                   mnt_version = 3;
253              else if (kernel_version < MAKE_VERSION(2,3,0))
254                   mnt_version = 4; /* since 2.2.18pre9 */
255              else if (kernel_version < MAKE_VERSION(2,3,99))
256                   mnt_version = 3;
257              else if (kernel_version < MAKE_VERSION(2,6,3))
258                   mnt_version = 4;
259              else
260                   mnt_version = 6;
261         }
262         if (mnt_version > NFS_MOUNT_VERSION)
263              mnt_version = NFS_MOUNT_VERSION;
264         return mnt_version;
265 }
266
267 int nfs_gethostbyname(const char *, struct sockaddr_in *);
268 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
269 {
270         struct hostent *hp;
271
272         saddr->sin_family = AF_INET;
273         if (!inet_aton(hostname, &saddr->sin_addr)) {
274                 if ((hp = gethostbyname(hostname)) == NULL) {
275                         fprintf(stderr, _("mount: can't get address for %s\n"),
276                                 hostname);
277                         return 0;
278                 } else {
279                         if (hp->h_length > sizeof(*saddr)) {
280                                 fprintf(stderr,
281                                         _("mount: got bad hp->h_length\n"));
282                                 hp->h_length = sizeof(*saddr);
283                         }
284                         memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
285                 }
286         }
287         return 1;
288 }
289
290 /*
291  * getport() is very similar to pmap_getport() with
292  * the exception this version uses a non-reserve ports 
293  * instead of reserve ports since reserve ports
294  * are not needed for pmap requests.
295  */
296 static u_short
297 getport(
298         struct sockaddr_in *saddr, 
299         u_long prog, 
300         u_long vers, 
301         u_int prot)
302 {
303         u_short port;
304         int    socket;
305         CLIENT *clnt = NULL;
306         struct pmap parms;
307         enum clnt_stat stat;
308
309         saddr->sin_port = htons (PMAPPORT);
310         socket = get_socket(saddr, prot, FALSE, FALSE);
311
312         switch (prot) {
313         case IPPROTO_UDP:
314                 clnt = clntudp_bufcreate(saddr,
315                                          PMAPPROG, PMAPVERS, TIMEOUT, &socket,
316                                          UDPMSGSIZE, UDPMSGSIZE);
317                 break;
318         case IPPROTO_TCP:
319                 clnt = clnttcp_create(saddr,
320                         PMAPPROG, PMAPVERS, &socket, 50, 500);
321                 break;
322         }
323         if (clnt != NULL) {
324                 parms.pm_prog = prog;
325                 parms.pm_vers = vers;
326                 parms.pm_prot = prot;
327                 parms.pm_port = 0;    /* not needed or used */
328
329                 stat = clnt_call(clnt, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
330                         (caddr_t)&parms, (xdrproc_t)xdr_u_short, (caddr_t)&port, TIMEOUT);
331                 if (stat) {
332                         clnt_geterr(clnt, &rpc_createerr.cf_error);
333                         rpc_createerr.cf_stat = stat;
334                 }
335                 clnt_destroy(clnt);
336                 if (stat != RPC_SUCCESS)
337                         port = 0;
338                 else if (port == 0)
339                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
340         }
341         if (socket != 1)
342                 close(socket);
343
344         return port;
345 }
346
347 /*
348  * Use the portmapper to discover whether or not the service we want is
349  * available. The lists 'versions' and 'protos' define ordered sequences
350  * of service versions and udp/tcp protocols to probe for.
351  */
352 static int
353 probe_port(clnt_addr_t *server, 
354            const u_long *versions,
355            const u_int *protos)
356 {
357         struct sockaddr_in *saddr = &server->saddr;
358         struct pmap *pmap = &server->pmap;
359         const u_long prog = pmap->pm_prog, *p_vers;
360         const u_int prot = (u_int)pmap->pm_prot,
361                 *p_prot;
362         const u_short port = (u_short) pmap->pm_port;
363         u_long vers = pmap->pm_vers;
364         u_short p_port;
365         p_prot = prot ? &prot : protos;
366         p_vers = vers ? &vers : versions;
367         rpc_createerr.cf_stat = 0;
368         for (;;) {
369                 saddr->sin_port = htons(PMAPPORT);
370                 p_port = getport(saddr, prog, *p_vers, *p_prot);
371                 if (p_port) {
372                         if (!port || port == p_port) {
373                                 saddr->sin_port = htons(p_port);
374                                 if (verbose) {
375                                         fprintf(stderr, 
376                                                 "mount: trying %s prog %ld vers %ld prot %s port %d\n", 
377                                                 inet_ntoa(saddr->sin_addr), prog, *p_vers,
378                                                 *p_prot == IPPROTO_UDP ? "udp" : "tcp", p_port);
379                                 }
380                                 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
381                                         goto out_ok;
382                                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
383                                         goto out_bad;
384                         }
385                 }
386                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED) 
387                         goto out_bad;
388
389                 if (!prot) {
390                         if (*++p_prot)
391                                 continue;
392                         p_prot = protos;
393                 }
394                 if (vers == pmap->pm_vers) {
395                         p_vers = versions;
396                         vers = 0;
397                 }
398                 if (vers || !*++p_vers)
399                         break;
400         }
401 out_bad:
402         return 0;
403
404  out_ok:
405         if (!vers)
406                 pmap->pm_vers = *p_vers;
407         if (!prot)
408                 pmap->pm_prot = *p_prot;
409         if (!port)
410                 pmap->pm_port = p_port;
411         rpc_createerr.cf_stat = 0;
412         return 1;
413 }
414
415 static int
416 probe_nfsport(clnt_addr_t *nfs_server)
417 {
418         const struct pmap *pmap = &nfs_server->pmap;
419         const u_long *probe_vers;
420         const u_int *probe_prot;
421
422         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
423                 return 1;
424         probe_vers = nfs_probelist(MAX_NFSPROT);
425         probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? use_tcp : udp_only);
426         return probe_port(nfs_server, probe_vers, probe_prot);
427 }
428
429 int probe_mntport(clnt_addr_t *mnt_server)
430 {
431         const struct pmap *pmap = &mnt_server->pmap;
432         const u_long *probe_vers;
433         const u_int *probe_prot;
434
435         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
436                 return 1;
437         probe_vers = mnt_probelist(MAX_MNTPROT);
438         probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? udp_tcp : udp_only);
439         return probe_port(mnt_server, probe_vers, probe_prot);
440 }
441
442 static int
443 probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
444 {
445         struct pmap *nfs_pmap = &nfs_server->pmap;
446         struct pmap *mnt_pmap = &mnt_server->pmap;
447         struct pmap save_nfs, save_mnt;
448         int res;
449         const u_long *probe_vers;
450
451         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
452                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
453         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
454                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
455         if (nfs_pmap->pm_vers)
456                 goto version_fixed;
457         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
458         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
459         for (probe_vers = mnt_probelist(MAX_MNTPROT); *probe_vers; probe_vers++) {
460                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
461                 if ((res = probe_nfsport(nfs_server) != 0)) {
462                         mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
463                         if ((res = probe_mntport(mnt_server)) != 0)
464                                 return 1;
465                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
466                 }
467                 switch (rpc_createerr.cf_stat) {
468                 case RPC_PROGVERSMISMATCH:
469                 case RPC_PROGNOTREGISTERED:
470                         break;
471                 default:
472                         goto out_bad;
473                 }
474                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
475         }
476  out_bad:
477         return 0;
478  version_fixed:
479         if (!probe_nfsport(nfs_server))
480                 goto out_bad;
481         return probe_mntport(mnt_server);
482 }
483
484 static inline enum clnt_stat
485 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
486 {
487         return clnt_call(clnt, MOUNTPROC3_MNT,
488                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
489                          (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
490                          TIMEOUT);
491 }
492
493 static inline enum clnt_stat
494 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
495 {
496         return clnt_call(clnt, MOUNTPROC_MNT,
497                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
498                          (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
499                          TIMEOUT);
500 }
501
502 static int
503 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
504                mntarg_t *mntarg, mntres_t *mntres)
505 {
506         CLIENT *clnt;
507         enum clnt_stat stat;
508         int msock;
509
510         if (!probe_bothports(mnt_server, nfs_server))
511                 goto out_bad;
512
513         clnt = mnt_openclnt(mnt_server, &msock);
514         if (!clnt)
515                 goto out_bad;
516         /* make pointers in xdr_mountres3 NULL so
517          * that xdr_array allocates memory for us
518          */
519         memset(mntres, 0, sizeof(*mntres));
520         switch (mnt_server->pmap.pm_vers) {
521         case 3:
522                 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
523                 break;
524         case 2:
525         case 1:
526                 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
527                 break;
528         default:
529                 goto out_bad;
530         }
531         if (stat != RPC_SUCCESS) {
532                 clnt_geterr(clnt, &rpc_createerr.cf_error);
533                 rpc_createerr.cf_stat = stat;
534         }
535         mnt_closeclnt(clnt, msock);
536         if (stat == RPC_SUCCESS)
537                 return 1;
538  out_bad:
539         return 0;
540 }
541
542 static int
543 parse_options(char *old_opts, struct nfs_mount_data *data,
544               int *bg, int *retry, clnt_addr_t *mnt_server,
545               clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
546 {
547         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
548         struct pmap *mnt_pmap = &mnt_server->pmap;
549         struct pmap *nfs_pmap = &nfs_server->pmap;
550         int len;
551         char *opt, *opteq;
552         char *mounthost = NULL;
553         char cbuf[128];
554
555         data->flags = 0;
556         *bg = 0;
557
558         len = strlen(new_opts);
559         for (opt = strtok(old_opts, ","); opt; opt = strtok(NULL, ",")) {
560                 if (strlen(opt) >= sizeof(cbuf))
561                         goto bad_parameter;
562                 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
563                         int val = atoi(opteq + 1);      
564                         *opteq = '\0';
565 /* printf("opt=%s\n", opt); */
566                         if (!strcmp(opt, "rsize"))
567                                 data->rsize = val;
568                         else if (!strcmp(opt, "wsize"))
569                                 data->wsize = val;
570                         else if (!strcmp(opt, "timeo"))
571                                 data->timeo = val;
572                         else if (!strcmp(opt, "retrans"))
573                                 data->retrans = val;
574                         else if (!strcmp(opt, "acregmin"))
575                                 data->acregmin = val;
576                         else if (!strcmp(opt, "acregmax"))
577                                 data->acregmax = val;
578                         else if (!strcmp(opt, "acdirmin"))
579                                 data->acdirmin = val;
580                         else if (!strcmp(opt, "acdirmax"))
581                                 data->acdirmax = val;
582                         else if (!strcmp(opt, "actimeo")) {
583                                 data->acregmin = val;
584                                 data->acregmax = val;
585                                 data->acdirmin = val;
586                                 data->acdirmax = val;
587                         }
588                         else if (!strcmp(opt, "retry"))
589                                 *retry = val;
590                         else if (!strcmp(opt, "port"))
591                                 nfs_pmap->pm_port = val;
592                         else if (!strcmp(opt, "mountport"))
593                                 mnt_pmap->pm_port = val;
594                         else if (!strcmp(opt, "mountprog"))
595                                 mnt_pmap->pm_prog = val;
596                         else if (!strcmp(opt, "mountvers"))
597                                 mnt_pmap->pm_vers = val;
598                         else if (!strcmp(opt, "mounthost"))
599                                 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
600                         else if (!strcmp(opt, "nfsprog"))
601                                 nfs_pmap->pm_prog = val;
602                         else if (!strcmp(opt, "nfsvers") ||
603                                  !strcmp(opt, "vers")) {
604                                 nfs_pmap->pm_vers = val;
605                                 opt = "nfsvers";
606 #if NFS_MOUNT_VERSION >= 2
607                         } else if (!strcmp(opt, "namlen")) {
608                                 if (nfs_mount_version >= 2)
609                                         data->namlen = val;
610                                 else if (sloppy)
611                                         continue;
612                                 else
613                                         goto bad_parameter;
614 #endif
615                         } else if (!strcmp(opt, "addr")) {
616                                 /* ignore */;
617                                 continue;
618                         } else if (sloppy)
619                                 continue;
620                         else
621                                 goto bad_parameter;
622                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
623                 } else if (opteq) {
624                         *opteq = '\0';
625                         if (!strcmp(opt, "proto")) {
626                                 if (!strcmp(opteq+1, "udp")) {
627                                         nfs_pmap->pm_prot = IPPROTO_UDP;
628                                         mnt_pmap->pm_prot = IPPROTO_UDP;
629 #if NFS_MOUNT_VERSION >= 2
630                                         data->flags &= ~NFS_MOUNT_TCP;
631                                 } else if (!strcmp(opteq+1, "tcp") &&
632                                            nfs_mount_version > 2) {
633                                         nfs_pmap->pm_prot = IPPROTO_TCP;
634                                         mnt_pmap->pm_prot = IPPROTO_TCP;
635                                         data->flags |= NFS_MOUNT_TCP;
636 #endif
637                                 } else if (sloppy)
638                                         continue;
639                                 else
640                                         goto bad_parameter;
641 #if NFS_MOUNT_VERSION >= 5
642                         } else if (!strcmp(opt, "sec")) {
643                                 char *secflavor = opteq+1;
644                                 /* see RFC 2623 */
645                                 if (nfs_mount_version < 5) {
646                                         printf(_("Warning: ignoring sec=%s option\n"), secflavor);
647                                         continue;
648                                 } else if (!strcmp(secflavor, "none"))
649                                         data->pseudoflavor = AUTH_NONE;
650                                 else if (!strcmp(secflavor, "sys"))
651                                         data->pseudoflavor = AUTH_SYS;
652                                 else if (!strcmp(secflavor, "krb5"))
653                                         data->pseudoflavor = AUTH_GSS_KRB5;
654                                 else if (!strcmp(secflavor, "krb5i"))
655                                         data->pseudoflavor = AUTH_GSS_KRB5I;
656                                 else if (!strcmp(secflavor, "krb5p"))
657                                         data->pseudoflavor = AUTH_GSS_KRB5P;
658                                 else if (!strcmp(secflavor, "lipkey"))
659                                         data->pseudoflavor = AUTH_GSS_LKEY;
660                                 else if (!strcmp(secflavor, "lipkey-i"))
661                                         data->pseudoflavor = AUTH_GSS_LKEYI;
662                                 else if (!strcmp(secflavor, "lipkey-p"))
663                                         data->pseudoflavor = AUTH_GSS_LKEYP;
664                                 else if (!strcmp(secflavor, "spkm3"))
665                                         data->pseudoflavor = AUTH_GSS_SPKM;
666                                 else if (!strcmp(secflavor, "spkm3i"))
667                                         data->pseudoflavor = AUTH_GSS_SPKMI;
668                                 else if (!strcmp(secflavor, "spkm3p"))
669                                         data->pseudoflavor = AUTH_GSS_SPKMP;
670                                 else if (sloppy)
671                                         continue;
672                                 else {
673                                         printf(_("Warning: Unrecognized security flavor %s.\n"),
674                                                 secflavor);
675                                         goto bad_parameter;
676                                 }
677                                 data->flags |= NFS_MOUNT_SECFLAVOUR;
678 #endif
679                         } else if (!strcmp(opt, "mounthost"))
680                                 mounthost=xstrndup(opteq+1,
681                                                    strcspn(opteq+1," \t\n\r,"));
682                          else if (!strcmp(opt, "context")) {
683                                 char *context = opteq + 1;
684                                 
685                                 if (strlen(context) > NFS_MAX_CONTEXT_LEN) {
686                                         printf(_("context parameter exceeds limit of %d\n"),
687                                                  NFS_MAX_CONTEXT_LEN);
688                                         goto bad_parameter;
689                                 }
690                                 strncpy(data->context, context, NFS_MAX_CONTEXT_LEN);
691                         } else if (sloppy)
692                                 continue;
693                         else
694                                 goto bad_parameter;
695                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
696                 } else {
697                         int val = 1;
698                         if (!strncmp(opt, "no", 2)) {
699                                 val = 0;
700                                 opt += 2;
701                         }
702                         if (!strcmp(opt, "bg")) 
703                                 *bg = val;
704                         else if (!strcmp(opt, "fg")) 
705                                 *bg = !val;
706                         else if (!strcmp(opt, "soft")) {
707                                 data->flags &= ~NFS_MOUNT_SOFT;
708                                 if (val)
709                                         data->flags |= NFS_MOUNT_SOFT;
710                         } else if (!strcmp(opt, "hard")) {
711                                 data->flags &= ~NFS_MOUNT_SOFT;
712                                 if (!val)
713                                         data->flags |= NFS_MOUNT_SOFT;
714                         } else if (!strcmp(opt, "intr")) {
715                                 data->flags &= ~NFS_MOUNT_INTR;
716                                 if (val)
717                                         data->flags |= NFS_MOUNT_INTR;
718                         } else if (!strcmp(opt, "posix")) {
719                                 data->flags &= ~NFS_MOUNT_POSIX;
720                                 if (val)
721                                         data->flags |= NFS_MOUNT_POSIX;
722                         } else if (!strcmp(opt, "cto")) {
723                                 data->flags &= ~NFS_MOUNT_NOCTO;
724                                 if (!val)
725                                         data->flags |= NFS_MOUNT_NOCTO;
726                         } else if (!strcmp(opt, "ac")) {
727                                 data->flags &= ~NFS_MOUNT_NOAC;
728                                 if (!val)
729                                         data->flags |= NFS_MOUNT_NOAC;
730 #if NFS_MOUNT_VERSION >= 2
731                         } else if (!strcmp(opt, "tcp")) {
732                                 data->flags &= ~NFS_MOUNT_TCP;
733                                 if (val) {
734                                         if (nfs_mount_version < 2)
735                                                 goto bad_option;
736                                         nfs_pmap->pm_prot = IPPROTO_TCP;
737                                         mnt_pmap->pm_prot = IPPROTO_TCP;
738                                         data->flags |= NFS_MOUNT_TCP;
739                                 } else {
740                                         mnt_pmap->pm_prot = IPPROTO_UDP;
741                                         nfs_pmap->pm_prot = IPPROTO_UDP;
742                                 }
743                         } else if (!strcmp(opt, "udp")) {
744                                 data->flags &= ~NFS_MOUNT_TCP;
745                                 if (!val) {
746                                         if (nfs_mount_version < 2)
747                                                 goto bad_option;
748                                         nfs_pmap->pm_prot = IPPROTO_TCP;
749                                         mnt_pmap->pm_prot = IPPROTO_TCP;
750                                         data->flags |= NFS_MOUNT_TCP;
751                                 } else {
752                                         nfs_pmap->pm_prot = IPPROTO_UDP;
753                                         mnt_pmap->pm_prot = IPPROTO_UDP;
754                                 }
755 #endif
756 #if NFS_MOUNT_VERSION >= 3
757                         } else if (!strcmp(opt, "lock")) {
758                                 data->flags &= ~NFS_MOUNT_NONLM;
759                                 if (!val) {
760                                         if (nfs_mount_version < 3)
761                                                 goto bad_option;
762                                         data->flags |= NFS_MOUNT_NONLM;
763                                 }
764 #endif
765 #if NFS_MOUNT_VERSION >= 4
766                         } else if (!strcmp(opt, "broken_suid")) {
767                                 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
768                                 if (val) {
769                                         if (nfs_mount_version < 4)
770                                                 goto bad_option;
771                                         data->flags |= NFS_MOUNT_BROKEN_SUID;
772                                 }
773                         } else if (!strcmp(opt, "acl")) {
774                                 data->flags &= ~NFS_MOUNT_NOACL;
775                                 if (!val)
776                                         data->flags |= NFS_MOUNT_NOACL;
777 #endif
778                         } else {
779                         bad_option:
780                                 if (sloppy)
781                                         continue;
782                                 printf(_("Unsupported nfs mount option: "
783                                          "%s%s\n"), val ? "" : "no", opt);
784                                 goto out_bad;
785                         }
786                         sprintf(cbuf, val ? "%s,":"no%s,", opt);
787                 }
788                 len += strlen(cbuf);
789                 if (len >= opt_size) {
790                         printf(_("mount: excessively long option argument\n"));
791                         goto out_bad;
792                 }
793                 strcat(new_opts, cbuf);
794         }
795         /* See if the nfs host = mount host. */
796         if (mounthost) {
797                 if (!nfs_gethostbyname(mounthost, mnt_saddr))
798                         goto out_bad;
799                 *mnt_server->hostname = mounthost;
800         }
801         return 1;
802  bad_parameter:
803         printf(_("Bad nfs mount parameter: %s\n"), opt);
804  out_bad:
805         return 0;
806 }
807
808 static inline int
809 nfsmnt_check_compat(const struct pmap *nfs_pmap, const struct pmap *mnt_pmap)
810 {
811         if (nfs_pmap->pm_vers && 
812                 (nfs_pmap->pm_vers > MAX_NFSPROT || nfs_pmap->pm_vers < 2)) {
813                 if (nfs_pmap->pm_vers == 4)
814                         fprintf(stderr, _("'vers=4' is not supported.  "
815                                 "Use '-t nfs4' instead.\n"));
816                 else
817                         fprintf(stderr, _("NFS version %ld is not supported.\n"), 
818                                 nfs_pmap->pm_vers);
819                 goto out_bad;
820         }
821         if (mnt_pmap->pm_vers > MAX_MNTPROT) {
822                 fprintf(stderr, _("NFS mount version %ld s not supported.\n"), 
823                         mnt_pmap->pm_vers);
824                 goto out_bad;
825         }
826         return 1;
827  out_bad:
828         return 0;
829 }
830
831 int
832 nfsmount(const char *spec, const char *node, int *flags,
833          char **extra_opts, char **mount_opts, int *nfs_mount_vers,
834          int running_bg, int *need_statd)
835 {
836         static char *prev_bg_host;
837         char hostdir[1024];
838         char *hostname, *dirname, *old_opts, *mounthost = NULL;
839         char new_opts[1024], cbuf[1024];
840         static struct nfs_mount_data data;
841         int val;
842         static int doonce = 0;
843
844         clnt_addr_t mnt_server = { &mounthost, };
845         clnt_addr_t nfs_server = { &hostname, };
846         struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
847         struct pmap  *mnt_pmap = &mnt_server.pmap, 
848                      *nfs_pmap = &nfs_server.pmap;
849         struct pmap  save_mnt, save_nfs;
850
851         int fsock = -1;
852
853         mntres_t mntres;
854
855         struct stat statbuf;
856         char *s;
857         int bg, retry;
858         int retval = EX_FAIL;
859         time_t t;
860         time_t prevt;
861         time_t timeout;
862
863         /* The version to try is either specified or 0
864            In case it is 0 we tell the caller what we tried */
865         if (!*nfs_mount_vers)
866                 *nfs_mount_vers = find_kernel_nfs_mount_version();
867         nfs_mount_version = *nfs_mount_vers;
868
869         if (strlen(spec) >= sizeof(hostdir)) {
870                 fprintf(stderr, _("mount: "
871                                   "excessively long host:dir argument\n"));
872                 goto fail;
873         }
874         strcpy(hostdir, spec);
875         if ((s = strchr(hostdir, ':'))) {
876                 hostname = hostdir;
877                 dirname = s + 1;
878                 *s = '\0';
879                 /* Ignore all but first hostname in replicated mounts
880                    until they can be fully supported. (mack@sgi.com) */
881                 if ((s = strchr(hostdir, ','))) {
882                         *s = '\0';
883                         fprintf(stderr,
884                                 _("mount: warning: "
885                                   "multiple hostnames not supported\n"));
886                 }
887         } else {
888                 fprintf(stderr,
889                         _("mount: "
890                           "directory to mount not in host:dir format\n"));
891                 goto fail;
892         }
893
894         if (!nfs_gethostbyname(hostname, nfs_saddr))
895                 goto fail;
896         mounthost = hostname;
897         memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
898
899         /* add IP address to mtab options for use when unmounting */
900
901         s = inet_ntoa(nfs_saddr->sin_addr);
902         old_opts = *extra_opts;
903         if (!old_opts)
904                 old_opts = "";
905
906         /* Set default options.
907          * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
908          * let the kernel decide.
909          * timeo is filled in after we know whether it'll be TCP or UDP. */
910         memset(&data, 0, sizeof(data));
911         data.acregmin   = 3;
912         data.acregmax   = 60;
913         data.acdirmin   = 30;
914         data.acdirmax   = 60;
915 #if NFS_MOUNT_VERSION >= 2
916         data.namlen     = NAME_MAX;
917 #endif
918
919         bg = 0;
920         retry = 10000;          /* 10000 minutes ~ 1 week */
921
922         memset(mnt_pmap, 0, sizeof(*mnt_pmap));
923         mnt_pmap->pm_prog = MOUNTPROG;
924         memset(nfs_pmap, 0, sizeof(*nfs_pmap));
925         nfs_pmap->pm_prog = NFS_PROGRAM;
926
927         /* parse options */
928         new_opts[0] = 0;
929         if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
930                            new_opts, sizeof(new_opts)))
931                 goto fail;
932         if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
933                 goto fail;
934         
935         if (retry == 10000 && !bg)
936                 retry = 2; /* reset for fg mounts */
937         
938
939 #ifdef NFS_MOUNT_DEBUG
940         printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
941                data.rsize, data.wsize, data.timeo, data.retrans);
942         printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
943                data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
944         printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
945                nfs_pmap->pm_port, bg, retry, data.flags);
946         printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
947                mnt_pmap->pm_prog, mnt_pmap->pm_vers,
948                nfs_pmap->pm_prog, nfs_pmap->pm_vers);
949         printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d ",
950                (data.flags & NFS_MOUNT_SOFT) != 0,
951                (data.flags & NFS_MOUNT_INTR) != 0,
952                (data.flags & NFS_MOUNT_POSIX) != 0,
953                (data.flags & NFS_MOUNT_NOCTO) != 0,
954                (data.flags & NFS_MOUNT_NOAC) != 0);
955 #if NFS_MOUNT_VERSION >= 2
956         printf("tcp = %d ",
957                (data.flags & NFS_MOUNT_TCP) != 0);
958 #endif
959 #if NFS_MOUNT_VERSION >= 4
960         printf("noacl = %d ", (data.flags & NFS_MOUNT_NOACL) != 0);
961 #endif
962 #if NFS_MOUNT_VERSION >= 5
963         printf("sec = %u ", data.pseudoflavor);
964 #endif
965         printf("\n");
966 #endif
967
968         data.version = nfs_mount_version;
969         *mount_opts = (char *) &data;
970
971         if (*flags & MS_REMOUNT)
972                 goto out_ok;
973
974         /*
975          * If the previous mount operation on the same host was
976          * backgrounded, and the "bg" for this mount is also set,
977          * give up immediately, to avoid the initial timeout.
978          */
979         if (bg && !running_bg &&
980             prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
981                 if (retry > 0)
982                         retval = EX_BG;
983                 return retval;
984         }
985
986         /* create mount deamon client */
987
988         /*
989          * The following loop implements the mount retries. On the first
990          * call, "running_bg" is 0. When the mount times out, and the
991          * "bg" option is set, the exit status EX_BG will be returned.
992          * For a backgrounded mount, there will be a second call by the
993          * child process with "running_bg" set to 1.
994          *
995          * The case where the mount point is not present and the "bg"
996          * option is set, is treated as a timeout. This is done to
997          * support nested mounts.
998          *
999          * The "retry" count specified by the user is the number of
1000          * minutes to retry before giving up.
1001          *
1002          * Only the first error message will be displayed.
1003          */
1004         timeout = time(NULL) + 60 * retry;
1005         prevt = 0;
1006         t = 30;
1007         val = 1;
1008
1009         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
1010         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
1011         for (;;) {
1012                 if (bg && stat(node, &statbuf) == -1) {
1013                         /* no mount point yet - sleep */
1014                         if (running_bg) {
1015                                 sleep(val);     /* 1, 2, 4, 8, 16, 30, ... */
1016                                 val *= 2;
1017                                 if (val > 30)
1018                                         val = 30;
1019                         }
1020                 } else {
1021                         int stat;
1022                         /* be careful not to use too many CPU cycles */
1023                         if (t - prevt < 30)
1024                                 sleep(30);
1025
1026                         stat = nfs_call_mount(&mnt_server, &nfs_server,
1027                                               &dirname, &mntres);
1028                         if (stat)
1029                                 break;
1030                         memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
1031                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
1032                         prevt = t;
1033                 }
1034                 if (!bg) {
1035                         switch(rpc_createerr.cf_stat){
1036                         case RPC_TIMEDOUT:
1037                                 break;
1038                         case RPC_SYSTEMERROR:
1039                                 if (errno == ETIMEDOUT)
1040                                         break;
1041                         default:
1042                                 mount_errors(*nfs_server.hostname, 0, bg);
1043                         goto fail;
1044                         }
1045                         t = time(NULL);
1046                         if (t >= timeout) {
1047                                 mount_errors(*nfs_server.hostname, 0, bg);
1048                                 goto fail;
1049                         }
1050                         mount_errors(*nfs_server.hostname, 1, bg);
1051                         continue;
1052                 }
1053                 if (!running_bg) {
1054                         prev_bg_host = xstrdup(hostname);
1055                         if (retry > 0)
1056                                 retval = EX_BG;
1057                         goto fail;
1058                 }
1059                 t = time(NULL);
1060                 if (t >= timeout) {
1061                         mount_errors(*nfs_server.hostname, 0, bg);
1062                         goto fail;
1063                 }
1064                 if (doonce++ < 1)
1065                         mount_errors(*nfs_server.hostname, 1, bg);
1066         }
1067
1068         if (nfs_pmap->pm_vers == 2) {
1069                 if (mntres.nfsv2.fhs_status != 0) {
1070                         fprintf(stderr,
1071                                 _("mount: %s:%s failed, reason given by server: %s\n"),
1072                                 hostname, dirname,
1073                                 nfs_strerror(mntres.nfsv2.fhs_status));
1074                         goto fail;
1075                 }
1076                 memcpy(data.root.data,
1077                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
1078                        NFS_FHSIZE);
1079 #if NFS_MOUNT_VERSION >= 4
1080                 data.root.size = NFS_FHSIZE;
1081                 memcpy(data.old_root.data,
1082                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
1083                        NFS_FHSIZE);
1084 #endif
1085         } else {
1086 #if NFS_MOUNT_VERSION >= 4
1087                 mountres3_ok *mountres;
1088                 fhandle3 *fhandle;
1089                 int i, *flavor, yum = 0;
1090                 if (mntres.nfsv3.fhs_status != 0) {
1091                         fprintf(stderr,
1092                                 _("mount: %s:%s failed, reason given by server: %s\n"),
1093                                 hostname, dirname,
1094                                 nfs_strerror(mntres.nfsv3.fhs_status));
1095                         goto fail;
1096                 }
1097 #if NFS_MOUNT_VERSION >= 5
1098                 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
1099                 i = mountres->auth_flavors.auth_flavors_len;
1100                 if (i <= 0) 
1101                         goto noauth_flavors;
1102
1103                 flavor = mountres->auth_flavors.auth_flavors_val;
1104                 while (--i >= 0) {
1105                         /* If no flavour requested, use first simple
1106                          * flavour that is offered.
1107                          */
1108                         if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
1109                             (flavor[i] == AUTH_SYS ||
1110                              flavor[i] == AUTH_NONE)) {
1111                                 data.pseudoflavor = flavor[i];
1112                                 data.flags |= NFS_MOUNT_SECFLAVOUR;
1113                         }
1114                         if (flavor[i] == data.pseudoflavor)
1115                                 yum = 1;
1116 #ifdef NFS_MOUNT_DEBUG
1117                         printf("auth flavor %d: %d\n",
1118                                 i, flavor[i]);
1119 #endif
1120                 }
1121                 if (!yum) {
1122                         fprintf(stderr,
1123                                 "mount: %s:%s failed, "
1124                                 "security flavor not supported\n",
1125                                 hostname, dirname);
1126                         /* server has registered us in rmtab, send umount */
1127                         nfs_call_umount(&mnt_server, &dirname);
1128                         goto fail;
1129                 }
1130 noauth_flavors:
1131 #endif
1132                 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
1133                 memset(data.old_root.data, 0, NFS_FHSIZE);
1134                 memset(&data.root, 0, sizeof(data.root));
1135                 data.root.size = fhandle->fhandle3_len;
1136                 memcpy(data.root.data,
1137                        (char *) fhandle->fhandle3_val,
1138                        fhandle->fhandle3_len);
1139
1140                 data.flags |= NFS_MOUNT_VER3;
1141 #endif
1142         }
1143
1144         if (nfs_mount_version == 1) {
1145                 /* create nfs socket for kernel */
1146                 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1147                         fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1148                 else
1149                         fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1150                 if (fsock < 0) {
1151                         perror(_("nfs socket"));
1152                         goto fail;
1153                 }
1154                 if (bindresvport(fsock, 0) < 0) {
1155                         perror(_("nfs bindresvport"));
1156                         goto fail;
1157                 }
1158         }
1159
1160 #ifdef NFS_MOUNT_DEBUG
1161         printf(_("using port %d for nfs deamon\n"), nfs_pmap->pm_port);
1162 #endif
1163         nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
1164         /*
1165          * connect() the socket for kernels 1.3.10 and below only,
1166          * to avoid problems with multihomed hosts.
1167          * --Swen
1168          */
1169         if (linux_version_code() <= 0x01030a && fsock != -1
1170             && connect(fsock, (struct sockaddr *) nfs_saddr,
1171                        sizeof (*nfs_saddr)) < 0) {
1172                 perror(_("nfs connect"));
1173                 goto fail;
1174         }
1175
1176 #if NFS_MOUNT_VERSION >= 2
1177         if (nfs_pmap->pm_prot == IPPROTO_TCP)
1178                 data.flags |= NFS_MOUNT_TCP;
1179         else
1180                 data.flags &= ~NFS_MOUNT_TCP;
1181 #endif
1182
1183         /* prepare data structure for kernel */
1184
1185         data.fd = fsock;
1186         memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
1187         strncpy(data.hostname, hostname, sizeof(data.hostname));
1188
1189  out_ok:
1190         /* Ensure we have enough padding for the following strcat()s */
1191         if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
1192                 fprintf(stderr, _("mount: "
1193                                   "excessively long option argument\n"));
1194                 goto fail;
1195         }
1196
1197         snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
1198         strcat(new_opts, cbuf);
1199
1200         *extra_opts = xstrdup(new_opts);
1201         *need_statd = ! (data.flags & NFS_MOUNT_NONLM);
1202         return 0;
1203
1204         /* abort */
1205  fail:
1206         if (fsock != -1)
1207                 close(fsock);
1208         return retval;
1209 }
1210
1211 /*
1212  * We need to translate between nfs status return values and
1213  * the local errno values which may not be the same.
1214  *
1215  * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
1216  * "after #include <errno.h> the symbol errno is reserved for any use,
1217  *  it cannot even be used as a struct tag or field name".
1218  */
1219
1220 #ifndef EDQUOT
1221 #define EDQUOT  ENOSPC
1222 #endif
1223
1224 static struct {
1225         enum nfsstat stat;
1226         int errnum;
1227 } nfs_errtbl[] = {
1228         { NFS_OK,               0               },
1229         { NFSERR_PERM,          EPERM           },
1230         { NFSERR_NOENT,         ENOENT          },
1231         { NFSERR_IO,            EIO             },
1232         { NFSERR_NXIO,          ENXIO           },
1233         { NFSERR_ACCES,         EACCES          },
1234         { NFSERR_EXIST,         EEXIST          },
1235         { NFSERR_NODEV,         ENODEV          },
1236         { NFSERR_NOTDIR,        ENOTDIR         },
1237         { NFSERR_ISDIR,         EISDIR          },
1238 #ifdef NFSERR_INVAL
1239         { NFSERR_INVAL,         EINVAL          },      /* that Sun forgot */
1240 #endif
1241         { NFSERR_FBIG,          EFBIG           },
1242         { NFSERR_NOSPC,         ENOSPC          },
1243         { NFSERR_ROFS,          EROFS           },
1244         { NFSERR_NAMETOOLONG,   ENAMETOOLONG    },
1245         { NFSERR_NOTEMPTY,      ENOTEMPTY       },
1246         { NFSERR_DQUOT,         EDQUOT          },
1247         { NFSERR_STALE,         ESTALE          },
1248 #ifdef EWFLUSH
1249         { NFSERR_WFLUSH,        EWFLUSH         },
1250 #endif
1251         /* Throw in some NFSv3 values for even more fun (HP returns these) */
1252         { 71,                   EREMOTE         },
1253
1254         { -1,                   EIO             }
1255 };
1256
1257 static char *nfs_strerror(int stat)
1258 {
1259         int i;
1260         static char buf[256];
1261
1262         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
1263                 if (nfs_errtbl[i].stat == stat)
1264                         return strerror(nfs_errtbl[i].errnum);
1265         }
1266         sprintf(buf, _("unknown nfs status return value: %d"), stat);
1267         return buf;
1268 }