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