mount_nfs.c revision 1.62 1 /* $NetBSD: mount_nfs.c,v 1.62 2008/07/20 01:20:22 lukem 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.62 2008/07/20 01:20:22 lukem 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 #ifdef ISO
56 #include <netiso/iso.h>
57 #endif
58
59 #include <nfs/rpcv2.h>
60 #include <nfs/nfsproto.h>
61 #include <nfs/nfs.h>
62 #include <nfs/nfsmount.h>
63
64 #include <arpa/inet.h>
65
66 #include <err.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <netdb.h>
70 #include <signal.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <util.h>
76
77 #include <mntopts.h>
78
79 #include "mount_nfs.h"
80
81 #define ALTF_BG 0x00000001
82 #define ALTF_CONN 0x00000002
83 #define ALTF_DUMBTIMR 0x00000004
84 #define ALTF_INTR 0x00000008
85 #define ALTF_NFSV3 0x00000020
86 #define ALTF_RDIRPLUS 0x00000040
87 #define ALTF_MNTUDP 0x00000080
88 #define ALTF_NORESPORT 0x00000100
89 #define ALTF_SEQPACKET 0x00000200
90 #define ALTF_NQNFS 0x00000400
91 #define ALTF_SOFT 0x00000800
92 #define ALTF_TCP 0x00001000
93 #define ALTF_NFSV2 0x00002000
94 #define ALTF_PORT 0x00004000
95 #define ALTF_RSIZE 0x00008000
96 #define ALTF_WSIZE 0x00010000
97 #define ALTF_RDIRSIZE 0x00020000
98 #define ALTF_MAXGRPS 0x00040000
99 #define ALTF_LEASETERM 0x00080000
100 #define ALTF_READAHEAD 0x00100000
101 #define ALTF_DEADTHRESH 0x00200000
102 #define ALTF_TIMEO 0x00400000
103 #define ALTF_RETRANS 0x00800000
104
105 static const struct mntopt mopts[] = {
106 MOPT_STDOPTS,
107 MOPT_FORCE,
108 MOPT_UPDATE,
109 MOPT_GETARGS,
110 { "bg", 0, ALTF_BG, 1 },
111 { "conn", 0, ALTF_CONN, 1 },
112 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
113 { "intr", 0, ALTF_INTR, 1 },
114 { "nfsv3", 0, ALTF_NFSV3, 1 },
115 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
116 { "mntudp", 0, ALTF_MNTUDP, 1 },
117 { "noresport", 0, ALTF_NORESPORT, 1 },
118 #ifdef ISO
119 { "seqpacket", 0, ALTF_SEQPACKET, 1 },
120 #endif
121 { "nqnfs", 0, ALTF_NQNFS, 1 },
122 { "soft", 0, ALTF_SOFT, 1 },
123 { "tcp", 0, ALTF_TCP, 1 },
124 { "nfsv2", 0, ALTF_NFSV2, 1 },
125 { "port", 0, ALTF_PORT, 1 },
126 { "rsize", 0, ALTF_RSIZE, 1 },
127 { "wsize", 0, ALTF_WSIZE, 1 },
128 { "rdirsize", 0, ALTF_RDIRSIZE, 1 },
129 { "maxgrps", 0, ALTF_MAXGRPS, 1 },
130 { "leaseterm", 0, ALTF_LEASETERM, 1 },
131 { "readahead", 0, ALTF_READAHEAD, 1 },
132 { "deadthresh", 0, ALTF_DEADTHRESH, 1 },
133 { "timeo", 0, ALTF_TIMEO, 1 },
134 MOPT_NULL,
135
136 };
137
138 struct nfs_args nfsdefargs = {
139 NFS_ARGSVERSION,
140 (struct sockaddr *)0,
141 sizeof (struct sockaddr_in),
142 SOCK_DGRAM,
143 0,
144 (u_char *)0,
145 0,
146 NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
147 NFS_WSIZE,
148 NFS_RSIZE,
149 NFS_READDIRSIZE,
150 10,
151 NFS_RETRANS,
152 NFS_MAXGRPS,
153 NFS_DEFRAHEAD,
154 0, /* Ignored; lease term */
155 NFS_DEFDEADTHRESH,
156 (char *)0,
157 };
158
159 int retrycnt = 0;
160 int opflags = 0;
161 int force2 = 0;
162 int force3 = 0;
163 int mnttcp_ok = 1;
164 int port = 0;
165
166 static void shownfsargs(const struct nfs_args *);
167 #ifdef ISO
168 static struct iso_addr *iso_addr(const char *);
169 #endif
170 int mount_nfs(int argc, char **argv);
171 /* void set_rpc_maxgrouplist(int); */
172 static void usage(void);
173
174 #ifndef MOUNT_NOMAIN
175 int
176 main(int argc, char **argv)
177 {
178 return mount_nfs(argc, argv);
179 }
180 #endif
181
182 int
183 mount_nfs(int argc, char *argv[])
184 {
185 int c, retval;
186 struct nfs_args *nfsargsp;
187 struct nfs_args nfsargs;
188 struct sockaddr_storage sa;
189 int mntflags, altflags, num;
190 char name[MAXPATHLEN], *p, *spec;
191 mntoptparse_t mp;
192 retrycnt = DEF_RETRY;
193
194 mntflags = 0;
195 altflags = 0;
196 nfsargs = nfsdefargs;
197 nfsargsp = &nfsargs;
198 while ((c = getopt(argc, argv,
199 "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
200 switch (c) {
201 case '3':
202 case 'q':
203 if (force2)
204 errx(1, "conflicting version options");
205 force3 = 1;
206 break;
207 case '2':
208 if (force3)
209 errx(1, "conflicting version options");
210 force2 = 1;
211 nfsargsp->flags &= ~NFSMNT_NFSV3;
212 break;
213 case 'a':
214 num = strtol(optarg, &p, 10);
215 if (*p || num < 0)
216 errx(1, "illegal -a value -- %s", optarg);
217 nfsargsp->readahead = num;
218 nfsargsp->flags |= NFSMNT_READAHEAD;
219 break;
220 case 'b':
221 opflags |= BGRND;
222 break;
223 case 'c':
224 nfsargsp->flags |= NFSMNT_NOCONN;
225 break;
226 case 'C':
227 nfsargsp->flags &= ~NFSMNT_NOCONN;
228 break;
229 case 'D':
230 num = strtol(optarg, &p, 10);
231 if (*p || num <= 0)
232 errx(1, "illegal -D value -- %s", optarg);
233 nfsargsp->deadthresh = num;
234 nfsargsp->flags |= NFSMNT_DEADTHRESH;
235 break;
236 case 'd':
237 nfsargsp->flags |= NFSMNT_DUMBTIMR;
238 break;
239 #if 0 /* XXXX */
240 case 'g':
241 num = strtol(optarg, &p, 10);
242 if (*p || num <= 0)
243 errx(1, "illegal -g value -- %s", optarg);
244 set_rpc_maxgrouplist(num);
245 nfsargsp->maxgrouplist = num;
246 nfsargsp->flags |= NFSMNT_MAXGRPS;
247 break;
248 #endif
249 case 'I':
250 num = strtol(optarg, &p, 10);
251 if (*p || num <= 0)
252 errx(1, "illegal -I value -- %s", optarg);
253 nfsargsp->readdirsize = num;
254 nfsargsp->flags |= NFSMNT_READDIRSIZE;
255 break;
256 case 'i':
257 nfsargsp->flags |= NFSMNT_INT;
258 break;
259 case 'L':
260 /* ignore */
261 break;
262 case 'l':
263 nfsargsp->flags |= NFSMNT_RDIRPLUS;
264 break;
265 case 'o':
266 mp = getmntopts(optarg, mopts, &mntflags, &altflags);
267 if (mp == NULL)
268 err(1, "getmntopts");
269 if (altflags & ALTF_BG)
270 opflags |= BGRND;
271 if (altflags & ALTF_CONN)
272 nfsargsp->flags &= ~NFSMNT_NOCONN;
273 if (altflags & ALTF_DUMBTIMR)
274 nfsargsp->flags |= NFSMNT_DUMBTIMR;
275 if (altflags & ALTF_INTR)
276 nfsargsp->flags |= NFSMNT_INT;
277 if (altflags & (ALTF_NFSV3|ALTF_NQNFS)) {
278 if (force2)
279 errx(1, "conflicting version options");
280 force3 = 1;
281 }
282 if (altflags & ALTF_NFSV2) {
283 if (force3)
284 errx(1, "conflicting version options");
285 force2 = 1;
286 nfsargsp->flags &= ~NFSMNT_NFSV3;
287 }
288 if (altflags & ALTF_RDIRPLUS)
289 nfsargsp->flags |= NFSMNT_RDIRPLUS;
290 if (altflags & ALTF_MNTUDP)
291 mnttcp_ok = 0;
292 if (altflags & ALTF_NORESPORT)
293 nfsargsp->flags &= ~NFSMNT_RESVPORT;
294 #ifdef ISO
295 if (altflags & ALTF_SEQPACKET)
296 nfsargsp->sotype = SOCK_SEQPACKET;
297 #endif
298 if (altflags & ALTF_SOFT)
299 nfsargsp->flags |= NFSMNT_SOFT;
300 if (altflags & ALTF_TCP) {
301 nfsargsp->sotype = SOCK_STREAM;
302 }
303 if (altflags & ALTF_PORT) {
304 port = getmntoptnum(mp, "port");
305 }
306 if (altflags & ALTF_RSIZE) {
307 nfsargsp->rsize =
308 (int)getmntoptnum(mp, "rsize");
309 nfsargsp->flags |= NFSMNT_RSIZE;
310 }
311 if (altflags & ALTF_WSIZE) {
312 nfsargsp->wsize =
313 (int)getmntoptnum(mp, "wsize");
314 nfsargsp->flags |= NFSMNT_WSIZE;
315 }
316 if (altflags & ALTF_RDIRSIZE) {
317 nfsargsp->rsize =
318 (int)getmntoptnum(mp, "rdirsize");
319 nfsargsp->flags |= NFSMNT_READDIRSIZE;
320 }
321 #if 0
322 if (altflags & ALTF_MAXGRPS) {
323 set_rpc_maxgrouplist(num);
324 nfsargsp->maxgrouplist =
325 (int)getmntoptnum(mp, "maxgrps");
326 nfsargsp->flags |= NFSMNT_MAXGRPS;
327 }
328 #endif
329 if (altflags & ALTF_LEASETERM) {
330 nfsargsp->leaseterm =
331 (int)getmntoptnum(mp, "leaseterm");
332 nfsargsp->flags |= NFSMNT_LEASETERM;
333 }
334 if (altflags & ALTF_READAHEAD) {
335 nfsargsp->readahead =
336 (int)getmntoptnum(mp, "readahead");
337 nfsargsp->flags |= NFSMNT_READAHEAD;
338 }
339 if (altflags & ALTF_DEADTHRESH) {
340 nfsargsp->deadthresh =
341 (int)getmntoptnum(mp, "deadthresh");
342 nfsargsp->flags |= NFSMNT_DEADTHRESH;
343 }
344 if (altflags & ALTF_TIMEO) {
345 nfsargsp->timeo =
346 (int)getmntoptnum(mp, "timeo");
347 nfsargsp->flags |= NFSMNT_TIMEO;
348 }
349 if (altflags & ALTF_RETRANS) {
350 nfsargsp->retrans =
351 (int)getmntoptnum(mp, "retrans");
352 nfsargsp->flags |= NFSMNT_RETRANS;
353 }
354 altflags = 0;
355 freemntopts(mp);
356 break;
357 case 'P':
358 nfsargsp->flags |= NFSMNT_RESVPORT;
359 break;
360 case 'p':
361 nfsargsp->flags &= ~NFSMNT_RESVPORT;
362 break;
363 case 'R':
364 num = strtol(optarg, &p, 10);
365 if (*p || num <= 0)
366 errx(1, "illegal -R value -- %s", optarg);
367 retrycnt = num;
368 break;
369 case 'r':
370 num = strtol(optarg, &p, 10);
371 if (*p || num <= 0)
372 errx(1, "illegal -r value -- %s", optarg);
373 nfsargsp->rsize = num;
374 nfsargsp->flags |= NFSMNT_RSIZE;
375 break;
376 #ifdef ISO
377 case 'S':
378 nfsargsp->sotype = SOCK_SEQPACKET;
379 break;
380 #endif
381 case 's':
382 nfsargsp->flags |= NFSMNT_SOFT;
383 break;
384 case 'T':
385 nfsargsp->sotype = SOCK_STREAM;
386 break;
387 case 't':
388 num = strtol(optarg, &p, 10);
389 if (*p || num <= 0)
390 errx(1, "illegal -t value -- %s", optarg);
391 nfsargsp->timeo = num;
392 nfsargsp->flags |= NFSMNT_TIMEO;
393 break;
394 case 'w':
395 num = strtol(optarg, &p, 10);
396 if (*p || num <= 0)
397 errx(1, "illegal -w value -- %s", optarg);
398 nfsargsp->wsize = num;
399 nfsargsp->flags |= NFSMNT_WSIZE;
400 break;
401 case 'x':
402 num = strtol(optarg, &p, 10);
403 if (*p || num <= 0)
404 errx(1, "illegal -x value -- %s", optarg);
405 nfsargsp->retrans = num;
406 nfsargsp->flags |= NFSMNT_RETRANS;
407 break;
408 case 'X':
409 nfsargsp->flags |= NFSMNT_XLATECOOKIE;
410 break;
411 case 'U':
412 mnttcp_ok = 0;
413 break;
414 default:
415 usage();
416 break;
417 }
418 argc -= optind;
419 argv += optind;
420
421 if (argc != 2)
422 usage();
423
424 spec = *argv++;
425 if (realpath(*argv, name) == NULL) /* Check mounton path */
426 err(1, "realpath %s", *argv);
427 if (strncmp(*argv, name, MAXPATHLEN)) {
428 warnx("\"%s\" is a relative path.", *argv);
429 warnx("using \"%s\" instead.", name);
430 }
431
432 retry:
433 if ((mntflags & MNT_GETARGS) != 0) {
434 memset(&sa, 0, sizeof(sa));
435 nfsargsp->addr = (struct sockaddr *)&sa;
436 nfsargsp->addrlen = sizeof(sa);
437 } else {
438 char *tspec;
439
440 if ((tspec = strdup(spec)) == NULL) {
441 err(1, "strdup");
442 }
443 if (!getnfsargs(tspec, nfsargsp)) {
444 exit(1);
445 }
446 free(tspec);
447 }
448 if ((retval = mount(MOUNT_NFS, name, mntflags,
449 nfsargsp, sizeof *nfsargsp)) == -1) {
450 /* Did we just default to v3 on a v2-only kernel?
451 * If so, default to v2 & try again */
452 if (errno == EPROGMISMATCH &&
453 (nfsargsp->flags & NFSMNT_NFSV3) != 0 && !force3) {
454 /*
455 * fall back to v2. XXX lack of V3 umount.
456 */
457 nfsargsp->flags &= ~NFSMNT_NFSV3;
458 goto retry;
459 }
460 }
461 if (retval == -1)
462 err(1, "%s on %s", spec, name);
463 if (mntflags & MNT_GETARGS) {
464 shownfsargs(nfsargsp);
465 return (0);
466 }
467
468 exit(0);
469 }
470
471 static void
472 shownfsargs(const struct nfs_args *nfsargsp)
473 {
474 char fbuf[2048];
475 char host[NI_MAXHOST], serv[NI_MAXSERV];
476 int error;
477
478 (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
479 if (nfsargsp->addr != NULL) {
480 error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
481 sizeof(host), serv, sizeof(serv),
482 NI_NUMERICHOST | NI_NUMERICSERV);
483 if (error != 0)
484 warnx("getnameinfo: %s", gai_strerror(error));
485 } else
486 error = -1;
487
488 if (error == 0)
489 printf("addr=%s, port=%s, addrlen=%d, ",
490 host, serv, nfsargsp->addrlen);
491 printf("sotype=%d, proto=%d, fhsize=%d, "
492 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
493 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
494 "deadthresh=%d\n",
495 nfsargsp->sotype,
496 nfsargsp->proto,
497 nfsargsp->fhsize,
498 fbuf,
499 nfsargsp->wsize,
500 nfsargsp->rsize,
501 nfsargsp->readdirsize,
502 nfsargsp->timeo,
503 nfsargsp->retrans,
504 nfsargsp->maxgrouplist,
505 nfsargsp->readahead,
506 nfsargsp->leaseterm,
507 nfsargsp->deadthresh);
508 }
509
510 static void
511 usage(void)
512 {
513 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
514 "[-23bCcdilPpqsTUX] [-a maxreadahead] [-D deadthresh]",
515 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm]",
516 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
517 "\t[-w writesize] [-x retrans]",
518 "\trhost:path node");
519 exit(1);
520 }
521