1 /* $NetBSD: mount_nfs.c,v 1.67 2009/04/03 07:26:07 pooka Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: mount_nfs.c,v 1.67 2009/04/03 07:26:07 pooka Exp $");
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
56 #include <netiso/iso.h>
59 #include <nfs/rpcv2.h>
60 #include <nfs/nfsproto.h>
62 #include <nfs/nfsmount.h>
64 #include <arpa/inet.h>
79 #include "mountprog.h"
80 #include "mount_nfs.h"
82 #define ALTF_BG 0x00000001
83 #define ALTF_CONN 0x00000002
84 #define ALTF_DUMBTIMR 0x00000004
85 #define ALTF_INTR 0x00000008
86 #define ALTF_NFSV3 0x00000020
87 #define ALTF_RDIRPLUS 0x00000040
88 #define ALTF_MNTUDP 0x00000080
89 #define ALTF_NORESPORT 0x00000100
90 #define ALTF_SEQPACKET 0x00000200
91 #define ALTF_NQNFS 0x00000400
92 #define ALTF_SOFT 0x00000800
93 #define ALTF_TCP 0x00001000
94 #define ALTF_NFSV2 0x00002000
95 #define ALTF_PORT 0x00004000
96 #define ALTF_RSIZE 0x00008000
97 #define ALTF_WSIZE 0x00010000
98 #define ALTF_RDIRSIZE 0x00020000
99 #define ALTF_MAXGRPS 0x00040000
100 #define ALTF_LEASETERM 0x00080000
101 #define ALTF_READAHEAD 0x00100000
102 #define ALTF_DEADTHRESH 0x00200000
103 #define ALTF_TIMEO 0x00400000
104 #define ALTF_RETRANS 0x00800000
106 static const struct mntopt mopts
[] = {
111 { "bg", 0, ALTF_BG
, 1 },
112 { "conn", 0, ALTF_CONN
, 1 },
113 { "dumbtimer", 0, ALTF_DUMBTIMR
, 1 },
114 { "intr", 0, ALTF_INTR
, 1 },
115 { "nfsv3", 0, ALTF_NFSV3
, 1 },
116 { "rdirplus", 0, ALTF_RDIRPLUS
, 1 },
117 { "mntudp", 0, ALTF_MNTUDP
, 1 },
118 { "resport", 1, ALTF_NORESPORT
, 1 },
120 { "seqpacket", 0, ALTF_SEQPACKET
, 1 },
122 { "nqnfs", 0, ALTF_NQNFS
, 1 },
123 { "soft", 0, ALTF_SOFT
, 1 },
124 { "tcp", 0, ALTF_TCP
, 1 },
125 { "nfsv2", 0, ALTF_NFSV2
, 1 },
126 { "port", 0, ALTF_PORT
, 1 },
127 { "rsize", 0, ALTF_RSIZE
, 1 },
128 { "wsize", 0, ALTF_WSIZE
, 1 },
129 { "rdirsize", 0, ALTF_RDIRSIZE
, 1 },
130 { "maxgrps", 0, ALTF_MAXGRPS
, 1 },
131 { "leaseterm", 0, ALTF_LEASETERM
, 1 },
132 { "readahead", 0, ALTF_READAHEAD
, 1 },
133 { "deadthresh", 0, ALTF_DEADTHRESH
, 1 },
134 { "timeo", 0, ALTF_TIMEO
, 1 },
139 struct nfs_args nfsdefargs
= {
140 .version
= NFS_ARGSVERSION
,
142 .addrlen
= sizeof(struct sockaddr_in
),
143 .sotype
= SOCK_DGRAM
,
147 .flags
= NFSMNT_NFSV3
|NFSMNT_NOCONN
|NFSMNT_RESVPORT
,
150 .readdirsize
= NFS_READDIRSIZE
,
152 .retrans
= NFS_RETRANS
,
153 .maxgrouplist
= NFS_MAXGRPS
,
154 .readahead
= NFS_DEFRAHEAD
,
155 .leaseterm
= 0, /* Ignored; lease term */
156 .deadthresh
= NFS_DEFDEADTHRESH
,
160 #define DEF_RETRY 10000
162 int retrycnt
= DEF_RETRY
;
169 static void shownfsargs(const struct nfs_args
*);
171 static struct iso_addr
*iso_addr(const char *);
173 int mount_nfs(int argc
, char **argv
);
174 /* void set_rpc_maxgrouplist(int); */
175 static void usage(void);
179 main(int argc
, char **argv
)
182 setprogname(argv
[0]);
183 return mount_nfs(argc
, argv
);
188 mount_nfs_dogetargs(struct nfs_args
*nfsargsp
, int mntflags
, const char *spec
)
190 static struct sockaddr_storage sa
;
193 if ((mntflags
& MNT_GETARGS
) != 0) {
194 memset(&sa
, 0, sizeof(sa
));
195 nfsargsp
->addr
= (struct sockaddr
*)&sa
;
196 nfsargsp
->addrlen
= sizeof(sa
);
198 if ((tspec
= strdup(spec
)) == NULL
) {
201 if (!getnfsargs(tspec
, nfsargsp
)) {
209 mount_nfs_parseargs(int argc
, char *argv
[],
210 struct nfs_args
*nfsargsp
, int *mntflags
,
211 char *spec
, char *name
)
220 memset(nfsargsp
, 0, sizeof(*nfsargsp
));
221 *nfsargsp
= nfsdefargs
;
222 while ((c
= getopt(argc
, argv
,
223 "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
228 errx(1, "conflicting version options");
233 errx(1, "conflicting version options");
235 nfsargsp
->flags
&= ~NFSMNT_NFSV3
;
238 num
= strtol(optarg
, &p
, 10);
240 errx(1, "illegal -a value -- %s", optarg
);
241 nfsargsp
->readahead
= num
;
242 nfsargsp
->flags
|= NFSMNT_READAHEAD
;
248 nfsargsp
->flags
|= NFSMNT_NOCONN
;
251 nfsargsp
->flags
&= ~NFSMNT_NOCONN
;
254 num
= strtol(optarg
, &p
, 10);
256 errx(1, "illegal -D value -- %s", optarg
);
257 nfsargsp
->deadthresh
= num
;
258 nfsargsp
->flags
|= NFSMNT_DEADTHRESH
;
261 nfsargsp
->flags
|= NFSMNT_DUMBTIMR
;
265 num
= strtol(optarg
, &p
, 10);
267 errx(1, "illegal -g value -- %s", optarg
);
268 set_rpc_maxgrouplist(num
);
269 nfsargsp
->maxgrouplist
= num
;
270 nfsargsp
->flags
|= NFSMNT_MAXGRPS
;
274 num
= strtol(optarg
, &p
, 10);
276 errx(1, "illegal -I value -- %s", optarg
);
277 nfsargsp
->readdirsize
= num
;
278 nfsargsp
->flags
|= NFSMNT_READDIRSIZE
;
281 nfsargsp
->flags
|= NFSMNT_INT
;
287 nfsargsp
->flags
|= NFSMNT_RDIRPLUS
;
290 mp
= getmntopts(optarg
, mopts
, mntflags
, &altflags
);
292 err(1, "getmntopts");
293 if (altflags
& ALTF_BG
)
295 if (altflags
& ALTF_CONN
)
296 nfsargsp
->flags
&= ~NFSMNT_NOCONN
;
297 if (altflags
& ALTF_DUMBTIMR
)
298 nfsargsp
->flags
|= NFSMNT_DUMBTIMR
;
299 if (altflags
& ALTF_INTR
)
300 nfsargsp
->flags
|= NFSMNT_INT
;
301 if (altflags
& (ALTF_NFSV3
|ALTF_NQNFS
)) {
303 errx(1, "conflicting version options");
306 if (altflags
& ALTF_NFSV2
) {
308 errx(1, "conflicting version options");
310 nfsargsp
->flags
&= ~NFSMNT_NFSV3
;
312 if (altflags
& ALTF_RDIRPLUS
)
313 nfsargsp
->flags
|= NFSMNT_RDIRPLUS
;
314 if (altflags
& ALTF_MNTUDP
)
316 if (altflags
& ALTF_NORESPORT
)
317 nfsargsp
->flags
&= ~NFSMNT_RESVPORT
;
319 if (altflags
& ALTF_SEQPACKET
)
320 nfsargsp
->sotype
= SOCK_SEQPACKET
;
322 if (altflags
& ALTF_SOFT
)
323 nfsargsp
->flags
|= NFSMNT_SOFT
;
324 if (altflags
& ALTF_TCP
) {
325 nfsargsp
->sotype
= SOCK_STREAM
;
327 if (altflags
& ALTF_PORT
) {
328 port
= getmntoptnum(mp
, "port");
330 if (altflags
& ALTF_RSIZE
) {
332 (int)getmntoptnum(mp
, "rsize");
333 nfsargsp
->flags
|= NFSMNT_RSIZE
;
335 if (altflags
& ALTF_WSIZE
) {
337 (int)getmntoptnum(mp
, "wsize");
338 nfsargsp
->flags
|= NFSMNT_WSIZE
;
340 if (altflags
& ALTF_RDIRSIZE
) {
342 (int)getmntoptnum(mp
, "rdirsize");
343 nfsargsp
->flags
|= NFSMNT_READDIRSIZE
;
346 if (altflags
& ALTF_MAXGRPS
) {
347 set_rpc_maxgrouplist(num
);
348 nfsargsp
->maxgrouplist
=
349 (int)getmntoptnum(mp
, "maxgrps");
350 nfsargsp
->flags
|= NFSMNT_MAXGRPS
;
353 if (altflags
& ALTF_LEASETERM
) {
354 nfsargsp
->leaseterm
=
355 (int)getmntoptnum(mp
, "leaseterm");
356 nfsargsp
->flags
|= NFSMNT_LEASETERM
;
358 if (altflags
& ALTF_READAHEAD
) {
359 nfsargsp
->readahead
=
360 (int)getmntoptnum(mp
, "readahead");
361 nfsargsp
->flags
|= NFSMNT_READAHEAD
;
363 if (altflags
& ALTF_DEADTHRESH
) {
364 nfsargsp
->deadthresh
=
365 (int)getmntoptnum(mp
, "deadthresh");
366 nfsargsp
->flags
|= NFSMNT_DEADTHRESH
;
368 if (altflags
& ALTF_TIMEO
) {
370 (int)getmntoptnum(mp
, "timeo");
371 nfsargsp
->flags
|= NFSMNT_TIMEO
;
373 if (altflags
& ALTF_RETRANS
) {
375 (int)getmntoptnum(mp
, "retrans");
376 nfsargsp
->flags
|= NFSMNT_RETRANS
;
382 nfsargsp
->flags
|= NFSMNT_RESVPORT
;
385 nfsargsp
->flags
&= ~NFSMNT_RESVPORT
;
388 num
= strtol(optarg
, &p
, 10);
390 errx(1, "illegal -R value -- %s", optarg
);
394 num
= strtol(optarg
, &p
, 10);
396 errx(1, "illegal -r value -- %s", optarg
);
397 nfsargsp
->rsize
= num
;
398 nfsargsp
->flags
|= NFSMNT_RSIZE
;
402 nfsargsp
->sotype
= SOCK_SEQPACKET
;
406 nfsargsp
->flags
|= NFSMNT_SOFT
;
409 nfsargsp
->sotype
= SOCK_STREAM
;
412 num
= strtol(optarg
, &p
, 10);
414 errx(1, "illegal -t value -- %s", optarg
);
415 nfsargsp
->timeo
= num
;
416 nfsargsp
->flags
|= NFSMNT_TIMEO
;
419 num
= strtol(optarg
, &p
, 10);
421 errx(1, "illegal -w value -- %s", optarg
);
422 nfsargsp
->wsize
= num
;
423 nfsargsp
->flags
|= NFSMNT_WSIZE
;
426 num
= strtol(optarg
, &p
, 10);
428 errx(1, "illegal -x value -- %s", optarg
);
429 nfsargsp
->retrans
= num
;
430 nfsargsp
->flags
|= NFSMNT_RETRANS
;
433 nfsargsp
->flags
|= NFSMNT_XLATECOOKIE
;
448 strlcpy(spec
, *argv
++, MAXPATHLEN
);
449 pathadj(*argv
, name
);
450 mount_nfs_dogetargs(nfsargsp
, *mntflags
, spec
);
454 mount_nfs(int argc
, char *argv
[])
456 char spec
[MAXPATHLEN
], name
[MAXPATHLEN
];
457 struct nfs_args args
;
461 mount_nfs_parseargs(argc
, argv
, &args
, &mntflags
, spec
, name
);
464 if ((retval
= mount(MOUNT_NFS
, name
, mntflags
,
465 &args
, sizeof args
)) == -1) {
466 /* Did we just default to v3 on a v2-only kernel?
467 * If so, default to v2 & try again */
468 if (errno
== EPROGMISMATCH
&&
469 (args
.flags
& NFSMNT_NFSV3
) != 0 && !force3
) {
471 * fall back to v2. XXX lack of V3 umount.
473 args
.flags
&= ~NFSMNT_NFSV3
;
474 mount_nfs_dogetargs(&args
, mntflags
, spec
);
479 err(1, "%s on %s", spec
, name
);
480 if (mntflags
& MNT_GETARGS
) {
489 shownfsargs(const struct nfs_args
*nfsargsp
)
492 char host
[NI_MAXHOST
], serv
[NI_MAXSERV
];
495 (void)snprintb(fbuf
, sizeof(fbuf
), NFSMNT_BITS
, nfsargsp
->flags
);
496 if (nfsargsp
->addr
!= NULL
) {
497 error
= getnameinfo(nfsargsp
->addr
, nfsargsp
->addrlen
, host
,
498 sizeof(host
), serv
, sizeof(serv
),
499 NI_NUMERICHOST
| NI_NUMERICSERV
);
501 warnx("getnameinfo: %s", gai_strerror(error
));
506 printf("addr=%s, port=%s, addrlen=%d, ",
507 host
, serv
, nfsargsp
->addrlen
);
508 printf("sotype=%d, proto=%d, fhsize=%d, "
509 "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
510 "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
518 nfsargsp
->readdirsize
,
521 nfsargsp
->maxgrouplist
,
524 nfsargsp
->deadthresh
);
530 (void)fprintf(stderr
, "usage: %s %s\n%s\n%s\n%s\n%s\n", getprogname(),
531 "[-23bCcdilPpqsTUX] [-a maxreadahead] [-D deadthresh]",
532 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm]",
533 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
534 "\t[-w writesize] [-x retrans]",
535 "\trhost:path node");