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