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