Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_20.c revision 1.36.10.7
      1 /*	$NetBSD: netbsd32_compat_20.c,v 1.36.10.7 2018/09/11 05:00:42 pgoyette 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_20.c,v 1.36.10.7 2018/09/11 05:00:42 pgoyette Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/module.h>
     35 #include <sys/mount.h>
     36 #include <sys/stat.h>
     37 #include <sys/time.h>
     38 #include <sys/ktrace.h>
     39 #include <sys/vnode.h>
     40 #include <sys/socket.h>
     41 #include <sys/file.h>
     42 #include <sys/filedesc.h>
     43 #include <sys/namei.h>
     44 #include <sys/syscallargs.h>
     45 #include <sys/syscallvar.h>
     46 #include <sys/proc.h>
     47 #include <sys/dirent.h>
     48 
     49 #include <compat/netbsd32/netbsd32.h>
     50 #include <compat/netbsd32/netbsd32_syscall.h>
     51 #include <compat/netbsd32/netbsd32_syscallargs.h>
     52 #include <compat/netbsd32/netbsd32_conv.h>
     53 
     54 extern struct emul emul_netbsd32;
     55 
     56 static inline void compat_20_netbsd32_from_statvfs(struct statvfs *,
     57     struct netbsd32_statfs *);
     58 
     59 static inline void
     60 compat_20_netbsd32_from_statvfs(struct statvfs *sbp, struct netbsd32_statfs *sb32p)
     61 {
     62 	sb32p->f_flags = sbp->f_flag;
     63 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
     64 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
     65 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
     66 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
     67 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
     68 	sb32p->f_files = (netbsd32_long)sbp->f_files;
     69 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
     70 	sb32p->f_fsid = sbp->f_fsidx;
     71 	sb32p->f_owner = sbp->f_owner;
     72 	sb32p->f_spare[0] = 0;
     73 	sb32p->f_spare[1] = 0;
     74 	sb32p->f_spare[2] = 0;
     75 	sb32p->f_spare[3] = 0;
     76 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
     77 	    sizeof(sb32p->f_fstypename));
     78 	(void)memcpy(sb32p->f_mntonname, sbp->f_mntonname,
     79 	    sizeof(sb32p->f_mntonname));
     80 	(void)memcpy(sb32p->f_mntfromname, sbp->f_mntfromname,
     81 	    sizeof(sb32p->f_mntfromname));
     82 }
     83 
     84 int
     85 compat_20_netbsd32_getfsstat(struct lwp *l, const struct compat_20_netbsd32_getfsstat_args *uap, register_t *retval)
     86 {
     87 	/* {
     88 		syscallarg(netbsd32_statfsp_t) buf;
     89 		syscallarg(netbsd32_long) bufsize;
     90 		syscallarg(int) flags;
     91 	} */
     92 	int root = 0;
     93 	struct proc *p = l->l_proc;
     94 	mount_iterator_t *iter;
     95 	struct mount *mp;
     96 	struct statvfs *sb;
     97 	struct netbsd32_statfs sb32;
     98 	void *sfsp;
     99 	size_t count, maxcount;
    100 	int error = 0;
    101 
    102 	sb = STATVFSBUF_GET();
    103 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
    104 	sfsp = SCARG_P32(uap, buf);
    105 	mountlist_iterator_init(&iter);
    106 	count = 0;
    107 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
    108 		if (sfsp && count < maxcount) {
    109 			error = dostatvfs(mp, sb, l, SCARG(uap, flags), 0);
    110 			if (error) {
    111 				error = 0;
    112 				continue;
    113 			}
    114 			compat_20_netbsd32_from_statvfs(sb, &sb32);
    115 			error = copyout(&sb32, sfsp, sizeof(sb32));
    116 			if (error)
    117 				goto out;
    118 			sfsp = (char *)sfsp + sizeof(sb32);
    119 			root |= strcmp(sb->f_mntonname, "/") == 0;
    120 		}
    121 		count++;
    122 	}
    123 
    124 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
    125 		/*
    126 		 * fake a root entry
    127 		 */
    128 		error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
    129 		    sb, l, SCARG(uap, flags), 1);
    130 		if (error != 0)
    131 			goto out;
    132 		if (sfsp) {
    133 			compat_20_netbsd32_from_statvfs(sb, &sb32);
    134 			error = copyout(&sb32, sfsp, sizeof(sb32));
    135 			if (error != 0)
    136 				goto out;
    137 		}
    138 		count++;
    139 	}
    140 
    141 	if (sfsp && count > maxcount)
    142 		*retval = maxcount;
    143 	else
    144 		*retval = count;
    145 out:
    146 	mountlist_iterator_destroy(iter);
    147 	STATVFSBUF_PUT(sb);
    148 	return error;
    149 }
    150 
    151 int
    152 compat_20_netbsd32_statfs(struct lwp *l, const struct compat_20_netbsd32_statfs_args *uap, register_t *retval)
    153 {
    154 	/* {
    155 		syscallarg(const netbsd32_charp) path;
    156 		syscallarg(netbsd32_statfsp_t) buf;
    157 	} */
    158 	struct mount *mp;
    159 	struct statvfs *sb;
    160 	struct netbsd32_statfs s32;
    161 	int error;
    162 	struct vnode *vp;
    163 
    164 	error = namei_simple_user(SCARG_P32(uap, path),
    165 				NSM_FOLLOW_TRYEMULROOT, &vp);
    166 	if (error != 0)
    167 		return (error);
    168 	mp = vp->v_mount;
    169 	vrele(vp);
    170 	sb = STATVFSBUF_GET();
    171 	if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
    172 		goto out;
    173 	compat_20_netbsd32_from_statvfs(sb, &s32);
    174 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
    175 out:
    176 	STATVFSBUF_PUT(sb);
    177 	return error;
    178 }
    179 
    180 int
    181 compat_20_netbsd32_fstatfs(struct lwp *l, const struct compat_20_netbsd32_fstatfs_args *uap, register_t *retval)
    182 {
    183 	/* {
    184 		syscallarg(int) fd;
    185 		syscallarg(netbsd32_statfsp_t) buf;
    186 	} */
    187 	file_t *fp;
    188 	struct mount *mp;
    189 	struct statvfs *sb;
    190 	struct netbsd32_statfs s32;
    191 	int error;
    192 
    193 	/* fd_getvnode() will use the descriptor for us */
    194 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    195 		return (error);
    196 	mp = fp->f_vnode->v_mount;
    197 	sb = STATVFSBUF_GET();
    198 	if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
    199 		goto out;
    200 	compat_20_netbsd32_from_statvfs(sb, &s32);
    201 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
    202  out:
    203 	STATVFSBUF_PUT(sb);
    204 	fd_putfile(SCARG(uap, fd));
    205 	return (error);
    206 }
    207 
    208 int
    209 compat_20_netbsd32_fhstatfs(struct lwp *l, const struct compat_20_netbsd32_fhstatfs_args *uap, register_t *retval)
    210 {
    211 	/* {
    212 		syscallarg(const netbsd32_fhandlep_t) fhp;
    213 		syscallarg(struct statvfs *) buf;
    214 	} */
    215 	struct compat_30_sys_fhstatvfs1_args ua;
    216 
    217 	NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
    218 	NETBSD32TOP_UAP(buf, struct statvfs);
    219 #ifdef notyet
    220 	NETBSD32TOP_UAP(flags, int);
    221 #endif
    222 	return (compat_30_sys_fhstatvfs1(l, &ua, retval));
    223 }
    224 
    225 static struct syscall_package compat_netbsd32_20_syscalls[] = {
    226 	{ NETBSD32_SYS_compat_20_netbsd32_statfs, 0,
    227 	    (sy_call_t *)compat_20_netbsd32_statfs },
    228 	{ NETBSD32_SYS_compat_20_netbsd32_fstatfs, 0,
    229 	    (sy_call_t *)compat_20_netbsd32_fstatfs },
    230 	{ NETBSD32_SYS_compat_20_netbsd32_fhstatfs, 0,
    231 	    (sy_call_t *)compat_20_netbsd32_fhstatfs },
    232 	{ NETBSD32_SYS_compat_20_netbsd32_getfsstat, 0,
    233 	    (sy_call_t *)compat_20_netbsd32_getfsstat },
    234 	{ 0, 0, NULL }
    235 };
    236 
    237 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_20, "compat_netbsd32,compat_20");
    238 
    239 static int
    240 compat_netbsd32_20_modcmd(modcmd_t cmd, void *arg)
    241 {
    242 
    243 	switch (cmd) {
    244 	case MODULE_CMD_INIT:
    245 		return syscall_establish(&emul_netbsd32,
    246 		    compat_netbsd32_20_syscalls);
    247 
    248 	case MODULE_CMD_FINI:
    249 		return syscall_disestablish(&emul_netbsd32,
    250 		    compat_netbsd32_20_syscalls);
    251 
    252 	default:
    253 		return ENOTTY;
    254 	}
    255 }
    256