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