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