xref: /freebsd/sys/compat/linux/linux_stats.c (revision 4af27623361335eef9a191f7d3a76047095d0f52)
1c21dee17SSøren Schmidt /*-
2c21dee17SSøren Schmidt  * Copyright (c) 1994-1995 S�ren Schmidt
3c21dee17SSøren Schmidt  * All rights reserved.
4c21dee17SSøren Schmidt  *
5c21dee17SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
6c21dee17SSøren Schmidt  * modification, are permitted provided that the following conditions
7c21dee17SSøren Schmidt  * are met:
8c21dee17SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
9c21dee17SSøren Schmidt  *    notice, this list of conditions and the following disclaimer
10c21dee17SSøren Schmidt  *    in this position and unchanged.
11c21dee17SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
12c21dee17SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
13c21dee17SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
14c21dee17SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
1521dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
16c21dee17SSøren Schmidt  *
17c21dee17SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18c21dee17SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19c21dee17SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20c21dee17SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21c21dee17SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22c21dee17SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23c21dee17SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24c21dee17SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25c21dee17SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26c21dee17SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27c21dee17SSøren Schmidt  */
28c21dee17SSøren Schmidt 
2916dbc7f2SDavid E. O'Brien #include <sys/cdefs.h>
3016dbc7f2SDavid E. O'Brien __FBSDID("$FreeBSD$");
3116dbc7f2SDavid E. O'Brien 
32eddc160eSRobert Watson #include "opt_mac.h"
33eddc160eSRobert Watson 
34c21dee17SSøren Schmidt #include <sys/param.h>
35408da119SMarcel Moolenaar #include <sys/conf.h>
36c21dee17SSøren Schmidt #include <sys/dirent.h>
37c21dee17SSøren Schmidt #include <sys/file.h>
38c21dee17SSøren Schmidt #include <sys/filedesc.h>
39c21dee17SSøren Schmidt #include <sys/proc.h>
40eddc160eSRobert Watson #include <sys/mac.h>
4185422e62SBruce Evans #include <sys/malloc.h>
42c21dee17SSøren Schmidt #include <sys/mount.h>
43c21dee17SSøren Schmidt #include <sys/namei.h>
44c21dee17SSøren Schmidt #include <sys/stat.h>
45408da119SMarcel Moolenaar #include <sys/systm.h>
46c21dee17SSøren Schmidt #include <sys/vnode.h>
47c21dee17SSøren Schmidt 
484af27623STim J. Robbins #include "opt_compat.h"
494af27623STim J. Robbins 
504af27623STim J. Robbins #if !COMPAT_LINUX32
51ac951e62SMarcel Moolenaar #include <machine/../linux/linux.h>
52ebea8660SMarcel Moolenaar #include <machine/../linux/linux_proto.h>
534af27623STim J. Robbins #else
544af27623STim J. Robbins #include <machine/../linux32/linux.h>
554af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
564af27623STim J. Robbins #endif
5785422e62SBruce Evans 
58ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
59762e6b85SEivind Eklund 
60c21dee17SSøren Schmidt static int
61c21dee17SSøren Schmidt newstat_copyout(struct stat *buf, void *ubuf)
62c21dee17SSøren Schmidt {
635002a60fSMarcel Moolenaar 	struct l_newstat tbuf;
644c3a3ec0SJosef Karthauser 	struct cdevsw *cdevsw;
6589c9c53dSPoul-Henning Kamp 	struct cdev *dev;
66c21dee17SSøren Schmidt 
67a966b13dSMartin Blapp 	bzero(&tbuf, sizeof(tbuf));
685002a60fSMarcel Moolenaar 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
695002a60fSMarcel Moolenaar 	tbuf.st_ino = buf->st_ino;
705002a60fSMarcel Moolenaar 	tbuf.st_mode = buf->st_mode;
715002a60fSMarcel Moolenaar 	tbuf.st_nlink = buf->st_nlink;
725002a60fSMarcel Moolenaar 	tbuf.st_uid = buf->st_uid;
735002a60fSMarcel Moolenaar 	tbuf.st_gid = buf->st_gid;
745002a60fSMarcel Moolenaar 	tbuf.st_rdev = buf->st_rdev;
755002a60fSMarcel Moolenaar 	tbuf.st_size = buf->st_size;
765002a60fSMarcel Moolenaar 	tbuf.st_atime = buf->st_atime;
775002a60fSMarcel Moolenaar 	tbuf.st_mtime = buf->st_mtime;
785002a60fSMarcel Moolenaar 	tbuf.st_ctime = buf->st_ctime;
795002a60fSMarcel Moolenaar 	tbuf.st_blksize = buf->st_blksize;
805002a60fSMarcel Moolenaar 	tbuf.st_blocks = buf->st_blocks;
817842f151SPaul Richards 
824c3a3ec0SJosef Karthauser 	/* Lie about disk drives which are character devices
834c3a3ec0SJosef Karthauser 	 * in FreeBSD but block devices under Linux.
844c3a3ec0SJosef Karthauser 	 */
855002a60fSMarcel Moolenaar 	if (S_ISCHR(tbuf.st_mode) &&
86f3732fd1SPoul-Henning Kamp 	    (dev = findcdev(buf->st_rdev)) != NULL) {
874c3a3ec0SJosef Karthauser 		cdevsw = devsw(dev);
884c3a3ec0SJosef Karthauser 		if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
895002a60fSMarcel Moolenaar 			tbuf.st_mode &= ~S_IFMT;
905002a60fSMarcel Moolenaar 			tbuf.st_mode |= S_IFBLK;
917842f151SPaul Richards 
927842f151SPaul Richards 			/* XXX this may not be quite right */
937842f151SPaul Richards 			/* Map major number to 0 */
945002a60fSMarcel Moolenaar 			tbuf.st_dev = uminor(buf->st_dev) & 0xf;
955002a60fSMarcel Moolenaar 			tbuf.st_rdev = buf->st_rdev & 0xff;
967842f151SPaul Richards 		}
974c3a3ec0SJosef Karthauser 	}
984e0eaf69SMarcel Moolenaar 
994e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
100c21dee17SSøren Schmidt }
101c21dee17SSøren Schmidt 
102c21dee17SSøren Schmidt int
103b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
104c21dee17SSøren Schmidt {
105c21dee17SSøren Schmidt 	struct stat buf;
106c21dee17SSøren Schmidt 	struct nameidata nd;
107206a5d3aSIan Dowse 	char *path;
108c21dee17SSøren Schmidt 	int error;
109d66a5066SPeter Wemm 
110206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
111c21dee17SSøren Schmidt 
112c21dee17SSøren Schmidt #ifdef DEBUG
11324593369SJonathan Lemon 	if (ldebug(newstat))
114206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
115c21dee17SSøren Schmidt #endif
1164e0eaf69SMarcel Moolenaar 
117206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path, td);
118c21dee17SSøren Schmidt 	error = namei(&nd);
119206a5d3aSIan Dowse 	LFREEPATH(path);
1204e0eaf69SMarcel Moolenaar 	if (error)
1214e0eaf69SMarcel Moolenaar 		return (error);
122762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1234e0eaf69SMarcel Moolenaar 
124ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
125c21dee17SSøren Schmidt 	vput(nd.ni_vp);
1264e0eaf69SMarcel Moolenaar 	if (error)
1274e0eaf69SMarcel Moolenaar 		return (error);
1284e0eaf69SMarcel Moolenaar 
1294e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
130c21dee17SSøren Schmidt }
131c21dee17SSøren Schmidt 
132c21dee17SSøren Schmidt int
133b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
134c21dee17SSøren Schmidt {
135c21dee17SSøren Schmidt 	int error;
1364e0eaf69SMarcel Moolenaar 	struct stat sb;
137d66a5066SPeter Wemm 	struct nameidata nd;
138206a5d3aSIan Dowse 	char *path;
139d66a5066SPeter Wemm 
140206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
141c21dee17SSøren Schmidt 
142c21dee17SSøren Schmidt #ifdef DEBUG
14324593369SJonathan Lemon 	if (ldebug(newlstat))
144206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
145c21dee17SSøren Schmidt #endif
1464e0eaf69SMarcel Moolenaar 
147206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path,
148206a5d3aSIan Dowse 	    td);
149c21dee17SSøren Schmidt 	error = namei(&nd);
150206a5d3aSIan Dowse 	LFREEPATH(path);
151d66a5066SPeter Wemm 	if (error)
152d66a5066SPeter Wemm 		return (error);
153762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1544e0eaf69SMarcel Moolenaar 
155ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
1565002a60fSMarcel Moolenaar 	vput(nd.ni_vp);
157d66a5066SPeter Wemm 	if (error)
158d66a5066SPeter Wemm 		return (error);
1594e0eaf69SMarcel Moolenaar 
1605002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
161d66a5066SPeter Wemm }
162c21dee17SSøren Schmidt 
163c21dee17SSøren Schmidt int
164b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
165c21dee17SSøren Schmidt {
166c21dee17SSøren Schmidt 	struct file *fp;
167c21dee17SSøren Schmidt 	struct stat buf;
168c21dee17SSøren Schmidt 	int error;
169c21dee17SSøren Schmidt 
170c21dee17SSøren Schmidt #ifdef DEBUG
17124593369SJonathan Lemon 	if (ldebug(newfstat))
17224593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
173c21dee17SSøren Schmidt #endif
1744e0eaf69SMarcel Moolenaar 
175a4db4953SAlfred Perlstein 	if ((error = fget(td, args->fd, &fp)) != 0)
176a4db4953SAlfred Perlstein 		return (error);
1774e0eaf69SMarcel Moolenaar 
178ea6027a8SRobert Watson 	error = fo_stat(fp, &buf, td->td_ucred, td);
179426da3bcSAlfred Perlstein 	fdrop(fp, td);
180c21dee17SSøren Schmidt 	if (!error)
181c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
1824e0eaf69SMarcel Moolenaar 
1834e0eaf69SMarcel Moolenaar 	return (error);
184c21dee17SSøren Schmidt }
185c21dee17SSøren Schmidt 
1865002a60fSMarcel Moolenaar /* XXX - All fields of type l_int are defined as l_long on i386 */
1875002a60fSMarcel Moolenaar struct l_statfs {
1885002a60fSMarcel Moolenaar 	l_int		f_type;
1895002a60fSMarcel Moolenaar 	l_int		f_bsize;
1905002a60fSMarcel Moolenaar 	l_int		f_blocks;
1915002a60fSMarcel Moolenaar 	l_int		f_bfree;
1925002a60fSMarcel Moolenaar 	l_int		f_bavail;
1935002a60fSMarcel Moolenaar 	l_int		f_files;
1945002a60fSMarcel Moolenaar 	l_int		f_ffree;
1955002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
1965002a60fSMarcel Moolenaar 	l_int		f_namelen;
1975002a60fSMarcel Moolenaar 	l_int		f_spare[6];
198c21dee17SSøren Schmidt };
199c21dee17SSøren Schmidt 
200dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
201dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
202dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
203dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
204dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
205dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
206dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
207dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
208dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
209dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
210dca60efcSMarcel Moolenaar 
211dca60efcSMarcel Moolenaar static long
212962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
213dca60efcSMarcel Moolenaar {
214962cf420SMaxim Sobolev 	int i;
215962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
216962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
217962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
218962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
219962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
220962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
221962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
222962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
223962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
224962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
225962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
226962cf420SMaxim Sobolev 		{NULL,      0L}};
227dca60efcSMarcel Moolenaar 
228962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
229962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
230962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
231dca60efcSMarcel Moolenaar 
232dca60efcSMarcel Moolenaar 	return (0L);
233dca60efcSMarcel Moolenaar }
234dca60efcSMarcel Moolenaar 
235c21dee17SSøren Schmidt int
236b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
237c21dee17SSøren Schmidt {
238c21dee17SSøren Schmidt 	struct mount *mp;
239c21dee17SSøren Schmidt 	struct nameidata *ndp;
240c21dee17SSøren Schmidt 	struct statfs *bsd_statfs;
241c21dee17SSøren Schmidt 	struct nameidata nd;
2425002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
243206a5d3aSIan Dowse 	char *path;
244c21dee17SSøren Schmidt 	int error;
245d66a5066SPeter Wemm 
246206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
247c21dee17SSøren Schmidt 
248c21dee17SSøren Schmidt #ifdef DEBUG
24924593369SJonathan Lemon 	if (ldebug(statfs))
250206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
251c21dee17SSøren Schmidt #endif
252c21dee17SSøren Schmidt 	ndp = &nd;
253206a5d3aSIan Dowse 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
254d5558c00SPeter Wemm 	error = namei(ndp);
255206a5d3aSIan Dowse 	LFREEPATH(path);
256d5558c00SPeter Wemm 	if (error)
257c21dee17SSøren Schmidt 		return error;
258762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
259c21dee17SSøren Schmidt 	mp = ndp->ni_vp->v_mount;
260c21dee17SSøren Schmidt 	bsd_statfs = &mp->mnt_stat;
261c21dee17SSøren Schmidt 	vrele(ndp->ni_vp);
262eddc160eSRobert Watson #ifdef MAC
26331566c96SJohn Baldwin 	error = mac_check_mount_stat(td->td_ucred, mp);
264eddc160eSRobert Watson 	if (error)
265eddc160eSRobert Watson 		return (error);
266eddc160eSRobert Watson #endif
267b40ce416SJulian Elischer 	error = VFS_STATFS(mp, bsd_statfs, td);
268d5558c00SPeter Wemm 	if (error)
269c21dee17SSøren Schmidt 		return error;
270c21dee17SSøren Schmidt 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
271962cf420SMaxim Sobolev 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
2725002a60fSMarcel Moolenaar 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
2735002a60fSMarcel Moolenaar 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
2745002a60fSMarcel Moolenaar 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
2755002a60fSMarcel Moolenaar 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
2765002a60fSMarcel Moolenaar 	linux_statfs.f_ffree = bsd_statfs->f_ffree;
2775002a60fSMarcel Moolenaar 	linux_statfs.f_files = bsd_statfs->f_files;
2780b399cc8SEric Anholt 	if (suser(td)) {
2790b399cc8SEric Anholt 		linux_statfs.f_fsid.val[0] = 0;
2800b399cc8SEric Anholt 		linux_statfs.f_fsid.val[1] = 0;
2810b399cc8SEric Anholt 	} else {
2825002a60fSMarcel Moolenaar 		linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
2835002a60fSMarcel Moolenaar 		linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
2840b399cc8SEric Anholt 	}
2855002a60fSMarcel Moolenaar 	linux_statfs.f_namelen = MAXNAMLEN;
2864b7ef73dSDag-Erling Smørgrav 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
287c21dee17SSøren Schmidt }
288c21dee17SSøren Schmidt 
289c21dee17SSøren Schmidt int
290b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
291c21dee17SSøren Schmidt {
292c21dee17SSøren Schmidt 	struct file *fp;
293c21dee17SSøren Schmidt 	struct mount *mp;
294c21dee17SSøren Schmidt 	struct statfs *bsd_statfs;
2955002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
296c21dee17SSøren Schmidt 	int error;
297c21dee17SSøren Schmidt 
298c21dee17SSøren Schmidt #ifdef DEBUG
29924593369SJonathan Lemon 	if (ldebug(fstatfs))
30024593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
301c21dee17SSøren Schmidt #endif
302b40ce416SJulian Elischer 	error = getvnode(td->td_proc->p_fd, args->fd, &fp);
303d5558c00SPeter Wemm 	if (error)
304c21dee17SSøren Schmidt 		return error;
3053b6d9652SPoul-Henning Kamp 	mp = fp->f_vnode->v_mount;
306eddc160eSRobert Watson #ifdef MAC
30731566c96SJohn Baldwin 	error = mac_check_mount_stat(td->td_ucred, mp);
308eddc160eSRobert Watson 	if (error) {
309eddc160eSRobert Watson 		fdrop(fp, td);
310eddc160eSRobert Watson 		return (error);
311eddc160eSRobert Watson 	}
312eddc160eSRobert Watson #endif
313c21dee17SSøren Schmidt 	bsd_statfs = &mp->mnt_stat;
314b40ce416SJulian Elischer 	error = VFS_STATFS(mp, bsd_statfs, td);
315426da3bcSAlfred Perlstein 	if (error) {
316426da3bcSAlfred Perlstein 		fdrop(fp, td);
317c21dee17SSøren Schmidt 		return error;
318426da3bcSAlfred Perlstein 	}
319c21dee17SSøren Schmidt 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
320962cf420SMaxim Sobolev 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
3215002a60fSMarcel Moolenaar 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
3225002a60fSMarcel Moolenaar 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
3235002a60fSMarcel Moolenaar 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
3245002a60fSMarcel Moolenaar 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
3255002a60fSMarcel Moolenaar 	linux_statfs.f_ffree = bsd_statfs->f_ffree;
3265002a60fSMarcel Moolenaar 	linux_statfs.f_files = bsd_statfs->f_files;
3270b399cc8SEric Anholt 	if (suser(td)) {
3280b399cc8SEric Anholt 		linux_statfs.f_fsid.val[0] = 0;
3290b399cc8SEric Anholt 		linux_statfs.f_fsid.val[1] = 0;
3300b399cc8SEric Anholt 	} else {
3315002a60fSMarcel Moolenaar 		linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
3325002a60fSMarcel Moolenaar 		linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
3330b399cc8SEric Anholt 	}
3345002a60fSMarcel Moolenaar 	linux_statfs.f_namelen = MAXNAMLEN;
3354b7ef73dSDag-Erling Smørgrav 	error = copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
336426da3bcSAlfred Perlstein 	fdrop(fp, td);
337426da3bcSAlfred Perlstein 	return error;
338c21dee17SSøren Schmidt }
339408da119SMarcel Moolenaar 
3405002a60fSMarcel Moolenaar struct l_ustat
341408da119SMarcel Moolenaar {
3425002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
3435002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
3445002a60fSMarcel Moolenaar 	char		f_fname[6];
3455002a60fSMarcel Moolenaar 	char		f_fpack[6];
3465002a60fSMarcel Moolenaar };
3475002a60fSMarcel Moolenaar 
3485002a60fSMarcel Moolenaar int
349b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
3505002a60fSMarcel Moolenaar {
3515002a60fSMarcel Moolenaar 	struct l_ustat lu;
35289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
353408da119SMarcel Moolenaar 	struct vnode *vp;
354408da119SMarcel Moolenaar 	struct statfs *stat;
355408da119SMarcel Moolenaar 	int error;
356408da119SMarcel Moolenaar 
357408da119SMarcel Moolenaar #ifdef DEBUG
35824593369SJonathan Lemon 	if (ldebug(ustat))
3595002a60fSMarcel Moolenaar 		printf(ARGS(ustat, "%d, *"), args->dev);
360408da119SMarcel Moolenaar #endif
361408da119SMarcel Moolenaar 
362408da119SMarcel Moolenaar 	/*
363408da119SMarcel Moolenaar 	 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
364408da119SMarcel Moolenaar 	 * lu.f_tinode and lu.f_tfree are set from the device's super block.
365408da119SMarcel Moolenaar 	 */
366408da119SMarcel Moolenaar 	bzero(&lu, sizeof(lu));
367408da119SMarcel Moolenaar 
368408da119SMarcel Moolenaar 	/*
369408da119SMarcel Moolenaar 	 * XXX - Don't return an error if we can't find a vnode for the
37089c9c53dSPoul-Henning Kamp 	 * device. Our struct cdev *is 32-bits whereas Linux only has a 16-bits
37189c9c53dSPoul-Henning Kamp 	 * struct cdev *. The struct cdev *that is used now may as well be a truncated
37289c9c53dSPoul-Henning Kamp 	 * struct cdev *returned from previous syscalls. Just return a bzeroed
373408da119SMarcel Moolenaar 	 * ustat in that case.
37441befa53SPoul-Henning Kamp 	 *
37541befa53SPoul-Henning Kamp 	 * XXX: findcdev() SHALL not be used this way.  Somebody (TM) will
37641befa53SPoul-Henning Kamp 	 *	have to find a better way.  It may be that we should stick
37741befa53SPoul-Henning Kamp 	 *	a dev_t into struct mount, and walk the mountlist for a
37841befa53SPoul-Henning Kamp 	 *	perfect match and failing that try again looking for a
37941befa53SPoul-Henning Kamp 	 *	minor-truncated match.
380408da119SMarcel Moolenaar 	 */
381f3732fd1SPoul-Henning Kamp 	dev = findcdev(makedev(args->dev >> 8, args->dev & 0xFF));
382f3732fd1SPoul-Henning Kamp 	if (dev != NULL && vfinddev(dev, &vp)) {
383408da119SMarcel Moolenaar 		if (vp->v_mount == NULL)
384408da119SMarcel Moolenaar 			return (EINVAL);
385eddc160eSRobert Watson #ifdef MAC
38631566c96SJohn Baldwin 		error = mac_check_mount_stat(td->td_ucred, vp->v_mount);
387eddc160eSRobert Watson 		if (error)
388eddc160eSRobert Watson 			return (error);
389eddc160eSRobert Watson #endif
390408da119SMarcel Moolenaar 		stat = &(vp->v_mount->mnt_stat);
391b40ce416SJulian Elischer 		error = VFS_STATFS(vp->v_mount, stat, td);
392408da119SMarcel Moolenaar 		if (error)
393408da119SMarcel Moolenaar 			return (error);
394408da119SMarcel Moolenaar 
395408da119SMarcel Moolenaar 		lu.f_tfree = stat->f_bfree;
396408da119SMarcel Moolenaar 		lu.f_tinode = stat->f_ffree;
397408da119SMarcel Moolenaar 	}
398408da119SMarcel Moolenaar 
3995002a60fSMarcel Moolenaar 	return (copyout(&lu, args->ubuf, sizeof(lu)));
400408da119SMarcel Moolenaar }
4015002a60fSMarcel Moolenaar 
4024af27623STim J. Robbins #if defined(__i386__) || (defined(__amd64__) && COMPAT_LINUX32)
4035002a60fSMarcel Moolenaar 
4045002a60fSMarcel Moolenaar static int
4055002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
4065002a60fSMarcel Moolenaar {
4075002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
408616aa29aSMartin Blapp 	struct cdevsw *cdevsw;
40989c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4105002a60fSMarcel Moolenaar 
4115002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
4125002a60fSMarcel Moolenaar 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
4135002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
4145002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
4155002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
4165002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
4175002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
4185002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
4195002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
4205002a60fSMarcel Moolenaar 	lbuf.st_atime = buf->st_atime;
4215002a60fSMarcel Moolenaar 	lbuf.st_mtime = buf->st_mtime;
4225002a60fSMarcel Moolenaar 	lbuf.st_ctime = buf->st_ctime;
4235002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
4245002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
4255002a60fSMarcel Moolenaar 
426616aa29aSMartin Blapp 	/* Lie about disk drives which are character devices
427616aa29aSMartin Blapp 	 * in FreeBSD but block devices under Linux.
428616aa29aSMartin Blapp 	 */
429616aa29aSMartin Blapp 	if (S_ISCHR(lbuf.st_mode) &&
430f3732fd1SPoul-Henning Kamp 	    (dev = findcdev(buf->st_rdev)) != NULL) {
431616aa29aSMartin Blapp 		cdevsw = devsw(dev);
432616aa29aSMartin Blapp 		if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
433616aa29aSMartin Blapp 			lbuf.st_mode &= ~S_IFMT;
434616aa29aSMartin Blapp 			lbuf.st_mode |= S_IFBLK;
435616aa29aSMartin Blapp 
436616aa29aSMartin Blapp 			/* XXX this may not be quite right */
437616aa29aSMartin Blapp 			/* Map major number to 0 */
438616aa29aSMartin Blapp 			lbuf.st_dev = uminor(buf->st_dev) & 0xf;
439616aa29aSMartin Blapp 			lbuf.st_rdev = buf->st_rdev & 0xff;
440616aa29aSMartin Blapp 		}
441616aa29aSMartin Blapp 	}
442616aa29aSMartin Blapp 
4435002a60fSMarcel Moolenaar 	/*
4445002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
4455002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
4465002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
4475002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
4485002a60fSMarcel Moolenaar 	 */
4495002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
4505002a60fSMarcel Moolenaar 
4515002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
4525002a60fSMarcel Moolenaar }
4535002a60fSMarcel Moolenaar 
4545002a60fSMarcel Moolenaar int
455b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
4565002a60fSMarcel Moolenaar {
4575002a60fSMarcel Moolenaar 	struct stat buf;
4585002a60fSMarcel Moolenaar 	struct nameidata nd;
4595002a60fSMarcel Moolenaar 	int error;
460206a5d3aSIan Dowse 	char *filename;
4615002a60fSMarcel Moolenaar 
462206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
4635002a60fSMarcel Moolenaar 
4645002a60fSMarcel Moolenaar #ifdef DEBUG
4655002a60fSMarcel Moolenaar 	if (ldebug(stat64))
466206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
4675002a60fSMarcel Moolenaar #endif
4685002a60fSMarcel Moolenaar 
469206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
470206a5d3aSIan Dowse 	    td);
4715002a60fSMarcel Moolenaar 	error = namei(&nd);
472206a5d3aSIan Dowse 	LFREEPATH(filename);
4735002a60fSMarcel Moolenaar 	if (error)
4745002a60fSMarcel Moolenaar 		return (error);
4755002a60fSMarcel Moolenaar 	NDFREE(&nd, NDF_ONLY_PNBUF);
4765002a60fSMarcel Moolenaar 
477ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
4785002a60fSMarcel Moolenaar 	vput(nd.ni_vp);
4795002a60fSMarcel Moolenaar 	if (error)
4805002a60fSMarcel Moolenaar 		return (error);
4815002a60fSMarcel Moolenaar 
4825002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
4835002a60fSMarcel Moolenaar }
4845002a60fSMarcel Moolenaar 
4855002a60fSMarcel Moolenaar int
486b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
4875002a60fSMarcel Moolenaar {
4885002a60fSMarcel Moolenaar 	int error;
4895002a60fSMarcel Moolenaar 	struct stat sb;
4905002a60fSMarcel Moolenaar 	struct nameidata nd;
491206a5d3aSIan Dowse 	char *filename;
4925002a60fSMarcel Moolenaar 
493206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
4945002a60fSMarcel Moolenaar 
4955002a60fSMarcel Moolenaar #ifdef DEBUG
4965002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
4975002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
4985002a60fSMarcel Moolenaar #endif
4995002a60fSMarcel Moolenaar 
500206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
501206a5d3aSIan Dowse 	    td);
5025002a60fSMarcel Moolenaar 	error = namei(&nd);
503206a5d3aSIan Dowse 	LFREEPATH(filename);
5045002a60fSMarcel Moolenaar 	if (error)
5055002a60fSMarcel Moolenaar 		return (error);
5065002a60fSMarcel Moolenaar 	NDFREE(&nd, NDF_ONLY_PNBUF);
5075002a60fSMarcel Moolenaar 
508ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
5095002a60fSMarcel Moolenaar 	vput(nd.ni_vp);
5105002a60fSMarcel Moolenaar 	if (error)
5115002a60fSMarcel Moolenaar 		return (error);
5125002a60fSMarcel Moolenaar 
5135002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
5145002a60fSMarcel Moolenaar }
5155002a60fSMarcel Moolenaar 
5165002a60fSMarcel Moolenaar int
517b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
5185002a60fSMarcel Moolenaar {
5195002a60fSMarcel Moolenaar 	struct filedesc *fdp;
5205002a60fSMarcel Moolenaar 	struct file *fp;
5215002a60fSMarcel Moolenaar 	struct stat buf;
5225002a60fSMarcel Moolenaar 	int error;
5235002a60fSMarcel Moolenaar 
5245002a60fSMarcel Moolenaar #ifdef DEBUG
5255002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
5265002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
5275002a60fSMarcel Moolenaar #endif
5285002a60fSMarcel Moolenaar 
529b40ce416SJulian Elischer 	fdp = td->td_proc->p_fd;
5305002a60fSMarcel Moolenaar 	if ((unsigned)args->fd >= fdp->fd_nfiles ||
5315002a60fSMarcel Moolenaar 	    (fp = fdp->fd_ofiles[args->fd]) == NULL)
5325002a60fSMarcel Moolenaar 		return (EBADF);
5335002a60fSMarcel Moolenaar 
534ea6027a8SRobert Watson 	error = fo_stat(fp, &buf, td->td_ucred, td);
5355002a60fSMarcel Moolenaar 	if (!error)
5365002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
5375002a60fSMarcel Moolenaar 
5385002a60fSMarcel Moolenaar 	return (error);
5395002a60fSMarcel Moolenaar }
5405002a60fSMarcel Moolenaar 
5414af27623STim J. Robbins #endif /* __i386__ || __amd64__ */
542