xref: /freebsd/sys/compat/linux/linux_stats.c (revision 931e2a1a6e0d8ac7d99f48eb7fc744e3a40284f0)
1c21dee17SSøren Schmidt /*-
20ba1b365SEd Maste  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
37f2d13d6SPedro F. Giffuni  *
49a14aa01SUlrich Spörlein  * Copyright (c) 1994-1995 Søren Schmidt
5c21dee17SSøren Schmidt  * All rights reserved.
6c21dee17SSøren Schmidt  *
7c21dee17SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
8c21dee17SSøren Schmidt  * modification, are permitted provided that the following conditions
9c21dee17SSøren Schmidt  * are met:
10c21dee17SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
110ba1b365SEd Maste  *    notice, this list of conditions and the following disclaimer.
12c21dee17SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
13c21dee17SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
14c21dee17SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
15c21dee17SSøren Schmidt  *
160ba1b365SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170ba1b365SEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180ba1b365SEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190ba1b365SEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200ba1b365SEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210ba1b365SEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220ba1b365SEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230ba1b365SEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240ba1b365SEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250ba1b365SEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260ba1b365SEd Maste  * 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>
359802eb9eSDmitry Chagin #include <sys/capsicum.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>
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>
46bc093719SEd Schouten #include <sys/tty.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>
6048b05c3fSKonstantin Belousov #include <compat/linux/linux_file.h>
61762e6b85SEivind Eklund 
62529844c7SAlexander Leidinger 
630eee862aSEd Schouten static void
640eee862aSEd Schouten translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
650eee862aSEd Schouten {
660eee862aSEd Schouten 	int major, minor;
670eee862aSEd Schouten 
680eee862aSEd Schouten 	if (vp->v_type == VCHR && vp->v_rdev != NULL &&
697870adb6SEd Schouten 	    linux_driver_get_major_minor(devtoname(vp->v_rdev),
700eee862aSEd Schouten 	    &major, &minor) == 0) {
710eee862aSEd Schouten 		sb->st_rdev = (major << 8 | minor);
720eee862aSEd Schouten 	}
730eee862aSEd Schouten }
740eee862aSEd Schouten 
750eee862aSEd Schouten static int
760eee862aSEd Schouten linux_kern_statat(struct thread *td, int flag, int fd, char *path,
770eee862aSEd Schouten     enum uio_seg pathseg, struct stat *sbp)
780eee862aSEd Schouten {
790eee862aSEd Schouten 
806e646651SKonstantin Belousov 	return (kern_statat(td, flag, fd, path, pathseg, sbp,
810eee862aSEd Schouten 	    translate_vnhook_major_minor));
820eee862aSEd Schouten }
830eee862aSEd Schouten 
84*931e2a1aSEd Maste #ifdef LINUX_LEGACY_SYSCALLS
850eee862aSEd Schouten static int
860eee862aSEd Schouten linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
870eee862aSEd Schouten     struct stat *sbp)
880eee862aSEd Schouten {
890eee862aSEd Schouten 
900eee862aSEd Schouten 	return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
910eee862aSEd Schouten }
920eee862aSEd Schouten 
930eee862aSEd Schouten static int
940eee862aSEd Schouten linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
950eee862aSEd Schouten     struct stat *sbp)
960eee862aSEd Schouten {
970eee862aSEd Schouten 
980eee862aSEd Schouten 	return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
990eee862aSEd Schouten 	    pathseg, sbp));
1000eee862aSEd Schouten }
101*931e2a1aSEd Maste #endif
1020eee862aSEd Schouten 
103060e4882SDoug Ambrisko static void
104060e4882SDoug Ambrisko translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
105060e4882SDoug Ambrisko {
106060e4882SDoug Ambrisko 	struct file *fp;
1070eee862aSEd Schouten 	struct vnode *vp;
108060e4882SDoug Ambrisko 	int major, minor;
109060e4882SDoug Ambrisko 
110a9d2f8d8SRobert Watson 	/*
111a9d2f8d8SRobert Watson 	 * No capability rights required here.
112a9d2f8d8SRobert Watson 	 */
113b256a1e1SJung-uk Kim 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
114cbd92ce6SMatt Macy 	    fget(td, fd, &cap_no_rights, &fp) != 0)
115060e4882SDoug Ambrisko 		return;
1160eee862aSEd Schouten 	vp = fp->f_vnode;
1170eee862aSEd Schouten 	if (vp != NULL && vp->v_rdev != NULL &&
1187870adb6SEd Schouten 	    linux_driver_get_major_minor(devtoname(vp->v_rdev),
119bc093719SEd Schouten 					 &major, &minor) == 0) {
120060e4882SDoug Ambrisko 		buf->st_rdev = (major << 8 | minor);
121bc093719SEd Schouten 	} else if (fp->f_type == DTYPE_PTS) {
122bc093719SEd Schouten 		struct tty *tp = fp->f_data;
123bc093719SEd Schouten 
124bc093719SEd Schouten 		/* Convert the numbers for the slave device. */
1257870adb6SEd Schouten 		if (linux_driver_get_major_minor(devtoname(tp->t_dev),
126bc093719SEd Schouten 					 &major, &minor) == 0) {
127bc093719SEd Schouten 			buf->st_rdev = (major << 8 | minor);
128bc093719SEd Schouten 		}
129bc093719SEd Schouten 	}
130060e4882SDoug Ambrisko 	fdrop(fp, td);
131060e4882SDoug Ambrisko }
132060e4882SDoug Ambrisko 
133ab35e1c7SBruce Evans /*
134ab35e1c7SBruce Evans  * l_dev_t has the same encoding as dev_t in the latter's low 16 bits, so
135407a8126SBruce Evans  * truncation of a dev_t to 16 bits gives the same result as unpacking
136407a8126SBruce Evans  * using major() and minor() and repacking in the l_dev_t format.  This
137407a8126SBruce Evans  * detail is hidden in dev_to_ldev().  Overflow in conversions of dev_t's
138407a8126SBruce Evans  * are not checked for, as for other fields.
139ab35e1c7SBruce Evans  *
140407a8126SBruce Evans  * dev_to_ldev() is only used for translating st_dev.  When we convert
141407a8126SBruce Evans  * st_rdev for copying it out, it isn't really a dev_t, but has already
142407a8126SBruce Evans  * been translated to an l_dev_t in a nontrivial way.  Translating it
143407a8126SBruce Evans  * again would be illogical but would have no effect since the low 16
144407a8126SBruce Evans  * bits have the same encoding.
145407a8126SBruce Evans  *
146407a8126SBruce Evans  * The nontrivial translation for st_rdev renumbers some devices, but not
147407a8126SBruce Evans  * ones that can be mounted on, so it is consistent with the translation
148407a8126SBruce Evans  * for st_dev except when the renumbering or truncation causes conflicts.
149ab35e1c7SBruce Evans  */
150ab35e1c7SBruce Evans #define	dev_to_ldev(d)	((uint16_t)(d))
151ab35e1c7SBruce Evans 
152bbbc2d96SPoul-Henning Kamp static int
153bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf)
154bbbc2d96SPoul-Henning Kamp {
155bbbc2d96SPoul-Henning Kamp 	struct l_newstat tbuf;
156bbbc2d96SPoul-Henning Kamp 
157bbbc2d96SPoul-Henning Kamp 	bzero(&tbuf, sizeof(tbuf));
158ab35e1c7SBruce Evans 	tbuf.st_dev = dev_to_ldev(buf->st_dev);
159bbbc2d96SPoul-Henning Kamp 	tbuf.st_ino = buf->st_ino;
160bbbc2d96SPoul-Henning Kamp 	tbuf.st_mode = buf->st_mode;
161bbbc2d96SPoul-Henning Kamp 	tbuf.st_nlink = buf->st_nlink;
162bbbc2d96SPoul-Henning Kamp 	tbuf.st_uid = buf->st_uid;
163bbbc2d96SPoul-Henning Kamp 	tbuf.st_gid = buf->st_gid;
164bbbc2d96SPoul-Henning Kamp 	tbuf.st_rdev = buf->st_rdev;
165bbbc2d96SPoul-Henning Kamp 	tbuf.st_size = buf->st_size;
166510ea843SEd Schouten 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
167510ea843SEd Schouten 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
168510ea843SEd Schouten 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
169510ea843SEd Schouten 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
170510ea843SEd Schouten 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
171510ea843SEd Schouten 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
172bbbc2d96SPoul-Henning Kamp 	tbuf.st_blksize = buf->st_blksize;
173bbbc2d96SPoul-Henning Kamp 	tbuf.st_blocks = buf->st_blocks;
174bbbc2d96SPoul-Henning Kamp 
1754e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
176c21dee17SSøren Schmidt }
177c21dee17SSøren Schmidt 
178*931e2a1aSEd Maste #ifdef LINUX_LEGACY_SYSCALLS
179c21dee17SSøren Schmidt int
180b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
181c21dee17SSøren Schmidt {
182c21dee17SSøren Schmidt 	struct stat buf;
183206a5d3aSIan Dowse 	char *path;
184c21dee17SSøren Schmidt 	int error;
185d66a5066SPeter Wemm 
186206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
187c21dee17SSøren Schmidt 
188c21dee17SSøren Schmidt #ifdef DEBUG
18924593369SJonathan Lemon 	if (ldebug(newstat))
190206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
191c21dee17SSøren Schmidt #endif
1924e0eaf69SMarcel Moolenaar 
1930eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
194206a5d3aSIan Dowse 	LFREEPATH(path);
1954e0eaf69SMarcel Moolenaar 	if (error)
1964e0eaf69SMarcel Moolenaar 		return (error);
1974e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
198c21dee17SSøren Schmidt }
199c21dee17SSøren Schmidt 
200c21dee17SSøren Schmidt int
201b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
202c21dee17SSøren Schmidt {
2034e0eaf69SMarcel Moolenaar 	struct stat sb;
204206a5d3aSIan Dowse 	char *path;
205f7a25872SJohn Baldwin 	int error;
206d66a5066SPeter Wemm 
207206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
208c21dee17SSøren Schmidt 
209c21dee17SSøren Schmidt #ifdef DEBUG
21024593369SJonathan Lemon 	if (ldebug(newlstat))
211206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
212c21dee17SSøren Schmidt #endif
2134e0eaf69SMarcel Moolenaar 
2140eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
215206a5d3aSIan Dowse 	LFREEPATH(path);
216d66a5066SPeter Wemm 	if (error)
217d66a5066SPeter Wemm 		return (error);
2185002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
219d66a5066SPeter Wemm }
220*931e2a1aSEd Maste #endif
221c21dee17SSøren Schmidt 
222c21dee17SSøren Schmidt int
223b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
224c21dee17SSøren Schmidt {
225c21dee17SSøren Schmidt 	struct stat buf;
226c21dee17SSøren Schmidt 	int error;
227c21dee17SSøren Schmidt 
228c21dee17SSøren Schmidt #ifdef DEBUG
22924593369SJonathan Lemon 	if (ldebug(newfstat))
23024593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
231c21dee17SSøren Schmidt #endif
2324e0eaf69SMarcel Moolenaar 
233f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
234060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
235c21dee17SSøren Schmidt 	if (!error)
236c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
2374e0eaf69SMarcel Moolenaar 
2384e0eaf69SMarcel Moolenaar 	return (error);
239c21dee17SSøren Schmidt }
240c21dee17SSøren Schmidt 
2417f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2425c8919adSAlexander Leidinger static int
2435c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf)
2445c8919adSAlexander Leidinger {
2455c8919adSAlexander Leidinger 	struct l_stat lbuf;
2465c8919adSAlexander Leidinger 
2475c8919adSAlexander Leidinger 	bzero(&lbuf, sizeof(lbuf));
248ab35e1c7SBruce Evans 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
2495c8919adSAlexander Leidinger 	lbuf.st_ino = buf->st_ino;
2505c8919adSAlexander Leidinger 	lbuf.st_mode = buf->st_mode;
2515c8919adSAlexander Leidinger 	lbuf.st_nlink = buf->st_nlink;
2525c8919adSAlexander Leidinger 	lbuf.st_uid = buf->st_uid;
2535c8919adSAlexander Leidinger 	lbuf.st_gid = buf->st_gid;
2545c8919adSAlexander Leidinger 	lbuf.st_rdev = buf->st_rdev;
255372639f9SBruce Evans 	lbuf.st_size = MIN(buf->st_size, INT32_MAX);
256510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
257510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
258510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
259510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
260510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
261510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
2625c8919adSAlexander Leidinger 	lbuf.st_blksize = buf->st_blksize;
2635c8919adSAlexander Leidinger 	lbuf.st_blocks = buf->st_blocks;
2645c8919adSAlexander Leidinger 	lbuf.st_flags = buf->st_flags;
2655c8919adSAlexander Leidinger 	lbuf.st_gen = buf->st_gen;
2665c8919adSAlexander Leidinger 
2675c8919adSAlexander Leidinger 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
2685c8919adSAlexander Leidinger }
2695c8919adSAlexander Leidinger 
2705c8919adSAlexander Leidinger int
2715c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args)
2725c8919adSAlexander Leidinger {
2735c8919adSAlexander Leidinger 	struct stat buf;
27415b78ac5SKonstantin Belousov 	char *path;
2755c8919adSAlexander Leidinger 	int error;
27615b78ac5SKonstantin Belousov 
27715b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
27815b78ac5SKonstantin Belousov 
2795c8919adSAlexander Leidinger #ifdef DEBUG
2805c8919adSAlexander Leidinger 	if (ldebug(stat))
281d075105dSKonstantin Belousov 		printf(ARGS(stat, "%s, *"), path);
2825c8919adSAlexander Leidinger #endif
2830eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
284d075105dSKonstantin Belousov 	if (error) {
28515b78ac5SKonstantin Belousov 		LFREEPATH(path);
2865c8919adSAlexander Leidinger 		return (error);
287d075105dSKonstantin Belousov 	}
288d075105dSKonstantin Belousov 	LFREEPATH(path);
2895c8919adSAlexander Leidinger 	return (stat_copyout(&buf, args->up));
2905c8919adSAlexander Leidinger }
2915c8919adSAlexander Leidinger 
2925c8919adSAlexander Leidinger int
2935c8919adSAlexander Leidinger linux_lstat(struct thread *td, struct linux_lstat_args *args)
2945c8919adSAlexander Leidinger {
2955c8919adSAlexander Leidinger 	struct stat buf;
29615b78ac5SKonstantin Belousov 	char *path;
2975c8919adSAlexander Leidinger 	int error;
2985c8919adSAlexander Leidinger 
29915b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
30015b78ac5SKonstantin Belousov 
3015c8919adSAlexander Leidinger #ifdef DEBUG
3025c8919adSAlexander Leidinger 	if (ldebug(lstat))
303d075105dSKonstantin Belousov 		printf(ARGS(lstat, "%s, *"), path);
3045c8919adSAlexander Leidinger #endif
3050eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
306d075105dSKonstantin Belousov 	if (error) {
30715b78ac5SKonstantin Belousov 		LFREEPATH(path);
3085c8919adSAlexander Leidinger 		return (error);
309d075105dSKonstantin Belousov 	}
310d075105dSKonstantin Belousov 	LFREEPATH(path);
3115c8919adSAlexander Leidinger 	return (stat_copyout(&buf, args->up));
3125c8919adSAlexander Leidinger }
3137f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
3145c8919adSAlexander Leidinger 
3155002a60fSMarcel Moolenaar struct l_statfs {
316297f61ccSDmitry Chagin 	l_long		f_type;
317297f61ccSDmitry Chagin 	l_long		f_bsize;
318297f61ccSDmitry Chagin 	l_long		f_blocks;
319297f61ccSDmitry Chagin 	l_long		f_bfree;
320297f61ccSDmitry Chagin 	l_long		f_bavail;
321297f61ccSDmitry Chagin 	l_long		f_files;
322297f61ccSDmitry Chagin 	l_long		f_ffree;
3235002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
324297f61ccSDmitry Chagin 	l_long		f_namelen;
325e801ac78SEdward Tomasz Napierala 	l_long		f_frsize;
326e801ac78SEdward Tomasz Napierala 	l_long		f_flags;
327e801ac78SEdward Tomasz Napierala 	l_long		f_spare[4];
328c21dee17SSøren Schmidt };
329c21dee17SSøren Schmidt 
330dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
331dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
332dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
333dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
334dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
335dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
336dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
337dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
338dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
339dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
340dbaa9ebfSEd Maste #define	LINUX_ZFS_SUPER_MAGIC	0x2FC12FC1
341e83d253bSOlivier Houchard #define	LINUX_DEVFS_SUPER_MAGIC	0x1373L
3422166e4e0SDmitry Chagin #define	LINUX_SHMFS_MAGIC	0x01021994
343dca60efcSMarcel Moolenaar 
344dca60efcSMarcel Moolenaar static long
345962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
346dca60efcSMarcel Moolenaar {
347962cf420SMaxim Sobolev 	int i;
348962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
349962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
350dbaa9ebfSEd Maste 		{"zfs",     LINUX_ZFS_SUPER_MAGIC},
351962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
352962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
353962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
354962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
355962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
356962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
357962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
358962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
359962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
360e83d253bSOlivier Houchard 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
3612166e4e0SDmitry Chagin 		{"tmpfs",   LINUX_SHMFS_MAGIC},
362962cf420SMaxim Sobolev 		{NULL,      0L}};
363dca60efcSMarcel Moolenaar 
364962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
365962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
366962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
367dca60efcSMarcel Moolenaar 
368dca60efcSMarcel Moolenaar 	return (0L);
369dca60efcSMarcel Moolenaar }
370dca60efcSMarcel Moolenaar 
371525c9796SDmitry Chagin static int
372d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
373f7a25872SJohn Baldwin {
374525c9796SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
375525c9796SDmitry Chagin 	uint64_t tmp;
376f7a25872SJohn Baldwin 
377525c9796SDmitry Chagin #define	LINUX_HIBITS	0xffffffff00000000ULL
378525c9796SDmitry Chagin 
379525c9796SDmitry Chagin 	tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
380525c9796SDmitry Chagin 	    bsd_statfs->f_bsize;
381525c9796SDmitry Chagin 	if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
382525c9796SDmitry Chagin 	    (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
383525c9796SDmitry Chagin 	    (tmp & LINUX_HIBITS))
384525c9796SDmitry Chagin 		return (EOVERFLOW);
385525c9796SDmitry Chagin #undef	LINUX_HIBITS
386525c9796SDmitry Chagin #endif
387f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
388f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
389f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
390f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
391f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
392f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
393f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
394f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
395f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
396f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
397e801ac78SEdward Tomasz Napierala 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
398e801ac78SEdward Tomasz Napierala 	linux_statfs->f_flags = 0;
399e801ac78SEdward Tomasz Napierala 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
400525c9796SDmitry Chagin 
401525c9796SDmitry Chagin 	return (0);
402f7a25872SJohn Baldwin }
403f7a25872SJohn Baldwin 
404c21dee17SSøren Schmidt int
405b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
406c21dee17SSøren Schmidt {
4075002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
4082f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
409206a5d3aSIan Dowse 	char *path;
4102166e4e0SDmitry Chagin 	int error;
411d66a5066SPeter Wemm 
412206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
413c21dee17SSøren Schmidt 
414c21dee17SSøren Schmidt #ifdef DEBUG
41524593369SJonathan Lemon 	if (ldebug(statfs))
416206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
417c21dee17SSøren Schmidt #endif
4182f304845SKonstantin Belousov 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4192f304845SKonstantin Belousov 	error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
420206a5d3aSIan Dowse 	LFREEPATH(path);
4212f304845SKonstantin Belousov 	if (error == 0)
4222f304845SKonstantin Belousov 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
4232f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
4242f304845SKonstantin Belousov 	if (error != 0)
4252ad02313SDmitry Chagin 		return (error);
4267958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
427c21dee17SSøren Schmidt }
428c21dee17SSøren Schmidt 
4297f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
430835e5061SAlexander Leidinger static void
431835e5061SAlexander Leidinger bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
432835e5061SAlexander Leidinger {
433835e5061SAlexander Leidinger 
434835e5061SAlexander Leidinger 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
435835e5061SAlexander Leidinger 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
436835e5061SAlexander Leidinger 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
437835e5061SAlexander Leidinger 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
438835e5061SAlexander Leidinger 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
439835e5061SAlexander Leidinger 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
440835e5061SAlexander Leidinger 	linux_statfs->f_files = bsd_statfs->f_files;
441835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
442835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
443835e5061SAlexander Leidinger 	linux_statfs->f_namelen = MAXNAMLEN;
444e801ac78SEdward Tomasz Napierala 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
445e801ac78SEdward Tomasz Napierala 	linux_statfs->f_flags = 0;
446e801ac78SEdward Tomasz Napierala 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
447835e5061SAlexander Leidinger }
448835e5061SAlexander Leidinger 
449835e5061SAlexander Leidinger int
450835e5061SAlexander Leidinger linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
451835e5061SAlexander Leidinger {
452835e5061SAlexander Leidinger 	struct l_statfs64 linux_statfs;
4532f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
454835e5061SAlexander Leidinger 	char *path;
455835e5061SAlexander Leidinger 	int error;
456835e5061SAlexander Leidinger 
4573ab85269SDavid Malone 	if (args->bufsize != sizeof(struct l_statfs64))
458340f4a8dSEd Maste 		return (EINVAL);
4593ab85269SDavid Malone 
460835e5061SAlexander Leidinger 	LCONVPATHEXIST(td, args->path, &path);
461835e5061SAlexander Leidinger 
462835e5061SAlexander Leidinger #ifdef DEBUG
463835e5061SAlexander Leidinger 	if (ldebug(statfs64))
464835e5061SAlexander Leidinger 		printf(ARGS(statfs64, "%s, *"), path);
465835e5061SAlexander Leidinger #endif
4662f304845SKonstantin Belousov 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4672f304845SKonstantin Belousov 	error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
468835e5061SAlexander Leidinger 	LFREEPATH(path);
4692f304845SKonstantin Belousov 	if (error == 0)
4702f304845SKonstantin Belousov 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
4712f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
4722f304845SKonstantin Belousov 	if (error != 0)
473835e5061SAlexander Leidinger 		return (error);
4747958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
475835e5061SAlexander Leidinger }
47699546279SDmitry Chagin 
47799546279SDmitry Chagin int
47899546279SDmitry Chagin linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
47999546279SDmitry Chagin {
48099546279SDmitry Chagin 	struct l_statfs64 linux_statfs;
4812f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
48299546279SDmitry Chagin 	int error;
48399546279SDmitry Chagin 
48499546279SDmitry Chagin #ifdef DEBUG
48599546279SDmitry Chagin 	if (ldebug(fstatfs64))
48699546279SDmitry Chagin 		printf(ARGS(fstatfs64, "%d, *"), args->fd);
48799546279SDmitry Chagin #endif
48899546279SDmitry Chagin 	if (args->bufsize != sizeof(struct l_statfs64))
48999546279SDmitry Chagin 		return (EINVAL);
49099546279SDmitry Chagin 
4912f304845SKonstantin Belousov 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4922f304845SKonstantin Belousov 	error = kern_fstatfs(td, args->fd, bsd_statfs);
4932f304845SKonstantin Belousov 	if (error == 0)
4942f304845SKonstantin Belousov 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
4952f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
4962f304845SKonstantin Belousov 	if (error != 0)
4972f304845SKonstantin Belousov 		return (error);
49899546279SDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
49999546279SDmitry Chagin }
5007f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
501835e5061SAlexander Leidinger 
502c21dee17SSøren Schmidt int
503b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
504c21dee17SSøren Schmidt {
5055002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
5062f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
507c21dee17SSøren Schmidt 	int error;
508c21dee17SSøren Schmidt 
509c21dee17SSøren Schmidt #ifdef DEBUG
51024593369SJonathan Lemon 	if (ldebug(fstatfs))
51124593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
512c21dee17SSøren Schmidt #endif
5132f304845SKonstantin Belousov 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
5142f304845SKonstantin Belousov 	error = kern_fstatfs(td, args->fd, bsd_statfs);
5152f304845SKonstantin Belousov 	if (error == 0)
5162f304845SKonstantin Belousov 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
5172f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
5182f304845SKonstantin Belousov 	if (error != 0)
5192ad02313SDmitry Chagin 		return (error);
5207958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
521c21dee17SSøren Schmidt }
522408da119SMarcel Moolenaar 
5235002a60fSMarcel Moolenaar struct l_ustat
524408da119SMarcel Moolenaar {
5255002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
5265002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
5275002a60fSMarcel Moolenaar 	char		f_fname[6];
5285002a60fSMarcel Moolenaar 	char		f_fpack[6];
5295002a60fSMarcel Moolenaar };
5305002a60fSMarcel Moolenaar 
531*931e2a1aSEd Maste #ifdef LINUX_LEGACY_SYSCALLS
5325002a60fSMarcel Moolenaar int
533b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
5345002a60fSMarcel Moolenaar {
5351e247cc2SPoul-Henning Kamp #ifdef DEBUG
5361e247cc2SPoul-Henning Kamp 	if (ldebug(ustat))
5374ca75bedSDmitry Chagin 		printf(ARGS(ustat, "%ju, *"), (uintmax_t)args->dev);
5381e247cc2SPoul-Henning Kamp #endif
5391e247cc2SPoul-Henning Kamp 
5401e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
541408da119SMarcel Moolenaar }
542*931e2a1aSEd Maste #endif
5435002a60fSMarcel Moolenaar 
5441997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
5455002a60fSMarcel Moolenaar 
5465002a60fSMarcel Moolenaar static int
5475002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
5485002a60fSMarcel Moolenaar {
5495002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
5505002a60fSMarcel Moolenaar 
5515002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
552ab35e1c7SBruce Evans 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
5535002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
5545002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
5555002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
5565002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
5575002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
5585002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
5595002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
560510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
561510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
562510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
563510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
564510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
565510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
5665002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
5675002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
5685002a60fSMarcel Moolenaar 
5695002a60fSMarcel Moolenaar 	/*
5705002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
5715002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
5725002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
5735002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
5745002a60fSMarcel Moolenaar 	 */
5755002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
5765002a60fSMarcel Moolenaar 
5775002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
5785002a60fSMarcel Moolenaar }
5795002a60fSMarcel Moolenaar 
5805002a60fSMarcel Moolenaar int
581b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
5825002a60fSMarcel Moolenaar {
5835002a60fSMarcel Moolenaar 	struct stat buf;
584206a5d3aSIan Dowse 	char *filename;
585f7a25872SJohn Baldwin 	int error;
5865002a60fSMarcel Moolenaar 
587206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5885002a60fSMarcel Moolenaar 
5895002a60fSMarcel Moolenaar #ifdef DEBUG
5905002a60fSMarcel Moolenaar 	if (ldebug(stat64))
591206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
5925002a60fSMarcel Moolenaar #endif
5935002a60fSMarcel Moolenaar 
5940eee862aSEd Schouten 	error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
595206a5d3aSIan Dowse 	LFREEPATH(filename);
5965002a60fSMarcel Moolenaar 	if (error)
5975002a60fSMarcel Moolenaar 		return (error);
5985002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
5995002a60fSMarcel Moolenaar }
6005002a60fSMarcel Moolenaar 
6015002a60fSMarcel Moolenaar int
602b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
6035002a60fSMarcel Moolenaar {
6045002a60fSMarcel Moolenaar 	struct stat sb;
605206a5d3aSIan Dowse 	char *filename;
606f7a25872SJohn Baldwin 	int error;
6075002a60fSMarcel Moolenaar 
608206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
6095002a60fSMarcel Moolenaar 
6105002a60fSMarcel Moolenaar #ifdef DEBUG
6115002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
6125002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
6135002a60fSMarcel Moolenaar #endif
6145002a60fSMarcel Moolenaar 
6150eee862aSEd Schouten 	error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
616206a5d3aSIan Dowse 	LFREEPATH(filename);
6175002a60fSMarcel Moolenaar 	if (error)
6185002a60fSMarcel Moolenaar 		return (error);
6195002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
6205002a60fSMarcel Moolenaar }
6215002a60fSMarcel Moolenaar 
6225002a60fSMarcel Moolenaar int
623b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
6245002a60fSMarcel Moolenaar {
6255002a60fSMarcel Moolenaar 	struct stat buf;
6265002a60fSMarcel Moolenaar 	int error;
6275002a60fSMarcel Moolenaar 
6285002a60fSMarcel Moolenaar #ifdef DEBUG
6295002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
6305002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
6315002a60fSMarcel Moolenaar #endif
6325002a60fSMarcel Moolenaar 
633f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
634060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
6355002a60fSMarcel Moolenaar 	if (!error)
6365002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
6375002a60fSMarcel Moolenaar 
6385002a60fSMarcel Moolenaar 	return (error);
6395002a60fSMarcel Moolenaar }
6405002a60fSMarcel Moolenaar 
64148b05c3fSKonstantin Belousov int
64248b05c3fSKonstantin Belousov linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
64348b05c3fSKonstantin Belousov {
64448b05c3fSKonstantin Belousov 	char *path;
64548b05c3fSKonstantin Belousov 	int error, dfd, flag;
64648b05c3fSKonstantin Belousov 	struct stat buf;
64748b05c3fSKonstantin Belousov 
64848b05c3fSKonstantin Belousov 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
64948b05c3fSKonstantin Belousov 		return (EINVAL);
65048b05c3fSKonstantin Belousov 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
65148b05c3fSKonstantin Belousov 	    AT_SYMLINK_NOFOLLOW : 0;
65248b05c3fSKonstantin Belousov 
65348b05c3fSKonstantin Belousov 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
65448b05c3fSKonstantin Belousov 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
65548b05c3fSKonstantin Belousov 
65648b05c3fSKonstantin Belousov #ifdef DEBUG
65748b05c3fSKonstantin Belousov 	if (ldebug(fstatat64))
65848b05c3fSKonstantin Belousov 		printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
65948b05c3fSKonstantin Belousov #endif
66048b05c3fSKonstantin Belousov 
6610eee862aSEd Schouten 	error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
66248b05c3fSKonstantin Belousov 	if (!error)
66348b05c3fSKonstantin Belousov 		error = stat64_copyout(&buf, args->statbuf);
66448b05c3fSKonstantin Belousov 	LFREEPATH(path);
66548b05c3fSKonstantin Belousov 
66648b05c3fSKonstantin Belousov 	return (error);
66748b05c3fSKonstantin Belousov }
66848b05c3fSKonstantin Belousov 
669606bcc17SDmitry Chagin #else /* __amd64__ && !COMPAT_LINUX32 */
670606bcc17SDmitry Chagin 
671606bcc17SDmitry Chagin int
672606bcc17SDmitry Chagin linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
673606bcc17SDmitry Chagin {
674606bcc17SDmitry Chagin 	char *path;
675606bcc17SDmitry Chagin 	int error, dfd, flag;
676606bcc17SDmitry Chagin 	struct stat buf;
677606bcc17SDmitry Chagin 
678606bcc17SDmitry Chagin 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
679606bcc17SDmitry Chagin 		return (EINVAL);
680606bcc17SDmitry Chagin 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
681606bcc17SDmitry Chagin 	    AT_SYMLINK_NOFOLLOW : 0;
682606bcc17SDmitry Chagin 
683606bcc17SDmitry Chagin 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
684606bcc17SDmitry Chagin 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
685606bcc17SDmitry Chagin 
686606bcc17SDmitry Chagin #ifdef DEBUG
687606bcc17SDmitry Chagin 	if (ldebug(newfstatat))
688606bcc17SDmitry Chagin 		printf(ARGS(newfstatat, "%i, %s, %i"), args->dfd, path, args->flag);
689606bcc17SDmitry Chagin #endif
690606bcc17SDmitry Chagin 
691606bcc17SDmitry Chagin 	error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
692606bcc17SDmitry Chagin 	if (error == 0)
693606bcc17SDmitry Chagin 		error = newstat_copyout(&buf, args->statbuf);
694606bcc17SDmitry Chagin 	LFREEPATH(path);
695606bcc17SDmitry Chagin 
696606bcc17SDmitry Chagin 	return (error);
697606bcc17SDmitry Chagin }
698606bcc17SDmitry Chagin 
6991997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
7009802eb9eSDmitry Chagin 
7019802eb9eSDmitry Chagin int
7029802eb9eSDmitry Chagin linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
7039802eb9eSDmitry Chagin {
7049802eb9eSDmitry Chagin 	struct mount *mp;
7059802eb9eSDmitry Chagin 	struct vnode *vp;
7069802eb9eSDmitry Chagin 	int error, save;
7079802eb9eSDmitry Chagin 
708cbd92ce6SMatt Macy 	error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
7099802eb9eSDmitry Chagin 	if (error != 0)
7109802eb9eSDmitry Chagin 		/*
7119802eb9eSDmitry Chagin 		 * Linux syncfs() returns only EBADF, however fgetvp()
7129802eb9eSDmitry Chagin 		 * can return EINVAL in case of file descriptor does
7139802eb9eSDmitry Chagin 		 * not represent a vnode. XXX.
7149802eb9eSDmitry Chagin 		 */
7159802eb9eSDmitry Chagin 		return (error);
7169802eb9eSDmitry Chagin 
7179802eb9eSDmitry Chagin 	mp = vp->v_mount;
7189802eb9eSDmitry Chagin 	mtx_lock(&mountlist_mtx);
7199802eb9eSDmitry Chagin 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
7209802eb9eSDmitry Chagin 	if (error != 0) {
7219802eb9eSDmitry Chagin 		/* See comment above. */
7229802eb9eSDmitry Chagin 		mtx_unlock(&mountlist_mtx);
7239802eb9eSDmitry Chagin 		goto out;
7249802eb9eSDmitry Chagin 	}
7259802eb9eSDmitry Chagin 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
7269802eb9eSDmitry Chagin 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
7279802eb9eSDmitry Chagin 		save = curthread_pflags_set(TDP_SYNCIO);
7289802eb9eSDmitry Chagin 		vfs_msync(mp, MNT_NOWAIT);
7299802eb9eSDmitry Chagin 		VFS_SYNC(mp, MNT_NOWAIT);
7309802eb9eSDmitry Chagin 		curthread_pflags_restore(save);
7319802eb9eSDmitry Chagin 		vn_finished_write(mp);
7329802eb9eSDmitry Chagin 	}
7339802eb9eSDmitry Chagin 	vfs_unbusy(mp);
7349802eb9eSDmitry Chagin 
7359802eb9eSDmitry Chagin  out:
7369802eb9eSDmitry Chagin 	vrele(vp);
7379802eb9eSDmitry Chagin 	return (error);
7389802eb9eSDmitry Chagin }
739