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