Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_20.c revision 1.24.2.1
      1 /*	$NetBSD: netbsd32_compat_20.c,v 1.24.2.1 2008/05/10 23:48:57 wrstuden 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.24.2.1 2008/05/10 23:48:57 wrstuden 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(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 #if 1
     75 	/* May as well do the whole batch in one go */
     76 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
     77 	    sizeof(sb32p->f_fstypename) +
     78 	    sizeof(sb32p->f_mntonname) +
     79 	    sizeof(sb32p->f_mntfromname));
     80 #else
     81 	/* If we want to be careful */
     82 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
     83 	    sizeof(sb32p->f_fstypename));
     84 	(void)memcpy(sb32p->f_mntonname, sbp->f_mntonname,
     85 	    sizeof(sb32p->f_mntonname));
     86 	(void)memcpy(sb32p->f_mntfromname, sbp->f_mntfromname,
     87 	    sizeof(sb32p->f_mntfromname));
     88 #endif
     89 }
     90 
     91 int
     92 compat_20_netbsd32_getfsstat(struct lwp *l, const struct compat_20_netbsd32_getfsstat_args *uap, register_t *retval)
     93 {
     94 	/* {
     95 		syscallarg(netbsd32_statfsp_t) buf;
     96 		syscallarg(netbsd32_long) bufsize;
     97 		syscallarg(int) flags;
     98 	} */
     99 	struct mount *mp, *nmp;
    100 	struct statvfs *sp;
    101 	struct netbsd32_statfs sb32;
    102 	void *sfsp;
    103 	long count, maxcount, error;
    104 
    105 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
    106 	sfsp = SCARG_P32(uap, buf);
    107 	mutex_enter(&mountlist_lock);
    108 	count = 0;
    109 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    110 		if (vfs_busy(mp, &nmp)) {
    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_LAZY
    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)) != 0) {
    125 				mutex_enter(&mountlist_lock);
    126 				vfs_unbusy(mp, false, &nmp);
    127 				continue;
    128 			}
    129 			sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    130 			compat_20_netbsd32_from_statvfs(sp, &sb32);
    131 			error = copyout(&sb32, sfsp, sizeof(sb32));
    132 			if (error) {
    133 				vfs_unbusy(mp, false, NULL);
    134 				return (error);
    135 			}
    136 			sfsp = (char *)sfsp + sizeof(sb32);
    137 		}
    138 		count++;
    139 		mutex_enter(&mountlist_lock);
    140 		vfs_unbusy(mp, false, &nmp);
    141 	}
    142 	mutex_exit(&mountlist_lock);
    143 	if (sfsp && count > maxcount)
    144 		*retval = maxcount;
    145 	else
    146 		*retval = count;
    147 	return (0);
    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 *sp;
    159 	struct netbsd32_statfs s32;
    160 	int error;
    161 	struct nameidata nd;
    162 
    163 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
    164 	    SCARG_P32(uap, path));
    165 	if ((error = namei(&nd)) != 0)
    166 		return (error);
    167 	mp = nd.ni_vp->v_mount;
    168 	sp = &mp->mnt_stat;
    169 	vrele(nd.ni_vp);
    170 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    171 		return (error);
    172 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    173 	compat_20_netbsd32_from_statvfs(sp, &s32);
    174 	return copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
    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 *sp;
    187 	struct netbsd32_statfs s32;
    188 	int error;
    189 
    190 	/* getvnode() will use the descriptor for us */
    191 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    192 		return (error);
    193 	mp = ((struct vnode *)fp->f_data)->v_mount;
    194 	sp = &mp->mnt_stat;
    195 	if ((error = VFS_STATVFS(mp, sp)) != 0)
    196 		goto out;
    197 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    198 	compat_20_netbsd32_from_statvfs(sp, &s32);
    199 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
    200  out:
    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