xref: /src/sys/compat/linux/linux_stats.c (revision a3c457398f269c913aaa4d9dedcc72a70c02e845)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994-1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "opt_ktrace.h"
30 
31 #include <sys/param.h>
32 #include <sys/capsicum.h>
33 #include <sys/dirent.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/namei.h>
38 #include <sys/proc.h>
39 #include <sys/stat.h>
40 #include <sys/syscallsubr.h>
41 #include <sys/tty.h>
42 #include <sys/vnode.h>
43 #ifdef KTRACE
44 #include <sys/ktrace.h>
45 #endif
46 
47 #include <security/audit/audit.h>
48 
49 #ifdef COMPAT_LINUX32
50 #include <machine/../linux32/linux.h>
51 #include <machine/../linux32/linux32_proto.h>
52 #else
53 #include <machine/../linux/linux.h>
54 #include <machine/../linux/linux_proto.h>
55 #endif
56 
57 #include <compat/linux/linux.h>
58 #include <compat/linux/linux_file.h>
59 #include <compat/linux/linux_util.h>
60 
61 
62 static int
linux_kern_fstat(struct thread * td,int fd,struct stat * sbp)63 linux_kern_fstat(struct thread *td, int fd, struct stat *sbp)
64 {
65 	struct vnode *vp;
66 	struct file *fp;
67 	int error;
68 
69 	AUDIT_ARG_FD(fd);
70 
71 	error = fget(td, fd, &cap_fstat_rights, &fp);
72 	if (__predict_false(error != 0))
73 		return (error);
74 
75 	AUDIT_ARG_FILE(td->td_proc, fp);
76 
77 	error = fo_stat(fp, sbp, td->td_ucred);
78 	if (error == 0 && (vp = fp->f_vnode) != NULL)
79 		translate_vnhook_major_minor(vp, sbp);
80 	fdrop(fp, td);
81 #ifdef KTRACE
82 	if (KTRPOINT(td, KTR_STRUCT))
83 		ktrstat_error(sbp, error);
84 #endif
85 	return (error);
86 }
87 
88 static int
linux_kern_statat(struct thread * td,int flag,int fd,const char * path,enum uio_seg pathseg,struct stat * sbp)89 linux_kern_statat(struct thread *td, int flag, int fd, const char *path,
90     enum uio_seg pathseg, struct stat *sbp)
91 {
92 	struct nameidata nd;
93 	int error;
94 
95 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
96 	    AT_EMPTY_PATH)) != 0)
97 		return (EINVAL);
98 
99 	NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
100 	    AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
101 	    AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
102 
103 	if ((error = namei(&nd)) != 0) {
104 		if (error == ENOTDIR &&
105 		    (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
106 			error = linux_kern_fstat(td, fd, sbp);
107 		return (error);
108 	}
109 	error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
110 	if (error == 0)
111 		translate_vnhook_major_minor(nd.ni_vp, sbp);
112 	NDFREE_PNBUF(&nd);
113 	vput(nd.ni_vp);
114 #ifdef KTRACE
115 	if (KTRPOINT(td, KTR_STRUCT))
116 		ktrstat_error(sbp, error);
117 #endif
118 	return (error);
119 }
120 
121 #ifdef LINUX_LEGACY_SYSCALLS
122 static int
linux_kern_stat(struct thread * td,const char * path,enum uio_seg pathseg,struct stat * sbp)123 linux_kern_stat(struct thread *td, const char *path, enum uio_seg pathseg,
124     struct stat *sbp)
125 {
126 
127 	return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
128 }
129 
130 static int
linux_kern_lstat(struct thread * td,const char * path,enum uio_seg pathseg,struct stat * sbp)131 linux_kern_lstat(struct thread *td, const char *path, enum uio_seg pathseg,
132     struct stat *sbp)
133 {
134 
135 	return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
136 	    pathseg, sbp));
137 }
138 #endif
139 
140 static int
newstat_copyout(struct stat * buf,void * ubuf)141 newstat_copyout(struct stat *buf, void *ubuf)
142 {
143 	struct l_newstat tbuf;
144 
145 	bzero(&tbuf, sizeof(tbuf));
146 	tbuf.st_dev = linux_new_encode_dev(buf->st_dev);
147 	tbuf.st_ino = buf->st_ino;
148 	tbuf.st_mode = buf->st_mode;
149 	tbuf.st_nlink = buf->st_nlink;
150 	tbuf.st_uid = buf->st_uid;
151 	tbuf.st_gid = buf->st_gid;
152 	tbuf.st_rdev = linux_new_encode_dev(buf->st_rdev);
153 	tbuf.st_size = buf->st_size;
154 	tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
155 	tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
156 	tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
157 	tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
158 	tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
159 	tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
160 	tbuf.st_blksize = buf->st_blksize;
161 	tbuf.st_blocks = buf->st_blocks;
162 
163 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
164 }
165 
166 
167 #ifdef LINUX_LEGACY_SYSCALLS
168 int
linux_newstat(struct thread * td,struct linux_newstat_args * args)169 linux_newstat(struct thread *td, struct linux_newstat_args *args)
170 {
171 	struct stat buf;
172 	int error;
173 
174 	error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
175 	if (error)
176 		return (error);
177 	return (newstat_copyout(&buf, args->buf));
178 }
179 
180 int
linux_newlstat(struct thread * td,struct linux_newlstat_args * args)181 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
182 {
183 	struct stat sb;
184 	int error;
185 
186 	error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &sb);
187 	if (error)
188 		return (error);
189 	return (newstat_copyout(&sb, args->buf));
190 }
191 #endif
192 
193 int
linux_newfstat(struct thread * td,struct linux_newfstat_args * args)194 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
195 {
196 	struct stat buf;
197 	int error;
198 
199 	error = linux_kern_fstat(td, args->fd, &buf);
200 	if (!error)
201 		error = newstat_copyout(&buf, args->buf);
202 
203 	return (error);
204 }
205 
206 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
207 
208 static __inline uint16_t
linux_old_encode_dev(dev_t _dev)209 linux_old_encode_dev(dev_t _dev)
210 {
211 
212 	return (_dev == NODEV ? 0 : linux_encode_dev(major(_dev), minor(_dev)));
213 }
214 
215 static int
old_stat_copyout(struct stat * buf,void * ubuf)216 old_stat_copyout(struct stat *buf, void *ubuf)
217 {
218 	struct l_old_stat lbuf;
219 
220 	bzero(&lbuf, sizeof(lbuf));
221 	lbuf.st_dev = linux_old_encode_dev(buf->st_dev);
222 	lbuf.st_ino = buf->st_ino;
223 	lbuf.st_mode = buf->st_mode;
224 	lbuf.st_nlink = buf->st_nlink;
225 	lbuf.st_uid = buf->st_uid;
226 	lbuf.st_gid = buf->st_gid;
227 	lbuf.st_rdev = linux_old_encode_dev(buf->st_rdev);
228 	lbuf.st_size = MIN(buf->st_size, INT32_MAX);
229 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
230 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
231 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
232 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
233 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
234 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
235 	lbuf.st_blksize = buf->st_blksize;
236 	lbuf.st_blocks = buf->st_blocks;
237 	lbuf.st_flags = buf->st_flags;
238 	lbuf.st_gen = buf->st_gen;
239 
240 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
241 }
242 
243 int
linux_stat(struct thread * td,struct linux_stat_args * args)244 linux_stat(struct thread *td, struct linux_stat_args *args)
245 {
246 	struct stat buf;
247 	int error;
248 
249 	error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
250 	if (error) {
251 		return (error);
252 	}
253 	return (old_stat_copyout(&buf, args->up));
254 }
255 
256 int
linux_lstat(struct thread * td,struct linux_lstat_args * args)257 linux_lstat(struct thread *td, struct linux_lstat_args *args)
258 {
259 	struct stat buf;
260 	int error;
261 
262 	error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &buf);
263 	if (error) {
264 		return (error);
265 	}
266 	return (old_stat_copyout(&buf, args->up));
267 }
268 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
269 
270 struct l_statfs {
271 	l_long		f_type;
272 	l_long		f_bsize;
273 	l_long		f_blocks;
274 	l_long		f_bfree;
275 	l_long		f_bavail;
276 	l_long		f_files;
277 	l_long		f_ffree;
278 	l_fsid_t	f_fsid;
279 	l_long		f_namelen;
280 	l_long		f_frsize;
281 	l_long		f_flags;
282 	l_long		f_spare[4];
283 };
284 
285 #define	LINUX_CODA_SUPER_MAGIC	0x73757245L
286 #define	LINUX_EXT2_SUPER_MAGIC	0xEF53L
287 #define	LINUX_HPFS_SUPER_MAGIC	0xf995e849L
288 #define	LINUX_ISOFS_SUPER_MAGIC	0x9660L
289 #define	LINUX_MSDOS_SUPER_MAGIC	0x4d44L
290 #define	LINUX_NCP_SUPER_MAGIC	0x564cL
291 #define	LINUX_NFS_SUPER_MAGIC	0x6969L
292 #define	LINUX_NTFS_SUPER_MAGIC	0x5346544EL
293 #define	LINUX_PROC_SUPER_MAGIC	0x9fa0L
294 #define	LINUX_UFS_SUPER_MAGIC	0x00011954L	/* XXX - UFS_MAGIC in Linux */
295 #define	LINUX_ZFS_SUPER_MAGIC	0x2FC12FC1
296 #define	LINUX_DEVFS_SUPER_MAGIC	0x1373L
297 #define	LINUX_SHMFS_MAGIC	0x01021994
298 #define	LINUX_SYSFS_MAGIC	0x62656572
299 
300 static long
bsd_to_linux_ftype(const char * fstypename)301 bsd_to_linux_ftype(const char *fstypename)
302 {
303 	int i;
304 	static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
305 		{"ufs",      LINUX_UFS_SUPER_MAGIC},
306 		{"zfs",      LINUX_ZFS_SUPER_MAGIC},
307 		{"cd9660",   LINUX_ISOFS_SUPER_MAGIC},
308 		{"nfs",      LINUX_NFS_SUPER_MAGIC},
309 		{"ext2fs",   LINUX_EXT2_SUPER_MAGIC},
310 		{"procfs",   LINUX_PROC_SUPER_MAGIC},
311 		{"msdosfs",  LINUX_MSDOS_SUPER_MAGIC},
312 		{"ntfs",     LINUX_NTFS_SUPER_MAGIC},
313 		{"nwfs",     LINUX_NCP_SUPER_MAGIC},
314 		{"hpfs",     LINUX_HPFS_SUPER_MAGIC},
315 		{"coda",     LINUX_CODA_SUPER_MAGIC},
316 		{"devfs",    LINUX_DEVFS_SUPER_MAGIC},
317 		{"tmpfs",    LINUX_SHMFS_MAGIC},
318 		{"linsysfs", LINUX_SYSFS_MAGIC},
319 		{NULL,       0L}};
320 
321 	for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
322 		if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
323 			return (b2l_tbl[i].linux_type);
324 
325 	return (0L);
326 }
327 
328 static int
bsd_to_linux_mnt_flags(int f_flags)329 bsd_to_linux_mnt_flags(int f_flags)
330 {
331 	int flags = LINUX_ST_VALID;
332 
333 	if (f_flags & MNT_RDONLY)
334 		flags |= LINUX_ST_RDONLY;
335 	if (f_flags & MNT_NOEXEC)
336 		flags |= LINUX_ST_NOEXEC;
337 	if (f_flags & MNT_NOSUID)
338 		flags |= LINUX_ST_NOSUID;
339 	if (f_flags & MNT_NOATIME)
340 		flags |= LINUX_ST_NOATIME;
341 	if (f_flags & MNT_NOSYMFOLLOW)
342 		flags |= LINUX_ST_NOSYMFOLLOW;
343 	if (f_flags & MNT_SYNCHRONOUS)
344 		flags |= LINUX_ST_SYNCHRONOUS;
345 
346 	return (flags);
347 }
348 
349 static int
bsd_to_linux_statfs(struct statfs * bsd_statfs,struct l_statfs * linux_statfs)350 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
351 {
352 
353 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
354 	statfs_scale_blocks(bsd_statfs, INT32_MAX);
355 #endif
356 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
357 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
358 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
359 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
360 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
361 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
362 	linux_statfs->f_ffree = MIN(bsd_statfs->f_ffree, INT32_MAX);
363 	linux_statfs->f_files = MIN(bsd_statfs->f_files, INT32_MAX);
364 #else
365 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
366 	linux_statfs->f_files = bsd_statfs->f_files;
367 #endif
368 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
369 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
370 	linux_statfs->f_namelen = MAXNAMLEN;
371 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
372 	linux_statfs->f_flags = bsd_to_linux_mnt_flags(bsd_statfs->f_flags);
373 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
374 
375 	return (0);
376 }
377 
378 int
linux_statfs(struct thread * td,struct linux_statfs_args * args)379 linux_statfs(struct thread *td, struct linux_statfs_args *args)
380 {
381 	struct l_statfs linux_statfs;
382 	struct statfs *bsd_statfs;
383 	int error;
384 
385 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
386 	error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
387 	if (error == 0)
388 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
389 	free(bsd_statfs, M_STATFS);
390 	if (error != 0)
391 		return (error);
392 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
393 }
394 
395 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
396 static void
bsd_to_linux_statfs64(struct statfs * bsd_statfs,struct l_statfs64 * linux_statfs)397 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
398 {
399 
400 	linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
401 	linux_statfs->f_bsize = bsd_statfs->f_bsize;
402 	linux_statfs->f_blocks = bsd_statfs->f_blocks;
403 	linux_statfs->f_bfree = bsd_statfs->f_bfree;
404 	linux_statfs->f_bavail = bsd_statfs->f_bavail;
405 	linux_statfs->f_ffree = bsd_statfs->f_ffree;
406 	linux_statfs->f_files = bsd_statfs->f_files;
407 	linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
408 	linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
409 	linux_statfs->f_namelen = MAXNAMLEN;
410 	linux_statfs->f_frsize = bsd_statfs->f_bsize;
411 	linux_statfs->f_flags = bsd_to_linux_mnt_flags(bsd_statfs->f_flags);
412 	memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
413 }
414 
415 int
linux_statfs64(struct thread * td,struct linux_statfs64_args * args)416 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
417 {
418 	struct l_statfs64 linux_statfs;
419 	struct statfs *bsd_statfs;
420 	int error;
421 
422 	if (args->bufsize != sizeof(struct l_statfs64))
423 		return (EINVAL);
424 
425 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
426 	error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
427 	if (error == 0)
428 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
429 	free(bsd_statfs, M_STATFS);
430 	if (error != 0)
431 		return (error);
432 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
433 }
434 
435 int
linux_fstatfs64(struct thread * td,struct linux_fstatfs64_args * args)436 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
437 {
438 	struct l_statfs64 linux_statfs;
439 	struct statfs *bsd_statfs;
440 	int error;
441 
442 	if (args->bufsize != sizeof(struct l_statfs64))
443 		return (EINVAL);
444 
445 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
446 	error = kern_fstatfs(td, args->fd, bsd_statfs);
447 	if (error == 0)
448 		bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
449 	free(bsd_statfs, M_STATFS);
450 	if (error != 0)
451 		return (error);
452 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
453 }
454 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
455 
456 int
linux_fstatfs(struct thread * td,struct linux_fstatfs_args * args)457 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
458 {
459 	struct l_statfs linux_statfs;
460 	struct statfs *bsd_statfs;
461 	int error;
462 
463 	bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
464 	error = kern_fstatfs(td, args->fd, bsd_statfs);
465 	if (error == 0)
466 		error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
467 	free(bsd_statfs, M_STATFS);
468 	if (error != 0)
469 		return (error);
470 	return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
471 }
472 
473 struct l_ustat
474 {
475 	l_daddr_t	f_tfree;
476 	l_ino_t		f_tinode;
477 	char		f_fname[6];
478 	char		f_fpack[6];
479 };
480 
481 #ifdef LINUX_LEGACY_SYSCALLS
482 int
linux_ustat(struct thread * td,struct linux_ustat_args * args)483 linux_ustat(struct thread *td, struct linux_ustat_args *args)
484 {
485 
486 	return (EOPNOTSUPP);
487 }
488 #endif
489 
490 /*
491  * Convert Linux stat flags to BSD flags.  Return value indicates successful
492  * conversion (no unknown flags).
493  */
494 static bool
linux_to_bsd_stat_flags(int linux_flags,int * out_flags)495 linux_to_bsd_stat_flags(int linux_flags, int *out_flags)
496 {
497 	int flags, unsupported;
498 
499 	unsupported = linux_flags & ~(LINUX_AT_SYMLINK_NOFOLLOW |
500 	    LINUX_AT_EMPTY_PATH | LINUX_AT_NO_AUTOMOUNT);
501 	if (unsupported != 0) {
502 		*out_flags = unsupported;
503 		return (false);
504 	}
505 
506 	flags = 0;
507 	if (linux_flags & LINUX_AT_SYMLINK_NOFOLLOW)
508 		flags |= AT_SYMLINK_NOFOLLOW;
509 	if (linux_flags & LINUX_AT_EMPTY_PATH)
510 		flags |= AT_EMPTY_PATH;
511 	*out_flags = flags;
512 	return (true);
513 }
514 
515 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
516 
517 static int
stat64_copyout(struct stat * buf,void * ubuf)518 stat64_copyout(struct stat *buf, void *ubuf)
519 {
520 	struct l_stat64 lbuf;
521 
522 	bzero(&lbuf, sizeof(lbuf));
523 	lbuf.st_dev = linux_new_encode_dev(buf->st_dev);
524 	lbuf.st_ino = buf->st_ino;
525 	lbuf.st_mode = buf->st_mode;
526 	lbuf.st_nlink = buf->st_nlink;
527 	lbuf.st_uid = buf->st_uid;
528 	lbuf.st_gid = buf->st_gid;
529 	lbuf.st_rdev = linux_new_encode_dev(buf->st_rdev);
530 	lbuf.st_size = buf->st_size;
531 	lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
532 	lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
533 	lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
534 	lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
535 	lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
536 	lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
537 	lbuf.st_blksize = buf->st_blksize;
538 	lbuf.st_blocks = buf->st_blocks;
539 
540 	/*
541 	 * The __st_ino field makes all the difference. In the Linux kernel
542 	 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
543 	 * but without the assignment to __st_ino the runtime linker refuses
544 	 * to mmap(2) any shared libraries. I guess it's broken alright :-)
545 	 */
546 	lbuf.__st_ino = buf->st_ino;
547 
548 	return (copyout(&lbuf, ubuf, sizeof(lbuf)));
549 }
550 
551 int
linux_stat64(struct thread * td,struct linux_stat64_args * args)552 linux_stat64(struct thread *td, struct linux_stat64_args *args)
553 {
554 	struct stat buf;
555 	int error;
556 
557 	error = linux_kern_stat(td, args->filename, UIO_USERSPACE, &buf);
558 	if (error)
559 		return (error);
560 	return (stat64_copyout(&buf, args->statbuf));
561 }
562 
563 int
linux_lstat64(struct thread * td,struct linux_lstat64_args * args)564 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
565 {
566 	struct stat sb;
567 	int error;
568 
569 	error = linux_kern_lstat(td, args->filename, UIO_USERSPACE, &sb);
570 	if (error)
571 		return (error);
572 	return (stat64_copyout(&sb, args->statbuf));
573 }
574 
575 int
linux_fstat64(struct thread * td,struct linux_fstat64_args * args)576 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
577 {
578 	struct stat buf;
579 	int error;
580 
581 	error = linux_kern_fstat(td, args->fd, &buf);
582 	if (!error)
583 		error = stat64_copyout(&buf, args->statbuf);
584 
585 	return (error);
586 }
587 
588 int
linux_fstatat64(struct thread * td,struct linux_fstatat64_args * args)589 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
590 {
591 	int error, dfd, flags;
592 	struct stat buf;
593 
594 	if (!linux_to_bsd_stat_flags(args->flag, &flags)) {
595 		linux_msg(td, "fstatat64 unsupported flags 0x%x", flags);
596 		return (EINVAL);
597 	}
598 
599 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
600 	error = linux_kern_statat(td, flags, dfd, args->pathname,
601 	    UIO_USERSPACE, &buf);
602 	if (error == 0)
603 		error = stat64_copyout(&buf, args->statbuf);
604 
605 	return (error);
606 }
607 
608 #else /* __amd64__ && !COMPAT_LINUX32 */
609 
610 int
linux_newfstatat(struct thread * td,struct linux_newfstatat_args * args)611 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
612 {
613 	int error, dfd, flags;
614 	struct stat buf;
615 
616 	if (!linux_to_bsd_stat_flags(args->flag, &flags)) {
617 		linux_msg(td, "fstatat unsupported flags 0x%x", flags);
618 		return (EINVAL);
619 	}
620 
621 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
622 	error = linux_kern_statat(td, flags, dfd, args->pathname,
623 	    UIO_USERSPACE, &buf);
624 	if (error == 0)
625 		error = newstat_copyout(&buf, args->statbuf);
626 
627 	return (error);
628 }
629 
630 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
631 
632 int
linux_syncfs(struct thread * td,struct linux_syncfs_args * args)633 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
634 {
635 	struct mount *mp;
636 	struct vnode *vp;
637 	int error, save;
638 
639 	error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
640 	if (error != 0)
641 		/*
642 		 * Linux syncfs() returns only EBADF, however fgetvp()
643 		 * can return EINVAL in case of file descriptor does
644 		 * not represent a vnode. XXX.
645 		 */
646 		return (error);
647 
648 	mp = vp->v_mount;
649 	mtx_lock(&mountlist_mtx);
650 	error = vfs_busy(mp, MBF_MNTLSTLOCK);
651 	if (error != 0) {
652 		/* See comment above. */
653 		mtx_unlock(&mountlist_mtx);
654 		goto out;
655 	}
656 	if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
657 	    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
658 		save = curthread_pflags_set(TDP_SYNCIO);
659 		vfs_periodic(mp, MNT_NOWAIT);
660 		VFS_SYNC(mp, MNT_NOWAIT);
661 		curthread_pflags_restore(save);
662 		vn_finished_write(mp);
663 	}
664 	vfs_unbusy(mp);
665 
666  out:
667 	vrele(vp);
668 	return (error);
669 }
670 
671 static int
statx_copyout(struct stat * buf,void * ubuf)672 statx_copyout(struct stat *buf, void *ubuf)
673 {
674 	struct l_statx tbuf;
675 
676 	bzero(&tbuf, sizeof(tbuf));
677 	tbuf.stx_mask = STATX_ALL;
678 	tbuf.stx_blksize = buf->st_blksize;
679 	tbuf.stx_attributes = 0;
680 	tbuf.stx_nlink = buf->st_nlink;
681 	tbuf.stx_uid = buf->st_uid;
682 	tbuf.stx_gid = buf->st_gid;
683 	tbuf.stx_mode = buf->st_mode;
684 	tbuf.stx_ino = buf->st_ino;
685 	tbuf.stx_size = buf->st_size;
686 	tbuf.stx_blocks = buf->st_blocks;
687 
688 	tbuf.stx_atime.tv_sec = buf->st_atim.tv_sec;
689 	tbuf.stx_atime.tv_nsec = buf->st_atim.tv_nsec;
690 	tbuf.stx_btime.tv_sec = buf->st_birthtim.tv_sec;
691 	tbuf.stx_btime.tv_nsec = buf->st_birthtim.tv_nsec;
692 	tbuf.stx_ctime.tv_sec = buf->st_ctim.tv_sec;
693 	tbuf.stx_ctime.tv_nsec = buf->st_ctim.tv_nsec;
694 	tbuf.stx_mtime.tv_sec = buf->st_mtim.tv_sec;
695 	tbuf.stx_mtime.tv_nsec = buf->st_mtim.tv_nsec;
696 	tbuf.stx_rdev_major = linux_encode_major(buf->st_rdev);
697 	tbuf.stx_rdev_minor = linux_encode_minor(buf->st_rdev);
698 	tbuf.stx_dev_major = linux_encode_major(buf->st_dev);
699 	tbuf.stx_dev_minor = linux_encode_minor(buf->st_dev);
700 
701 	return (copyout(&tbuf, ubuf, sizeof(tbuf)));
702 }
703 
704 int
linux_statx(struct thread * td,struct linux_statx_args * args)705 linux_statx(struct thread *td, struct linux_statx_args *args)
706 {
707 	int error, dirfd, flags;
708 	struct stat buf;
709 
710 	if (!linux_to_bsd_stat_flags(args->flags, &flags)) {
711 		linux_msg(td, "statx unsupported flags 0x%x", flags);
712 		return (EINVAL);
713 	}
714 
715 	dirfd = (args->dirfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dirfd;
716 	error = linux_kern_statat(td, flags, dirfd, args->pathname,
717 	    UIO_USERSPACE, &buf);
718 	if (error == 0)
719 		error = statx_copyout(&buf, args->statxbuf);
720 
721 	return (error);
722 }
723