]> git.ipfire.org Git - thirdparty/util-linux.git/blob - mount/nfsmount.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / 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@misiek.eu.org>
25 * - added Native Language Support
26 *
27 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
28 * plus NFSv3 stuff.
29 */
30
31 /*
32 * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
33 */
34
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <netdb.h>
40 #include <rpc/rpc.h>
41 #include <rpc/pmap_prot.h>
42 #include <rpc/pmap_clnt.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <sys/utsname.h>
46 #include <sys/stat.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49
50 #include "sundries.h"
51 #include "nfsmount.h"
52
53 #include <linux/nfs.h>
54 #include "mount_constants.h"
55 #include "nfs_mount4.h"
56
57 #include "nls.h"
58
59 #include "../defines.h" /* for HAVE_inet_aton */
60
61 #ifndef NFS_PORT
62 #define NFS_PORT 2049
63 #endif
64 #ifndef NFS_FHSIZE
65 #define NFS_FHSIZE 32
66 #endif
67
68 static char *nfs_strerror(int stat);
69
70 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
71
72 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
73
74 static int
75 linux_version_code(void) {
76 struct utsname my_utsname;
77 int p, q, r;
78
79 if (uname(&my_utsname) == 0) {
80 p = atoi(strtok(my_utsname.release, "."));
81 q = atoi(strtok(NULL, "."));
82 r = atoi(strtok(NULL, "."));
83 return MAKE_VERSION(p,q,r);
84 }
85 return 0;
86 }
87
88 /*
89 * nfs_mount_version according to the sources seen at compile time.
90 */
91 int nfs_mount_version = NFS_MOUNT_VERSION;
92
93 /*
94 * Unfortunately, the kernel prints annoying console messages
95 * in case of an unexpected nfs mount version (instead of
96 * just returning some error). Therefore we'll have to try
97 * and figure out what version the kernel expects.
98 *
99 * Variables:
100 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
101 * nfs_mount_version: version this source and running kernel can handle
102 */
103 static void
104 find_kernel_nfs_mount_version(void) {
105 static int kernel_version = 0;
106
107 if (kernel_version)
108 return;
109
110 kernel_version = linux_version_code();
111
112 if (kernel_version) {
113 if (kernel_version < MAKE_VERSION(2,1,32))
114 nfs_mount_version = 1;
115 else if (kernel_version < MAKE_VERSION(2,2,18))
116 nfs_mount_version = 3;
117 else if (kernel_version < MAKE_VERSION(2,3,0))
118 nfs_mount_version = 4; /* since 2.2.18pre9 */
119 else if (kernel_version < MAKE_VERSION(2,3,99))
120 nfs_mount_version = 3;
121 else
122 nfs_mount_version = 4; /* since 2.3.99pre4 */
123 }
124 if (nfs_mount_version > NFS_MOUNT_VERSION)
125 nfs_mount_version = NFS_MOUNT_VERSION;
126 }
127
128 static struct pmap *
129 get_mountport(struct sockaddr_in *server_addr,
130 long unsigned prog,
131 long unsigned version,
132 long unsigned proto,
133 long unsigned port)
134 {
135 struct pmaplist *pmap;
136 static struct pmap p = {0, 0, 0, 0};
137
138 server_addr->sin_port = PMAPPORT;
139 pmap = pmap_getmaps(server_addr);
140
141 if (version > MAX_NFSPROT)
142 version = MAX_NFSPROT;
143 if (!prog)
144 prog = MOUNTPROG;
145 p.pm_prog = prog;
146 p.pm_vers = version;
147 p.pm_prot = proto;
148 p.pm_port = port;
149
150 while (pmap) {
151 if (pmap->pml_map.pm_prog != prog)
152 goto next;
153 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
154 goto next;
155 if (version > 2 && pmap->pml_map.pm_vers != version)
156 goto next;
157 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
158 goto next;
159 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
160 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
161 (port && pmap->pml_map.pm_port != port))
162 goto next;
163 memcpy(&p, &pmap->pml_map, sizeof(p));
164 next:
165 pmap = pmap->pml_next;
166 }
167 if (!p.pm_vers)
168 p.pm_vers = MOUNTVERS;
169 if (!p.pm_port)
170 p.pm_port = MOUNTPORT;
171 if (!p.pm_prot)
172 p.pm_prot = IPPROTO_TCP;
173 return &p;
174 }
175
176 int nfsmount(const char *spec, const char *node, int *flags,
177 char **extra_opts, char **mount_opts, int running_bg)
178 {
179 static char *prev_bg_host;
180 char hostdir[1024];
181 CLIENT *mclient;
182 char *hostname;
183 char *dirname;
184 char *old_opts;
185 char *mounthost=NULL;
186 char new_opts[1024];
187 struct timeval total_timeout;
188 enum clnt_stat clnt_stat;
189 static struct nfs_mount_data data;
190 char *opt, *opteq;
191 int val;
192 struct hostent *hp;
193 struct sockaddr_in server_addr;
194 struct sockaddr_in mount_server_addr;
195 struct pmap* pm_mnt;
196 int msock, fsock;
197 struct timeval retry_timeout;
198 union {
199 struct fhstatus nfsv2;
200 struct mountres3 nfsv3;
201 } status;
202 struct stat statbuf;
203 char *s;
204 int port;
205 int mountport;
206 int proto;
207 int bg;
208 int soft;
209 int intr;
210 int posix;
211 int nocto;
212 int noac;
213 int nolock;
214 int broken_suid;
215 int retry;
216 int tcp;
217 int mountprog;
218 int mountvers;
219 int nfsprog;
220 int nfsvers;
221 int retval;
222 time_t t;
223 time_t prevt;
224 time_t timeout;
225
226 find_kernel_nfs_mount_version();
227
228 retval = EX_FAIL;
229 msock = fsock = -1;
230 mclient = NULL;
231 if (strlen(spec) >= sizeof(hostdir)) {
232 fprintf(stderr, _("mount: "
233 "excessively long host:dir argument\n"));
234 goto fail;
235 }
236 strcpy(hostdir, spec);
237 if ((s = strchr(hostdir, ':'))) {
238 hostname = hostdir;
239 dirname = s + 1;
240 *s = '\0';
241 /* Ignore all but first hostname in replicated mounts
242 until they can be fully supported. (mack@sgi.com) */
243 if ((s = strchr(hostdir, ','))) {
244 *s = '\0';
245 fprintf(stderr, _("mount: warning: "
246 "multiple hostnames not supported\n"));
247 }
248 } else {
249 fprintf(stderr, _("mount: "
250 "directory to mount not in host:dir format\n"));
251 goto fail;
252 }
253
254 server_addr.sin_family = AF_INET;
255 #ifdef HAVE_inet_aton
256 if (!inet_aton(hostname, &server_addr.sin_addr))
257 #endif
258 {
259 if ((hp = gethostbyname(hostname)) == NULL) {
260 fprintf(stderr, _("mount: can't get address for %s\n"),
261 hostname);
262 goto fail;
263 } else {
264 if (hp->h_length > sizeof(struct in_addr)) {
265 fprintf(stderr,
266 _("mount: got bad hp->h_length\n"));
267 hp->h_length = sizeof(struct in_addr);
268 }
269 memcpy(&server_addr.sin_addr,
270 hp->h_addr, hp->h_length);
271 }
272 }
273
274 memcpy (&mount_server_addr, &server_addr, sizeof (mount_server_addr));
275
276 /* add IP address to mtab options for use when unmounting */
277
278 s = inet_ntoa(server_addr.sin_addr);
279 old_opts = *extra_opts;
280 if (!old_opts)
281 old_opts = "";
282 if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
283 fprintf(stderr, _("mount: "
284 "excessively long option argument\n"));
285 goto fail;
286 }
287 sprintf(new_opts, "%s%saddr=%s",
288 old_opts, *old_opts ? "," : "", s);
289 *extra_opts = xstrdup(new_opts);
290
291 /* Set default options.
292 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
293 * let the kernel decide.
294 * timeo is filled in after we know whether it'll be TCP or UDP. */
295 memset(&data, 0, sizeof(data));
296 data.retrans = 3;
297 data.acregmin = 3;
298 data.acregmax = 60;
299 data.acdirmin = 30;
300 data.acdirmax = 60;
301 #if NFS_MOUNT_VERSION >= 2
302 data.namlen = NAME_MAX;
303 #endif
304
305 bg = 0;
306 soft = 0;
307 intr = 0;
308 posix = 0;
309 nocto = 0;
310 nolock = 0;
311 broken_suid = 0;
312 noac = 0;
313 retry = 10000; /* 10000 minutes ~ 1 week */
314 tcp = 0;
315
316 mountprog = MOUNTPROG;
317 mountvers = 0;
318 port = 0;
319 mountport = 0;
320 nfsprog = NFS_PROGRAM;
321 nfsvers = 0;
322
323 /* parse options */
324
325 for (opt = strtok(old_opts, ","); opt; opt = strtok(NULL, ",")) {
326 if ((opteq = strchr(opt, '='))) {
327 val = atoi(opteq + 1);
328 *opteq = '\0';
329 if (!strcmp(opt, "rsize"))
330 data.rsize = val;
331 else if (!strcmp(opt, "wsize"))
332 data.wsize = val;
333 else if (!strcmp(opt, "timeo"))
334 data.timeo = val;
335 else if (!strcmp(opt, "retrans"))
336 data.retrans = val;
337 else if (!strcmp(opt, "acregmin"))
338 data.acregmin = val;
339 else if (!strcmp(opt, "acregmax"))
340 data.acregmax = val;
341 else if (!strcmp(opt, "acdirmin"))
342 data.acdirmin = val;
343 else if (!strcmp(opt, "acdirmax"))
344 data.acdirmax = val;
345 else if (!strcmp(opt, "actimeo")) {
346 data.acregmin = val;
347 data.acregmax = val;
348 data.acdirmin = val;
349 data.acdirmax = val;
350 }
351 else if (!strcmp(opt, "retry"))
352 retry = val;
353 else if (!strcmp(opt, "port"))
354 port = val;
355 else if (!strcmp(opt, "mountport"))
356 mountport = val;
357 else if (!strcmp(opt, "mounthost"))
358 mounthost=xstrndup(opteq+1,
359 strcspn(opteq+1," \t\n\r,"));
360 else if (!strcmp(opt, "mountprog"))
361 mountprog = val;
362 else if (!strcmp(opt, "mountvers"))
363 mountvers = val;
364 else if (!strcmp(opt, "nfsprog"))
365 nfsprog = val;
366 else if (!strcmp(opt, "nfsvers") ||
367 !strcmp(opt, "vers"))
368 nfsvers = val;
369 else if (!strcmp(opt, "proto")) {
370 if (!strncmp(opteq+1, "tcp", 3))
371 tcp = 1;
372 else if (!strncmp(opteq+1, "udp", 3))
373 tcp = 0;
374 else
375 printf(_("Warning: Unrecognized proto= option.\n"));
376 } else if (!strcmp(opt, "namlen")) {
377 #if NFS_MOUNT_VERSION >= 2
378 if (nfs_mount_version >= 2)
379 data.namlen = val;
380 else
381 #endif
382 printf(_("Warning: Option namlen is not supported.\n"));
383 } else if (!strcmp(opt, "addr"))
384 /* ignore */;
385 else {
386 printf(_("unknown nfs mount parameter: "
387 "%s=%d\n"), opt, val);
388 goto fail;
389 }
390 }
391 else {
392 val = 1;
393 if (!strncmp(opt, "no", 2)) {
394 val = 0;
395 opt += 2;
396 }
397 if (!strcmp(opt, "bg"))
398 bg = val;
399 else if (!strcmp(opt, "fg"))
400 bg = !val;
401 else if (!strcmp(opt, "soft"))
402 soft = val;
403 else if (!strcmp(opt, "hard"))
404 soft = !val;
405 else if (!strcmp(opt, "intr"))
406 intr = val;
407 else if (!strcmp(opt, "posix"))
408 posix = val;
409 else if (!strcmp(opt, "cto"))
410 nocto = !val;
411 else if (!strcmp(opt, "ac"))
412 noac = !val;
413 else if (!strcmp(opt, "tcp"))
414 tcp = val;
415 else if (!strcmp(opt, "udp"))
416 tcp = !val;
417 else if (!strcmp(opt, "lock")) {
418 if (nfs_mount_version >= 3)
419 nolock = !val;
420 else
421 printf(_("Warning: option nolock is not supported.\n"));
422 } else if (!strcmp(opt, "broken_suid")) {
423 broken_suid = val;
424 } else {
425 if (!sloppy) {
426 printf(_("unknown nfs mount option: "
427 "%s%s\n"), val ? "" : "no", opt);
428 goto fail;
429 }
430 }
431 }
432 }
433 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
434
435 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
436 | (intr ? NFS_MOUNT_INTR : 0)
437 | (posix ? NFS_MOUNT_POSIX : 0)
438 | (nocto ? NFS_MOUNT_NOCTO : 0)
439 | (noac ? NFS_MOUNT_NOAC : 0);
440 #if NFS_MOUNT_VERSION >= 2
441 if (nfs_mount_version >= 2)
442 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
443 #endif
444 #if NFS_MOUNT_VERSION >= 3
445 if (nfs_mount_version >= 3)
446 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
447 #endif
448 #if NFS_MOUNT_VERSION >= 4
449 if (nfs_mount_version >= 4)
450 data.flags |= (broken_suid ? NFS_MOUNT_BROKEN_SUID : 0);
451 #endif
452 if (nfsvers > MAX_NFSPROT) {
453 fprintf(stderr, "NFSv%d not supported!\n", nfsvers);
454 return 0;
455 }
456 if (mountvers > MAX_NFSPROT) {
457 fprintf(stderr, "NFSv%d not supported!\n", nfsvers);
458 return 0;
459 }
460 if (nfsvers && !mountvers)
461 mountvers = (nfsvers < 3) ? 1 : nfsvers;
462 if (nfsvers && nfsvers < mountvers) {
463 mountvers = nfsvers;
464 }
465
466 /* Adjust options if none specified */
467 if (!data.timeo)
468 data.timeo = tcp ? 70 : 7;
469
470 #ifdef NFS_MOUNT_DEBUG
471 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
472 data.rsize, data.wsize, data.timeo, data.retrans);
473 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
474 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
475 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
476 port, bg, retry, data.flags);
477 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
478 mountprog, mountvers, nfsprog, nfsvers);
479 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d\n",
480 (data.flags & NFS_MOUNT_SOFT) != 0,
481 (data.flags & NFS_MOUNT_INTR) != 0,
482 (data.flags & NFS_MOUNT_POSIX) != 0,
483 (data.flags & NFS_MOUNT_NOCTO) != 0,
484 (data.flags & NFS_MOUNT_NOAC) != 0);
485 #if NFS_MOUNT_VERSION >= 2
486 printf("tcp = %d\n",
487 (data.flags & NFS_MOUNT_TCP) != 0);
488 #endif
489 #endif
490
491 data.version = nfs_mount_version;
492 *mount_opts = (char *) &data;
493
494 if (*flags & MS_REMOUNT)
495 return 0;
496
497 /*
498 * If the previous mount operation on the same host was
499 * backgrounded, and the "bg" for this mount is also set,
500 * give up immediately, to avoid the initial timeout.
501 */
502 if (bg && !running_bg &&
503 prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
504 if (retry > 0)
505 retval = EX_BG;
506 return retval;
507 }
508
509 /* create mount deamon client */
510 /* See if the nfs host = mount host. */
511 if (mounthost) {
512 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
513 mount_server_addr.sin_family = AF_INET;
514 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
515 } else {
516 if ((hp = gethostbyname(mounthost)) == NULL) {
517 fprintf(stderr, _("mount: can't get address for %s\n"),
518 hostname);
519 goto fail;
520 } else {
521 if (hp->h_length > sizeof(struct in_addr)) {
522 fprintf(stderr,
523 _("mount: got bad hp->h_length?\n"));
524 hp->h_length = sizeof(struct in_addr);
525 }
526 mount_server_addr.sin_family = AF_INET;
527 memcpy(&mount_server_addr.sin_addr,
528 hp->h_addr, hp->h_length);
529 }
530 }
531 }
532
533 /*
534 * The following loop implements the mount retries. On the first
535 * call, "running_bg" is 0. When the mount times out, and the
536 * "bg" option is set, the exit status EX_BG will be returned.
537 * For a backgrounded mount, there will be a second call by the
538 * child process with "running_bg" set to 1.
539 *
540 * The case where the mount point is not present and the "bg"
541 * option is set, is treated as a timeout. This is done to
542 * support nested mounts.
543 *
544 * The "retry" count specified by the user is the number of
545 * minutes to retry before giving up.
546 *
547 * Only the first error message will be displayed.
548 */
549 retry_timeout.tv_sec = 3;
550 retry_timeout.tv_usec = 0;
551 total_timeout.tv_sec = 20;
552 total_timeout.tv_usec = 0;
553 timeout = time(NULL) + 60 * retry;
554 prevt = 0;
555 t = 30;
556 val = 1;
557 for (;;) {
558 if (bg && stat(node, &statbuf) == -1) {
559 if (running_bg) {
560 sleep(val); /* 1, 2, 4, 8, 16, 30, ... */
561 val *= 2;
562 if (val > 30)
563 val = 30;
564 }
565 } else {
566 /* be careful not to use too many CPU cycles */
567 if (t - prevt < 30)
568 sleep(30);
569
570 pm_mnt = get_mountport(&mount_server_addr,
571 mountprog,
572 mountvers,
573 proto,
574 mountport);
575
576 /* contact the mount daemon via TCP */
577 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
578 msock = RPC_ANYSOCK;
579
580 switch (pm_mnt->pm_prot) {
581 case IPPROTO_UDP:
582 mclient = clntudp_create(&mount_server_addr,
583 pm_mnt->pm_prog,
584 pm_mnt->pm_vers,
585 retry_timeout,
586 &msock);
587 if (mclient)
588 break;
589 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
590 msock = RPC_ANYSOCK;
591 case IPPROTO_TCP:
592 mclient = clnttcp_create(&mount_server_addr,
593 pm_mnt->pm_prog,
594 pm_mnt->pm_vers,
595 &msock, 0, 0);
596 break;
597 default:
598 mclient = 0;
599 }
600 if (mclient) {
601 /* try to mount hostname:dirname */
602 mclient->cl_auth = authunix_create_default();
603
604 /* make pointers in xdr_mountres3 NULL so
605 * that xdr_array allocates memory for us
606 */
607 memset(&status, 0, sizeof(status));
608
609 if (pm_mnt->pm_vers == 3)
610 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
611 (xdrproc_t) xdr_dirpath,
612 (caddr_t) &dirname,
613 (xdrproc_t) xdr_mountres3,
614 (caddr_t) &status,
615 total_timeout);
616 else
617 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
618 (xdrproc_t) xdr_dirpath,
619 (caddr_t) &dirname,
620 (xdrproc_t) xdr_fhstatus,
621 (caddr_t) &status,
622 total_timeout);
623
624 if (clnt_stat == RPC_SUCCESS)
625 break; /* we're done */
626 if (errno != ECONNREFUSED) {
627 clnt_perror(mclient, "mount");
628 goto fail; /* don't retry */
629 }
630 if (!running_bg && prevt == 0)
631 clnt_perror(mclient, "mount");
632 auth_destroy(mclient->cl_auth);
633 clnt_destroy(mclient);
634 mclient = 0;
635 close(msock);
636 } else {
637 if (!running_bg && prevt == 0)
638 clnt_pcreateerror("mount");
639 }
640 prevt = t;
641 }
642 if (!bg)
643 goto fail;
644 if (!running_bg) {
645 prev_bg_host = xstrdup(hostname);
646 if (retry > 0)
647 retval = EX_BG;
648 goto fail;
649 }
650 t = time(NULL);
651 if (t >= timeout)
652 goto fail;
653 }
654 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
655
656 if (nfsvers == 2) {
657 if (status.nfsv2.fhs_status != 0) {
658 fprintf(stderr,
659 "mount: %s:%s failed, reason given by server: %s\n",
660 hostname, dirname,
661 nfs_strerror(status.nfsv2.fhs_status));
662 goto fail;
663 }
664 memcpy(data.root.data,
665 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
666 NFS_FHSIZE);
667 #if NFS_MOUNT_VERSION >= 4
668 data.root.size = NFS_FHSIZE;
669 memcpy(data.old_root.data,
670 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
671 NFS_FHSIZE);
672 #endif
673 } else {
674 #if NFS_MOUNT_VERSION >= 4
675 fhandle3 *fhandle;
676 if (status.nfsv3.fhs_status != 0) {
677 fprintf(stderr,
678 "mount: %s:%s failed, reason given by server: %s\n",
679 hostname, dirname,
680 nfs_strerror(status.nfsv3.fhs_status));
681 goto fail;
682 }
683 fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
684 memset(data.old_root.data, 0, NFS_FHSIZE);
685 memset(&data.root, 0, sizeof(data.root));
686 data.root.size = fhandle->fhandle3_len;
687 memcpy(data.root.data,
688 (char *) fhandle->fhandle3_val,
689 fhandle->fhandle3_len);
690
691 data.flags |= NFS_MOUNT_VER3;
692 #endif
693 }
694
695 /* create nfs socket for kernel */
696
697 if (tcp) {
698 if (nfs_mount_version < 3) {
699 printf(_("NFS over TCP is not supported.\n"));
700 goto fail;
701 }
702 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
703 } else
704 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
705 if (fsock < 0) {
706 perror(_("nfs socket"));
707 goto fail;
708 }
709 if (bindresvport(fsock, 0) < 0) {
710 perror(_("nfs bindresvport"));
711 goto fail;
712 }
713 if (port == 0) {
714 server_addr.sin_port = PMAPPORT;
715 port = pmap_getport(&server_addr, nfsprog, nfsvers,
716 tcp ? IPPROTO_TCP : IPPROTO_UDP);
717 if (port == 0)
718 port = NFS_PORT;
719 #ifdef NFS_MOUNT_DEBUG
720 else
721 printf(_("used portmapper to find NFS port\n"));
722 #endif
723 }
724 #ifdef NFS_MOUNT_DEBUG
725 printf(_("using port %d for nfs deamon\n"), port);
726 #endif
727 server_addr.sin_port = htons(port);
728 /*
729 * connect() the socket for kernels 1.3.10 and below only,
730 * to avoid problems with multihomed hosts.
731 * --Swen
732 */
733 if (linux_version_code() <= 66314
734 && connect(fsock, (struct sockaddr *) &server_addr,
735 sizeof (server_addr)) < 0) {
736 perror(_("nfs connect"));
737 goto fail;
738 }
739
740 /* prepare data structure for kernel */
741
742 data.fd = fsock;
743 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
744 strncpy(data.hostname, hostname, sizeof(data.hostname));
745
746 /* clean up */
747
748 auth_destroy(mclient->cl_auth);
749 clnt_destroy(mclient);
750 close(msock);
751 return 0;
752
753 /* abort */
754
755 fail:
756 if (msock != -1) {
757 if (mclient) {
758 auth_destroy(mclient->cl_auth);
759 clnt_destroy(mclient);
760 }
761 close(msock);
762 }
763 if (fsock != -1)
764 close(fsock);
765 return retval;
766 }
767
768 /*
769 * We need to translate between nfs status return values and
770 * the local errno values which may not be the same.
771 *
772 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
773 * "after #include <errno.h> the symbol errno is reserved for any use,
774 * it cannot even be used as a struct tag or field name".
775 */
776
777 #ifndef EDQUOT
778 #define EDQUOT ENOSPC
779 #endif
780
781 static struct {
782 enum nfs_stat stat;
783 int errnum;
784 } nfs_errtbl[] = {
785 { NFS_OK, 0 },
786 { NFSERR_PERM, EPERM },
787 { NFSERR_NOENT, ENOENT },
788 { NFSERR_IO, EIO },
789 { NFSERR_NXIO, ENXIO },
790 { NFSERR_ACCES, EACCES },
791 { NFSERR_EXIST, EEXIST },
792 { NFSERR_NODEV, ENODEV },
793 { NFSERR_NOTDIR, ENOTDIR },
794 { NFSERR_ISDIR, EISDIR },
795 #ifdef NFSERR_INVAL
796 { NFSERR_INVAL, EINVAL }, /* that Sun forgot */
797 #endif
798 { NFSERR_FBIG, EFBIG },
799 { NFSERR_NOSPC, ENOSPC },
800 { NFSERR_ROFS, EROFS },
801 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
802 { NFSERR_NOTEMPTY, ENOTEMPTY },
803 { NFSERR_DQUOT, EDQUOT },
804 { NFSERR_STALE, ESTALE },
805 #ifdef EWFLUSH
806 { NFSERR_WFLUSH, EWFLUSH },
807 #endif
808 /* Throw in some NFSv3 values for even more fun (HP returns these) */
809 { 71, EREMOTE },
810
811 { -1, EIO }
812 };
813
814 static char *nfs_strerror(int stat)
815 {
816 int i;
817 static char buf[256];
818
819 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
820 if (nfs_errtbl[i].stat == stat)
821 return strerror(nfs_errtbl[i].errnum);
822 }
823 sprintf(buf, _("unknown nfs status return value: %d"), stat);
824 return buf;
825 }
826
827 #if 0
828 int
829 my_getport(struct in_addr server, struct timeval *timeo, ...)
830 {
831 struct sockaddr_in sin;
832 struct pmap pmap;
833 CLIENT *clnt;
834 int sock = RPC_ANYSOCK, port;
835
836 pmap.pm_prog = prog;
837 pmap.pm_vers = vers;
838 pmap.pm_prot = prot;
839 pmap.pm_port = 0;
840 sin.sin_family = AF_INET;
841 sin.sin_addr = server;
842 sin.sin_port = htons(111);
843 clnt = clntudp_create(&sin, 100000, 2, *timeo, &sock);
844 status = clnt_call(clnt, PMAP_GETPORT,
845 &pmap, (xdrproc_t) xdr_pmap,
846 &port, (xdrproc_t) xdr_uint);
847 if (status != SUCCESS) {
848 /* natter */
849 port = 0;
850 }
851
852 clnt_destroy(clnt);
853 close(sock);
854 return port;
855 }
856 #endif