1 /*	$NetBSD: netbsd32_compat_30.c,v 1.24 2008/03/21 21:54:58 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_30.c,v 1.24 2008/03/21 21:54:58 ad Exp $");
33 
34 #include "opt_nfsserver.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/ktrace.h>
45 #include <sys/resourcevar.h>
46 #include <sys/vnode.h>
47 #include <sys/file.h>
48 #include <sys/filedesc.h>
49 #include <sys/namei.h>
50 #include <sys/statvfs.h>
51 #include <sys/syscallargs.h>
52 #include <sys/proc.h>
53 #include <sys/dirent.h>
54 #include <sys/kauth.h>
55 #include <sys/vfs_syscalls.h>
56 
57 #include <compat/netbsd32/netbsd32.h>
58 #include <compat/netbsd32/netbsd32_syscallargs.h>
59 #include <compat/netbsd32/netbsd32_conv.h>
60 #include <compat/sys/mount.h>
61 
62 
63 int
64 compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
65 {
66 	/* {
67 		syscallarg(int) fd;
68 		syscallarg(netbsd32_charp) buf;
69 		syscallarg(netbsd32_size_t) count;
70 	} */
71 	file_t *fp;
72 	int error, done;
73 	char  *buf;
74 	netbsd32_size_t count;
75 
76 	/* Limit the size on any kernel buffers used by VOP_READDIR */
77 	count = min(MAXBSIZE, SCARG(uap, count));
78 
79 	/* getvnode() will use the descriptor for us */
80 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
81 		return (error);
82 	if ((fp->f_flag & FREAD) == 0) {
83 		error = EBADF;
84 		goto out;
85 	}
86 	buf = malloc(count, M_TEMP, M_WAITOK);
87 	error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
88 	if (error == 0) {
89 		*retval = netbsd32_to_dirent12(buf, done);
90 		error = copyout(buf, SCARG_P32(uap, buf), *retval);
91 	}
92 	free(buf, M_TEMP);
93  out:
94  	fd_putfile(SCARG(uap, fd));
95 	return (error);
96 }
97 
98 int
99 compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
100 {
101 	/* {
102 		syscallarg(const netbsd32_charp) path;
103 		syscallarg(netbsd32_stat13p_t) ub;
104 	} */
105 	struct netbsd32_stat13 sb32;
106 	struct stat sb;
107 	int error;
108 	const char *path;
109 
110 	path = SCARG_P32(uap, path);
111 
112 	error = do_sys_stat(path, FOLLOW, &sb);
113 	if (error)
114 		return (error);
115 	netbsd32_from___stat13(&sb, &sb32);
116 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
117 	return (error);
118 }
119 
120 int
121 compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
122 {
123 	/* {
124 		syscallarg(int) fd;
125 		syscallarg(netbsd32_stat13p_t) sb;
126 	} */
127 	int fd = SCARG(uap, fd);
128 	file_t *fp;
129 	struct netbsd32_stat13 sb32;
130 	struct stat ub;
131 	int error = 0;
132 
133 	if ((fp = fd_getfile(fd)) == NULL)
134 		return (EBADF);
135 	error = (*fp->f_ops->fo_stat)(fp, &ub);
136 	fd_putfile(fd);
137 
138 	if (error == 0) {
139 		netbsd32_from___stat13(&ub, &sb32);
140 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
141 	}
142 	return (error);
143 }
144 
145 int
146 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
147 {
148 	/* {
149 		syscallarg(const netbsd32_charp) path;
150 		syscallarg(netbsd32_stat13p_t) ub;
151 	} */
152 	struct netbsd32_stat13 sb32;
153 	struct stat sb;
154 	int error;
155 	const char *path;
156 
157 	path = SCARG_P32(uap, path);
158 
159 	error = do_sys_stat(path, NOFOLLOW, &sb);
160 	if (error)
161 		return (error);
162 	netbsd32_from___stat13(&sb, &sb32);
163 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
164 	return (error);
165 }
166 
167 int
168 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
169 {
170 	/* {
171 		syscallarg(const netbsd32_fhandlep_t) fhp;
172 		syscallarg(netbsd32_stat13p_t) sb;
173 	} */
174 	struct stat sb;
175 	struct netbsd32_stat13 sb32;
176 	int error;
177 	struct compat_30_fhandle fh;
178 	struct mount *mp;
179 	struct vnode *vp;
180 
181 	/*
182 	 * Must be super user
183 	 */
184 	if ((error = kauth_authorize_system(l->l_cred,
185 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
186 		return (error);
187 
188 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
189 		return (error);
190 
191 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
192 		return (ESTALE);
193 	if (mp->mnt_op->vfs_fhtovp == NULL)
194 		return EOPNOTSUPP;
195 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
196 		return (error);
197 	error = vn_stat(vp, &sb);
198 	vput(vp);
199 	if (error)
200 		return (error);
201 	netbsd32_from___stat13(&sb, &sb32);
202 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
203 	return (error);
204 }
205 
206 int
207 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
208 {
209 	/* {
210 		syscallarg(const netbsd32_fhandlep_t) fhp;
211 		syscallarg(netbsd32_statvfsp_t) buf;
212 		syscallarg(int) flags;
213 	} */
214 	struct statvfs *sbuf;
215 	struct netbsd32_statvfs *s32;
216 	int error;
217 
218 	sbuf = STATVFSBUF_GET();
219 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
220 	    SCARG(uap, flags));
221 
222 	if (error != 0) {
223 		s32 = malloc(sizeof *s32, M_TEMP, M_WAITOK);
224 		netbsd32_from_statvfs(sbuf, s32);
225 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
226 		free(s32, M_TEMP);
227 	}
228 	STATVFSBUF_PUT(sbuf);
229 
230 	return (error);
231 }
232 
233 int
234 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
235 {
236 	/* {
237 		syscallarg(int) domain;
238 		syscallarg(int) type;
239 		syscallarg(int) protocol;
240 	} */
241 	struct compat_30_sys_socket_args ua;
242 
243 	NETBSD32TO64_UAP(domain);
244 	NETBSD32TO64_UAP(type);
245 	NETBSD32TO64_UAP(protocol);
246 	return (compat_30_sys_socket(l, &ua, retval));
247 }
248 
249 int
250 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
251 {
252 	/* {
253 		syscallarg(const netbsd32_charp) fname;
254 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
255 	} */
256 	struct compat_30_sys_getfh_args ua;
257 
258 	NETBSD32TOP_UAP(fname, const char);
259 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
260 	/* Lucky for us a fhandle_t doesn't change sizes */
261 	return (compat_30_sys_getfh(l, &ua, retval));
262 }
263 
264 
265 int
266 compat_30_netbsd32_sys___fhstat30(struct lwp *l, const struct compat_30_netbsd32_sys___fhstat30_args *uap, register_t *retval)
267 {
268 	/* {
269 		syscallarg(const netbsd32_fhandlep_t) fhp;
270 		syscallarg(netbsd32_statp_t) sb;
271 	} */
272 	struct stat sb;
273 	struct netbsd32_stat sb32;
274 	int error;
275 
276 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
277 	if (error)
278 		return error;
279 
280 	netbsd32_from___stat30(&sb, &sb32);
281 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
282 	return error;
283 }
284 
285 /*
286  * Open a file given a file handle.
287  *
288  * Check permissions, allocate an open file structure,
289  * and call the device open routine if any.
290  */
291 int
292 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
293 {
294 	/* {
295 		syscallarg(const fhandle_t *) fhp;
296 		syscallarg(int) flags;
297 	} */
298 	struct compat_30_sys_fhopen_args ua;
299 
300 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
301 	NETBSD32TO64_UAP(flags);
302 	return (compat_30_sys_fhopen(l, &ua, retval));
303 }
304