netbsd32_compat_30.c revision 1.20 1 /* $NetBSD: netbsd32_compat_30.c,v 1.20 2007/04/22 08:29:58 dsl 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.20 2007/04/22 08:29:58 dsl 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(l, v, retval)
65 struct lwp *l;
66 void *v;
67 register_t *retval;
68 {
69 struct compat_30_netbsd32_getdents_args /* {
70 syscallarg(int) fd;
71 syscallarg(netbsd32_charp) buf;
72 syscallarg(netbsd32_size_t) count;
73 } */ *uap = v;
74 struct file *fp;
75 int error, done;
76 char *buf;
77 netbsd32_size_t count;
78 struct proc *p = l->l_proc;
79
80 /* Limit the size on any kernel buffers used by VOP_READDIR */
81 count = min(MAXBSIZE, SCARG(uap, count));
82
83 /* getvnode() will use the descriptor for us */
84 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
85 return (error);
86 if ((fp->f_flag & FREAD) == 0) {
87 error = EBADF;
88 goto out;
89 }
90 buf = malloc(count, M_TEMP, M_WAITOK);
91 error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
92 if (error == 0) {
93 *retval = netbsd32_to_dirent12(buf, done);
94 error = copyout(buf, SCARG_P32(uap, buf), *retval);
95 }
96 free(buf, M_TEMP);
97 out:
98 FILE_UNUSE(fp, l);
99 return (error);
100 }
101
102 int
103 compat_30_netbsd32___stat13(l, v, retval)
104 struct lwp *l;
105 void *v;
106 register_t *retval;
107 {
108 struct compat_30_netbsd32___stat13_args /* {
109 syscallarg(const netbsd32_charp) path;
110 syscallarg(netbsd32_stat13p_t) ub;
111 } */ *uap = v;
112 struct netbsd32_stat13 sb32;
113 struct stat sb;
114 int error;
115 void *sg;
116 const char *path;
117 struct proc *p = l->l_proc;
118
119 path = SCARG_P32(uap, path);
120 sg = stackgap_init(p, 0);
121
122 error = do_sys_stat(l, path, FOLLOW, &sb);
123 if (error)
124 return (error);
125 netbsd32_from___stat13(&sb, &sb32);
126 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
127 return (error);
128 }
129
130 int
131 compat_30_netbsd32___fstat13(l, v, retval)
132 struct lwp *l;
133 void *v;
134 register_t *retval;
135 {
136 struct compat_30_netbsd32___fstat13_args /* {
137 syscallarg(int) fd;
138 syscallarg(netbsd32_stat13p_t) sb;
139 } */ *uap = v;
140 int fd = SCARG(uap, fd);
141 struct proc *p = l->l_proc;
142 struct filedesc *fdp = p->p_fd;
143 struct file *fp;
144 struct netbsd32_stat13 sb32;
145 struct stat ub;
146 int error = 0;
147
148 if ((fp = fd_getfile(fdp, fd)) == NULL)
149 return (EBADF);
150
151 FILE_USE(fp);
152 error = (*fp->f_ops->fo_stat)(fp, &ub, l);
153 FILE_UNUSE(fp, l);
154
155 if (error == 0) {
156 netbsd32_from___stat13(&ub, &sb32);
157 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
158 }
159 return (error);
160 }
161
162 int
163 compat_30_netbsd32___lstat13(l, v, retval)
164 struct lwp *l;
165 void *v;
166 register_t *retval;
167 {
168 struct compat_30_netbsd32___lstat13_args /* {
169 syscallarg(const netbsd32_charp) path;
170 syscallarg(netbsd32_stat13p_t) ub;
171 } */ *uap = v;
172 struct netbsd32_stat13 sb32;
173 struct stat sb;
174 int error;
175 void *sg;
176 const char *path;
177 struct proc *p = l->l_proc;
178
179 path = SCARG_P32(uap, path);
180 sg = stackgap_init(p, 0);
181
182 error = do_sys_stat(l, path, NOFOLLOW, &sb);
183 if (error)
184 return (error);
185 netbsd32_from___stat13(&sb, &sb32);
186 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
187 return (error);
188 }
189
190 int
191 compat_30_netbsd32_fhstat(l, v, retval)
192 struct lwp *l;
193 void *v;
194 register_t *retval;
195 {
196 struct compat_30_netbsd32_fhstat_args /* {
197 syscallarg(const netbsd32_fhandlep_t) fhp;
198 syscallarg(netbsd32_stat13p_t) sb;
199 } */ *uap = v;
200 struct stat sb;
201 struct netbsd32_stat13 sb32;
202 int error;
203 struct compat_30_fhandle fh;
204 struct mount *mp;
205 struct vnode *vp;
206
207 /*
208 * Must be super user
209 */
210 if ((error = kauth_authorize_system(l->l_cred,
211 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
212 return (error);
213
214 if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
215 return (error);
216
217 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
218 return (ESTALE);
219 if (mp->mnt_op->vfs_fhtovp == NULL)
220 return EOPNOTSUPP;
221 if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
222 return (error);
223 error = vn_stat(vp, &sb, l);
224 vput(vp);
225 if (error)
226 return (error);
227 netbsd32_from___stat13(&sb, &sb32);
228 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
229 return (error);
230 }
231
232 int
233 compat_30_netbsd32_fhstatvfs1(l, v, retval)
234 struct lwp *l;
235 void *v;
236 register_t *retval;
237 {
238 struct compat_30_netbsd32_fhstatvfs1_args /* {
239 syscallarg(const netbsd32_fhandlep_t) fhp;
240 syscallarg(netbsd32_statvfsp_t) buf;
241 syscallarg(int) flags;
242 } */ *uap = v;
243 struct statvfs *sbuf;
244 struct netbsd32_statvfs *s32;
245 fhandle_t *fh;
246 struct vnode *vp;
247 int error;
248
249 /*
250 * Must be super user
251 */
252 if ((error = kauth_authorize_system(l->l_cred,
253 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
254 return error;
255
256 if ((error = vfs_copyinfh_alloc(SCARG_P32(uap, fhp),
257 FHANDLE_SIZE_COMPAT, &fh)) != 0)
258 goto bad;
259 if ((error = vfs_fhtovp(fh, &vp)) != 0)
260 goto bad;
261
262 sbuf = (struct statvfs *)malloc(sizeof(struct statvfs), M_TEMP,
263 M_WAITOK);
264 error = dostatvfs(vp->v_mount, sbuf, l, SCARG(uap, flags), 1);
265 vput(vp);
266 if (error != 0)
267 goto out;
268
269 s32 = (struct netbsd32_statvfs *)
270 malloc(sizeof(struct netbsd32_statvfs), M_TEMP, M_WAITOK);
271 netbsd32_from_statvfs(sbuf, s32);
272 error = copyout(s32, SCARG_P32(uap, buf),
273 sizeof(struct netbsd32_statvfs));
274 free(s32, M_TEMP);
275
276 out:
277 free(sbuf, M_TEMP);
278 bad:
279 vfs_copyinfh_free(fh);
280 return (error);
281 }
282
283 int
284 compat_30_netbsd32_socket(l, v, retval)
285 struct lwp *l;
286 void *v;
287 register_t *retval;
288 {
289 struct compat_30_netbsd32_socket_args /* {
290 syscallarg(int) domain;
291 syscallarg(int) type;
292 syscallarg(int) protocol;
293 } */ *uap = v;
294 struct compat_30_sys_socket_args ua;
295
296 NETBSD32TO64_UAP(domain);
297 NETBSD32TO64_UAP(type);
298 NETBSD32TO64_UAP(protocol);
299 return (compat_30_sys_socket(l, &ua, retval));
300 }
301
302 int
303 compat_30_netbsd32_getfh(l, v, retval)
304 struct lwp *l;
305 void *v;
306 register_t *retval;
307 {
308 struct compat_30_netbsd32_getfh_args /* {
309 syscallarg(const netbsd32_charp) fname;
310 syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
311 } */ *uap = v;
312 struct compat_30_sys_getfh_args ua;
313
314 NETBSD32TOP_UAP(fname, const char);
315 NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
316 /* Lucky for us a fhandle_t doesn't change sizes */
317 return (compat_30_sys_getfh(l, &ua, retval));
318 }
319
320
321 int compat_30_netbsd32_sys___fhstat30(l, v, retval)
322 struct lwp *l;
323 void *v;
324 register_t *retval;
325 {
326 struct compat_30_netbsd32_sys___fhstat30_args /* {
327 syscallarg(const netbsd32_fhandlep_t) fhp;
328 syscallarg(netbsd32_statp_t) sb;
329 } */ *uap = v;
330 struct stat sb;
331 struct netbsd32_stat sb32;
332 int error;
333 fhandle_t *fh;
334 struct vnode *vp;
335
336 /*
337 * Must be super user
338 */
339 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
340 0, NULL, NULL, NULL)))
341 return error;
342
343 if ((error = vfs_copyinfh_alloc(SCARG_P32(uap, fhp),
344 FHANDLE_SIZE_COMPAT, &fh)) != 0)
345 goto bad;
346
347 if ((error = vfs_fhtovp(fh, &vp)) != 0)
348 goto bad;
349
350 error = vn_stat(vp, &sb, l);
351 vput(vp);
352 if (error)
353 goto bad;
354 netbsd32_from___stat30(&sb, &sb32);
355 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
356 bad:
357 vfs_copyinfh_free(fh);
358 return error;
359 }
360
361 /*
362 * Open a file given a file handle.
363 *
364 * Check permissions, allocate an open file structure,
365 * and call the device open routine if any.
366 */
367 int
368 compat_30_netbsd32_fhopen(l, v, retval)
369 struct lwp *l;
370 void *v;
371 register_t *retval;
372 {
373 struct compat_30_netbsd32_fhopen_args /* {
374 syscallarg(const fhandle_t *) fhp;
375 syscallarg(int) flags;
376 } */ *uap = v;
377 struct compat_30_sys_fhopen_args ua;
378
379 NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
380 NETBSD32TO64_UAP(flags);
381 return (compat_30_sys_fhopen(l, &ua, retval));
382 }
383