xref: /freebsd/sys/compat/linux/linux_stats.c (revision 85422e62d3453be59520eca83273c2acb880ecd2)
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  *
28c3aac50fSPeter Wemm  * $FreeBSD$
29c21dee17SSøren Schmidt  */
30c21dee17SSøren Schmidt 
31eddc160eSRobert Watson #include "opt_mac.h"
32eddc160eSRobert Watson 
33c21dee17SSøren Schmidt #include <sys/param.h>
34408da119SMarcel Moolenaar #include <sys/conf.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>
44408da119SMarcel Moolenaar #include <sys/systm.h>
45c21dee17SSøren Schmidt #include <sys/vnode.h>
46c21dee17SSøren Schmidt 
47ac951e62SMarcel Moolenaar #include <machine/../linux/linux.h>
48ebea8660SMarcel Moolenaar #include <machine/../linux/linux_proto.h>
4985422e62SBruce Evans 
50ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
51762e6b85SEivind Eklund 
52c21dee17SSøren Schmidt static int
53c21dee17SSøren Schmidt newstat_copyout(struct stat *buf, void *ubuf)
54c21dee17SSøren Schmidt {
555002a60fSMarcel Moolenaar 	struct l_newstat tbuf;
564c3a3ec0SJosef Karthauser 	struct cdevsw *cdevsw;
574c3a3ec0SJosef Karthauser 	dev_t dev;
58c21dee17SSøren Schmidt 
595002a60fSMarcel Moolenaar 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
605002a60fSMarcel Moolenaar 	tbuf.st_ino = buf->st_ino;
615002a60fSMarcel Moolenaar 	tbuf.st_mode = buf->st_mode;
625002a60fSMarcel Moolenaar 	tbuf.st_nlink = buf->st_nlink;
635002a60fSMarcel Moolenaar 	tbuf.st_uid = buf->st_uid;
645002a60fSMarcel Moolenaar 	tbuf.st_gid = buf->st_gid;
655002a60fSMarcel Moolenaar 	tbuf.st_rdev = buf->st_rdev;
665002a60fSMarcel Moolenaar 	tbuf.st_size = buf->st_size;
675002a60fSMarcel Moolenaar 	tbuf.st_atime = buf->st_atime;
685002a60fSMarcel Moolenaar 	tbuf.st_mtime = buf->st_mtime;
695002a60fSMarcel Moolenaar 	tbuf.st_ctime = buf->st_ctime;
705002a60fSMarcel Moolenaar 	tbuf.st_blksize = buf->st_blksize;
715002a60fSMarcel Moolenaar 	tbuf.st_blocks = buf->st_blocks;
727842f151SPaul Richards 
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) &&
774c3a3ec0SJosef Karthauser 	    (dev = udev2dev(buf->st_rdev, 0)) != NODEV) {
784c3a3ec0SJosef Karthauser 		cdevsw = devsw(dev);
794c3a3ec0SJosef Karthauser 		if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
805002a60fSMarcel Moolenaar 			tbuf.st_mode &= ~S_IFMT;
815002a60fSMarcel Moolenaar 			tbuf.st_mode |= S_IFBLK;
827842f151SPaul Richards 
837842f151SPaul Richards 			/* XXX this may not be quite right */
847842f151SPaul Richards 			/* Map major number to 0 */
855002a60fSMarcel Moolenaar 			tbuf.st_dev = uminor(buf->st_dev) & 0xf;
865002a60fSMarcel Moolenaar 			tbuf.st_rdev = buf->st_rdev & 0xff;
877842f151SPaul Richards 		}
884c3a3ec0SJosef Karthauser 	}
894e0eaf69SMarcel Moolenaar 
904e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
91c21dee17SSøren Schmidt }
92c21dee17SSøren Schmidt 
93c21dee17SSøren Schmidt int
94b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
95c21dee17SSøren Schmidt {
96c21dee17SSøren Schmidt 	struct stat buf;
97c21dee17SSøren Schmidt 	struct nameidata nd;
98206a5d3aSIan Dowse 	char *path;
99c21dee17SSøren Schmidt 	int error;
100d66a5066SPeter Wemm 
101206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
102c21dee17SSøren Schmidt 
103c21dee17SSøren Schmidt #ifdef DEBUG
10424593369SJonathan Lemon 	if (ldebug(newstat))
105206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
106c21dee17SSøren Schmidt #endif
1074e0eaf69SMarcel Moolenaar 
108206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path, td);
109c21dee17SSøren Schmidt 	error = namei(&nd);
110206a5d3aSIan Dowse 	LFREEPATH(path);
1114e0eaf69SMarcel Moolenaar 	if (error)
1124e0eaf69SMarcel Moolenaar 		return (error);
113762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1144e0eaf69SMarcel Moolenaar 
115ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
116c21dee17SSøren Schmidt 	vput(nd.ni_vp);
1174e0eaf69SMarcel Moolenaar 	if (error)
1184e0eaf69SMarcel Moolenaar 		return (error);
1194e0eaf69SMarcel Moolenaar 
1204e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
121c21dee17SSøren Schmidt }
122c21dee17SSøren Schmidt 
123c21dee17SSøren Schmidt int
124b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
125c21dee17SSøren Schmidt {
126c21dee17SSøren Schmidt 	int error;
1274e0eaf69SMarcel Moolenaar 	struct stat sb;
128d66a5066SPeter Wemm 	struct nameidata nd;
129206a5d3aSIan Dowse 	char *path;
130d66a5066SPeter Wemm 
131206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
132c21dee17SSøren Schmidt 
133c21dee17SSøren Schmidt #ifdef DEBUG
13424593369SJonathan Lemon 	if (ldebug(newlstat))
135206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
136c21dee17SSøren Schmidt #endif
1374e0eaf69SMarcel Moolenaar 
138206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path,
139206a5d3aSIan Dowse 	    td);
140c21dee17SSøren Schmidt 	error = namei(&nd);
141206a5d3aSIan Dowse 	LFREEPATH(path);
142d66a5066SPeter Wemm 	if (error)
143d66a5066SPeter Wemm 		return (error);
144762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
1454e0eaf69SMarcel Moolenaar 
146ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
1475002a60fSMarcel Moolenaar 	vput(nd.ni_vp);
148d66a5066SPeter Wemm 	if (error)
149d66a5066SPeter Wemm 		return (error);
1504e0eaf69SMarcel Moolenaar 
1515002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
152d66a5066SPeter Wemm }
153c21dee17SSøren Schmidt 
154c21dee17SSøren Schmidt int
155b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
156c21dee17SSøren Schmidt {
157c21dee17SSøren Schmidt 	struct file *fp;
158c21dee17SSøren Schmidt 	struct stat buf;
159c21dee17SSøren Schmidt 	int error;
160c21dee17SSøren Schmidt 
161c21dee17SSøren Schmidt #ifdef DEBUG
16224593369SJonathan Lemon 	if (ldebug(newfstat))
16324593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
164c21dee17SSøren Schmidt #endif
1654e0eaf69SMarcel Moolenaar 
166a4db4953SAlfred Perlstein 	if ((error = fget(td, args->fd, &fp)) != 0)
167a4db4953SAlfred Perlstein 		return (error);
1684e0eaf69SMarcel Moolenaar 
169ea6027a8SRobert Watson 	error = fo_stat(fp, &buf, td->td_ucred, td);
170426da3bcSAlfred Perlstein 	fdrop(fp, td);
171c21dee17SSøren Schmidt 	if (!error)
172c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
1734e0eaf69SMarcel Moolenaar 
1744e0eaf69SMarcel Moolenaar 	return (error);
175c21dee17SSøren Schmidt }
176c21dee17SSøren Schmidt 
1775002a60fSMarcel Moolenaar /* XXX - All fields of type l_int are defined as l_long on i386 */
1785002a60fSMarcel Moolenaar struct l_statfs {
1795002a60fSMarcel Moolenaar 	l_int		f_type;
1805002a60fSMarcel Moolenaar 	l_int		f_bsize;
1815002a60fSMarcel Moolenaar 	l_int		f_blocks;
1825002a60fSMarcel Moolenaar 	l_int		f_bfree;
1835002a60fSMarcel Moolenaar 	l_int		f_bavail;
1845002a60fSMarcel Moolenaar 	l_int		f_files;
1855002a60fSMarcel Moolenaar 	l_int		f_ffree;
1865002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
1875002a60fSMarcel Moolenaar 	l_int		f_namelen;
1885002a60fSMarcel Moolenaar 	l_int		f_spare[6];
189c21dee17SSøren Schmidt };
190c21dee17SSøren Schmidt 
191dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
192dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
193dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
194dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
195dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
196dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
197dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
198dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
199dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
200dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
201dca60efcSMarcel Moolenaar 
202dca60efcSMarcel Moolenaar static long
203962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
204dca60efcSMarcel Moolenaar {
205962cf420SMaxim Sobolev 	int i;
206962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
207962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
208962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
209962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
210962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
211962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
212962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
213962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
214962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
215962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
216962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
217962cf420SMaxim Sobolev 		{NULL,      0L}};
218dca60efcSMarcel Moolenaar 
219962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
220962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
221962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
222dca60efcSMarcel Moolenaar 
223dca60efcSMarcel Moolenaar 	return (0L);
224dca60efcSMarcel Moolenaar }
225dca60efcSMarcel Moolenaar 
226c21dee17SSøren Schmidt int
227b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
228c21dee17SSøren Schmidt {
229c21dee17SSøren Schmidt 	struct mount *mp;
230c21dee17SSøren Schmidt 	struct nameidata *ndp;
231c21dee17SSøren Schmidt 	struct statfs *bsd_statfs;
232c21dee17SSøren Schmidt 	struct nameidata nd;
2335002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
234206a5d3aSIan Dowse 	char *path;
235c21dee17SSøren Schmidt 	int error;
236d66a5066SPeter Wemm 
237206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
238c21dee17SSøren Schmidt 
239c21dee17SSøren Schmidt #ifdef DEBUG
24024593369SJonathan Lemon 	if (ldebug(statfs))
241206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
242c21dee17SSøren Schmidt #endif
243c21dee17SSøren Schmidt 	ndp = &nd;
244206a5d3aSIan Dowse 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
245d5558c00SPeter Wemm 	error = namei(ndp);
246206a5d3aSIan Dowse 	LFREEPATH(path);
247d5558c00SPeter Wemm 	if (error)
248c21dee17SSøren Schmidt 		return error;
249762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
250c21dee17SSøren Schmidt 	mp = ndp->ni_vp->v_mount;
251c21dee17SSøren Schmidt 	bsd_statfs = &mp->mnt_stat;
252c21dee17SSøren Schmidt 	vrele(ndp->ni_vp);
253eddc160eSRobert Watson #ifdef MAC
254eddc160eSRobert Watson 	error = mac_check_mount_stat(td->td_proc->p_ucred, mp);
255eddc160eSRobert Watson 	if (error)
256eddc160eSRobert Watson 		return (error);
257eddc160eSRobert Watson #endif
258b40ce416SJulian Elischer 	error = VFS_STATFS(mp, bsd_statfs, td);
259d5558c00SPeter Wemm 	if (error)
260c21dee17SSøren Schmidt 		return error;
261c21dee17SSøren Schmidt 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
262962cf420SMaxim Sobolev 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
2635002a60fSMarcel Moolenaar 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
2645002a60fSMarcel Moolenaar 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
2655002a60fSMarcel Moolenaar 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
2665002a60fSMarcel Moolenaar 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
2675002a60fSMarcel Moolenaar   	linux_statfs.f_ffree = bsd_statfs->f_ffree;
2685002a60fSMarcel Moolenaar 	linux_statfs.f_files = bsd_statfs->f_files;
2695002a60fSMarcel Moolenaar 	linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
2705002a60fSMarcel Moolenaar 	linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
2715002a60fSMarcel Moolenaar 	linux_statfs.f_namelen = MAXNAMLEN;
2725002a60fSMarcel Moolenaar 	return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
2735002a60fSMarcel Moolenaar 	    sizeof(linux_statfs));
274c21dee17SSøren Schmidt }
275c21dee17SSøren Schmidt 
276c21dee17SSøren Schmidt int
277b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
278c21dee17SSøren Schmidt {
279c21dee17SSøren Schmidt 	struct file *fp;
280c21dee17SSøren Schmidt 	struct mount *mp;
281c21dee17SSøren Schmidt 	struct statfs *bsd_statfs;
2825002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
283c21dee17SSøren Schmidt 	int error;
284c21dee17SSøren Schmidt 
285c21dee17SSøren Schmidt #ifdef DEBUG
28624593369SJonathan Lemon 	if (ldebug(fstatfs))
28724593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
288c21dee17SSøren Schmidt #endif
289b40ce416SJulian Elischer 	error = getvnode(td->td_proc->p_fd, args->fd, &fp);
290d5558c00SPeter Wemm 	if (error)
291c21dee17SSøren Schmidt 		return error;
292c21dee17SSøren Schmidt 	mp = ((struct vnode *)fp->f_data)->v_mount;
293eddc160eSRobert Watson #ifdef MAC
294eddc160eSRobert Watson 	error = mac_check_mount_stat(td->td_proc->p_ucred, mp);
295eddc160eSRobert Watson 	if (error) {
296eddc160eSRobert Watson 		fdrop(fp, td);
297eddc160eSRobert Watson 		return (error);
298eddc160eSRobert Watson 	}
299eddc160eSRobert Watson #endif
300c21dee17SSøren Schmidt 	bsd_statfs = &mp->mnt_stat;
301b40ce416SJulian Elischer 	error = VFS_STATFS(mp, bsd_statfs, td);
302426da3bcSAlfred Perlstein 	if (error) {
303426da3bcSAlfred Perlstein 		fdrop(fp, td);
304c21dee17SSøren Schmidt 		return error;
305426da3bcSAlfred Perlstein 	}
306c21dee17SSøren Schmidt 	bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
307962cf420SMaxim Sobolev 	linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
3085002a60fSMarcel Moolenaar 	linux_statfs.f_bsize = bsd_statfs->f_bsize;
3095002a60fSMarcel Moolenaar 	linux_statfs.f_blocks = bsd_statfs->f_blocks;
3105002a60fSMarcel Moolenaar 	linux_statfs.f_bfree = bsd_statfs->f_bfree;
3115002a60fSMarcel Moolenaar 	linux_statfs.f_bavail = bsd_statfs->f_bavail;
3125002a60fSMarcel Moolenaar   	linux_statfs.f_ffree = bsd_statfs->f_ffree;
3135002a60fSMarcel Moolenaar 	linux_statfs.f_files = bsd_statfs->f_files;
3145002a60fSMarcel Moolenaar 	linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
3155002a60fSMarcel Moolenaar 	linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
3165002a60fSMarcel Moolenaar 	linux_statfs.f_namelen = MAXNAMLEN;
317426da3bcSAlfred Perlstein 	error = copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
3185002a60fSMarcel Moolenaar 	    sizeof(linux_statfs));
319426da3bcSAlfred Perlstein 	fdrop(fp, td);
320426da3bcSAlfred Perlstein 	return error;
321c21dee17SSøren Schmidt }
322408da119SMarcel Moolenaar 
3235002a60fSMarcel Moolenaar struct l_ustat
324408da119SMarcel Moolenaar {
3255002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
3265002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
3275002a60fSMarcel Moolenaar 	char		f_fname[6];
3285002a60fSMarcel Moolenaar 	char		f_fpack[6];
3295002a60fSMarcel Moolenaar };
3305002a60fSMarcel Moolenaar 
3315002a60fSMarcel Moolenaar int
332b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
3335002a60fSMarcel Moolenaar {
3345002a60fSMarcel Moolenaar 	struct l_ustat lu;
335408da119SMarcel Moolenaar 	dev_t dev;
336408da119SMarcel Moolenaar 	struct vnode *vp;
337408da119SMarcel Moolenaar 	struct statfs *stat;
338408da119SMarcel Moolenaar 	int error;
339408da119SMarcel Moolenaar 
340408da119SMarcel Moolenaar #ifdef DEBUG
34124593369SJonathan Lemon 	if (ldebug(ustat))
3425002a60fSMarcel Moolenaar 		printf(ARGS(ustat, "%d, *"), args->dev);
343408da119SMarcel Moolenaar #endif
344408da119SMarcel Moolenaar 
345408da119SMarcel Moolenaar 	/*
346408da119SMarcel Moolenaar 	 * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
347408da119SMarcel Moolenaar 	 * lu.f_tinode and lu.f_tfree are set from the device's super block.
348408da119SMarcel Moolenaar 	 */
349408da119SMarcel Moolenaar 	bzero(&lu, sizeof(lu));
350408da119SMarcel Moolenaar 
351408da119SMarcel Moolenaar 	/*
352408da119SMarcel Moolenaar 	 * XXX - Don't return an error if we can't find a vnode for the
353408da119SMarcel Moolenaar 	 * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
354408da119SMarcel Moolenaar 	 * dev_t. The dev_t that is used now may as well be a truncated
355408da119SMarcel Moolenaar 	 * dev_t returned from previous syscalls. Just return a bzeroed
356408da119SMarcel Moolenaar 	 * ustat in that case.
357408da119SMarcel Moolenaar 	 */
3585002a60fSMarcel Moolenaar 	dev = makedev(args->dev >> 8, args->dev & 0xFF);
359a4e13024SMarcel Moolenaar 	if (vfinddev(dev, VCHR, &vp)) {
360408da119SMarcel Moolenaar 		if (vp->v_mount == NULL)
361408da119SMarcel Moolenaar 			return (EINVAL);
362eddc160eSRobert Watson #ifdef MAC
3639702d652SRobert Watson 		error = mac_check_mount_stat(td->td_proc->p_ucred,
3649702d652SRobert Watson 		    vp->v_mount);
365eddc160eSRobert Watson 		if (error)
366eddc160eSRobert Watson 			return (error);
367eddc160eSRobert Watson #endif
368408da119SMarcel Moolenaar 		stat = &(vp->v_mount->mnt_stat);
369b40ce416SJulian Elischer 		error = VFS_STATFS(vp->v_mount, stat, td);
370408da119SMarcel Moolenaar 		if (error)
371408da119SMarcel Moolenaar 			return (error);
372408da119SMarcel Moolenaar 
373408da119SMarcel Moolenaar 		lu.f_tfree = stat->f_bfree;
374408da119SMarcel Moolenaar 		lu.f_tinode = stat->f_ffree;
375408da119SMarcel Moolenaar 	}
376408da119SMarcel Moolenaar 
3775002a60fSMarcel Moolenaar 	return (copyout(&lu, args->ubuf, sizeof(lu)));
378408da119SMarcel Moolenaar }
3795002a60fSMarcel Moolenaar 
3805002a60fSMarcel Moolenaar #if defined(__i386__)
3815002a60fSMarcel Moolenaar 
3825002a60fSMarcel Moolenaar static int
3835002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
3845002a60fSMarcel Moolenaar {
3855002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
3865002a60fSMarcel Moolenaar 
3875002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
3885002a60fSMarcel Moolenaar 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
3895002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
3905002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
3915002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
3925002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
3935002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
3945002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
3955002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
3965002a60fSMarcel Moolenaar 	lbuf.st_atime = buf->st_atime;
3975002a60fSMarcel Moolenaar 	lbuf.st_mtime = buf->st_mtime;
3985002a60fSMarcel Moolenaar 	lbuf.st_ctime = buf->st_ctime;
3995002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
4005002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
4015002a60fSMarcel Moolenaar 
4025002a60fSMarcel Moolenaar 	/*
4035002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
4045002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
4055002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
4065002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
4075002a60fSMarcel Moolenaar 	 */
4085002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
4095002a60fSMarcel Moolenaar 
4105002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
4115002a60fSMarcel Moolenaar }
4125002a60fSMarcel Moolenaar 
4135002a60fSMarcel Moolenaar int
414b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
4155002a60fSMarcel Moolenaar {
4165002a60fSMarcel Moolenaar 	struct stat buf;
4175002a60fSMarcel Moolenaar 	struct nameidata nd;
4185002a60fSMarcel Moolenaar 	int error;
419206a5d3aSIan Dowse 	char *filename;
4205002a60fSMarcel Moolenaar 
421206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
4225002a60fSMarcel Moolenaar 
4235002a60fSMarcel Moolenaar #ifdef DEBUG
4245002a60fSMarcel Moolenaar 	if (ldebug(stat64))
425206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
4265002a60fSMarcel Moolenaar #endif
4275002a60fSMarcel Moolenaar 
428206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
429206a5d3aSIan Dowse 	    td);
4305002a60fSMarcel Moolenaar 	error = namei(&nd);
431206a5d3aSIan Dowse 	LFREEPATH(filename);
4325002a60fSMarcel Moolenaar 	if (error)
4335002a60fSMarcel Moolenaar 		return (error);
4345002a60fSMarcel Moolenaar 	NDFREE(&nd, NDF_ONLY_PNBUF);
4355002a60fSMarcel Moolenaar 
436ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
4375002a60fSMarcel Moolenaar 	vput(nd.ni_vp);
4385002a60fSMarcel Moolenaar 	if (error)
4395002a60fSMarcel Moolenaar 		return (error);
4405002a60fSMarcel Moolenaar 
4415002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
4425002a60fSMarcel Moolenaar }
4435002a60fSMarcel Moolenaar 
4445002a60fSMarcel Moolenaar int
445b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
4465002a60fSMarcel Moolenaar {
4475002a60fSMarcel Moolenaar 	int error;
4485002a60fSMarcel Moolenaar 	struct stat sb;
4495002a60fSMarcel Moolenaar 	struct nameidata nd;
450206a5d3aSIan Dowse 	char *filename;
4515002a60fSMarcel Moolenaar 
452206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
4535002a60fSMarcel Moolenaar 
4545002a60fSMarcel Moolenaar #ifdef DEBUG
4555002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
4565002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
4575002a60fSMarcel Moolenaar #endif
4585002a60fSMarcel Moolenaar 
459206a5d3aSIan Dowse 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
460206a5d3aSIan Dowse 	    td);
4615002a60fSMarcel Moolenaar 	error = namei(&nd);
462206a5d3aSIan Dowse 	LFREEPATH(filename);
4635002a60fSMarcel Moolenaar 	if (error)
4645002a60fSMarcel Moolenaar 		return (error);
4655002a60fSMarcel Moolenaar 	NDFREE(&nd, NDF_ONLY_PNBUF);
4665002a60fSMarcel Moolenaar 
467ea6027a8SRobert Watson 	error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
4685002a60fSMarcel Moolenaar 	vput(nd.ni_vp);
4695002a60fSMarcel Moolenaar 	if (error)
4705002a60fSMarcel Moolenaar 		return (error);
4715002a60fSMarcel Moolenaar 
4725002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
4735002a60fSMarcel Moolenaar }
4745002a60fSMarcel Moolenaar 
4755002a60fSMarcel Moolenaar int
476b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
4775002a60fSMarcel Moolenaar {
4785002a60fSMarcel Moolenaar 	struct filedesc *fdp;
4795002a60fSMarcel Moolenaar 	struct file *fp;
4805002a60fSMarcel Moolenaar 	struct stat buf;
4815002a60fSMarcel Moolenaar 	int error;
4825002a60fSMarcel Moolenaar 
4835002a60fSMarcel Moolenaar #ifdef DEBUG
4845002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
4855002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
4865002a60fSMarcel Moolenaar #endif
4875002a60fSMarcel Moolenaar 
488b40ce416SJulian Elischer 	fdp = td->td_proc->p_fd;
4895002a60fSMarcel Moolenaar 	if ((unsigned)args->fd >= fdp->fd_nfiles ||
4905002a60fSMarcel Moolenaar 	    (fp = fdp->fd_ofiles[args->fd]) == NULL)
4915002a60fSMarcel Moolenaar 		return (EBADF);
4925002a60fSMarcel Moolenaar 
493ea6027a8SRobert Watson 	error = fo_stat(fp, &buf, td->td_ucred, td);
4945002a60fSMarcel Moolenaar 	if (!error)
4955002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
4965002a60fSMarcel Moolenaar 
4975002a60fSMarcel Moolenaar 	return (error);
4985002a60fSMarcel Moolenaar }
4995002a60fSMarcel Moolenaar 
5005002a60fSMarcel Moolenaar #endif /* __i386__ */
501