blob: 7b742cb5f7ae2c155bb931940efebf7d716ef852 [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
Eric Andersenc7bda1c2004-03-15 08:29:22 +000027 *
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>
Eric Andersen1c43d0c1999-11-18 07:58:07 +000041#include <sys/socket.h>
Glenn L McGrath3f5bcbf2001-02-16 10:33:30 +000042#include <time.h>
Eric Andersen1c43d0c1999-11-18 07:58:07 +000043#include <sys/utsname.h>
Eric Andersen1c43d0c1999-11-18 07:58:07 +000044#include <netinet/in.h>
45#include <arpa/inet.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000046#include <stdlib.h>
47#include "busybox.h"
48#undef TRUE
49#undef FALSE
50#include <rpc/rpc.h>
51#include <rpc/pmap_prot.h>
52#include <rpc/pmap_clnt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000053#include "nfsmount.h"
Eric Andersen1c43d0c1999-11-18 07:58:07 +000054
Rob Landley7b363fd2005-12-20 17:18:01 +000055/* This is just a warning of a common mistake. Possibly this should be a
Bernhard Reutner-Fischered7bb622006-02-23 14:25:15 +000056 * uclibc faq entry rather than in busybox... */
Rob Landley7b363fd2005-12-20 17:18:01 +000057#if ENABLE_FEATURE_MOUNT_NFS && defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
58#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
59#endif
60
Eric Andersen80ff9442002-12-11 04:04:26 +000061
62/*
63 * NFS stats. The good thing with these values is that NFSv3 errors are
64 * a superset of NFSv2 errors (with the exception of NFSERR_WFLUSH which
65 * no-one uses anyway), so we can happily mix code as long as we make sure
66 * no NFSv3 errors are returned to NFSv2 clients.
67 * Error codes that have a `--' in the v2 column are not part of the
68 * standard, but seem to be widely used nevertheless.
69 */
70enum nfs_stat {
71 NFS_OK = 0, /* v2 v3 */
72 NFSERR_PERM = 1, /* v2 v3 */
73 NFSERR_NOENT = 2, /* v2 v3 */
74 NFSERR_IO = 5, /* v2 v3 */
75 NFSERR_NXIO = 6, /* v2 v3 */
76 NFSERR_EAGAIN = 11, /* v2 v3 */
77 NFSERR_ACCES = 13, /* v2 v3 */
78 NFSERR_EXIST = 17, /* v2 v3 */
79 NFSERR_XDEV = 18, /* v3 */
80 NFSERR_NODEV = 19, /* v2 v3 */
81 NFSERR_NOTDIR = 20, /* v2 v3 */
82 NFSERR_ISDIR = 21, /* v2 v3 */
83 NFSERR_INVAL = 22, /* v2 v3 that Sun forgot */
84 NFSERR_FBIG = 27, /* v2 v3 */
85 NFSERR_NOSPC = 28, /* v2 v3 */
86 NFSERR_ROFS = 30, /* v2 v3 */
87 NFSERR_MLINK = 31, /* v3 */
88 NFSERR_OPNOTSUPP = 45, /* v2 v3 */
89 NFSERR_NAMETOOLONG = 63, /* v2 v3 */
90 NFSERR_NOTEMPTY = 66, /* v2 v3 */
91 NFSERR_DQUOT = 69, /* v2 v3 */
92 NFSERR_STALE = 70, /* v2 v3 */
93 NFSERR_REMOTE = 71, /* v2 v3 */
94 NFSERR_WFLUSH = 99, /* v2 */
95 NFSERR_BADHANDLE = 10001, /* v3 */
96 NFSERR_NOT_SYNC = 10002, /* v3 */
97 NFSERR_BAD_COOKIE = 10003, /* v3 */
98 NFSERR_NOTSUPP = 10004, /* v3 */
99 NFSERR_TOOSMALL = 10005, /* v3 */
100 NFSERR_SERVERFAULT = 10006, /* v3 */
101 NFSERR_BADTYPE = 10007, /* v3 */
102 NFSERR_JUKEBOX = 10008 /* v3 */
103};
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000104
Eric Andersen80ff9442002-12-11 04:04:26 +0000105#define NFS_PROGRAM 100003
106
107
Rob Landleybc68cd12006-03-10 19:22:06 +0000108enum {
Glenn L McGrathc52a97d2000-12-09 23:59:04 +0000109#ifndef NFS_FHSIZE
Rob Landleybc68cd12006-03-10 19:22:06 +0000110 NFS_FHSIZE = 32,
Glenn L McGrathc52a97d2000-12-09 23:59:04 +0000111#endif
112#ifndef NFS_PORT
Rob Landleybc68cd12006-03-10 19:22:06 +0000113 NFS_PORT = 2049
Glenn L McGrathc52a97d2000-12-09 23:59:04 +0000114#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000115};
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000116
Eric Andersenda1d1e72000-07-10 23:39:44 +0000117/* Disable the nls stuff */
118# undef bindtextdomain
119# define bindtextdomain(Domain, Directory) /* empty */
120# undef textdomain
121# define textdomain(Domain) /* empty */
Eric Andersenda1d1e72000-07-10 23:39:44 +0000122
Rob Landleybc68cd12006-03-10 19:22:06 +0000123enum {
124 MS_MGC_VAL = 0xc0ed0000, /* Magic number indicatng "new" flags */
125 MS_RDONLY = 1, /* Mount read-only */
126 MS_NOSUID = 2, /* Ignore suid and sgid bits */
127 MS_NODEV = 4, /* Disallow access to device special files */
128 MS_NOEXEC = 8, /* Disallow program execution */
129 MS_SYNCHRONOUS = 16, /* Writes are synced at once */
130 MS_REMOUNT = 32, /* Alter flags of a mounted FS */
131 MS_MANDLOCK = 64, /* Allow mandatory locks on an FS */
132 S_QUOTA = 128, /* Quota initialized for file/directory/symlink */
133 S_APPEND = 256, /* Append-only file */
134 S_IMMUTABLE = 512, /* Immutable file */
135 MS_NOATIME = 1024, /* Do not update access times. */
136 MS_NODIRATIME = 2048 /* Do not update directory access times */
137};
Eric Andersenda1d1e72000-07-10 23:39:44 +0000138
139
140/*
141 * We want to be able to compile mount on old kernels in such a way
142 * that the binary will work well on more recent kernels.
143 * Thus, if necessary we teach nfsmount.c the structure of new fields
144 * that will come later.
145 *
146 * Moreover, the new kernel includes conflict with glibc includes
147 * so it is easiest to ignore the kernel altogether (at compile time).
148 */
149
Mark Whitley85260672001-01-30 18:53:42 +0000150/* NOTE: Do not make this into a 'static const int' because the pre-processor
151 * needs to test this value in some #if statements. */
152#define NFS_MOUNT_VERSION 4
Eric Andersenda1d1e72000-07-10 23:39:44 +0000153
154struct nfs2_fh {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000155 char data[32];
Eric Andersenda1d1e72000-07-10 23:39:44 +0000156};
157struct nfs3_fh {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000158 unsigned short size;
159 unsigned char data[64];
Eric Andersenda1d1e72000-07-10 23:39:44 +0000160};
161
162struct nfs_mount_data {
163 int version; /* 1 */
164 int fd; /* 1 */
165 struct nfs2_fh old_root; /* 1 */
166 int flags; /* 1 */
167 int rsize; /* 1 */
168 int wsize; /* 1 */
169 int timeo; /* 1 */
170 int retrans; /* 1 */
171 int acregmin; /* 1 */
172 int acregmax; /* 1 */
173 int acdirmin; /* 1 */
174 int acdirmax; /* 1 */
175 struct sockaddr_in addr; /* 1 */
176 char hostname[256]; /* 1 */
177 int namlen; /* 2 */
178 unsigned int bsize; /* 3 */
179 struct nfs3_fh root; /* 4 */
180};
181
182/* bits in the flags field */
Rob Landleybc68cd12006-03-10 19:22:06 +0000183enum {
184 NFS_MOUNT_SOFT = 0x0001, /* 1 */
185 NFS_MOUNT_INTR = 0x0002, /* 1 */
186 NFS_MOUNT_SECURE = 0x0004, /* 1 */
187 NFS_MOUNT_POSIX = 0x0008, /* 1 */
188 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
189 NFS_MOUNT_NOAC = 0x0020, /* 1 */
190 NFS_MOUNT_TCP = 0x0040, /* 2 */
191 NFS_MOUNT_VER3 = 0x0080, /* 3 */
192 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
193 NFS_MOUNT_NONLM = 0x0200 /* 3 */
194};
Eric Andersenda1d1e72000-07-10 23:39:44 +0000195
196
197#define UTIL_LINUX_VERSION "2.10m"
198#define util_linux_version "util-linux-2.10m"
199
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000200#define HAVE_inet_aton
Eric Andersenda1d1e72000-07-10 23:39:44 +0000201#define HAVE_scsi_h
202#define HAVE_blkpg_h
203#define HAVE_kd_h
204#define HAVE_termcap
205#define HAVE_locale_h
206#define HAVE_libintl_h
207#define ENABLE_NLS
208#define HAVE_langinfo_h
209#define HAVE_progname
210#define HAVE_openpty
211#define HAVE_nanosleep
212#define HAVE_personality
213#define HAVE_tm_gmtoff
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000214
Eric Andersen1ca20a72001-03-21 07:34:27 +0000215static char *nfs_strerror(int status);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000216
Eric Andersenda1d1e72000-07-10 23:39:44 +0000217#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000218
Rob Landleybc68cd12006-03-10 19:22:06 +0000219enum {
220 EX_FAIL = 32, /* mount failure */
221 EX_BG = 256 /* retry in background (internal only) */
222};
Eric Andersenda1d1e72000-07-10 23:39:44 +0000223
224
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000225/*
Eric Andersenda1d1e72000-07-10 23:39:44 +0000226 * nfs_mount_version according to the sources seen at compile time.
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000227 */
Mark Whitley59ab0252001-01-23 22:30:04 +0000228static int nfs_mount_version;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000229
230/*
231 * Unfortunately, the kernel prints annoying console messages
232 * in case of an unexpected nfs mount version (instead of
233 * just returning some error). Therefore we'll have to try
234 * and figure out what version the kernel expects.
235 *
236 * Variables:
237 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
238 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
239 * nfs_mount_version: version this source and running kernel can handle
240 */
Eric Andersenda1d1e72000-07-10 23:39:44 +0000241static void
Mark Whitley065c7e72001-02-01 19:51:13 +0000242find_kernel_nfs_mount_version(void)
243{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000244 static int kernel_version = 0;
245
246 if (kernel_version)
247 return;
248
Mark Whitley59ab0252001-01-23 22:30:04 +0000249 nfs_mount_version = NFS_MOUNT_VERSION; /* default */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000250
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +0000251 kernel_version = get_linux_version_code();
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000252 if (kernel_version) {
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +0000253 if (kernel_version < KERNEL_VERSION(2,1,32))
Mark Whitley065c7e72001-02-01 19:51:13 +0000254 nfs_mount_version = 1;
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +0000255 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
256 (kernel_version >= KERNEL_VERSION(2,3,0) &&
257 kernel_version < KERNEL_VERSION(2,3,99)))
Mark Whitley065c7e72001-02-01 19:51:13 +0000258 nfs_mount_version = 3;
259 else
260 nfs_mount_version = 4; /* since 2.3.99pre4 */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000261 }
262 if (nfs_mount_version > NFS_MOUNT_VERSION)
Mark Whitley065c7e72001-02-01 19:51:13 +0000263 nfs_mount_version = NFS_MOUNT_VERSION;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000264}
265
Eric Andersenda1d1e72000-07-10 23:39:44 +0000266static struct pmap *
267get_mountport(struct sockaddr_in *server_addr,
268 long unsigned prog,
269 long unsigned version,
270 long unsigned proto,
271 long unsigned port)
272{
273struct pmaplist *pmap;
274static struct pmap p = {0, 0, 0, 0};
275
276server_addr->sin_port = PMAPPORT;
277pmap = pmap_getmaps(server_addr);
278
279if (version > MAX_NFSPROT)
280 version = MAX_NFSPROT;
281if (!prog)
282 prog = MOUNTPROG;
283p.pm_prog = prog;
284p.pm_vers = version;
285p.pm_prot = proto;
286p.pm_port = port;
287
288while (pmap) {
289 if (pmap->pml_map.pm_prog != prog)
290 goto next;
291 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
292 goto next;
293 if (version > 2 && pmap->pml_map.pm_vers != version)
294 goto next;
295 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
296 goto next;
297 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
298 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
299 (port && pmap->pml_map.pm_port != port))
300 goto next;
301 memcpy(&p, &pmap->pml_map, sizeof(p));
302next:
303 pmap = pmap->pml_next;
304}
305if (!p.pm_vers)
306 p.pm_vers = MOUNTVERS;
307if (!p.pm_port)
308 p.pm_port = MOUNTPORT;
309if (!p.pm_prot)
310 p.pm_prot = IPPROTO_TCP;
311return &p;
312}
313
314int nfsmount(const char *spec, const char *node, int *flags,
Rob Landley6a6798b2005-08-10 20:35:54 +0000315 char **mount_opts, int running_bg)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000316{
317 static char *prev_bg_host;
318 char hostdir[1024];
319 CLIENT *mclient;
320 char *hostname;
Matt Kraaif9d6aa02001-07-26 14:26:53 +0000321 char *pathname;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000322 char *old_opts;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000323 char *mounthost=NULL;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000324 char new_opts[1024];
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000325 struct timeval total_timeout;
326 enum clnt_stat clnt_stat;
Eric Andersen882cec32004-07-26 12:05:12 +0000327 struct nfs_mount_data data;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000328 char *opt, *opteq;
329 int val;
330 struct hostent *hp;
331 struct sockaddr_in server_addr;
332 struct sockaddr_in mount_server_addr;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000333 struct pmap* pm_mnt;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000334 int msock, fsock;
335 struct timeval retry_timeout;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000336 union {
337 struct fhstatus nfsv2;
338 struct mountres3 nfsv3;
339 } status;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000340 struct stat statbuf;
341 char *s;
342 int port;
343 int mountport;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000344 int proto;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000345 int bg;
346 int soft;
347 int intr;
348 int posix;
349 int nocto;
350 int noac;
351 int nolock;
352 int retry;
353 int tcp;
354 int mountprog;
355 int mountvers;
356 int nfsprog;
357 int nfsvers;
358 int retval;
359 time_t t;
360 time_t prevt;
361 time_t timeout;
362
363 find_kernel_nfs_mount_version();
364
365 retval = EX_FAIL;
366 msock = fsock = -1;
367 mclient = NULL;
368 if (strlen(spec) >= sizeof(hostdir)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000369 bb_error_msg("excessively long host:dir argument");
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000370 goto fail;
371 }
372 strcpy(hostdir, spec);
373 if ((s = strchr(hostdir, ':'))) {
374 hostname = hostdir;
Matt Kraaif9d6aa02001-07-26 14:26:53 +0000375 pathname = s + 1;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000376 *s = '\0';
377 /* Ignore all but first hostname in replicated mounts
378 until they can be fully supported. (mack@sgi.com) */
379 if ((s = strchr(hostdir, ','))) {
380 *s = '\0';
Manuel Novoa III cad53642003-03-19 09:13:01 +0000381 bb_error_msg("warning: multiple hostnames not supported");
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000382 }
383 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000384 bb_error_msg("directory to mount not in host:dir format");
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000385 goto fail;
386 }
387
388 server_addr.sin_family = AF_INET;
389#ifdef HAVE_inet_aton
390 if (!inet_aton(hostname, &server_addr.sin_addr))
391#endif
392 {
393 if ((hp = gethostbyname(hostname)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000394 bb_herror_msg("%s", hostname);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000395 goto fail;
396 } else {
397 if (hp->h_length > sizeof(struct in_addr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000398 bb_error_msg("got bad hp->h_length");
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000399 hp->h_length = sizeof(struct in_addr);
400 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000401 memcpy(&server_addr.sin_addr,
402 hp->h_addr, hp->h_length);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000403 }
404 }
405
Eric Andersenda1d1e72000-07-10 23:39:44 +0000406 memcpy (&mount_server_addr, &server_addr, sizeof (mount_server_addr));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000407
408 /* add IP address to mtab options for use when unmounting */
409
410 s = inet_ntoa(server_addr.sin_addr);
Rob Landley6a6798b2005-08-10 20:35:54 +0000411 old_opts = *mount_opts;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000412 if (!old_opts)
413 old_opts = "";
414 if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000415 bb_error_msg("excessively long option argument");
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000416 goto fail;
417 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000418 sprintf(new_opts, "%s%saddr=%s",
419 old_opts, *old_opts ? "," : "", s);
Rob Landley6a6798b2005-08-10 20:35:54 +0000420 *mount_opts = bb_xstrdup(new_opts);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000421
422 /* Set default options.
423 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
424 * let the kernel decide.
425 * timeo is filled in after we know whether it'll be TCP or UDP. */
426 memset(&data, 0, sizeof(data));
Eric Andersenda1d1e72000-07-10 23:39:44 +0000427 data.retrans = 3;
428 data.acregmin = 3;
429 data.acregmax = 60;
430 data.acdirmin = 30;
431 data.acdirmax = 60;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000432#if NFS_MOUNT_VERSION >= 2
Eric Andersenda1d1e72000-07-10 23:39:44 +0000433 data.namlen = NAME_MAX;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000434#endif
435
436 bg = 0;
437 soft = 0;
438 intr = 0;
439 posix = 0;
440 nocto = 0;
441 nolock = 0;
442 noac = 0;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000443 retry = 10000; /* 10000 minutes ~ 1 week */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000444 tcp = 0;
445
446 mountprog = MOUNTPROG;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000447 mountvers = 0;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000448 port = 0;
449 mountport = 0;
450 nfsprog = NFS_PROGRAM;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000451 nfsvers = 0;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000452
453 /* parse options */
454
455 for (opt = strtok(old_opts, ","); opt; opt = strtok(NULL, ",")) {
456 if ((opteq = strchr(opt, '='))) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000457 val = atoi(opteq + 1);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000458 *opteq = '\0';
459 if (!strcmp(opt, "rsize"))
460 data.rsize = val;
461 else if (!strcmp(opt, "wsize"))
462 data.wsize = val;
463 else if (!strcmp(opt, "timeo"))
464 data.timeo = val;
465 else if (!strcmp(opt, "retrans"))
466 data.retrans = val;
467 else if (!strcmp(opt, "acregmin"))
468 data.acregmin = val;
469 else if (!strcmp(opt, "acregmax"))
470 data.acregmax = val;
471 else if (!strcmp(opt, "acdirmin"))
472 data.acdirmin = val;
473 else if (!strcmp(opt, "acdirmax"))
474 data.acdirmax = val;
475 else if (!strcmp(opt, "actimeo")) {
476 data.acregmin = val;
477 data.acregmax = val;
478 data.acdirmin = val;
479 data.acdirmax = val;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000480 }
481 else if (!strcmp(opt, "retry"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000482 retry = val;
483 else if (!strcmp(opt, "port"))
484 port = val;
485 else if (!strcmp(opt, "mountport"))
Eric Andersenda1d1e72000-07-10 23:39:44 +0000486 mountport = val;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000487 else if (!strcmp(opt, "mounthost"))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000488 mounthost=bb_xstrndup(opteq+1,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000489 strcspn(opteq+1," \t\n\r,"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000490 else if (!strcmp(opt, "mountprog"))
491 mountprog = val;
492 else if (!strcmp(opt, "mountvers"))
493 mountvers = val;
494 else if (!strcmp(opt, "nfsprog"))
495 nfsprog = val;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000496 else if (!strcmp(opt, "nfsvers") ||
497 !strcmp(opt, "vers"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000498 nfsvers = val;
499 else if (!strcmp(opt, "proto")) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000500 if (!strncmp(opteq+1, "tcp", 3))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000501 tcp = 1;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000502 else if (!strncmp(opteq+1, "udp", 3))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000503 tcp = 0;
504 else
505 printf(_("Warning: Unrecognized proto= option.\n"));
506 } else if (!strcmp(opt, "namlen")) {
507#if NFS_MOUNT_VERSION >= 2
508 if (nfs_mount_version >= 2)
509 data.namlen = val;
510 else
511#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +0000512 printf(_("Warning: Option namlen is not supported.\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000513 } else if (!strcmp(opt, "addr"))
Eric Andersenda1d1e72000-07-10 23:39:44 +0000514 /* ignore */;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000515 else {
516 printf(_("unknown nfs mount parameter: "
Eric Andersenda1d1e72000-07-10 23:39:44 +0000517 "%s=%d\n"), opt, val);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000518 goto fail;
519 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000520 }
521 else {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000522 val = 1;
523 if (!strncmp(opt, "no", 2)) {
524 val = 0;
525 opt += 2;
526 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000527 if (!strcmp(opt, "bg"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000528 bg = val;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000529 else if (!strcmp(opt, "fg"))
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000530 bg = !val;
531 else if (!strcmp(opt, "soft"))
532 soft = val;
533 else if (!strcmp(opt, "hard"))
534 soft = !val;
535 else if (!strcmp(opt, "intr"))
536 intr = val;
537 else if (!strcmp(opt, "posix"))
538 posix = val;
539 else if (!strcmp(opt, "cto"))
540 nocto = !val;
541 else if (!strcmp(opt, "ac"))
542 noac = !val;
543 else if (!strcmp(opt, "tcp"))
544 tcp = val;
545 else if (!strcmp(opt, "udp"))
546 tcp = !val;
547 else if (!strcmp(opt, "lock")) {
548 if (nfs_mount_version >= 3)
549 nolock = !val;
550 else
Eric Andersenda1d1e72000-07-10 23:39:44 +0000551 printf(_("Warning: option nolock is not supported.\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000552 } else {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000553 printf(_("unknown nfs mount option: "
554 "%s%s\n"), val ? "" : "no", opt);
555 goto fail;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000556 }
557 }
558 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000559 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
560
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000561 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
562 | (intr ? NFS_MOUNT_INTR : 0)
563 | (posix ? NFS_MOUNT_POSIX : 0)
564 | (nocto ? NFS_MOUNT_NOCTO : 0)
565 | (noac ? NFS_MOUNT_NOAC : 0);
566#if NFS_MOUNT_VERSION >= 2
567 if (nfs_mount_version >= 2)
568 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
569#endif
570#if NFS_MOUNT_VERSION >= 3
571 if (nfs_mount_version >= 3)
572 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
573#endif
Rob Landley29ba9792006-04-17 23:02:57 +0000574 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000575 bb_error_msg("NFSv%d not supported!", nfsvers);
Rob Landley29ba9792006-04-17 23:02:57 +0000576 return 1;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000577 }
578 if (nfsvers && !mountvers)
579 mountvers = (nfsvers < 3) ? 1 : nfsvers;
580 if (nfsvers && nfsvers < mountvers) {
581 mountvers = nfsvers;
582 }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000583
584 /* Adjust options if none specified */
585 if (!data.timeo)
586 data.timeo = tcp ? 70 : 7;
587
588#ifdef NFS_MOUNT_DEBUG
589 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000590 data.rsize, data.wsize, data.timeo, data.retrans);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000591 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000592 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000593 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000594 port, bg, retry, data.flags);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000595 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000596 mountprog, mountvers, nfsprog, nfsvers);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000597 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d\n",
Eric Andersenda1d1e72000-07-10 23:39:44 +0000598 (data.flags & NFS_MOUNT_SOFT) != 0,
599 (data.flags & NFS_MOUNT_INTR) != 0,
600 (data.flags & NFS_MOUNT_POSIX) != 0,
601 (data.flags & NFS_MOUNT_NOCTO) != 0,
602 (data.flags & NFS_MOUNT_NOAC) != 0);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000603#if NFS_MOUNT_VERSION >= 2
Eric Andersenda1d1e72000-07-10 23:39:44 +0000604 printf("tcp = %d\n",
605 (data.flags & NFS_MOUNT_TCP) != 0);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000606#endif
607#endif
608
609 data.version = nfs_mount_version;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000610
611 if (*flags & MS_REMOUNT)
Eric Andersen882cec32004-07-26 12:05:12 +0000612 goto copy_data_and_return;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000613
614 /*
615 * If the previous mount operation on the same host was
616 * backgrounded, and the "bg" for this mount is also set,
617 * give up immediately, to avoid the initial timeout.
618 */
619 if (bg && !running_bg &&
Eric Andersenda1d1e72000-07-10 23:39:44 +0000620 prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000621 if (retry > 0)
622 retval = EX_BG;
623 return retval;
624 }
625
626 /* create mount deamon client */
627 /* See if the nfs host = mount host. */
628 if (mounthost) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000629 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
630 mount_server_addr.sin_family = AF_INET;
631 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
632 } else {
633 if ((hp = gethostbyname(mounthost)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000634 bb_herror_msg("%s", mounthost);
Eric Andersenda1d1e72000-07-10 23:39:44 +0000635 goto fail;
636 } else {
637 if (hp->h_length > sizeof(struct in_addr)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000638 bb_error_msg("got bad hp->h_length?");
Eric Andersenda1d1e72000-07-10 23:39:44 +0000639 hp->h_length = sizeof(struct in_addr);
640 }
641 mount_server_addr.sin_family = AF_INET;
642 memcpy(&mount_server_addr.sin_addr,
643 hp->h_addr, hp->h_length);
644 }
645 }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000646 }
647
648 /*
649 * The following loop implements the mount retries. On the first
650 * call, "running_bg" is 0. When the mount times out, and the
651 * "bg" option is set, the exit status EX_BG will be returned.
652 * For a backgrounded mount, there will be a second call by the
653 * child process with "running_bg" set to 1.
654 *
655 * The case where the mount point is not present and the "bg"
656 * option is set, is treated as a timeout. This is done to
657 * support nested mounts.
658 *
659 * The "retry" count specified by the user is the number of
660 * minutes to retry before giving up.
661 *
662 * Only the first error message will be displayed.
663 */
664 retry_timeout.tv_sec = 3;
665 retry_timeout.tv_usec = 0;
666 total_timeout.tv_sec = 20;
667 total_timeout.tv_usec = 0;
668 timeout = time(NULL) + 60 * retry;
669 prevt = 0;
670 t = 30;
671 val = 1;
672 for (;;) {
673 if (bg && stat(node, &statbuf) == -1) {
674 if (running_bg) {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000675 sleep(val); /* 1, 2, 4, 8, 16, 30, ... */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000676 val *= 2;
677 if (val > 30)
678 val = 30;
679 }
680 } else {
681 /* be careful not to use too many CPU cycles */
682 if (t - prevt < 30)
683 sleep(30);
684
Eric Andersenda1d1e72000-07-10 23:39:44 +0000685 pm_mnt = get_mountport(&mount_server_addr,
686 mountprog,
687 mountvers,
688 proto,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000689 mountport);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000690
Eric Andersenda1d1e72000-07-10 23:39:44 +0000691 /* contact the mount daemon via TCP */
692 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
693 msock = RPC_ANYSOCK;
694
695 switch (pm_mnt->pm_prot) {
696 case IPPROTO_UDP:
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000697 mclient = clntudp_create(&mount_server_addr,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000698 pm_mnt->pm_prog,
699 pm_mnt->pm_vers,
700 retry_timeout,
701 &msock);
702 if (mclient)
703 break;
704 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
705 msock = RPC_ANYSOCK;
706 case IPPROTO_TCP:
707 mclient = clnttcp_create(&mount_server_addr,
708 pm_mnt->pm_prog,
709 pm_mnt->pm_vers,
710 &msock, 0, 0);
711 break;
712 default:
713 mclient = 0;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000714 }
715 if (mclient) {
Matt Kraaif9d6aa02001-07-26 14:26:53 +0000716 /* try to mount hostname:pathname */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000717 mclient->cl_auth = authunix_create_default();
Eric Andersenda1d1e72000-07-10 23:39:44 +0000718
719 /* make pointers in xdr_mountres3 NULL so
720 * that xdr_array allocates memory for us
721 */
722 memset(&status, 0, sizeof(status));
723
724 if (pm_mnt->pm_vers == 3)
725 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
726 (xdrproc_t) xdr_dirpath,
Matt Kraaif9d6aa02001-07-26 14:26:53 +0000727 (caddr_t) &pathname,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000728 (xdrproc_t) xdr_mountres3,
729 (caddr_t) &status,
730 total_timeout);
731 else
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000732 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000733 (xdrproc_t) xdr_dirpath,
Matt Kraaif9d6aa02001-07-26 14:26:53 +0000734 (caddr_t) &pathname,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000735 (xdrproc_t) xdr_fhstatus,
736 (caddr_t) &status,
737 total_timeout);
738
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000739 if (clnt_stat == RPC_SUCCESS)
740 break; /* we're done */
741 if (errno != ECONNREFUSED) {
742 clnt_perror(mclient, "mount");
743 goto fail; /* don't retry */
744 }
745 if (!running_bg && prevt == 0)
746 clnt_perror(mclient, "mount");
747 auth_destroy(mclient->cl_auth);
748 clnt_destroy(mclient);
749 mclient = 0;
750 close(msock);
751 } else {
752 if (!running_bg && prevt == 0)
753 clnt_pcreateerror("mount");
754 }
755 prevt = t;
756 }
757 if (!bg)
Eric Andersenda1d1e72000-07-10 23:39:44 +0000758 goto fail;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000759 if (!running_bg) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000760 prev_bg_host = bb_xstrdup(hostname);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000761 if (retry > 0)
762 retval = EX_BG;
763 goto fail;
764 }
765 t = time(NULL);
766 if (t >= timeout)
767 goto fail;
768 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000769 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000770
Eric Andersenda1d1e72000-07-10 23:39:44 +0000771 if (nfsvers == 2) {
772 if (status.nfsv2.fhs_status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000773 bb_error_msg("%s:%s failed, reason given by server: %s",
Matt Kraaif9d6aa02001-07-26 14:26:53 +0000774 hostname, pathname,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000775 nfs_strerror(status.nfsv2.fhs_status));
776 goto fail;
777 }
778 memcpy(data.root.data,
779 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
780 NFS_FHSIZE);
781#if NFS_MOUNT_VERSION >= 4
782 data.root.size = NFS_FHSIZE;
783 memcpy(data.old_root.data,
784 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
785 NFS_FHSIZE);
786#endif
787 } else {
788#if NFS_MOUNT_VERSION >= 4
Eric Andersen851895a2001-03-21 21:52:25 +0000789 fhandle3 *my_fhandle;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000790 if (status.nfsv3.fhs_status != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000791 bb_error_msg("%s:%s failed, reason given by server: %s",
Matt Kraaif9d6aa02001-07-26 14:26:53 +0000792 hostname, pathname,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000793 nfs_strerror(status.nfsv3.fhs_status));
794 goto fail;
795 }
Eric Andersen851895a2001-03-21 21:52:25 +0000796 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000797 memset(data.old_root.data, 0, NFS_FHSIZE);
798 memset(&data.root, 0, sizeof(data.root));
Eric Andersen851895a2001-03-21 21:52:25 +0000799 data.root.size = my_fhandle->fhandle3_len;
Eric Andersenda1d1e72000-07-10 23:39:44 +0000800 memcpy(data.root.data,
Eric Andersen851895a2001-03-21 21:52:25 +0000801 (char *) my_fhandle->fhandle3_val,
802 my_fhandle->fhandle3_len);
Eric Andersenda1d1e72000-07-10 23:39:44 +0000803
804 data.flags |= NFS_MOUNT_VER3;
805#endif
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000806 }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000807
808 /* create nfs socket for kernel */
809
810 if (tcp) {
811 if (nfs_mount_version < 3) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000812 printf(_("NFS over TCP is not supported.\n"));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000813 goto fail;
814 }
815 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
816 } else
817 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
818 if (fsock < 0) {
819 perror(_("nfs socket"));
820 goto fail;
821 }
822 if (bindresvport(fsock, 0) < 0) {
823 perror(_("nfs bindresvport"));
824 goto fail;
825 }
826 if (port == 0) {
827 server_addr.sin_port = PMAPPORT;
828 port = pmap_getport(&server_addr, nfsprog, nfsvers,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000829 tcp ? IPPROTO_TCP : IPPROTO_UDP);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000830 if (port == 0)
831 port = NFS_PORT;
832#ifdef NFS_MOUNT_DEBUG
833 else
834 printf(_("used portmapper to find NFS port\n"));
835#endif
836 }
837#ifdef NFS_MOUNT_DEBUG
838 printf(_("using port %d for nfs deamon\n"), port);
839#endif
840 server_addr.sin_port = htons(port);
Eric Andersenda1d1e72000-07-10 23:39:44 +0000841 /*
842 * connect() the socket for kernels 1.3.10 and below only,
843 * to avoid problems with multihomed hosts.
844 * --Swen
845 */
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +0000846 if (get_linux_version_code() <= KERNEL_VERSION(2,3,10)
Eric Andersenda1d1e72000-07-10 23:39:44 +0000847 && connect(fsock, (struct sockaddr *) &server_addr,
848 sizeof (server_addr)) < 0) {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000849 perror(_("nfs connect"));
850 goto fail;
851 }
852
853 /* prepare data structure for kernel */
854
855 data.fd = fsock;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000856 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
857 strncpy(data.hostname, hostname, sizeof(data.hostname));
858
859 /* clean up */
860
861 auth_destroy(mclient->cl_auth);
862 clnt_destroy(mclient);
863 close(msock);
Eric Andersen882cec32004-07-26 12:05:12 +0000864copy_data_and_return:
865 *mount_opts = xrealloc(*mount_opts, sizeof(data));
866 memcpy(*mount_opts, &data, sizeof(data));
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000867 return 0;
868
869 /* abort */
870
Eric Andersenda1d1e72000-07-10 23:39:44 +0000871fail:
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000872 if (msock != -1) {
873 if (mclient) {
874 auth_destroy(mclient->cl_auth);
875 clnt_destroy(mclient);
876 }
877 close(msock);
878 }
879 if (fsock != -1)
880 close(fsock);
881 return retval;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000882}
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000883
884/*
885 * We need to translate between nfs status return values and
886 * the local errno values which may not be the same.
887 *
888 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
889 * "after #include <errno.h> the symbol errno is reserved for any use,
890 * it cannot even be used as a struct tag or field name".
891 */
892
893#ifndef EDQUOT
894#define EDQUOT ENOSPC
895#endif
896
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000897static const struct {
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000898 enum nfs_stat stat;
899 int errnum;
900} nfs_errtbl[] = {
Eric Andersenda1d1e72000-07-10 23:39:44 +0000901 { NFS_OK, 0 },
902 { NFSERR_PERM, EPERM },
903 { NFSERR_NOENT, ENOENT },
904 { NFSERR_IO, EIO },
905 { NFSERR_NXIO, ENXIO },
906 { NFSERR_ACCES, EACCES },
907 { NFSERR_EXIST, EEXIST },
908 { NFSERR_NODEV, ENODEV },
909 { NFSERR_NOTDIR, ENOTDIR },
910 { NFSERR_ISDIR, EISDIR },
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000911#ifdef NFSERR_INVAL
Eric Andersenda1d1e72000-07-10 23:39:44 +0000912 { NFSERR_INVAL, EINVAL }, /* that Sun forgot */
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000913#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +0000914 { NFSERR_FBIG, EFBIG },
915 { NFSERR_NOSPC, ENOSPC },
916 { NFSERR_ROFS, EROFS },
917 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
918 { NFSERR_NOTEMPTY, ENOTEMPTY },
919 { NFSERR_DQUOT, EDQUOT },
920 { NFSERR_STALE, ESTALE },
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000921#ifdef EWFLUSH
Eric Andersenda1d1e72000-07-10 23:39:44 +0000922 { NFSERR_WFLUSH, EWFLUSH },
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000923#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +0000924 /* Throw in some NFSv3 values for even more fun (HP returns these) */
925 { 71, EREMOTE },
926
927 { -1, EIO }
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000928};
929
Eric Andersen1ca20a72001-03-21 07:34:27 +0000930static char *nfs_strerror(int status)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000931{
932 int i;
933 static char buf[256];
934
935 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000936 if (nfs_errtbl[i].stat == status)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000937 return strerror(nfs_errtbl[i].errnum);
938 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000939 sprintf(buf, _("unknown nfs status return value: %d"), status);
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000940 return buf;
941}
942
Eric Andersen3e6ff902001-03-09 21:24:12 +0000943static bool_t
Eric Andersenda1d1e72000-07-10 23:39:44 +0000944xdr_fhandle (XDR *xdrs, fhandle objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000945{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000946 if (!xdr_opaque (xdrs, objp, FHSIZE))
947 return FALSE;
948 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000949}
950
Eric Andersenda1d1e72000-07-10 23:39:44 +0000951bool_t
952xdr_fhstatus (XDR *xdrs, fhstatus *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000953{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000954 if (!xdr_u_int (xdrs, &objp->fhs_status))
955 return FALSE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000956 switch (objp->fhs_status) {
957 case 0:
Eric Andersenda1d1e72000-07-10 23:39:44 +0000958 if (!xdr_fhandle (xdrs, objp->fhstatus_u.fhs_fhandle))
959 return FALSE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000960 break;
961 default:
962 break;
963 }
Eric Andersenda1d1e72000-07-10 23:39:44 +0000964 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000965}
966
Eric Andersenda1d1e72000-07-10 23:39:44 +0000967bool_t
968xdr_dirpath (XDR *xdrs, dirpath *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000969{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000970 if (!xdr_string (xdrs, objp, MNTPATHLEN))
971 return FALSE;
972 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000973}
974
Eric Andersenda1d1e72000-07-10 23:39:44 +0000975bool_t
976xdr_fhandle3 (XDR *xdrs, fhandle3 *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000977{
Eric Andersend2420792004-01-30 22:56:20 +0000978 if (!xdr_bytes (xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
Eric Andersenda1d1e72000-07-10 23:39:44 +0000979 return FALSE;
980 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000981}
982
Eric Andersenda1d1e72000-07-10 23:39:44 +0000983bool_t
984xdr_mountres3_ok (XDR *xdrs, mountres3_ok *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000985{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000986 if (!xdr_fhandle3 (xdrs, &objp->fhandle))
987 return FALSE;
Eric Andersend2420792004-01-30 22:56:20 +0000988 if (!xdr_array (xdrs, (char **)&objp->auth_flavours.auth_flavours_val, (unsigned int *) &objp->auth_flavours.auth_flavours_len, ~0,
Eric Andersenda1d1e72000-07-10 23:39:44 +0000989 sizeof (int), (xdrproc_t) xdr_int))
990 return FALSE;
991 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000992}
993
Eric Andersenda1d1e72000-07-10 23:39:44 +0000994bool_t
995xdr_mountstat3 (XDR *xdrs, mountstat3 *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +0000996{
Eric Andersenda1d1e72000-07-10 23:39:44 +0000997 if (!xdr_enum (xdrs, (enum_t *) objp))
998 return FALSE;
999 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +00001000}
1001
Eric Andersenda1d1e72000-07-10 23:39:44 +00001002bool_t
1003xdr_mountres3 (XDR *xdrs, mountres3 *objp)
Eric Andersen1c43d0c1999-11-18 07:58:07 +00001004{
Eric Andersenda1d1e72000-07-10 23:39:44 +00001005 if (!xdr_mountstat3 (xdrs, &objp->fhs_status))
1006 return FALSE;
1007 switch (objp->fhs_status) {
1008 case MNT_OK:
1009 if (!xdr_mountres3_ok (xdrs, &objp->mountres3_u.mountinfo))
1010 return FALSE;
1011 break;
1012 default:
1013 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001014 }
Eric Andersenda1d1e72000-07-10 23:39:44 +00001015 return TRUE;
Eric Andersen1c43d0c1999-11-18 07:58:07 +00001016}
1017