1 /* $NetBSD: getnfsargs_small.c,v 1.7 2007/03/10 00:30:37 hubertf Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1993 John Brezak
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
49 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
51 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
52 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
61 * Copyright (c) 1992, 1993, 1994
62 * The Regents of the University of California. All rights reserved.
64 * This code is derived from software contributed to Berkeley by
65 * Rick Macklem at The University of Guelph.
67 * Redistribution and use in source and binary forms, with or without
68 * modification, are permitted provided that the following conditions
70 * 1. Redistributions of source code must retain the above copyright
71 * notice, this list of conditions and the following disclaimer.
72 * 2. Redistributions in binary form must reproduce the above copyright
73 * notice, this list of conditions and the following disclaimer in the
74 * documentation and/or other materials provided with the distribution.
75 * 3. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 #include <sys/cdefs.h>
93 __RCSID("$NetBSD: getnfsargs_small.c,v 1.7 2007/03/10 00:30:37 hubertf Exp $");
95 #include <sys/param.h>
96 #include <sys/mount.h>
97 #include <sys/socket.h>
101 #include <nfs/rpcv2.h>
102 #include <nfs/nfsproto.h>
104 #include <nfs/nfsmount.h>
106 #include <arpa/inet.h>
119 #include "mount_nfs.h"
122 typedef int32_t n_long
;
125 #define RPC_HEADER_WORDS 28 /* more than enough */
126 #define FNAME_SIZE 512
130 u_char nfh
[NFSX_V3FHMAX
];
133 /* Ripped from src/sys/arch/i386/stand/libsa/nfs.c */
135 nfs_getrootfh(struct iodesc
*d
, const char *path
, int mntvers
, struct nfhret
*nfhret
)
140 char path
[FNAME_SIZE
];
144 u_char fh
[NFSX_V3FHMAX
];
147 uint32_t h
[RPC_HEADER_WORDS
];
151 uint32_t h
[RPC_HEADER_WORDS
];
159 memset(args
, 0, sizeof(*args
));
161 if (len
> sizeof(args
->path
))
162 len
= sizeof(args
->path
);
163 args
->len
= htonl(len
);
164 memcpy(args
->path
, path
, len
);
165 len
= 4 + roundup(len
, 4);
167 cc
= rpc_call(d
, RPCPROG_MNT
, mntvers
, RPCMNT_MOUNT
,
168 args
, len
, repl
, sizeof(*repl
));
170 /* errno was set by rpc_call */
176 return ntohl(repl
->errval
);
178 memcpy(nfhret
->nfh
, repl
->fh
, sizeof(repl
->fh
));
183 getnfsargs(char *spec
, struct nfs_args
*nfsargsp
)
185 struct addrinfo hints
, *ai_nfs
;
187 int nfsvers
, mntvers
;
188 char *hostp
, *delimp
;
189 static struct nfhret nfhret
;
190 static char nam
[MNAMELEN
+ 1];
194 strlcpy(nam
, spec
, sizeof(nam
));
195 if ((delimp
= strchr(spec
, '@')) != NULL
) {
197 } else if ((delimp
= strrchr(spec
, ':')) != NULL
) {
201 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
206 memset(&hints
, 0, sizeof hints
);
207 hints
.ai_socktype
= nfsargsp
->sotype
;
209 if ((ecode
= getaddrinfo(hostp
, "nfs", &hints
, &ai_nfs
)) != 0) {
210 warnx("can't get net id for host \"%s\": %s", hostp
,
211 gai_strerror(ecode
));
215 if ((nfsargsp
->flags
& NFSMNT_NFSV3
) != 0) {
217 mntvers
= RPCMNT_VER3
;
220 mntvers
= RPCMNT_VER1
;
224 for (d
.ai
= ai_nfs
; ; d
.ai
= d
.ai
->ai_next
) {
226 if (nfsvers
== NFS_VER3
&& !force3
) {
228 mntvers
= RPCMNT_VER1
;
234 nfs_port
= rpc_getport(&d
, RPCPROG_NFS
, nfsvers
);
237 if (nfs_getrootfh(&d
, spec
, mntvers
, &nfhret
) == 0)
243 set_port(d
.ai
->ai_addr
, htons(nfs_port
));
245 nfsargsp
->hostname
= nam
;
246 nfsargsp
->addr
= d
.ai
->ai_addr
;
247 nfsargsp
->addrlen
= d
.ai
->ai_addrlen
;
249 nfsargsp
->fh
= nfhret
.nfh
;
250 if (nfsvers
== NFS_VER3
) {
251 nfsargsp
->fhsize
= ntohl(*(uint32_t *)nfhret
.nfh
);
254 nfsargsp
->fhsize
= NFSX_V2FH
;
255 nfsargsp
->flags
&= ~NFSMNT_NFSV3
;