netbsd32_compat_30.c revision 1.24.6.1 1 /* $NetBSD: netbsd32_compat_30.c,v 1.24.6.1 2008/05/10 23:48:57 wrstuden 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.6.1 2008/05/10 23:48:57 wrstuden 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/sa.h>
52 #include <sys/syscallargs.h>
53 #include <sys/proc.h>
54 #include <sys/dirent.h>
55 #include <sys/kauth.h>
56 #include <sys/vfs_syscalls.h>
57
58 #include <compat/netbsd32/netbsd32.h>
59 #include <compat/netbsd32/netbsd32_syscallargs.h>
60 #include <compat/netbsd32/netbsd32_conv.h>
61 #include <compat/sys/mount.h>
62
63
64 int
65 compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
66 {
67 /* {
68 syscallarg(int) fd;
69 syscallarg(netbsd32_charp) buf;
70 syscallarg(netbsd32_size_t) count;
71 } */
72 file_t *fp;
73 int error, done;
74 char *buf;
75 netbsd32_size_t count;
76
77 /* Limit the size on any kernel buffers used by VOP_READDIR */
78 count = min(MAXBSIZE, SCARG(uap, count));
79
80 /* getvnode() will use the descriptor for us */
81 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
82 return (error);
83 if ((fp->f_flag & FREAD) == 0) {
84 error = EBADF;
85 goto out;
86 }
87 buf = malloc(count, M_TEMP, M_WAITOK);
88 error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
89 if (error == 0) {
90 *retval = netbsd32_to_dirent12(buf, done);
91 error = copyout(buf, SCARG_P32(uap, buf), *retval);
92 }
93 free(buf, M_TEMP);
94 out:
95 fd_putfile(SCARG(uap, fd));
96 return (error);
97 }
98
99 int
100 compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
101 {
102 /* {
103 syscallarg(const netbsd32_charp) path;
104 syscallarg(netbsd32_stat13p_t) ub;
105 } */
106 struct netbsd32_stat13 sb32;
107 struct stat sb;
108 int error;
109 const char *path;
110
111 path = SCARG_P32(uap, path);
112
113 error = do_sys_stat(path, FOLLOW, &sb);
114 if (error)
115 return (error);
116 netbsd32_from___stat13(&sb, &sb32);
117 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
118 return (error);
119 }
120
121 int
122 compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
123 {
124 /* {
125 syscallarg(int) fd;
126 syscallarg(netbsd32_stat13p_t) sb;
127 } */
128 int fd = SCARG(uap, fd);
129 file_t *fp;
130 struct netbsd32_stat13 sb32;
131 struct stat ub;
132 int error = 0;
133
134 if ((fp = fd_getfile(fd)) == NULL)
135 return (EBADF);
136 error = (*fp->f_ops->fo_stat)(fp, &ub);
137 fd_putfile(fd);
138
139 if (error == 0) {
140 netbsd32_from___stat13(&ub, &sb32);
141 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
142 }
143 return (error);
144 }
145
146 int
147 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
148 {
149 /* {
150 syscallarg(const netbsd32_charp) path;
151 syscallarg(netbsd32_stat13p_t) ub;
152 } */
153 struct netbsd32_stat13 sb32;
154 struct stat sb;
155 int error;
156 const char *path;
157
158 path = SCARG_P32(uap, path);
159
160 error = do_sys_stat(path, NOFOLLOW, &sb);
161 if (error)
162 return (error);
163 netbsd32_from___stat13(&sb, &sb32);
164 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
165 return (error);
166 }
167
168 int
169 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
170 {
171 /* {
172 syscallarg(const netbsd32_fhandlep_t) fhp;
173 syscallarg(netbsd32_stat13p_t) sb;
174 } */
175 struct stat sb;
176 struct netbsd32_stat13 sb32;
177 int error;
178 struct compat_30_fhandle fh;
179 struct mount *mp;
180 struct vnode *vp;
181
182 /*
183 * Must be super user
184 */
185 if ((error = kauth_authorize_system(l->l_cred,
186 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
187 return (error);
188
189 if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
190 return (error);
191
192 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
193 return (ESTALE);
194 if (mp->mnt_op->vfs_fhtovp == NULL)
195 return EOPNOTSUPP;
196 if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
197 return (error);
198 error = vn_stat(vp, &sb);
199 vput(vp);
200 if (error)
201 return (error);
202 netbsd32_from___stat13(&sb, &sb32);
203 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
204 return (error);
205 }
206
207 int
208 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
209 {
210 /* {
211 syscallarg(const netbsd32_fhandlep_t) fhp;
212 syscallarg(netbsd32_statvfsp_t) buf;
213 syscallarg(int) flags;
214 } */
215 struct statvfs *sbuf;
216 struct netbsd32_statvfs *s32;
217 int error;
218
219 sbuf = STATVFSBUF_GET();
220 error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
221 SCARG(uap, flags));
222
223 if (error != 0) {
224 s32 = malloc(sizeof *s32, M_TEMP, M_WAITOK);
225 netbsd32_from_statvfs(sbuf, s32);
226 error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
227 free(s32, M_TEMP);
228 }
229 STATVFSBUF_PUT(sbuf);
230
231 return (error);
232 }
233
234 int
235 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
236 {
237 /* {
238 syscallarg(int) domain;
239 syscallarg(int) type;
240 syscallarg(int) protocol;
241 } */
242 struct compat_30_sys_socket_args ua;
243
244 NETBSD32TO64_UAP(domain);
245 NETBSD32TO64_UAP(type);
246 NETBSD32TO64_UAP(protocol);
247 return (compat_30_sys_socket(l, &ua, retval));
248 }
249
250 int
251 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
252 {
253 /* {
254 syscallarg(const netbsd32_charp) fname;
255 syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
256 } */
257 struct compat_30_sys_getfh_args ua;
258
259 NETBSD32TOP_UAP(fname, const char);
260 NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
261 /* Lucky for us a fhandle_t doesn't change sizes */
262 return (compat_30_sys_getfh(l, &ua, retval));
263 }
264
265
266 int
267 compat_30_netbsd32_sys___fhstat30(struct lwp *l, const struct compat_30_netbsd32_sys___fhstat30_args *uap, register_t *retval)
268 {
269 /* {
270 syscallarg(const netbsd32_fhandlep_t) fhp;
271 syscallarg(netbsd32_statp_t) sb;
272 } */
273 struct stat sb;
274 struct netbsd32_stat sb32;
275 int error;
276
277 error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
278 if (error)
279 return error;
280
281 netbsd32_from___stat30(&sb, &sb32);
282 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
283 return error;
284 }
285
286 /*
287 * Open a file given a file handle.
288 *
289 * Check permissions, allocate an open file structure,
290 * and call the device open routine if any.
291 */
292 int
293 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
294 {
295 /* {
296 syscallarg(const fhandle_t *) fhp;
297 syscallarg(int) flags;
298 } */
299 struct compat_30_sys_fhopen_args ua;
300
301 NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
302 NETBSD32TO64_UAP(flags);
303 return (compat_30_sys_fhopen(l, &ua, retval));
304 }
305