xref: /freebsd/sys/compat/linux/linux_stats.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_ktrace.h"
31 
32 #include <sys/param.h>
33 #include <sys/capsicum.h>
34 #include <sys/dirent.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/namei.h>
39 #include <sys/proc.h>
40 #include <sys/stat.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/tty.h>
43 #include <sys/vnode.h>
44 #ifdef KTRACE
45 #include <sys/ktrace.h>
46 #endif
47 
48 #include <security/audit/audit.h>
49 
50 #ifdef COMPAT_LINUX32
51 #include <machine/../linux32/linux.h>
52 #include <machine/../linux32/linux32_proto.h>
53 #else
54 #include <machine/../linux/linux.h>
55 #include <machine/../linux/linux_proto.h>
56 #endif
57 
58 #include <compat/linux/linux.h>
59 #include <compat/linux/linux_file.h>
60 #include <compat/linux/linux_util.h>
61 
62 
63 static int
64 linux_kern_fstat(struct thread *td, int fd, struct stat *sbp)
65 {
66 	struct vnode *vp;
67 	struct file *fp;
68 	int error;
69 
70 	AUDIT_ARG_FD(fd);
71 
72 	error = fget(td, fd, &cap_fstat_rights, &fp);
73 	if (__predict_false(error != 0))
74 		return (error);
75 
76 	AUDIT_ARG_FILE(td->td_proc, fp);
77 
78 	error = fo_stat(fp, sbp, td->td_ucred);
79 	if (error == 0 && (vp = fp->f_vnode) != NULL)
80 		translate_vnhook_major_minor(vp, sbp);
81 	fdrop(fp, td);
82 #ifdef KTRACE
83 	if (KTRPOINT(td, KTR_STRUCT))
84 		ktrstat_error(sbp, error);
85 #endif
86 	return (error);
87 }
88 
89 static int
90 linux_kern_statat(struct thread *td, int flag, int fd, const char *path,
91     enum uio_seg pathseg, struct stat *sbp)
92 {
93 	struct nameidata nd;
94 	int error;
95 
96 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
97 	    AT_EMPTY_PATH)) != 0)
98 		return (EINVAL);
99 
100 	NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
101 	    AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
102 	    AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
103 
104 	if ((error = namei(&nd)) != 0) {
105 		if (error == ENOTDIR &&
106 		    (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
107 			error = linux_kern_fstat(td, fd, sbp);
108 		return (error);
109 	}
110 	error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
111 	if (error == 0)
112 		translate_vnhook_major_minor(nd.ni_vp, sbp);
113 	NDFREE_PNBUF(&nd);
114 	vput(nd.ni_vp);
115 #ifdef KTRACE
116 	if (KTRPOINT(td, KTR_STRUCT))
117 		ktrstat_error(sbp, error);
118 #endif
119 	return (error);
120 }
121 
122 #ifdef LINUX_LEGACY_SYSCALLS
123 static int
124 linux_kern_stat(struct thread *td, const char *path, enum uio_seg pathseg,
125     struct stat *sbp)
126 {
127 
128 	return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
129 }
130 
131 static int
132 linux_kern_lstat(struct thread *td, const char *path, enum uio_seg pathseg,
133     struct stat *sbp)
134 {
135 
136 	return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
137 	    pathseg, sbp));
138 }
139 #endif
140 
141 static int
142 newstat_copyout(struct stat *buf, void *ubuf)
143 {
144 	struct l_newstat tbuf;
145 
146 	bzero(&tbuf, sizeof(tbuf));
147 	tbuf.st_dev = linux_new_encode_dev(buf->st_dev);
148 	tbuf.st_ino = buf->st_ino;
149 	tbuf.st_mode = buf->st_mode;
150 	tbuf.st_nlink = buf->st_nlink;
151 	tbuf.st_uid = buf->st_uid;
152 	tbuf.st_gid = buf->st_gid;
153 	tbuf.st_rdev = linux_new_encode_dev(buf->st_rdev);
154 	tbuf.st_size = buf->st_size;
155 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
156 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
157 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
158 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
159 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
160 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
161 	tbuf.st_blksize = buf->st_blksize;
162 	tbuf.st_blocks = buf->st_blocks;
163 
164 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
165 }
166 
167 
168 #ifdef LINUX_LEGACY_SYSCALLS
169 int
170 linux_newstat(struct thread *td, struct linux_newstat_args *args)
171 {
172 	struct stat buf;
173 	int error;
174 
175 	error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
176 	if (error)
177 		return (error);
178 	return (newstat_copyout(&buf, args->buf));
179 }
180 
181 int
182 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
183 {
184 	struct stat sb;
185 	int error;
186 
187 	error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &sb);
188 	if (error)
189 		return (error);
190 	return (newstat_copyout(&sb, args->buf));
191 }
192 #endif
193 
194 int
195 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
196 {
197 	struct stat buf;
198 	int error;
199 
200 	error = linux_kern_fstat(td, args->fd, &buf);
201 	if (!error)
202 		error = newstat_copyout(&buf, args->buf);
203 
204 	return (error);
205 }
206 
207 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
208 
209 static __inline uint16_t
210 linux_old_encode_dev(dev_t _dev)
211 {
212 
213 	return (_dev == NODEV ? 0 : linux_encode_dev(major(_dev), minor(_dev)));
214 }
215 
216 static int
217 old_stat_copyout(struct stat *buf, void *ubuf)
218 {
219 	struct l_old_stat lbuf;
220 
221 	bzero(&lbuf, sizeof(lbuf));
222 	lbuf.st_dev = linux_old_encode_dev(buf->st_dev);
223 	lbuf.st_ino = buf->st_ino;
224 	lbuf.st_mode = buf->st_mode;
225 	lbuf.st_nlink = buf->st_nlink;
226 	lbuf.st_uid = buf->st_uid;
227 	lbuf.st_gid = buf->st_gid;
228 	lbuf.st_rdev = linux_old_encode_dev(buf->st_rdev);
229 	lbuf.st_size = MIN(buf->st_size, INT32_MAX);
230 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
231 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
232 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
233 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
234 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
235 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
236 	lbuf.st_blksize = buf->st_blksize;
237 	lbuf.st_blocks = buf->st_blocks;
238 	lbuf.st_flags = buf->st_flags;
239 	lbuf.st_gen = buf->st_gen;
240 
241 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
242 }
243 
244 int
245 linux_stat(struct thread *td, struct linux_stat_args *args)
246 {
247 	struct stat buf;
248 	int error;
249 
250 	error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
251 	if (error) {
252 		return (error);
253 	}
254 	return (old_stat_copyout(&buf, args->up));
255 }
256 
257 int
258 linux_lstat(struct thread *td, struct linux_lstat_args *args)
259 {
260 	struct stat buf;
261 	int error;
262 
263 	error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &buf);
264 	if (error) {
265 		return (error);
266 	}
267 	return (old_stat_copyout(&buf, args->up));
268 }
269 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
270 
271 struct l_statfs {
272 	l_long		f_type;
273 	l_long		f_bsize;
274 	l_long		f_blocks;
275 	l_long		f_bfree;
276 	l_long		f_bavail;
277 	l_long		f_files;
278 	l_long		f_ffree;
279 	l_fsid_t	f_fsid;
280 	l_long		f_namelen;
281 	l_long		f_frsize;
282 	l_long		f_flags;
283 	l_long		f_spare[4];
284 };
285 
286 #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
287 #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
288 #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
289 #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
290 #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
291 #define	LINUX_NCP_SUPER_MAGIC	0x564cL
292 #define	LINUX_NFS_SUPER_MAGIC	0x6969L
293 #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
294 #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
295 #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
296 #define	LINUX_ZFS_SUPER_MAGIC	0x2FC12FC1
297 #define	LINUX_DEVFS_SUPER_MAGIC	0x1373L
298 #define	LINUX_SHMFS_MAGIC	0x01021994
299 
300 static long
301 bsd_to_linux_ftype(const char *fstypename)
302 {
303 	int i;
304 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
305 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
306 		{"zfs",     LINUX_ZFS_SUPER_MAGIC},
307 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
308 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
309 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
310 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
311 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
312 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
313 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
314 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
315 		{"coda",    LINUX_CODA_SUPER_MAGIC},
316 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
317 		{"tmpfs",   LINUX_SHMFS_MAGIC},
318 		{NULL,      0L}};
319 
320 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
321 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
322 			return (b2l_tbl[i].linux_type);
323 
324 	return (0L);
325 }
326 
327 static int
328 bsd_to_linux_mnt_flags(int f_flags)
329 {
330 	int flags = LINUX_ST_VALID;
331 
332 	if (f_flags & MNT_RDONLY)
333 		flags |= LINUX_ST_RDONLY;
334 	if (f_flags & MNT_NOEXEC)
335 		flags |= LINUX_ST_NOEXEC;
336 	if (f_flags & MNT_NOSUID)
337 		flags |= LINUX_ST_NOSUID;
338 	if (f_flags & MNT_NOATIME)
339 		flags |= LINUX_ST_NOATIME;
340 	if (f_flags & MNT_NOSYMFOLLOW)
341 		flags |= LINUX_ST_NOSYMFOLLOW;
342 	if (f_flags & MNT_SYNCHRONOUS)
343 		flags |= LINUX_ST_SYNCHRONOUS;
344 
345 	return (flags);
346 }
347 
348 static int
349 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
350 {
351 
352 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
353 	statfs_scale_blocks(bsd_statfs, INT32_MAX);
354 #endif
355 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
356 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
357 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
358 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
359 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
360 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
361 	linux_statfs->f_ffree = MIN(bsd_statfs->f_ffree, INT32_MAX);
362 	linux_statfs->f_files = MIN(bsd_statfs->f_files, INT32_MAX);
363 #else
364 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
365 	linux_statfs->f_files = bsd_statfs->f_files;
366 #endif
367 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
368 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
369 	linux_statfs->f_namelen = MAXNAMLEN;
370 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
371 	linux_statfs->f_flags = bsd_to_linux_mnt_flags(bsd_statfs->f_flags);
372 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
373 
374 	return (0);
375 }
376 
377 int
378 linux_statfs(struct thread *td, struct linux_statfs_args *args)
379 {
380 	struct l_statfs linux_statfs;
381 	struct statfs *bsd_statfs;
382 	int error;
383 
384 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
385 	error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
386 	if (error == 0)
387 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
388 	free(bsd_statfs, M_STATFS);
389 	if (error != 0)
390 		return (error);
391 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
392 }
393 
394 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
395 static void
396 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
397 {
398 
399 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
400 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
401 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
402 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
403 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
404 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
405 	linux_statfs->f_files = bsd_statfs->f_files;
406 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
407 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
408 	linux_statfs->f_namelen = MAXNAMLEN;
409 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
410 	linux_statfs->f_flags = bsd_to_linux_mnt_flags(bsd_statfs->f_flags);
411 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
412 }
413 
414 int
415 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
416 {
417 	struct l_statfs64 linux_statfs;
418 	struct statfs *bsd_statfs;
419 	int error;
420 
421 	if (args->bufsize != sizeof(struct l_statfs64))
422 		return (EINVAL);
423 
424 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
425 	error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
426 	if (error == 0)
427 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
428 	free(bsd_statfs, M_STATFS);
429 	if (error != 0)
430 		return (error);
431 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
432 }
433 
434 int
435 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
436 {
437 	struct l_statfs64 linux_statfs;
438 	struct statfs *bsd_statfs;
439 	int error;
440 
441 	if (args->bufsize != sizeof(struct l_statfs64))
442 		return (EINVAL);
443 
444 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
445 	error = kern_fstatfs(td, args->fd, bsd_statfs);
446 	if (error == 0)
447 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
448 	free(bsd_statfs, M_STATFS);
449 	if (error != 0)
450 		return (error);
451 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
452 }
453 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
454 
455 int
456 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
457 {
458 	struct l_statfs linux_statfs;
459 	struct statfs *bsd_statfs;
460 	int error;
461 
462 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
463 	error = kern_fstatfs(td, args->fd, bsd_statfs);
464 	if (error == 0)
465 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
466 	free(bsd_statfs, M_STATFS);
467 	if (error != 0)
468 		return (error);
469 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
470 }
471 
472 struct l_ustat
473 {
474 	l_daddr_t	f_tfree;
475 	l_ino_t		f_tinode;
476 	char		f_fname[6];
477 	char		f_fpack[6];
478 };
479 
480 #ifdef LINUX_LEGACY_SYSCALLS
481 int
482 linux_ustat(struct thread *td, struct linux_ustat_args *args)
483 {
484 
485 	return (EOPNOTSUPP);
486 }
487 #endif
488 
489 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
490 
491 static int
492 stat64_copyout(struct stat *buf, void *ubuf)
493 {
494 	struct l_stat64 lbuf;
495 
496 	bzero(&lbuf, sizeof(lbuf));
497 	lbuf.st_dev = linux_new_encode_dev(buf->st_dev);
498 	lbuf.st_ino = buf->st_ino;
499 	lbuf.st_mode = buf->st_mode;
500 	lbuf.st_nlink = buf->st_nlink;
501 	lbuf.st_uid = buf->st_uid;
502 	lbuf.st_gid = buf->st_gid;
503 	lbuf.st_rdev = linux_new_encode_dev(buf->st_rdev);
504 	lbuf.st_size = buf->st_size;
505 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
506 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
507 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
508 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
509 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
510 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
511 	lbuf.st_blksize = buf->st_blksize;
512 	lbuf.st_blocks = buf->st_blocks;
513 
514 	/*
515 	 * The __st_ino field makes all the difference. In the Linux kernel
516 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
517 	 * but without the assignment to __st_ino the runtime linker refuses
518 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
519 	 */
520 	lbuf.__st_ino = buf->st_ino;
521 
522 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
523 }
524 
525 int
526 linux_stat64(struct thread *td, struct linux_stat64_args *args)
527 {
528 	struct stat buf;
529 	int error;
530 
531 	error = linux_kern_stat(td, args->filename, UIO_USERSPACE, &buf);
532 	if (error)
533 		return (error);
534 	return (stat64_copyout(&buf, args->statbuf));
535 }
536 
537 int
538 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
539 {
540 	struct stat sb;
541 	int error;
542 
543 	error = linux_kern_lstat(td, args->filename, UIO_USERSPACE, &sb);
544 	if (error)
545 		return (error);
546 	return (stat64_copyout(&sb, args->statbuf));
547 }
548 
549 int
550 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
551 {
552 	struct stat buf;
553 	int error;
554 
555 	error = linux_kern_fstat(td, args->fd, &buf);
556 	if (!error)
557 		error = stat64_copyout(&buf, args->statbuf);
558 
559 	return (error);
560 }
561 
562 int
563 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
564 {
565 	int error, dfd, flag, unsupported;
566 	struct stat buf;
567 
568 	unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
569 	if (unsupported != 0) {
570 		linux_msg(td, "fstatat64 unsupported flag 0x%x", unsupported);
571 		return (EINVAL);
572 	}
573 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
574 	    AT_SYMLINK_NOFOLLOW : 0;
575 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
576 	    AT_EMPTY_PATH : 0;
577 
578 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
579 	error = linux_kern_statat(td, flag, dfd, args->pathname,
580 	    UIO_USERSPACE, &buf);
581 	if (error == 0)
582 		error = stat64_copyout(&buf, args->statbuf);
583 
584 	return (error);
585 }
586 
587 #else /* __amd64__ && !COMPAT_LINUX32 */
588 
589 int
590 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
591 {
592 	int error, dfd, flag, unsupported;
593 	struct stat buf;
594 
595 	unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
596 	if (unsupported != 0) {
597 		linux_msg(td, "fstatat unsupported flag 0x%x", unsupported);
598 		return (EINVAL);
599 	}
600 
601 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
602 	    AT_SYMLINK_NOFOLLOW : 0;
603 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
604 	    AT_EMPTY_PATH : 0;
605 
606 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
607 	error = linux_kern_statat(td, flag, dfd, args->pathname,
608 	    UIO_USERSPACE, &buf);
609 	if (error == 0)
610 		error = newstat_copyout(&buf, args->statbuf);
611 
612 	return (error);
613 }
614 
615 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
616 
617 int
618 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
619 {
620 	struct mount *mp;
621 	struct vnode *vp;
622 	int error, save;
623 
624 	error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
625 	if (error != 0)
626 		/*
627 		 * Linux syncfs() returns only EBADF, however fgetvp()
628 		 * can return EINVAL in case of file descriptor does
629 		 * not represent a vnode. XXX.
630 		 */
631 		return (error);
632 
633 	mp = vp->v_mount;
634 	mtx_lock(&mountlist_mtx);
635 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
636 	if (error != 0) {
637 		/* See comment above. */
638 		mtx_unlock(&mountlist_mtx);
639 		goto out;
640 	}
641 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
642 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
643 		save = curthread_pflags_set(TDP_SYNCIO);
644 		vfs_periodic(mp, MNT_NOWAIT);
645 		VFS_SYNC(mp, MNT_NOWAIT);
646 		curthread_pflags_restore(save);
647 		vn_finished_write(mp);
648 	}
649 	vfs_unbusy(mp);
650 
651  out:
652 	vrele(vp);
653 	return (error);
654 }
655 
656 static int
657 statx_copyout(struct stat *buf, void *ubuf)
658 {
659 	struct l_statx tbuf;
660 
661 	bzero(&tbuf, sizeof(tbuf));
662 	tbuf.stx_mask = STATX_ALL;
663 	tbuf.stx_blksize = buf->st_blksize;
664 	tbuf.stx_attributes = 0;
665 	tbuf.stx_nlink = buf->st_nlink;
666 	tbuf.stx_uid = buf->st_uid;
667 	tbuf.stx_gid = buf->st_gid;
668 	tbuf.stx_mode = buf->st_mode;
669 	tbuf.stx_ino = buf->st_ino;
670 	tbuf.stx_size = buf->st_size;
671 	tbuf.stx_blocks = buf->st_blocks;
672 
673 	tbuf.stx_atime.tv_sec = buf->st_atim.tv_sec;
674 	tbuf.stx_atime.tv_nsec = buf->st_atim.tv_nsec;
675 	tbuf.stx_btime.tv_sec = buf->st_birthtim.tv_sec;
676 	tbuf.stx_btime.tv_nsec = buf->st_birthtim.tv_nsec;
677 	tbuf.stx_ctime.tv_sec = buf->st_ctim.tv_sec;
678 	tbuf.stx_ctime.tv_nsec = buf->st_ctim.tv_nsec;
679 	tbuf.stx_mtime.tv_sec = buf->st_mtim.tv_sec;
680 	tbuf.stx_mtime.tv_nsec = buf->st_mtim.tv_nsec;
681 	tbuf.stx_rdev_major = linux_encode_major(buf->st_rdev);
682 	tbuf.stx_rdev_minor = linux_encode_minor(buf->st_rdev);
683 	tbuf.stx_dev_major = linux_encode_major(buf->st_dev);
684 	tbuf.stx_dev_minor = linux_encode_minor(buf->st_dev);
685 
686 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
687 }
688 
689 int
690 linux_statx(struct thread *td, struct linux_statx_args *args)
691 {
692 	int error, dirfd, flags, unsupported;
693 	struct stat buf;
694 
695 	unsupported = args->flags & ~(LINUX_AT_SYMLINK_NOFOLLOW |
696 	    LINUX_AT_EMPTY_PATH | LINUX_AT_NO_AUTOMOUNT);
697 	if (unsupported != 0) {
698 		linux_msg(td, "statx unsupported flags 0x%x", unsupported);
699 		return (EINVAL);
700 	}
701 
702 	flags = (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) ?
703 	    AT_SYMLINK_NOFOLLOW : 0;
704 	flags |= (args->flags & LINUX_AT_EMPTY_PATH) ?
705 	    AT_EMPTY_PATH : 0;
706 
707 	dirfd = (args->dirfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dirfd;
708 	error = linux_kern_statat(td, flags, dirfd, args->pathname,
709 	    UIO_USERSPACE, &buf);
710 	if (error == 0)
711 		error = statx_copyout(&buf, args->statxbuf);
712 
713 	return (error);
714 }
715