xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_20.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /*	$NetBSD: netbsd32_compat_20.c,v 1.23 2008/04/30 12:49:16 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.23 2008/04/30 12:49:16 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(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, const struct compat_20_netbsd32_getfsstat_args *uap, register_t *retval)
92 {
93 	/* {
94 		syscallarg(netbsd32_statfsp_t) buf;
95 		syscallarg(netbsd32_long) bufsize;
96 		syscallarg(int) flags;
97 	} */
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_trybusy(mp, RW_READER, &nmp)) {
110 			continue;
111 		}
112 		if (sfsp && count < maxcount) {
113 			sp = &mp->mnt_stat;
114 			/*
115 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
116 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
117 			 * overrides MNT_NOWAIT.
118 			 */
119 			if (SCARG(uap, flags) != MNT_NOWAIT &&
120 			    SCARG(uap, flags) != MNT_LAZY &&
121 			    (SCARG(uap, flags) == MNT_WAIT ||
122 			     SCARG(uap, flags) == 0) &&
123 			    (error = VFS_STATVFS(mp, sp)) != 0) {
124 				mutex_enter(&mountlist_lock);
125 				vfs_unbusy(mp, false, &nmp);
126 				continue;
127 			}
128 			sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
129 			compat_20_netbsd32_from_statvfs(sp, &sb32);
130 			error = copyout(&sb32, sfsp, sizeof(sb32));
131 			if (error) {
132 				vfs_unbusy(mp, false, NULL);
133 				return (error);
134 			}
135 			sfsp = (char *)sfsp + sizeof(sb32);
136 		}
137 		count++;
138 		mutex_enter(&mountlist_lock);
139 		vfs_unbusy(mp, false, &nmp);
140 	}
141 	mutex_exit(&mountlist_lock);
142 	if (sfsp && count > maxcount)
143 		*retval = maxcount;
144 	else
145 		*retval = count;
146 	return (0);
147 }
148 
149 int
150 compat_20_netbsd32_statfs(struct lwp *l, const struct compat_20_netbsd32_statfs_args *uap, register_t *retval)
151 {
152 	/* {
153 		syscallarg(const netbsd32_charp) path;
154 		syscallarg(netbsd32_statfsp_t) buf;
155 	} */
156 	struct mount *mp;
157 	struct statvfs *sp;
158 	struct netbsd32_statfs s32;
159 	int error;
160 	struct nameidata nd;
161 
162 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
163 	    SCARG_P32(uap, path));
164 	if ((error = namei(&nd)) != 0)
165 		return (error);
166 	mp = nd.ni_vp->v_mount;
167 	sp = &mp->mnt_stat;
168 	vrele(nd.ni_vp);
169 	if ((error = VFS_STATVFS(mp, sp)) != 0)
170 		return (error);
171 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
172 	compat_20_netbsd32_from_statvfs(sp, &s32);
173 	return copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
174 }
175 
176 int
177 compat_20_netbsd32_fstatfs(struct lwp *l, const struct compat_20_netbsd32_fstatfs_args *uap, register_t *retval)
178 {
179 	/* {
180 		syscallarg(int) fd;
181 		syscallarg(netbsd32_statfsp_t) buf;
182 	} */
183 	file_t *fp;
184 	struct mount *mp;
185 	struct statvfs *sp;
186 	struct netbsd32_statfs s32;
187 	int error;
188 
189 	/* getvnode() will use the descriptor for us */
190 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
191 		return (error);
192 	mp = ((struct vnode *)fp->f_data)->v_mount;
193 	sp = &mp->mnt_stat;
194 	if ((error = VFS_STATVFS(mp, sp)) != 0)
195 		goto out;
196 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
197 	compat_20_netbsd32_from_statvfs(sp, &s32);
198 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
199  out:
200 	fd_putfile(SCARG(uap, fd));
201 	return (error);
202 }
203 
204 int
205 compat_20_netbsd32_fhstatfs(struct lwp *l, const struct compat_20_netbsd32_fhstatfs_args *uap, register_t *retval)
206 {
207 	/* {
208 		syscallarg(const netbsd32_fhandlep_t) fhp;
209 		syscallarg(struct statvfs *) buf;
210 	} */
211 	struct compat_30_sys_fhstatvfs1_args ua;
212 
213 	NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
214 	NETBSD32TOP_UAP(buf, struct statvfs);
215 #ifdef notyet
216 	NETBSD32TOP_UAP(flags, int);
217 #endif
218 	return (compat_30_sys_fhstatvfs1(l, &ua, retval));
219 }
220