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