xref: /freebsd/sys/compat/linux/linux_stats.c (revision f374ba41)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/capsicum.h>
36 #include <sys/dirent.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/proc.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/stat.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/systm.h>
46 #include <sys/tty.h>
47 #include <sys/vnode.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50 
51 #ifdef COMPAT_LINUX32
52 #include <machine/../linux32/linux.h>
53 #include <machine/../linux32/linux32_proto.h>
54 #else
55 #include <machine/../linux/linux.h>
56 #include <machine/../linux/linux_proto.h>
57 #endif
58 
59 #include <compat/linux/linux_util.h>
60 #include <compat/linux/linux_file.h>
61 
62 static void
63 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
64 {
65 	int major, minor;
66 
67 	if (vn_isdisk(vp)) {
68 		sb->st_mode &= ~S_IFMT;
69 		sb->st_mode |= S_IFBLK;
70 	}
71 
72 	/*
73 	 * Return the same st_dev for every devfs instance.  The reason
74 	 * for this is to work around an idiosyncrasy of glibc getttynam()
75 	 * implementation: it checks whether st_dev returned for fd 0
76 	 * is the same as st_dev returned for the target of /proc/self/fd/0
77 	 * symlink, and with linux chroots having their own devfs instance,
78 	 * the check will fail if you chroot into it.
79 	 */
80 	if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc)
81 		sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
82 
83 	if (linux_vn_get_major_minor(vp, &major, &minor) == 0)
84 		sb->st_rdev = (major << 8 | minor);
85 }
86 
87 static int
88 linux_kern_statat(struct thread *td, int flag, int fd, const char *path,
89     enum uio_seg pathseg, struct stat *sbp)
90 {
91 
92 	return (kern_statat(td, flag, fd, path, pathseg, sbp,
93 	    translate_vnhook_major_minor));
94 }
95 
96 #ifdef LINUX_LEGACY_SYSCALLS
97 static int
98 linux_kern_stat(struct thread *td, const char *path, enum uio_seg pathseg,
99     struct stat *sbp)
100 {
101 
102 	return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
103 }
104 
105 static int
106 linux_kern_lstat(struct thread *td, const char *path, enum uio_seg pathseg,
107     struct stat *sbp)
108 {
109 
110 	return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
111 	    pathseg, sbp));
112 }
113 #endif
114 
115 static void
116 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
117 {
118 	struct file *fp;
119 	struct vnode *vp;
120 	struct mount *mp;
121 	int major, minor;
122 
123 	/*
124 	 * No capability rights required here.
125 	 */
126 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
127 	    fget(td, fd, &cap_no_rights, &fp) != 0)
128 		return;
129 	vp = fp->f_vnode;
130 	if (vp != NULL && vn_isdisk(vp)) {
131 		buf->st_mode &= ~S_IFMT;
132 		buf->st_mode |= S_IFBLK;
133 	}
134 	if (vp != NULL && rootdevmp != NULL) {
135 		mp = vp->v_mount;
136 		__compiler_membar();
137 		if (mp != NULL && mp->mnt_vfc == rootdevmp->mnt_vfc)
138 			buf->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
139 	}
140 	if (linux_vn_get_major_minor(vp, &major, &minor) == 0) {
141 		buf->st_rdev = (major << 8 | minor);
142 	} else if (fp->f_type == DTYPE_PTS) {
143 		struct tty *tp = fp->f_data;
144 
145 		/* Convert the numbers for the slave device. */
146 		if (linux_driver_get_major_minor(devtoname(tp->t_dev),
147 					 &major, &minor) == 0) {
148 			buf->st_rdev = (major << 8 | minor);
149 		}
150 	}
151 	fdrop(fp, td);
152 }
153 
154 /*
155  * l_dev_t has the same encoding as dev_t in the latter's low 16 bits, so
156  * truncation of a dev_t to 16 bits gives the same result as unpacking
157  * using major() and minor() and repacking in the l_dev_t format.  This
158  * detail is hidden in dev_to_ldev().  Overflow in conversions of dev_t's
159  * are not checked for, as for other fields.
160  *
161  * dev_to_ldev() is only used for translating st_dev.  When we convert
162  * st_rdev for copying it out, it isn't really a dev_t, but has already
163  * been translated to an l_dev_t in a nontrivial way.  Translating it
164  * again would be illogical but would have no effect since the low 16
165  * bits have the same encoding.
166  *
167  * The nontrivial translation for st_rdev renumbers some devices, but not
168  * ones that can be mounted on, so it is consistent with the translation
169  * for st_dev except when the renumbering or truncation causes conflicts.
170  */
171 #define	dev_to_ldev(d)	((uint16_t)(d))
172 
173 static int
174 newstat_copyout(struct stat *buf, void *ubuf)
175 {
176 	struct l_newstat tbuf;
177 
178 	bzero(&tbuf, sizeof(tbuf));
179 	tbuf.st_dev = dev_to_ldev(buf->st_dev);
180 	tbuf.st_ino = buf->st_ino;
181 	tbuf.st_mode = buf->st_mode;
182 	tbuf.st_nlink = buf->st_nlink;
183 	tbuf.st_uid = buf->st_uid;
184 	tbuf.st_gid = buf->st_gid;
185 	tbuf.st_rdev = buf->st_rdev;
186 	tbuf.st_size = buf->st_size;
187 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
188 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
189 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
190 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
191 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
192 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
193 	tbuf.st_blksize = buf->st_blksize;
194 	tbuf.st_blocks = buf->st_blocks;
195 
196 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
197 }
198 
199 static int
200 statx_copyout(struct stat *buf, void *ubuf)
201 {
202 	struct l_statx tbuf;
203 
204 	bzero(&tbuf, sizeof(tbuf));
205 	tbuf.stx_mask = STATX_ALL;
206 	tbuf.stx_blksize = buf->st_blksize;
207 	tbuf.stx_attributes = 0;
208 	tbuf.stx_nlink = buf->st_nlink;
209 	tbuf.stx_uid = buf->st_uid;
210 	tbuf.stx_gid = buf->st_gid;
211 	tbuf.stx_mode = buf->st_mode;
212 	tbuf.stx_ino = buf->st_ino;
213 	tbuf.stx_size = buf->st_size;
214 	tbuf.stx_blocks = buf->st_blocks;
215 
216 	tbuf.stx_atime.tv_sec = buf->st_atim.tv_sec;
217 	tbuf.stx_atime.tv_nsec = buf->st_atim.tv_nsec;
218 	tbuf.stx_btime.tv_sec = buf->st_birthtim.tv_sec;
219 	tbuf.stx_btime.tv_nsec = buf->st_birthtim.tv_nsec;
220 	tbuf.stx_ctime.tv_sec = buf->st_ctim.tv_sec;
221 	tbuf.stx_ctime.tv_nsec = buf->st_ctim.tv_nsec;
222 	tbuf.stx_mtime.tv_sec = buf->st_mtim.tv_sec;
223 	tbuf.stx_mtime.tv_nsec = buf->st_mtim.tv_nsec;
224 
225 	tbuf.stx_rdev_major = buf->st_rdev >> 8;
226 	tbuf.stx_rdev_minor = buf->st_rdev & 0xff;
227 	tbuf.stx_dev_major = buf->st_dev >> 8;
228 	tbuf.stx_dev_minor = buf->st_dev & 0xff;
229 
230 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
231 }
232 
233 #ifdef LINUX_LEGACY_SYSCALLS
234 int
235 linux_newstat(struct thread *td, struct linux_newstat_args *args)
236 {
237 	struct stat buf;
238 	char *path;
239 	int error;
240 
241 	if (!LUSECONVPATH(td)) {
242 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
243 	} else {
244 		LCONVPATHEXIST(args->path, &path);
245 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
246 		LFREEPATH(path);
247 	}
248 	if (error)
249 		return (error);
250 	return (newstat_copyout(&buf, args->buf));
251 }
252 
253 int
254 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
255 {
256 	struct stat sb;
257 	char *path;
258 	int error;
259 
260 	if (!LUSECONVPATH(td)) {
261 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &sb);
262 	} else {
263 		LCONVPATHEXIST(args->path, &path);
264 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
265 		LFREEPATH(path);
266 	}
267 	if (error)
268 		return (error);
269 	return (newstat_copyout(&sb, args->buf));
270 }
271 #endif
272 
273 int
274 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
275 {
276 	struct stat buf;
277 	int error;
278 
279 	error = kern_fstat(td, args->fd, &buf);
280 	translate_fd_major_minor(td, args->fd, &buf);
281 	if (!error)
282 		error = newstat_copyout(&buf, args->buf);
283 
284 	return (error);
285 }
286 
287 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
288 static int
289 stat_copyout(struct stat *buf, void *ubuf)
290 {
291 	struct l_stat lbuf;
292 
293 	bzero(&lbuf, sizeof(lbuf));
294 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
295 	lbuf.st_ino = buf->st_ino;
296 	lbuf.st_mode = buf->st_mode;
297 	lbuf.st_nlink = buf->st_nlink;
298 	lbuf.st_uid = buf->st_uid;
299 	lbuf.st_gid = buf->st_gid;
300 	lbuf.st_rdev = buf->st_rdev;
301 	lbuf.st_size = MIN(buf->st_size, INT32_MAX);
302 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
303 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
304 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
305 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
306 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
307 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
308 	lbuf.st_blksize = buf->st_blksize;
309 	lbuf.st_blocks = buf->st_blocks;
310 	lbuf.st_flags = buf->st_flags;
311 	lbuf.st_gen = buf->st_gen;
312 
313 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
314 }
315 
316 int
317 linux_stat(struct thread *td, struct linux_stat_args *args)
318 {
319 	struct stat buf;
320 	char *path;
321 	int error;
322 
323 	if (!LUSECONVPATH(td)) {
324 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
325 	} else {
326 		LCONVPATHEXIST(args->path, &path);
327 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
328 		LFREEPATH(path);
329 	}
330 	if (error) {
331 		return (error);
332 	}
333 	return (stat_copyout(&buf, args->up));
334 }
335 
336 int
337 linux_lstat(struct thread *td, struct linux_lstat_args *args)
338 {
339 	struct stat buf;
340 	char *path;
341 	int error;
342 
343 	if (!LUSECONVPATH(td)) {
344 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &buf);
345 	} else {
346 		LCONVPATHEXIST(args->path, &path);
347 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
348 		LFREEPATH(path);
349 	}
350 	if (error) {
351 		return (error);
352 	}
353 	return (stat_copyout(&buf, args->up));
354 }
355 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
356 
357 struct l_statfs {
358 	l_long		f_type;
359 	l_long		f_bsize;
360 	l_long		f_blocks;
361 	l_long		f_bfree;
362 	l_long		f_bavail;
363 	l_long		f_files;
364 	l_long		f_ffree;
365 	l_fsid_t	f_fsid;
366 	l_long		f_namelen;
367 	l_long		f_frsize;
368 	l_long		f_flags;
369 	l_long		f_spare[4];
370 };
371 
372 #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
373 #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
374 #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
375 #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
376 #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
377 #define	LINUX_NCP_SUPER_MAGIC	0x564cL
378 #define	LINUX_NFS_SUPER_MAGIC	0x6969L
379 #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
380 #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
381 #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
382 #define	LINUX_ZFS_SUPER_MAGIC	0x2FC12FC1
383 #define	LINUX_DEVFS_SUPER_MAGIC	0x1373L
384 #define	LINUX_SHMFS_MAGIC	0x01021994
385 
386 static long
387 bsd_to_linux_ftype(const char *fstypename)
388 {
389 	int i;
390 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
391 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
392 		{"zfs",     LINUX_ZFS_SUPER_MAGIC},
393 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
394 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
395 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
396 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
397 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
398 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
399 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
400 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
401 		{"coda",    LINUX_CODA_SUPER_MAGIC},
402 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
403 		{"tmpfs",   LINUX_SHMFS_MAGIC},
404 		{NULL,      0L}};
405 
406 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
407 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
408 			return (b2l_tbl[i].linux_type);
409 
410 	return (0L);
411 }
412 
413 static int
414 bsd_to_linux_mnt_flags(int f_flags)
415 {
416 	int flags = LINUX_ST_VALID;
417 
418 	if (f_flags & MNT_RDONLY)
419 		flags |= LINUX_ST_RDONLY;
420 	if (f_flags & MNT_NOEXEC)
421 		flags |= LINUX_ST_NOEXEC;
422 	if (f_flags & MNT_NOSUID)
423 		flags |= LINUX_ST_NOSUID;
424 	if (f_flags & MNT_NOATIME)
425 		flags |= LINUX_ST_NOATIME;
426 	if (f_flags & MNT_NOSYMFOLLOW)
427 		flags |= LINUX_ST_NOSYMFOLLOW;
428 	if (f_flags & MNT_SYNCHRONOUS)
429 		flags |= LINUX_ST_SYNCHRONOUS;
430 
431 	return (flags);
432 }
433 
434 static int
435 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
436 {
437 
438 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
439 	statfs_scale_blocks(bsd_statfs, INT32_MAX);
440 #endif
441 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
442 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
443 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
444 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
445 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
446 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
447 	linux_statfs->f_ffree = MIN(bsd_statfs->f_ffree, INT32_MAX);
448 	linux_statfs->f_files = MIN(bsd_statfs->f_files, INT32_MAX);
449 #else
450 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
451 	linux_statfs->f_files = bsd_statfs->f_files;
452 #endif
453 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
454 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
455 	linux_statfs->f_namelen = MAXNAMLEN;
456 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
457 	linux_statfs->f_flags = bsd_to_linux_mnt_flags(bsd_statfs->f_flags);
458 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
459 
460 	return (0);
461 }
462 
463 int
464 linux_statfs(struct thread *td, struct linux_statfs_args *args)
465 {
466 	struct l_statfs linux_statfs;
467 	struct statfs *bsd_statfs;
468 	char *path;
469 	int error;
470 
471 	if (!LUSECONVPATH(td)) {
472 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
473 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
474 	} else {
475 		LCONVPATHEXIST(args->path, &path);
476 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
477 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
478 		LFREEPATH(path);
479 	}
480 	if (error == 0)
481 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
482 	free(bsd_statfs, M_STATFS);
483 	if (error != 0)
484 		return (error);
485 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
486 }
487 
488 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
489 static void
490 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
491 {
492 
493 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
494 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
495 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
496 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
497 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
498 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
499 	linux_statfs->f_files = bsd_statfs->f_files;
500 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
501 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
502 	linux_statfs->f_namelen = MAXNAMLEN;
503 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
504 	linux_statfs->f_flags = bsd_to_linux_mnt_flags(bsd_statfs->f_flags);
505 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
506 }
507 
508 int
509 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
510 {
511 	struct l_statfs64 linux_statfs;
512 	struct statfs *bsd_statfs;
513 	char *path;
514 	int error;
515 
516 	if (args->bufsize != sizeof(struct l_statfs64))
517 		return (EINVAL);
518 
519 	if (!LUSECONVPATH(td)) {
520 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
521 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
522 	} else {
523 		LCONVPATHEXIST(args->path, &path);
524 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
525 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
526 		LFREEPATH(path);
527 	}
528 	if (error == 0)
529 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
530 	free(bsd_statfs, M_STATFS);
531 	if (error != 0)
532 		return (error);
533 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
534 }
535 
536 int
537 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
538 {
539 	struct l_statfs64 linux_statfs;
540 	struct statfs *bsd_statfs;
541 	int error;
542 
543 	if (args->bufsize != sizeof(struct l_statfs64))
544 		return (EINVAL);
545 
546 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
547 	error = kern_fstatfs(td, args->fd, bsd_statfs);
548 	if (error == 0)
549 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
550 	free(bsd_statfs, M_STATFS);
551 	if (error != 0)
552 		return (error);
553 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
554 }
555 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
556 
557 int
558 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
559 {
560 	struct l_statfs linux_statfs;
561 	struct statfs *bsd_statfs;
562 	int error;
563 
564 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
565 	error = kern_fstatfs(td, args->fd, bsd_statfs);
566 	if (error == 0)
567 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
568 	free(bsd_statfs, M_STATFS);
569 	if (error != 0)
570 		return (error);
571 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
572 }
573 
574 struct l_ustat
575 {
576 	l_daddr_t	f_tfree;
577 	l_ino_t		f_tinode;
578 	char		f_fname[6];
579 	char		f_fpack[6];
580 };
581 
582 #ifdef LINUX_LEGACY_SYSCALLS
583 int
584 linux_ustat(struct thread *td, struct linux_ustat_args *args)
585 {
586 
587 	return (EOPNOTSUPP);
588 }
589 #endif
590 
591 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
592 
593 static int
594 stat64_copyout(struct stat *buf, void *ubuf)
595 {
596 	struct l_stat64 lbuf;
597 
598 	bzero(&lbuf, sizeof(lbuf));
599 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
600 	lbuf.st_ino = buf->st_ino;
601 	lbuf.st_mode = buf->st_mode;
602 	lbuf.st_nlink = buf->st_nlink;
603 	lbuf.st_uid = buf->st_uid;
604 	lbuf.st_gid = buf->st_gid;
605 	lbuf.st_rdev = buf->st_rdev;
606 	lbuf.st_size = buf->st_size;
607 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
608 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
609 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
610 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
611 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
612 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
613 	lbuf.st_blksize = buf->st_blksize;
614 	lbuf.st_blocks = buf->st_blocks;
615 
616 	/*
617 	 * The __st_ino field makes all the difference. In the Linux kernel
618 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
619 	 * but without the assignment to __st_ino the runtime linker refuses
620 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
621 	 */
622 	lbuf.__st_ino = buf->st_ino;
623 
624 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
625 }
626 
627 int
628 linux_stat64(struct thread *td, struct linux_stat64_args *args)
629 {
630 	struct stat buf;
631 	char *filename;
632 	int error;
633 
634 	if (!LUSECONVPATH(td)) {
635 		error = linux_kern_stat(td, args->filename, UIO_USERSPACE, &buf);
636 	} else {
637 		LCONVPATHEXIST(args->filename, &filename);
638 		error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
639 		LFREEPATH(filename);
640 	}
641 	if (error)
642 		return (error);
643 	return (stat64_copyout(&buf, args->statbuf));
644 }
645 
646 int
647 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
648 {
649 	struct stat sb;
650 	char *filename;
651 	int error;
652 
653 	if (!LUSECONVPATH(td)) {
654 		error = linux_kern_lstat(td, args->filename, UIO_USERSPACE, &sb);
655 	} else {
656 		LCONVPATHEXIST(args->filename, &filename);
657 		error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
658 		LFREEPATH(filename);
659 	}
660 	if (error)
661 		return (error);
662 	return (stat64_copyout(&sb, args->statbuf));
663 }
664 
665 int
666 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
667 {
668 	struct stat buf;
669 	int error;
670 
671 	error = kern_fstat(td, args->fd, &buf);
672 	translate_fd_major_minor(td, args->fd, &buf);
673 	if (!error)
674 		error = stat64_copyout(&buf, args->statbuf);
675 
676 	return (error);
677 }
678 
679 int
680 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
681 {
682 	char *path;
683 	int error, dfd, flag, unsupported;
684 	struct stat buf;
685 
686 	unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
687 	if (unsupported != 0) {
688 		linux_msg(td, "fstatat64 unsupported flag 0x%x", unsupported);
689 		return (EINVAL);
690 	}
691 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
692 	    AT_SYMLINK_NOFOLLOW : 0;
693 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
694 	    AT_EMPTY_PATH : 0;
695 
696 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
697 	if (!LUSECONVPATH(td)) {
698 		error = linux_kern_statat(td, flag, dfd, args->pathname,
699 		    UIO_USERSPACE, &buf);
700 	} else {
701 		LCONVPATHEXIST_AT(args->pathname, &path, dfd);
702 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
703 		LFREEPATH(path);
704 	}
705 	if (error == 0)
706 		error = stat64_copyout(&buf, args->statbuf);
707 
708 	return (error);
709 }
710 
711 #else /* __amd64__ && !COMPAT_LINUX32 */
712 
713 int
714 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
715 {
716 	char *path;
717 	int error, dfd, flag, unsupported;
718 	struct stat buf;
719 
720 	unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
721 	if (unsupported != 0) {
722 		linux_msg(td, "fstatat unsupported flag 0x%x", unsupported);
723 		return (EINVAL);
724 	}
725 
726 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
727 	    AT_SYMLINK_NOFOLLOW : 0;
728 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
729 	    AT_EMPTY_PATH : 0;
730 
731 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
732 	if (!LUSECONVPATH(td)) {
733 		error = linux_kern_statat(td, flag, dfd, args->pathname,
734 		    UIO_USERSPACE, &buf);
735 	} else {
736 		LCONVPATHEXIST_AT(args->pathname, &path, dfd);
737 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
738 		LFREEPATH(path);
739 	}
740 	if (error == 0)
741 		error = newstat_copyout(&buf, args->statbuf);
742 
743 	return (error);
744 }
745 
746 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
747 
748 int
749 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
750 {
751 	struct mount *mp;
752 	struct vnode *vp;
753 	int error, save;
754 
755 	error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
756 	if (error != 0)
757 		/*
758 		 * Linux syncfs() returns only EBADF, however fgetvp()
759 		 * can return EINVAL in case of file descriptor does
760 		 * not represent a vnode. XXX.
761 		 */
762 		return (error);
763 
764 	mp = vp->v_mount;
765 	mtx_lock(&mountlist_mtx);
766 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
767 	if (error != 0) {
768 		/* See comment above. */
769 		mtx_unlock(&mountlist_mtx);
770 		goto out;
771 	}
772 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
773 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
774 		save = curthread_pflags_set(TDP_SYNCIO);
775 		vfs_periodic(mp, MNT_NOWAIT);
776 		VFS_SYNC(mp, MNT_NOWAIT);
777 		curthread_pflags_restore(save);
778 		vn_finished_write(mp);
779 	}
780 	vfs_unbusy(mp);
781 
782  out:
783 	vrele(vp);
784 	return (error);
785 }
786 
787 int
788 linux_statx(struct thread *td, struct linux_statx_args *args)
789 {
790 	char *path;
791 	int error, dirfd, flags, unsupported;
792 	struct stat buf;
793 
794 	unsupported = args->flags & ~(LINUX_AT_SYMLINK_NOFOLLOW |
795 	    LINUX_AT_EMPTY_PATH | LINUX_AT_NO_AUTOMOUNT);
796 	if (unsupported != 0) {
797 		linux_msg(td, "statx unsupported flags 0x%x", unsupported);
798 		return (EINVAL);
799 	}
800 
801 	flags = (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) ?
802 	    AT_SYMLINK_NOFOLLOW : 0;
803 	flags |= (args->flags & LINUX_AT_EMPTY_PATH) ?
804 	    AT_EMPTY_PATH : 0;
805 
806 	dirfd = (args->dirfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dirfd;
807 	if (!LUSECONVPATH(td)) {
808 		error = linux_kern_statat(td, flags, dirfd, args->pathname,
809 		    UIO_USERSPACE, &buf);
810 	} else {
811 		LCONVPATHEXIST_AT(args->pathname, &path, dirfd);
812 		error = linux_kern_statat(td, flags, dirfd, path, UIO_SYSSPACE, &buf);
813 		LFREEPATH(path);
814 	}
815 	if (error == 0)
816 		error = statx_copyout(&buf, args->statxbuf);
817 
818 	return (error);
819 }
820