xref: /netbsd-src/sys/compat/common/vfs_syscalls_20.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: vfs_syscalls_20.c,v 1.10 2006/06/30 15:50:46 drochner Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_20.c,v 1.10 2006/06/30 15:50:46 drochner Exp $");
41 
42 #include "opt_compat_netbsd.h"
43 #include "opt_compat_43.h"
44 #include "fss.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/proc.h>
56 #include <sys/uio.h>
57 #include <sys/malloc.h>
58 #include <sys/dirent.h>
59 #include <sys/sysctl.h>
60 #include <sys/sa.h>
61 #include <sys/syscallargs.h>
62 #include <sys/kauth.h>
63 
64 #include <compat/sys/mount.h>
65 
66 #ifdef COMPAT_09
67 #define MOUNTNO_NONE	0
68 #define MOUNTNO_UFS	1		/* UNIX "Fast" Filesystem */
69 #define MOUNTNO_NFS	2		/* Network Filesystem */
70 #define MOUNTNO_MFS	3		/* Memory Filesystem */
71 #define MOUNTNO_MSDOS	4		/* MSDOS Filesystem */
72 #define MOUNTNO_CD9660	5		/* iso9660 cdrom */
73 #define MOUNTNO_FDESC	6		/* /dev/fd filesystem */
74 #define MOUNTNO_KERNFS	7		/* kernel variable filesystem */
75 #define MOUNTNO_DEVFS	8		/* device node filesystem */
76 #define MOUNTNO_AFS	9		/* AFS 3.x */
77 static const struct {
78 	const char *name;
79 	const int value;
80 } nv[] = {
81 	{ MOUNT_UFS, MOUNTNO_UFS },
82 	{ MOUNT_NFS, MOUNTNO_NFS },
83 	{ MOUNT_MFS, MOUNTNO_MFS },
84 	{ MOUNT_MSDOS, MOUNTNO_MSDOS },
85 	{ MOUNT_CD9660, MOUNTNO_CD9660 },
86 	{ MOUNT_FDESC, MOUNTNO_FDESC },
87 	{ MOUNT_KERNFS, MOUNTNO_KERNFS },
88 	{ MOUNT_AFS, MOUNTNO_AFS },
89 };
90 #endif
91 
92 static int
93 vfs2fs(struct statfs12 *bfs, const struct statvfs *fs)
94 {
95 	struct statfs12 ofs;
96 #ifdef COMPAT_09
97 	int i = 0;
98 	ofs.f_type = 0;
99 	ofs.f_oflags = (short)fs->f_flag;
100 
101 	for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++) {
102 		if (strcmp(nv[i].name, fs->f_fstypename) == 0) {
103 			ofs.f_type = nv[i].value;
104 			break;
105 		}
106 	}
107 #else
108 	ofs.f_type = 0;
109 #endif
110 #define CLAMP(a)	(long)(((a) & ~LONG_MAX) ? LONG_MAX : (a))
111 	ofs.f_bsize = CLAMP(fs->f_frsize);
112 	ofs.f_iosize = CLAMP(fs->f_iosize);
113 	ofs.f_blocks = CLAMP(fs->f_blocks);
114 	ofs.f_bfree = CLAMP(fs->f_bfree);
115 	if (fs->f_bfree > fs->f_bresvd)
116 		ofs.f_bavail = CLAMP(fs->f_bfree - fs->f_bresvd);
117 	else
118 		ofs.f_bavail = -CLAMP(fs->f_bresvd - fs->f_bfree);
119 	ofs.f_files = CLAMP(fs->f_files);
120 	ofs.f_ffree = CLAMP(fs->f_ffree);
121 	ofs.f_fsid = fs->f_fsidx;
122 	ofs.f_owner = fs->f_owner;
123 	ofs.f_flags = (long)fs->f_flag;
124 	ofs.f_syncwrites = CLAMP(fs->f_syncwrites);
125 	ofs.f_asyncwrites = CLAMP(fs->f_asyncwrites);
126 	(void)strncpy(ofs.f_fstypename, fs->f_fstypename,
127 	    sizeof(ofs.f_fstypename));
128 	(void)strncpy(ofs.f_mntonname, fs->f_mntonname,
129 	    sizeof(ofs.f_mntonname));
130 	(void)strncpy(ofs.f_mntfromname, fs->f_mntfromname,
131 	    sizeof(ofs.f_mntfromname));
132 
133 	return copyout(&ofs, bfs, sizeof(ofs));
134 }
135 
136 /*
137  * Get filesystem statistics.
138  */
139 /* ARGSUSED */
140 int
141 compat_20_sys_statfs(l, v, retval)
142 	struct lwp *l;
143 	void *v;
144 	register_t *retval;
145 {
146 	struct compat_20_sys_statfs_args /* {
147 		syscallarg(const char *) path;
148 		syscallarg(struct statfs12 *) buf;
149 	} */ *uap = v;
150 	struct mount *mp;
151 	struct statvfs *sbuf;
152 	int error = 0;
153 	struct nameidata nd;
154 
155 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
156 	if ((error = namei(&nd)) != 0)
157 		return error;
158 
159 	mp = nd.ni_vp->v_mount;
160 	vrele(nd.ni_vp);
161 
162 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
163 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
164 		goto done;
165 
166 	error = vfs2fs(SCARG(uap, buf), sbuf);
167 done:
168 	free(sbuf, M_TEMP);
169 	return error;
170 }
171 
172 /*
173  * Get filesystem statistics.
174  */
175 /* ARGSUSED */
176 int
177 compat_20_sys_fstatfs(l, v, retval)
178 	struct lwp *l;
179 	void *v;
180 	register_t *retval;
181 {
182 	struct compat_20_sys_fstatfs_args /* {
183 		syscallarg(int) fd;
184 		syscallarg(struct statfs12 *) buf;
185 	} */ *uap = v;
186 	struct proc *p = l->l_proc;
187 	struct file *fp;
188 	struct mount *mp;
189 	struct statvfs *sbuf;
190 	int error;
191 
192 	/* getvnode() will use the descriptor for us */
193 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
194 		return (error);
195 	mp = ((struct vnode *)fp->f_data)->v_mount;
196 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
197 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
198 		goto out;
199 	error = vfs2fs(SCARG(uap, buf), sbuf);
200  out:
201 	FILE_UNUSE(fp, l);
202 	free(sbuf, M_TEMP);
203 	return error;
204 }
205 
206 
207 /*
208  * Get statistics on all filesystems.
209  */
210 int
211 compat_20_sys_getfsstat(l, v, retval)
212 	struct lwp *l;
213 	void *v;
214 	register_t *retval;
215 {
216 	struct compat_20_sys_getfsstat_args /* {
217 		syscallarg(struct statfs12 *) buf;
218 		syscallarg(long) bufsize;
219 		syscallarg(int) flags;
220 	} */ *uap = v;
221 	int root = 0;
222 	struct mount *mp, *nmp;
223 	struct statvfs *sbuf;
224 	struct statfs12 *sfsp;
225 	size_t count, maxcount;
226 	int error = 0;
227 
228 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
229 	maxcount = (size_t)SCARG(uap, bufsize) / sizeof(struct statfs12);
230 	sfsp = SCARG(uap, buf);
231 	simple_lock(&mountlist_slock);
232 	count = 0;
233 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
234 	     mp = nmp) {
235 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
236 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
237 			continue;
238 		}
239 		if (sfsp && count < maxcount) {
240 			error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 0);
241 			if (error) {
242 				simple_lock(&mountlist_slock);
243 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
244 				vfs_unbusy(mp);
245 				continue;
246 			}
247 			error = vfs2fs(sfsp, sbuf);
248 			if (error) {
249 				vfs_unbusy(mp);
250 				goto out;
251 			}
252 			sfsp++;
253 			root |= strcmp(sbuf->f_mntonname, "/") == 0;
254 		}
255 		count++;
256 		simple_lock(&mountlist_slock);
257 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
258 		vfs_unbusy(mp);
259 	}
260 	simple_unlock(&mountlist_slock);
261 	if (root == 0 && l->l_proc->p_cwdi->cwdi_rdir) {
262 		/*
263 		 * fake a root entry
264 		 */
265 		if ((error = dostatvfs(l->l_proc->p_cwdi->cwdi_rdir->v_mount,
266 				       sbuf, l, SCARG(uap, flags), 1)) != 0)
267 			goto out;
268 		if (sfsp)
269 			error = vfs2fs(sfsp, sbuf);
270 		count++;
271 	}
272 	if (sfsp && count > maxcount)
273 		*retval = maxcount;
274 	else
275 		*retval = count;
276 out:
277 	free(sbuf, M_TEMP);
278 	return error;
279 }
280 
281 int
282 compat_20_sys_fhstatfs(l, v, retval)
283 	struct lwp *l;
284 	void *v;
285 	register_t *retval;
286 {
287 	struct compat_20_sys_fhstatfs_args /*
288 		syscallarg(const fhandle_t *) fhp;
289 		syscallarg(struct statfs12 *) buf;
290 	} */ *uap = v;
291 	struct proc *p = l->l_proc;
292 	struct statvfs *sbuf;
293 	fhandle_t fh;
294 	struct mount *mp;
295 	struct vnode *vp;
296 	int error;
297 
298 	/*
299 	 * Must be super user
300 	 */
301 	if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
302 					     &p->p_acflag)))
303 		return (error);
304 
305 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
306 		return (error);
307 
308 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
309 		return (ESTALE);
310 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
311 		return (error);
312 	mp = vp->v_mount;
313 	vput(vp);
314 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
315 	if ((error = VFS_STATVFS(mp, sbuf, l)) != 0)
316 		goto out;
317 	error = vfs2fs(SCARG(uap, buf), sbuf);
318 out:
319 	free(sbuf, M_TEMP);
320 	return error;
321 }
322