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