xref: /freebsd/sys/compat/linux/linux_stats.c (revision 25fb30bd9abc492359ad1f66901a06cb8cd08370)
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 #ifdef LINUX_LEGACY_SYSCALLS
200 int
201 linux_newstat(struct thread *td, struct linux_newstat_args *args)
202 {
203 	struct stat buf;
204 	char *path;
205 	int error;
206 
207 	if (!LUSECONVPATH(td)) {
208 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
209 	} else {
210 		LCONVPATHEXIST(td, args->path, &path);
211 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
212 		LFREEPATH(path);
213 	}
214 	if (error)
215 		return (error);
216 	return (newstat_copyout(&buf, args->buf));
217 }
218 
219 int
220 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
221 {
222 	struct stat sb;
223 	char *path;
224 	int error;
225 
226 	if (!LUSECONVPATH(td)) {
227 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &sb);
228 	} else {
229 		LCONVPATHEXIST(td, args->path, &path);
230 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
231 		LFREEPATH(path);
232 	}
233 	if (error)
234 		return (error);
235 	return (newstat_copyout(&sb, args->buf));
236 }
237 #endif
238 
239 int
240 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
241 {
242 	struct stat buf;
243 	int error;
244 
245 	error = kern_fstat(td, args->fd, &buf);
246 	translate_fd_major_minor(td, args->fd, &buf);
247 	if (!error)
248 		error = newstat_copyout(&buf, args->buf);
249 
250 	return (error);
251 }
252 
253 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
254 static int
255 stat_copyout(struct stat *buf, void *ubuf)
256 {
257 	struct l_stat lbuf;
258 
259 	bzero(&lbuf, sizeof(lbuf));
260 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
261 	lbuf.st_ino = buf->st_ino;
262 	lbuf.st_mode = buf->st_mode;
263 	lbuf.st_nlink = buf->st_nlink;
264 	lbuf.st_uid = buf->st_uid;
265 	lbuf.st_gid = buf->st_gid;
266 	lbuf.st_rdev = buf->st_rdev;
267 	lbuf.st_size = MIN(buf->st_size, INT32_MAX);
268 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
269 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
270 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
271 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
272 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
273 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
274 	lbuf.st_blksize = buf->st_blksize;
275 	lbuf.st_blocks = buf->st_blocks;
276 	lbuf.st_flags = buf->st_flags;
277 	lbuf.st_gen = buf->st_gen;
278 
279 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
280 }
281 
282 int
283 linux_stat(struct thread *td, struct linux_stat_args *args)
284 {
285 	struct stat buf;
286 	char *path;
287 	int error;
288 
289 	if (!LUSECONVPATH(td)) {
290 		error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
291 	} else {
292 		LCONVPATHEXIST(td, args->path, &path);
293 		error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
294 		LFREEPATH(path);
295 	}
296 	if (error) {
297 		return (error);
298 	}
299 	return (stat_copyout(&buf, args->up));
300 }
301 
302 int
303 linux_lstat(struct thread *td, struct linux_lstat_args *args)
304 {
305 	struct stat buf;
306 	char *path;
307 	int error;
308 
309 	if (!LUSECONVPATH(td)) {
310 		error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &buf);
311 	} else {
312 		LCONVPATHEXIST(td, args->path, &path);
313 		error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
314 		LFREEPATH(path);
315 	}
316 	if (error) {
317 		return (error);
318 	}
319 	return (stat_copyout(&buf, args->up));
320 }
321 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
322 
323 struct l_statfs {
324 	l_long		f_type;
325 	l_long		f_bsize;
326 	l_long		f_blocks;
327 	l_long		f_bfree;
328 	l_long		f_bavail;
329 	l_long		f_files;
330 	l_long		f_ffree;
331 	l_fsid_t	f_fsid;
332 	l_long		f_namelen;
333 	l_long		f_frsize;
334 	l_long		f_flags;
335 	l_long		f_spare[4];
336 };
337 
338 #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
339 #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
340 #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
341 #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
342 #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
343 #define	LINUX_NCP_SUPER_MAGIC	0x564cL
344 #define	LINUX_NFS_SUPER_MAGIC	0x6969L
345 #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
346 #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
347 #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
348 #define	LINUX_ZFS_SUPER_MAGIC	0x2FC12FC1
349 #define	LINUX_DEVFS_SUPER_MAGIC	0x1373L
350 #define	LINUX_SHMFS_MAGIC	0x01021994
351 
352 static long
353 bsd_to_linux_ftype(const char *fstypename)
354 {
355 	int i;
356 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
357 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
358 		{"zfs",     LINUX_ZFS_SUPER_MAGIC},
359 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
360 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
361 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
362 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
363 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
364 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
365 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
366 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
367 		{"coda",    LINUX_CODA_SUPER_MAGIC},
368 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
369 		{"tmpfs",   LINUX_SHMFS_MAGIC},
370 		{NULL,      0L}};
371 
372 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
373 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
374 			return (b2l_tbl[i].linux_type);
375 
376 	return (0L);
377 }
378 
379 static int
380 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
381 {
382 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
383 	uint64_t tmp;
384 
385 #define	LINUX_HIBITS	0xffffffff00000000ULL
386 
387 	tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
388 	    bsd_statfs->f_bsize;
389 	if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
390 	    (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
391 	    (tmp & LINUX_HIBITS))
392 		return (EOVERFLOW);
393 #undef	LINUX_HIBITS
394 #endif
395 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
396 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
397 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
398 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
399 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
400 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
401 	linux_statfs->f_files = bsd_statfs->f_files;
402 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
403 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
404 	linux_statfs->f_namelen = MAXNAMLEN;
405 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
406 	linux_statfs->f_flags = 0;
407 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
408 
409 	return (0);
410 }
411 
412 int
413 linux_statfs(struct thread *td, struct linux_statfs_args *args)
414 {
415 	struct l_statfs linux_statfs;
416 	struct statfs *bsd_statfs;
417 	char *path;
418 	int error;
419 
420 	if (!LUSECONVPATH(td)) {
421 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
422 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
423 	} else {
424 		LCONVPATHEXIST(td, args->path, &path);
425 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
426 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
427 		LFREEPATH(path);
428 	}
429 	if (error == 0)
430 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
431 	free(bsd_statfs, M_STATFS);
432 	if (error != 0)
433 		return (error);
434 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
435 }
436 
437 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
438 static void
439 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
440 {
441 
442 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
443 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
444 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
445 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
446 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
447 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
448 	linux_statfs->f_files = bsd_statfs->f_files;
449 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
450 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
451 	linux_statfs->f_namelen = MAXNAMLEN;
452 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
453 	linux_statfs->f_flags = 0;
454 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
455 }
456 
457 int
458 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
459 {
460 	struct l_statfs64 linux_statfs;
461 	struct statfs *bsd_statfs;
462 	char *path;
463 	int error;
464 
465 	if (args->bufsize != sizeof(struct l_statfs64))
466 		return (EINVAL);
467 
468 	if (!LUSECONVPATH(td)) {
469 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
470 		error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
471 	} else {
472 		LCONVPATHEXIST(td, args->path, &path);
473 		bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
474 		error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
475 		LFREEPATH(path);
476 	}
477 	if (error == 0)
478 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
479 	free(bsd_statfs, M_STATFS);
480 	if (error != 0)
481 		return (error);
482 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
483 }
484 
485 int
486 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
487 {
488 	struct l_statfs64 linux_statfs;
489 	struct statfs *bsd_statfs;
490 	int error;
491 
492 	if (args->bufsize != sizeof(struct l_statfs64))
493 		return (EINVAL);
494 
495 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
496 	error = kern_fstatfs(td, args->fd, bsd_statfs);
497 	if (error == 0)
498 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
499 	free(bsd_statfs, M_STATFS);
500 	if (error != 0)
501 		return (error);
502 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
503 }
504 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
505 
506 int
507 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
508 {
509 	struct l_statfs linux_statfs;
510 	struct statfs *bsd_statfs;
511 	int error;
512 
513 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
514 	error = kern_fstatfs(td, args->fd, bsd_statfs);
515 	if (error == 0)
516 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
517 	free(bsd_statfs, M_STATFS);
518 	if (error != 0)
519 		return (error);
520 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
521 }
522 
523 struct l_ustat
524 {
525 	l_daddr_t	f_tfree;
526 	l_ino_t		f_tinode;
527 	char		f_fname[6];
528 	char		f_fpack[6];
529 };
530 
531 #ifdef LINUX_LEGACY_SYSCALLS
532 int
533 linux_ustat(struct thread *td, struct linux_ustat_args *args)
534 {
535 
536 	return (EOPNOTSUPP);
537 }
538 #endif
539 
540 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
541 
542 static int
543 stat64_copyout(struct stat *buf, void *ubuf)
544 {
545 	struct l_stat64 lbuf;
546 
547 	bzero(&lbuf, sizeof(lbuf));
548 	lbuf.st_dev = dev_to_ldev(buf->st_dev);
549 	lbuf.st_ino = buf->st_ino;
550 	lbuf.st_mode = buf->st_mode;
551 	lbuf.st_nlink = buf->st_nlink;
552 	lbuf.st_uid = buf->st_uid;
553 	lbuf.st_gid = buf->st_gid;
554 	lbuf.st_rdev = buf->st_rdev;
555 	lbuf.st_size = buf->st_size;
556 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
557 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
558 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
559 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
560 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
561 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
562 	lbuf.st_blksize = buf->st_blksize;
563 	lbuf.st_blocks = buf->st_blocks;
564 
565 	/*
566 	 * The __st_ino field makes all the difference. In the Linux kernel
567 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
568 	 * but without the assignment to __st_ino the runtime linker refuses
569 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
570 	 */
571 	lbuf.__st_ino = buf->st_ino;
572 
573 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
574 }
575 
576 int
577 linux_stat64(struct thread *td, struct linux_stat64_args *args)
578 {
579 	struct stat buf;
580 	char *filename;
581 	int error;
582 
583 	if (!LUSECONVPATH(td)) {
584 		error = linux_kern_stat(td, args->filename, UIO_USERSPACE, &buf);
585 	} else {
586 		LCONVPATHEXIST(td, args->filename, &filename);
587 		error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
588 		LFREEPATH(filename);
589 	}
590 	if (error)
591 		return (error);
592 	return (stat64_copyout(&buf, args->statbuf));
593 }
594 
595 int
596 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
597 {
598 	struct stat sb;
599 	char *filename;
600 	int error;
601 
602 	if (!LUSECONVPATH(td)) {
603 		error = linux_kern_lstat(td, args->filename, UIO_USERSPACE, &sb);
604 	} else {
605 		LCONVPATHEXIST(td, args->filename, &filename);
606 		error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
607 		LFREEPATH(filename);
608 	}
609 	if (error)
610 		return (error);
611 	return (stat64_copyout(&sb, args->statbuf));
612 }
613 
614 int
615 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
616 {
617 	struct stat buf;
618 	int error;
619 
620 	error = kern_fstat(td, args->fd, &buf);
621 	translate_fd_major_minor(td, args->fd, &buf);
622 	if (!error)
623 		error = stat64_copyout(&buf, args->statbuf);
624 
625 	return (error);
626 }
627 
628 int
629 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
630 {
631 	char *path;
632 	int error, dfd, flag;
633 	struct stat buf;
634 
635 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
636 		return (EINVAL);
637 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
638 	    AT_SYMLINK_NOFOLLOW : 0;
639 
640 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
641 	if (!LUSECONVPATH(td)) {
642 		error = linux_kern_statat(td, flag, dfd, args->pathname,
643 		    UIO_USERSPACE, &buf);
644 	} else {
645 		LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
646 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
647 		LFREEPATH(path);
648 	}
649 	if (error == 0)
650 		error = stat64_copyout(&buf, args->statbuf);
651 
652 	return (error);
653 }
654 
655 #else /* __amd64__ && !COMPAT_LINUX32 */
656 
657 int
658 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
659 {
660 	char *path;
661 	int error, dfd, flag;
662 	struct stat buf;
663 
664 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
665 		return (EINVAL);
666 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
667 	    AT_SYMLINK_NOFOLLOW : 0;
668 
669 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
670 	if (!LUSECONVPATH(td)) {
671 		error = linux_kern_statat(td, flag, dfd, args->pathname,
672 		    UIO_USERSPACE, &buf);
673 	} else {
674 		LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
675 		error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
676 		LFREEPATH(path);
677 	}
678 	if (error == 0)
679 		error = newstat_copyout(&buf, args->statbuf);
680 
681 	return (error);
682 }
683 
684 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
685 
686 int
687 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
688 {
689 	struct mount *mp;
690 	struct vnode *vp;
691 	int error, save;
692 
693 	error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
694 	if (error != 0)
695 		/*
696 		 * Linux syncfs() returns only EBADF, however fgetvp()
697 		 * can return EINVAL in case of file descriptor does
698 		 * not represent a vnode. XXX.
699 		 */
700 		return (error);
701 
702 	mp = vp->v_mount;
703 	mtx_lock(&mountlist_mtx);
704 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
705 	if (error != 0) {
706 		/* See comment above. */
707 		mtx_unlock(&mountlist_mtx);
708 		goto out;
709 	}
710 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
711 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
712 		save = curthread_pflags_set(TDP_SYNCIO);
713 		vfs_periodic(mp, MNT_NOWAIT);
714 		VFS_SYNC(mp, MNT_NOWAIT);
715 		curthread_pflags_restore(save);
716 		vn_finished_write(mp);
717 	}
718 	vfs_unbusy(mp);
719 
720  out:
721 	vrele(vp);
722 	return (error);
723 }
724