xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_20.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: netbsd32_compat_20.c,v 1.15 2007/10/10 22:00:53 ad 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.15 2007/10/10 22:00:53 ad 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/syscallargs.h>
46 #include <sys/proc.h>
47 #include <sys/dirent.h>
48 
49 #include <compat/netbsd32/netbsd32.h>
50 #include <compat/netbsd32/netbsd32_syscallargs.h>
51 #include <compat/netbsd32/netbsd32_conv.h>
52 
53 static inline void compat_20_netbsd32_from_statvfs(struct statvfs *,
54     struct netbsd32_statfs *);
55 
56 static inline void
57 compat_20_netbsd32_from_statvfs(sbp, sb32p)
58 	struct statvfs *sbp;
59 	struct netbsd32_statfs *sb32p;
60 {
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 #if 1
76 	/* May as well do the whole batch in one go */
77 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
78 	    sizeof(sb32p->f_fstypename) +
79 	    sizeof(sb32p->f_mntonname) +
80 	    sizeof(sb32p->f_mntfromname));
81 #else
82 	/* If we want to be careful */
83 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
84 	    sizeof(sb32p->f_fstypename));
85 	(void)memcpy(sb32p->f_mntonname, sbp->f_mntonname,
86 	    sizeof(sb32p->f_mntonname));
87 	(void)memcpy(sb32p->f_mntfromname, sbp->f_mntfromname,
88 	    sizeof(sb32p->f_mntfromname));
89 #endif
90 }
91 
92 int
93 compat_20_netbsd32_getfsstat(l, v, retval)
94 	struct lwp *l;
95 	void *v;
96 	register_t *retval;
97 {
98 	struct compat_20_netbsd32_getfsstat_args /* {
99 		syscallarg(netbsd32_statfsp_t) buf;
100 		syscallarg(netbsd32_long) bufsize;
101 		syscallarg(int) flags;
102 	} */ *uap = v;
103 	struct mount *mp, *nmp;
104 	struct statvfs *sp;
105 	struct netbsd32_statfs sb32;
106 	void *sfsp;
107 	long count, maxcount, error;
108 
109 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
110 	sfsp = SCARG_P32(uap, buf);
111 	mutex_enter(&mountlist_lock);
112 	count = 0;
113 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
114 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
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_LAZY
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, l)) != 0) {
130 				mutex_enter(&mountlist_lock);
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 = (char *)sfsp + sizeof(sb32);
143 		}
144 		count++;
145 		mutex_enter(&mountlist_lock);
146 		nmp = mp->mnt_list.cqe_next;
147 		vfs_unbusy(mp);
148 	}
149 	mutex_exit(&mountlist_lock);
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 
173 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG_P32(uap, path), l);
174 	if ((error = namei(&nd)) != 0)
175 		return (error);
176 	mp = nd.ni_vp->v_mount;
177 	sp = &mp->mnt_stat;
178 	vrele(nd.ni_vp);
179 	if ((error = VFS_STATVFS(mp, sp, l)) != 0)
180 		return (error);
181 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
182 	compat_20_netbsd32_from_statvfs(sp, &s32);
183 	return copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
184 }
185 
186 int
187 compat_20_netbsd32_fstatfs(l, v, retval)
188 	struct lwp *l;
189 	void *v;
190 	register_t *retval;
191 {
192 	struct compat_20_netbsd32_fstatfs_args /* {
193 		syscallarg(int) fd;
194 		syscallarg(netbsd32_statfsp_t) buf;
195 	} */ *uap = v;
196 	struct file *fp;
197 	struct mount *mp;
198 	struct statvfs *sp;
199 	struct netbsd32_statfs s32;
200 	int error;
201 	struct proc *p = l->l_proc;
202 
203 	/* getvnode() will use the descriptor for us */
204 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
205 		return (error);
206 	mp = ((struct vnode *)fp->f_data)->v_mount;
207 	sp = &mp->mnt_stat;
208 	if ((error = VFS_STATVFS(mp, sp, l)) != 0)
209 		goto out;
210 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
211 	compat_20_netbsd32_from_statvfs(sp, &s32);
212 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
213  out:
214 	FILE_UNUSE(fp, l);
215 	return (error);
216 }
217 
218 int
219 compat_20_netbsd32_fhstatfs(l, v, retval)
220 	struct lwp *l;
221 	void *v;
222 	register_t *retval;
223 {
224 	struct compat_20_netbsd32_fhstatfs_args /* {
225 		syscallarg(const netbsd32_fhandlep_t) fhp;
226 		syscallarg(struct statvfs *) buf;
227 	} */ *uap = v;
228 	struct compat_30_sys_fhstatvfs1_args ua;
229 
230 	NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
231 	NETBSD32TOP_UAP(buf, struct statvfs);
232 #ifdef notyet
233 	NETBSD32TOP_UAP(flags, int);
234 #endif
235 	return (compat_30_sys_fhstatvfs1(l, &ua, retval));
236 }
237