xref: /freebsd/sys/compat/linux/linux_stats.c (revision 15b78ac5d1bdf0b38498e281fe0a769e37f0384b)
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 
32aefce619SRuslan Ermilov #include "opt_compat.h"
33eddc160eSRobert Watson #include "opt_mac.h"
34eddc160eSRobert Watson 
35c21dee17SSøren Schmidt #include <sys/param.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>
40672d95c5SPawel Jakub Dawidek #include <sys/jail.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>
45f7a25872SJohn Baldwin #include <sys/syscallsubr.h>
46408da119SMarcel Moolenaar #include <sys/systm.h>
47c21dee17SSøren Schmidt #include <sys/vnode.h>
48060e4882SDoug Ambrisko #include <sys/conf.h>
49060e4882SDoug Ambrisko #include <sys/fcntl.h>
50c21dee17SSøren Schmidt 
511997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32
524af27623STim J. Robbins #include <machine/../linux32/linux.h>
534af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
541997c537SDavid E. O'Brien #else
551997c537SDavid E. O'Brien #include <machine/../linux/linux.h>
561997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h>
574af27623STim J. Robbins #endif
5885422e62SBruce Evans 
59ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
60762e6b85SEivind Eklund 
61aed55708SRobert Watson #include <security/mac/mac_framework.h>
62aed55708SRobert Watson 
63bbbc2d96SPoul-Henning Kamp /*
64bbbc2d96SPoul-Henning Kamp  * XXX: This was removed from newstat_copyout(), and almost identical
65bbbc2d96SPoul-Henning Kamp  * XXX: code was in stat64_copyout().  findcdev() needs to be replaced
66bbbc2d96SPoul-Henning Kamp  * XXX: with something that does lookup and locking properly.
67bbbc2d96SPoul-Henning Kamp  * XXX: When somebody fixes this: please try to avoid duplicating it.
68bbbc2d96SPoul-Henning Kamp  */
69bbbc2d96SPoul-Henning Kamp #if 0
70bbbc2d96SPoul-Henning Kamp static void
71bbbc2d96SPoul-Henning Kamp disk_foo(struct somestat *tbuf)
72c21dee17SSøren Schmidt {
734c3a3ec0SJosef Karthauser 	struct cdevsw *cdevsw;
7489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
75c21dee17SSøren Schmidt 
764c3a3ec0SJosef Karthauser 	/* Lie about disk drives which are character devices
774c3a3ec0SJosef Karthauser 	 * in FreeBSD but block devices under Linux.
784c3a3ec0SJosef Karthauser 	 */
795002a60fSMarcel Moolenaar 	if (S_ISCHR(tbuf.st_mode) &&
80f3732fd1SPoul-Henning Kamp 	    (dev = findcdev(buf->st_rdev)) != NULL) {
81f69f5fbdSPoul-Henning Kamp 		cdevsw = dev_refthread(dev);
82f69f5fbdSPoul-Henning Kamp 		if (cdevsw != NULL) {
83f69f5fbdSPoul-Henning Kamp 			if (cdevsw->d_flags & D_DISK) {
845002a60fSMarcel Moolenaar 				tbuf.st_mode &= ~S_IFMT;
855002a60fSMarcel Moolenaar 				tbuf.st_mode |= S_IFBLK;
867842f151SPaul Richards 
877842f151SPaul Richards 				/* XXX this may not be quite right */
887842f151SPaul Richards 				/* Map major number to 0 */
895002a60fSMarcel Moolenaar 				tbuf.st_dev = uminor(buf->st_dev) & 0xf;
905002a60fSMarcel Moolenaar 				tbuf.st_rdev = buf->st_rdev & 0xff;
917842f151SPaul Richards 			}
92f69f5fbdSPoul-Henning Kamp 			dev_relthread(dev);
93f69f5fbdSPoul-Henning Kamp 		}
944c3a3ec0SJosef Karthauser 	}
954e0eaf69SMarcel Moolenaar 
96bbbc2d96SPoul-Henning Kamp }
97bbbc2d96SPoul-Henning Kamp #endif
98bbbc2d96SPoul-Henning Kamp 
99060e4882SDoug Ambrisko static void
100060e4882SDoug Ambrisko translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
101060e4882SDoug Ambrisko {
102060e4882SDoug Ambrisko 	struct file *fp;
103060e4882SDoug Ambrisko 	int major, minor;
104060e4882SDoug Ambrisko 
105b256a1e1SJung-uk Kim 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
106b256a1e1SJung-uk Kim 	    fget(td, fd, &fp) != 0)
107060e4882SDoug Ambrisko 		return;
108b256a1e1SJung-uk Kim 	if (fp->f_vnode != NULL &&
109b256a1e1SJung-uk Kim 	    fp->f_vnode->v_un.vu_cdev != NULL &&
110b256a1e1SJung-uk Kim 	    linux_driver_get_major_minor(fp->f_vnode->v_un.vu_cdev->si_name,
111b256a1e1SJung-uk Kim 					 &major, &minor) == 0)
112060e4882SDoug Ambrisko 		buf->st_rdev = (major << 8 | minor);
113060e4882SDoug Ambrisko 	fdrop(fp, td);
114060e4882SDoug Ambrisko }
115060e4882SDoug Ambrisko 
116060e4882SDoug Ambrisko static void
117060e4882SDoug Ambrisko translate_path_major_minor(struct thread *td, char *path, struct stat *buf)
118060e4882SDoug Ambrisko {
119edb75ecaSDoug Ambrisko 	struct proc *p = td->td_proc;
120edb75ecaSDoug Ambrisko 	struct filedesc *fdp = p->p_fd;
121060e4882SDoug Ambrisko 	int fd;
122060e4882SDoug Ambrisko 	int temp;
123060e4882SDoug Ambrisko 
124b256a1e1SJung-uk Kim 	if (!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode))
125b256a1e1SJung-uk Kim 		return;
126060e4882SDoug Ambrisko 	temp = td->td_retval[0];
127060e4882SDoug Ambrisko 	if (kern_open(td, path, UIO_SYSSPACE, O_RDONLY, 0) != 0)
128060e4882SDoug Ambrisko 		return;
129060e4882SDoug Ambrisko 	fd = td->td_retval[0];
130060e4882SDoug Ambrisko 	td->td_retval[0] = temp;
131060e4882SDoug Ambrisko 	translate_fd_major_minor(td, fd, buf);
132edb75ecaSDoug Ambrisko 	fdclose(fdp, fdp->fd_ofiles[fd], fd, td);
133060e4882SDoug Ambrisko }
134060e4882SDoug Ambrisko 
135bbbc2d96SPoul-Henning Kamp static int
136bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf)
137bbbc2d96SPoul-Henning Kamp {
138bbbc2d96SPoul-Henning Kamp 	struct l_newstat tbuf;
139bbbc2d96SPoul-Henning Kamp 
140bbbc2d96SPoul-Henning Kamp 	bzero(&tbuf, sizeof(tbuf));
141bbbc2d96SPoul-Henning Kamp 	tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
142bbbc2d96SPoul-Henning Kamp 	tbuf.st_ino = buf->st_ino;
143bbbc2d96SPoul-Henning Kamp 	tbuf.st_mode = buf->st_mode;
144bbbc2d96SPoul-Henning Kamp 	tbuf.st_nlink = buf->st_nlink;
145bbbc2d96SPoul-Henning Kamp 	tbuf.st_uid = buf->st_uid;
146bbbc2d96SPoul-Henning Kamp 	tbuf.st_gid = buf->st_gid;
147bbbc2d96SPoul-Henning Kamp 	tbuf.st_rdev = buf->st_rdev;
148bbbc2d96SPoul-Henning Kamp 	tbuf.st_size = buf->st_size;
149bbbc2d96SPoul-Henning Kamp 	tbuf.st_atime = buf->st_atime;
150bbbc2d96SPoul-Henning Kamp 	tbuf.st_mtime = buf->st_mtime;
151bbbc2d96SPoul-Henning Kamp 	tbuf.st_ctime = buf->st_ctime;
152bbbc2d96SPoul-Henning Kamp 	tbuf.st_blksize = buf->st_blksize;
153bbbc2d96SPoul-Henning Kamp 	tbuf.st_blocks = buf->st_blocks;
154bbbc2d96SPoul-Henning Kamp 
1554e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
156c21dee17SSøren Schmidt }
157c21dee17SSøren Schmidt 
158c21dee17SSøren Schmidt int
159b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
160c21dee17SSøren Schmidt {
161c21dee17SSøren Schmidt 	struct stat buf;
162206a5d3aSIan Dowse 	char *path;
163c21dee17SSøren Schmidt 	int error;
164d66a5066SPeter Wemm 
165206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
166c21dee17SSøren Schmidt 
167c21dee17SSøren Schmidt #ifdef DEBUG
16824593369SJonathan Lemon 	if (ldebug(newstat))
169206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
170c21dee17SSøren Schmidt #endif
1714e0eaf69SMarcel Moolenaar 
172f7a25872SJohn Baldwin 	error = kern_stat(td, path, UIO_SYSSPACE, &buf);
173b256a1e1SJung-uk Kim 	if (!error) {
174b256a1e1SJung-uk Kim 		if (strlen(path) > strlen("/dev/pts/") &&
175b256a1e1SJung-uk Kim 		    !strncmp(path, "/dev/pts/", strlen("/dev/pts/")) &&
176b256a1e1SJung-uk Kim 		    path[9] >= '0' && path[9] <= '9') {
177e83d253bSOlivier Houchard 			/*
178b256a1e1SJung-uk Kim 			 * Linux checks major and minors of the slave device
179b256a1e1SJung-uk Kim 			 * to make sure it's a pty device, so let's make him
180b256a1e1SJung-uk Kim 			 * believe it is.
181e83d253bSOlivier Houchard 			 */
182e83d253bSOlivier Houchard 			buf.st_rdev = (136 << 8);
183b256a1e1SJung-uk Kim 		} else
184060e4882SDoug Ambrisko 			translate_path_major_minor(td, path, &buf);
185b256a1e1SJung-uk Kim 	}
186206a5d3aSIan Dowse 	LFREEPATH(path);
1874e0eaf69SMarcel Moolenaar 	if (error)
1884e0eaf69SMarcel Moolenaar 		return (error);
1894e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
190c21dee17SSøren Schmidt }
191c21dee17SSøren Schmidt 
192c21dee17SSøren Schmidt int
193b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
194c21dee17SSøren Schmidt {
1954e0eaf69SMarcel Moolenaar 	struct stat sb;
196206a5d3aSIan Dowse 	char *path;
197f7a25872SJohn Baldwin 	int error;
198d66a5066SPeter Wemm 
199206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
200c21dee17SSøren Schmidt 
201c21dee17SSøren Schmidt #ifdef DEBUG
20224593369SJonathan Lemon 	if (ldebug(newlstat))
203206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
204c21dee17SSøren Schmidt #endif
2054e0eaf69SMarcel Moolenaar 
206f7a25872SJohn Baldwin 	error = kern_lstat(td, path, UIO_SYSSPACE, &sb);
207b256a1e1SJung-uk Kim 	if (!error)
208060e4882SDoug Ambrisko 		translate_path_major_minor(td, path, &sb);
209206a5d3aSIan Dowse 	LFREEPATH(path);
210d66a5066SPeter Wemm 	if (error)
211d66a5066SPeter Wemm 		return (error);
2125002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
213d66a5066SPeter Wemm }
214c21dee17SSøren Schmidt 
215c21dee17SSøren Schmidt int
216b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
217c21dee17SSøren Schmidt {
218c21dee17SSøren Schmidt 	struct stat buf;
219c21dee17SSøren Schmidt 	int error;
220c21dee17SSøren Schmidt 
221c21dee17SSøren Schmidt #ifdef DEBUG
22224593369SJonathan Lemon 	if (ldebug(newfstat))
22324593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
224c21dee17SSøren Schmidt #endif
2254e0eaf69SMarcel Moolenaar 
226f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
227060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
228c21dee17SSøren Schmidt 	if (!error)
229c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
2304e0eaf69SMarcel Moolenaar 
2314e0eaf69SMarcel Moolenaar 	return (error);
232c21dee17SSøren Schmidt }
233c21dee17SSøren Schmidt 
2345c8919adSAlexander Leidinger static int
2355c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf)
2365c8919adSAlexander Leidinger {
2375c8919adSAlexander Leidinger 	struct l_stat lbuf;
2385c8919adSAlexander Leidinger 
2395c8919adSAlexander Leidinger 	bzero(&lbuf, sizeof(lbuf));
2405c8919adSAlexander Leidinger 	lbuf.st_dev = buf->st_dev;
2415c8919adSAlexander Leidinger 	lbuf.st_ino = buf->st_ino;
2425c8919adSAlexander Leidinger 	lbuf.st_mode = buf->st_mode;
2435c8919adSAlexander Leidinger 	lbuf.st_nlink = buf->st_nlink;
2445c8919adSAlexander Leidinger 	lbuf.st_uid = buf->st_uid;
2455c8919adSAlexander Leidinger 	lbuf.st_gid = buf->st_gid;
2465c8919adSAlexander Leidinger 	lbuf.st_rdev = buf->st_rdev;
2475c8919adSAlexander Leidinger 	if (buf->st_size < (quad_t)1 << 32)
2485c8919adSAlexander Leidinger 		lbuf.st_size = buf->st_size;
2495c8919adSAlexander Leidinger 	else
2505c8919adSAlexander Leidinger 		lbuf.st_size = -2;
2515c8919adSAlexander Leidinger 	lbuf.st_atime = buf->st_atime;
2525c8919adSAlexander Leidinger 	lbuf.st_mtime = buf->st_mtime;
2535c8919adSAlexander Leidinger 	lbuf.st_ctime = buf->st_ctime;
2545c8919adSAlexander Leidinger 	lbuf.st_blksize = buf->st_blksize;
2555c8919adSAlexander Leidinger 	lbuf.st_blocks = buf->st_blocks;
2565c8919adSAlexander Leidinger 	lbuf.st_flags = buf->st_flags;
2575c8919adSAlexander Leidinger 	lbuf.st_gen = buf->st_gen;
2585c8919adSAlexander Leidinger 
2595c8919adSAlexander Leidinger 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
2605c8919adSAlexander Leidinger }
2615c8919adSAlexander Leidinger 
2625c8919adSAlexander Leidinger int
2635c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args)
2645c8919adSAlexander Leidinger {
2655c8919adSAlexander Leidinger 	struct stat buf;
26615b78ac5SKonstantin Belousov 	char *path;
2675c8919adSAlexander Leidinger 	int error;
26815b78ac5SKonstantin Belousov 
26915b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
27015b78ac5SKonstantin Belousov 
2715c8919adSAlexander Leidinger #ifdef DEBUG
2725c8919adSAlexander Leidinger 	if (ldebug(stat))
2735c8919adSAlexander Leidinger 		printf(ARGS(stat, "%s, *"), args->path);
2745c8919adSAlexander Leidinger #endif
27515b78ac5SKonstantin Belousov 	error = kern_stat(td, path, UIO_SYSSPACE, &buf);
27615b78ac5SKonstantin Belousov 	LFREEPATH(path);
2775c8919adSAlexander Leidinger 	if (error)
2785c8919adSAlexander Leidinger 		return (error);
279060e4882SDoug Ambrisko 	translate_path_major_minor(td, args->path, &buf);
2805c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
2815c8919adSAlexander Leidinger }
2825c8919adSAlexander Leidinger 
2835c8919adSAlexander Leidinger int
2845c8919adSAlexander Leidinger linux_lstat(struct thread *td, struct linux_lstat_args *args)
2855c8919adSAlexander Leidinger {
2865c8919adSAlexander Leidinger 	struct stat buf;
28715b78ac5SKonstantin Belousov 	char *path;
2885c8919adSAlexander Leidinger 	int error;
2895c8919adSAlexander Leidinger 
29015b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
29115b78ac5SKonstantin Belousov 
2925c8919adSAlexander Leidinger #ifdef DEBUG
2935c8919adSAlexander Leidinger 	if (ldebug(lstat))
2945c8919adSAlexander Leidinger 		printf(ARGS(lstat, "%s, *"), args->path);
2955c8919adSAlexander Leidinger #endif
2965c8919adSAlexander Leidinger 	error = kern_lstat(td, args->path, UIO_SYSSPACE, &buf);
29715b78ac5SKonstantin Belousov 	LFREEPATH(path);
2985c8919adSAlexander Leidinger 	if (error)
2995c8919adSAlexander Leidinger 		return (error);
300060e4882SDoug Ambrisko 	translate_path_major_minor(td, args->path, &buf);
3015c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
3025c8919adSAlexander Leidinger }
3035c8919adSAlexander Leidinger 
3045002a60fSMarcel Moolenaar /* XXX - All fields of type l_int are defined as l_long on i386 */
3055002a60fSMarcel Moolenaar struct l_statfs {
3065002a60fSMarcel Moolenaar 	l_int		f_type;
3075002a60fSMarcel Moolenaar 	l_int		f_bsize;
3085002a60fSMarcel Moolenaar 	l_int		f_blocks;
3095002a60fSMarcel Moolenaar 	l_int		f_bfree;
3105002a60fSMarcel Moolenaar 	l_int		f_bavail;
3115002a60fSMarcel Moolenaar 	l_int		f_files;
3125002a60fSMarcel Moolenaar 	l_int		f_ffree;
3135002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
3145002a60fSMarcel Moolenaar 	l_int		f_namelen;
3155002a60fSMarcel Moolenaar 	l_int		f_spare[6];
316c21dee17SSøren Schmidt };
317c21dee17SSøren Schmidt 
318dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
319dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
320dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
321dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
322dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
323dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
324dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
325dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
326dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
327dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
328e83d253bSOlivier Houchard #define LINUX_DEVFS_SUPER_MAGIC	0x1373L
329dca60efcSMarcel Moolenaar 
330dca60efcSMarcel Moolenaar static long
331962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
332dca60efcSMarcel Moolenaar {
333962cf420SMaxim Sobolev 	int i;
334962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
335962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
336962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
337962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
338962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
339962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
340962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
341962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
342962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
343962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
344962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
345e83d253bSOlivier Houchard 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
346962cf420SMaxim Sobolev 		{NULL,      0L}};
347dca60efcSMarcel Moolenaar 
348962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
349962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
350962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
351dca60efcSMarcel Moolenaar 
352dca60efcSMarcel Moolenaar 	return (0L);
353dca60efcSMarcel Moolenaar }
354dca60efcSMarcel Moolenaar 
355f7a25872SJohn Baldwin static void
356d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
357f7a25872SJohn Baldwin {
358f7a25872SJohn Baldwin 
359f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
360f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
361f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
362f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
363f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
364f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
365f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
366f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
367f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
368f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
369f7a25872SJohn Baldwin }
370f7a25872SJohn Baldwin 
371c21dee17SSøren Schmidt int
372b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
373c21dee17SSøren Schmidt {
3745002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
375f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
376206a5d3aSIan Dowse 	char *path;
377c21dee17SSøren Schmidt 	int error;
378d66a5066SPeter Wemm 
379206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
380c21dee17SSøren Schmidt 
381c21dee17SSøren Schmidt #ifdef DEBUG
38224593369SJonathan Lemon 	if (ldebug(statfs))
383206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
384c21dee17SSøren Schmidt #endif
385f7a25872SJohn Baldwin 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
386206a5d3aSIan Dowse 	LFREEPATH(path);
387d5558c00SPeter Wemm 	if (error)
388eddc160eSRobert Watson 		return (error);
389d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
3904b7ef73dSDag-Erling Smørgrav 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
391c21dee17SSøren Schmidt }
392c21dee17SSøren Schmidt 
393835e5061SAlexander Leidinger static void
394835e5061SAlexander Leidinger bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
395835e5061SAlexander Leidinger {
396835e5061SAlexander Leidinger 
397835e5061SAlexander Leidinger 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
398835e5061SAlexander Leidinger 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
399835e5061SAlexander Leidinger 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
400835e5061SAlexander Leidinger 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
401835e5061SAlexander Leidinger 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
402835e5061SAlexander Leidinger 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
403835e5061SAlexander Leidinger 	linux_statfs->f_files = bsd_statfs->f_files;
404835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
405835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
406835e5061SAlexander Leidinger 	linux_statfs->f_namelen = MAXNAMLEN;
407835e5061SAlexander Leidinger }
408835e5061SAlexander Leidinger 
409835e5061SAlexander Leidinger int
410835e5061SAlexander Leidinger linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
411835e5061SAlexander Leidinger {
412835e5061SAlexander Leidinger 	struct l_statfs64 linux_statfs;
413835e5061SAlexander Leidinger 	struct statfs bsd_statfs;
414835e5061SAlexander Leidinger 	char *path;
415835e5061SAlexander Leidinger 	int error;
416835e5061SAlexander Leidinger 
4173ab85269SDavid Malone 	if (args->bufsize != sizeof(struct l_statfs64))
4183ab85269SDavid Malone 		return EINVAL;
4193ab85269SDavid Malone 
420835e5061SAlexander Leidinger 	LCONVPATHEXIST(td, args->path, &path);
421835e5061SAlexander Leidinger 
422835e5061SAlexander Leidinger #ifdef DEBUG
423835e5061SAlexander Leidinger 	if (ldebug(statfs64))
424835e5061SAlexander Leidinger 		printf(ARGS(statfs64, "%s, *"), path);
425835e5061SAlexander Leidinger #endif
426835e5061SAlexander Leidinger 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
427835e5061SAlexander Leidinger 	LFREEPATH(path);
428835e5061SAlexander Leidinger 	if (error)
429835e5061SAlexander Leidinger 		return (error);
430835e5061SAlexander Leidinger 	bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
431835e5061SAlexander Leidinger 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
432835e5061SAlexander Leidinger }
433835e5061SAlexander Leidinger 
434c21dee17SSøren Schmidt int
435b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
436c21dee17SSøren Schmidt {
4375002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
438f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
439c21dee17SSøren Schmidt 	int error;
440c21dee17SSøren Schmidt 
441c21dee17SSøren Schmidt #ifdef DEBUG
44224593369SJonathan Lemon 	if (ldebug(fstatfs))
44324593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
444c21dee17SSøren Schmidt #endif
445f7a25872SJohn Baldwin 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
446d5558c00SPeter Wemm 	if (error)
447c21dee17SSøren Schmidt 		return error;
448d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
449f7a25872SJohn Baldwin 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
450c21dee17SSøren Schmidt }
451408da119SMarcel Moolenaar 
4525002a60fSMarcel Moolenaar struct l_ustat
453408da119SMarcel Moolenaar {
4545002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
4555002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
4565002a60fSMarcel Moolenaar 	char		f_fname[6];
4575002a60fSMarcel Moolenaar 	char		f_fpack[6];
4585002a60fSMarcel Moolenaar };
4595002a60fSMarcel Moolenaar 
4605002a60fSMarcel Moolenaar int
461b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
4625002a60fSMarcel Moolenaar {
4631e247cc2SPoul-Henning Kamp #ifdef DEBUG
4641e247cc2SPoul-Henning Kamp 	if (ldebug(ustat))
4651e247cc2SPoul-Henning Kamp 		printf(ARGS(ustat, "%d, *"), args->dev);
4661e247cc2SPoul-Henning Kamp #endif
4671e247cc2SPoul-Henning Kamp 
4681e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
469408da119SMarcel Moolenaar }
4705002a60fSMarcel Moolenaar 
4711997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
4725002a60fSMarcel Moolenaar 
4735002a60fSMarcel Moolenaar static int
4745002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
4755002a60fSMarcel Moolenaar {
4765002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
4775002a60fSMarcel Moolenaar 
4785002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
4795002a60fSMarcel Moolenaar 	lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
4805002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
4815002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
4825002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
4835002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
4845002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
4855002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
4865002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
4875002a60fSMarcel Moolenaar 	lbuf.st_atime = buf->st_atime;
4885002a60fSMarcel Moolenaar 	lbuf.st_mtime = buf->st_mtime;
4895002a60fSMarcel Moolenaar 	lbuf.st_ctime = buf->st_ctime;
4905002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
4915002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
4925002a60fSMarcel Moolenaar 
4935002a60fSMarcel Moolenaar 	/*
4945002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
4955002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
4965002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
4975002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
4985002a60fSMarcel Moolenaar 	 */
4995002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
5005002a60fSMarcel Moolenaar 
5015002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
5025002a60fSMarcel Moolenaar }
5035002a60fSMarcel Moolenaar 
5045002a60fSMarcel Moolenaar int
505b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
5065002a60fSMarcel Moolenaar {
5075002a60fSMarcel Moolenaar 	struct stat buf;
508206a5d3aSIan Dowse 	char *filename;
509f7a25872SJohn Baldwin 	int error;
5105002a60fSMarcel Moolenaar 
511206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5125002a60fSMarcel Moolenaar 
5135002a60fSMarcel Moolenaar #ifdef DEBUG
5145002a60fSMarcel Moolenaar 	if (ldebug(stat64))
515206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
5165002a60fSMarcel Moolenaar #endif
5175002a60fSMarcel Moolenaar 
518f7a25872SJohn Baldwin 	error = kern_stat(td, filename, UIO_SYSSPACE, &buf);
519b256a1e1SJung-uk Kim 	if (!error) {
520b256a1e1SJung-uk Kim 		if (strlen(filename) > strlen("/dev/pts/") &&
521b256a1e1SJung-uk Kim 		    !strncmp(filename, "/dev/pts/", strlen("/dev/pts/")) &&
522b256a1e1SJung-uk Kim 		    filename[9] >= '0' && filename[9] <= '9') {
523e83d253bSOlivier Houchard 			/*
524b256a1e1SJung-uk Kim 			 * Linux checks major and minors of the slave device
525b256a1e1SJung-uk Kim 			 * to make sure it's a pty deivce, so let's make him
526b256a1e1SJung-uk Kim 			 * believe it is.
527e83d253bSOlivier Houchard 			 */
528e83d253bSOlivier Houchard 			buf.st_rdev = (136 << 8);
529b256a1e1SJung-uk Kim 		} else
530060e4882SDoug Ambrisko 			translate_path_major_minor(td, filename, &buf);
531b256a1e1SJung-uk Kim 	}
532206a5d3aSIan Dowse 	LFREEPATH(filename);
5335002a60fSMarcel Moolenaar 	if (error)
5345002a60fSMarcel Moolenaar 		return (error);
5355002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
5365002a60fSMarcel Moolenaar }
5375002a60fSMarcel Moolenaar 
5385002a60fSMarcel Moolenaar int
539b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
5405002a60fSMarcel Moolenaar {
5415002a60fSMarcel Moolenaar 	struct stat sb;
542206a5d3aSIan Dowse 	char *filename;
543f7a25872SJohn Baldwin 	int error;
5445002a60fSMarcel Moolenaar 
545206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5465002a60fSMarcel Moolenaar 
5475002a60fSMarcel Moolenaar #ifdef DEBUG
5485002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
5495002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
5505002a60fSMarcel Moolenaar #endif
5515002a60fSMarcel Moolenaar 
552f7a25872SJohn Baldwin 	error = kern_lstat(td, filename, UIO_SYSSPACE, &sb);
553b256a1e1SJung-uk Kim 	if (!error)
554060e4882SDoug Ambrisko 		translate_path_major_minor(td, filename, &sb);
555206a5d3aSIan Dowse 	LFREEPATH(filename);
5565002a60fSMarcel Moolenaar 	if (error)
5575002a60fSMarcel Moolenaar 		return (error);
5585002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
5595002a60fSMarcel Moolenaar }
5605002a60fSMarcel Moolenaar 
5615002a60fSMarcel Moolenaar int
562b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
5635002a60fSMarcel Moolenaar {
5645002a60fSMarcel Moolenaar 	struct stat buf;
5655002a60fSMarcel Moolenaar 	int error;
5665002a60fSMarcel Moolenaar 
5675002a60fSMarcel Moolenaar #ifdef DEBUG
5685002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
5695002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
5705002a60fSMarcel Moolenaar #endif
5715002a60fSMarcel Moolenaar 
572f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
573060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
5745002a60fSMarcel Moolenaar 	if (!error)
5755002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
5765002a60fSMarcel Moolenaar 
5775002a60fSMarcel Moolenaar 	return (error);
5785002a60fSMarcel Moolenaar }
5795002a60fSMarcel Moolenaar 
5801997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
581