mount_nfs.c revision 1.69.8.1 1 /* $NetBSD: mount_nfs.c,v 1.69.8.1 2013/06/23 06:28:51 tls Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
44 #else
45 __RCSID("$NetBSD: mount_nfs.c,v 1.69.8.1 2013/06/23 06:28:51 tls Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <syslog.h>
54
55 #include <nfs/rpcv2.h>
56 #include <nfs/nfsproto.h>
57 #include <nfs/nfs.h>
58 #include <nfs/nfsmount.h>
59
60 #include <arpa/inet.h>
61
62 #include <err.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <netdb.h>
66 #include <signal.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 #include <util.h>
72
73 #include <mntopts.h>
74
75 #include "mountprog.h"
76 #include "mount_nfs.h"
77
78 #define ALTF_BG 0x00000001
79 #define ALTF_CONN 0x00000002
80 #define ALTF_DUMBTIMR 0x00000004
81 #define ALTF_INTR 0x00000008
82 #define ALTF_NFSV3 0x00000020
83 #define ALTF_RDIRPLUS 0x00000040
84 #define ALTF_MNTUDP 0x00000080
85 #define ALTF_NORESPORT 0x00000100
86 #define ALTF_SEQPACKET 0x00000200
87 #define ALTF_NQNFS 0x00000400
88 #define ALTF_SOFT 0x00000800
89 #define ALTF_TCP 0x00001000
90 #define ALTF_NFSV2 0x00002000
91 #define ALTF_PORT 0x00004000
92 #define ALTF_RSIZE 0x00008000
93 #define ALTF_WSIZE 0x00010000
94 #define ALTF_RDIRSIZE 0x00020000
95 #define ALTF_MAXGRPS 0x00040000
96 #define ALTF_LEASETERM 0x00080000
97 #define ALTF_READAHEAD 0x00100000
98 #define ALTF_DEADTHRESH 0x00200000
99 #define ALTF_TIMEO 0x00400000
100 #define ALTF_RETRANS 0x00800000
101
102 static const struct mntopt mopts[] = {
103 MOPT_STDOPTS,
104 MOPT_FORCE,
105 MOPT_UPDATE,
106 MOPT_GETARGS,
107 { "bg", 0, ALTF_BG, 1 },
108 { "conn", 0, ALTF_CONN, 1 },
109 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
110 { "intr", 0, ALTF_INTR, 1 },
111 { "nfsv3", 0, ALTF_NFSV3, 1 },
112 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
113 { "mntudp", 0, ALTF_MNTUDP, 1 },
114 { "resport", 1, ALTF_NORESPORT, 1 },
115 { "nqnfs", 0, ALTF_NQNFS, 1 },
116 { "soft", 0, ALTF_SOFT, 1 },
117 { "tcp", 0, ALTF_TCP, 1 },
118 { "nfsv2", 0, ALTF_NFSV2, 1 },
119 { "port", 0, ALTF_PORT, 1 },
120 { "rsize", 0, ALTF_RSIZE, 1 },
121 { "wsize", 0, ALTF_WSIZE, 1 },
122 { "rdirsize", 0, ALTF_RDIRSIZE, 1 },
123 { "maxgrps", 0, ALTF_MAXGRPS, 1 },
124 { "leaseterm", 0, ALTF_LEASETERM, 1 },
125 { "readahead", 0, ALTF_READAHEAD, 1 },
126 { "deadthresh", 0, ALTF_DEADTHRESH, 1 },
127 { "timeo", 0, ALTF_TIMEO, 1 },
128 MOPT_NULL,
129
130 };
131
132 struct nfs_args nfsdefargs = {
133 .version = NFS_ARGSVERSION,
134 .addr = NULL,
135 .addrlen = sizeof(struct sockaddr_in),
136 .sotype = SOCK_DGRAM,
137 .proto = 0,
138 .fh = NULL,
139 .fhsize = 0,
140 .flags = NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
141 .wsize = NFS_WSIZE,
142 .rsize = NFS_RSIZE,
143 .readdirsize = NFS_READDIRSIZE,
144 .timeo = 10,
145 .retrans = NFS_RETRANS,
146 .maxgrouplist = NFS_MAXGRPS,
147 .readahead = NFS_DEFRAHEAD,
148 .leaseterm = 0, /* Ignored; lease term */
149 .deadthresh = NFS_DEFDEADTHRESH,
150 .hostname = NULL,
151 };
152
153 #define DEF_RETRY 10000
154
155 int retrycnt = DEF_RETRY;
156 int opflags = 0;
157 int force2 = 0;
158 int force3 = 0;
159 int mnttcp_ok = 1;
160 int port = 0;
161
162 static void shownfsargs(const struct nfs_args *);
163 int mount_nfs(int argc, char **argv);
164 /* void set_rpc_maxgrouplist(int); */
165 __dead static void usage(void);
166
167 #ifndef MOUNT_NOMAIN
168 int
169 main(int argc, char **argv)
170 {
171
172 setprogname(argv[0]);
173 return mount_nfs(argc, argv);
174 }
175 #endif
176
177 void
178 mount_nfs_dogetargs(struct nfs_args *nfsargsp, int mntflags, const char *spec)
179 {
180 static struct sockaddr_storage sa;
181 char *tspec;
182
183 if ((mntflags & MNT_GETARGS) != 0) {
184 memset(&sa, 0, sizeof(sa));
185 nfsargsp->addr = (struct sockaddr *)&sa;
186 nfsargsp->addrlen = sizeof(sa);
187 } else {
188 if ((tspec = strdup(spec)) == NULL) {
189 err(1, "strdup");
190 }
191 if (!getnfsargs(tspec, nfsargsp)) {
192 exit(1);
193 }
194 free(tspec);
195 }
196 }
197
198 void
199 mount_nfs_parseargs(int argc, char *argv[],
200 struct nfs_args *nfsargsp, int *mntflags,
201 char *spec, char *name)
202 {
203 char *p;
204 int altflags, num;
205 int c;
206 mntoptparse_t mp;
207
208 *mntflags = 0;
209 altflags = 0;
210 memset(nfsargsp, 0, sizeof(*nfsargsp));
211 *nfsargsp = nfsdefargs;
212 while ((c = getopt(argc, argv,
213 "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
214 switch (c) {
215 case '3':
216 case 'q':
217 if (force2)
218 errx(1, "conflicting version options");
219 force3 = 1;
220 break;
221 case '2':
222 if (force3)
223 errx(1, "conflicting version options");
224 force2 = 1;
225 nfsargsp->flags &= ~NFSMNT_NFSV3;
226 break;
227 case 'a':
228 num = strtol(optarg, &p, 10);
229 if (*p || num < 0)
230 errx(1, "illegal -a value -- %s", optarg);
231 nfsargsp->readahead = num;
232 nfsargsp->flags |= NFSMNT_READAHEAD;
233 break;
234 case 'b':
235 opflags |= BGRND;
236 break;
237 case 'c':
238 nfsargsp->flags |= NFSMNT_NOCONN;
239 break;
240 case 'C':
241 nfsargsp->flags &= ~NFSMNT_NOCONN;
242 break;
243 case 'D':
244 num = strtol(optarg, &p, 10);
245 if (*p || num <= 0)
246 errx(1, "illegal -D value -- %s", optarg);
247 nfsargsp->deadthresh = num;
248 nfsargsp->flags |= NFSMNT_DEADTHRESH;
249 break;
250 case 'd':
251 nfsargsp->flags |= NFSMNT_DUMBTIMR;
252 break;
253 #if 0 /* XXXX */
254 case 'g':
255 num = strtol(optarg, &p, 10);
256 if (*p || num <= 0)
257 errx(1, "illegal -g value -- %s", optarg);
258 set_rpc_maxgrouplist(num);
259 nfsargsp->maxgrouplist = num;
260 nfsargsp->flags |= NFSMNT_MAXGRPS;
261 break;
262 #endif
263 case 'I':
264 num = strtol(optarg, &p, 10);
265 if (*p || num <= 0)
266 errx(1, "illegal -I value -- %s", optarg);
267 nfsargsp->readdirsize = num;
268 nfsargsp->flags |= NFSMNT_READDIRSIZE;
269 break;
270 case 'i':
271 nfsargsp->flags |= NFSMNT_INT;
272 break;
273 case 'L':
274 /* ignore */
275 break;
276 case 'l':
277 nfsargsp->flags |= NFSMNT_RDIRPLUS;
278 break;
279 case 'o':
280 mp = getmntopts(optarg, mopts, mntflags, &altflags);
281 if (mp == NULL)
282 err(1, "getmntopts");
283 if (altflags & ALTF_BG)
284 opflags |= BGRND;
285 if (altflags & ALTF_CONN)
286 nfsargsp->flags &= ~NFSMNT_NOCONN;
287 if (altflags & ALTF_DUMBTIMR)
288 nfsargsp->flags |= NFSMNT_DUMBTIMR;
289 if (altflags & ALTF_INTR)
290 nfsargsp->flags |= NFSMNT_INT;
291 if (altflags & (ALTF_NFSV3|ALTF_NQNFS)) {
292 if (force2)
293 errx(1, "conflicting version options");
294 force3 = 1;
295 }
296 if (altflags & ALTF_NFSV2) {
297 if (force3)
298 errx(1, "conflicting version options");
299 force2 = 1;
300 nfsargsp->flags &= ~NFSMNT_NFSV3;
301 }
302 if (altflags & ALTF_RDIRPLUS)
303 nfsargsp->flags |= NFSMNT_RDIRPLUS;
304 if (altflags & ALTF_MNTUDP)
305 mnttcp_ok = 0;
306 if (altflags & ALTF_NORESPORT)
307 nfsargsp->flags &= ~NFSMNT_RESVPORT;
308 if (altflags & ALTF_SOFT)
309 nfsargsp->flags |= NFSMNT_SOFT;
310 if (altflags & ALTF_TCP) {
311 nfsargsp->sotype = SOCK_STREAM;
312 }
313 if (altflags & ALTF_PORT) {
314 port = getmntoptnum(mp, "port");
315 }
316 if (altflags & ALTF_RSIZE) {
317 nfsargsp->rsize =
318 (int)getmntoptnum(mp, "rsize");
319 nfsargsp->flags |= NFSMNT_RSIZE;
320 }
321 if (altflags & ALTF_WSIZE) {
322 nfsargsp->wsize =
323 (int)getmntoptnum(mp, "wsize");
324 nfsargsp->flags |= NFSMNT_WSIZE;
325 }
326 if (altflags & ALTF_RDIRSIZE) {
327 nfsargsp->rsize =
328 (int)getmntoptnum(mp, "rdirsize");
329 nfsargsp->flags |= NFSMNT_READDIRSIZE;
330 }
331 #if 0
332 if (altflags & ALTF_MAXGRPS) {
333 set_rpc_maxgrouplist(num);
334 nfsargsp->maxgrouplist =
335 (int)getmntoptnum(mp, "maxgrps");
336 nfsargsp->flags |= NFSMNT_MAXGRPS;
337 }
338 #endif
339 if (altflags & ALTF_LEASETERM) {
340 nfsargsp->leaseterm =
341 (int)getmntoptnum(mp, "leaseterm");
342 nfsargsp->flags |= NFSMNT_LEASETERM;
343 }
344 if (altflags & ALTF_READAHEAD) {
345 nfsargsp->readahead =
346 (int)getmntoptnum(mp, "readahead");
347 nfsargsp->flags |= NFSMNT_READAHEAD;
348 }
349 if (altflags & ALTF_DEADTHRESH) {
350 nfsargsp->deadthresh =
351 (int)getmntoptnum(mp, "deadthresh");
352 nfsargsp->flags |= NFSMNT_DEADTHRESH;
353 }
354 if (altflags & ALTF_TIMEO) {
355 nfsargsp->timeo =
356 (int)getmntoptnum(mp, "timeo");
357 nfsargsp->flags |= NFSMNT_TIMEO;
358 }
359 if (altflags & ALTF_RETRANS) {
360 nfsargsp->retrans =
361 (int)getmntoptnum(mp, "retrans");
362 nfsargsp->flags |= NFSMNT_RETRANS;
363 }
364 altflags = 0;
365 freemntopts(mp);
366 break;
367 case 'P':
368 nfsargsp->flags |= NFSMNT_RESVPORT;
369 break;
370 case 'p':
371 nfsargsp->flags &= ~NFSMNT_RESVPORT;
372 break;
373 case 'R':
374 num = strtol(optarg, &p, 10);
375 if (*p || num <= 0)
376 errx(1, "illegal -R value -- %s", optarg);
377 retrycnt = num;
378 break;
379 case 'r':
380 num = strtol(optarg, &p, 10);
381 if (*p || num <= 0)
382 errx(1, "illegal -r value -- %s", optarg);
383 nfsargsp->rsize = num;
384 nfsargsp->flags |= NFSMNT_RSIZE;
385 break;
386 case 's':
387 nfsargsp->flags |= NFSMNT_SOFT;
388 break;
389 case 'T':
390 nfsargsp->sotype = SOCK_STREAM;
391 break;
392 case 't':
393 num = strtol(optarg, &p, 10);
394 if (*p || num <= 0)
395 errx(1, "illegal -t value -- %s", optarg);
396 nfsargsp->timeo = num;
397 nfsargsp->flags |= NFSMNT_TIMEO;
398 break;
399 case 'w':
400 num = strtol(optarg, &p, 10);
401 if (*p || num <= 0)
402 errx(1, "illegal -w value -- %s", optarg);
403 nfsargsp->wsize = num;
404 nfsargsp->flags |= NFSMNT_WSIZE;
405 break;
406 case 'x':
407 num = strtol(optarg, &p, 10);
408 if (*p || num <= 0)
409 errx(1, "illegal -x value -- %s", optarg);
410 nfsargsp->retrans = num;
411 nfsargsp->flags |= NFSMNT_RETRANS;
412 break;
413 case 'X':
414 nfsargsp->flags |= NFSMNT_XLATECOOKIE;
415 break;
416 case 'U':
417 mnttcp_ok = 0;
418 break;
419 default:
420 usage();
421 break;
422 }
423 argc -= optind;
424 argv += optind;
425
426 if (argc != 2)
427 usage();
428
429 strlcpy(spec, *argv++, MAXPATHLEN);
430 pathadj(*argv, name);
431 mount_nfs_dogetargs(nfsargsp, *mntflags, spec);
432 }
433
434 int
435 mount_nfs(int argc, char *argv[])
436 {
437 char spec[MAXPATHLEN], name[MAXPATHLEN];
438 struct nfs_args args;
439 int mntflags;
440 int retval;
441
442 mount_nfs_parseargs(argc, argv, &args, &mntflags, spec, name);
443
444 retry:
445 if ((retval = mount(MOUNT_NFS, name, mntflags,
446 &args, sizeof args)) == -1) {
447 /* Did we just default to v3 on a v2-only kernel?
448 * If so, default to v2 & try again */
449 if (errno == EPROGMISMATCH &&
450 (args.flags & NFSMNT_NFSV3) != 0 && !force3) {
451 /*
452 * fall back to v2. XXX lack of V3 umount.
453 */
454 args.flags &= ~NFSMNT_NFSV3;
455 mount_nfs_dogetargs(&args, mntflags, spec);
456 goto retry;
457 }
458 }
459 if (retval == -1)
460 err(1, "%s on %s", spec, name);
461 if (mntflags & MNT_GETARGS) {
462 shownfsargs(&args);
463 return (0);
464 }
465
466 exit(0);
467 }
468
469 static void
470 shownfsargs(const struct nfs_args *nfsargsp)
471 {
472 char fbuf[2048];
473 char host[NI_MAXHOST], serv[NI_MAXSERV];
474 int error;
475
476 (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
477 if (nfsargsp->addr != NULL) {
478 error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
479 sizeof(host), serv, sizeof(serv),
480 NI_NUMERICHOST | NI_NUMERICSERV);
481 if (error != 0)
482 warnx("getnameinfo: %s", gai_strerror(error));
483 } else
484 error = -1;
485
486 if (error == 0)
487 printf("addr=%s, port=%s, addrlen=%d, ",
488 host, serv, nfsargsp->addrlen);
489 printf("sotype=%d, proto=%d, fhsize=%d, "
490 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
491 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
492 "deadthresh=%d\n",
493 nfsargsp->sotype,
494 nfsargsp->proto,
495 nfsargsp->fhsize,
496 fbuf,
497 nfsargsp->wsize,
498 nfsargsp->rsize,
499 nfsargsp->readdirsize,
500 nfsargsp->timeo,
501 nfsargsp->retrans,
502 nfsargsp->maxgrouplist,
503 nfsargsp->readahead,
504 nfsargsp->leaseterm,
505 nfsargsp->deadthresh);
506 }
507
508 static void
509 usage(void)
510 {
511 (void)fprintf(stderr, "usage: %s %s\n%s\n%s\n%s\n%s\n", getprogname(),
512 "[-23bCcdilPpqsTUX] [-a maxreadahead] [-D deadthresh]",
513 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm]",
514 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
515 "\t[-w writesize] [-x retrans]",
516 "\trhost:path node");
517 exit(1);
518 }
519