]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_stats.c
awk: Merge 20210729 from One True Awk upstream (0592de4a)
[FreeBSD/FreeBSD.git] / sys / compat / linux / linux_stats.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_compat.h"
33
34 #include <sys/param.h>
35 #include <sys/capsicum.h>
36 #include <sys/dirent.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/proc.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/stat.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/systm.h>
46 #include <sys/tty.h>
47 #include <sys/vnode.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50
51 #ifdef COMPAT_LINUX32
52 #include <machine/../linux32/linux.h>
53 #include <machine/../linux32/linux32_proto.h>
54 #else
55 #include <machine/../linux/linux.h>
56 #include <machine/../linux/linux_proto.h>
57 #endif
58
59 #include <compat/linux/linux_util.h>
60 #include <compat/linux/linux_file.h>
61
62 static void
63 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
64 {
65         int major, minor;
66
67         if (vn_isdisk(vp)) {
68                 sb->st_mode &= ~S_IFMT;
69                 sb->st_mode |= S_IFBLK;
70         }
71
72         /*
73          * Return the same st_dev for every devfs instance.  The reason
74          * for this is to work around an idiosyncrasy of glibc getttynam()
75          * implementation: it checks whether st_dev returned for fd 0
76          * is the same as st_dev returned for the target of /proc/self/fd/0
77          * symlink, and with linux chroots having their own devfs instance,
78          * the check will fail if you chroot into it.
79          */
80         if (rootdevmp != NULL && vp->v_mount->mnt_vfc == rootdevmp->mnt_vfc)
81                 sb->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
82
83         if (linux_vn_get_major_minor(vp, &major, &minor) == 0)
84                 sb->st_rdev = (major << 8 | minor);
85 }
86
87 static int
88 linux_kern_statat(struct thread *td, int flag, int fd, const char *path,
89     enum uio_seg pathseg, struct stat *sbp)
90 {
91
92         return (kern_statat(td, flag, fd, path, pathseg, sbp,
93             translate_vnhook_major_minor));
94 }
95
96 #ifdef LINUX_LEGACY_SYSCALLS
97 static int
98 linux_kern_stat(struct thread *td, const char *path, enum uio_seg pathseg,
99     struct stat *sbp)
100 {
101
102         return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
103 }
104
105 static int
106 linux_kern_lstat(struct thread *td, const char *path, enum uio_seg pathseg,
107     struct stat *sbp)
108 {
109
110         return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
111             pathseg, sbp));
112 }
113 #endif
114
115 static void
116 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
117 {
118         struct file *fp;
119         struct vnode *vp;
120         struct mount *mp;
121         int major, minor;
122
123         /*
124          * No capability rights required here.
125          */
126         if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
127             fget(td, fd, &cap_no_rights, &fp) != 0)
128                 return;
129         vp = fp->f_vnode;
130         if (vp != NULL && vn_isdisk(vp)) {
131                 buf->st_mode &= ~S_IFMT;
132                 buf->st_mode |= S_IFBLK;
133         }
134         if (vp != NULL && rootdevmp != NULL) {
135                 mp = vp->v_mount;
136                 __compiler_membar();
137                 if (mp != NULL && mp->mnt_vfc == rootdevmp->mnt_vfc)
138                         buf->st_dev = rootdevmp->mnt_stat.f_fsid.val[0];
139         }
140         if (linux_vn_get_major_minor(vp, &major, &minor) == 0) {
141                 buf->st_rdev = (major << 8 | minor);
142         } else if (fp->f_type == DTYPE_PTS) {
143                 struct tty *tp = fp->f_data;
144
145                 /* Convert the numbers for the slave device. */
146                 if (linux_driver_get_major_minor(devtoname(tp->t_dev),
147                                          &major, &minor) == 0) {
148                         buf->st_rdev = (major << 8 | minor);
149                 }
150         }
151         fdrop(fp, td);
152 }
153
154 /*
155  * l_dev_t has the same encoding as dev_t in the latter's low 16 bits, so
156  * truncation of a dev_t to 16 bits gives the same result as unpacking
157  * using major() and minor() and repacking in the l_dev_t format.  This
158  * detail is hidden in dev_to_ldev().  Overflow in conversions of dev_t's
159  * are not checked for, as for other fields.
160  *
161  * dev_to_ldev() is only used for translating st_dev.  When we convert
162  * st_rdev for copying it out, it isn't really a dev_t, but has already
163  * been translated to an l_dev_t in a nontrivial way.  Translating it
164  * again would be illogical but would have no effect since the low 16
165  * bits have the same encoding.
166  *
167  * The nontrivial translation for st_rdev renumbers some devices, but not
168  * ones that can be mounted on, so it is consistent with the translation
169  * for st_dev except when the renumbering or truncation causes conflicts.
170  */
171 #define dev_to_ldev(d)  ((uint16_t)(d))
172
173 static int
174 newstat_copyout(struct stat *buf, void *ubuf)
175 {
176         struct l_newstat tbuf;
177
178         bzero(&tbuf, sizeof(tbuf));
179         tbuf.st_dev = dev_to_ldev(buf->st_dev);
180         tbuf.st_ino = buf->st_ino;
181         tbuf.st_mode = buf->st_mode;
182         tbuf.st_nlink = buf->st_nlink;
183         tbuf.st_uid = buf->st_uid;
184         tbuf.st_gid = buf->st_gid;
185         tbuf.st_rdev = buf->st_rdev;
186         tbuf.st_size = buf->st_size;
187         tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
188         tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
189         tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
190         tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
191         tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
192         tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
193         tbuf.st_blksize = buf->st_blksize;
194         tbuf.st_blocks = buf->st_blocks;
195
196         return (copyout(&tbuf, ubuf, sizeof(tbuf)));
197 }
198
199 static int
200 statx_copyout(struct stat *buf, void *ubuf)
201 {
202         struct l_statx tbuf;
203
204         bzero(&tbuf, sizeof(tbuf));
205         tbuf.stx_mask = STATX_ALL;
206         tbuf.stx_blksize = buf->st_blksize;
207         tbuf.stx_attributes = 0;
208         tbuf.stx_nlink = buf->st_nlink;
209         tbuf.stx_uid = buf->st_uid;
210         tbuf.stx_gid = buf->st_gid;
211         tbuf.stx_mode = buf->st_mode;
212         tbuf.stx_ino = buf->st_ino;
213         tbuf.stx_size = buf->st_size;
214         tbuf.stx_blocks = buf->st_blocks;
215
216         tbuf.stx_atime.tv_sec = buf->st_atim.tv_sec;
217         tbuf.stx_atime.tv_nsec = buf->st_atim.tv_nsec;
218         tbuf.stx_btime.tv_sec = buf->st_birthtim.tv_sec;
219         tbuf.stx_btime.tv_nsec = buf->st_birthtim.tv_nsec;
220         tbuf.stx_ctime.tv_sec = buf->st_ctim.tv_sec;
221         tbuf.stx_ctime.tv_nsec = buf->st_ctim.tv_nsec;
222         tbuf.stx_mtime.tv_sec = buf->st_mtim.tv_sec;
223         tbuf.stx_mtime.tv_nsec = buf->st_mtim.tv_nsec;
224
225         tbuf.stx_rdev_major = buf->st_rdev >> 8;
226         tbuf.stx_rdev_minor = buf->st_rdev & 0xff;
227         tbuf.stx_dev_major = buf->st_dev >> 8;
228         tbuf.stx_dev_minor = buf->st_dev & 0xff;
229
230         return (copyout(&tbuf, ubuf, sizeof(tbuf)));
231 }
232
233 #ifdef LINUX_LEGACY_SYSCALLS
234 int
235 linux_newstat(struct thread *td, struct linux_newstat_args *args)
236 {
237         struct stat buf;
238         char *path;
239         int error;
240
241         if (!LUSECONVPATH(td)) {
242                 error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
243         } else {
244                 LCONVPATHEXIST(td, args->path, &path);
245                 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
246                 LFREEPATH(path);
247         }
248         if (error)
249                 return (error);
250         return (newstat_copyout(&buf, args->buf));
251 }
252
253 int
254 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
255 {
256         struct stat sb;
257         char *path;
258         int error;
259
260         if (!LUSECONVPATH(td)) {
261                 error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &sb);
262         } else {
263                 LCONVPATHEXIST(td, args->path, &path);
264                 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
265                 LFREEPATH(path);
266         }
267         if (error)
268                 return (error);
269         return (newstat_copyout(&sb, args->buf));
270 }
271 #endif
272
273 int
274 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
275 {
276         struct stat buf;
277         int error;
278
279         error = kern_fstat(td, args->fd, &buf);
280         translate_fd_major_minor(td, args->fd, &buf);
281         if (!error)
282                 error = newstat_copyout(&buf, args->buf);
283
284         return (error);
285 }
286
287 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
288 static int
289 stat_copyout(struct stat *buf, void *ubuf)
290 {
291         struct l_stat lbuf;
292
293         bzero(&lbuf, sizeof(lbuf));
294         lbuf.st_dev = dev_to_ldev(buf->st_dev);
295         lbuf.st_ino = buf->st_ino;
296         lbuf.st_mode = buf->st_mode;
297         lbuf.st_nlink = buf->st_nlink;
298         lbuf.st_uid = buf->st_uid;
299         lbuf.st_gid = buf->st_gid;
300         lbuf.st_rdev = buf->st_rdev;
301         lbuf.st_size = MIN(buf->st_size, INT32_MAX);
302         lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
303         lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
304         lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
305         lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
306         lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
307         lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
308         lbuf.st_blksize = buf->st_blksize;
309         lbuf.st_blocks = buf->st_blocks;
310         lbuf.st_flags = buf->st_flags;
311         lbuf.st_gen = buf->st_gen;
312
313         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
314 }
315
316 int
317 linux_stat(struct thread *td, struct linux_stat_args *args)
318 {
319         struct stat buf;
320         char *path;
321         int error;
322
323         if (!LUSECONVPATH(td)) {
324                 error = linux_kern_stat(td, args->path, UIO_USERSPACE, &buf);
325         } else {
326                 LCONVPATHEXIST(td, args->path, &path);
327                 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
328                 LFREEPATH(path);
329         }
330         if (error) {
331                 return (error);
332         }
333         return (stat_copyout(&buf, args->up));
334 }
335
336 int
337 linux_lstat(struct thread *td, struct linux_lstat_args *args)
338 {
339         struct stat buf;
340         char *path;
341         int error;
342
343         if (!LUSECONVPATH(td)) {
344                 error = linux_kern_lstat(td, args->path, UIO_USERSPACE, &buf);
345         } else {
346                 LCONVPATHEXIST(td, args->path, &path);
347                 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
348                 LFREEPATH(path);
349         }
350         if (error) {
351                 return (error);
352         }
353         return (stat_copyout(&buf, args->up));
354 }
355 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
356
357 struct l_statfs {
358         l_long          f_type;
359         l_long          f_bsize;
360         l_long          f_blocks;
361         l_long          f_bfree;
362         l_long          f_bavail;
363         l_long          f_files;
364         l_long          f_ffree;
365         l_fsid_t        f_fsid;
366         l_long          f_namelen;
367         l_long          f_frsize;
368         l_long          f_flags;
369         l_long          f_spare[4];
370 };
371
372 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
373 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
374 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
375 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
376 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
377 #define LINUX_NCP_SUPER_MAGIC   0x564cL
378 #define LINUX_NFS_SUPER_MAGIC   0x6969L
379 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
380 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
381 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
382 #define LINUX_ZFS_SUPER_MAGIC   0x2FC12FC1
383 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
384 #define LINUX_SHMFS_MAGIC       0x01021994
385
386 static long
387 bsd_to_linux_ftype(const char *fstypename)
388 {
389         int i;
390         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
391                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
392                 {"zfs",     LINUX_ZFS_SUPER_MAGIC},
393                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
394                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
395                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
396                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
397                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
398                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
399                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
400                 {"hpfs",    LINUX_HPFS_SUPER_MAGIC},
401                 {"coda",    LINUX_CODA_SUPER_MAGIC},
402                 {"devfs",   LINUX_DEVFS_SUPER_MAGIC},
403                 {"tmpfs",   LINUX_SHMFS_MAGIC},
404                 {NULL,      0L}};
405
406         for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
407                 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
408                         return (b2l_tbl[i].linux_type);
409
410         return (0L);
411 }
412
413 static int
414 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
415 {
416 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
417         uint64_t tmp;
418
419 #define LINUX_HIBITS    0xffffffff00000000ULL
420
421         tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
422             bsd_statfs->f_bsize;
423         if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
424             (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
425             (tmp & LINUX_HIBITS))
426                 return (EOVERFLOW);
427 #undef  LINUX_HIBITS
428 #endif
429         linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
430         linux_statfs->f_bsize = bsd_statfs->f_bsize;
431         linux_statfs->f_blocks = bsd_statfs->f_blocks;
432         linux_statfs->f_bfree = bsd_statfs->f_bfree;
433         linux_statfs->f_bavail = bsd_statfs->f_bavail;
434         linux_statfs->f_ffree = bsd_statfs->f_ffree;
435         linux_statfs->f_files = bsd_statfs->f_files;
436         linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
437         linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
438         linux_statfs->f_namelen = MAXNAMLEN;
439         linux_statfs->f_frsize = bsd_statfs->f_bsize;
440         linux_statfs->f_flags = 0;
441         memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
442
443         return (0);
444 }
445
446 int
447 linux_statfs(struct thread *td, struct linux_statfs_args *args)
448 {
449         struct l_statfs linux_statfs;
450         struct statfs *bsd_statfs;
451         char *path;
452         int error;
453
454         if (!LUSECONVPATH(td)) {
455                 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
456                 error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
457         } else {
458                 LCONVPATHEXIST(td, args->path, &path);
459                 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
460                 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
461                 LFREEPATH(path);
462         }
463         if (error == 0)
464                 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
465         free(bsd_statfs, M_STATFS);
466         if (error != 0)
467                 return (error);
468         return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
469 }
470
471 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
472 static void
473 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
474 {
475
476         linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
477         linux_statfs->f_bsize = bsd_statfs->f_bsize;
478         linux_statfs->f_blocks = bsd_statfs->f_blocks;
479         linux_statfs->f_bfree = bsd_statfs->f_bfree;
480         linux_statfs->f_bavail = bsd_statfs->f_bavail;
481         linux_statfs->f_ffree = bsd_statfs->f_ffree;
482         linux_statfs->f_files = bsd_statfs->f_files;
483         linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
484         linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
485         linux_statfs->f_namelen = MAXNAMLEN;
486         linux_statfs->f_frsize = bsd_statfs->f_bsize;
487         linux_statfs->f_flags = 0;
488         memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
489 }
490
491 int
492 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
493 {
494         struct l_statfs64 linux_statfs;
495         struct statfs *bsd_statfs;
496         char *path;
497         int error;
498
499         if (args->bufsize != sizeof(struct l_statfs64))
500                 return (EINVAL);
501
502         if (!LUSECONVPATH(td)) {
503                 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
504                 error = kern_statfs(td, args->path, UIO_USERSPACE, bsd_statfs);
505         } else {
506                 LCONVPATHEXIST(td, args->path, &path);
507                 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
508                 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
509                 LFREEPATH(path);
510         }
511         if (error == 0)
512                 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
513         free(bsd_statfs, M_STATFS);
514         if (error != 0)
515                 return (error);
516         return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
517 }
518
519 int
520 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
521 {
522         struct l_statfs64 linux_statfs;
523         struct statfs *bsd_statfs;
524         int error;
525
526         if (args->bufsize != sizeof(struct l_statfs64))
527                 return (EINVAL);
528
529         bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
530         error = kern_fstatfs(td, args->fd, bsd_statfs);
531         if (error == 0)
532                 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
533         free(bsd_statfs, M_STATFS);
534         if (error != 0)
535                 return (error);
536         return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
537 }
538 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
539
540 int
541 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
542 {
543         struct l_statfs linux_statfs;
544         struct statfs *bsd_statfs;
545         int error;
546
547         bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
548         error = kern_fstatfs(td, args->fd, bsd_statfs);
549         if (error == 0)
550                 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
551         free(bsd_statfs, M_STATFS);
552         if (error != 0)
553                 return (error);
554         return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
555 }
556
557 struct l_ustat
558 {
559         l_daddr_t       f_tfree;
560         l_ino_t         f_tinode;
561         char            f_fname[6];
562         char            f_fpack[6];
563 };
564
565 #ifdef LINUX_LEGACY_SYSCALLS
566 int
567 linux_ustat(struct thread *td, struct linux_ustat_args *args)
568 {
569
570         return (EOPNOTSUPP);
571 }
572 #endif
573
574 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
575
576 static int
577 stat64_copyout(struct stat *buf, void *ubuf)
578 {
579         struct l_stat64 lbuf;
580
581         bzero(&lbuf, sizeof(lbuf));
582         lbuf.st_dev = dev_to_ldev(buf->st_dev);
583         lbuf.st_ino = buf->st_ino;
584         lbuf.st_mode = buf->st_mode;
585         lbuf.st_nlink = buf->st_nlink;
586         lbuf.st_uid = buf->st_uid;
587         lbuf.st_gid = buf->st_gid;
588         lbuf.st_rdev = buf->st_rdev;
589         lbuf.st_size = buf->st_size;
590         lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
591         lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
592         lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
593         lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
594         lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
595         lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
596         lbuf.st_blksize = buf->st_blksize;
597         lbuf.st_blocks = buf->st_blocks;
598
599         /*
600          * The __st_ino field makes all the difference. In the Linux kernel
601          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
602          * but without the assignment to __st_ino the runtime linker refuses
603          * to mmap(2) any shared libraries. I guess it's broken alright :-)
604          */
605         lbuf.__st_ino = buf->st_ino;
606
607         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
608 }
609
610 int
611 linux_stat64(struct thread *td, struct linux_stat64_args *args)
612 {
613         struct stat buf;
614         char *filename;
615         int error;
616
617         if (!LUSECONVPATH(td)) {
618                 error = linux_kern_stat(td, args->filename, UIO_USERSPACE, &buf);
619         } else {
620                 LCONVPATHEXIST(td, args->filename, &filename);
621                 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
622                 LFREEPATH(filename);
623         }
624         if (error)
625                 return (error);
626         return (stat64_copyout(&buf, args->statbuf));
627 }
628
629 int
630 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
631 {
632         struct stat sb;
633         char *filename;
634         int error;
635
636         if (!LUSECONVPATH(td)) {
637                 error = linux_kern_lstat(td, args->filename, UIO_USERSPACE, &sb);
638         } else {
639                 LCONVPATHEXIST(td, args->filename, &filename);
640                 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
641                 LFREEPATH(filename);
642         }
643         if (error)
644                 return (error);
645         return (stat64_copyout(&sb, args->statbuf));
646 }
647
648 int
649 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
650 {
651         struct stat buf;
652         int error;
653
654         error = kern_fstat(td, args->fd, &buf);
655         translate_fd_major_minor(td, args->fd, &buf);
656         if (!error)
657                 error = stat64_copyout(&buf, args->statbuf);
658
659         return (error);
660 }
661
662 int
663 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
664 {
665         char *path;
666         int error, dfd, flag, unsupported;
667         struct stat buf;
668
669         unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
670         if (unsupported != 0) {
671                 linux_msg(td, "fstatat64 unsupported flag 0x%x", unsupported);
672                 return (EINVAL);
673         }
674         flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
675             AT_SYMLINK_NOFOLLOW : 0;
676         flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
677             AT_EMPTY_PATH : 0;
678
679         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
680         if (!LUSECONVPATH(td)) {
681                 error = linux_kern_statat(td, flag, dfd, args->pathname,
682                     UIO_USERSPACE, &buf);
683         } else {
684                 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
685                 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
686                 LFREEPATH(path);
687         }
688         if (error == 0)
689                 error = stat64_copyout(&buf, args->statbuf);
690
691         return (error);
692 }
693
694 #else /* __amd64__ && !COMPAT_LINUX32 */
695
696 int
697 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
698 {
699         char *path;
700         int error, dfd, flag, unsupported;
701         struct stat buf;
702
703         unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
704         if (unsupported != 0) {
705                 linux_msg(td, "fstatat unsupported flag 0x%x", unsupported);
706                 return (EINVAL);
707         }
708
709         flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
710             AT_SYMLINK_NOFOLLOW : 0;
711         flag |= (args->flag & LINUX_AT_EMPTY_PATH) ?
712             AT_EMPTY_PATH : 0;
713
714         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
715         if (!LUSECONVPATH(td)) {
716                 error = linux_kern_statat(td, flag, dfd, args->pathname,
717                     UIO_USERSPACE, &buf);
718         } else {
719                 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
720                 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
721                 LFREEPATH(path);
722         }
723         if (error == 0)
724                 error = newstat_copyout(&buf, args->statbuf);
725
726         return (error);
727 }
728
729 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
730
731 int
732 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
733 {
734         struct mount *mp;
735         struct vnode *vp;
736         int error, save;
737
738         error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
739         if (error != 0)
740                 /*
741                  * Linux syncfs() returns only EBADF, however fgetvp()
742                  * can return EINVAL in case of file descriptor does
743                  * not represent a vnode. XXX.
744                  */
745                 return (error);
746
747         mp = vp->v_mount;
748         mtx_lock(&mountlist_mtx);
749         error = vfs_busy(mp, MBF_MNTLSTLOCK);
750         if (error != 0) {
751                 /* See comment above. */
752                 mtx_unlock(&mountlist_mtx);
753                 goto out;
754         }
755         if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
756             vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
757                 save = curthread_pflags_set(TDP_SYNCIO);
758                 vfs_periodic(mp, MNT_NOWAIT);
759                 VFS_SYNC(mp, MNT_NOWAIT);
760                 curthread_pflags_restore(save);
761                 vn_finished_write(mp);
762         }
763         vfs_unbusy(mp);
764
765  out:
766         vrele(vp);
767         return (error);
768 }
769
770 int
771 linux_statx(struct thread *td, struct linux_statx_args *args)
772 {
773         char *path;
774         int error, dirfd, flags, unsupported;
775         struct stat buf;
776
777         unsupported = args->flags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
778         if (unsupported != 0) {
779                 linux_msg(td, "statx unsupported flags 0x%x", unsupported);
780                 return (EINVAL);
781         }
782
783         flags = (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) ?
784             AT_SYMLINK_NOFOLLOW : 0;
785         flags |= (args->flags & LINUX_AT_EMPTY_PATH) ?
786             AT_EMPTY_PATH : 0;
787
788         dirfd = (args->dirfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dirfd;
789         if (!LUSECONVPATH(td)) {
790                 error = linux_kern_statat(td, flags, dirfd, args->pathname,
791                     UIO_USERSPACE, &buf);
792         } else {
793                 LCONVPATHEXIST_AT(td, args->pathname, &path, dirfd);
794                 error = linux_kern_statat(td, flags, dirfd, path, UIO_SYSSPACE, &buf);
795                 LFREEPATH(path);
796         }
797         if (error == 0)
798                 error = statx_copyout(&buf, args->statxbuf);
799
800         return (error);
801 }
802