xref: /freebsd/sys/compat/linux/linux_stats.c (revision 2362ad457a01d56d87e74823599578ab37bdbfb9)
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 
199*2362ad45SPhilippe Michaud-Boudreault static int
200*2362ad45SPhilippe Michaud-Boudreault statx_copyout(struct stat *buf, void *ubuf)
201*2362ad45SPhilippe Michaud-Boudreault {
202*2362ad45SPhilippe Michaud-Boudreault 	struct l_statx tbuf;
203*2362ad45SPhilippe Michaud-Boudreault 
204*2362ad45SPhilippe Michaud-Boudreault 	bzero(&tbuf, sizeof(tbuf));
205*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_mask = STATX_ALL;
206*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_blksize = buf->st_blksize;
207*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_attributes = 0;
208*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_nlink = buf->st_nlink;
209*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_uid = buf->st_uid;
210*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_gid = buf->st_gid;
211*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_mode = buf->st_mode;
212*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_ino = buf->st_ino;
213*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_size = buf->st_size;
214*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_blocks = buf->st_blocks;
215*2362ad45SPhilippe Michaud-Boudreault 
216*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_atime.tv_sec = buf->st_atim.tv_sec;
217*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_atime.tv_nsec = buf->st_atim.tv_nsec;
218*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_btime.tv_sec = buf->st_birthtim.tv_sec;
219*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_btime.tv_nsec = buf->st_birthtim.tv_nsec;
220*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_ctime.tv_sec = buf->st_ctim.tv_sec;
221*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_ctime.tv_nsec = buf->st_ctim.tv_nsec;
222*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_mtime.tv_sec = buf->st_mtim.tv_sec;
223*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_mtime.tv_nsec = buf->st_mtim.tv_nsec;
224*2362ad45SPhilippe Michaud-Boudreault 
225*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_rdev_major = buf->st_rdev >> 8;
226*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_rdev_minor = buf->st_rdev & 0xff;
227*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_dev_major = buf->st_dev >> 8;
228*2362ad45SPhilippe Michaud-Boudreault 	tbuf.stx_dev_minor = buf->st_dev & 0xff;
229*2362ad45SPhilippe Michaud-Boudreault 
230*2362ad45SPhilippe Michaud-Boudreault 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
231*2362ad45SPhilippe Michaud-Boudreault }
232*2362ad45SPhilippe Michaud-Boudreault 
233931e2a1aSEd Maste #ifdef LINUX_LEGACY_SYSCALLS
234c21dee17SSøren Schmidt int
235b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
236c21dee17SSøren Schmidt {
237c21dee17SSøren Schmidt 	struct stat buf;
238206a5d3aSIan Dowse 	char *path;
239c21dee17SSøren Schmidt 	int error;
240d66a5066SPeter Wemm 
241a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
242a125ed50SMateusz Guzik 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
243a125ed50SMateusz Guzik 	} else {
244206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->path, &path);
2450eee862aSEd Schouten 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
246206a5d3aSIan Dowse 		LFREEPATH(path);
247a125ed50SMateusz Guzik 	}
2484e0eaf69SMarcel Moolenaar 	if (error)
2494e0eaf69SMarcel Moolenaar 		return (error);
2504e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
251c21dee17SSøren Schmidt }
252c21dee17SSøren Schmidt 
253c21dee17SSøren Schmidt int
254b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
255c21dee17SSøren Schmidt {
2564e0eaf69SMarcel Moolenaar 	struct stat sb;
257206a5d3aSIan Dowse 	char *path;
258f7a25872SJohn Baldwin 	int error;
259d66a5066SPeter Wemm 
260a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
261a125ed50SMateusz Guzik 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &sb);
262a125ed50SMateusz Guzik 	} else {
263206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->path, &path);
2640eee862aSEd Schouten 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
265206a5d3aSIan Dowse 		LFREEPATH(path);
266a125ed50SMateusz Guzik 	}
267d66a5066SPeter Wemm 	if (error)
268d66a5066SPeter Wemm 		return (error);
2695002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
270d66a5066SPeter Wemm }
271931e2a1aSEd Maste #endif
272c21dee17SSøren Schmidt 
273c21dee17SSøren Schmidt int
274b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
275c21dee17SSøren Schmidt {
276c21dee17SSøren Schmidt 	struct stat buf;
277c21dee17SSøren Schmidt 	int error;
278c21dee17SSøren Schmidt 
279f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
280060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
281c21dee17SSøren Schmidt 	if (!error)
282c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
2834e0eaf69SMarcel Moolenaar 
2844e0eaf69SMarcel Moolenaar 	return (error);
285c21dee17SSøren Schmidt }
286c21dee17SSøren Schmidt 
2877f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2885c8919adSAlexander Leidinger static int
2895c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf)
2905c8919adSAlexander Leidinger {
2915c8919adSAlexander Leidinger 	struct l_stat lbuf;
2925c8919adSAlexander Leidinger 
2935c8919adSAlexander Leidinger 	bzero(&lbuf, sizeof(lbuf));
294ab35e1c7SBruce Evans 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
2955c8919adSAlexander Leidinger 	lbuf.st_ino = buf->st_ino;
2965c8919adSAlexander Leidinger 	lbuf.st_mode = buf->st_mode;
2975c8919adSAlexander Leidinger 	lbuf.st_nlink = buf->st_nlink;
2985c8919adSAlexander Leidinger 	lbuf.st_uid = buf->st_uid;
2995c8919adSAlexander Leidinger 	lbuf.st_gid = buf->st_gid;
3005c8919adSAlexander Leidinger 	lbuf.st_rdev = buf->st_rdev;
301372639f9SBruce Evans 	lbuf.st_size = MIN(buf->st_size, INT32_MAX);
302510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
303510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
304510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
305510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
306510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
307510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
3085c8919adSAlexander Leidinger 	lbuf.st_blksize = buf->st_blksize;
3095c8919adSAlexander Leidinger 	lbuf.st_blocks = buf->st_blocks;
3105c8919adSAlexander Leidinger 	lbuf.st_flags = buf->st_flags;
3115c8919adSAlexander Leidinger 	lbuf.st_gen = buf->st_gen;
3125c8919adSAlexander Leidinger 
3135c8919adSAlexander Leidinger 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
3145c8919adSAlexander Leidinger }
3155c8919adSAlexander Leidinger 
3165c8919adSAlexander Leidinger int
3175c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args)
3185c8919adSAlexander Leidinger {
3195c8919adSAlexander Leidinger 	struct stat buf;
32015b78ac5SKonstantin Belousov 	char *path;
3215c8919adSAlexander Leidinger 	int error;
32215b78ac5SKonstantin Belousov 
323a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
324a125ed50SMateusz Guzik 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
325a125ed50SMateusz Guzik 	} else {
32615b78ac5SKonstantin Belousov 		LCONVPATHEXIST(td, args->path, &path);
3270eee862aSEd Schouten 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
32815b78ac5SKonstantin Belousov 		LFREEPATH(path);
329a125ed50SMateusz Guzik 	}
330a125ed50SMateusz Guzik 	if (error) {
3315c8919adSAlexander Leidinger 		return (error);
332d075105dSKonstantin Belousov 	}
3335c8919adSAlexander Leidinger 	return (stat_copyout(&buf, args->up));
3345c8919adSAlexander Leidinger }
3355c8919adSAlexander Leidinger 
3365c8919adSAlexander Leidinger int
3375c8919adSAlexander Leidinger linux_lstat(struct thread *td, struct linux_lstat_args *args)
3385c8919adSAlexander Leidinger {
3395c8919adSAlexander Leidinger 	struct stat buf;
34015b78ac5SKonstantin Belousov 	char *path;
3415c8919adSAlexander Leidinger 	int error;
3425c8919adSAlexander Leidinger 
343a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
344a125ed50SMateusz Guzik 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &buf);
345a125ed50SMateusz Guzik 	} else {
34615b78ac5SKonstantin Belousov 		LCONVPATHEXIST(td, args->path, &path);
3470eee862aSEd Schouten 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
34815b78ac5SKonstantin Belousov 		LFREEPATH(path);
349a125ed50SMateusz Guzik 	}
350a125ed50SMateusz Guzik 	if (error) {
3515c8919adSAlexander Leidinger 		return (error);
352d075105dSKonstantin Belousov 	}
3535c8919adSAlexander Leidinger 	return (stat_copyout(&buf, args->up));
3545c8919adSAlexander Leidinger }
3557f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
3565c8919adSAlexander Leidinger 
3575002a60fSMarcel Moolenaar struct l_statfs {
358297f61ccSDmitry Chagin 	l_long		f_type;
359297f61ccSDmitry Chagin 	l_long		f_bsize;
360297f61ccSDmitry Chagin 	l_long		f_blocks;
361297f61ccSDmitry Chagin 	l_long		f_bfree;
362297f61ccSDmitry Chagin 	l_long		f_bavail;
363297f61ccSDmitry Chagin 	l_long		f_files;
364297f61ccSDmitry Chagin 	l_long		f_ffree;
3655002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
366297f61ccSDmitry Chagin 	l_long		f_namelen;
367e801ac78SEdward Tomasz Napierala 	l_long		f_frsize;
368e801ac78SEdward Tomasz Napierala 	l_long		f_flags;
369e801ac78SEdward Tomasz Napierala 	l_long		f_spare[4];
370c21dee17SSøren Schmidt };
371c21dee17SSøren Schmidt 
372dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
373dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
374dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
375dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
376dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
377dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
378dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
379dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
380dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
381dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
382dbaa9ebfSEd Maste #define	LINUX_ZFS_SUPER_MAGIC	0x2FC12FC1
383e83d253bSOlivier Houchard #define	LINUX_DEVFS_SUPER_MAGIC	0x1373L
3842166e4e0SDmitry Chagin #define	LINUX_SHMFS_MAGIC	0x01021994
385dca60efcSMarcel Moolenaar 
386dca60efcSMarcel Moolenaar static long
387962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
388dca60efcSMarcel Moolenaar {
389962cf420SMaxim Sobolev 	int i;
390962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
391962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
392dbaa9ebfSEd Maste 		{"zfs",     LINUX_ZFS_SUPER_MAGIC},
393962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
394962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
395962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
396962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
397962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
398962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
399962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
400962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
401962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
402e83d253bSOlivier Houchard 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
4032166e4e0SDmitry Chagin 		{"tmpfs",   LINUX_SHMFS_MAGIC},
404962cf420SMaxim Sobolev 		{NULL,      0L}};
405dca60efcSMarcel Moolenaar 
406962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
407962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
408962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
409dca60efcSMarcel Moolenaar 
410dca60efcSMarcel Moolenaar 	return (0L);
411dca60efcSMarcel Moolenaar }
412dca60efcSMarcel Moolenaar 
413525c9796SDmitry Chagin static int
414d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
415f7a25872SJohn Baldwin {
416525c9796SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
417525c9796SDmitry Chagin 	uint64_t tmp;
418f7a25872SJohn Baldwin 
419525c9796SDmitry Chagin #define	LINUX_HIBITS	0xffffffff00000000ULL
420525c9796SDmitry Chagin 
421525c9796SDmitry Chagin 	tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
422525c9796SDmitry Chagin 	    bsd_statfs->f_bsize;
423525c9796SDmitry Chagin 	if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
424525c9796SDmitry Chagin 	    (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
425525c9796SDmitry Chagin 	    (tmp & LINUX_HIBITS))
426525c9796SDmitry Chagin 		return (EOVERFLOW);
427525c9796SDmitry Chagin #undef	LINUX_HIBITS
428525c9796SDmitry Chagin #endif
429f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
430f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
431f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
432f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
433f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
434f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
435f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
436f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
437f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
438f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
439e801ac78SEdward Tomasz Napierala 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
440e801ac78SEdward Tomasz Napierala 	linux_statfs->f_flags = 0;
441e801ac78SEdward Tomasz Napierala 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
442525c9796SDmitry Chagin 
443525c9796SDmitry Chagin 	return (0);
444f7a25872SJohn Baldwin }
445f7a25872SJohn Baldwin 
446c21dee17SSøren Schmidt int
447b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
448c21dee17SSøren Schmidt {
4495002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
4502f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
451206a5d3aSIan Dowse 	char *path;
4522166e4e0SDmitry Chagin 	int error;
453d66a5066SPeter Wemm 
454a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
455a125ed50SMateusz Guzik 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
456a125ed50SMateusz Guzik 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
457a125ed50SMateusz Guzik 	} else {
458206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->path, &path);
4592f304845SKonstantin Belousov 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4602f304845SKonstantin Belousov 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
461206a5d3aSIan Dowse 		LFREEPATH(path);
462a125ed50SMateusz Guzik 	}
4632f304845SKonstantin Belousov 	if (error == 0)
4642f304845SKonstantin Belousov 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
4652f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
4662f304845SKonstantin Belousov 	if (error != 0)
4672ad02313SDmitry Chagin 		return (error);
4687958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
469c21dee17SSøren Schmidt }
470c21dee17SSøren Schmidt 
4717f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
472835e5061SAlexander Leidinger static void
473835e5061SAlexander Leidinger bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
474835e5061SAlexander Leidinger {
475835e5061SAlexander Leidinger 
476835e5061SAlexander Leidinger 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
477835e5061SAlexander Leidinger 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
478835e5061SAlexander Leidinger 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
479835e5061SAlexander Leidinger 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
480835e5061SAlexander Leidinger 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
481835e5061SAlexander Leidinger 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
482835e5061SAlexander Leidinger 	linux_statfs->f_files = bsd_statfs->f_files;
483835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
484835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
485835e5061SAlexander Leidinger 	linux_statfs->f_namelen = MAXNAMLEN;
486e801ac78SEdward Tomasz Napierala 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
487e801ac78SEdward Tomasz Napierala 	linux_statfs->f_flags = 0;
488e801ac78SEdward Tomasz Napierala 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
489835e5061SAlexander Leidinger }
490835e5061SAlexander Leidinger 
491835e5061SAlexander Leidinger int
492835e5061SAlexander Leidinger linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
493835e5061SAlexander Leidinger {
494835e5061SAlexander Leidinger 	struct l_statfs64 linux_statfs;
4952f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
496835e5061SAlexander Leidinger 	char *path;
497835e5061SAlexander Leidinger 	int error;
498835e5061SAlexander Leidinger 
4993ab85269SDavid Malone 	if (args->bufsize != sizeof(struct l_statfs64))
500340f4a8dSEd Maste 		return (EINVAL);
5013ab85269SDavid Malone 
502a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
503a125ed50SMateusz Guzik 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
504a125ed50SMateusz Guzik 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
505a125ed50SMateusz Guzik 	} else {
506835e5061SAlexander Leidinger 		LCONVPATHEXIST(td, args->path, &path);
5072f304845SKonstantin Belousov 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
5082f304845SKonstantin Belousov 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
509835e5061SAlexander Leidinger 		LFREEPATH(path);
510a125ed50SMateusz Guzik 	}
5112f304845SKonstantin Belousov 	if (error == 0)
5122f304845SKonstantin Belousov 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
5132f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
5142f304845SKonstantin Belousov 	if (error != 0)
515835e5061SAlexander Leidinger 		return (error);
5167958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
517835e5061SAlexander Leidinger }
51899546279SDmitry Chagin 
51999546279SDmitry Chagin int
52099546279SDmitry Chagin linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
52199546279SDmitry Chagin {
52299546279SDmitry Chagin 	struct l_statfs64 linux_statfs;
5232f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
52499546279SDmitry Chagin 	int error;
52599546279SDmitry Chagin 
52699546279SDmitry Chagin 	if (args->bufsize != sizeof(struct l_statfs64))
52799546279SDmitry Chagin 		return (EINVAL);
52899546279SDmitry Chagin 
5292f304845SKonstantin Belousov 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
5302f304845SKonstantin Belousov 	error = kern_fstatfs(td, args->fd, bsd_statfs);
5312f304845SKonstantin Belousov 	if (error == 0)
5322f304845SKonstantin Belousov 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
5332f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
5342f304845SKonstantin Belousov 	if (error != 0)
5352f304845SKonstantin Belousov 		return (error);
53699546279SDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
53799546279SDmitry Chagin }
5387f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
539835e5061SAlexander Leidinger 
540c21dee17SSøren Schmidt int
541b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
542c21dee17SSøren Schmidt {
5435002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
5442f304845SKonstantin Belousov 	struct statfs *bsd_statfs;
545c21dee17SSøren Schmidt 	int error;
546c21dee17SSøren Schmidt 
5472f304845SKonstantin Belousov 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
5482f304845SKonstantin Belousov 	error = kern_fstatfs(td, args->fd, bsd_statfs);
5492f304845SKonstantin Belousov 	if (error == 0)
5502f304845SKonstantin Belousov 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
5512f304845SKonstantin Belousov 	free(bsd_statfs, M_STATFS);
5522f304845SKonstantin Belousov 	if (error != 0)
5532ad02313SDmitry Chagin 		return (error);
5547958a34cSDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
555c21dee17SSøren Schmidt }
556408da119SMarcel Moolenaar 
5575002a60fSMarcel Moolenaar struct l_ustat
558408da119SMarcel Moolenaar {
5595002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
5605002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
5615002a60fSMarcel Moolenaar 	char		f_fname[6];
5625002a60fSMarcel Moolenaar 	char		f_fpack[6];
5635002a60fSMarcel Moolenaar };
5645002a60fSMarcel Moolenaar 
565931e2a1aSEd Maste #ifdef LINUX_LEGACY_SYSCALLS
5665002a60fSMarcel Moolenaar int
567b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
5685002a60fSMarcel Moolenaar {
5691e247cc2SPoul-Henning Kamp 
5701e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
571408da119SMarcel Moolenaar }
572931e2a1aSEd Maste #endif
5735002a60fSMarcel Moolenaar 
5741997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
5755002a60fSMarcel Moolenaar 
5765002a60fSMarcel Moolenaar static int
5775002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
5785002a60fSMarcel Moolenaar {
5795002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
5805002a60fSMarcel Moolenaar 
5815002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
582ab35e1c7SBruce Evans 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
5835002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
5845002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
5855002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
5865002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
5875002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
5885002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
5895002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
590510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
591510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
592510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
593510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
594510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
595510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
5965002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
5975002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
5985002a60fSMarcel Moolenaar 
5995002a60fSMarcel Moolenaar 	/*
6005002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
6015002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
6025002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
6035002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
6045002a60fSMarcel Moolenaar 	 */
6055002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
6065002a60fSMarcel Moolenaar 
6075002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
6085002a60fSMarcel Moolenaar }
6095002a60fSMarcel Moolenaar 
6105002a60fSMarcel Moolenaar int
611b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
6125002a60fSMarcel Moolenaar {
6135002a60fSMarcel Moolenaar 	struct stat buf;
614206a5d3aSIan Dowse 	char *filename;
615f7a25872SJohn Baldwin 	int error;
6165002a60fSMarcel Moolenaar 
617a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
618a125ed50SMateusz Guzik 		error = linux_kern_stat(td, args->filename, UIO_USERSPACE, &buf);
619a125ed50SMateusz Guzik 	} else {
620206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->filename, &filename);
6210eee862aSEd Schouten 		error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
622206a5d3aSIan Dowse 		LFREEPATH(filename);
623a125ed50SMateusz Guzik 	}
6245002a60fSMarcel Moolenaar 	if (error)
6255002a60fSMarcel Moolenaar 		return (error);
6265002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
6275002a60fSMarcel Moolenaar }
6285002a60fSMarcel Moolenaar 
6295002a60fSMarcel Moolenaar int
630b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
6315002a60fSMarcel Moolenaar {
6325002a60fSMarcel Moolenaar 	struct stat sb;
633206a5d3aSIan Dowse 	char *filename;
634f7a25872SJohn Baldwin 	int error;
6355002a60fSMarcel Moolenaar 
636a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
637a125ed50SMateusz Guzik 		error = linux_kern_lstat(td, args->filename, UIO_USERSPACE, &sb);
638a125ed50SMateusz Guzik 	} else {
639206a5d3aSIan Dowse 		LCONVPATHEXIST(td, args->filename, &filename);
6400eee862aSEd Schouten 		error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
641206a5d3aSIan Dowse 		LFREEPATH(filename);
642a125ed50SMateusz Guzik 	}
6435002a60fSMarcel Moolenaar 	if (error)
6445002a60fSMarcel Moolenaar 		return (error);
6455002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
6465002a60fSMarcel Moolenaar }
6475002a60fSMarcel Moolenaar 
6485002a60fSMarcel Moolenaar int
649b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
6505002a60fSMarcel Moolenaar {
6515002a60fSMarcel Moolenaar 	struct stat buf;
6525002a60fSMarcel Moolenaar 	int error;
6535002a60fSMarcel Moolenaar 
654f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
655060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
6565002a60fSMarcel Moolenaar 	if (!error)
6575002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
6585002a60fSMarcel Moolenaar 
6595002a60fSMarcel Moolenaar 	return (error);
6605002a60fSMarcel Moolenaar }
6615002a60fSMarcel Moolenaar 
66248b05c3fSKonstantin Belousov int
66348b05c3fSKonstantin Belousov linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
66448b05c3fSKonstantin Belousov {
66548b05c3fSKonstantin Belousov 	char *path;
66648b05c3fSKonstantin Belousov 	int error, dfd, flag;
66748b05c3fSKonstantin Belousov 	struct stat buf;
66848b05c3fSKonstantin Belousov 
6694b45c2bbSEdward Tomasz Napierala 	if (args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH)) {
6704b45c2bbSEdward Tomasz Napierala 		linux_msg(td, "fstatat64 unsupported flag 0x%x", args->flag);
67148b05c3fSKonstantin Belousov 		return (EINVAL);
6724b45c2bbSEdward Tomasz Napierala 	}
67348b05c3fSKonstantin Belousov 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
67448b05c3fSKonstantin Belousov 	    AT_SYMLINK_NOFOLLOW : 0;
6754b45c2bbSEdward Tomasz Napierala 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
6764b45c2bbSEdward Tomasz Napierala 	    AT_EMPTY_PATH : 0;
67748b05c3fSKonstantin Belousov 
67848b05c3fSKonstantin Belousov 	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 {
68348b05c3fSKonstantin Belousov 		LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
6840eee862aSEd Schouten 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
68548b05c3fSKonstantin Belousov 		LFREEPATH(path);
686a125ed50SMateusz Guzik 	}
687a125ed50SMateusz Guzik 	if (error == 0)
688a125ed50SMateusz Guzik 		error = stat64_copyout(&buf, args->statbuf);
68948b05c3fSKonstantin Belousov 
69048b05c3fSKonstantin Belousov 	return (error);
69148b05c3fSKonstantin Belousov }
69248b05c3fSKonstantin Belousov 
693606bcc17SDmitry Chagin #else /* __amd64__ && !COMPAT_LINUX32 */
694606bcc17SDmitry Chagin 
695606bcc17SDmitry Chagin int
696606bcc17SDmitry Chagin linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
697606bcc17SDmitry Chagin {
698606bcc17SDmitry Chagin 	char *path;
699606bcc17SDmitry Chagin 	int error, dfd, flag;
700606bcc17SDmitry Chagin 	struct stat buf;
701606bcc17SDmitry Chagin 
7024b45c2bbSEdward Tomasz Napierala 	if (args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH)) {
7034b45c2bbSEdward Tomasz Napierala 		linux_msg(td, "fstatat unsupported flag 0x%x", args->flag);
704606bcc17SDmitry Chagin 		return (EINVAL);
7054b45c2bbSEdward Tomasz Napierala 	}
7064b45c2bbSEdward Tomasz Napierala 
707606bcc17SDmitry Chagin 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
708606bcc17SDmitry Chagin 	    AT_SYMLINK_NOFOLLOW : 0;
7094b45c2bbSEdward Tomasz Napierala 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
7104b45c2bbSEdward Tomasz Napierala 	    AT_EMPTY_PATH : 0;
711606bcc17SDmitry Chagin 
712606bcc17SDmitry Chagin 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
713a125ed50SMateusz Guzik 	if (!LUSECONVPATH(td)) {
714a125ed50SMateusz Guzik 		error = linux_kern_statat(td, flag, dfd, args->pathname,
715a125ed50SMateusz Guzik 		    UIO_USERSPACE, &buf);
716a125ed50SMateusz Guzik 	} else {
717606bcc17SDmitry Chagin 		LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
718606bcc17SDmitry Chagin 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
719a125ed50SMateusz Guzik 		LFREEPATH(path);
720a125ed50SMateusz Guzik 	}
721606bcc17SDmitry Chagin 	if (error == 0)
722606bcc17SDmitry Chagin 		error = newstat_copyout(&buf, args->statbuf);
723606bcc17SDmitry Chagin 
724606bcc17SDmitry Chagin 	return (error);
725606bcc17SDmitry Chagin }
726606bcc17SDmitry Chagin 
7271997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
7289802eb9eSDmitry Chagin 
7299802eb9eSDmitry Chagin int
7309802eb9eSDmitry Chagin linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
7319802eb9eSDmitry Chagin {
7329802eb9eSDmitry Chagin 	struct mount *mp;
7339802eb9eSDmitry Chagin 	struct vnode *vp;
7349802eb9eSDmitry Chagin 	int error, save;
7359802eb9eSDmitry Chagin 
736cbd92ce6SMatt Macy 	error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
7379802eb9eSDmitry Chagin 	if (error != 0)
7389802eb9eSDmitry Chagin 		/*
7399802eb9eSDmitry Chagin 		 * Linux syncfs() returns only EBADF, however fgetvp()
7409802eb9eSDmitry Chagin 		 * can return EINVAL in case of file descriptor does
7419802eb9eSDmitry Chagin 		 * not represent a vnode. XXX.
7429802eb9eSDmitry Chagin 		 */
7439802eb9eSDmitry Chagin 		return (error);
7449802eb9eSDmitry Chagin 
7459802eb9eSDmitry Chagin 	mp = vp->v_mount;
7469802eb9eSDmitry Chagin 	mtx_lock(&mountlist_mtx);
7479802eb9eSDmitry Chagin 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
7489802eb9eSDmitry Chagin 	if (error != 0) {
7499802eb9eSDmitry Chagin 		/* See comment above. */
7509802eb9eSDmitry Chagin 		mtx_unlock(&mountlist_mtx);
7519802eb9eSDmitry Chagin 		goto out;
7529802eb9eSDmitry Chagin 	}
7539802eb9eSDmitry Chagin 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
7549802eb9eSDmitry Chagin 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
7559802eb9eSDmitry Chagin 		save = curthread_pflags_set(TDP_SYNCIO);
756c8b3463dSMateusz Guzik 		vfs_periodic(mp, MNT_NOWAIT);
7579802eb9eSDmitry Chagin 		VFS_SYNC(mp, MNT_NOWAIT);
7589802eb9eSDmitry Chagin 		curthread_pflags_restore(save);
7599802eb9eSDmitry Chagin 		vn_finished_write(mp);
7609802eb9eSDmitry Chagin 	}
7619802eb9eSDmitry Chagin 	vfs_unbusy(mp);
7629802eb9eSDmitry Chagin 
7639802eb9eSDmitry Chagin  out:
7649802eb9eSDmitry Chagin 	vrele(vp);
7659802eb9eSDmitry Chagin 	return (error);
7669802eb9eSDmitry Chagin }
767*2362ad45SPhilippe Michaud-Boudreault 
768*2362ad45SPhilippe Michaud-Boudreault int
769*2362ad45SPhilippe Michaud-Boudreault linux_statx(struct thread *td, struct linux_statx_args *args)
770*2362ad45SPhilippe Michaud-Boudreault {
771*2362ad45SPhilippe Michaud-Boudreault 	char *path;
772*2362ad45SPhilippe Michaud-Boudreault 	int error, dirfd, flags;
773*2362ad45SPhilippe Michaud-Boudreault 	struct stat buf;
774*2362ad45SPhilippe Michaud-Boudreault 
775*2362ad45SPhilippe Michaud-Boudreault 	if (args->flags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH)) {
776*2362ad45SPhilippe Michaud-Boudreault 		linux_msg(td, "statx unsupported flags 0x%x", args->flags);
777*2362ad45SPhilippe Michaud-Boudreault 		return (EINVAL);
778*2362ad45SPhilippe Michaud-Boudreault 	}
779*2362ad45SPhilippe Michaud-Boudreault 
780*2362ad45SPhilippe Michaud-Boudreault 	flags = (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) ?
781*2362ad45SPhilippe Michaud-Boudreault 	    AT_SYMLINK_NOFOLLOW : 0;
782*2362ad45SPhilippe Michaud-Boudreault 	flags |= (args->flags & LINUX_AT_EMPTY_PATH) ?
783*2362ad45SPhilippe Michaud-Boudreault 	    AT_EMPTY_PATH : 0;
784*2362ad45SPhilippe Michaud-Boudreault 
785*2362ad45SPhilippe Michaud-Boudreault 	dirfd = (args->dirfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dirfd;
786*2362ad45SPhilippe Michaud-Boudreault 	if (!LUSECONVPATH(td)) {
787*2362ad45SPhilippe Michaud-Boudreault 		error = linux_kern_statat(td, flags, dirfd, args->pathname,
788*2362ad45SPhilippe Michaud-Boudreault 		    UIO_USERSPACE, &buf);
789*2362ad45SPhilippe Michaud-Boudreault 	} else {
790*2362ad45SPhilippe Michaud-Boudreault 		LCONVPATHEXIST_AT(td, args->pathname, &path, dirfd);
791*2362ad45SPhilippe Michaud-Boudreault 		error = linux_kern_statat(td, flags, dirfd, path, UIO_SYSSPACE, &buf);
792*2362ad45SPhilippe Michaud-Boudreault 		LFREEPATH(path);
793*2362ad45SPhilippe Michaud-Boudreault 	}
794*2362ad45SPhilippe Michaud-Boudreault 	if (error == 0)
795*2362ad45SPhilippe Michaud-Boudreault 		error = statx_copyout(&buf, args->statxbuf);
796*2362ad45SPhilippe Michaud-Boudreault 
797*2362ad45SPhilippe Michaud-Boudreault 	return (error);
798*2362ad45SPhilippe Michaud-Boudreault }
799*2362ad45SPhilippe Michaud-Boudreault 
800