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