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