xref: /freebsd/sys/compat/linux/linux_stats.c (revision 510ea843baf66ae678ca6ddbbbca9816177be5b0)
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 
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>
3985422e62SBruce Evans #include <sys/malloc.h>
40c21dee17SSøren Schmidt #include <sys/mount.h>
41c21dee17SSøren Schmidt #include <sys/namei.h>
42c21dee17SSøren Schmidt #include <sys/stat.h>
43f7a25872SJohn Baldwin #include <sys/syscallsubr.h>
44408da119SMarcel Moolenaar #include <sys/systm.h>
45bc093719SEd Schouten #include <sys/tty.h>
46c21dee17SSøren Schmidt #include <sys/vnode.h>
47060e4882SDoug Ambrisko #include <sys/conf.h>
48060e4882SDoug Ambrisko #include <sys/fcntl.h>
49c21dee17SSøren Schmidt 
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>
5948b05c3fSKonstantin Belousov #include <compat/linux/linux_file.h>
60762e6b85SEivind Eklund 
610eee862aSEd Schouten static void
620eee862aSEd Schouten translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
630eee862aSEd Schouten {
640eee862aSEd Schouten 	int major, minor;
650eee862aSEd Schouten 
660eee862aSEd Schouten 	if (vp->v_type == VCHR && vp->v_rdev != NULL &&
670eee862aSEd Schouten 	    linux_driver_get_major_minor(vp->v_rdev->si_name,
680eee862aSEd Schouten 	    &major, &minor) == 0) {
690eee862aSEd Schouten 		sb->st_rdev = (major << 8 | minor);
700eee862aSEd Schouten 	}
710eee862aSEd Schouten }
720eee862aSEd Schouten 
730eee862aSEd Schouten static int
740eee862aSEd Schouten linux_kern_statat(struct thread *td, int flag, int fd, char *path,
750eee862aSEd Schouten     enum uio_seg pathseg, struct stat *sbp)
760eee862aSEd Schouten {
770eee862aSEd Schouten 
780eee862aSEd Schouten 	return (kern_statat_vnhook(td, flag, fd, path, pathseg, sbp,
790eee862aSEd Schouten 	    translate_vnhook_major_minor));
800eee862aSEd Schouten }
810eee862aSEd Schouten 
820eee862aSEd Schouten static int
830eee862aSEd Schouten linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
840eee862aSEd Schouten     struct stat *sbp)
850eee862aSEd Schouten {
860eee862aSEd Schouten 
870eee862aSEd Schouten 	return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
880eee862aSEd Schouten }
890eee862aSEd Schouten 
900eee862aSEd Schouten static int
910eee862aSEd Schouten linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
920eee862aSEd Schouten     struct stat *sbp)
930eee862aSEd Schouten {
940eee862aSEd Schouten 
950eee862aSEd Schouten 	return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
960eee862aSEd Schouten 	    pathseg, sbp));
970eee862aSEd Schouten }
980eee862aSEd Schouten 
99bbbc2d96SPoul-Henning Kamp /*
100bbbc2d96SPoul-Henning Kamp  * XXX: This was removed from newstat_copyout(), and almost identical
101bbbc2d96SPoul-Henning Kamp  * XXX: code was in stat64_copyout().  findcdev() needs to be replaced
102bbbc2d96SPoul-Henning Kamp  * XXX: with something that does lookup and locking properly.
103bbbc2d96SPoul-Henning Kamp  * XXX: When somebody fixes this: please try to avoid duplicating it.
104bbbc2d96SPoul-Henning Kamp  */
105bbbc2d96SPoul-Henning Kamp #if 0
106bbbc2d96SPoul-Henning Kamp static void
107bbbc2d96SPoul-Henning Kamp disk_foo(struct somestat *tbuf)
108c21dee17SSøren Schmidt {
1094c3a3ec0SJosef Karthauser 	struct cdevsw *cdevsw;
11089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
111c21dee17SSøren Schmidt 
1124c3a3ec0SJosef Karthauser 	/* Lie about disk drives which are character devices
1134c3a3ec0SJosef Karthauser 	 * in FreeBSD but block devices under Linux.
1144c3a3ec0SJosef Karthauser 	 */
1155002a60fSMarcel Moolenaar 	if (S_ISCHR(tbuf.st_mode) &&
116f3732fd1SPoul-Henning Kamp 	    (dev = findcdev(buf->st_rdev)) != NULL) {
117f69f5fbdSPoul-Henning Kamp 		cdevsw = dev_refthread(dev);
118f69f5fbdSPoul-Henning Kamp 		if (cdevsw != NULL) {
119f69f5fbdSPoul-Henning Kamp 			if (cdevsw->d_flags & D_DISK) {
1205002a60fSMarcel Moolenaar 				tbuf.st_mode &= ~S_IFMT;
1215002a60fSMarcel Moolenaar 				tbuf.st_mode |= S_IFBLK;
1227842f151SPaul Richards 
1237842f151SPaul Richards 				/* XXX this may not be quite right */
1247842f151SPaul Richards 				/* Map major number to 0 */
125a4611ab6SEd Schouten 				tbuf.st_dev = minor(buf->st_dev) & 0xf;
1265002a60fSMarcel Moolenaar 				tbuf.st_rdev = buf->st_rdev & 0xff;
1277842f151SPaul Richards 			}
128f69f5fbdSPoul-Henning Kamp 			dev_relthread(dev);
129f69f5fbdSPoul-Henning Kamp 		}
1304c3a3ec0SJosef Karthauser 	}
1314e0eaf69SMarcel Moolenaar 
132bbbc2d96SPoul-Henning Kamp }
133bbbc2d96SPoul-Henning Kamp #endif
134bbbc2d96SPoul-Henning Kamp 
135060e4882SDoug Ambrisko static void
136060e4882SDoug Ambrisko translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
137060e4882SDoug Ambrisko {
138060e4882SDoug Ambrisko 	struct file *fp;
1390eee862aSEd Schouten 	struct vnode *vp;
140060e4882SDoug Ambrisko 	int major, minor;
141060e4882SDoug Ambrisko 
142b256a1e1SJung-uk Kim 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
143b256a1e1SJung-uk Kim 	    fget(td, fd, &fp) != 0)
144060e4882SDoug Ambrisko 		return;
1450eee862aSEd Schouten 	vp = fp->f_vnode;
1460eee862aSEd Schouten 	if (vp != NULL && vp->v_rdev != NULL &&
1470eee862aSEd Schouten 	    linux_driver_get_major_minor(vp->v_rdev->si_name,
148bc093719SEd Schouten 					 &major, &minor) == 0) {
149060e4882SDoug Ambrisko 		buf->st_rdev = (major << 8 | minor);
150bc093719SEd Schouten 	} else if (fp->f_type == DTYPE_PTS) {
151bc093719SEd Schouten 		struct tty *tp = fp->f_data;
152bc093719SEd Schouten 
153bc093719SEd Schouten 		/* Convert the numbers for the slave device. */
154bc093719SEd Schouten 		if (linux_driver_get_major_minor(tp->t_dev->si_name,
155bc093719SEd Schouten 					 &major, &minor) == 0) {
156bc093719SEd Schouten 			buf->st_rdev = (major << 8 | minor);
157bc093719SEd Schouten 		}
158bc093719SEd Schouten 	}
159060e4882SDoug Ambrisko 	fdrop(fp, td);
160060e4882SDoug Ambrisko }
161060e4882SDoug Ambrisko 
162bbbc2d96SPoul-Henning Kamp static int
163bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf)
164bbbc2d96SPoul-Henning Kamp {
165bbbc2d96SPoul-Henning Kamp 	struct l_newstat tbuf;
166bbbc2d96SPoul-Henning Kamp 
167bbbc2d96SPoul-Henning Kamp 	bzero(&tbuf, sizeof(tbuf));
168a4611ab6SEd Schouten 	tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
169bbbc2d96SPoul-Henning Kamp 	tbuf.st_ino = buf->st_ino;
170bbbc2d96SPoul-Henning Kamp 	tbuf.st_mode = buf->st_mode;
171bbbc2d96SPoul-Henning Kamp 	tbuf.st_nlink = buf->st_nlink;
172bbbc2d96SPoul-Henning Kamp 	tbuf.st_uid = buf->st_uid;
173bbbc2d96SPoul-Henning Kamp 	tbuf.st_gid = buf->st_gid;
174bbbc2d96SPoul-Henning Kamp 	tbuf.st_rdev = buf->st_rdev;
175bbbc2d96SPoul-Henning Kamp 	tbuf.st_size = buf->st_size;
176510ea843SEd Schouten 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
177510ea843SEd Schouten 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
178510ea843SEd Schouten 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
179510ea843SEd Schouten 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
180510ea843SEd Schouten 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
181510ea843SEd Schouten 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
182bbbc2d96SPoul-Henning Kamp 	tbuf.st_blksize = buf->st_blksize;
183bbbc2d96SPoul-Henning Kamp 	tbuf.st_blocks = buf->st_blocks;
184bbbc2d96SPoul-Henning Kamp 
1854e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
186c21dee17SSøren Schmidt }
187c21dee17SSøren Schmidt 
188c21dee17SSøren Schmidt int
189b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
190c21dee17SSøren Schmidt {
191c21dee17SSøren Schmidt 	struct stat buf;
192206a5d3aSIan Dowse 	char *path;
193c21dee17SSøren Schmidt 	int error;
194d66a5066SPeter Wemm 
195206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
196c21dee17SSøren Schmidt 
197c21dee17SSøren Schmidt #ifdef DEBUG
19824593369SJonathan Lemon 	if (ldebug(newstat))
199206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
200c21dee17SSøren Schmidt #endif
2014e0eaf69SMarcel Moolenaar 
2020eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
203206a5d3aSIan Dowse 	LFREEPATH(path);
2044e0eaf69SMarcel Moolenaar 	if (error)
2054e0eaf69SMarcel Moolenaar 		return (error);
2064e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
207c21dee17SSøren Schmidt }
208c21dee17SSøren Schmidt 
209c21dee17SSøren Schmidt int
210b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
211c21dee17SSøren Schmidt {
2124e0eaf69SMarcel Moolenaar 	struct stat sb;
213206a5d3aSIan Dowse 	char *path;
214f7a25872SJohn Baldwin 	int error;
215d66a5066SPeter Wemm 
216206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
217c21dee17SSøren Schmidt 
218c21dee17SSøren Schmidt #ifdef DEBUG
21924593369SJonathan Lemon 	if (ldebug(newlstat))
220206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
221c21dee17SSøren Schmidt #endif
2224e0eaf69SMarcel Moolenaar 
2230eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
224206a5d3aSIan Dowse 	LFREEPATH(path);
225d66a5066SPeter Wemm 	if (error)
226d66a5066SPeter Wemm 		return (error);
2275002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
228d66a5066SPeter Wemm }
229c21dee17SSøren Schmidt 
230c21dee17SSøren Schmidt int
231b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
232c21dee17SSøren Schmidt {
233c21dee17SSøren Schmidt 	struct stat buf;
234c21dee17SSøren Schmidt 	int error;
235c21dee17SSøren Schmidt 
236c21dee17SSøren Schmidt #ifdef DEBUG
23724593369SJonathan Lemon 	if (ldebug(newfstat))
23824593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
239c21dee17SSøren Schmidt #endif
2404e0eaf69SMarcel Moolenaar 
241f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
242060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
243c21dee17SSøren Schmidt 	if (!error)
244c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
2454e0eaf69SMarcel Moolenaar 
2464e0eaf69SMarcel Moolenaar 	return (error);
247c21dee17SSøren Schmidt }
248c21dee17SSøren Schmidt 
2495c8919adSAlexander Leidinger static int
2505c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf)
2515c8919adSAlexander Leidinger {
2525c8919adSAlexander Leidinger 	struct l_stat lbuf;
2535c8919adSAlexander Leidinger 
2545c8919adSAlexander Leidinger 	bzero(&lbuf, sizeof(lbuf));
2555c8919adSAlexander Leidinger 	lbuf.st_dev = buf->st_dev;
2565c8919adSAlexander Leidinger 	lbuf.st_ino = buf->st_ino;
2575c8919adSAlexander Leidinger 	lbuf.st_mode = buf->st_mode;
2585c8919adSAlexander Leidinger 	lbuf.st_nlink = buf->st_nlink;
2595c8919adSAlexander Leidinger 	lbuf.st_uid = buf->st_uid;
2605c8919adSAlexander Leidinger 	lbuf.st_gid = buf->st_gid;
2615c8919adSAlexander Leidinger 	lbuf.st_rdev = buf->st_rdev;
2625c8919adSAlexander Leidinger 	if (buf->st_size < (quad_t)1 << 32)
2635c8919adSAlexander Leidinger 		lbuf.st_size = buf->st_size;
2645c8919adSAlexander Leidinger 	else
2655c8919adSAlexander Leidinger 		lbuf.st_size = -2;
266510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
267510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
268510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
269510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
270510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
271510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
2725c8919adSAlexander Leidinger 	lbuf.st_blksize = buf->st_blksize;
2735c8919adSAlexander Leidinger 	lbuf.st_blocks = buf->st_blocks;
2745c8919adSAlexander Leidinger 	lbuf.st_flags = buf->st_flags;
2755c8919adSAlexander Leidinger 	lbuf.st_gen = buf->st_gen;
2765c8919adSAlexander Leidinger 
2775c8919adSAlexander Leidinger 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
2785c8919adSAlexander Leidinger }
2795c8919adSAlexander Leidinger 
2805c8919adSAlexander Leidinger int
2815c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args)
2825c8919adSAlexander Leidinger {
2835c8919adSAlexander Leidinger 	struct stat buf;
28415b78ac5SKonstantin Belousov 	char *path;
2855c8919adSAlexander Leidinger 	int error;
28615b78ac5SKonstantin Belousov 
28715b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
28815b78ac5SKonstantin Belousov 
2895c8919adSAlexander Leidinger #ifdef DEBUG
2905c8919adSAlexander Leidinger 	if (ldebug(stat))
291d075105dSKonstantin Belousov 		printf(ARGS(stat, "%s, *"), path);
2925c8919adSAlexander Leidinger #endif
2930eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
294d075105dSKonstantin Belousov 	if (error) {
29515b78ac5SKonstantin Belousov 		LFREEPATH(path);
2965c8919adSAlexander Leidinger 		return (error);
297d075105dSKonstantin Belousov 	}
298d075105dSKonstantin Belousov 	LFREEPATH(path);
2995c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
3005c8919adSAlexander Leidinger }
3015c8919adSAlexander Leidinger 
3025c8919adSAlexander Leidinger int
3035c8919adSAlexander Leidinger linux_lstat(struct thread *td, struct linux_lstat_args *args)
3045c8919adSAlexander Leidinger {
3055c8919adSAlexander Leidinger 	struct stat buf;
30615b78ac5SKonstantin Belousov 	char *path;
3075c8919adSAlexander Leidinger 	int error;
3085c8919adSAlexander Leidinger 
30915b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
31015b78ac5SKonstantin Belousov 
3115c8919adSAlexander Leidinger #ifdef DEBUG
3125c8919adSAlexander Leidinger 	if (ldebug(lstat))
313d075105dSKonstantin Belousov 		printf(ARGS(lstat, "%s, *"), path);
3145c8919adSAlexander Leidinger #endif
3150eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
316d075105dSKonstantin Belousov 	if (error) {
31715b78ac5SKonstantin Belousov 		LFREEPATH(path);
3185c8919adSAlexander Leidinger 		return (error);
319d075105dSKonstantin Belousov 	}
320d075105dSKonstantin Belousov 	LFREEPATH(path);
3215c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
3225c8919adSAlexander Leidinger }
3235c8919adSAlexander Leidinger 
3245002a60fSMarcel Moolenaar /* XXX - All fields of type l_int are defined as l_long on i386 */
3255002a60fSMarcel Moolenaar struct l_statfs {
3265002a60fSMarcel Moolenaar 	l_int		f_type;
3275002a60fSMarcel Moolenaar 	l_int		f_bsize;
3285002a60fSMarcel Moolenaar 	l_int		f_blocks;
3295002a60fSMarcel Moolenaar 	l_int		f_bfree;
3305002a60fSMarcel Moolenaar 	l_int		f_bavail;
3315002a60fSMarcel Moolenaar 	l_int		f_files;
3325002a60fSMarcel Moolenaar 	l_int		f_ffree;
3335002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
3345002a60fSMarcel Moolenaar 	l_int		f_namelen;
3355002a60fSMarcel Moolenaar 	l_int		f_spare[6];
336c21dee17SSøren Schmidt };
337c21dee17SSøren Schmidt 
338dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
339dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
340dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
341dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
342dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
343dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
344dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
345dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
346dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
347dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
348e83d253bSOlivier Houchard #define LINUX_DEVFS_SUPER_MAGIC	0x1373L
349dca60efcSMarcel Moolenaar 
350dca60efcSMarcel Moolenaar static long
351962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
352dca60efcSMarcel Moolenaar {
353962cf420SMaxim Sobolev 	int i;
354962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
355962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
356962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
357962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
358962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
359962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
360962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
361962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
362962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
363962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
364962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
365e83d253bSOlivier Houchard 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
366962cf420SMaxim Sobolev 		{NULL,      0L}};
367dca60efcSMarcel Moolenaar 
368962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
369962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
370962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
371dca60efcSMarcel Moolenaar 
372dca60efcSMarcel Moolenaar 	return (0L);
373dca60efcSMarcel Moolenaar }
374dca60efcSMarcel Moolenaar 
375f7a25872SJohn Baldwin static void
376d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
377f7a25872SJohn Baldwin {
378f7a25872SJohn Baldwin 
379f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
380f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
381f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
382f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
383f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
384f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
385f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
386f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
387f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
388f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
389f7a25872SJohn Baldwin }
390f7a25872SJohn Baldwin 
391c21dee17SSøren Schmidt int
392b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
393c21dee17SSøren Schmidt {
3945002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
395f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
396206a5d3aSIan Dowse 	char *path;
397c21dee17SSøren Schmidt 	int error;
398d66a5066SPeter Wemm 
399206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
400c21dee17SSøren Schmidt 
401c21dee17SSøren Schmidt #ifdef DEBUG
40224593369SJonathan Lemon 	if (ldebug(statfs))
403206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
404c21dee17SSøren Schmidt #endif
405f7a25872SJohn Baldwin 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
406206a5d3aSIan Dowse 	LFREEPATH(path);
407d5558c00SPeter Wemm 	if (error)
408eddc160eSRobert Watson 		return (error);
409d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
4104b7ef73dSDag-Erling Smørgrav 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
411c21dee17SSøren Schmidt }
412c21dee17SSøren Schmidt 
413835e5061SAlexander Leidinger static void
414835e5061SAlexander Leidinger bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
415835e5061SAlexander Leidinger {
416835e5061SAlexander Leidinger 
417835e5061SAlexander Leidinger 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
418835e5061SAlexander Leidinger 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
419835e5061SAlexander Leidinger 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
420835e5061SAlexander Leidinger 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
421835e5061SAlexander Leidinger 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
422835e5061SAlexander Leidinger 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
423835e5061SAlexander Leidinger 	linux_statfs->f_files = bsd_statfs->f_files;
424835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
425835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
426835e5061SAlexander Leidinger 	linux_statfs->f_namelen = MAXNAMLEN;
427835e5061SAlexander Leidinger }
428835e5061SAlexander Leidinger 
429835e5061SAlexander Leidinger int
430835e5061SAlexander Leidinger linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
431835e5061SAlexander Leidinger {
432835e5061SAlexander Leidinger 	struct l_statfs64 linux_statfs;
433835e5061SAlexander Leidinger 	struct statfs bsd_statfs;
434835e5061SAlexander Leidinger 	char *path;
435835e5061SAlexander Leidinger 	int error;
436835e5061SAlexander Leidinger 
4373ab85269SDavid Malone 	if (args->bufsize != sizeof(struct l_statfs64))
4383ab85269SDavid Malone 		return EINVAL;
4393ab85269SDavid Malone 
440835e5061SAlexander Leidinger 	LCONVPATHEXIST(td, args->path, &path);
441835e5061SAlexander Leidinger 
442835e5061SAlexander Leidinger #ifdef DEBUG
443835e5061SAlexander Leidinger 	if (ldebug(statfs64))
444835e5061SAlexander Leidinger 		printf(ARGS(statfs64, "%s, *"), path);
445835e5061SAlexander Leidinger #endif
446835e5061SAlexander Leidinger 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
447835e5061SAlexander Leidinger 	LFREEPATH(path);
448835e5061SAlexander Leidinger 	if (error)
449835e5061SAlexander Leidinger 		return (error);
450835e5061SAlexander Leidinger 	bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
451835e5061SAlexander Leidinger 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
452835e5061SAlexander Leidinger }
453835e5061SAlexander Leidinger 
454c21dee17SSøren Schmidt int
455b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
456c21dee17SSøren Schmidt {
4575002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
458f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
459c21dee17SSøren Schmidt 	int error;
460c21dee17SSøren Schmidt 
461c21dee17SSøren Schmidt #ifdef DEBUG
46224593369SJonathan Lemon 	if (ldebug(fstatfs))
46324593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
464c21dee17SSøren Schmidt #endif
465f7a25872SJohn Baldwin 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
466d5558c00SPeter Wemm 	if (error)
467c21dee17SSøren Schmidt 		return error;
468d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
469f7a25872SJohn Baldwin 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
470c21dee17SSøren Schmidt }
471408da119SMarcel Moolenaar 
4725002a60fSMarcel Moolenaar struct l_ustat
473408da119SMarcel Moolenaar {
4745002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
4755002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
4765002a60fSMarcel Moolenaar 	char		f_fname[6];
4775002a60fSMarcel Moolenaar 	char		f_fpack[6];
4785002a60fSMarcel Moolenaar };
4795002a60fSMarcel Moolenaar 
4805002a60fSMarcel Moolenaar int
481b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
4825002a60fSMarcel Moolenaar {
4831e247cc2SPoul-Henning Kamp #ifdef DEBUG
4841e247cc2SPoul-Henning Kamp 	if (ldebug(ustat))
4851e247cc2SPoul-Henning Kamp 		printf(ARGS(ustat, "%d, *"), args->dev);
4861e247cc2SPoul-Henning Kamp #endif
4871e247cc2SPoul-Henning Kamp 
4881e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
489408da119SMarcel Moolenaar }
4905002a60fSMarcel Moolenaar 
4911997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
4925002a60fSMarcel Moolenaar 
4935002a60fSMarcel Moolenaar static int
4945002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
4955002a60fSMarcel Moolenaar {
4965002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
4975002a60fSMarcel Moolenaar 
4985002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
499a4611ab6SEd Schouten 	lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
5005002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
5015002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
5025002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
5035002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
5045002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
5055002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
5065002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
507510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
508510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
509510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
510510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
511510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
512510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
5135002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
5145002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
5155002a60fSMarcel Moolenaar 
5165002a60fSMarcel Moolenaar 	/*
5175002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
5185002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
5195002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
5205002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
5215002a60fSMarcel Moolenaar 	 */
5225002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
5235002a60fSMarcel Moolenaar 
5245002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
5255002a60fSMarcel Moolenaar }
5265002a60fSMarcel Moolenaar 
5275002a60fSMarcel Moolenaar int
528b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
5295002a60fSMarcel Moolenaar {
5305002a60fSMarcel Moolenaar 	struct stat buf;
531206a5d3aSIan Dowse 	char *filename;
532f7a25872SJohn Baldwin 	int error;
5335002a60fSMarcel Moolenaar 
534206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5355002a60fSMarcel Moolenaar 
5365002a60fSMarcel Moolenaar #ifdef DEBUG
5375002a60fSMarcel Moolenaar 	if (ldebug(stat64))
538206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
5395002a60fSMarcel Moolenaar #endif
5405002a60fSMarcel Moolenaar 
5410eee862aSEd Schouten 	error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
542206a5d3aSIan Dowse 	LFREEPATH(filename);
5435002a60fSMarcel Moolenaar 	if (error)
5445002a60fSMarcel Moolenaar 		return (error);
5455002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
5465002a60fSMarcel Moolenaar }
5475002a60fSMarcel Moolenaar 
5485002a60fSMarcel Moolenaar int
549b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
5505002a60fSMarcel Moolenaar {
5515002a60fSMarcel Moolenaar 	struct stat sb;
552206a5d3aSIan Dowse 	char *filename;
553f7a25872SJohn Baldwin 	int error;
5545002a60fSMarcel Moolenaar 
555206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5565002a60fSMarcel Moolenaar 
5575002a60fSMarcel Moolenaar #ifdef DEBUG
5585002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
5595002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
5605002a60fSMarcel Moolenaar #endif
5615002a60fSMarcel Moolenaar 
5620eee862aSEd Schouten 	error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
563206a5d3aSIan Dowse 	LFREEPATH(filename);
5645002a60fSMarcel Moolenaar 	if (error)
5655002a60fSMarcel Moolenaar 		return (error);
5665002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
5675002a60fSMarcel Moolenaar }
5685002a60fSMarcel Moolenaar 
5695002a60fSMarcel Moolenaar int
570b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
5715002a60fSMarcel Moolenaar {
5725002a60fSMarcel Moolenaar 	struct stat buf;
5735002a60fSMarcel Moolenaar 	int error;
5745002a60fSMarcel Moolenaar 
5755002a60fSMarcel Moolenaar #ifdef DEBUG
5765002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
5775002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
5785002a60fSMarcel Moolenaar #endif
5795002a60fSMarcel Moolenaar 
580f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
581060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
5825002a60fSMarcel Moolenaar 	if (!error)
5835002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
5845002a60fSMarcel Moolenaar 
5855002a60fSMarcel Moolenaar 	return (error);
5865002a60fSMarcel Moolenaar }
5875002a60fSMarcel Moolenaar 
58848b05c3fSKonstantin Belousov int
58948b05c3fSKonstantin Belousov linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
59048b05c3fSKonstantin Belousov {
59148b05c3fSKonstantin Belousov 	char *path;
59248b05c3fSKonstantin Belousov 	int error, dfd, flag;
59348b05c3fSKonstantin Belousov 	struct stat buf;
59448b05c3fSKonstantin Belousov 
59548b05c3fSKonstantin Belousov 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
59648b05c3fSKonstantin Belousov 		return (EINVAL);
59748b05c3fSKonstantin Belousov 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
59848b05c3fSKonstantin Belousov 	    AT_SYMLINK_NOFOLLOW : 0;
59948b05c3fSKonstantin Belousov 
60048b05c3fSKonstantin Belousov 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
60148b05c3fSKonstantin Belousov 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
60248b05c3fSKonstantin Belousov 
60348b05c3fSKonstantin Belousov #ifdef DEBUG
60448b05c3fSKonstantin Belousov 	if (ldebug(fstatat64))
60548b05c3fSKonstantin Belousov 		printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
60648b05c3fSKonstantin Belousov #endif
60748b05c3fSKonstantin Belousov 
6080eee862aSEd Schouten 	error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
60948b05c3fSKonstantin Belousov 	if (!error)
61048b05c3fSKonstantin Belousov 		error = stat64_copyout(&buf, args->statbuf);
61148b05c3fSKonstantin Belousov 	LFREEPATH(path);
61248b05c3fSKonstantin Belousov 
61348b05c3fSKonstantin Belousov 	return (error);
61448b05c3fSKonstantin Belousov }
61548b05c3fSKonstantin Belousov 
6161997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
617