1 /*	$NetBSD: netbsd32_compat_20.c,v 1.34 2014/09/05 09:21:54 matt 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.34 2014/09/05 09:21:54 matt 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/file.h>
40 #include <sys/filedesc.h>
41 #include <sys/namei.h>
42 #include <sys/syscallargs.h>
43 #include <sys/proc.h>
44 #include <sys/dirent.h>
45 
46 #include <compat/netbsd32/netbsd32.h>
47 #include <compat/netbsd32/netbsd32_syscallargs.h>
48 #include <compat/netbsd32/netbsd32_conv.h>
49 
50 static inline void compat_20_netbsd32_from_statvfs(struct statvfs *,
51     struct netbsd32_statfs *);
52 
53 static inline void
compat_20_netbsd32_from_statvfs(struct statvfs * sbp,struct netbsd32_statfs * sb32p)54 compat_20_netbsd32_from_statvfs(struct statvfs *sbp, struct netbsd32_statfs *sb32p)
55 {
56 	sb32p->f_flags = sbp->f_flag;
57 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
58 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
59 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
60 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
61 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
62 	sb32p->f_files = (netbsd32_long)sbp->f_files;
63 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
64 	sb32p->f_fsid = sbp->f_fsidx;
65 	sb32p->f_owner = sbp->f_owner;
66 	sb32p->f_spare[0] = 0;
67 	sb32p->f_spare[1] = 0;
68 	sb32p->f_spare[2] = 0;
69 	sb32p->f_spare[3] = 0;
70 	(void)memcpy(sb32p->f_fstypename, sbp->f_fstypename,
71 	    sizeof(sb32p->f_fstypename));
72 	(void)memcpy(sb32p->f_mntonname, sbp->f_mntonname,
73 	    sizeof(sb32p->f_mntonname));
74 	(void)memcpy(sb32p->f_mntfromname, sbp->f_mntfromname,
75 	    sizeof(sb32p->f_mntfromname));
76 }
77 
78 int
compat_20_netbsd32_getfsstat(struct lwp * l,const struct compat_20_netbsd32_getfsstat_args * uap,register_t * retval)79 compat_20_netbsd32_getfsstat(struct lwp *l, const struct compat_20_netbsd32_getfsstat_args *uap, register_t *retval)
80 {
81 	/* {
82 		syscallarg(netbsd32_statfsp_t) buf;
83 		syscallarg(netbsd32_long) bufsize;
84 		syscallarg(int) flags;
85 	} */
86 	int root = 0;
87 	struct proc *p = l->l_proc;
88 	struct mount *mp, *nmp;
89 	struct statvfs *sb;
90 	struct netbsd32_statfs sb32;
91 	void *sfsp;
92 	size_t count, maxcount;
93 	int error = 0;
94 
95 	sb = STATVFSBUF_GET();
96 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
97 	sfsp = SCARG_P32(uap, buf);
98 	mutex_enter(&mountlist_lock);
99 	count = 0;
100 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
101 		if (vfs_busy(mp, &nmp)) {
102 			continue;
103 		}
104 		if (sfsp && count < maxcount) {
105 			error = dostatvfs(mp, sb, l, SCARG(uap, flags), 0);
106 			if (error) {
107 				vfs_unbusy(mp, false, &nmp);
108 				error = 0;
109 				continue;
110 			}
111 			compat_20_netbsd32_from_statvfs(sb, &sb32);
112 			error = copyout(&sb32, sfsp, sizeof(sb32));
113 			if (error) {
114 				vfs_unbusy(mp, false, NULL);
115 				goto out;
116 			}
117 			sfsp = (char *)sfsp + sizeof(sb32);
118 			root |= strcmp(sb->f_mntonname, "/") == 0;
119 		}
120 		count++;
121 		vfs_unbusy(mp, false, &nmp);
122 	}
123 	mutex_exit(&mountlist_lock);
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 	STATVFSBUF_PUT(sb);
148 	return error;
149 }
150 
151 int
compat_20_netbsd32_statfs(struct lwp * l,const struct compat_20_netbsd32_statfs_args * uap,register_t * retval)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 *sb;
160 	struct netbsd32_statfs s32;
161 	int error;
162 	struct vnode *vp;
163 
164 	error = namei_simple_user(SCARG_P32(uap, path),
165 				NSM_FOLLOW_TRYEMULROOT, &vp);
166 	if (error != 0)
167 		return (error);
168 	mp = vp->v_mount;
169 	sb = &mp->mnt_stat;
170 	vrele(vp);
171 	if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
172 		return (error);
173 	compat_20_netbsd32_from_statvfs(sb, &s32);
174 	return copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
175 }
176 
177 int
compat_20_netbsd32_fstatfs(struct lwp * l,const struct compat_20_netbsd32_fstatfs_args * uap,register_t * retval)178 compat_20_netbsd32_fstatfs(struct lwp *l, const struct compat_20_netbsd32_fstatfs_args *uap, register_t *retval)
179 {
180 	/* {
181 		syscallarg(int) fd;
182 		syscallarg(netbsd32_statfsp_t) buf;
183 	} */
184 	file_t *fp;
185 	struct mount *mp;
186 	struct statvfs *sb;
187 	struct netbsd32_statfs s32;
188 	int error;
189 
190 	/* fd_getvnode() will use the descriptor for us */
191 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
192 		return (error);
193 	mp = fp->f_vnode->v_mount;
194 	sb = &mp->mnt_stat;
195 	if ((error = dostatvfs(mp, sb, l, 0, 0)) != 0)
196 		goto out;
197 	compat_20_netbsd32_from_statvfs(sb, &s32);
198 	error = copyout(&s32, SCARG_P32(uap, buf), sizeof(s32));
199  out:
200 	fd_putfile(SCARG(uap, fd));
201 	return (error);
202 }
203 
204 int
compat_20_netbsd32_fhstatfs(struct lwp * l,const struct compat_20_netbsd32_fhstatfs_args * uap,register_t * retval)205 compat_20_netbsd32_fhstatfs(struct lwp *l, const struct compat_20_netbsd32_fhstatfs_args *uap, register_t *retval)
206 {
207 	/* {
208 		syscallarg(const netbsd32_fhandlep_t) fhp;
209 		syscallarg(struct statvfs *) buf;
210 	} */
211 	struct compat_30_sys_fhstatvfs1_args ua;
212 
213 	NETBSD32TOP_UAP(fhp, const struct compat_30_fhandle);
214 	NETBSD32TOP_UAP(buf, struct statvfs);
215 #ifdef notyet
216 	NETBSD32TOP_UAP(flags, int);
217 #endif
218 	return (compat_30_sys_fhstatvfs1(l, &ua, retval));
219 }
220