xref: /freebsd/sys/compat/linux/linux_stats.c (revision bbbc2d967e7566506ad2d9aff6899eee7702081a)
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>
35c21dee17SSøren Schmidt #include <sys/dirent.h>
36c21dee17SSøren Schmidt #include <sys/file.h>
37c21dee17SSøren Schmidt #include <sys/filedesc.h>
38c21dee17SSøren Schmidt #include <sys/proc.h>
39eddc160eSRobert Watson #include <sys/mac.h>
4085422e62SBruce Evans #include <sys/malloc.h>
41c21dee17SSøren Schmidt #include <sys/mount.h>
42c21dee17SSøren Schmidt #include <sys/namei.h>
43c21dee17SSøren Schmidt #include <sys/stat.h>
44f7a25872SJohn Baldwin #include <sys/syscallsubr.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 
501997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32
514af27623STim J. Robbins #include <machine/../linux32/linux.h>
524af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
531997c537SDavid E. O'Brien #else
541997c537SDavid E. O'Brien #include <machine/../linux/linux.h>
551997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h>
564af27623STim J. Robbins #endif
5785422e62SBruce Evans 
58ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
59762e6b85SEivind Eklund 
60bbbc2d96SPoul-Henning Kamp /*
61bbbc2d96SPoul-Henning Kamp  * XXX: This was removed from newstat_copyout(), and almost identical
62bbbc2d96SPoul-Henning Kamp  * XXX: code was in stat64_copyout().  findcdev() needs to be replaced
63bbbc2d96SPoul-Henning Kamp  * XXX: with something that does lookup and locking properly.
64bbbc2d96SPoul-Henning Kamp  * XXX: When somebody fixes this: please try to avoid duplicating it.
65bbbc2d96SPoul-Henning Kamp  */
66bbbc2d96SPoul-Henning Kamp #if 0
67bbbc2d96SPoul-Henning Kamp static void
68bbbc2d96SPoul-Henning Kamp disk_foo(struct somestat *tbuf)
69c21dee17SSøren Schmidt {
704c3a3ec0SJosef Karthauser 	struct cdevsw *cdevsw;
7189c9c53dSPoul-Henning Kamp 	struct cdev *dev;
72c21dee17SSøren Schmidt 
734c3a3ec0SJosef Karthauser 	/* Lie about disk drives which are character devices
744c3a3ec0SJosef Karthauser 	 * in FreeBSD but block devices under Linux.
754c3a3ec0SJosef Karthauser 	 */
765002a60fSMarcel Moolenaar 	if (S_ISCHR(tbuf.st_mode) &&
77f3732fd1SPoul-Henning Kamp 	    (dev = findcdev(buf->st_rdev)) != NULL) {
78f69f5fbdSPoul-Henning Kamp 		cdevsw = dev_refthread(dev);
79f69f5fbdSPoul-Henning Kamp 		if (cdevsw != NULL) {
80f69f5fbdSPoul-Henning Kamp 			if (cdevsw->d_flags & D_DISK) {
815002a60fSMarcel Moolenaar 				tbuf.st_mode &= ~S_IFMT;
825002a60fSMarcel Moolenaar 				tbuf.st_mode |= S_IFBLK;
837842f151SPaul Richards 
847842f151SPaul Richards 				/* XXX this may not be quite right */
857842f151SPaul Richards 				/* Map major number to 0 */
865002a60fSMarcel Moolenaar 				tbuf.st_dev = uminor(buf->st_dev) & 0xf;
875002a60fSMarcel Moolenaar 				tbuf.st_rdev = buf->st_rdev & 0xff;
887842f151SPaul Richards 			}
89f69f5fbdSPoul-Henning Kamp 			dev_relthread(dev);
90f69f5fbdSPoul-Henning Kamp 		}
914c3a3ec0SJosef Karthauser 	}
924e0eaf69SMarcel Moolenaar 
93bbbc2d96SPoul-Henning Kamp }
94bbbc2d96SPoul-Henning Kamp #endif
95bbbc2d96SPoul-Henning Kamp 
96bbbc2d96SPoul-Henning Kamp static int
97bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf)
98bbbc2d96SPoul-Henning Kamp {
99bbbc2d96SPoul-Henning Kamp 	struct l_newstat tbuf;
100bbbc2d96SPoul-Henning Kamp 
101bbbc2d96SPoul-Henning Kamp 	bzero(&tbuf, sizeof(tbuf));
102bbbc2d96SPoul-Henning Kamp 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
103bbbc2d96SPoul-Henning Kamp 	tbuf.st_ino = buf->st_ino;
104bbbc2d96SPoul-Henning Kamp 	tbuf.st_mode = buf->st_mode;
105bbbc2d96SPoul-Henning Kamp 	tbuf.st_nlink = buf->st_nlink;
106bbbc2d96SPoul-Henning Kamp 	tbuf.st_uid = buf->st_uid;
107bbbc2d96SPoul-Henning Kamp 	tbuf.st_gid = buf->st_gid;
108bbbc2d96SPoul-Henning Kamp 	tbuf.st_rdev = buf->st_rdev;
109bbbc2d96SPoul-Henning Kamp 	tbuf.st_size = buf->st_size;
110bbbc2d96SPoul-Henning Kamp 	tbuf.st_atime = buf->st_atime;
111bbbc2d96SPoul-Henning Kamp 	tbuf.st_mtime = buf->st_mtime;
112bbbc2d96SPoul-Henning Kamp 	tbuf.st_ctime = buf->st_ctime;
113bbbc2d96SPoul-Henning Kamp 	tbuf.st_blksize = buf->st_blksize;
114bbbc2d96SPoul-Henning Kamp 	tbuf.st_blocks = buf->st_blocks;
115bbbc2d96SPoul-Henning Kamp 
1164e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
117c21dee17SSøren Schmidt }
118c21dee17SSøren Schmidt 
119c21dee17SSøren Schmidt int
120b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
121c21dee17SSøren Schmidt {
122c21dee17SSøren Schmidt 	struct stat buf;
123206a5d3aSIan Dowse 	char *path;
124c21dee17SSøren Schmidt 	int error;
125d66a5066SPeter Wemm 
126206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
127c21dee17SSøren Schmidt 
128c21dee17SSøren Schmidt #ifdef DEBUG
12924593369SJonathan Lemon 	if (ldebug(newstat))
130206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
131c21dee17SSøren Schmidt #endif
1324e0eaf69SMarcel Moolenaar 
133f7a25872SJohn Baldwin 	error = kern_stat(td, path, UIO_SYSSPACE, &buf);
134206a5d3aSIan Dowse 	LFREEPATH(path);
1354e0eaf69SMarcel Moolenaar 	if (error)
1364e0eaf69SMarcel Moolenaar 		return (error);
1374e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
138c21dee17SSøren Schmidt }
139c21dee17SSøren Schmidt 
140c21dee17SSøren Schmidt int
141b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
142c21dee17SSøren Schmidt {
1434e0eaf69SMarcel Moolenaar 	struct stat sb;
144206a5d3aSIan Dowse 	char *path;
145f7a25872SJohn Baldwin 	int error;
146d66a5066SPeter Wemm 
147206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
148c21dee17SSøren Schmidt 
149c21dee17SSøren Schmidt #ifdef DEBUG
15024593369SJonathan Lemon 	if (ldebug(newlstat))
151206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
152c21dee17SSøren Schmidt #endif
1534e0eaf69SMarcel Moolenaar 
154f7a25872SJohn Baldwin 	error = kern_lstat(td, path, UIO_SYSSPACE, &sb);
155206a5d3aSIan Dowse 	LFREEPATH(path);
156d66a5066SPeter Wemm 	if (error)
157d66a5066SPeter Wemm 		return (error);
1585002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
159d66a5066SPeter Wemm }
160c21dee17SSøren Schmidt 
161c21dee17SSøren Schmidt int
162b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
163c21dee17SSøren Schmidt {
164c21dee17SSøren Schmidt 	struct stat buf;
165c21dee17SSøren Schmidt 	int error;
166c21dee17SSøren Schmidt 
167c21dee17SSøren Schmidt #ifdef DEBUG
16824593369SJonathan Lemon 	if (ldebug(newfstat))
16924593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
170c21dee17SSøren Schmidt #endif
1714e0eaf69SMarcel Moolenaar 
172f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
173c21dee17SSøren Schmidt 	if (!error)
174c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
1754e0eaf69SMarcel Moolenaar 
1764e0eaf69SMarcel Moolenaar 	return (error);
177c21dee17SSøren Schmidt }
178c21dee17SSøren Schmidt 
1795002a60fSMarcel Moolenaar /* XXX - All fields of type l_int are defined as l_long on i386 */
1805002a60fSMarcel Moolenaar struct l_statfs {
1815002a60fSMarcel Moolenaar 	l_int		f_type;
1825002a60fSMarcel Moolenaar 	l_int		f_bsize;
1835002a60fSMarcel Moolenaar 	l_int		f_blocks;
1845002a60fSMarcel Moolenaar 	l_int		f_bfree;
1855002a60fSMarcel Moolenaar 	l_int		f_bavail;
1865002a60fSMarcel Moolenaar 	l_int		f_files;
1875002a60fSMarcel Moolenaar 	l_int		f_ffree;
1885002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
1895002a60fSMarcel Moolenaar 	l_int		f_namelen;
1905002a60fSMarcel Moolenaar 	l_int		f_spare[6];
191c21dee17SSøren Schmidt };
192c21dee17SSøren Schmidt 
193dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
194dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
195dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
196dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
197dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
198dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
199dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
200dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
201dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
202dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
203dca60efcSMarcel Moolenaar 
204dca60efcSMarcel Moolenaar static long
205962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
206dca60efcSMarcel Moolenaar {
207962cf420SMaxim Sobolev 	int i;
208962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
209962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
210962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
211962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
212962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
213962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
214962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
215962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
216962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
217962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
218962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
219962cf420SMaxim Sobolev 		{NULL,      0L}};
220dca60efcSMarcel Moolenaar 
221962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
222962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
223962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
224dca60efcSMarcel Moolenaar 
225dca60efcSMarcel Moolenaar 	return (0L);
226dca60efcSMarcel Moolenaar }
227dca60efcSMarcel Moolenaar 
228f7a25872SJohn Baldwin static void
229f7a25872SJohn Baldwin bsd_to_linux_statfs(struct thread *td, struct statfs *bsd_statfs,
230f7a25872SJohn Baldwin     struct l_statfs *linux_statfs)
231f7a25872SJohn Baldwin {
232f7a25872SJohn Baldwin 
233f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
234f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
235f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
236f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
237f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
238f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
239f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
240f7a25872SJohn Baldwin 	if (suser(td)) {
241f7a25872SJohn Baldwin 		linux_statfs->f_fsid.val[0] = 0;
242f7a25872SJohn Baldwin 		linux_statfs->f_fsid.val[1] = 0;
243f7a25872SJohn Baldwin 	} else {
244f7a25872SJohn Baldwin 		linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
245f7a25872SJohn Baldwin 		linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
246f7a25872SJohn Baldwin 	}
247f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
248f7a25872SJohn Baldwin }
249f7a25872SJohn Baldwin 
250c21dee17SSøren Schmidt int
251b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
252c21dee17SSøren Schmidt {
2535002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
254f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
255206a5d3aSIan Dowse 	char *path;
256c21dee17SSøren Schmidt 	int error;
257d66a5066SPeter Wemm 
258206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
259c21dee17SSøren Schmidt 
260c21dee17SSøren Schmidt #ifdef DEBUG
26124593369SJonathan Lemon 	if (ldebug(statfs))
262206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
263c21dee17SSøren Schmidt #endif
264f7a25872SJohn Baldwin 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
265206a5d3aSIan Dowse 	LFREEPATH(path);
266d5558c00SPeter Wemm 	if (error)
267eddc160eSRobert Watson 		return (error);
268f7a25872SJohn Baldwin 	bsd_to_linux_statfs(td, &bsd_statfs, &linux_statfs);
2694b7ef73dSDag-Erling Smørgrav 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
270c21dee17SSøren Schmidt }
271c21dee17SSøren Schmidt 
272c21dee17SSøren Schmidt int
273b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
274c21dee17SSøren Schmidt {
2755002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
276f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
277c21dee17SSøren Schmidt 	int error;
278c21dee17SSøren Schmidt 
279c21dee17SSøren Schmidt #ifdef DEBUG
28024593369SJonathan Lemon 	if (ldebug(fstatfs))
28124593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
282c21dee17SSøren Schmidt #endif
283f7a25872SJohn Baldwin 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
284d5558c00SPeter Wemm 	if (error)
285c21dee17SSøren Schmidt 		return error;
286f7a25872SJohn Baldwin 	bsd_to_linux_statfs(td, &bsd_statfs, &linux_statfs);
287f7a25872SJohn Baldwin 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
288c21dee17SSøren Schmidt }
289408da119SMarcel Moolenaar 
2905002a60fSMarcel Moolenaar struct l_ustat
291408da119SMarcel Moolenaar {
2925002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
2935002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
2945002a60fSMarcel Moolenaar 	char		f_fname[6];
2955002a60fSMarcel Moolenaar 	char		f_fpack[6];
2965002a60fSMarcel Moolenaar };
2975002a60fSMarcel Moolenaar 
2985002a60fSMarcel Moolenaar int
299b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
3005002a60fSMarcel Moolenaar {
3011e247cc2SPoul-Henning Kamp #ifdef DEBUG
3021e247cc2SPoul-Henning Kamp 	if (ldebug(ustat))
3031e247cc2SPoul-Henning Kamp 		printf(ARGS(ustat, "%d, *"), args->dev);
3041e247cc2SPoul-Henning Kamp #endif
3051e247cc2SPoul-Henning Kamp 
3061e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
3071e247cc2SPoul-Henning Kamp 
3081e247cc2SPoul-Henning Kamp #ifdef not_that_way
3095002a60fSMarcel Moolenaar 	struct l_ustat lu;
31089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
311408da119SMarcel Moolenaar 	struct vnode *vp;
312408da119SMarcel Moolenaar 	struct statfs *stat;
313408da119SMarcel Moolenaar 	int error;
314408da119SMarcel Moolenaar 
315408da119SMarcel Moolenaar 
316408da119SMarcel Moolenaar 	/*
317408da119SMarcel Moolenaar 	 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
318408da119SMarcel Moolenaar 	 * lu.f_tinode and lu.f_tfree are set from the device's super block.
319408da119SMarcel Moolenaar 	 */
320408da119SMarcel Moolenaar 	bzero(&lu, sizeof(lu));
321408da119SMarcel Moolenaar 
322408da119SMarcel Moolenaar 	/*
323408da119SMarcel Moolenaar 	 * XXX - Don't return an error if we can't find a vnode for the
32489c9c53dSPoul-Henning Kamp 	 * device. Our struct cdev *is 32-bits whereas Linux only has a 16-bits
32589c9c53dSPoul-Henning Kamp 	 * struct cdev *. The struct cdev *that is used now may as well be a truncated
32689c9c53dSPoul-Henning Kamp 	 * struct cdev *returned from previous syscalls. Just return a bzeroed
327408da119SMarcel Moolenaar 	 * ustat in that case.
32841befa53SPoul-Henning Kamp 	 *
32941befa53SPoul-Henning Kamp 	 * XXX: findcdev() SHALL not be used this way.  Somebody (TM) will
33041befa53SPoul-Henning Kamp 	 *	have to find a better way.  It may be that we should stick
33141befa53SPoul-Henning Kamp 	 *	a dev_t into struct mount, and walk the mountlist for a
33241befa53SPoul-Henning Kamp 	 *	perfect match and failing that try again looking for a
33341befa53SPoul-Henning Kamp 	 *	minor-truncated match.
334408da119SMarcel Moolenaar 	 */
335f3732fd1SPoul-Henning Kamp 	dev = findcdev(makedev(args->dev >> 8, args->dev & 0xFF));
336f3732fd1SPoul-Henning Kamp 	if (dev != NULL && vfinddev(dev, &vp)) {
337408da119SMarcel Moolenaar 		if (vp->v_mount == NULL)
338408da119SMarcel Moolenaar 			return (EINVAL);
339eddc160eSRobert Watson #ifdef MAC
34031566c96SJohn Baldwin 		error = mac_check_mount_stat(td->td_ucred, vp->v_mount);
341eddc160eSRobert Watson 		if (error)
342eddc160eSRobert Watson 			return (error);
343eddc160eSRobert Watson #endif
344408da119SMarcel Moolenaar 		stat = &(vp->v_mount->mnt_stat);
345b40ce416SJulian Elischer 		error = VFS_STATFS(vp->v_mount, stat, td);
346408da119SMarcel Moolenaar 		if (error)
347408da119SMarcel Moolenaar 			return (error);
348408da119SMarcel Moolenaar 
349408da119SMarcel Moolenaar 		lu.f_tfree = stat->f_bfree;
350408da119SMarcel Moolenaar 		lu.f_tinode = stat->f_ffree;
351408da119SMarcel Moolenaar 	}
352408da119SMarcel Moolenaar 
3535002a60fSMarcel Moolenaar 	return (copyout(&lu, args->ubuf, sizeof(lu)));
3541e247cc2SPoul-Henning Kamp #endif
355408da119SMarcel Moolenaar }
3565002a60fSMarcel Moolenaar 
3571997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
3585002a60fSMarcel Moolenaar 
3595002a60fSMarcel Moolenaar static int
3605002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
3615002a60fSMarcel Moolenaar {
3625002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
3635002a60fSMarcel Moolenaar 
3645002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
3655002a60fSMarcel Moolenaar 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
3665002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
3675002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
3685002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
3695002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
3705002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
3715002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
3725002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
3735002a60fSMarcel Moolenaar 	lbuf.st_atime = buf->st_atime;
3745002a60fSMarcel Moolenaar 	lbuf.st_mtime = buf->st_mtime;
3755002a60fSMarcel Moolenaar 	lbuf.st_ctime = buf->st_ctime;
3765002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
3775002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
3785002a60fSMarcel Moolenaar 
3795002a60fSMarcel Moolenaar 	/*
3805002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
3815002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
3825002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
3835002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
3845002a60fSMarcel Moolenaar 	 */
3855002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
3865002a60fSMarcel Moolenaar 
3875002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
3885002a60fSMarcel Moolenaar }
3895002a60fSMarcel Moolenaar 
3905002a60fSMarcel Moolenaar int
391b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
3925002a60fSMarcel Moolenaar {
3935002a60fSMarcel Moolenaar 	struct stat buf;
394206a5d3aSIan Dowse 	char *filename;
395f7a25872SJohn Baldwin 	int error;
3965002a60fSMarcel Moolenaar 
397206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
3985002a60fSMarcel Moolenaar 
3995002a60fSMarcel Moolenaar #ifdef DEBUG
4005002a60fSMarcel Moolenaar 	if (ldebug(stat64))
401206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
4025002a60fSMarcel Moolenaar #endif
4035002a60fSMarcel Moolenaar 
404f7a25872SJohn Baldwin 	error = kern_stat(td, filename, UIO_SYSSPACE, &buf);
405206a5d3aSIan Dowse 	LFREEPATH(filename);
4065002a60fSMarcel Moolenaar 	if (error)
4075002a60fSMarcel Moolenaar 		return (error);
4085002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
4095002a60fSMarcel Moolenaar }
4105002a60fSMarcel Moolenaar 
4115002a60fSMarcel Moolenaar int
412b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
4135002a60fSMarcel Moolenaar {
4145002a60fSMarcel Moolenaar 	struct stat sb;
415206a5d3aSIan Dowse 	char *filename;
416f7a25872SJohn Baldwin 	int error;
4175002a60fSMarcel Moolenaar 
418206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
4195002a60fSMarcel Moolenaar 
4205002a60fSMarcel Moolenaar #ifdef DEBUG
4215002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
4225002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
4235002a60fSMarcel Moolenaar #endif
4245002a60fSMarcel Moolenaar 
425f7a25872SJohn Baldwin 	error = kern_lstat(td, filename, UIO_SYSSPACE, &sb);
426206a5d3aSIan Dowse 	LFREEPATH(filename);
4275002a60fSMarcel Moolenaar 	if (error)
4285002a60fSMarcel Moolenaar 		return (error);
4295002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
4305002a60fSMarcel Moolenaar }
4315002a60fSMarcel Moolenaar 
4325002a60fSMarcel Moolenaar int
433b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
4345002a60fSMarcel Moolenaar {
4355002a60fSMarcel Moolenaar 	struct stat buf;
4365002a60fSMarcel Moolenaar 	int error;
4375002a60fSMarcel Moolenaar 
4385002a60fSMarcel Moolenaar #ifdef DEBUG
4395002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
4405002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
4415002a60fSMarcel Moolenaar #endif
4425002a60fSMarcel Moolenaar 
443f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
4445002a60fSMarcel Moolenaar 	if (!error)
4455002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
4465002a60fSMarcel Moolenaar 
4475002a60fSMarcel Moolenaar 	return (error);
4485002a60fSMarcel Moolenaar }
4495002a60fSMarcel Moolenaar 
4501997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
451