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