xref: /netbsd-src/sys/compat/common/vfs_syscalls_20.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: vfs_syscalls_20.c,v 1.13 2006/10/12 01:30:47 christos 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.13 2006/10/12 01:30:47 christos 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(struct lwp *l, void *v, register_t *retval __unused)
142 {
143 	struct compat_20_sys_statfs_args /* {
144 		syscallarg(const char *) path;
145 		syscallarg(struct statfs12 *) buf;
146 	} */ *uap = v;
147 	struct mount *mp;
148 	struct statvfs *sbuf;
149 	int error = 0;
150 	struct nameidata nd;
151 
152 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
153 	if ((error = namei(&nd)) != 0)
154 		return error;
155 
156 	mp = nd.ni_vp->v_mount;
157 	vrele(nd.ni_vp);
158 
159 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
160 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
161 		goto done;
162 
163 	error = vfs2fs(SCARG(uap, buf), sbuf);
164 done:
165 	free(sbuf, M_TEMP);
166 	return error;
167 }
168 
169 /*
170  * Get filesystem statistics.
171  */
172 /* ARGSUSED */
173 int
174 compat_20_sys_fstatfs(struct lwp *l, void *v, register_t *retval __unused)
175 {
176 	struct compat_20_sys_fstatfs_args /* {
177 		syscallarg(int) fd;
178 		syscallarg(struct statfs12 *) buf;
179 	} */ *uap = v;
180 	struct proc *p = l->l_proc;
181 	struct file *fp;
182 	struct mount *mp;
183 	struct statvfs *sbuf;
184 	int error;
185 
186 	/* getvnode() will use the descriptor for us */
187 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
188 		return (error);
189 	mp = ((struct vnode *)fp->f_data)->v_mount;
190 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
191 	if ((error = dostatvfs(mp, sbuf, l, 0, 1)) != 0)
192 		goto out;
193 	error = vfs2fs(SCARG(uap, buf), sbuf);
194  out:
195 	FILE_UNUSE(fp, l);
196 	free(sbuf, M_TEMP);
197 	return error;
198 }
199 
200 
201 /*
202  * Get statistics on all filesystems.
203  */
204 int
205 compat_20_sys_getfsstat(l, v, retval)
206 	struct lwp *l;
207 	void *v;
208 	register_t *retval;
209 {
210 	struct compat_20_sys_getfsstat_args /* {
211 		syscallarg(struct statfs12 *) buf;
212 		syscallarg(long) bufsize;
213 		syscallarg(int) flags;
214 	} */ *uap = v;
215 	int root = 0;
216 	struct mount *mp, *nmp;
217 	struct statvfs *sbuf;
218 	struct statfs12 *sfsp;
219 	size_t count, maxcount;
220 	int error = 0;
221 
222 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
223 	maxcount = (size_t)SCARG(uap, bufsize) / sizeof(struct statfs12);
224 	sfsp = SCARG(uap, buf);
225 	simple_lock(&mountlist_slock);
226 	count = 0;
227 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
228 	     mp = nmp) {
229 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
230 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
231 			continue;
232 		}
233 		if (sfsp && count < maxcount) {
234 			error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 0);
235 			if (error) {
236 				simple_lock(&mountlist_slock);
237 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
238 				vfs_unbusy(mp);
239 				continue;
240 			}
241 			error = vfs2fs(sfsp, sbuf);
242 			if (error) {
243 				vfs_unbusy(mp);
244 				goto out;
245 			}
246 			sfsp++;
247 			root |= strcmp(sbuf->f_mntonname, "/") == 0;
248 		}
249 		count++;
250 		simple_lock(&mountlist_slock);
251 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
252 		vfs_unbusy(mp);
253 	}
254 	simple_unlock(&mountlist_slock);
255 	if (root == 0 && l->l_proc->p_cwdi->cwdi_rdir) {
256 		/*
257 		 * fake a root entry
258 		 */
259 		if ((error = dostatvfs(l->l_proc->p_cwdi->cwdi_rdir->v_mount,
260 				       sbuf, l, SCARG(uap, flags), 1)) != 0)
261 			goto out;
262 		if (sfsp)
263 			error = vfs2fs(sfsp, sbuf);
264 		count++;
265 	}
266 	if (sfsp && count > maxcount)
267 		*retval = maxcount;
268 	else
269 		*retval = count;
270 out:
271 	free(sbuf, M_TEMP);
272 	return error;
273 }
274 
275 int
276 compat_20_sys_fhstatfs(struct lwp *l, void *v, register_t *retval __unused)
277 {
278 	struct compat_20_sys_fhstatfs_args /*
279 		syscallarg(const struct compat_30_fhandle *) fhp;
280 		syscallarg(struct statfs12 *) buf;
281 	} */ *uap = v;
282 	struct statvfs *sbuf;
283 	struct compat_30_fhandle fh;
284 	struct mount *mp;
285 	struct vnode *vp;
286 	int error;
287 
288 	/*
289 	 * Must be super user
290 	 */
291 	if ((error = kauth_authorize_generic(l->l_cred,
292 	    KAUTH_GENERIC_ISSUSER, &l->l_acflag)))
293 		return (error);
294 
295 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
296 		return (error);
297 
298 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
299 		return (ESTALE);
300 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
301 		return (error);
302 	mp = vp->v_mount;
303 	vput(vp);
304 	sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK);
305 	if ((error = VFS_STATVFS(mp, sbuf, l)) != 0)
306 		goto out;
307 	error = vfs2fs(SCARG(uap, buf), sbuf);
308 out:
309 	free(sbuf, M_TEMP);
310 	return error;
311 }
312