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