blob: 8cdfebfce6d80bc4f1044ce121c4e10a5017d03d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen1c43d0c1999-11-18 07:58:07 +00002/*
3 * nfsmount.c -- Linux NFS mount
4 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
17 * numbers to be specified on the command line.
18 *
19 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
20 * Omit the call to connect() for Linux version 1.3.11 or later.
21 *
22 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
23 * Implemented the "bg", "fg" and "retry" mount options for NFS.
24 *
25 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
26 * - added Native Language Support
27 *
Eric Andersenda1d1e72000-07-10 23:39:44 +000028 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
29 * plus NFSv3 stuff.
Eric Andersen1c43d0c1999-11-18 07:58:07 +000030 */
31
32/*
33 * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
34 */
35
36#include <unistd.h>
37#include <stdio.h>
38#include <string.h>
39#include <errno.h>
40#include <netdb.h>
41#include <rpc/rpc.h>
42#include <rpc/pmap_prot.h>
43#include <rpc/pmap_clnt.h>
44#include <sys/socket.h>
45#include <sys/time.h>
46#include <sys/utsname.h>
47#include <sys/stat.h>
48#include <netinet/in.h>
49#include <arpa/inet.h>
50
51#include "nfsmount.h"
Eric Andersenda1d1e72000-07-10 23:39:44 +000052#include <linux/nfs.h> /* For the kernels nfs stuff */
Eric Andersen1c43d0c1999-11-18 07:58:07 +000053
Eric Andersen1c43d0c1999-11-18 07:58:07 +000054
Eric Andersenda1d1e72000-07-10 23:39:44 +000055/* Disable the nls stuff */
56# undef bindtextdomain
57# define bindtextdomain(Domain, Directory) /* empty */
58# undef textdomain
59# define textdomain(Domain) /* empty */
60# define _(Text) (Text)
61# define N_(Text) (Text)
62
63#define MS_MGC_VAL 0xc0ed0000 /* Magic number indicatng "new" flags */
64#define MS_RDONLY 1 /* Mount read-only */
65#define MS_NOSUID 2 /* Ignore suid and sgid bits */
66#define MS_NODEV 4 /* Disallow access to device special files */
67#define MS_NOEXEC 8 /* Disallow program execution */
68#define MS_SYNCHRONOUS 16 /* Writes are synced at once */
69#define MS_REMOUNT 32 /* Alter flags of a mounted FS */
70#define MS_MANDLOCK 64 /* Allow mandatory locks on an FS */
71#define S_QUOTA 128 /* Quota initialized for file/directory/symlink */
72#define S_APPEND 256 /* Append-only file */
73#define S_IMMUTABLE 512 /* Immutable file */
74#define MS_NOATIME 1024 /* Do not update access times. */
75#define MS_NODIRATIME 2048 /* Do not update directory access times */
76
77
78/*
79 * We want to be able to compile mount on old kernels in such a way
80 * that the binary will work well on more recent kernels.
81 * Thus, if necessary we teach nfsmount.c the structure of new fields
82 * that will come later.
83 *
84 * Moreover, the new kernel includes conflict with glibc includes
85 * so it is easiest to ignore the kernel altogether (at compile time).
86 */
87
88#define NFS_MOUNT_VERSION 4
89
90struct nfs2_fh {
91 char data[32];
92};
93struct nfs3_fh {
94 unsigned short size;
95 unsigned char data[64];
96};
97
98struct nfs_mount_data {
99 int version; /* 1 */
100 int fd; /* 1 */
101 struct nfs2_fh old_root; /* 1 */
102 int flags; /* 1 */
103 int rsize; /* 1 */
104 int wsize; /* 1 */
105 int timeo; /* 1 */
106 int retrans; /* 1 */
107 int acregmin; /* 1 */
108 int acregmax; /* 1 */
109 int acdirmin; /* 1 */
110 int acdirmax; /* 1 */
111 struct sockaddr_in addr; /* 1 */
112 char hostname[256]; /* 1 */
113 int namlen; /* 2 */
114 unsigned int bsize; /* 3 */
115 struct nfs3_fh root; /* 4 */
116};
117
118/* bits in the flags field */
119
120#define NFS_MOUNT_SOFT 0x0001 /* 1 */
121#define NFS_MOUNT_INTR 0x0002 /* 1 */
122#define NFS_MOUNT_SECURE 0x0004 /* 1 */
123#define NFS_MOUNT_POSIX 0x0008 /* 1 */
124#define NFS_MOUNT_NOCTO 0x0010 /* 1 */
125#define NFS_MOUNT_NOAC 0x0020 /* 1 */
126#define NFS_MOUNT_TCP 0x0040 /* 2 */
127#define NFS_MOUNT_VER3 0x0080 /* 3 */
128#define NFS_MOUNT_KERBEROS 0x0100 /* 3 */
129#define NFS_MOUNT_NONLM 0x0200 /* 3 */
130
131
132#define UTIL_LINUX_VERSION "2.10m"
133#define util_linux_version "util-linux-2.10m"
134
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000135#define HAVE_inet_aton
Eric Andersenda1d1e72000-07-10 23:39:44 +0000136#define HAVE_scsi_h
137#define HAVE_blkpg_h
138#define HAVE_kd_h
139#define HAVE_termcap
140#define HAVE_locale_h
141#define HAVE_libintl_h
142#define ENABLE_NLS
143#define HAVE_langinfo_h
144#define HAVE_progname
145#define HAVE_openpty
146#define HAVE_nanosleep
147#define HAVE_personality
148#define HAVE_tm_gmtoff
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000149
Eric Andersenda1d1e72000-07-10 23:39:44 +0000150extern char *xstrdup (const char *s);
151extern char *xstrndup (const char *s, int n);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000152static char *nfs_strerror(int stat);
153
154#define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
Eric Andersenda1d1e72000-07-10 23:39:44 +0000155#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000156
Eric Andersenda1d1e72000-07-10 23:39:44 +0000157#define EX_FAIL 32 /* mount failure */
158#define EX_BG 256 /* retry in background (internal only) */
159
160
161static int
162linux_version_code(void) {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000163 struct utsname my_utsname;
164 int p, q, r;
165
166 if (uname(&my_utsname) == 0) {
167 p = atoi(strtok(my_utsname.release, "."));
168 q = atoi(strtok(NULL, "."));
169 r = atoi(strtok(NULL, "."));
Eric Andersenda1d1e72000-07-10 23:39:44 +0000170 return MAKE_VERSION(p,q,r);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000171 }
172 return 0;
173}
174
175/*
Eric Andersenda1d1e72000-07-10 23:39:44 +0000176 * nfs_mount_version according to the sources seen at compile time.
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000177 */
Eric Andersenda1d1e72000-07-10 23:39:44 +0000178int nfs_mount_version = NFS_MOUNT_VERSION;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000179
180/*
181 * Unfortunately, the kernel prints annoying console messages
182 * in case of an unexpected nfs mount version (instead of
183 * just returning some error). Therefore we'll have to try
184 * and figure out what version the kernel expects.
185 *
186 * Variables:
187 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
188 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
189 * nfs_mount_version: version this source and running kernel can handle
190 */
Eric Andersenda1d1e72000-07-10 23:39:44 +0000191static void
192find_kernel_nfs_mount_version(void) {
193 static int kernel_version = 0;
194
195 if (kernel_version)
196 return;
197
198 kernel_version = linux_version_code();
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000199
200 if (kernel_version) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000201 if (kernel_version < MAKE_VERSION(2,1,32))
202 nfs_mount_version = 1;
203 else if (kernel_version < MAKE_VERSION(2,3,99))
204 nfs_mount_version = 3;
205 else
206 nfs_mount_version = 4; /* since 2.3.99pre4 */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000207 }
208 if (nfs_mount_version > NFS_MOUNT_VERSION)
Eric Andersenda1d1e72000-07-10 23:39:44 +0000209 nfs_mount_version = NFS_MOUNT_VERSION;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000210}
211
Eric Andersenda1d1e72000-07-10 23:39:44 +0000212static struct pmap *
213get_mountport(struct sockaddr_in *server_addr,
214 long unsigned prog,
215 long unsigned version,
216 long unsigned proto,
217 long unsigned port)
218{
219struct pmaplist *pmap;
220static struct pmap p = {0, 0, 0, 0};
221
222server_addr->sin_port = PMAPPORT;
223pmap = pmap_getmaps(server_addr);
224
225if (version > MAX_NFSPROT)
226 version = MAX_NFSPROT;
227if (!prog)
228 prog = MOUNTPROG;
229p.pm_prog = prog;
230p.pm_vers = version;
231p.pm_prot = proto;
232p.pm_port = port;
233
234while (pmap) {
235 if (pmap->pml_map.pm_prog != prog)
236 goto next;
237 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
238 goto next;
239 if (version > 2 && pmap->pml_map.pm_vers != version)
240 goto next;
241 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
242 goto next;
243 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
244 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
245 (port && pmap->pml_map.pm_port != port))
246 goto next;
247 memcpy(&p, &pmap->pml_map, sizeof(p));
248next:
249 pmap = pmap->pml_next;
250}
251if (!p.pm_vers)
252 p.pm_vers = MOUNTVERS;
253if (!p.pm_port)
254 p.pm_port = MOUNTPORT;
255if (!p.pm_prot)
256 p.pm_prot = IPPROTO_TCP;
257return &p;
258}
259
260int nfsmount(const char *spec, const char *node, int *flags,
261 char **extra_opts, char **mount_opts, int running_bg)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000262{
263 static char *prev_bg_host;
264 char hostdir[1024];
265 CLIENT *mclient;
266 char *hostname;
267 char *dirname;
268 char *old_opts;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000269 char *mounthost=NULL;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000270 char new_opts[1024];
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000271 struct timeval total_timeout;
272 enum clnt_stat clnt_stat;
273 static struct nfs_mount_data data;
274 char *opt, *opteq;
275 int val;
276 struct hostent *hp;
277 struct sockaddr_in server_addr;
278 struct sockaddr_in mount_server_addr;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000279 struct pmap* pm_mnt;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000280 int msock, fsock;
281 struct timeval retry_timeout;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000282 union {
283 struct fhstatus nfsv2;
284 struct mountres3 nfsv3;
285 } status;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000286 struct stat statbuf;
287 char *s;
288 int port;
289 int mountport;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000290 int proto;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000291 int bg;
292 int soft;
293 int intr;
294 int posix;
295 int nocto;
296 int noac;
297 int nolock;
298 int retry;
299 int tcp;
300 int mountprog;
301 int mountvers;
302 int nfsprog;
303 int nfsvers;
304 int retval;
305 time_t t;
306 time_t prevt;
307 time_t timeout;
308
309 find_kernel_nfs_mount_version();
310
311 retval = EX_FAIL;
312 msock = fsock = -1;
313 mclient = NULL;
314 if (strlen(spec) >= sizeof(hostdir)) {
315 fprintf(stderr, _("mount: "
Eric Andersenda1d1e72000-07-10 23:39:44 +0000316 "excessively long host:dir argument\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000317 goto fail;
318 }
319 strcpy(hostdir, spec);
320 if ((s = strchr(hostdir, ':'))) {
321 hostname = hostdir;
322 dirname = s + 1;
323 *s = '\0';
324 /* Ignore all but first hostname in replicated mounts
325 until they can be fully supported. (mack@sgi.com) */
326 if ((s = strchr(hostdir, ','))) {
327 *s = '\0';
328 fprintf(stderr, _("mount: warning: "
Eric Andersenda1d1e72000-07-10 23:39:44 +0000329 "multiple hostnames not supported\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000330 }
331 } else {
332 fprintf(stderr, _("mount: "
Eric Andersenda1d1e72000-07-10 23:39:44 +0000333 "directory to mount not in host:dir format\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000334 goto fail;
335 }
336
337 server_addr.sin_family = AF_INET;
338#ifdef HAVE_inet_aton
339 if (!inet_aton(hostname, &server_addr.sin_addr))
340#endif
341 {
342 if ((hp = gethostbyname(hostname)) == NULL) {
343 fprintf(stderr, _("mount: can't get address for %s\n"),
Eric Andersenda1d1e72000-07-10 23:39:44 +0000344 hostname);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000345 goto fail;
346 } else {
347 if (hp->h_length > sizeof(struct in_addr)) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000348 fprintf(stderr,
349 _("mount: got bad hp->h_length\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000350 hp->h_length = sizeof(struct in_addr);
351 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000352 memcpy(&server_addr.sin_addr,
353 hp->h_addr, hp->h_length);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000354 }
355 }
356
Eric Andersenda1d1e72000-07-10 23:39:44 +0000357 memcpy (&mount_server_addr, &server_addr, sizeof (mount_server_addr));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000358
359 /* add IP address to mtab options for use when unmounting */
360
361 s = inet_ntoa(server_addr.sin_addr);
362 old_opts = *extra_opts;
363 if (!old_opts)
364 old_opts = "";
365 if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000366 fprintf(stderr, _("mount: "
367 "excessively long option argument\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000368 goto fail;
369 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000370 sprintf(new_opts, "%s%saddr=%s",
371 old_opts, *old_opts ? "," : "", s);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000372 *extra_opts = xstrdup(new_opts);
373
374 /* Set default options.
375 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
376 * let the kernel decide.
377 * timeo is filled in after we know whether it'll be TCP or UDP. */
378 memset(&data, 0, sizeof(data));
Eric Andersenda1d1e72000-07-10 23:39:44 +0000379 data.retrans = 3;
380 data.acregmin = 3;
381 data.acregmax = 60;
382 data.acdirmin = 30;
383 data.acdirmax = 60;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000384#if NFS_MOUNT_VERSION >= 2
Eric Andersenda1d1e72000-07-10 23:39:44 +0000385 data.namlen = NAME_MAX;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000386#endif
387
388 bg = 0;
389 soft = 0;
390 intr = 0;
391 posix = 0;
392 nocto = 0;
393 nolock = 0;
394 noac = 0;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000395 retry = 10000; /* 10000 minutes ~ 1 week */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000396 tcp = 0;
397
398 mountprog = MOUNTPROG;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000399 mountvers = 0;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000400 port = 0;
401 mountport = 0;
402 nfsprog = NFS_PROGRAM;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000403 nfsvers = 0;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000404
405 /* parse options */
406
407 for (opt = strtok(old_opts, ","); opt; opt = strtok(NULL, ",")) {
408 if ((opteq = strchr(opt, '='))) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000409 val = atoi(opteq + 1);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000410 *opteq = '\0';
411 if (!strcmp(opt, "rsize"))
412 data.rsize = val;
413 else if (!strcmp(opt, "wsize"))
414 data.wsize = val;
415 else if (!strcmp(opt, "timeo"))
416 data.timeo = val;
417 else if (!strcmp(opt, "retrans"))
418 data.retrans = val;
419 else if (!strcmp(opt, "acregmin"))
420 data.acregmin = val;
421 else if (!strcmp(opt, "acregmax"))
422 data.acregmax = val;
423 else if (!strcmp(opt, "acdirmin"))
424 data.acdirmin = val;
425 else if (!strcmp(opt, "acdirmax"))
426 data.acdirmax = val;
427 else if (!strcmp(opt, "actimeo")) {
428 data.acregmin = val;
429 data.acregmax = val;
430 data.acdirmin = val;
431 data.acdirmax = val;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000432 }
433 else if (!strcmp(opt, "retry"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000434 retry = val;
435 else if (!strcmp(opt, "port"))
436 port = val;
437 else if (!strcmp(opt, "mountport"))
Eric Andersenda1d1e72000-07-10 23:39:44 +0000438 mountport = val;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000439 else if (!strcmp(opt, "mounthost"))
Eric Andersenda1d1e72000-07-10 23:39:44 +0000440 mounthost=xstrndup(opteq+1,
441 strcspn(opteq+1," \t\n\r,"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000442 else if (!strcmp(opt, "mountprog"))
443 mountprog = val;
444 else if (!strcmp(opt, "mountvers"))
445 mountvers = val;
446 else if (!strcmp(opt, "nfsprog"))
447 nfsprog = val;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000448 else if (!strcmp(opt, "nfsvers") ||
449 !strcmp(opt, "vers"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000450 nfsvers = val;
451 else if (!strcmp(opt, "proto")) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000452 if (!strncmp(opteq+1, "tcp", 3))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000453 tcp = 1;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000454 else if (!strncmp(opteq+1, "udp", 3))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000455 tcp = 0;
456 else
457 printf(_("Warning: Unrecognized proto= option.\n"));
458 } else if (!strcmp(opt, "namlen")) {
459#if NFS_MOUNT_VERSION >= 2
460 if (nfs_mount_version >= 2)
461 data.namlen = val;
462 else
463#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +0000464 printf(_("Warning: Option namlen is not supported.\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000465 } else if (!strcmp(opt, "addr"))
Eric Andersenda1d1e72000-07-10 23:39:44 +0000466 /* ignore */;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000467 else {
468 printf(_("unknown nfs mount parameter: "
Eric Andersenda1d1e72000-07-10 23:39:44 +0000469 "%s=%d\n"), opt, val);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000470 goto fail;
471 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000472 }
473 else {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000474 val = 1;
475 if (!strncmp(opt, "no", 2)) {
476 val = 0;
477 opt += 2;
478 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000479 if (!strcmp(opt, "bg"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000480 bg = val;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000481 else if (!strcmp(opt, "fg"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000482 bg = !val;
483 else if (!strcmp(opt, "soft"))
484 soft = val;
485 else if (!strcmp(opt, "hard"))
486 soft = !val;
487 else if (!strcmp(opt, "intr"))
488 intr = val;
489 else if (!strcmp(opt, "posix"))
490 posix = val;
491 else if (!strcmp(opt, "cto"))
492 nocto = !val;
493 else if (!strcmp(opt, "ac"))
494 noac = !val;
495 else if (!strcmp(opt, "tcp"))
496 tcp = val;
497 else if (!strcmp(opt, "udp"))
498 tcp = !val;
499 else if (!strcmp(opt, "lock")) {
500 if (nfs_mount_version >= 3)
501 nolock = !val;
502 else
Eric Andersenda1d1e72000-07-10 23:39:44 +0000503 printf(_("Warning: option nolock is not supported.\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000504 } else {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000505 printf(_("unknown nfs mount option: "
506 "%s%s\n"), val ? "" : "no", opt);
507 goto fail;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000508 }
509 }
510 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000511 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
512
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000513 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
514 | (intr ? NFS_MOUNT_INTR : 0)
515 | (posix ? NFS_MOUNT_POSIX : 0)
516 | (nocto ? NFS_MOUNT_NOCTO : 0)
517 | (noac ? NFS_MOUNT_NOAC : 0);
518#if NFS_MOUNT_VERSION >= 2
519 if (nfs_mount_version >= 2)
520 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
521#endif
522#if NFS_MOUNT_VERSION >= 3
523 if (nfs_mount_version >= 3)
524 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
525#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +0000526 if (nfsvers > MAX_NFSPROT) {
527 fprintf(stderr, "NFSv%d not supported!\n", nfsvers);
528 return 0;
529 }
530 if (mountvers > MAX_NFSPROT) {
531 fprintf(stderr, "NFSv%d not supported!\n", nfsvers);
532 return 0;
533 }
534 if (nfsvers && !mountvers)
535 mountvers = (nfsvers < 3) ? 1 : nfsvers;
536 if (nfsvers && nfsvers < mountvers) {
537 mountvers = nfsvers;
538 }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000539
540 /* Adjust options if none specified */
541 if (!data.timeo)
542 data.timeo = tcp ? 70 : 7;
543
544#ifdef NFS_MOUNT_DEBUG
545 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000546 data.rsize, data.wsize, data.timeo, data.retrans);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000547 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000548 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000549 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000550 port, bg, retry, data.flags);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000551 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000552 mountprog, mountvers, nfsprog, nfsvers);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000553 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000554 (data.flags & NFS_MOUNT_SOFT) != 0,
555 (data.flags & NFS_MOUNT_INTR) != 0,
556 (data.flags & NFS_MOUNT_POSIX) != 0,
557 (data.flags & NFS_MOUNT_NOCTO) != 0,
558 (data.flags & NFS_MOUNT_NOAC) != 0);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000559#if NFS_MOUNT_VERSION >= 2
Eric Andersenda1d1e72000-07-10 23:39:44 +0000560 printf("tcp = %d\n",
561 (data.flags & NFS_MOUNT_TCP) != 0);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000562#endif
563#endif
564
565 data.version = nfs_mount_version;
566 *mount_opts = (char *) &data;
567
568 if (*flags & MS_REMOUNT)
569 return 0;
570
571 /*
572 * If the previous mount operation on the same host was
573 * backgrounded, and the "bg" for this mount is also set,
574 * give up immediately, to avoid the initial timeout.
575 */
576 if (bg && !running_bg &&
Eric Andersenda1d1e72000-07-10 23:39:44 +0000577 prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000578 if (retry > 0)
579 retval = EX_BG;
580 return retval;
581 }
582
583 /* create mount deamon client */
584 /* See if the nfs host = mount host. */
585 if (mounthost) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000586 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
587 mount_server_addr.sin_family = AF_INET;
588 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
589 } else {
590 if ((hp = gethostbyname(mounthost)) == NULL) {
591 fprintf(stderr, _("mount: can't get address for %s\n"),
592 hostname);
593 goto fail;
594 } else {
595 if (hp->h_length > sizeof(struct in_addr)) {
596 fprintf(stderr,
597 _("mount: got bad hp->h_length?\n"));
598 hp->h_length = sizeof(struct in_addr);
599 }
600 mount_server_addr.sin_family = AF_INET;
601 memcpy(&mount_server_addr.sin_addr,
602 hp->h_addr, hp->h_length);
603 }
604 }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000605 }
606
607 /*
608 * The following loop implements the mount retries. On the first
609 * call, "running_bg" is 0. When the mount times out, and the
610 * "bg" option is set, the exit status EX_BG will be returned.
611 * For a backgrounded mount, there will be a second call by the
612 * child process with "running_bg" set to 1.
613 *
614 * The case where the mount point is not present and the "bg"
615 * option is set, is treated as a timeout. This is done to
616 * support nested mounts.
617 *
618 * The "retry" count specified by the user is the number of
619 * minutes to retry before giving up.
620 *
621 * Only the first error message will be displayed.
622 */
623 retry_timeout.tv_sec = 3;
624 retry_timeout.tv_usec = 0;
625 total_timeout.tv_sec = 20;
626 total_timeout.tv_usec = 0;
627 timeout = time(NULL) + 60 * retry;
628 prevt = 0;
629 t = 30;
630 val = 1;
631 for (;;) {
632 if (bg && stat(node, &statbuf) == -1) {
633 if (running_bg) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000634 sleep(val); /* 1, 2, 4, 8, 16, 30, ... */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000635 val *= 2;
636 if (val > 30)
637 val = 30;
638 }
639 } else {
640 /* be careful not to use too many CPU cycles */
641 if (t - prevt < 30)
642 sleep(30);
643
Eric Andersenda1d1e72000-07-10 23:39:44 +0000644 pm_mnt = get_mountport(&mount_server_addr,
645 mountprog,
646 mountvers,
647 proto,
648 mountport);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000649
Eric Andersenda1d1e72000-07-10 23:39:44 +0000650 /* contact the mount daemon via TCP */
651 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
652 msock = RPC_ANYSOCK;
653
654 switch (pm_mnt->pm_prot) {
655 case IPPROTO_UDP:
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000656 mclient = clntudp_create(&mount_server_addr,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000657 pm_mnt->pm_prog,
658 pm_mnt->pm_vers,
659 retry_timeout,
660 &msock);
661 if (mclient)
662 break;
663 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
664 msock = RPC_ANYSOCK;
665 case IPPROTO_TCP:
666 mclient = clnttcp_create(&mount_server_addr,
667 pm_mnt->pm_prog,
668 pm_mnt->pm_vers,
669 &msock, 0, 0);
670 break;
671 default:
672 mclient = 0;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000673 }
674 if (mclient) {
675 /* try to mount hostname:dirname */
676 mclient->cl_auth = authunix_create_default();
Eric Andersenda1d1e72000-07-10 23:39:44 +0000677
678 /* make pointers in xdr_mountres3 NULL so
679 * that xdr_array allocates memory for us
680 */
681 memset(&status, 0, sizeof(status));
682
683 if (pm_mnt->pm_vers == 3)
684 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
685 (xdrproc_t) xdr_dirpath,
686 (caddr_t) &dirname,
687 (xdrproc_t) xdr_mountres3,
688 (caddr_t) &status,
689 total_timeout);
690 else
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000691 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000692 (xdrproc_t) xdr_dirpath,
693 (caddr_t) &dirname,
694 (xdrproc_t) xdr_fhstatus,
695 (caddr_t) &status,
696 total_timeout);
697
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000698 if (clnt_stat == RPC_SUCCESS)
699 break; /* we're done */
700 if (errno != ECONNREFUSED) {
701 clnt_perror(mclient, "mount");
702 goto fail; /* don't retry */
703 }
704 if (!running_bg && prevt == 0)
705 clnt_perror(mclient, "mount");
706 auth_destroy(mclient->cl_auth);
707 clnt_destroy(mclient);
708 mclient = 0;
709 close(msock);
710 } else {
711 if (!running_bg && prevt == 0)
712 clnt_pcreateerror("mount");
713 }
714 prevt = t;
715 }
716 if (!bg)
Eric Andersenda1d1e72000-07-10 23:39:44 +0000717 goto fail;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000718 if (!running_bg) {
719 prev_bg_host = xstrdup(hostname);
720 if (retry > 0)
721 retval = EX_BG;
722 goto fail;
723 }
724 t = time(NULL);
725 if (t >= timeout)
726 goto fail;
727 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000728 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000729
Eric Andersenda1d1e72000-07-10 23:39:44 +0000730 if (nfsvers == 2) {
731 if (status.nfsv2.fhs_status != 0) {
732 fprintf(stderr,
733 "mount: %s:%s failed, reason given by server: %s\n",
734 hostname, dirname,
735 nfs_strerror(status.nfsv2.fhs_status));
736 goto fail;
737 }
738 memcpy(data.root.data,
739 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
740 NFS_FHSIZE);
741#if NFS_MOUNT_VERSION >= 4
742 data.root.size = NFS_FHSIZE;
743 memcpy(data.old_root.data,
744 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
745 NFS_FHSIZE);
746#endif
747 } else {
748#if NFS_MOUNT_VERSION >= 4
749 fhandle3 *fhandle;
750 if (status.nfsv3.fhs_status != 0) {
751 fprintf(stderr,
752 "mount: %s:%s failed, reason given by server: %s\n",
753 hostname, dirname,
754 nfs_strerror(status.nfsv3.fhs_status));
755 goto fail;
756 }
757 fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
758 memset(data.old_root.data, 0, NFS_FHSIZE);
759 memset(&data.root, 0, sizeof(data.root));
760 data.root.size = fhandle->fhandle3_len;
761 memcpy(data.root.data,
762 (char *) fhandle->fhandle3_val,
763 fhandle->fhandle3_len);
764
765 data.flags |= NFS_MOUNT_VER3;
766#endif
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000767 }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000768
769 /* create nfs socket for kernel */
770
771 if (tcp) {
772 if (nfs_mount_version < 3) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000773 printf(_("NFS over TCP is not supported.\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000774 goto fail;
775 }
776 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
777 } else
778 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
779 if (fsock < 0) {
780 perror(_("nfs socket"));
781 goto fail;
782 }
783 if (bindresvport(fsock, 0) < 0) {
784 perror(_("nfs bindresvport"));
785 goto fail;
786 }
787 if (port == 0) {
788 server_addr.sin_port = PMAPPORT;
789 port = pmap_getport(&server_addr, nfsprog, nfsvers,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000790 tcp ? IPPROTO_TCP : IPPROTO_UDP);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000791 if (port == 0)
792 port = NFS_PORT;
793#ifdef NFS_MOUNT_DEBUG
794 else
795 printf(_("used portmapper to find NFS port\n"));
796#endif
797 }
798#ifdef NFS_MOUNT_DEBUG
799 printf(_("using port %d for nfs deamon\n"), port);
800#endif
801 server_addr.sin_port = htons(port);
Eric Andersenda1d1e72000-07-10 23:39:44 +0000802 /*
803 * connect() the socket for kernels 1.3.10 and below only,
804 * to avoid problems with multihomed hosts.
805 * --Swen
806 */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000807 if (linux_version_code() <= 66314
Eric Andersenda1d1e72000-07-10 23:39:44 +0000808 && connect(fsock, (struct sockaddr *) &server_addr,
809 sizeof (server_addr)) < 0) {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000810 perror(_("nfs connect"));
811 goto fail;
812 }
813
814 /* prepare data structure for kernel */
815
816 data.fd = fsock;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000817 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
818 strncpy(data.hostname, hostname, sizeof(data.hostname));
819
820 /* clean up */
821
822 auth_destroy(mclient->cl_auth);
823 clnt_destroy(mclient);
824 close(msock);
825 return 0;
826
827 /* abort */
828
Eric Andersenda1d1e72000-07-10 23:39:44 +0000829fail:
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000830 if (msock != -1) {
831 if (mclient) {
832 auth_destroy(mclient->cl_auth);
833 clnt_destroy(mclient);
834 }
835 close(msock);
836 }
837 if (fsock != -1)
838 close(fsock);
839 return retval;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000840}
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000841
842/*
843 * We need to translate between nfs status return values and
844 * the local errno values which may not be the same.
845 *
846 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
847 * "after #include <errno.h> the symbol errno is reserved for any use,
848 * it cannot even be used as a struct tag or field name".
849 */
850
851#ifndef EDQUOT
852#define EDQUOT ENOSPC
853#endif
854
855static struct {
856 enum nfs_stat stat;
857 int errnum;
858} nfs_errtbl[] = {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000859 { NFS_OK, 0 },
860 { NFSERR_PERM, EPERM },
861 { NFSERR_NOENT, ENOENT },
862 { NFSERR_IO, EIO },
863 { NFSERR_NXIO, ENXIO },
864 { NFSERR_ACCES, EACCES },
865 { NFSERR_EXIST, EEXIST },
866 { NFSERR_NODEV, ENODEV },
867 { NFSERR_NOTDIR, ENOTDIR },
868 { NFSERR_ISDIR, EISDIR },
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000869#ifdef NFSERR_INVAL
Eric Andersenda1d1e72000-07-10 23:39:44 +0000870 { NFSERR_INVAL, EINVAL }, /* that Sun forgot */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000871#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +0000872 { NFSERR_FBIG, EFBIG },
873 { NFSERR_NOSPC, ENOSPC },
874 { NFSERR_ROFS, EROFS },
875 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
876 { NFSERR_NOTEMPTY, ENOTEMPTY },
877 { NFSERR_DQUOT, EDQUOT },
878 { NFSERR_STALE, ESTALE },
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000879#ifdef EWFLUSH
Eric Andersenda1d1e72000-07-10 23:39:44 +0000880 { NFSERR_WFLUSH, EWFLUSH },
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000881#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +0000882 /* Throw in some NFSv3 values for even more fun (HP returns these) */
883 { 71, EREMOTE },
884
885 { -1, EIO }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000886};
887
888static char *nfs_strerror(int stat)
889{
890 int i;
891 static char buf[256];
892
893 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
894 if (nfs_errtbl[i].stat == stat)
895 return strerror(nfs_errtbl[i].errnum);
896 }
897 sprintf(buf, _("unknown nfs status return value: %d"), stat);
898 return buf;
899}
900
Eric Andersenda1d1e72000-07-10 23:39:44 +0000901bool_t
902xdr_fhandle (XDR *xdrs, fhandle objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000903{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000904 //register int32_t *buf;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000905
Eric Andersenda1d1e72000-07-10 23:39:44 +0000906 if (!xdr_opaque (xdrs, objp, FHSIZE))
907 return FALSE;
908 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000909}
910
Eric Andersenda1d1e72000-07-10 23:39:44 +0000911bool_t
912xdr_fhstatus (XDR *xdrs, fhstatus *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000913{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000914 //register int32_t *buf;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000915
Eric Andersenda1d1e72000-07-10 23:39:44 +0000916 if (!xdr_u_int (xdrs, &objp->fhs_status))
917 return FALSE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000918 switch (objp->fhs_status) {
919 case 0:
Eric Andersenda1d1e72000-07-10 23:39:44 +0000920 if (!xdr_fhandle (xdrs, objp->fhstatus_u.fhs_fhandle))
921 return FALSE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000922 break;
923 default:
924 break;
925 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000926 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000927}
928
Eric Andersenda1d1e72000-07-10 23:39:44 +0000929bool_t
930xdr_dirpath (XDR *xdrs, dirpath *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000931{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000932 //register int32_t *buf;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000933
Eric Andersenda1d1e72000-07-10 23:39:44 +0000934 if (!xdr_string (xdrs, objp, MNTPATHLEN))
935 return FALSE;
936 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000937}
938
Eric Andersenda1d1e72000-07-10 23:39:44 +0000939bool_t
940xdr_fhandle3 (XDR *xdrs, fhandle3 *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000941{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000942 //register int32_t *buf;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000943
Eric Andersenda1d1e72000-07-10 23:39:44 +0000944 if (!xdr_bytes (xdrs, (char **)&objp->fhandle3_val, (u_int *) &objp->fhandle3_len, FHSIZE3))
945 return FALSE;
946 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000947}
948
Eric Andersenda1d1e72000-07-10 23:39:44 +0000949bool_t
950xdr_mountres3_ok (XDR *xdrs, mountres3_ok *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000951{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000952 //register int32_t *buf;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000953
Eric Andersenda1d1e72000-07-10 23:39:44 +0000954 if (!xdr_fhandle3 (xdrs, &objp->fhandle))
955 return FALSE;
956 if (!xdr_array (xdrs, (char **)&objp->auth_flavours.auth_flavours_val, (u_int *) &objp->auth_flavours.auth_flavours_len, ~0,
957 sizeof (int), (xdrproc_t) xdr_int))
958 return FALSE;
959 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000960}
961
Eric Andersenda1d1e72000-07-10 23:39:44 +0000962bool_t
963xdr_mountstat3 (XDR *xdrs, mountstat3 *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000964{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000965 //register int32_t *buf;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000966
Eric Andersenda1d1e72000-07-10 23:39:44 +0000967 if (!xdr_enum (xdrs, (enum_t *) objp))
968 return FALSE;
969 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000970}
971
Eric Andersenda1d1e72000-07-10 23:39:44 +0000972bool_t
973xdr_mountres3 (XDR *xdrs, mountres3 *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000974{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000975 //register int32_t *buf;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000976
Eric Andersenda1d1e72000-07-10 23:39:44 +0000977 if (!xdr_mountstat3 (xdrs, &objp->fhs_status))
978 return FALSE;
979 switch (objp->fhs_status) {
980 case MNT_OK:
981 if (!xdr_mountres3_ok (xdrs, &objp->mountres3_u.mountinfo))
982 return FALSE;
983 break;
984 default:
985 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000986 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000987 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000988}
989