xref: /freebsd/sys/compat/linux/linux_stats.c (revision 297f61cc01609f3420e2fe545aa71fbd00b2321b)
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>
35c21dee17SSøren Schmidt #include <sys/dirent.h>
36c21dee17SSøren Schmidt #include <sys/file.h>
37c21dee17SSøren Schmidt #include <sys/filedesc.h>
38c21dee17SSøren Schmidt #include <sys/proc.h>
3985422e62SBruce Evans #include <sys/malloc.h>
40c21dee17SSøren Schmidt #include <sys/mount.h>
41c21dee17SSøren Schmidt #include <sys/namei.h>
42c21dee17SSøren Schmidt #include <sys/stat.h>
43f7a25872SJohn Baldwin #include <sys/syscallsubr.h>
44408da119SMarcel Moolenaar #include <sys/systm.h>
45bc093719SEd Schouten #include <sys/tty.h>
46c21dee17SSøren Schmidt #include <sys/vnode.h>
47060e4882SDoug Ambrisko #include <sys/conf.h>
48060e4882SDoug Ambrisko #include <sys/fcntl.h>
49c21dee17SSøren Schmidt 
501997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32
514af27623STim J. Robbins #include <machine/../linux32/linux.h>
524af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h>
531997c537SDavid E. O'Brien #else
541997c537SDavid E. O'Brien #include <machine/../linux/linux.h>
551997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h>
564af27623STim J. Robbins #endif
5785422e62SBruce Evans 
58ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h>
5948b05c3fSKonstantin Belousov #include <compat/linux/linux_file.h>
60762e6b85SEivind Eklund 
61529844c7SAlexander Leidinger #define	LINUX_SHMFS_MAGIC 0x01021994
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;
142060e4882SDoug Ambrisko 	int major, minor;
143060e4882SDoug Ambrisko 
144a9d2f8d8SRobert Watson 	/*
145a9d2f8d8SRobert Watson 	 * No capability rights required here.
146a9d2f8d8SRobert Watson 	 */
147b256a1e1SJung-uk Kim 	if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
148a9d2f8d8SRobert Watson 	    fget(td, fd, 0, &fp) != 0)
149060e4882SDoug Ambrisko 		return;
1500eee862aSEd Schouten 	vp = fp->f_vnode;
1510eee862aSEd Schouten 	if (vp != NULL && vp->v_rdev != NULL &&
1527870adb6SEd Schouten 	    linux_driver_get_major_minor(devtoname(vp->v_rdev),
153bc093719SEd Schouten 					 &major, &minor) == 0) {
154060e4882SDoug Ambrisko 		buf->st_rdev = (major << 8 | minor);
155bc093719SEd Schouten 	} else if (fp->f_type == DTYPE_PTS) {
156bc093719SEd Schouten 		struct tty *tp = fp->f_data;
157bc093719SEd Schouten 
158bc093719SEd Schouten 		/* Convert the numbers for the slave device. */
1597870adb6SEd Schouten 		if (linux_driver_get_major_minor(devtoname(tp->t_dev),
160bc093719SEd Schouten 					 &major, &minor) == 0) {
161bc093719SEd Schouten 			buf->st_rdev = (major << 8 | minor);
162bc093719SEd Schouten 		}
163bc093719SEd Schouten 	}
164060e4882SDoug Ambrisko 	fdrop(fp, td);
165060e4882SDoug Ambrisko }
166060e4882SDoug Ambrisko 
167bbbc2d96SPoul-Henning Kamp static int
168bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf)
169bbbc2d96SPoul-Henning Kamp {
170bbbc2d96SPoul-Henning Kamp 	struct l_newstat tbuf;
171bbbc2d96SPoul-Henning Kamp 
172bbbc2d96SPoul-Henning Kamp 	bzero(&tbuf, sizeof(tbuf));
173a4611ab6SEd Schouten 	tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
174bbbc2d96SPoul-Henning Kamp 	tbuf.st_ino = buf->st_ino;
175bbbc2d96SPoul-Henning Kamp 	tbuf.st_mode = buf->st_mode;
176bbbc2d96SPoul-Henning Kamp 	tbuf.st_nlink = buf->st_nlink;
177bbbc2d96SPoul-Henning Kamp 	tbuf.st_uid = buf->st_uid;
178bbbc2d96SPoul-Henning Kamp 	tbuf.st_gid = buf->st_gid;
179bbbc2d96SPoul-Henning Kamp 	tbuf.st_rdev = buf->st_rdev;
180bbbc2d96SPoul-Henning Kamp 	tbuf.st_size = buf->st_size;
181510ea843SEd Schouten 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
182510ea843SEd Schouten 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
183510ea843SEd Schouten 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
184510ea843SEd Schouten 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
185510ea843SEd Schouten 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
186510ea843SEd Schouten 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
187bbbc2d96SPoul-Henning Kamp 	tbuf.st_blksize = buf->st_blksize;
188bbbc2d96SPoul-Henning Kamp 	tbuf.st_blocks = buf->st_blocks;
189bbbc2d96SPoul-Henning Kamp 
1904e0eaf69SMarcel Moolenaar 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
191c21dee17SSøren Schmidt }
192c21dee17SSøren Schmidt 
193c21dee17SSøren Schmidt int
194b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args)
195c21dee17SSøren Schmidt {
196c21dee17SSøren Schmidt 	struct stat buf;
197206a5d3aSIan Dowse 	char *path;
198c21dee17SSøren Schmidt 	int error;
199d66a5066SPeter Wemm 
200206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
201c21dee17SSøren Schmidt 
202c21dee17SSøren Schmidt #ifdef DEBUG
20324593369SJonathan Lemon 	if (ldebug(newstat))
204206a5d3aSIan Dowse 		printf(ARGS(newstat, "%s, *"), path);
205c21dee17SSøren Schmidt #endif
2064e0eaf69SMarcel Moolenaar 
2070eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
208206a5d3aSIan Dowse 	LFREEPATH(path);
2094e0eaf69SMarcel Moolenaar 	if (error)
2104e0eaf69SMarcel Moolenaar 		return (error);
2114e0eaf69SMarcel Moolenaar 	return (newstat_copyout(&buf, args->buf));
212c21dee17SSøren Schmidt }
213c21dee17SSøren Schmidt 
214c21dee17SSøren Schmidt int
215b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
216c21dee17SSøren Schmidt {
2174e0eaf69SMarcel Moolenaar 	struct stat sb;
218206a5d3aSIan Dowse 	char *path;
219f7a25872SJohn Baldwin 	int error;
220d66a5066SPeter Wemm 
221206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
222c21dee17SSøren Schmidt 
223c21dee17SSøren Schmidt #ifdef DEBUG
22424593369SJonathan Lemon 	if (ldebug(newlstat))
225206a5d3aSIan Dowse 		printf(ARGS(newlstat, "%s, *"), path);
226c21dee17SSøren Schmidt #endif
2274e0eaf69SMarcel Moolenaar 
2280eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
229206a5d3aSIan Dowse 	LFREEPATH(path);
230d66a5066SPeter Wemm 	if (error)
231d66a5066SPeter Wemm 		return (error);
2325002a60fSMarcel Moolenaar 	return (newstat_copyout(&sb, args->buf));
233d66a5066SPeter Wemm }
234c21dee17SSøren Schmidt 
235c21dee17SSøren Schmidt int
236b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
237c21dee17SSøren Schmidt {
238c21dee17SSøren Schmidt 	struct stat buf;
239c21dee17SSøren Schmidt 	int error;
240c21dee17SSøren Schmidt 
241c21dee17SSøren Schmidt #ifdef DEBUG
24224593369SJonathan Lemon 	if (ldebug(newfstat))
24324593369SJonathan Lemon 		printf(ARGS(newfstat, "%d, *"), args->fd);
244c21dee17SSøren Schmidt #endif
2454e0eaf69SMarcel Moolenaar 
246f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
247060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
248c21dee17SSøren Schmidt 	if (!error)
249c21dee17SSøren Schmidt 		error = newstat_copyout(&buf, args->buf);
2504e0eaf69SMarcel Moolenaar 
2514e0eaf69SMarcel Moolenaar 	return (error);
252c21dee17SSøren Schmidt }
253c21dee17SSøren Schmidt 
2545c8919adSAlexander Leidinger static int
2555c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf)
2565c8919adSAlexander Leidinger {
2575c8919adSAlexander Leidinger 	struct l_stat lbuf;
2585c8919adSAlexander Leidinger 
2595c8919adSAlexander Leidinger 	bzero(&lbuf, sizeof(lbuf));
2605c8919adSAlexander Leidinger 	lbuf.st_dev = buf->st_dev;
2615c8919adSAlexander Leidinger 	lbuf.st_ino = buf->st_ino;
2625c8919adSAlexander Leidinger 	lbuf.st_mode = buf->st_mode;
2635c8919adSAlexander Leidinger 	lbuf.st_nlink = buf->st_nlink;
2645c8919adSAlexander Leidinger 	lbuf.st_uid = buf->st_uid;
2655c8919adSAlexander Leidinger 	lbuf.st_gid = buf->st_gid;
2665c8919adSAlexander Leidinger 	lbuf.st_rdev = buf->st_rdev;
2675c8919adSAlexander Leidinger 	if (buf->st_size < (quad_t)1 << 32)
2685c8919adSAlexander Leidinger 		lbuf.st_size = buf->st_size;
2695c8919adSAlexander Leidinger 	else
2705c8919adSAlexander Leidinger 		lbuf.st_size = -2;
271510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
272510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
273510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
274510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
275510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
276510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
2775c8919adSAlexander Leidinger 	lbuf.st_blksize = buf->st_blksize;
2785c8919adSAlexander Leidinger 	lbuf.st_blocks = buf->st_blocks;
2795c8919adSAlexander Leidinger 	lbuf.st_flags = buf->st_flags;
2805c8919adSAlexander Leidinger 	lbuf.st_gen = buf->st_gen;
2815c8919adSAlexander Leidinger 
2825c8919adSAlexander Leidinger 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
2835c8919adSAlexander Leidinger }
2845c8919adSAlexander Leidinger 
2855c8919adSAlexander Leidinger int
2865c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args)
2875c8919adSAlexander Leidinger {
2885c8919adSAlexander Leidinger 	struct stat buf;
28915b78ac5SKonstantin Belousov 	char *path;
2905c8919adSAlexander Leidinger 	int error;
29115b78ac5SKonstantin Belousov 
29215b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
29315b78ac5SKonstantin Belousov 
2945c8919adSAlexander Leidinger #ifdef DEBUG
2955c8919adSAlexander Leidinger 	if (ldebug(stat))
296d075105dSKonstantin Belousov 		printf(ARGS(stat, "%s, *"), path);
2975c8919adSAlexander Leidinger #endif
2980eee862aSEd Schouten 	error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
299d075105dSKonstantin Belousov 	if (error) {
30015b78ac5SKonstantin Belousov 		LFREEPATH(path);
3015c8919adSAlexander Leidinger 		return (error);
302d075105dSKonstantin Belousov 	}
303d075105dSKonstantin Belousov 	LFREEPATH(path);
3045c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
3055c8919adSAlexander Leidinger }
3065c8919adSAlexander Leidinger 
3075c8919adSAlexander Leidinger int
3085c8919adSAlexander Leidinger linux_lstat(struct thread *td, struct linux_lstat_args *args)
3095c8919adSAlexander Leidinger {
3105c8919adSAlexander Leidinger 	struct stat buf;
31115b78ac5SKonstantin Belousov 	char *path;
3125c8919adSAlexander Leidinger 	int error;
3135c8919adSAlexander Leidinger 
31415b78ac5SKonstantin Belousov 	LCONVPATHEXIST(td, args->path, &path);
31515b78ac5SKonstantin Belousov 
3165c8919adSAlexander Leidinger #ifdef DEBUG
3175c8919adSAlexander Leidinger 	if (ldebug(lstat))
318d075105dSKonstantin Belousov 		printf(ARGS(lstat, "%s, *"), path);
3195c8919adSAlexander Leidinger #endif
3200eee862aSEd Schouten 	error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
321d075105dSKonstantin Belousov 	if (error) {
32215b78ac5SKonstantin Belousov 		LFREEPATH(path);
3235c8919adSAlexander Leidinger 		return (error);
324d075105dSKonstantin Belousov 	}
325d075105dSKonstantin Belousov 	LFREEPATH(path);
3265c8919adSAlexander Leidinger 	return(stat_copyout(&buf, args->up));
3275c8919adSAlexander Leidinger }
3285c8919adSAlexander Leidinger 
3295002a60fSMarcel Moolenaar struct l_statfs {
330*297f61ccSDmitry Chagin 	l_long		f_type;
331*297f61ccSDmitry Chagin 	l_long		f_bsize;
332*297f61ccSDmitry Chagin 	l_long		f_blocks;
333*297f61ccSDmitry Chagin 	l_long		f_bfree;
334*297f61ccSDmitry Chagin 	l_long		f_bavail;
335*297f61ccSDmitry Chagin 	l_long		f_files;
336*297f61ccSDmitry Chagin 	l_long		f_ffree;
3375002a60fSMarcel Moolenaar 	l_fsid_t	f_fsid;
338*297f61ccSDmitry Chagin 	l_long		f_namelen;
339*297f61ccSDmitry Chagin 	l_long		f_spare[6];
340c21dee17SSøren Schmidt };
341c21dee17SSøren Schmidt 
342dca60efcSMarcel Moolenaar #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
343dca60efcSMarcel Moolenaar #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
344dca60efcSMarcel Moolenaar #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
345dca60efcSMarcel Moolenaar #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
346dca60efcSMarcel Moolenaar #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
347dca60efcSMarcel Moolenaar #define	LINUX_NCP_SUPER_MAGIC	0x564cL
348dca60efcSMarcel Moolenaar #define	LINUX_NFS_SUPER_MAGIC	0x6969L
349dca60efcSMarcel Moolenaar #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
350dca60efcSMarcel Moolenaar #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
351dca60efcSMarcel Moolenaar #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
352e83d253bSOlivier Houchard #define LINUX_DEVFS_SUPER_MAGIC	0x1373L
353dca60efcSMarcel Moolenaar 
354dca60efcSMarcel Moolenaar static long
355962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename)
356dca60efcSMarcel Moolenaar {
357962cf420SMaxim Sobolev 	int i;
358962cf420SMaxim Sobolev 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
359962cf420SMaxim Sobolev 		{"ufs",     LINUX_UFS_SUPER_MAGIC},
360962cf420SMaxim Sobolev 		{"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
361962cf420SMaxim Sobolev 		{"nfs",     LINUX_NFS_SUPER_MAGIC},
362962cf420SMaxim Sobolev 		{"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
363962cf420SMaxim Sobolev 		{"procfs",  LINUX_PROC_SUPER_MAGIC},
364962cf420SMaxim Sobolev 		{"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
365962cf420SMaxim Sobolev 		{"ntfs",    LINUX_NTFS_SUPER_MAGIC},
366962cf420SMaxim Sobolev 		{"nwfs",    LINUX_NCP_SUPER_MAGIC},
367962cf420SMaxim Sobolev 		{"hpfs",    LINUX_HPFS_SUPER_MAGIC},
368962cf420SMaxim Sobolev 		{"coda",    LINUX_CODA_SUPER_MAGIC},
369e83d253bSOlivier Houchard 		{"devfs",   LINUX_DEVFS_SUPER_MAGIC},
370962cf420SMaxim Sobolev 		{NULL,      0L}};
371dca60efcSMarcel Moolenaar 
372962cf420SMaxim Sobolev 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
373962cf420SMaxim Sobolev 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
374962cf420SMaxim Sobolev 			return (b2l_tbl[i].linux_type);
375dca60efcSMarcel Moolenaar 
376dca60efcSMarcel Moolenaar 	return (0L);
377dca60efcSMarcel Moolenaar }
378dca60efcSMarcel Moolenaar 
379f7a25872SJohn Baldwin static void
380d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
381f7a25872SJohn Baldwin {
382f7a25872SJohn Baldwin 
383f7a25872SJohn Baldwin 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
384f7a25872SJohn Baldwin 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
385f7a25872SJohn Baldwin 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
386f7a25872SJohn Baldwin 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
387f7a25872SJohn Baldwin 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
388f7a25872SJohn Baldwin 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
389f7a25872SJohn Baldwin 	linux_statfs->f_files = bsd_statfs->f_files;
390f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
391f7a25872SJohn Baldwin 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
392f7a25872SJohn Baldwin 	linux_statfs->f_namelen = MAXNAMLEN;
393f7a25872SJohn Baldwin }
394f7a25872SJohn Baldwin 
395c21dee17SSøren Schmidt int
396b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args)
397c21dee17SSøren Schmidt {
3985002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
399f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
400206a5d3aSIan Dowse 	char *path;
401529844c7SAlexander Leidinger 	int error, dev_shm;
402d66a5066SPeter Wemm 
403206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->path, &path);
404c21dee17SSøren Schmidt 
405c21dee17SSøren Schmidt #ifdef DEBUG
40624593369SJonathan Lemon 	if (ldebug(statfs))
407206a5d3aSIan Dowse 		printf(ARGS(statfs, "%s, *"), path);
408c21dee17SSøren Schmidt #endif
409529844c7SAlexander Leidinger 	dev_shm = 0;
410f7a25872SJohn Baldwin 	error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
411529844c7SAlexander Leidinger 	if (strncmp(path, "/dev/shm", sizeof("/dev/shm") - 1) == 0)
412529844c7SAlexander Leidinger 		dev_shm = (path[8] == '\0'
413529844c7SAlexander Leidinger 		    || (path[8] == '/' && path[9] == '\0'));
414206a5d3aSIan Dowse 	LFREEPATH(path);
415d5558c00SPeter Wemm 	if (error)
416eddc160eSRobert Watson 		return (error);
417d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
418529844c7SAlexander Leidinger 	if (dev_shm)
419529844c7SAlexander Leidinger 		linux_statfs.f_type = LINUX_SHMFS_MAGIC;
4204b7ef73dSDag-Erling Smørgrav 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
421c21dee17SSøren Schmidt }
422c21dee17SSøren Schmidt 
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 }
463835e5061SAlexander Leidinger 
464c21dee17SSøren Schmidt int
465b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
466c21dee17SSøren Schmidt {
4675002a60fSMarcel Moolenaar 	struct l_statfs linux_statfs;
468f7a25872SJohn Baldwin 	struct statfs bsd_statfs;
469c21dee17SSøren Schmidt 	int error;
470c21dee17SSøren Schmidt 
471c21dee17SSøren Schmidt #ifdef DEBUG
47224593369SJonathan Lemon 	if (ldebug(fstatfs))
47324593369SJonathan Lemon 		printf(ARGS(fstatfs, "%d, *"), args->fd);
474c21dee17SSøren Schmidt #endif
475f7a25872SJohn Baldwin 	error = kern_fstatfs(td, args->fd, &bsd_statfs);
476d5558c00SPeter Wemm 	if (error)
477c21dee17SSøren Schmidt 		return error;
478d0cad55dSPawel Jakub Dawidek 	bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
479f7a25872SJohn Baldwin 	return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
480c21dee17SSøren Schmidt }
481408da119SMarcel Moolenaar 
4825002a60fSMarcel Moolenaar struct l_ustat
483408da119SMarcel Moolenaar {
4845002a60fSMarcel Moolenaar 	l_daddr_t	f_tfree;
4855002a60fSMarcel Moolenaar 	l_ino_t		f_tinode;
4865002a60fSMarcel Moolenaar 	char		f_fname[6];
4875002a60fSMarcel Moolenaar 	char		f_fpack[6];
4885002a60fSMarcel Moolenaar };
4895002a60fSMarcel Moolenaar 
4905002a60fSMarcel Moolenaar int
491b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args)
4925002a60fSMarcel Moolenaar {
4931e247cc2SPoul-Henning Kamp #ifdef DEBUG
4941e247cc2SPoul-Henning Kamp 	if (ldebug(ustat))
4951e247cc2SPoul-Henning Kamp 		printf(ARGS(ustat, "%d, *"), args->dev);
4961e247cc2SPoul-Henning Kamp #endif
4971e247cc2SPoul-Henning Kamp 
4981e247cc2SPoul-Henning Kamp 	return (EOPNOTSUPP);
499408da119SMarcel Moolenaar }
5005002a60fSMarcel Moolenaar 
5011997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
5025002a60fSMarcel Moolenaar 
5035002a60fSMarcel Moolenaar static int
5045002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf)
5055002a60fSMarcel Moolenaar {
5065002a60fSMarcel Moolenaar 	struct l_stat64 lbuf;
5075002a60fSMarcel Moolenaar 
5085002a60fSMarcel Moolenaar 	bzero(&lbuf, sizeof(lbuf));
509a4611ab6SEd Schouten 	lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
5105002a60fSMarcel Moolenaar 	lbuf.st_ino = buf->st_ino;
5115002a60fSMarcel Moolenaar 	lbuf.st_mode = buf->st_mode;
5125002a60fSMarcel Moolenaar 	lbuf.st_nlink = buf->st_nlink;
5135002a60fSMarcel Moolenaar 	lbuf.st_uid = buf->st_uid;
5145002a60fSMarcel Moolenaar 	lbuf.st_gid = buf->st_gid;
5155002a60fSMarcel Moolenaar 	lbuf.st_rdev = buf->st_rdev;
5165002a60fSMarcel Moolenaar 	lbuf.st_size = buf->st_size;
517510ea843SEd Schouten 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
518510ea843SEd Schouten 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
519510ea843SEd Schouten 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
520510ea843SEd Schouten 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
521510ea843SEd Schouten 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
522510ea843SEd Schouten 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
5235002a60fSMarcel Moolenaar 	lbuf.st_blksize = buf->st_blksize;
5245002a60fSMarcel Moolenaar 	lbuf.st_blocks = buf->st_blocks;
5255002a60fSMarcel Moolenaar 
5265002a60fSMarcel Moolenaar 	/*
5275002a60fSMarcel Moolenaar 	 * The __st_ino field makes all the difference. In the Linux kernel
5285002a60fSMarcel Moolenaar 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
5295002a60fSMarcel Moolenaar 	 * but without the assignment to __st_ino the runtime linker refuses
5305002a60fSMarcel Moolenaar 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
5315002a60fSMarcel Moolenaar 	 */
5325002a60fSMarcel Moolenaar 	lbuf.__st_ino = buf->st_ino;
5335002a60fSMarcel Moolenaar 
5345002a60fSMarcel Moolenaar 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
5355002a60fSMarcel Moolenaar }
5365002a60fSMarcel Moolenaar 
5375002a60fSMarcel Moolenaar int
538b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args)
5395002a60fSMarcel Moolenaar {
5405002a60fSMarcel Moolenaar 	struct stat buf;
541206a5d3aSIan Dowse 	char *filename;
542f7a25872SJohn Baldwin 	int error;
5435002a60fSMarcel Moolenaar 
544206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5455002a60fSMarcel Moolenaar 
5465002a60fSMarcel Moolenaar #ifdef DEBUG
5475002a60fSMarcel Moolenaar 	if (ldebug(stat64))
548206a5d3aSIan Dowse 		printf(ARGS(stat64, "%s, *"), filename);
5495002a60fSMarcel Moolenaar #endif
5505002a60fSMarcel Moolenaar 
5510eee862aSEd Schouten 	error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
552206a5d3aSIan Dowse 	LFREEPATH(filename);
5535002a60fSMarcel Moolenaar 	if (error)
5545002a60fSMarcel Moolenaar 		return (error);
5555002a60fSMarcel Moolenaar 	return (stat64_copyout(&buf, args->statbuf));
5565002a60fSMarcel Moolenaar }
5575002a60fSMarcel Moolenaar 
5585002a60fSMarcel Moolenaar int
559b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
5605002a60fSMarcel Moolenaar {
5615002a60fSMarcel Moolenaar 	struct stat sb;
562206a5d3aSIan Dowse 	char *filename;
563f7a25872SJohn Baldwin 	int error;
5645002a60fSMarcel Moolenaar 
565206a5d3aSIan Dowse 	LCONVPATHEXIST(td, args->filename, &filename);
5665002a60fSMarcel Moolenaar 
5675002a60fSMarcel Moolenaar #ifdef DEBUG
5685002a60fSMarcel Moolenaar 	if (ldebug(lstat64))
5695002a60fSMarcel Moolenaar 		printf(ARGS(lstat64, "%s, *"), args->filename);
5705002a60fSMarcel Moolenaar #endif
5715002a60fSMarcel Moolenaar 
5720eee862aSEd Schouten 	error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
573206a5d3aSIan Dowse 	LFREEPATH(filename);
5745002a60fSMarcel Moolenaar 	if (error)
5755002a60fSMarcel Moolenaar 		return (error);
5765002a60fSMarcel Moolenaar 	return (stat64_copyout(&sb, args->statbuf));
5775002a60fSMarcel Moolenaar }
5785002a60fSMarcel Moolenaar 
5795002a60fSMarcel Moolenaar int
580b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
5815002a60fSMarcel Moolenaar {
5825002a60fSMarcel Moolenaar 	struct stat buf;
5835002a60fSMarcel Moolenaar 	int error;
5845002a60fSMarcel Moolenaar 
5855002a60fSMarcel Moolenaar #ifdef DEBUG
5865002a60fSMarcel Moolenaar 	if (ldebug(fstat64))
5875002a60fSMarcel Moolenaar 		printf(ARGS(fstat64, "%d, *"), args->fd);
5885002a60fSMarcel Moolenaar #endif
5895002a60fSMarcel Moolenaar 
590f7a25872SJohn Baldwin 	error = kern_fstat(td, args->fd, &buf);
591060e4882SDoug Ambrisko 	translate_fd_major_minor(td, args->fd, &buf);
5925002a60fSMarcel Moolenaar 	if (!error)
5935002a60fSMarcel Moolenaar 		error = stat64_copyout(&buf, args->statbuf);
5945002a60fSMarcel Moolenaar 
5955002a60fSMarcel Moolenaar 	return (error);
5965002a60fSMarcel Moolenaar }
5975002a60fSMarcel Moolenaar 
59848b05c3fSKonstantin Belousov int
59948b05c3fSKonstantin Belousov linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
60048b05c3fSKonstantin Belousov {
60148b05c3fSKonstantin Belousov 	char *path;
60248b05c3fSKonstantin Belousov 	int error, dfd, flag;
60348b05c3fSKonstantin Belousov 	struct stat buf;
60448b05c3fSKonstantin Belousov 
60548b05c3fSKonstantin Belousov 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
60648b05c3fSKonstantin Belousov 		return (EINVAL);
60748b05c3fSKonstantin Belousov 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
60848b05c3fSKonstantin Belousov 	    AT_SYMLINK_NOFOLLOW : 0;
60948b05c3fSKonstantin Belousov 
61048b05c3fSKonstantin Belousov 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
61148b05c3fSKonstantin Belousov 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
61248b05c3fSKonstantin Belousov 
61348b05c3fSKonstantin Belousov #ifdef DEBUG
61448b05c3fSKonstantin Belousov 	if (ldebug(fstatat64))
61548b05c3fSKonstantin Belousov 		printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
61648b05c3fSKonstantin Belousov #endif
61748b05c3fSKonstantin Belousov 
6180eee862aSEd Schouten 	error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
61948b05c3fSKonstantin Belousov 	if (!error)
62048b05c3fSKonstantin Belousov 		error = stat64_copyout(&buf, args->statbuf);
62148b05c3fSKonstantin Belousov 	LFREEPATH(path);
62248b05c3fSKonstantin Belousov 
62348b05c3fSKonstantin Belousov 	return (error);
62448b05c3fSKonstantin Belousov }
62548b05c3fSKonstantin Belousov 
6261997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
627