mount_nfs.c revision 1.63 1 /* $NetBSD: mount_nfs.c,v 1.63 2008/09/27 20:39:42 pooka 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.63 2008/09/27 20:39:42 pooka 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 = DEF_RETRY;
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
193 mntflags = 0;
194 altflags = 0;
195 nfsargs = nfsdefargs;
196 nfsargsp = &nfsargs;
197 while ((c = getopt(argc, argv,
198 "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
199 switch (c) {
200 case '3':
201 case 'q':
202 if (force2)
203 errx(1, "conflicting version options");
204 force3 = 1;
205 break;
206 case '2':
207 if (force3)
208 errx(1, "conflicting version options");
209 force2 = 1;
210 nfsargsp->flags &= ~NFSMNT_NFSV3;
211 break;
212 case 'a':
213 num = strtol(optarg, &p, 10);
214 if (*p || num < 0)
215 errx(1, "illegal -a value -- %s", optarg);
216 nfsargsp->readahead = num;
217 nfsargsp->flags |= NFSMNT_READAHEAD;
218 break;
219 case 'b':
220 opflags |= BGRND;
221 break;
222 case 'c':
223 nfsargsp->flags |= NFSMNT_NOCONN;
224 break;
225 case 'C':
226 nfsargsp->flags &= ~NFSMNT_NOCONN;
227 break;
228 case 'D':
229 num = strtol(optarg, &p, 10);
230 if (*p || num <= 0)
231 errx(1, "illegal -D value -- %s", optarg);
232 nfsargsp->deadthresh = num;
233 nfsargsp->flags |= NFSMNT_DEADTHRESH;
234 break;
235 case 'd':
236 nfsargsp->flags |= NFSMNT_DUMBTIMR;
237 break;
238 #if 0 /* XXXX */
239 case 'g':
240 num = strtol(optarg, &p, 10);
241 if (*p || num <= 0)
242 errx(1, "illegal -g value -- %s", optarg);
243 set_rpc_maxgrouplist(num);
244 nfsargsp->maxgrouplist = num;
245 nfsargsp->flags |= NFSMNT_MAXGRPS;
246 break;
247 #endif
248 case 'I':
249 num = strtol(optarg, &p, 10);
250 if (*p || num <= 0)
251 errx(1, "illegal -I value -- %s", optarg);
252 nfsargsp->readdirsize = num;
253 nfsargsp->flags |= NFSMNT_READDIRSIZE;
254 break;
255 case 'i':
256 nfsargsp->flags |= NFSMNT_INT;
257 break;
258 case 'L':
259 /* ignore */
260 break;
261 case 'l':
262 nfsargsp->flags |= NFSMNT_RDIRPLUS;
263 break;
264 case 'o':
265 mp = getmntopts(optarg, mopts, &mntflags, &altflags);
266 if (mp == NULL)
267 err(1, "getmntopts");
268 if (altflags & ALTF_BG)
269 opflags |= BGRND;
270 if (altflags & ALTF_CONN)
271 nfsargsp->flags &= ~NFSMNT_NOCONN;
272 if (altflags & ALTF_DUMBTIMR)
273 nfsargsp->flags |= NFSMNT_DUMBTIMR;
274 if (altflags & ALTF_INTR)
275 nfsargsp->flags |= NFSMNT_INT;
276 if (altflags & (ALTF_NFSV3|ALTF_NQNFS)) {
277 if (force2)
278 errx(1, "conflicting version options");
279 force3 = 1;
280 }
281 if (altflags & ALTF_NFSV2) {
282 if (force3)
283 errx(1, "conflicting version options");
284 force2 = 1;
285 nfsargsp->flags &= ~NFSMNT_NFSV3;
286 }
287 if (altflags & ALTF_RDIRPLUS)
288 nfsargsp->flags |= NFSMNT_RDIRPLUS;
289 if (altflags & ALTF_MNTUDP)
290 mnttcp_ok = 0;
291 if (altflags & ALTF_NORESPORT)
292 nfsargsp->flags &= ~NFSMNT_RESVPORT;
293 #ifdef ISO
294 if (altflags & ALTF_SEQPACKET)
295 nfsargsp->sotype = SOCK_SEQPACKET;
296 #endif
297 if (altflags & ALTF_SOFT)
298 nfsargsp->flags |= NFSMNT_SOFT;
299 if (altflags & ALTF_TCP) {
300 nfsargsp->sotype = SOCK_STREAM;
301 }
302 if (altflags & ALTF_PORT) {
303 port = getmntoptnum(mp, "port");
304 }
305 if (altflags & ALTF_RSIZE) {
306 nfsargsp->rsize =
307 (int)getmntoptnum(mp, "rsize");
308 nfsargsp->flags |= NFSMNT_RSIZE;
309 }
310 if (altflags & ALTF_WSIZE) {
311 nfsargsp->wsize =
312 (int)getmntoptnum(mp, "wsize");
313 nfsargsp->flags |= NFSMNT_WSIZE;
314 }
315 if (altflags & ALTF_RDIRSIZE) {
316 nfsargsp->rsize =
317 (int)getmntoptnum(mp, "rdirsize");
318 nfsargsp->flags |= NFSMNT_READDIRSIZE;
319 }
320 #if 0
321 if (altflags & ALTF_MAXGRPS) {
322 set_rpc_maxgrouplist(num);
323 nfsargsp->maxgrouplist =
324 (int)getmntoptnum(mp, "maxgrps");
325 nfsargsp->flags |= NFSMNT_MAXGRPS;
326 }
327 #endif
328 if (altflags & ALTF_LEASETERM) {
329 nfsargsp->leaseterm =
330 (int)getmntoptnum(mp, "leaseterm");
331 nfsargsp->flags |= NFSMNT_LEASETERM;
332 }
333 if (altflags & ALTF_READAHEAD) {
334 nfsargsp->readahead =
335 (int)getmntoptnum(mp, "readahead");
336 nfsargsp->flags |= NFSMNT_READAHEAD;
337 }
338 if (altflags & ALTF_DEADTHRESH) {
339 nfsargsp->deadthresh =
340 (int)getmntoptnum(mp, "deadthresh");
341 nfsargsp->flags |= NFSMNT_DEADTHRESH;
342 }
343 if (altflags & ALTF_TIMEO) {
344 nfsargsp->timeo =
345 (int)getmntoptnum(mp, "timeo");
346 nfsargsp->flags |= NFSMNT_TIMEO;
347 }
348 if (altflags & ALTF_RETRANS) {
349 nfsargsp->retrans =
350 (int)getmntoptnum(mp, "retrans");
351 nfsargsp->flags |= NFSMNT_RETRANS;
352 }
353 altflags = 0;
354 freemntopts(mp);
355 break;
356 case 'P':
357 nfsargsp->flags |= NFSMNT_RESVPORT;
358 break;
359 case 'p':
360 nfsargsp->flags &= ~NFSMNT_RESVPORT;
361 break;
362 case 'R':
363 num = strtol(optarg, &p, 10);
364 if (*p || num <= 0)
365 errx(1, "illegal -R value -- %s", optarg);
366 retrycnt = num;
367 break;
368 case 'r':
369 num = strtol(optarg, &p, 10);
370 if (*p || num <= 0)
371 errx(1, "illegal -r value -- %s", optarg);
372 nfsargsp->rsize = num;
373 nfsargsp->flags |= NFSMNT_RSIZE;
374 break;
375 #ifdef ISO
376 case 'S':
377 nfsargsp->sotype = SOCK_SEQPACKET;
378 break;
379 #endif
380 case 's':
381 nfsargsp->flags |= NFSMNT_SOFT;
382 break;
383 case 'T':
384 nfsargsp->sotype = SOCK_STREAM;
385 break;
386 case 't':
387 num = strtol(optarg, &p, 10);
388 if (*p || num <= 0)
389 errx(1, "illegal -t value -- %s", optarg);
390 nfsargsp->timeo = num;
391 nfsargsp->flags |= NFSMNT_TIMEO;
392 break;
393 case 'w':
394 num = strtol(optarg, &p, 10);
395 if (*p || num <= 0)
396 errx(1, "illegal -w value -- %s", optarg);
397 nfsargsp->wsize = num;
398 nfsargsp->flags |= NFSMNT_WSIZE;
399 break;
400 case 'x':
401 num = strtol(optarg, &p, 10);
402 if (*p || num <= 0)
403 errx(1, "illegal -x value -- %s", optarg);
404 nfsargsp->retrans = num;
405 nfsargsp->flags |= NFSMNT_RETRANS;
406 break;
407 case 'X':
408 nfsargsp->flags |= NFSMNT_XLATECOOKIE;
409 break;
410 case 'U':
411 mnttcp_ok = 0;
412 break;
413 default:
414 usage();
415 break;
416 }
417 argc -= optind;
418 argv += optind;
419
420 if (argc != 2)
421 usage();
422
423 spec = *argv++;
424 if (realpath(*argv, name) == NULL) /* Check mounton path */
425 err(1, "realpath %s", *argv);
426 if (strncmp(*argv, name, MAXPATHLEN)) {
427 warnx("\"%s\" is a relative path.", *argv);
428 warnx("using \"%s\" instead.", name);
429 }
430
431 retry:
432 if ((mntflags & MNT_GETARGS) != 0) {
433 memset(&sa, 0, sizeof(sa));
434 nfsargsp->addr = (struct sockaddr *)&sa;
435 nfsargsp->addrlen = sizeof(sa);
436 } else {
437 char *tspec;
438
439 if ((tspec = strdup(spec)) == NULL) {
440 err(1, "strdup");
441 }
442 if (!getnfsargs(tspec, nfsargsp)) {
443 exit(1);
444 }
445 free(tspec);
446 }
447 if ((retval = mount(MOUNT_NFS, name, mntflags,
448 nfsargsp, sizeof *nfsargsp)) == -1) {
449 /* Did we just default to v3 on a v2-only kernel?
450 * If so, default to v2 & try again */
451 if (errno == EPROGMISMATCH &&
452 (nfsargsp->flags & NFSMNT_NFSV3) != 0 && !force3) {
453 /*
454 * fall back to v2. XXX lack of V3 umount.
455 */
456 nfsargsp->flags &= ~NFSMNT_NFSV3;
457 goto retry;
458 }
459 }
460 if (retval == -1)
461 err(1, "%s on %s", spec, name);
462 if (mntflags & MNT_GETARGS) {
463 shownfsargs(nfsargsp);
464 return (0);
465 }
466
467 exit(0);
468 }
469
470 static void
471 shownfsargs(const struct nfs_args *nfsargsp)
472 {
473 char fbuf[2048];
474 char host[NI_MAXHOST], serv[NI_MAXSERV];
475 int error;
476
477 (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
478 if (nfsargsp->addr != NULL) {
479 error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
480 sizeof(host), serv, sizeof(serv),
481 NI_NUMERICHOST | NI_NUMERICSERV);
482 if (error != 0)
483 warnx("getnameinfo: %s", gai_strerror(error));
484 } else
485 error = -1;
486
487 if (error == 0)
488 printf("addr=%s, port=%s, addrlen=%d, ",
489 host, serv, nfsargsp->addrlen);
490 printf("sotype=%d, proto=%d, fhsize=%d, "
491 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
492 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
493 "deadthresh=%d\n",
494 nfsargsp->sotype,
495 nfsargsp->proto,
496 nfsargsp->fhsize,
497 fbuf,
498 nfsargsp->wsize,
499 nfsargsp->rsize,
500 nfsargsp->readdirsize,
501 nfsargsp->timeo,
502 nfsargsp->retrans,
503 nfsargsp->maxgrouplist,
504 nfsargsp->readahead,
505 nfsargsp->leaseterm,
506 nfsargsp->deadthresh);
507 }
508
509 static void
510 usage(void)
511 {
512 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
513 "[-23bCcdilPpqsTUX] [-a maxreadahead] [-D deadthresh]",
514 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm]",
515 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
516 "\t[-w writesize] [-x retrans]",
517 "\trhost:path node");
518 exit(1);
519 }
520