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