xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_20.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: netbsd32_compat_20.c,v 1.18 2007/12/08 19:29:39 pooka 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.18 2007/12/08 19:29:39 pooka 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(struct statvfs *sbp, struct netbsd32_statfs *sb32p)
58 {
59 	sb32p->f_flags = sbp->f_flag;
60 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
61 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
62 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
63 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
64 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
65 	sb32p->f_files = (netbsd32_long)sbp->f_files;
66 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
67 	sb32p->f_fsid = sbp->f_fsidx;
68 	sb32p->f_owner = sbp->f_owner;
69 	sb32p->f_spare[0] = 0;
70 	sb32p->f_spare[1] = 0;
71 	sb32p->f_spare[2] = 0;
72 	sb32p->f_spare[3] = 0;
73 #if 1
74 	/* May as well do the whole batch in one go */
75 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
76 	    sizeof(sb32p->f_fstypename) +
77 	    sizeof(sb32p->f_mntonname) +
78 	    sizeof(sb32p->f_mntfromname));
79 #else
80 	/* If we want to be careful */
81 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
82 	    sizeof(sb32p->f_fstypename));
83 	(void)memcpy(sb32p->f_mntonname, sbp->f_mntonname,
84 	    sizeof(sb32p->f_mntonname));
85 	(void)memcpy(sb32p->f_mntfromname, sbp->f_mntfromname,
86 	    sizeof(sb32p->f_mntfromname));
87 #endif
88 }
89 
90 int
91 compat_20_netbsd32_getfsstat(struct lwp *l, void *v, 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 	void *sfsp;
102 	long count, maxcount, error;
103 
104 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
105 	sfsp = SCARG_P32(uap, buf);
106 	mutex_enter(&mountlist_lock);
107 	count = 0;
108 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
109 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_lock)) {
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_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 				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 = (char *)sfsp + sizeof(sb32);
138 		}
139 		count++;
140 		mutex_enter(&mountlist_lock);
141 		nmp = mp->mnt_list.cqe_next;
142 		vfs_unbusy(mp);
143 	}
144 	mutex_exit(&mountlist_lock);
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(struct lwp *l, void *v, register_t *retval)
154 {
155 	struct compat_20_netbsd32_statfs_args /* {
156 		syscallarg(const netbsd32_charp) path;
157 		syscallarg(netbsd32_statfsp_t) buf;
158 	} */ *uap = v;
159 	struct mount *mp;
160 	struct statvfs *sp;
161 	struct netbsd32_statfs s32;
162 	int error;
163 	struct nameidata nd;
164 
165 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
166 	    SCARG_P32(uap, path));
167 	if ((error = namei(&nd)) != 0)
168 		return (error);
169 	mp = nd.ni_vp->v_mount;
170 	sp = &mp->mnt_stat;
171 	vrele(nd.ni_vp);
172 	if ((error = VFS_STATVFS(mp, sp)) != 0)
173 		return (error);
174 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
175 	compat_20_netbsd32_from_statvfs(sp, &s32);
176 	return copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
177 }
178 
179 int
180 compat_20_netbsd32_fstatfs(struct lwp *l, void *v, register_t *retval)
181 {
182 	struct compat_20_netbsd32_fstatfs_args /* {
183 		syscallarg(int) fd;
184 		syscallarg(netbsd32_statfsp_t) buf;
185 	} */ *uap = v;
186 	struct file *fp;
187 	struct mount *mp;
188 	struct statvfs *sp;
189 	struct netbsd32_statfs s32;
190 	int error;
191 	struct proc *p = l->l_proc;
192 
193 	/* getvnode() will use the descriptor for us */
194 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
195 		return (error);
196 	mp = ((struct vnode *)fp->f_data)->v_mount;
197 	sp = &mp->mnt_stat;
198 	if ((error = VFS_STATVFS(mp, sp)) != 0)
199 		goto out;
200 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
201 	compat_20_netbsd32_from_statvfs(sp, &s32);
202 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
203  out:
204 	FILE_UNUSE(fp, l);
205 	return (error);
206 }
207 
208 int
209 compat_20_netbsd32_fhstatfs(struct lwp *l, void *v, register_t *retval)
210 {
211 	struct compat_20_netbsd32_fhstatfs_args /* {
212 		syscallarg(const netbsd32_fhandlep_t) fhp;
213 		syscallarg(struct statvfs *) buf;
214 	} */ *uap = v;
215 	struct compat_30_sys_fhstatvfs1_args ua;
216 
217 	NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
218 	NETBSD32TOP_UAP(buf, struct statvfs);
219 #ifdef notyet
220 	NETBSD32TOP_UAP(flags, int);
221 #endif
222 	return (compat_30_sys_fhstatvfs1(l, &ua, retval));
223 }
224