xref: /freebsd/sys/compat/linux/linux_stats.c (revision 99546279d612cfaf4ae5501f41785d37fda3b425)
1c21dee17SSøren Schmidt /*-
29a14aa01SUlrich Spörlein  * Copyright (c) 1994-1995 Søren Schmidt
3c21dee17SSøren Schmidt  * All rights reserved.
4c21dee17SSøren Schmidt  *
5c21dee17SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
6c21dee17SSøren Schmidt  * modification, are permitted provided that the following conditions
7c21dee17SSøren Schmidt  * are met:
8c21dee17SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
9c21dee17SSøren Schmidt  *    notice, this list of conditions and the following disclaimer
10c21dee17SSøren Schmidt  *    in this position and unchanged.
11c21dee17SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
12c21dee17SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
13c21dee17SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
14c21dee17SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
1521dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
16c21dee17SSøren Schmidt  *
17c21dee17SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18c21dee17SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19c21dee17SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20c21dee17SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21c21dee17SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22c21dee17SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23c21dee17SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24c21dee17SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25c21dee17SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26c21dee17SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27c21dee17SSøren Schmidt  */
28c21dee17SSøren Schmidt 
2916dbc7f2SDavid E. O'Brien #include <sys/cdefs.h>
3016dbc7f2SDavid E. O'Brien __FBSDID("$FreeBSD$");
3116dbc7f2SDavid E. O'Brien 
32aefce619SRuslan Ermilov #include "opt_compat.h"
33eddc160eSRobert Watson 
34c21dee17SSøren Schmidt #include <sys/param.h>
359802eb9eSDmitry Chagin #include <sys/capsicum.h>
36c21dee17SSøren Schmidt #include <sys/dirent.h>
37c21dee17SSøren Schmidt #include <sys/file.h>
38c21dee17SSøren Schmidt #include <sys/filedesc.h>
39c21dee17SSøren Schmidt #include <sys/proc.h>
4085422e62SBruce Evans #include <sys/malloc.h>
41c21dee17SSøren Schmidt #include <sys/mount.h>
42c21dee17SSøren Schmidt #include <sys/namei.h>
43c21dee17SSøren Schmidt #include <sys/stat.h>
44f7a25872SJohn Baldwin #include <sys/syscallsubr.h>
45408da119SMarcel Moolenaar #include <sys/systm.h>
46bc093719SEd Schouten #include <sys/tty.h>
47c21dee17SSøren Schmidt #include <sys/vnode.h>
48060e4882SDoug Ambrisko #include <sys/conf.h>
49060e4882SDoug Ambrisko #include <sys/fcntl.h>
50c21dee17SSøren Schmidt 
511997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32
524af27623STim J. Robbins #include <machine/../linux32/linux.h>
534af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
541997c537SDavid E. O'Brien #else
551997c537SDavid E. O'Brien #include <machine/../linux/linux.h>
561997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h>
574af27623STim J. Robbins #endif
5885422e62SBruce Evans 
59ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
6048b05c3fSKonstantin Belousov #include <compat/linux/linux_file.h>
61762e6b85SEivind Eklund 
62529844c7SAlexander Leidinger 
630eee862aSEd Schouten static void
640eee862aSEd Schouten translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
650eee862aSEd Schouten {
660eee862aSEd Schouten 	int major, minor;
670eee862aSEd Schouten 
680eee862aSEd Schouten 	if (vp->v_type == VCHR && vp->v_rdev != NULL &&
697870adb6SEd Schouten 	    linux_driver_get_major_minor(devtoname(vp->v_rdev),
700eee862aSEd Schouten 	    &major, &minor) == 0) {
710eee862aSEd Schouten 		sb->st_rdev = (major << 8 | minor);
720eee862aSEd Schouten 	}
730eee862aSEd Schouten }
740eee862aSEd Schouten 
750eee862aSEd Schouten static int
760eee862aSEd Schouten linux_kern_statat(struct thread *td, int flag, int fd, char *path,
770eee862aSEd Schouten     enum uio_seg pathseg, struct stat *sbp)
780eee862aSEd Schouten {
790eee862aSEd Schouten 
806e646651SKonstantin Belousov 	return (kern_statat(td, flag, fd, path, pathseg, sbp,
810eee862aSEd Schouten 	    translate_vnhook_major_minor));
820eee862aSEd Schouten }
830eee862aSEd Schouten 
840eee862aSEd Schouten static int
850eee862aSEd Schouten linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
860eee862aSEd Schouten     struct stat *sbp)
870eee862aSEd Schouten {
880eee862aSEd Schouten 
890eee862aSEd Schouten 	return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
900eee862aSEd Schouten }
910eee862aSEd Schouten 
920eee862aSEd Schouten static int
930eee862aSEd Schouten linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
940eee862aSEd Schouten     struct stat *sbp)
950eee862aSEd Schouten {
960eee862aSEd Schouten 
970eee862aSEd Schouten 	return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
980eee862aSEd Schouten 	    pathseg, sbp));
990eee862aSEd Schouten }
1000eee862aSEd Schouten 
101bbbc2d96SPoul-Henning Kamp /*
102bbbc2d96SPoul-Henning Kamp  * XXX: This was removed from newstat_copyout(), and almost identical
103bbbc2d96SPoul-Henning Kamp  * XXX: code was in stat64_copyout().  findcdev() needs to be replaced
104bbbc2d96SPoul-Henning Kamp  * XXX: with something that does lookup and locking properly.
105bbbc2d96SPoul-Henning Kamp  * XXX: When somebody fixes this: please try to avoid duplicating it.
106bbbc2d96SPoul-Henning Kamp  */
107bbbc2d96SPoul-Henning Kamp #if 0
108bbbc2d96SPoul-Henning Kamp static void
109bbbc2d96SPoul-Henning Kamp disk_foo(struct somestat *tbuf)
110c21dee17SSøren Schmidt {
1114c3a3ec0SJosef Karthauser 	struct cdevsw *cdevsw;
11289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
113c21dee17SSøren Schmidt 
1144c3a3ec0SJosef Karthauser 	/* Lie about disk drives which are character devices
1154c3a3ec0SJosef Karthauser 	 * in FreeBSD but block devices under Linux.
1164c3a3ec0SJosef Karthauser 	 */
1175002a60fSMarcel Moolenaar 	if (S_ISCHR(tbuf.st_mode) &&
118f3732fd1SPoul-Henning Kamp 	    (dev = findcdev(buf->st_rdev)) != NULL) {
119f69f5fbdSPoul-Henning Kamp 		cdevsw = dev_refthread(dev);
120f69f5fbdSPoul-Henning Kamp 		if (cdevsw != NULL) {
121f69f5fbdSPoul-Henning Kamp 			if (cdevsw->d_flags & D_DISK) {
1225002a60fSMarcel Moolenaar 				tbuf.st_mode &= ~S_IFMT;
1235002a60fSMarcel Moolenaar 				tbuf.st_mode |= S_IFBLK;
1247842f151SPaul Richards 
1257842f151SPaul Richards 				/* XXX this may not be quite right */
1267842f151SPaul Richards 				/* Map major number to 0 */
127a4611ab6SEd Schouten 				tbuf.st_dev = minor(buf->st_dev) & 0xf;
1285002a60fSMarcel Moolenaar 				tbuf.st_rdev = buf->st_rdev & 0xff;
1297842f151SPaul Richards 			}
130f69f5fbdSPoul-Henning Kamp 			dev_relthread(dev);
131f69f5fbdSPoul-Henning Kamp 		}
1324c3a3ec0SJosef Karthauser 	}
1334e0eaf69SMarcel Moolenaar 
134bbbc2d96SPoul-Henning Kamp }
135bbbc2d96SPoul-Henning Kamp #endif
136bbbc2d96SPoul-Henning Kamp 
137060e4882SDoug Ambrisko static void
138060e4882SDoug Ambrisko translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
139060e4882SDoug Ambrisko {
140060e4882SDoug Ambrisko 	struct file *fp;
1410eee862aSEd Schouten 	struct vnode *vp;
142f131759fSMateusz Guzik 	cap_rights_t rights;
143060e4882SDoug Ambrisko 	int major, minor;
144060e4882SDoug Ambrisko 
145a9d2f8d8SRobert Watson 	/*
146a9d2f8d8SRobert Watson 	 * No capability rights required here.
147a9d2f8d8SRobert Watson 	 */
148b256a1e1SJung-uk Kim 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
149f131759fSMateusz Guzik 	    fget(td, fd, cap_rights_init(&rights), &fp) != 0)
150060e4882SDoug Ambrisko 		return;
1510eee862aSEd Schouten 	vp = fp->f_vnode;
1520eee862aSEd Schouten 	if (vp != NULL && vp->v_rdev != NULL &&
1537870adb6SEd Schouten 	    linux_driver_get_major_minor(devtoname(vp->v_rdev),
154bc093719SEd Schouten 					 &major, &minor) == 0) {
155060e4882SDoug Ambrisko 		buf->st_rdev = (major << 8 | minor);
156bc093719SEd Schouten 	} else if (fp->f_type == DTYPE_PTS) {
157bc093719SEd Schouten 		struct tty *tp = fp->f_data;
158bc093719SEd Schouten 
159bc093719SEd Schouten 		/* Convert the numbers for the slave device. */
1607870adb6SEd Schouten 		if (linux_driver_get_major_minor(devtoname(tp->t_dev),
161bc093719SEd Schouten 					 &major, &minor) == 0) {
162bc093719SEd Schouten 			buf->st_rdev = (major << 8 | minor);
163bc093719SEd Schouten 		}
164bc093719SEd Schouten 	}
165060e4882SDoug Ambrisko 	fdrop(fp, td);
166060e4882SDoug Ambrisko }
167060e4882SDoug Ambrisko 
168bbbc2d96SPoul-Henning Kamp static int
169bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf)
170bbbc2d96SPoul-Henning Kamp {
171bbbc2d96SPoul-Henning Kamp 	struct l_newstat tbuf;
172bbbc2d96SPoul-Henning Kamp 
173bbbc2d96SPoul-Henning Kamp 	bzero(&tbuf, sizeof(tbuf));
174a4611ab6SEd Schouten 	tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
175bbbc2d96SPoul-Henning Kamp 	tbuf.st_ino = buf->st_ino;
176bbbc2d96SPoul-Henning Kamp 	tbuf.st_mode = buf->st_mode;
177bbbc2d96SPoul-Henning Kamp 	tbuf.st_nlink = buf->st_nlink;
178bbbc2d96SPoul-Henning Kamp 	tbuf.st_uid = buf->st_uid;
179bbbc2d96SPoul-Henning Kamp 	tbuf.st_gid = buf->st_gid;
180bbbc2d96SPoul-Henning Kamp 	tbuf.st_rdev = buf->st_rdev;
181bbbc2d96SPoul-Henning Kamp 	tbuf.st_size = buf->st_size;
182510ea843SEd Schouten 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
183510ea843SEd Schouten 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
184510ea843SEd Schouten 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
185510ea843SEd Schouten 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
186510ea843SEd Schouten 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
187510ea843SEd Schouten 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
188bbbc2d96SPoul-Henning Kamp 	tbuf.st_blksize = buf->st_blksize;
189bbbc2d96SPoul-Henning Kamp 	tbuf.st_blocks = buf->st_blocks;
190bbbc2d96SPoul-Henning Kamp 
1914e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
192c21dee17SSøren Schmidt }
193c21dee17SSøren Schmidt 
194c21dee17SSøren Schmidt int
195b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
196c21dee17SSøren Schmidt {
197c21dee17SSøren Schmidt 	struct stat buf;
198206a5d3aSIan Dowse 	char *path;
199c21dee17SSøren Schmidt 	int error;
200d66a5066SPeter Wemm 
201206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
202c21dee17SSøren Schmidt 
203c21dee17SSøren Schmidt #ifdef DEBUG
20424593369SJonathan Lemon 	if (ldebug(newstat))
205206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
206c21dee17SSøren Schmidt #endif
2074e0eaf69SMarcel Moolenaar 
2080eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
209206a5d3aSIan Dowse 	LFREEPATH(path);
2104e0eaf69SMarcel Moolenaar 	if (error)
2114e0eaf69SMarcel Moolenaar 		return (error);
2124e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
213c21dee17SSøren Schmidt }
214c21dee17SSøren Schmidt 
215c21dee17SSøren Schmidt int
216b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
217c21dee17SSøren Schmidt {
2184e0eaf69SMarcel Moolenaar 	struct stat sb;
219206a5d3aSIan Dowse 	char *path;
220f7a25872SJohn Baldwin 	int error;
221d66a5066SPeter Wemm 
222206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
223c21dee17SSøren Schmidt 
224c21dee17SSøren Schmidt #ifdef DEBUG
22524593369SJonathan Lemon 	if (ldebug(newlstat))
226206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
227c21dee17SSøren Schmidt #endif
2284e0eaf69SMarcel Moolenaar 
2290eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
230206a5d3aSIan Dowse 	LFREEPATH(path);
231d66a5066SPeter Wemm 	if (error)
232d66a5066SPeter Wemm 		return (error);
2335002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
234d66a5066SPeter Wemm }
235c21dee17SSøren Schmidt 
236c21dee17SSøren Schmidt int
237b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
238c21dee17SSøren Schmidt {
239c21dee17SSøren Schmidt 	struct stat buf;
240c21dee17SSøren Schmidt 	int error;
241c21dee17SSøren Schmidt 
242c21dee17SSøren Schmidt #ifdef DEBUG
24324593369SJonathan Lemon 	if (ldebug(newfstat))
24424593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
245c21dee17SSøren Schmidt #endif
2464e0eaf69SMarcel Moolenaar 
247f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
248060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
249c21dee17SSøren Schmidt 	if (!error)
250c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
2514e0eaf69SMarcel Moolenaar 
2524e0eaf69SMarcel Moolenaar 	return (error);
253c21dee17SSøren Schmidt }
254c21dee17SSøren Schmidt 
2557f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2565c8919adSAlexander Leidinger static int
2575c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf)
2585c8919adSAlexander Leidinger {
2595c8919adSAlexander Leidinger 	struct l_stat lbuf;
2605c8919adSAlexander Leidinger 
2615c8919adSAlexander Leidinger 	bzero(&lbuf, sizeof(lbuf));
2625c8919adSAlexander Leidinger 	lbuf.st_dev = buf->st_dev;
2635c8919adSAlexander Leidinger 	lbuf.st_ino = buf->st_ino;
2645c8919adSAlexander Leidinger 	lbuf.st_mode = buf->st_mode;
2655c8919adSAlexander Leidinger 	lbuf.st_nlink = buf->st_nlink;
2665c8919adSAlexander Leidinger 	lbuf.st_uid = buf->st_uid;
2675c8919adSAlexander Leidinger 	lbuf.st_gid = buf->st_gid;
2685c8919adSAlexander Leidinger 	lbuf.st_rdev = buf->st_rdev;
2695c8919adSAlexander Leidinger 	if (buf->st_size < (quad_t)1 << 32)
2705c8919adSAlexander Leidinger 		lbuf.st_size = buf->st_size;
2715c8919adSAlexander Leidinger 	else
2725c8919adSAlexander Leidinger 		lbuf.st_size = -2;
273510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
274510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
275510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
276510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
277510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
278510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
2795c8919adSAlexander Leidinger 	lbuf.st_blksize = buf->st_blksize;
2805c8919adSAlexander Leidinger 	lbuf.st_blocks = buf->st_blocks;
2815c8919adSAlexander Leidinger 	lbuf.st_flags = buf->st_flags;
2825c8919adSAlexander Leidinger 	lbuf.st_gen = buf->st_gen;
2835c8919adSAlexander Leidinger 
2845c8919adSAlexander Leidinger 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
2855c8919adSAlexander Leidinger }
2865c8919adSAlexander Leidinger 
2875c8919adSAlexander Leidinger int
2885c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args)
2895c8919adSAlexander Leidinger {
2905c8919adSAlexander Leidinger 	struct stat buf;
29115b78ac5SKonstantin Belousov 	char *path;
2925c8919adSAlexander Leidinger 	int error;
29315b78ac5SKonstantin Belousov 
29415b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
29515b78ac5SKonstantin Belousov 
2965c8919adSAlexander Leidinger #ifdef DEBUG
2975c8919adSAlexander Leidinger 	if (ldebug(stat))
298d075105dSKonstantin Belousov 		printf(ARGS(stat, "%s, *"), path);
2995c8919adSAlexander Leidinger #endif
3000eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
301d075105dSKonstantin Belousov 	if (error) {
30215b78ac5SKonstantin Belousov 		LFREEPATH(path);
3035c8919adSAlexander Leidinger 		return (error);
304d075105dSKonstantin Belousov 	}
305d075105dSKonstantin Belousov 	LFREEPATH(path);
3065c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
3075c8919adSAlexander Leidinger }
3085c8919adSAlexander Leidinger 
3095c8919adSAlexander Leidinger int
3105c8919adSAlexander Leidinger linux_lstat(struct thread *td, struct linux_lstat_args *args)
3115c8919adSAlexander Leidinger {
3125c8919adSAlexander Leidinger 	struct stat buf;
31315b78ac5SKonstantin Belousov 	char *path;
3145c8919adSAlexander Leidinger 	int error;
3155c8919adSAlexander Leidinger 
31615b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
31715b78ac5SKonstantin Belousov 
3185c8919adSAlexander Leidinger #ifdef DEBUG
3195c8919adSAlexander Leidinger 	if (ldebug(lstat))
320d075105dSKonstantin Belousov 		printf(ARGS(lstat, "%s, *"), path);
3215c8919adSAlexander Leidinger #endif
3220eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
323d075105dSKonstantin Belousov 	if (error) {
32415b78ac5SKonstantin Belousov 		LFREEPATH(path);
3255c8919adSAlexander Leidinger 		return (error);
326d075105dSKonstantin Belousov 	}
327d075105dSKonstantin Belousov 	LFREEPATH(path);
3285c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
3295c8919adSAlexander Leidinger }
3307f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
3315c8919adSAlexander Leidinger 
3325002a60fSMarcel Moolenaar struct l_statfs {
333297f61ccSDmitry Chagin 	l_long		f_type;
334297f61ccSDmitry Chagin 	l_long		f_bsize;
335297f61ccSDmitry Chagin 	l_long		f_blocks;
336297f61ccSDmitry Chagin 	l_long		f_bfree;
337297f61ccSDmitry Chagin 	l_long		f_bavail;
338297f61ccSDmitry Chagin 	l_long		f_files;
339297f61ccSDmitry Chagin 	l_long		f_ffree;
3405002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
341297f61ccSDmitry Chagin 	l_long		f_namelen;
342297f61ccSDmitry Chagin 	l_long		f_spare[6];
343c21dee17SSøren Schmidt };
344c21dee17SSøren Schmidt 
345dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
346dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
347dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
348dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
349dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
350dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
351dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
352dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
353dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
354dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
355e83d253bSOlivier Houchard #define LINUX_DEVFS_SUPER_MAGIC	0x1373L
3562166e4e0SDmitry Chagin #define	LINUX_SHMFS_MAGIC	0x01021994
357dca60efcSMarcel Moolenaar 
358dca60efcSMarcel Moolenaar static long
359962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
360dca60efcSMarcel Moolenaar {
361962cf420SMaxim Sobolev 	int i;
362962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
363962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
364962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
365962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
366962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
367962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
368962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
369962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
370962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
371962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
372962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
373e83d253bSOlivier Houchard 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
3742166e4e0SDmitry Chagin 		{"tmpfs",   LINUX_SHMFS_MAGIC},
375962cf420SMaxim Sobolev 		{NULL,      0L}};
376dca60efcSMarcel Moolenaar 
377962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
378962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
379962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
380dca60efcSMarcel Moolenaar 
381dca60efcSMarcel Moolenaar 	return (0L);
382dca60efcSMarcel Moolenaar }
383dca60efcSMarcel Moolenaar 
384f7a25872SJohn Baldwin static void
385d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
386f7a25872SJohn Baldwin {
387f7a25872SJohn Baldwin 
388f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
389f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
390f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
391f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
392f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
393f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
394f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
395f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
396f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
397f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
398f7a25872SJohn Baldwin }
399f7a25872SJohn Baldwin 
400c21dee17SSøren Schmidt int
401b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
402c21dee17SSøren Schmidt {
4035002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
404f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
405206a5d3aSIan Dowse 	char *path;
4062166e4e0SDmitry Chagin 	int error;
407d66a5066SPeter Wemm 
408206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
409c21dee17SSøren Schmidt 
410c21dee17SSøren Schmidt #ifdef DEBUG
41124593369SJonathan Lemon 	if (ldebug(statfs))
412206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
413c21dee17SSøren Schmidt #endif
414f7a25872SJohn Baldwin 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
415206a5d3aSIan Dowse 	LFREEPATH(path);
416d5558c00SPeter Wemm 	if (error)
417eddc160eSRobert Watson 		return (error);
418d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
4194b7ef73dSDag-Erling Smørgrav 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
420c21dee17SSøren Schmidt }
421c21dee17SSøren Schmidt 
4227f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
423835e5061SAlexander Leidinger static void
424835e5061SAlexander Leidinger bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
425835e5061SAlexander Leidinger {
426835e5061SAlexander Leidinger 
427835e5061SAlexander Leidinger 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
428835e5061SAlexander Leidinger 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
429835e5061SAlexander Leidinger 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
430835e5061SAlexander Leidinger 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
431835e5061SAlexander Leidinger 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
432835e5061SAlexander Leidinger 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
433835e5061SAlexander Leidinger 	linux_statfs->f_files = bsd_statfs->f_files;
434835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
435835e5061SAlexander Leidinger 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
436835e5061SAlexander Leidinger 	linux_statfs->f_namelen = MAXNAMLEN;
437835e5061SAlexander Leidinger }
438835e5061SAlexander Leidinger 
439835e5061SAlexander Leidinger int
440835e5061SAlexander Leidinger linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
441835e5061SAlexander Leidinger {
442835e5061SAlexander Leidinger 	struct l_statfs64 linux_statfs;
443835e5061SAlexander Leidinger 	struct statfs bsd_statfs;
444835e5061SAlexander Leidinger 	char *path;
445835e5061SAlexander Leidinger 	int error;
446835e5061SAlexander Leidinger 
4473ab85269SDavid Malone 	if (args->bufsize != sizeof(struct l_statfs64))
4483ab85269SDavid Malone 		return EINVAL;
4493ab85269SDavid Malone 
450835e5061SAlexander Leidinger 	LCONVPATHEXIST(td, args->path, &path);
451835e5061SAlexander Leidinger 
452835e5061SAlexander Leidinger #ifdef DEBUG
453835e5061SAlexander Leidinger 	if (ldebug(statfs64))
454835e5061SAlexander Leidinger 		printf(ARGS(statfs64, "%s, *"), path);
455835e5061SAlexander Leidinger #endif
456835e5061SAlexander Leidinger 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
457835e5061SAlexander Leidinger 	LFREEPATH(path);
458835e5061SAlexander Leidinger 	if (error)
459835e5061SAlexander Leidinger 		return (error);
460835e5061SAlexander Leidinger 	bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
461835e5061SAlexander Leidinger 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
462835e5061SAlexander Leidinger }
463*99546279SDmitry Chagin 
464*99546279SDmitry Chagin int
465*99546279SDmitry Chagin linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
466*99546279SDmitry Chagin {
467*99546279SDmitry Chagin 	struct l_statfs64 linux_statfs;
468*99546279SDmitry Chagin 	struct statfs bsd_statfs;
469*99546279SDmitry Chagin 	int error;
470*99546279SDmitry Chagin 
471*99546279SDmitry Chagin #ifdef DEBUG
472*99546279SDmitry Chagin 	if (ldebug(fstatfs64))
473*99546279SDmitry Chagin 		printf(ARGS(fstatfs64, "%d, *"), args->fd);
474*99546279SDmitry Chagin #endif
475*99546279SDmitry Chagin 	if (args->bufsize != sizeof(struct l_statfs64))
476*99546279SDmitry Chagin 		return (EINVAL);
477*99546279SDmitry Chagin 
478*99546279SDmitry Chagin 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
479*99546279SDmitry Chagin 	if (error)
480*99546279SDmitry Chagin 		return error;
481*99546279SDmitry Chagin 	bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
482*99546279SDmitry Chagin 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
483*99546279SDmitry Chagin }
4847f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
485835e5061SAlexander Leidinger 
486c21dee17SSøren Schmidt int
487b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
488c21dee17SSøren Schmidt {
4895002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
490f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
491c21dee17SSøren Schmidt 	int error;
492c21dee17SSøren Schmidt 
493c21dee17SSøren Schmidt #ifdef DEBUG
49424593369SJonathan Lemon 	if (ldebug(fstatfs))
49524593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
496c21dee17SSøren Schmidt #endif
497f7a25872SJohn Baldwin 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
498d5558c00SPeter Wemm 	if (error)
499c21dee17SSøren Schmidt 		return error;
500d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
501f7a25872SJohn Baldwin 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
502c21dee17SSøren Schmidt }
503408da119SMarcel Moolenaar 
5045002a60fSMarcel Moolenaar struct l_ustat
505408da119SMarcel Moolenaar {
5065002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
5075002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
5085002a60fSMarcel Moolenaar 	char		f_fname[6];
5095002a60fSMarcel Moolenaar 	char		f_fpack[6];
5105002a60fSMarcel Moolenaar };
5115002a60fSMarcel Moolenaar 
5125002a60fSMarcel Moolenaar int
513b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
5145002a60fSMarcel Moolenaar {
5151e247cc2SPoul-Henning Kamp #ifdef DEBUG
5161e247cc2SPoul-Henning Kamp 	if (ldebug(ustat))
5174ca75bedSDmitry Chagin 		printf(ARGS(ustat, "%ju, *"), (uintmax_t)args->dev);
5181e247cc2SPoul-Henning Kamp #endif
5191e247cc2SPoul-Henning Kamp 
5201e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
521408da119SMarcel Moolenaar }
5225002a60fSMarcel Moolenaar 
5231997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
5245002a60fSMarcel Moolenaar 
5255002a60fSMarcel Moolenaar static int
5265002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
5275002a60fSMarcel Moolenaar {
5285002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
5295002a60fSMarcel Moolenaar 
5305002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
531a4611ab6SEd Schouten 	lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
5325002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
5335002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
5345002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
5355002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
5365002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
5375002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
5385002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
539510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
540510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
541510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
542510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
543510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
544510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
5455002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
5465002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
5475002a60fSMarcel Moolenaar 
5485002a60fSMarcel Moolenaar 	/*
5495002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
5505002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
5515002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
5525002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
5535002a60fSMarcel Moolenaar 	 */
5545002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
5555002a60fSMarcel Moolenaar 
5565002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
5575002a60fSMarcel Moolenaar }
5585002a60fSMarcel Moolenaar 
5595002a60fSMarcel Moolenaar int
560b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
5615002a60fSMarcel Moolenaar {
5625002a60fSMarcel Moolenaar 	struct stat buf;
563206a5d3aSIan Dowse 	char *filename;
564f7a25872SJohn Baldwin 	int error;
5655002a60fSMarcel Moolenaar 
566206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5675002a60fSMarcel Moolenaar 
5685002a60fSMarcel Moolenaar #ifdef DEBUG
5695002a60fSMarcel Moolenaar 	if (ldebug(stat64))
570206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
5715002a60fSMarcel Moolenaar #endif
5725002a60fSMarcel Moolenaar 
5730eee862aSEd Schouten 	error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
574206a5d3aSIan Dowse 	LFREEPATH(filename);
5755002a60fSMarcel Moolenaar 	if (error)
5765002a60fSMarcel Moolenaar 		return (error);
5775002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
5785002a60fSMarcel Moolenaar }
5795002a60fSMarcel Moolenaar 
5805002a60fSMarcel Moolenaar int
581b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
5825002a60fSMarcel Moolenaar {
5835002a60fSMarcel Moolenaar 	struct stat sb;
584206a5d3aSIan Dowse 	char *filename;
585f7a25872SJohn Baldwin 	int error;
5865002a60fSMarcel Moolenaar 
587206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5885002a60fSMarcel Moolenaar 
5895002a60fSMarcel Moolenaar #ifdef DEBUG
5905002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
5915002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
5925002a60fSMarcel Moolenaar #endif
5935002a60fSMarcel Moolenaar 
5940eee862aSEd Schouten 	error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
595206a5d3aSIan Dowse 	LFREEPATH(filename);
5965002a60fSMarcel Moolenaar 	if (error)
5975002a60fSMarcel Moolenaar 		return (error);
5985002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
5995002a60fSMarcel Moolenaar }
6005002a60fSMarcel Moolenaar 
6015002a60fSMarcel Moolenaar int
602b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
6035002a60fSMarcel Moolenaar {
6045002a60fSMarcel Moolenaar 	struct stat buf;
6055002a60fSMarcel Moolenaar 	int error;
6065002a60fSMarcel Moolenaar 
6075002a60fSMarcel Moolenaar #ifdef DEBUG
6085002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
6095002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
6105002a60fSMarcel Moolenaar #endif
6115002a60fSMarcel Moolenaar 
612f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
613060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
6145002a60fSMarcel Moolenaar 	if (!error)
6155002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
6165002a60fSMarcel Moolenaar 
6175002a60fSMarcel Moolenaar 	return (error);
6185002a60fSMarcel Moolenaar }
6195002a60fSMarcel Moolenaar 
62048b05c3fSKonstantin Belousov int
62148b05c3fSKonstantin Belousov linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
62248b05c3fSKonstantin Belousov {
62348b05c3fSKonstantin Belousov 	char *path;
62448b05c3fSKonstantin Belousov 	int error, dfd, flag;
62548b05c3fSKonstantin Belousov 	struct stat buf;
62648b05c3fSKonstantin Belousov 
62748b05c3fSKonstantin Belousov 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
62848b05c3fSKonstantin Belousov 		return (EINVAL);
62948b05c3fSKonstantin Belousov 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
63048b05c3fSKonstantin Belousov 	    AT_SYMLINK_NOFOLLOW : 0;
63148b05c3fSKonstantin Belousov 
63248b05c3fSKonstantin Belousov 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
63348b05c3fSKonstantin Belousov 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
63448b05c3fSKonstantin Belousov 
63548b05c3fSKonstantin Belousov #ifdef DEBUG
63648b05c3fSKonstantin Belousov 	if (ldebug(fstatat64))
63748b05c3fSKonstantin Belousov 		printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
63848b05c3fSKonstantin Belousov #endif
63948b05c3fSKonstantin Belousov 
6400eee862aSEd Schouten 	error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
64148b05c3fSKonstantin Belousov 	if (!error)
64248b05c3fSKonstantin Belousov 		error = stat64_copyout(&buf, args->statbuf);
64348b05c3fSKonstantin Belousov 	LFREEPATH(path);
64448b05c3fSKonstantin Belousov 
64548b05c3fSKonstantin Belousov 	return (error);
64648b05c3fSKonstantin Belousov }
64748b05c3fSKonstantin Belousov 
648606bcc17SDmitry Chagin #else /* __amd64__ && !COMPAT_LINUX32 */
649606bcc17SDmitry Chagin 
650606bcc17SDmitry Chagin int
651606bcc17SDmitry Chagin linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
652606bcc17SDmitry Chagin {
653606bcc17SDmitry Chagin 	char *path;
654606bcc17SDmitry Chagin 	int error, dfd, flag;
655606bcc17SDmitry Chagin 	struct stat buf;
656606bcc17SDmitry Chagin 
657606bcc17SDmitry Chagin 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
658606bcc17SDmitry Chagin 		return (EINVAL);
659606bcc17SDmitry Chagin 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
660606bcc17SDmitry Chagin 	    AT_SYMLINK_NOFOLLOW : 0;
661606bcc17SDmitry Chagin 
662606bcc17SDmitry Chagin 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
663606bcc17SDmitry Chagin 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
664606bcc17SDmitry Chagin 
665606bcc17SDmitry Chagin #ifdef DEBUG
666606bcc17SDmitry Chagin 	if (ldebug(newfstatat))
667606bcc17SDmitry Chagin 		printf(ARGS(newfstatat, "%i, %s, %i"), args->dfd, path, args->flag);
668606bcc17SDmitry Chagin #endif
669606bcc17SDmitry Chagin 
670606bcc17SDmitry Chagin 	error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
671606bcc17SDmitry Chagin 	if (error == 0)
672606bcc17SDmitry Chagin 		error = newstat_copyout(&buf, args->statbuf);
673606bcc17SDmitry Chagin 	LFREEPATH(path);
674606bcc17SDmitry Chagin 
675606bcc17SDmitry Chagin 	return (error);
676606bcc17SDmitry Chagin }
677606bcc17SDmitry Chagin 
6781997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
6799802eb9eSDmitry Chagin 
6809802eb9eSDmitry Chagin int
6819802eb9eSDmitry Chagin linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
6829802eb9eSDmitry Chagin {
6839802eb9eSDmitry Chagin 	cap_rights_t rights;
6849802eb9eSDmitry Chagin 	struct mount *mp;
6859802eb9eSDmitry Chagin 	struct vnode *vp;
6869802eb9eSDmitry Chagin 	int error, save;
6879802eb9eSDmitry Chagin 
6889802eb9eSDmitry Chagin 	error = fgetvp(td, args->fd, cap_rights_init(&rights, CAP_FSYNC), &vp);
6899802eb9eSDmitry Chagin 	if (error != 0)
6909802eb9eSDmitry Chagin 		/*
6919802eb9eSDmitry Chagin 		 * Linux syncfs() returns only EBADF, however fgetvp()
6929802eb9eSDmitry Chagin 		 * can return EINVAL in case of file descriptor does
6939802eb9eSDmitry Chagin 		 * not represent a vnode. XXX.
6949802eb9eSDmitry Chagin 		 */
6959802eb9eSDmitry Chagin 		return (error);
6969802eb9eSDmitry Chagin 
6979802eb9eSDmitry Chagin 	mp = vp->v_mount;
6989802eb9eSDmitry Chagin 	mtx_lock(&mountlist_mtx);
6999802eb9eSDmitry Chagin 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
7009802eb9eSDmitry Chagin 	if (error != 0) {
7019802eb9eSDmitry Chagin 		/* See comment above. */
7029802eb9eSDmitry Chagin 		mtx_unlock(&mountlist_mtx);
7039802eb9eSDmitry Chagin 		goto out;
7049802eb9eSDmitry Chagin 	}
7059802eb9eSDmitry Chagin 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
7069802eb9eSDmitry Chagin 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
7079802eb9eSDmitry Chagin 		save = curthread_pflags_set(TDP_SYNCIO);
7089802eb9eSDmitry Chagin 		vfs_msync(mp, MNT_NOWAIT);
7099802eb9eSDmitry Chagin 		VFS_SYNC(mp, MNT_NOWAIT);
7109802eb9eSDmitry Chagin 		curthread_pflags_restore(save);
7119802eb9eSDmitry Chagin 		vn_finished_write(mp);
7129802eb9eSDmitry Chagin 	}
7139802eb9eSDmitry Chagin 	vfs_unbusy(mp);
7149802eb9eSDmitry Chagin 
7159802eb9eSDmitry Chagin  out:
7169802eb9eSDmitry Chagin 	vrele(vp);
7179802eb9eSDmitry Chagin 	return (error);
7189802eb9eSDmitry Chagin }
719