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