netbsd32_compat_20.c revision 1.10 1 /* $NetBSD: netbsd32_compat_20.c,v 1.10 2007/03/18 21:38:33 dsl 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.10 2007/03/18 21:38:33 dsl 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 memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN+MNAMELEN+MNAMELEN);
78 #else
79 /* If we want to be careful */
80 memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN);
81 memcpy(sb32p->f_mntonname, sbp->f_mntonname, MNAMELEN);
82 memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, MNAMELEN);
83 #endif
84 }
85
86 int
87 compat_20_netbsd32_getfsstat(l, v, retval)
88 struct lwp *l;
89 void *v;
90 register_t *retval;
91 {
92 struct compat_20_netbsd32_getfsstat_args /* {
93 syscallarg(netbsd32_statfsp_t) buf;
94 syscallarg(netbsd32_long) bufsize;
95 syscallarg(int) flags;
96 } */ *uap = v;
97 struct mount *mp, *nmp;
98 struct statvfs *sp;
99 struct netbsd32_statfs sb32;
100 void *sfsp;
101 long count, maxcount, error;
102
103 maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
104 sfsp = SCARG_P32(uap, buf);
105 simple_lock(&mountlist_slock);
106 count = 0;
107 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
108 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
109 nmp = mp->mnt_list.cqe_next;
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_LAXY
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, l)) != 0) {
124 simple_lock(&mountlist_slock);
125 nmp = mp->mnt_list.cqe_next;
126 vfs_unbusy(mp);
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);
134 return (error);
135 }
136 sfsp = (char *)sfsp + sizeof(sb32);
137 }
138 count++;
139 simple_lock(&mountlist_slock);
140 nmp = mp->mnt_list.cqe_next;
141 vfs_unbusy(mp);
142 }
143 simple_unlock(&mountlist_slock);
144 if (sfsp && count > maxcount)
145 *retval = maxcount;
146 else
147 *retval = count;
148 return (0);
149 }
150
151 int
152 compat_20_netbsd32_statfs(l, v, retval)
153 struct lwp *l;
154 void *v;
155 register_t *retval;
156 {
157 struct compat_20_netbsd32_statfs_args /* {
158 syscallarg(const netbsd32_charp) path;
159 syscallarg(netbsd32_statfsp_t) buf;
160 } */ *uap = v;
161 struct mount *mp;
162 struct statvfs *sp;
163 struct netbsd32_statfs s32;
164 int error;
165 struct nameidata nd;
166
167 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG_P32(uap, path), l);
168 if ((error = namei(&nd)) != 0)
169 return (error);
170 mp = nd.ni_vp->v_mount;
171 sp = &mp->mnt_stat;
172 vrele(nd.ni_vp);
173 if ((error = VFS_STATVFS(mp, sp, l)) != 0)
174 return (error);
175 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
176 compat_20_netbsd32_from_statvfs(sp, &s32);
177 return copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
178 }
179
180 int
181 compat_20_netbsd32_fstatfs(l, v, retval)
182 struct lwp *l;
183 void *v;
184 register_t *retval;
185 {
186 struct compat_20_netbsd32_fstatfs_args /* {
187 syscallarg(int) fd;
188 syscallarg(netbsd32_statfsp_t) buf;
189 } */ *uap = v;
190 struct file *fp;
191 struct mount *mp;
192 struct statvfs *sp;
193 struct netbsd32_statfs s32;
194 int error;
195 struct proc *p = l->l_proc;
196
197 /* getvnode() will use the descriptor for us */
198 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
199 return (error);
200 mp = ((struct vnode *)fp->f_data)->v_mount;
201 sp = &mp->mnt_stat;
202 if ((error = VFS_STATVFS(mp, sp, l)) != 0)
203 goto out;
204 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
205 compat_20_netbsd32_from_statvfs(sp, &s32);
206 error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
207 out:
208 FILE_UNUSE(fp, l);
209 return (error);
210 }
211
212 int
213 compat_20_netbsd32_fhstatfs(l, v, retval)
214 struct lwp *l;
215 void *v;
216 register_t *retval;
217 {
218 struct compat_20_netbsd32_fhstatfs_args /* {
219 syscallarg(const netbsd32_fhandlep_t) fhp;
220 syscallarg(struct statvfs *) buf;
221 } */ *uap = v;
222 struct compat_30_sys_fhstatvfs1_args ua;
223
224 NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
225 NETBSD32TOP_UAP(buf, struct statvfs);
226 #ifdef notyet
227 NETBSD32TOP_UAP(flags, int);
228 #endif
229 return (compat_30_sys_fhstatvfs1(l, &ua, retval));
230 }
231