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