xref: /freebsd/sys/compat/linux/linux_stats.c (revision 4b45c2bb83a1d7aded0c424d65595cc576760dc7)
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 
620eee862aSEd Schouten static void
630eee862aSEd Schouten translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
640eee862aSEd Schouten {
650eee862aSEd Schouten 	int major, minor;
660eee862aSEd Schouten 
677ad2a82dSMateusz Guzik 	if (vn_isdisk(vp)) {
68273ce4aeSEdward Tomasz Napierala 		sb->st_mode &= ~S_IFMT;
69273ce4aeSEdward Tomasz Napierala 		sb->st_mode |= S_IFBLK;
70273ce4aeSEdward Tomasz Napierala 	}
71273ce4aeSEdward Tomasz Napierala 
7217f701a3SEdward Tomasz Napierala 	/*
7317f701a3SEdward Tomasz Napierala 	 * Return the same st_dev for every devfs instance.  The reason
7417f701a3SEdward Tomasz Napierala 	 * for this is to work around an idiosyncrasy of glibc getttynam()
7517f701a3SEdward Tomasz Napierala 	 * implementation: it checks whether st_dev returned for fd 0
7617f701a3SEdward Tomasz Napierala 	 * is the same as st_dev returned for the target of /proc/self/fd/0
7717f701a3SEdward Tomasz Napierala 	 * symlink, and with linux chroots having their own devfs instance,
7817f701a3SEdward Tomasz Napierala 	 * the check will fail if you chroot into it.
7917f701a3SEdward Tomasz Napierala 	 */
8017f701a3SEdward Tomasz Napierala 	if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc)
8117f701a3SEdward Tomasz Napierala 		sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
8217f701a3SEdward Tomasz Napierala 
831a34e9faSEdward Tomasz Napierala 	if (linux_vn_get_major_minor(vp, &major, &minor) == 0)
840eee862aSEd Schouten 		sb->st_rdev = (major << 8 | minor);
850eee862aSEd Schouten }
860eee862aSEd Schouten 
870eee862aSEd Schouten static int
88a125ed50SMateusz Guzik linux_kern_statat(struct thread *td, int flag, int fd, const char *path,
890eee862aSEd Schouten     enum uio_seg pathseg, struct stat *sbp)
900eee862aSEd Schouten {
910eee862aSEd Schouten 
926e646651SKonstantin Belousov 	return (kern_statat(td, flag, fd, path, pathseg, sbp,
930eee862aSEd Schouten 	    translate_vnhook_major_minor));
940eee862aSEd Schouten }
950eee862aSEd Schouten 
96931e2a1aSEd Maste #ifdef LINUX_LEGACY_SYSCALLS
970eee862aSEd Schouten static int
98a125ed50SMateusz Guzik linux_kern_stat(struct thread *td, const char *path, enum uio_seg pathseg,
990eee862aSEd Schouten     struct stat *sbp)
1000eee862aSEd Schouten {
1010eee862aSEd Schouten 
1020eee862aSEd Schouten 	return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
1030eee862aSEd Schouten }
1040eee862aSEd Schouten 
1050eee862aSEd Schouten static int
106a125ed50SMateusz Guzik linux_kern_lstat(struct thread *td, const char *path, enum uio_seg pathseg,
1070eee862aSEd Schouten     struct stat *sbp)
1080eee862aSEd Schouten {
1090eee862aSEd Schouten 
1100eee862aSEd Schouten 	return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
1110eee862aSEd Schouten 	    pathseg, sbp));
1120eee862aSEd Schouten }
113931e2a1aSEd Maste #endif
1140eee862aSEd Schouten 
115060e4882SDoug Ambrisko static void
116060e4882SDoug Ambrisko translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
117060e4882SDoug Ambrisko {
118060e4882SDoug Ambrisko 	struct file *fp;
1190eee862aSEd Schouten 	struct vnode *vp;
12017f701a3SEdward Tomasz Napierala 	struct mount *mp;
121060e4882SDoug Ambrisko 	int major, minor;
122060e4882SDoug Ambrisko 
123a9d2f8d8SRobert Watson 	/*
124a9d2f8d8SRobert Watson 	 * No capability rights required here.
125a9d2f8d8SRobert Watson 	 */
126b256a1e1SJung-uk Kim 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
127cbd92ce6SMatt Macy 	    fget(td, fd, &cap_no_rights, &fp) != 0)
128060e4882SDoug Ambrisko 		return;
1290eee862aSEd Schouten 	vp = fp->f_vnode;
1307ad2a82dSMateusz Guzik 	if (vp != NULL && vn_isdisk(vp)) {
131273ce4aeSEdward Tomasz Napierala 		buf->st_mode &= ~S_IFMT;
132273ce4aeSEdward Tomasz Napierala 		buf->st_mode |= S_IFBLK;
133273ce4aeSEdward Tomasz Napierala 	}
13417f701a3SEdward Tomasz Napierala 	if (vp != NULL && rootdevmp != NULL) {
13517f701a3SEdward Tomasz Napierala 		mp = vp->v_mount;
13617f701a3SEdward Tomasz Napierala 		__compiler_membar();
13717f701a3SEdward Tomasz Napierala 		if (mp != NULL && mp->mnt_vfc == rootdevmp->mnt_vfc)
13817f701a3SEdward Tomasz Napierala 			buf->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
13917f701a3SEdward Tomasz Napierala 	}
1401a34e9faSEdward Tomasz Napierala 	if (linux_vn_get_major_minor(vp, &major, &minor) == 0) {
141060e4882SDoug Ambrisko 		buf->st_rdev = (major << 8 | minor);
142bc093719SEd Schouten 	} else if (fp->f_type == DTYPE_PTS) {
143bc093719SEd Schouten 		struct tty *tp = fp->f_data;
144bc093719SEd Schouten 
145bc093719SEd Schouten 		/* Convert the numbers for the slave device. */
1467870adb6SEd Schouten 		if (linux_driver_get_major_minor(devtoname(tp->t_dev),
147bc093719SEd Schouten 					 &major, &minor) == 0) {
148bc093719SEd Schouten 			buf->st_rdev = (major << 8 | minor);
149bc093719SEd Schouten 		}
150bc093719SEd Schouten 	}
151060e4882SDoug Ambrisko 	fdrop(fp, td);
152060e4882SDoug Ambrisko }
153060e4882SDoug Ambrisko 
154ab35e1c7SBruce Evans /*
155ab35e1c7SBruce Evans  * l_dev_t has the same encoding as dev_t in the latter's low 16 bits, so
156407a8126SBruce Evans  * truncation of a dev_t to 16 bits gives the same result as unpacking
157407a8126SBruce Evans  * using major() and minor() and repacking in the l_dev_t format.  This
158407a8126SBruce Evans  * detail is hidden in dev_to_ldev().  Overflow in conversions of dev_t's
159407a8126SBruce Evans  * are not checked for, as for other fields.
160ab35e1c7SBruce Evans  *
161407a8126SBruce Evans  * dev_to_ldev() is only used for translating st_dev.  When we convert
162407a8126SBruce Evans  * st_rdev for copying it out, it isn't really a dev_t, but has already
163407a8126SBruce Evans  * been translated to an l_dev_t in a nontrivial way.  Translating it
164407a8126SBruce Evans  * again would be illogical but would have no effect since the low 16
165407a8126SBruce Evans  * bits have the same encoding.
166407a8126SBruce Evans  *
167407a8126SBruce Evans  * The nontrivial translation for st_rdev renumbers some devices, but not
168407a8126SBruce Evans  * ones that can be mounted on, so it is consistent with the translation
169407a8126SBruce Evans  * for st_dev except when the renumbering or truncation causes conflicts.
170ab35e1c7SBruce Evans  */
171ab35e1c7SBruce Evans #define	dev_to_ldev(d)	((uint16_t)(d))
172ab35e1c7SBruce Evans 
173bbbc2d96SPoul-Henning Kamp static int
174bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf)
175bbbc2d96SPoul-Henning Kamp {
176bbbc2d96SPoul-Henning Kamp 	struct l_newstat tbuf;
177bbbc2d96SPoul-Henning Kamp 
178bbbc2d96SPoul-Henning Kamp 	bzero(&tbuf, sizeof(tbuf));
179ab35e1c7SBruce Evans 	tbuf.st_dev = dev_to_ldev(buf->st_dev);
180bbbc2d96SPoul-Henning Kamp 	tbuf.st_ino = buf->st_ino;
181bbbc2d96SPoul-Henning Kamp 	tbuf.st_mode = buf->st_mode;
182bbbc2d96SPoul-Henning Kamp 	tbuf.st_nlink = buf->st_nlink;
183bbbc2d96SPoul-Henning Kamp 	tbuf.st_uid = buf->st_uid;
184bbbc2d96SPoul-Henning Kamp 	tbuf.st_gid = buf->st_gid;
185bbbc2d96SPoul-Henning Kamp 	tbuf.st_rdev = buf->st_rdev;
186bbbc2d96SPoul-Henning Kamp 	tbuf.st_size = buf->st_size;
187510ea843SEd Schouten 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
188510ea843SEd Schouten 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
189510ea843SEd Schouten 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
190510ea843SEd Schouten 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
191510ea843SEd Schouten 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
192510ea843SEd Schouten 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
193bbbc2d96SPoul-Henning Kamp 	tbuf.st_blksize = buf->st_blksize;
194bbbc2d96SPoul-Henning Kamp 	tbuf.st_blocks = buf->st_blocks;
195bbbc2d96SPoul-Henning Kamp 
1964e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
197c21dee17SSøren Schmidt }
198c21dee17SSøren Schmidt 
199931e2a1aSEd Maste #ifdef LINUX_LEGACY_SYSCALLS
200c21dee17SSøren Schmidt int
201b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
202c21dee17SSøren Schmidt {
203c21dee17SSøren Schmidt 	struct stat buf;
204206a5d3aSIan Dowse 	char *path;
205c21dee17SSøren Schmidt 	int error;
206d66a5066SPeter Wemm 
207a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
208a125ed50SMateusz Guzik 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
209a125ed50SMateusz Guzik 	} else {
210206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->path, &path);
2110eee862aSEd Schouten 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
212206a5d3aSIan Dowse 		LFREEPATH(path);
213a125ed50SMateusz Guzik 	}
2144e0eaf69SMarcel Moolenaar 	if (error)
2154e0eaf69SMarcel Moolenaar 		return (error);
2164e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
217c21dee17SSøren Schmidt }
218c21dee17SSøren Schmidt 
219c21dee17SSøren Schmidt int
220b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
221c21dee17SSøren Schmidt {
2224e0eaf69SMarcel Moolenaar 	struct stat sb;
223206a5d3aSIan Dowse 	char *path;
224f7a25872SJohn Baldwin 	int error;
225d66a5066SPeter Wemm 
226a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
227a125ed50SMateusz Guzik 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &sb);
228a125ed50SMateusz Guzik 	} else {
229206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->path, &path);
2300eee862aSEd Schouten 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
231206a5d3aSIan Dowse 		LFREEPATH(path);
232a125ed50SMateusz Guzik 	}
233d66a5066SPeter Wemm 	if (error)
234d66a5066SPeter Wemm 		return (error);
2355002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
236d66a5066SPeter Wemm }
237931e2a1aSEd Maste #endif
238c21dee17SSøren Schmidt 
239c21dee17SSøren Schmidt int
240b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
241c21dee17SSøren Schmidt {
242c21dee17SSøren Schmidt 	struct stat buf;
243c21dee17SSøren Schmidt 	int error;
244c21dee17SSøren Schmidt 
245f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
246060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
247c21dee17SSøren Schmidt 	if (!error)
248c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
2494e0eaf69SMarcel Moolenaar 
2504e0eaf69SMarcel Moolenaar 	return (error);
251c21dee17SSøren Schmidt }
252c21dee17SSøren Schmidt 
2537f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2545c8919adSAlexander Leidinger static int
2555c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf)
2565c8919adSAlexander Leidinger {
2575c8919adSAlexander Leidinger 	struct l_stat lbuf;
2585c8919adSAlexander Leidinger 
2595c8919adSAlexander Leidinger 	bzero(&lbuf, sizeof(lbuf));
260ab35e1c7SBruce Evans 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
2615c8919adSAlexander Leidinger 	lbuf.st_ino = buf->st_ino;
2625c8919adSAlexander Leidinger 	lbuf.st_mode = buf->st_mode;
2635c8919adSAlexander Leidinger 	lbuf.st_nlink = buf->st_nlink;
2645c8919adSAlexander Leidinger 	lbuf.st_uid = buf->st_uid;
2655c8919adSAlexander Leidinger 	lbuf.st_gid = buf->st_gid;
2665c8919adSAlexander Leidinger 	lbuf.st_rdev = buf->st_rdev;
267372639f9SBruce Evans 	lbuf.st_size = MIN(buf->st_size, INT32_MAX);
268510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
269510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
270510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
271510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
272510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
273510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
2745c8919adSAlexander Leidinger 	lbuf.st_blksize = buf->st_blksize;
2755c8919adSAlexander Leidinger 	lbuf.st_blocks = buf->st_blocks;
2765c8919adSAlexander Leidinger 	lbuf.st_flags = buf->st_flags;
2775c8919adSAlexander Leidinger 	lbuf.st_gen = buf->st_gen;
2785c8919adSAlexander Leidinger 
2795c8919adSAlexander Leidinger 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
2805c8919adSAlexander Leidinger }
2815c8919adSAlexander Leidinger 
2825c8919adSAlexander Leidinger int
2835c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args)
2845c8919adSAlexander Leidinger {
2855c8919adSAlexander Leidinger 	struct stat buf;
28615b78ac5SKonstantin Belousov 	char *path;
2875c8919adSAlexander Leidinger 	int error;
28815b78ac5SKonstantin Belousov 
289a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
290a125ed50SMateusz Guzik 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
291a125ed50SMateusz Guzik 	} else {
29215b78ac5SKonstantin Belousov 		LCONVPATHEXIST(td, args->path, &path);
2930eee862aSEd Schouten 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
29415b78ac5SKonstantin Belousov 		LFREEPATH(path);
295a125ed50SMateusz Guzik 	}
296a125ed50SMateusz Guzik 	if (error) {
2975c8919adSAlexander Leidinger 		return (error);
298d075105dSKonstantin Belousov 	}
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 
309a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
310a125ed50SMateusz Guzik 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &buf);
311a125ed50SMateusz Guzik 	} else {
31215b78ac5SKonstantin Belousov 		LCONVPATHEXIST(td, args->path, &path);
3130eee862aSEd Schouten 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
31415b78ac5SKonstantin Belousov 		LFREEPATH(path);
315a125ed50SMateusz Guzik 	}
316a125ed50SMateusz Guzik 	if (error) {
3175c8919adSAlexander Leidinger 		return (error);
318d075105dSKonstantin Belousov 	}
3195c8919adSAlexander Leidinger 	return (stat_copyout(&buf, args->up));
3205c8919adSAlexander Leidinger }
3217f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
3225c8919adSAlexander Leidinger 
3235002a60fSMarcel Moolenaar struct l_statfs {
324297f61ccSDmitry Chagin 	l_long		f_type;
325297f61ccSDmitry Chagin 	l_long		f_bsize;
326297f61ccSDmitry Chagin 	l_long		f_blocks;
327297f61ccSDmitry Chagin 	l_long		f_bfree;
328297f61ccSDmitry Chagin 	l_long		f_bavail;
329297f61ccSDmitry Chagin 	l_long		f_files;
330297f61ccSDmitry Chagin 	l_long		f_ffree;
3315002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
332297f61ccSDmitry Chagin 	l_long		f_namelen;
333e801ac78SEdward Tomasz Napierala 	l_long		f_frsize;
334e801ac78SEdward Tomasz Napierala 	l_long		f_flags;
335e801ac78SEdward Tomasz Napierala 	l_long		f_spare[4];
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 */
348dbaa9ebfSEd Maste #define	LINUX_ZFS_SUPER_MAGIC	0x2FC12FC1
349e83d253bSOlivier Houchard #define	LINUX_DEVFS_SUPER_MAGIC	0x1373L
3502166e4e0SDmitry Chagin #define	LINUX_SHMFS_MAGIC	0x01021994
351dca60efcSMarcel Moolenaar 
352dca60efcSMarcel Moolenaar static long
353962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
354dca60efcSMarcel Moolenaar {
355962cf420SMaxim Sobolev 	int i;
356962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
357962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
358dbaa9ebfSEd Maste 		{"zfs",     LINUX_ZFS_SUPER_MAGIC},
359962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
360962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
361962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
362962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
363962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
364962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
365962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
366962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
367962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
368e83d253bSOlivier Houchard 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
3692166e4e0SDmitry Chagin 		{"tmpfs",   LINUX_SHMFS_MAGIC},
370962cf420SMaxim Sobolev 		{NULL,      0L}};
371dca60efcSMarcel Moolenaar 
372962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
373962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
374962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
375dca60efcSMarcel Moolenaar 
376dca60efcSMarcel Moolenaar 	return (0L);
377dca60efcSMarcel Moolenaar }
378dca60efcSMarcel Moolenaar 
379525c9796SDmitry Chagin static int
380d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
381f7a25872SJohn Baldwin {
382525c9796SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
383525c9796SDmitry Chagin 	uint64_t tmp;
384f7a25872SJohn Baldwin 
385525c9796SDmitry Chagin #define	LINUX_HIBITS	0xffffffff00000000ULL
386525c9796SDmitry Chagin 
387525c9796SDmitry Chagin 	tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
388525c9796SDmitry Chagin 	    bsd_statfs->f_bsize;
389525c9796SDmitry Chagin 	if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
390525c9796SDmitry Chagin 	    (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
391525c9796SDmitry Chagin 	    (tmp & LINUX_HIBITS))
392525c9796SDmitry Chagin 		return (EOVERFLOW);
393525c9796SDmitry Chagin #undef	LINUX_HIBITS
394525c9796SDmitry Chagin #endif
395f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
396f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
397f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
398f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
399f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
400f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
401f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
402f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
403f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
404f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
405e801ac78SEdward Tomasz Napierala 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
406e801ac78SEdward Tomasz Napierala 	linux_statfs->f_flags = 0;
407e801ac78SEdward Tomasz Napierala 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
408525c9796SDmitry Chagin 
409525c9796SDmitry Chagin 	return (0);
410f7a25872SJohn Baldwin }
411f7a25872SJohn Baldwin 
412c21dee17SSøren Schmidt int
413b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
414c21dee17SSøren Schmidt {
4155002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
4162f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
417206a5d3aSIan Dowse 	char *path;
4182166e4e0SDmitry Chagin 	int error;
419d66a5066SPeter Wemm 
420a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
421a125ed50SMateusz Guzik 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
422a125ed50SMateusz Guzik 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
423a125ed50SMateusz Guzik 	} else {
424206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->path, &path);
4252f304845SKonstantin Belousov 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4262f304845SKonstantin Belousov 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
427206a5d3aSIan Dowse 		LFREEPATH(path);
428a125ed50SMateusz Guzik 	}
4292f304845SKonstantin Belousov 	if (error == 0)
4302f304845SKonstantin Belousov 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
4312f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
4322f304845SKonstantin Belousov 	if (error != 0)
4332ad02313SDmitry Chagin 		return (error);
4347958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
435c21dee17SSøren Schmidt }
436c21dee17SSøren Schmidt 
4377f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
438835e5061SAlexander Leidinger static void
439835e5061SAlexander Leidinger bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
440835e5061SAlexander Leidinger {
441835e5061SAlexander Leidinger 
442835e5061SAlexander Leidinger 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
443835e5061SAlexander Leidinger 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
444835e5061SAlexander Leidinger 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
445835e5061SAlexander Leidinger 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
446835e5061SAlexander Leidinger 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
447835e5061SAlexander Leidinger 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
448835e5061SAlexander Leidinger 	linux_statfs->f_files = bsd_statfs->f_files;
449835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
450835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
451835e5061SAlexander Leidinger 	linux_statfs->f_namelen = MAXNAMLEN;
452e801ac78SEdward Tomasz Napierala 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
453e801ac78SEdward Tomasz Napierala 	linux_statfs->f_flags = 0;
454e801ac78SEdward Tomasz Napierala 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
455835e5061SAlexander Leidinger }
456835e5061SAlexander Leidinger 
457835e5061SAlexander Leidinger int
458835e5061SAlexander Leidinger linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
459835e5061SAlexander Leidinger {
460835e5061SAlexander Leidinger 	struct l_statfs64 linux_statfs;
4612f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
462835e5061SAlexander Leidinger 	char *path;
463835e5061SAlexander Leidinger 	int error;
464835e5061SAlexander Leidinger 
4653ab85269SDavid Malone 	if (args->bufsize != sizeof(struct l_statfs64))
466340f4a8dSEd Maste 		return (EINVAL);
4673ab85269SDavid Malone 
468a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
469a125ed50SMateusz Guzik 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
470a125ed50SMateusz Guzik 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
471a125ed50SMateusz Guzik 	} else {
472835e5061SAlexander Leidinger 		LCONVPATHEXIST(td, args->path, &path);
4732f304845SKonstantin Belousov 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4742f304845SKonstantin Belousov 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
475835e5061SAlexander Leidinger 		LFREEPATH(path);
476a125ed50SMateusz Guzik 	}
4772f304845SKonstantin Belousov 	if (error == 0)
4782f304845SKonstantin Belousov 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
4792f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
4802f304845SKonstantin Belousov 	if (error != 0)
481835e5061SAlexander Leidinger 		return (error);
4827958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
483835e5061SAlexander Leidinger }
48499546279SDmitry Chagin 
48599546279SDmitry Chagin int
48699546279SDmitry Chagin linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
48799546279SDmitry Chagin {
48899546279SDmitry Chagin 	struct l_statfs64 linux_statfs;
4892f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
49099546279SDmitry Chagin 	int error;
49199546279SDmitry Chagin 
49299546279SDmitry Chagin 	if (args->bufsize != sizeof(struct l_statfs64))
49399546279SDmitry Chagin 		return (EINVAL);
49499546279SDmitry Chagin 
4952f304845SKonstantin Belousov 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4962f304845SKonstantin Belousov 	error = kern_fstatfs(td, args->fd, bsd_statfs);
4972f304845SKonstantin Belousov 	if (error == 0)
4982f304845SKonstantin Belousov 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
4992f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
5002f304845SKonstantin Belousov 	if (error != 0)
5012f304845SKonstantin Belousov 		return (error);
50299546279SDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
50399546279SDmitry Chagin }
5047f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
505835e5061SAlexander Leidinger 
506c21dee17SSøren Schmidt int
507b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
508c21dee17SSøren Schmidt {
5095002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
5102f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
511c21dee17SSøren Schmidt 	int error;
512c21dee17SSøren Schmidt 
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 
531931e2a1aSEd 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 
5361e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
537408da119SMarcel Moolenaar }
538931e2a1aSEd Maste #endif
5395002a60fSMarcel Moolenaar 
5401997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
5415002a60fSMarcel Moolenaar 
5425002a60fSMarcel Moolenaar static int
5435002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
5445002a60fSMarcel Moolenaar {
5455002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
5465002a60fSMarcel Moolenaar 
5475002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
548ab35e1c7SBruce Evans 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
5495002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
5505002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
5515002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
5525002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
5535002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
5545002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
5555002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
556510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
557510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
558510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
559510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
560510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
561510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
5625002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
5635002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
5645002a60fSMarcel Moolenaar 
5655002a60fSMarcel Moolenaar 	/*
5665002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
5675002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
5685002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
5695002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
5705002a60fSMarcel Moolenaar 	 */
5715002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
5725002a60fSMarcel Moolenaar 
5735002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
5745002a60fSMarcel Moolenaar }
5755002a60fSMarcel Moolenaar 
5765002a60fSMarcel Moolenaar int
577b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
5785002a60fSMarcel Moolenaar {
5795002a60fSMarcel Moolenaar 	struct stat buf;
580206a5d3aSIan Dowse 	char *filename;
581f7a25872SJohn Baldwin 	int error;
5825002a60fSMarcel Moolenaar 
583a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
584a125ed50SMateusz Guzik 		error = linux_kern_stat(td, args->filename, UIO_USERSPACE, &buf);
585a125ed50SMateusz Guzik 	} else {
586206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->filename, &filename);
5870eee862aSEd Schouten 		error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
588206a5d3aSIan Dowse 		LFREEPATH(filename);
589a125ed50SMateusz Guzik 	}
5905002a60fSMarcel Moolenaar 	if (error)
5915002a60fSMarcel Moolenaar 		return (error);
5925002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
5935002a60fSMarcel Moolenaar }
5945002a60fSMarcel Moolenaar 
5955002a60fSMarcel Moolenaar int
596b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
5975002a60fSMarcel Moolenaar {
5985002a60fSMarcel Moolenaar 	struct stat sb;
599206a5d3aSIan Dowse 	char *filename;
600f7a25872SJohn Baldwin 	int error;
6015002a60fSMarcel Moolenaar 
602a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
603a125ed50SMateusz Guzik 		error = linux_kern_lstat(td, args->filename, UIO_USERSPACE, &sb);
604a125ed50SMateusz Guzik 	} else {
605206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->filename, &filename);
6060eee862aSEd Schouten 		error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
607206a5d3aSIan Dowse 		LFREEPATH(filename);
608a125ed50SMateusz Guzik 	}
6095002a60fSMarcel Moolenaar 	if (error)
6105002a60fSMarcel Moolenaar 		return (error);
6115002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
6125002a60fSMarcel Moolenaar }
6135002a60fSMarcel Moolenaar 
6145002a60fSMarcel Moolenaar int
615b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
6165002a60fSMarcel Moolenaar {
6175002a60fSMarcel Moolenaar 	struct stat buf;
6185002a60fSMarcel Moolenaar 	int error;
6195002a60fSMarcel Moolenaar 
620f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
621060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
6225002a60fSMarcel Moolenaar 	if (!error)
6235002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
6245002a60fSMarcel Moolenaar 
6255002a60fSMarcel Moolenaar 	return (error);
6265002a60fSMarcel Moolenaar }
6275002a60fSMarcel Moolenaar 
62848b05c3fSKonstantin Belousov int
62948b05c3fSKonstantin Belousov linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
63048b05c3fSKonstantin Belousov {
63148b05c3fSKonstantin Belousov 	char *path;
63248b05c3fSKonstantin Belousov 	int error, dfd, flag;
63348b05c3fSKonstantin Belousov 	struct stat buf;
63448b05c3fSKonstantin Belousov 
635*4b45c2bbSEdward Tomasz Napierala 	if (args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH)) {
636*4b45c2bbSEdward Tomasz Napierala 		linux_msg(td, "fstatat64 unsupported flag 0x%x", args->flag);
63748b05c3fSKonstantin Belousov 		return (EINVAL);
638*4b45c2bbSEdward Tomasz Napierala 	}
63948b05c3fSKonstantin Belousov 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
64048b05c3fSKonstantin Belousov 	    AT_SYMLINK_NOFOLLOW : 0;
641*4b45c2bbSEdward Tomasz Napierala 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
642*4b45c2bbSEdward Tomasz Napierala 	    AT_EMPTY_PATH : 0;
64348b05c3fSKonstantin Belousov 
64448b05c3fSKonstantin Belousov 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
645a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
646a125ed50SMateusz Guzik 		error = linux_kern_statat(td, flag, dfd, args->pathname,
647a125ed50SMateusz Guzik 		    UIO_USERSPACE, &buf);
648a125ed50SMateusz Guzik 	} else {
64948b05c3fSKonstantin Belousov 		LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
6500eee862aSEd Schouten 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
65148b05c3fSKonstantin Belousov 		LFREEPATH(path);
652a125ed50SMateusz Guzik 	}
653a125ed50SMateusz Guzik 	if (error == 0)
654a125ed50SMateusz Guzik 		error = stat64_copyout(&buf, args->statbuf);
65548b05c3fSKonstantin Belousov 
65648b05c3fSKonstantin Belousov 	return (error);
65748b05c3fSKonstantin Belousov }
65848b05c3fSKonstantin Belousov 
659606bcc17SDmitry Chagin #else /* __amd64__ && !COMPAT_LINUX32 */
660606bcc17SDmitry Chagin 
661606bcc17SDmitry Chagin int
662606bcc17SDmitry Chagin linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
663606bcc17SDmitry Chagin {
664606bcc17SDmitry Chagin 	char *path;
665606bcc17SDmitry Chagin 	int error, dfd, flag;
666606bcc17SDmitry Chagin 	struct stat buf;
667606bcc17SDmitry Chagin 
668*4b45c2bbSEdward Tomasz Napierala 	if (args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH)) {
669*4b45c2bbSEdward Tomasz Napierala 		linux_msg(td, "fstatat unsupported flag 0x%x", args->flag);
670606bcc17SDmitry Chagin 		return (EINVAL);
671*4b45c2bbSEdward Tomasz Napierala 	}
672*4b45c2bbSEdward Tomasz Napierala 
673606bcc17SDmitry Chagin 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
674606bcc17SDmitry Chagin 	    AT_SYMLINK_NOFOLLOW : 0;
675*4b45c2bbSEdward Tomasz Napierala 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
676*4b45c2bbSEdward Tomasz Napierala 	    AT_EMPTY_PATH : 0;
677606bcc17SDmitry Chagin 
678606bcc17SDmitry Chagin 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
679a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
680a125ed50SMateusz Guzik 		error = linux_kern_statat(td, flag, dfd, args->pathname,
681a125ed50SMateusz Guzik 		    UIO_USERSPACE, &buf);
682a125ed50SMateusz Guzik 	} else {
683606bcc17SDmitry Chagin 		LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
684606bcc17SDmitry Chagin 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
685a125ed50SMateusz Guzik 		LFREEPATH(path);
686a125ed50SMateusz Guzik 	}
687606bcc17SDmitry Chagin 	if (error == 0)
688606bcc17SDmitry Chagin 		error = newstat_copyout(&buf, args->statbuf);
689606bcc17SDmitry Chagin 
690606bcc17SDmitry Chagin 	return (error);
691606bcc17SDmitry Chagin }
692606bcc17SDmitry Chagin 
6931997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
6949802eb9eSDmitry Chagin 
6959802eb9eSDmitry Chagin int
6969802eb9eSDmitry Chagin linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
6979802eb9eSDmitry Chagin {
6989802eb9eSDmitry Chagin 	struct mount *mp;
6999802eb9eSDmitry Chagin 	struct vnode *vp;
7009802eb9eSDmitry Chagin 	int error, save;
7019802eb9eSDmitry Chagin 
702cbd92ce6SMatt Macy 	error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
7039802eb9eSDmitry Chagin 	if (error != 0)
7049802eb9eSDmitry Chagin 		/*
7059802eb9eSDmitry Chagin 		 * Linux syncfs() returns only EBADF, however fgetvp()
7069802eb9eSDmitry Chagin 		 * can return EINVAL in case of file descriptor does
7079802eb9eSDmitry Chagin 		 * not represent a vnode. XXX.
7089802eb9eSDmitry Chagin 		 */
7099802eb9eSDmitry Chagin 		return (error);
7109802eb9eSDmitry Chagin 
7119802eb9eSDmitry Chagin 	mp = vp->v_mount;
7129802eb9eSDmitry Chagin 	mtx_lock(&mountlist_mtx);
7139802eb9eSDmitry Chagin 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
7149802eb9eSDmitry Chagin 	if (error != 0) {
7159802eb9eSDmitry Chagin 		/* See comment above. */
7169802eb9eSDmitry Chagin 		mtx_unlock(&mountlist_mtx);
7179802eb9eSDmitry Chagin 		goto out;
7189802eb9eSDmitry Chagin 	}
7199802eb9eSDmitry Chagin 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
7209802eb9eSDmitry Chagin 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
7219802eb9eSDmitry Chagin 		save = curthread_pflags_set(TDP_SYNCIO);
722c8b3463dSMateusz Guzik 		vfs_periodic(mp, MNT_NOWAIT);
7239802eb9eSDmitry Chagin 		VFS_SYNC(mp, MNT_NOWAIT);
7249802eb9eSDmitry Chagin 		curthread_pflags_restore(save);
7259802eb9eSDmitry Chagin 		vn_finished_write(mp);
7269802eb9eSDmitry Chagin 	}
7279802eb9eSDmitry Chagin 	vfs_unbusy(mp);
7289802eb9eSDmitry Chagin 
7299802eb9eSDmitry Chagin  out:
7309802eb9eSDmitry Chagin 	vrele(vp);
7319802eb9eSDmitry Chagin 	return (error);
7329802eb9eSDmitry Chagin }
733