netbsd32_compat_20.c revision 1.3 1 /* $NetBSD: netbsd32_compat_20.c,v 1.3 2005/12/11 12:20:22 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.3 2005/12/11 12:20:22 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
108 maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
109 sfsp = (caddr_t)NETBSD32PTR64(SCARG(uap, buf));
110 simple_lock(&mountlist_slock);
111 count = 0;
112 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
113 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
114 nmp = mp->mnt_list.cqe_next;
115 continue;
116 }
117 if (sfsp && count < maxcount) {
118 sp = &mp->mnt_stat;
119 /*
120 * If MNT_NOWAIT or MNT_LAZY is specified, do not
121 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
122 * overrides MNT_NOWAIT.
123 */
124 if (SCARG(uap, flags) != MNT_NOWAIT &&
125 SCARG(uap, flags) != MNT_LAZY &&
126 (SCARG(uap, flags) == MNT_WAIT ||
127 SCARG(uap, flags) == 0) &&
128 (error = VFS_STATVFS(mp, sp, l)) != 0) {
129 simple_lock(&mountlist_slock);
130 nmp = mp->mnt_list.cqe_next;
131 vfs_unbusy(mp);
132 continue;
133 }
134 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
135 compat_20_netbsd32_from_statvfs(sp, &sb32);
136 error = copyout(&sb32, sfsp, sizeof(sb32));
137 if (error) {
138 vfs_unbusy(mp);
139 return (error);
140 }
141 sfsp += sizeof(sb32);
142 }
143 count++;
144 simple_lock(&mountlist_slock);
145 nmp = mp->mnt_list.cqe_next;
146 vfs_unbusy(mp);
147 }
148 simple_unlock(&mountlist_slock);
149 if (sfsp && count > maxcount)
150 *retval = maxcount;
151 else
152 *retval = count;
153 return (0);
154 }
155
156 int
157 compat_20_netbsd32_statfs(l, v, retval)
158 struct lwp *l;
159 void *v;
160 register_t *retval;
161 {
162 struct compat_20_netbsd32_statfs_args /* {
163 syscallarg(const netbsd32_charp) path;
164 syscallarg(netbsd32_statfsp_t) buf;
165 } */ *uap = v;
166 struct mount *mp;
167 struct statvfs *sp;
168 struct netbsd32_statfs s32;
169 int error;
170 struct nameidata nd;
171
172 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
173 (char *)NETBSD32PTR64(SCARG(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, (caddr_t)NETBSD32PTR64(SCARG(uap, buf)),
184 sizeof(s32)));
185 }
186
187 int
188 compat_20_netbsd32_fstatfs(l, v, retval)
189 struct lwp *l;
190 void *v;
191 register_t *retval;
192 {
193 struct compat_20_netbsd32_fstatfs_args /* {
194 syscallarg(int) fd;
195 syscallarg(netbsd32_statfsp_t) buf;
196 } */ *uap = v;
197 struct file *fp;
198 struct mount *mp;
199 struct statvfs *sp;
200 struct netbsd32_statfs s32;
201 int error;
202 struct proc *p = l->l_proc;
203
204 /* getvnode() will use the descriptor for us */
205 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
206 return (error);
207 mp = ((struct vnode *)fp->f_data)->v_mount;
208 sp = &mp->mnt_stat;
209 if ((error = VFS_STATVFS(mp, sp, l)) != 0)
210 goto out;
211 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
212 compat_20_netbsd32_from_statvfs(sp, &s32);
213 error = copyout(&s32, (caddr_t)NETBSD32PTR64(SCARG(uap, buf)),
214 sizeof(s32));
215 out:
216 FILE_UNUSE(fp, l);
217 return (error);
218 }
219
220 int
221 compat_20_netbsd32_fhstatfs(l, v, retval)
222 struct lwp *l;
223 void *v;
224 register_t *retval;
225 {
226 struct compat_20_netbsd32_fhstatfs_args /* {
227 syscallarg(const netbsd32_fhandlep_t) fhp;
228 syscallarg(struct statvfs *) buf;
229 } */ *uap = v;
230 struct sys_fhstatvfs1_args ua;
231
232 NETBSD32TOP_UAP(fhp, const fhandle_t);
233 NETBSD32TOP_UAP(buf, struct statvfs);
234 #ifdef notyet
235 NETBSD32TOP_UAP(flags, int);
236 #endif
237 return (sys_fhstatvfs1(l, &ua, retval));
238 }
239