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