1 /* $NetBSD: netbsd32_compat_20.c,v 1.5 2006/03/07 03:32:06 thorpej 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_20.c,v 1.5 2006/03/07 03:32:06 thorpej Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/malloc.h> 37 #include <sys/mount.h> 38 #include <sys/stat.h> 39 #include <sys/time.h> 40 #include <sys/ktrace.h> 41 #include <sys/vnode.h> 42 #include <sys/file.h> 43 #include <sys/filedesc.h> 44 #include <sys/namei.h> 45 #include <sys/sa.h> 46 #include <sys/syscallargs.h> 47 #include <sys/proc.h> 48 #include <sys/dirent.h> 49 50 #include <compat/netbsd32/netbsd32.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(sbp, sb32p) 59 struct statvfs *sbp; 60 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 #if 1 77 /* May as well do the whole batch in one go */ 78 memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN+MNAMELEN+MNAMELEN); 79 #else 80 /* If we want to be careful */ 81 memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN); 82 memcpy(sb32p->f_mntonname, sbp->f_mntonname, MNAMELEN); 83 memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, MNAMELEN); 84 #endif 85 } 86 87 int 88 compat_20_netbsd32_getfsstat(l, v, retval) 89 struct lwp *l; 90 void *v; 91 register_t *retval; 92 { 93 struct compat_20_netbsd32_getfsstat_args /* { 94 syscallarg(netbsd32_statfsp_t) buf; 95 syscallarg(netbsd32_long) bufsize; 96 syscallarg(int) flags; 97 } */ *uap = v; 98 struct mount *mp, *nmp; 99 struct statvfs *sp; 100 struct netbsd32_statfs sb32; 101 caddr_t sfsp; 102 long count, maxcount, error; 103 104 maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs); 105 sfsp = (caddr_t)NETBSD32PTR64(SCARG(uap, buf)); 106 simple_lock(&mountlist_slock); 107 count = 0; 108 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) { 109 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) { 110 nmp = mp->mnt_list.cqe_next; 111 continue; 112 } 113 if (sfsp && count < maxcount) { 114 sp = &mp->mnt_stat; 115 /* 116 * If MNT_NOWAIT or MNT_LAZY is specified, do not 117 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY 118 * overrides MNT_NOWAIT. 119 */ 120 if (SCARG(uap, flags) != MNT_NOWAIT && 121 SCARG(uap, flags) != MNT_LAZY && 122 (SCARG(uap, flags) == MNT_WAIT || 123 SCARG(uap, flags) == 0) && 124 (error = VFS_STATVFS(mp, sp, l)) != 0) { 125 simple_lock(&mountlist_slock); 126 nmp = mp->mnt_list.cqe_next; 127 vfs_unbusy(mp); 128 continue; 129 } 130 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; 131 compat_20_netbsd32_from_statvfs(sp, &sb32); 132 error = copyout(&sb32, sfsp, sizeof(sb32)); 133 if (error) { 134 vfs_unbusy(mp); 135 return (error); 136 } 137 sfsp += sizeof(sb32); 138 } 139 count++; 140 simple_lock(&mountlist_slock); 141 nmp = mp->mnt_list.cqe_next; 142 vfs_unbusy(mp); 143 } 144 simple_unlock(&mountlist_slock); 145 if (sfsp && count > maxcount) 146 *retval = maxcount; 147 else 148 *retval = count; 149 return (0); 150 } 151 152 int 153 compat_20_netbsd32_statfs(l, v, retval) 154 struct lwp *l; 155 void *v; 156 register_t *retval; 157 { 158 struct compat_20_netbsd32_statfs_args /* { 159 syscallarg(const netbsd32_charp) path; 160 syscallarg(netbsd32_statfsp_t) buf; 161 } */ *uap = v; 162 struct mount *mp; 163 struct statvfs *sp; 164 struct netbsd32_statfs s32; 165 int error; 166 struct nameidata nd; 167 168 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, 169 (char *)NETBSD32PTR64(SCARG(uap, path)), l); 170 if ((error = namei(&nd)) != 0) 171 return (error); 172 mp = nd.ni_vp->v_mount; 173 sp = &mp->mnt_stat; 174 vrele(nd.ni_vp); 175 if ((error = VFS_STATVFS(mp, sp, l)) != 0) 176 return (error); 177 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; 178 compat_20_netbsd32_from_statvfs(sp, &s32); 179 return (copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, buf)), 180 sizeof(s32))); 181 } 182 183 int 184 compat_20_netbsd32_fstatfs(l, v, retval) 185 struct lwp *l; 186 void *v; 187 register_t *retval; 188 { 189 struct compat_20_netbsd32_fstatfs_args /* { 190 syscallarg(int) fd; 191 syscallarg(netbsd32_statfsp_t) buf; 192 } */ *uap = v; 193 struct file *fp; 194 struct mount *mp; 195 struct statvfs *sp; 196 struct netbsd32_statfs s32; 197 int error; 198 struct proc *p = l->l_proc; 199 200 /* getvnode() will use the descriptor for us */ 201 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 202 return (error); 203 mp = ((struct vnode *)fp->f_data)->v_mount; 204 sp = &mp->mnt_stat; 205 if ((error = VFS_STATVFS(mp, sp, l)) != 0) 206 goto out; 207 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; 208 compat_20_netbsd32_from_statvfs(sp, &s32); 209 error = copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, buf)), 210 sizeof(s32)); 211 out: 212 FILE_UNUSE(fp, l); 213 return (error); 214 } 215 216 int 217 compat_20_netbsd32_fhstatfs(l, v, retval) 218 struct lwp *l; 219 void *v; 220 register_t *retval; 221 { 222 struct compat_20_netbsd32_fhstatfs_args /* { 223 syscallarg(const netbsd32_fhandlep_t) fhp; 224 syscallarg(struct statvfs *) buf; 225 } */ *uap = v; 226 struct sys_fhstatvfs1_args ua; 227 228 NETBSD32TOP_UAP(fhp, const fhandle_t); 229 NETBSD32TOP_UAP(buf, struct statvfs); 230 #ifdef notyet 231 NETBSD32TOP_UAP(flags, int); 232 #endif 233 return (sys_fhstatvfs1(l, &ua, retval)); 234 } 235