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