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