netbsd32_compat_20.c revision 1.22 1 /* $NetBSD: netbsd32_compat_20.c,v 1.22 2008/04/30 06:49:23 jmmv 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.22 2008/04/30 06:49:23 jmmv 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 nmp = mp->mnt_list.cqe_next;
126 vfs_unbusy(mp, false);
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, false);
134 return (error);
135 }
136 sfsp = (char *)sfsp + sizeof(sb32);
137 }
138 count++;
139 mutex_enter(&mountlist_lock);
140 nmp = mp->mnt_list.cqe_next;
141 vfs_unbusy(mp, false);
142 }
143 mutex_exit(&mountlist_lock);
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(struct lwp *l, const struct compat_20_netbsd32_statfs_args *uap, register_t *retval)
153 {
154 /* {
155 syscallarg(const netbsd32_charp) path;
156 syscallarg(netbsd32_statfsp_t) buf;
157 } */
158 struct mount *mp;
159 struct statvfs *sp;
160 struct netbsd32_statfs s32;
161 int error;
162 struct nameidata nd;
163
164 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
165 SCARG_P32(uap, path));
166 if ((error = namei(&nd)) != 0)
167 return (error);
168 mp = nd.ni_vp->v_mount;
169 sp = &mp->mnt_stat;
170 vrele(nd.ni_vp);
171 if ((error = VFS_STATVFS(mp, sp)) != 0)
172 return (error);
173 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
174 compat_20_netbsd32_from_statvfs(sp, &s32);
175 return copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
176 }
177
178 int
179 compat_20_netbsd32_fstatfs(struct lwp *l, const struct compat_20_netbsd32_fstatfs_args *uap, register_t *retval)
180 {
181 /* {
182 syscallarg(int) fd;
183 syscallarg(netbsd32_statfsp_t) buf;
184 } */
185 file_t *fp;
186 struct mount *mp;
187 struct statvfs *sp;
188 struct netbsd32_statfs s32;
189 int error;
190
191 /* getvnode() will use the descriptor for us */
192 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
193 return (error);
194 mp = ((struct vnode *)fp->f_data)->v_mount;
195 sp = &mp->mnt_stat;
196 if ((error = VFS_STATVFS(mp, sp)) != 0)
197 goto out;
198 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
199 compat_20_netbsd32_from_statvfs(sp, &s32);
200 error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
201 out:
202 fd_putfile(SCARG(uap, fd));
203 return (error);
204 }
205
206 int
207 compat_20_netbsd32_fhstatfs(struct lwp *l, const struct compat_20_netbsd32_fhstatfs_args *uap, register_t *retval)
208 {
209 /* {
210 syscallarg(const netbsd32_fhandlep_t) fhp;
211 syscallarg(struct statvfs *) buf;
212 } */
213 struct compat_30_sys_fhstatvfs1_args ua;
214
215 NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
216 NETBSD32TOP_UAP(buf, struct statvfs);
217 #ifdef notyet
218 NETBSD32TOP_UAP(flags, int);
219 #endif
220 return (compat_30_sys_fhstatvfs1(l, &ua, retval));
221 }
222