1 /* $NetBSD: netbsd32_compat_20.c,v 1.42 2021/09/07 11:43:05 riastradh 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_20.c,v 1.42 2021/09/07 11:43:05 riastradh Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/mount.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <sys/ktrace.h>
39 #include <sys/vnode.h>
40 #include <sys/socket.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/namei.h>
44 #include <sys/syscallargs.h>
45 #include <sys/syscallvar.h>
46 #include <sys/proc.h>
47 #include <sys/dirent.h>
48
49 #include <compat/netbsd32/netbsd32.h>
50 #include <compat/netbsd32/netbsd32_syscall.h>
51 #include <compat/netbsd32/netbsd32_syscallargs.h>
52 #include <compat/netbsd32/netbsd32_conv.h>
53
54 static inline void compat_20_netbsd32_from_statvfs(struct statvfs *,
55 struct netbsd32_statfs *);
56
57 static inline void
compat_20_netbsd32_from_statvfs(struct statvfs * sbp,struct netbsd32_statfs * sb32p)58 compat_20_netbsd32_from_statvfs(struct statvfs *sbp, struct netbsd32_statfs *sb32p)
59 {
60
61 memset(sb32p, 0, sizeof(*sb32p));
62 sb32p->f_type = 0; /* XXX Put an actual value? */
63 sb32p->f_flags = sbp->f_flag;
64 sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
65 sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
66 sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
67 sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
68 sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
69 sb32p->f_files = (netbsd32_long)sbp->f_files;
70 sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
71 sb32p->f_fsid = sbp->f_fsidx;
72 sb32p->f_owner = sbp->f_owner;
73 sb32p->f_spare[0] = 0;
74 sb32p->f_spare[1] = 0;
75 sb32p->f_spare[2] = 0;
76 sb32p->f_spare[3] = 0;
77 (void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
78 sizeof(sb32p->f_fstypename));
79 (void)memcpy(sb32p->f_mntonname, sbp->f_mntonname,
80 sizeof(sb32p->f_mntonname));
81 (void)memcpy(sb32p->f_mntfromname, sbp->f_mntfromname,
82 sizeof(sb32p->f_mntfromname));
83 }
84
85 int
compat_20_netbsd32_getfsstat(struct lwp * l,const struct compat_20_netbsd32_getfsstat_args * uap,register_t * retval)86 compat_20_netbsd32_getfsstat(struct lwp *l, const struct compat_20_netbsd32_getfsstat_args *uap, register_t *retval)
87 {
88 /* {
89 syscallarg(netbsd32_statfsp_t) buf;
90 syscallarg(netbsd32_long) bufsize;
91 syscallarg(int) flags;
92 } */
93 int root = 0;
94 struct proc *p = l->l_proc;
95 mount_iterator_t *iter;
96 struct mount *mp;
97 struct statvfs *sb;
98 struct netbsd32_statfs sb32;
99 void *sfsp;
100 size_t count, maxcount;
101 int error = 0;
102
103 sb = STATVFSBUF_GET();
104 maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
105 sfsp = SCARG_P32(uap, buf);
106 mountlist_iterator_init(&iter);
107 count = 0;
108 while ((mp = mountlist_iterator_next(iter)) != NULL) {
109 if (sfsp && count < maxcount) {
110 error = dostatvfs(mp, sb, l, SCARG(uap, flags), 0);
111 if (error) {
112 error = 0;
113 continue;
114 }
115 compat_20_netbsd32_from_statvfs(sb, &sb32);
116 error = copyout(&sb32, sfsp, sizeof(sb32));
117 if (error)
118 goto out;
119 sfsp = (char *)sfsp + sizeof(sb32);
120 root |= strcmp(sb->f_mntonname, "/") == 0;
121 }
122 count++;
123 }
124
125 if (root == 0 && p->p_cwdi->cwdi_rdir) {
126 /*
127 * fake a root entry
128 */
129 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
130 sb, l, SCARG(uap, flags), 1);
131 if (error != 0)
132 goto out;
133 if (sfsp) {
134 compat_20_netbsd32_from_statvfs(sb, &sb32);
135 error = copyout(&sb32, sfsp, sizeof(sb32));
136 if (error != 0)
137 goto out;
138 }
139 count++;
140 }
141
142 if (sfsp && count > maxcount)
143 *retval = maxcount;
144 else
145 *retval = count;
146 out:
147 mountlist_iterator_destroy(iter);
148 STATVFSBUF_PUT(sb);
149 return error;
150 }
151
152 int
compat_20_netbsd32_statfs(struct lwp * l,const struct compat_20_netbsd32_statfs_args * uap,register_t * retval)153 compat_20_netbsd32_statfs(struct lwp *l, const struct compat_20_netbsd32_statfs_args *uap, register_t *retval)
154 {
155 /* {
156 syscallarg(const netbsd32_charp) path;
157 syscallarg(netbsd32_statfsp_t) buf;
158 } */
159 struct mount *mp;
160 struct statvfs *sb;
161 struct netbsd32_statfs s32;
162 int error;
163 struct vnode *vp;
164
165 error = namei_simple_user(SCARG_P32(uap, path),
166 NSM_FOLLOW_TRYEMULROOT, &vp);
167 if (error != 0)
168 return error;
169 mp = vp->v_mount;
170 vrele(vp);
171 sb = STATVFSBUF_GET();
172 if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
173 goto out;
174 compat_20_netbsd32_from_statvfs(sb, &s32);
175 error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
176 out:
177 STATVFSBUF_PUT(sb);
178 return error;
179 }
180
181 int
compat_20_netbsd32_fstatfs(struct lwp * l,const struct compat_20_netbsd32_fstatfs_args * uap,register_t * retval)182 compat_20_netbsd32_fstatfs(struct lwp *l, const struct compat_20_netbsd32_fstatfs_args *uap, register_t *retval)
183 {
184 /* {
185 syscallarg(int) fd;
186 syscallarg(netbsd32_statfsp_t) buf;
187 } */
188 file_t *fp;
189 struct mount *mp;
190 struct statvfs *sb;
191 struct netbsd32_statfs s32;
192 int error;
193
194 /* fd_getvnode() will use the descriptor for us */
195 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
196 return error;
197 mp = fp->f_vnode->v_mount;
198 sb = STATVFSBUF_GET();
199 if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
200 goto out;
201 compat_20_netbsd32_from_statvfs(sb, &s32);
202 error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
203 out:
204 STATVFSBUF_PUT(sb);
205 fd_putfile(SCARG(uap, fd));
206 return error;
207 }
208
209 int
compat_20_netbsd32_fhstatfs(struct lwp * l,const struct compat_20_netbsd32_fhstatfs_args * uap,register_t * retval)210 compat_20_netbsd32_fhstatfs(struct lwp *l, const struct compat_20_netbsd32_fhstatfs_args *uap, register_t *retval)
211 {
212 /* {
213 syscallarg(const netbsd32_fhandlep_t) fhp;
214 syscallarg(struct statvfs *) buf;
215 } */
216 struct compat_30_sys_fhstatvfs1_args ua;
217
218 NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
219 NETBSD32TOP_UAP(buf, struct statvfs);
220 #ifdef notyet
221 NETBSD32TOP_UAP(flags, int);
222 #endif
223 return compat_30_sys_fhstatvfs1(l, &ua, retval);
224 }
225
226 static struct syscall_package compat_netbsd32_20_syscalls[] = {
227 { NETBSD32_SYS_compat_20_netbsd32_statfs, 0,
228 (sy_call_t *)compat_20_netbsd32_statfs },
229 { NETBSD32_SYS_compat_20_netbsd32_fstatfs, 0,
230 (sy_call_t *)compat_20_netbsd32_fstatfs },
231 { NETBSD32_SYS_compat_20_netbsd32_fhstatfs, 0,
232 (sy_call_t *)compat_20_netbsd32_fhstatfs },
233 { NETBSD32_SYS_compat_20_netbsd32_getfsstat, 0,
234 (sy_call_t *)compat_20_netbsd32_getfsstat },
235 { 0, 0, NULL }
236 };
237
238 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_20, "compat_netbsd32_30,compat_20");
239
240 static int
compat_netbsd32_20_modcmd(modcmd_t cmd,void * arg)241 compat_netbsd32_20_modcmd(modcmd_t cmd, void *arg)
242 {
243
244 switch (cmd) {
245 case MODULE_CMD_INIT:
246 return syscall_establish(&emul_netbsd32,
247 compat_netbsd32_20_syscalls);
248
249 case MODULE_CMD_FINI:
250 return syscall_disestablish(&emul_netbsd32,
251 compat_netbsd32_20_syscalls);
252
253 default:
254 return ENOTTY;
255 }
256 }
257