Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_20.c revision 1.2
      1 /*	$NetBSD: netbsd32_compat_20.c,v 1.2 2005/08/19 04:24:38 christos 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.2 2005/08/19 04:24:38 christos Exp $");
     33 
     34 #if defined(_KERNEL_OPT)
     35 #include "opt_ktrace.h"
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/malloc.h>
     41 #include <sys/mount.h>
     42 #include <sys/stat.h>
     43 #include <sys/time.h>
     44 #include <sys/ktrace.h>
     45 #include <sys/vnode.h>
     46 #include <sys/file.h>
     47 #include <sys/filedesc.h>
     48 #include <sys/namei.h>
     49 #include <sys/sa.h>
     50 #include <sys/syscallargs.h>
     51 #include <sys/proc.h>
     52 #include <sys/dirent.h>
     53 
     54 #include <compat/netbsd32/netbsd32.h>
     55 #include <compat/netbsd32/netbsd32_syscallargs.h>
     56 #include <compat/netbsd32/netbsd32_conv.h>
     57 
     58 static __inline void compat_20_netbsd32_from_statvfs(struct statvfs *,
     59     struct netbsd32_statfs *);
     60 
     61 static __inline void
     62 compat_20_netbsd32_from_statvfs(sbp, sb32p)
     63 	struct statvfs *sbp;
     64 	struct netbsd32_statfs *sb32p;
     65 {
     66 	sb32p->f_flags = sbp->f_flag;
     67 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
     68 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
     69 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
     70 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
     71 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
     72 	sb32p->f_files = (netbsd32_long)sbp->f_files;
     73 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
     74 	sb32p->f_fsid = sbp->f_fsidx;
     75 	sb32p->f_owner = sbp->f_owner;
     76 	sb32p->f_spare[0] = 0;
     77 	sb32p->f_spare[1] = 0;
     78 	sb32p->f_spare[2] = 0;
     79 	sb32p->f_spare[3] = 0;
     80 #if 1
     81 	/* May as well do the whole batch in one go */
     82 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN+MNAMELEN+MNAMELEN);
     83 #else
     84 	/* If we want to be careful */
     85 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN);
     86 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, MNAMELEN);
     87 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, MNAMELEN);
     88 #endif
     89 }
     90 
     91 int
     92 compat_20_netbsd32_getfsstat(l, v, retval)
     93 	struct lwp *l;
     94 	void *v;
     95 	register_t *retval;
     96 {
     97 	struct compat_20_netbsd32_getfsstat_args /* {
     98 		syscallarg(netbsd32_statfsp_t) buf;
     99 		syscallarg(netbsd32_long) bufsize;
    100 		syscallarg(int) flags;
    101 	} */ *uap = v;
    102 	struct mount *mp, *nmp;
    103 	struct statvfs *sp;
    104 	struct netbsd32_statfs sb32;
    105 	caddr_t sfsp;
    106 	long count, maxcount, error;
    107 	struct proc *p = l->l_proc;
    108 
    109 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
    110 	sfsp = (caddr_t)NETBSD32PTR64(SCARG(uap, buf));
    111 	simple_lock(&mountlist_slock);
    112 	count = 0;
    113 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    114 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    115 			nmp = mp->mnt_list.cqe_next;
    116 			continue;
    117 		}
    118 		if (sfsp && count < maxcount) {
    119 			sp = &mp->mnt_stat;
    120 			/*
    121 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    122 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
    123 			 * overrides MNT_NOWAIT.
    124 			 */
    125 			if (SCARG(uap, flags) != MNT_NOWAIT &&
    126 			    SCARG(uap, flags) != MNT_LAZY &&
    127 			    (SCARG(uap, flags) == MNT_WAIT ||
    128 			     SCARG(uap, flags) == 0) &&
    129 			    (error = VFS_STATVFS(mp, sp, p)) != 0) {
    130 				simple_lock(&mountlist_slock);
    131 				nmp = mp->mnt_list.cqe_next;
    132 				vfs_unbusy(mp);
    133 				continue;
    134 			}
    135 			sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    136 			compat_20_netbsd32_from_statvfs(sp, &sb32);
    137 			error = copyout(&sb32, sfsp, sizeof(sb32));
    138 			if (error) {
    139 				vfs_unbusy(mp);
    140 				return (error);
    141 			}
    142 			sfsp += sizeof(sb32);
    143 		}
    144 		count++;
    145 		simple_lock(&mountlist_slock);
    146 		nmp = mp->mnt_list.cqe_next;
    147 		vfs_unbusy(mp);
    148 	}
    149 	simple_unlock(&mountlist_slock);
    150 	if (sfsp && count > maxcount)
    151 		*retval = maxcount;
    152 	else
    153 		*retval = count;
    154 	return (0);
    155 }
    156 
    157 int
    158 compat_20_netbsd32_statfs(l, v, retval)
    159 	struct lwp *l;
    160 	void *v;
    161 	register_t *retval;
    162 {
    163 	struct compat_20_netbsd32_statfs_args /* {
    164 		syscallarg(const netbsd32_charp) path;
    165 		syscallarg(netbsd32_statfsp_t) buf;
    166 	} */ *uap = v;
    167 	struct mount *mp;
    168 	struct statvfs *sp;
    169 	struct netbsd32_statfs s32;
    170 	int error;
    171 	struct nameidata nd;
    172 	struct proc *p = l->l_proc;
    173 
    174 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
    175 	    (char *)NETBSD32PTR64(SCARG(uap, path)), p);
    176 	if ((error = namei(&nd)) != 0)
    177 		return (error);
    178 	mp = nd.ni_vp->v_mount;
    179 	sp = &mp->mnt_stat;
    180 	vrele(nd.ni_vp);
    181 	if ((error = VFS_STATVFS(mp, sp, p)) != 0)
    182 		return (error);
    183 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    184 	compat_20_netbsd32_from_statvfs(sp, &s32);
    185 	return (copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, buf)),
    186 	    sizeof(s32)));
    187 }
    188 
    189 int
    190 compat_20_netbsd32_fstatfs(l, v, retval)
    191 	struct lwp *l;
    192 	void *v;
    193 	register_t *retval;
    194 {
    195 	struct compat_20_netbsd32_fstatfs_args /* {
    196 		syscallarg(int) fd;
    197 		syscallarg(netbsd32_statfsp_t) buf;
    198 	} */ *uap = v;
    199 	struct file *fp;
    200 	struct mount *mp;
    201 	struct statvfs *sp;
    202 	struct netbsd32_statfs s32;
    203 	int error;
    204 	struct proc *p = l->l_proc;
    205 
    206 	/* getvnode() will use the descriptor for us */
    207 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    208 		return (error);
    209 	mp = ((struct vnode *)fp->f_data)->v_mount;
    210 	sp = &mp->mnt_stat;
    211 	if ((error = VFS_STATVFS(mp, sp, p)) != 0)
    212 		goto out;
    213 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    214 	compat_20_netbsd32_from_statvfs(sp, &s32);
    215 	error = copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, buf)),
    216 	    sizeof(s32));
    217  out:
    218 	FILE_UNUSE(fp, p);
    219 	return (error);
    220 }
    221 
    222 int
    223 compat_20_netbsd32_fhstatfs(l, v, retval)
    224 	struct lwp *l;
    225 	void *v;
    226 	register_t *retval;
    227 {
    228 	struct compat_20_netbsd32_fhstatfs_args /* {
    229 		syscallarg(const netbsd32_fhandlep_t) fhp;
    230 		syscallarg(struct statvfs *) buf;
    231 	} */ *uap = v;
    232 	struct sys_fhstatvfs1_args ua;
    233 
    234 	NETBSD32TOP_UAP(fhp, const fhandle_t);
    235 	NETBSD32TOP_UAP(buf, struct statvfs);
    236 #ifdef notyet
    237 	NETBSD32TOP_UAP(flags, int);
    238 #endif
    239 	return (sys_fhstatvfs1(l, &ua, retval));
    240 }
    241