xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_20.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: netbsd32_compat_20.c,v 1.37 2018/05/10 02:36:07 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  *
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.37 2018/05/10 02:36:07 christos Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mount.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/ktrace.h>
38 #include <sys/vnode.h>
39 #include <sys/socket.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/namei.h>
43 #include <sys/syscallargs.h>
44 #include <sys/proc.h>
45 #include <sys/dirent.h>
46 
47 #include <compat/netbsd32/netbsd32.h>
48 #include <compat/netbsd32/netbsd32_syscallargs.h>
49 #include <compat/netbsd32/netbsd32_conv.h>
50 
51 static inline void compat_20_netbsd32_from_statvfs(struct statvfs *,
52     struct netbsd32_statfs *);
53 
54 static inline void
55 compat_20_netbsd32_from_statvfs(struct statvfs *sbp, struct netbsd32_statfs *sb32p)
56 {
57 	sb32p->f_flags = sbp->f_flag;
58 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
59 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
60 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
61 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
62 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
63 	sb32p->f_files = (netbsd32_long)sbp->f_files;
64 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
65 	sb32p->f_fsid = sbp->f_fsidx;
66 	sb32p->f_owner = sbp->f_owner;
67 	sb32p->f_spare[0] = 0;
68 	sb32p->f_spare[1] = 0;
69 	sb32p->f_spare[2] = 0;
70 	sb32p->f_spare[3] = 0;
71 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
72 	    sizeof(sb32p->f_fstypename));
73 	(void)memcpy(sb32p->f_mntonname, sbp->f_mntonname,
74 	    sizeof(sb32p->f_mntonname));
75 	(void)memcpy(sb32p->f_mntfromname, sbp->f_mntfromname,
76 	    sizeof(sb32p->f_mntfromname));
77 }
78 
79 int
80 compat_20_netbsd32_getfsstat(struct lwp *l, const struct compat_20_netbsd32_getfsstat_args *uap, register_t *retval)
81 {
82 	/* {
83 		syscallarg(netbsd32_statfsp_t) buf;
84 		syscallarg(netbsd32_long) bufsize;
85 		syscallarg(int) flags;
86 	} */
87 	int root = 0;
88 	struct proc *p = l->l_proc;
89 	mount_iterator_t *iter;
90 	struct mount *mp;
91 	struct statvfs *sb;
92 	struct netbsd32_statfs sb32;
93 	void *sfsp;
94 	size_t count, maxcount;
95 	int error = 0;
96 
97 	sb = STATVFSBUF_GET();
98 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
99 	sfsp = SCARG_P32(uap, buf);
100 	mountlist_iterator_init(&iter);
101 	count = 0;
102 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
103 		if (sfsp && count < maxcount) {
104 			error = dostatvfs(mp, sb, l, SCARG(uap, flags), 0);
105 			if (error) {
106 				error = 0;
107 				continue;
108 			}
109 			compat_20_netbsd32_from_statvfs(sb, &sb32);
110 			error = copyout(&sb32, sfsp, sizeof(sb32));
111 			if (error)
112 				goto out;
113 			sfsp = (char *)sfsp + sizeof(sb32);
114 			root |= strcmp(sb->f_mntonname, "/") == 0;
115 		}
116 		count++;
117 	}
118 
119 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
120 		/*
121 		 * fake a root entry
122 		 */
123 		error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
124 		    sb, l, SCARG(uap, flags), 1);
125 		if (error != 0)
126 			goto out;
127 		if (sfsp) {
128 			compat_20_netbsd32_from_statvfs(sb, &sb32);
129 			error = copyout(&sb32, sfsp, sizeof(sb32));
130 			if (error != 0)
131 				goto out;
132 		}
133 		count++;
134 	}
135 
136 	if (sfsp && count > maxcount)
137 		*retval = maxcount;
138 	else
139 		*retval = count;
140 out:
141 	mountlist_iterator_destroy(iter);
142 	STATVFSBUF_PUT(sb);
143 	return error;
144 }
145 
146 int
147 compat_20_netbsd32_statfs(struct lwp *l, const struct compat_20_netbsd32_statfs_args *uap, register_t *retval)
148 {
149 	/* {
150 		syscallarg(const netbsd32_charp) path;
151 		syscallarg(netbsd32_statfsp_t) buf;
152 	} */
153 	struct mount *mp;
154 	struct statvfs *sb;
155 	struct netbsd32_statfs s32;
156 	int error;
157 	struct vnode *vp;
158 
159 	error = namei_simple_user(SCARG_P32(uap, path),
160 				NSM_FOLLOW_TRYEMULROOT, &vp);
161 	if (error != 0)
162 		return (error);
163 	mp = vp->v_mount;
164 	vrele(vp);
165 	sb = STATVFSBUF_GET();
166 	if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
167 		goto out;
168 	compat_20_netbsd32_from_statvfs(sb, &s32);
169 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
170 out:
171 	STATVFSBUF_PUT(sb);
172 	return error;
173 }
174 
175 int
176 compat_20_netbsd32_fstatfs(struct lwp *l, const struct compat_20_netbsd32_fstatfs_args *uap, register_t *retval)
177 {
178 	/* {
179 		syscallarg(int) fd;
180 		syscallarg(netbsd32_statfsp_t) buf;
181 	} */
182 	file_t *fp;
183 	struct mount *mp;
184 	struct statvfs *sb;
185 	struct netbsd32_statfs s32;
186 	int error;
187 
188 	/* fd_getvnode() will use the descriptor for us */
189 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
190 		return (error);
191 	mp = fp->f_vnode->v_mount;
192 	sb = STATVFSBUF_GET();
193 	if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
194 		goto out;
195 	compat_20_netbsd32_from_statvfs(sb, &s32);
196 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
197  out:
198 	STATVFSBUF_PUT(sb);
199 	fd_putfile(SCARG(uap, fd));
200 	return (error);
201 }
202 
203 int
204 compat_20_netbsd32_fhstatfs(struct lwp *l, const struct compat_20_netbsd32_fhstatfs_args *uap, register_t *retval)
205 {
206 	/* {
207 		syscallarg(const netbsd32_fhandlep_t) fhp;
208 		syscallarg(struct statvfs *) buf;
209 	} */
210 	struct compat_30_sys_fhstatvfs1_args ua;
211 
212 	NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
213 	NETBSD32TOP_UAP(buf, struct statvfs);
214 #ifdef notyet
215 	NETBSD32TOP_UAP(flags, int);
216 #endif
217 	return (compat_30_sys_fhstatvfs1(l, &ua, retval));
218 }
219