]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_stats.c
Make linux(4) set the openfiles soft resource limit to 1024 for Linux
[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
63 static void
64 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
65 {
66         int major, minor;
67
68         if (vn_isdisk(vp, NULL)) {
69                 sb->st_mode &= ~S_IFMT;
70                 sb->st_mode |= S_IFBLK;
71         }
72
73         if (vp->v_type == VCHR && vp->v_rdev != NULL &&
74             linux_driver_get_major_minor(devtoname(vp->v_rdev),
75             &major, &minor) == 0) {
76                 sb->st_rdev = (major << 8 | minor);
77         }
78 }
79
80 static int
81 linux_kern_statat(struct thread *td, int flag, int fd, char *path,
82     enum uio_seg pathseg, struct stat *sbp)
83 {
84
85         return (kern_statat(td, flag, fd, path, pathseg, sbp,
86             translate_vnhook_major_minor));
87 }
88
89 #ifdef LINUX_LEGACY_SYSCALLS
90 static int
91 linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
92     struct stat *sbp)
93 {
94
95         return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
96 }
97
98 static int
99 linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
100     struct stat *sbp)
101 {
102
103         return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
104             pathseg, sbp));
105 }
106 #endif
107
108 static void
109 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
110 {
111         struct file *fp;
112         struct vnode *vp;
113         int major, minor;
114
115         /*
116          * No capability rights required here.
117          */
118         if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
119             fget(td, fd, &cap_no_rights, &fp) != 0)
120                 return;
121         vp = fp->f_vnode;
122         if (vp != NULL && vn_isdisk(vp, NULL)) {
123                 buf->st_mode &= ~S_IFMT;
124                 buf->st_mode |= S_IFBLK;
125         }
126         if (vp != NULL && vp->v_rdev != NULL &&
127             linux_driver_get_major_minor(devtoname(vp->v_rdev),
128                                          &major, &minor) == 0) {
129                 buf->st_rdev = (major << 8 | minor);
130         } else if (fp->f_type == DTYPE_PTS) {
131                 struct tty *tp = fp->f_data;
132
133                 /* Convert the numbers for the slave device. */
134                 if (linux_driver_get_major_minor(devtoname(tp->t_dev),
135                                          &major, &minor) == 0) {
136                         buf->st_rdev = (major << 8 | minor);
137                 }
138         }
139         fdrop(fp, td);
140 }
141
142 /*
143  * l_dev_t has the same encoding as dev_t in the latter's low 16 bits, so
144  * truncation of a dev_t to 16 bits gives the same result as unpacking
145  * using major() and minor() and repacking in the l_dev_t format.  This
146  * detail is hidden in dev_to_ldev().  Overflow in conversions of dev_t's
147  * are not checked for, as for other fields.
148  *
149  * dev_to_ldev() is only used for translating st_dev.  When we convert
150  * st_rdev for copying it out, it isn't really a dev_t, but has already
151  * been translated to an l_dev_t in a nontrivial way.  Translating it
152  * again would be illogical but would have no effect since the low 16
153  * bits have the same encoding.
154  *
155  * The nontrivial translation for st_rdev renumbers some devices, but not
156  * ones that can be mounted on, so it is consistent with the translation
157  * for st_dev except when the renumbering or truncation causes conflicts.
158  */
159 #define dev_to_ldev(d)  ((uint16_t)(d))
160
161 static int
162 newstat_copyout(struct stat *buf, void *ubuf)
163 {
164         struct l_newstat tbuf;
165
166         bzero(&tbuf, sizeof(tbuf));
167         tbuf.st_dev = dev_to_ldev(buf->st_dev);
168         tbuf.st_ino = buf->st_ino;
169         tbuf.st_mode = buf->st_mode;
170         tbuf.st_nlink = buf->st_nlink;
171         tbuf.st_uid = buf->st_uid;
172         tbuf.st_gid = buf->st_gid;
173         tbuf.st_rdev = buf->st_rdev;
174         tbuf.st_size = buf->st_size;
175         tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
176         tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
177         tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
178         tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
179         tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
180         tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
181         tbuf.st_blksize = buf->st_blksize;
182         tbuf.st_blocks = buf->st_blocks;
183
184         return (copyout(&tbuf, ubuf, sizeof(tbuf)));
185 }
186
187 #ifdef LINUX_LEGACY_SYSCALLS
188 int
189 linux_newstat(struct thread *td, struct linux_newstat_args *args)
190 {
191         struct stat buf;
192         char *path;
193         int error;
194
195         LCONVPATHEXIST(td, args->path, &path);
196
197         error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
198         LFREEPATH(path);
199         if (error)
200                 return (error);
201         return (newstat_copyout(&buf, args->buf));
202 }
203
204 int
205 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
206 {
207         struct stat sb;
208         char *path;
209         int error;
210
211         LCONVPATHEXIST(td, args->path, &path);
212
213         error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
214         LFREEPATH(path);
215         if (error)
216                 return (error);
217         return (newstat_copyout(&sb, args->buf));
218 }
219 #endif
220
221 int
222 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
223 {
224         struct stat buf;
225         int error;
226
227         error = kern_fstat(td, args->fd, &buf);
228         translate_fd_major_minor(td, args->fd, &buf);
229         if (!error)
230                 error = newstat_copyout(&buf, args->buf);
231
232         return (error);
233 }
234
235 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
236 static int
237 stat_copyout(struct stat *buf, void *ubuf)
238 {
239         struct l_stat lbuf;
240
241         bzero(&lbuf, sizeof(lbuf));
242         lbuf.st_dev = dev_to_ldev(buf->st_dev);
243         lbuf.st_ino = buf->st_ino;
244         lbuf.st_mode = buf->st_mode;
245         lbuf.st_nlink = buf->st_nlink;
246         lbuf.st_uid = buf->st_uid;
247         lbuf.st_gid = buf->st_gid;
248         lbuf.st_rdev = buf->st_rdev;
249         lbuf.st_size = MIN(buf->st_size, INT32_MAX);
250         lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
251         lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
252         lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
253         lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
254         lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
255         lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
256         lbuf.st_blksize = buf->st_blksize;
257         lbuf.st_blocks = buf->st_blocks;
258         lbuf.st_flags = buf->st_flags;
259         lbuf.st_gen = buf->st_gen;
260
261         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
262 }
263
264 int
265 linux_stat(struct thread *td, struct linux_stat_args *args)
266 {
267         struct stat buf;
268         char *path;
269         int error;
270
271         LCONVPATHEXIST(td, args->path, &path);
272
273         error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
274         if (error) {
275                 LFREEPATH(path);
276                 return (error);
277         }
278         LFREEPATH(path);
279         return (stat_copyout(&buf, args->up));
280 }
281
282 int
283 linux_lstat(struct thread *td, struct linux_lstat_args *args)
284 {
285         struct stat buf;
286         char *path;
287         int error;
288
289         LCONVPATHEXIST(td, args->path, &path);
290
291         error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
292         if (error) {
293                 LFREEPATH(path);
294                 return (error);
295         }
296         LFREEPATH(path);
297         return (stat_copyout(&buf, args->up));
298 }
299 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
300
301 struct l_statfs {
302         l_long          f_type;
303         l_long          f_bsize;
304         l_long          f_blocks;
305         l_long          f_bfree;
306         l_long          f_bavail;
307         l_long          f_files;
308         l_long          f_ffree;
309         l_fsid_t        f_fsid;
310         l_long          f_namelen;
311         l_long          f_frsize;
312         l_long          f_flags;
313         l_long          f_spare[4];
314 };
315
316 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
317 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
318 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
319 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
320 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
321 #define LINUX_NCP_SUPER_MAGIC   0x564cL
322 #define LINUX_NFS_SUPER_MAGIC   0x6969L
323 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
324 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
325 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
326 #define LINUX_ZFS_SUPER_MAGIC   0x2FC12FC1
327 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
328 #define LINUX_SHMFS_MAGIC       0x01021994
329
330 static long
331 bsd_to_linux_ftype(const char *fstypename)
332 {
333         int i;
334         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
335                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
336                 {"zfs",     LINUX_ZFS_SUPER_MAGIC},
337                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
338                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
339                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
340                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
341                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
342                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
343                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
344                 {"hpfs",    LINUX_HPFS_SUPER_MAGIC},
345                 {"coda",    LINUX_CODA_SUPER_MAGIC},
346                 {"devfs",   LINUX_DEVFS_SUPER_MAGIC},
347                 {"tmpfs",   LINUX_SHMFS_MAGIC},
348                 {NULL,      0L}};
349
350         for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
351                 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
352                         return (b2l_tbl[i].linux_type);
353
354         return (0L);
355 }
356
357 static int
358 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
359 {
360 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
361         uint64_t tmp;
362
363 #define LINUX_HIBITS    0xffffffff00000000ULL
364
365         tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
366             bsd_statfs->f_bsize;
367         if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
368             (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
369             (tmp & LINUX_HIBITS))
370                 return (EOVERFLOW);
371 #undef  LINUX_HIBITS
372 #endif
373         linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
374         linux_statfs->f_bsize = bsd_statfs->f_bsize;
375         linux_statfs->f_blocks = bsd_statfs->f_blocks;
376         linux_statfs->f_bfree = bsd_statfs->f_bfree;
377         linux_statfs->f_bavail = bsd_statfs->f_bavail;
378         linux_statfs->f_ffree = bsd_statfs->f_ffree;
379         linux_statfs->f_files = bsd_statfs->f_files;
380         linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
381         linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
382         linux_statfs->f_namelen = MAXNAMLEN;
383         linux_statfs->f_frsize = bsd_statfs->f_bsize;
384         linux_statfs->f_flags = 0;
385         memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
386
387         return (0);
388 }
389
390 int
391 linux_statfs(struct thread *td, struct linux_statfs_args *args)
392 {
393         struct l_statfs linux_statfs;
394         struct statfs *bsd_statfs;
395         char *path;
396         int error;
397
398         LCONVPATHEXIST(td, args->path, &path);
399
400         bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
401         error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
402         LFREEPATH(path);
403         if (error == 0)
404                 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
405         free(bsd_statfs, M_STATFS);
406         if (error != 0)
407                 return (error);
408         return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
409 }
410
411 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
412 static void
413 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
414 {
415
416         linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
417         linux_statfs->f_bsize = bsd_statfs->f_bsize;
418         linux_statfs->f_blocks = bsd_statfs->f_blocks;
419         linux_statfs->f_bfree = bsd_statfs->f_bfree;
420         linux_statfs->f_bavail = bsd_statfs->f_bavail;
421         linux_statfs->f_ffree = bsd_statfs->f_ffree;
422         linux_statfs->f_files = bsd_statfs->f_files;
423         linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
424         linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
425         linux_statfs->f_namelen = MAXNAMLEN;
426         linux_statfs->f_frsize = bsd_statfs->f_bsize;
427         linux_statfs->f_flags = 0;
428         memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
429 }
430
431 int
432 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
433 {
434         struct l_statfs64 linux_statfs;
435         struct statfs *bsd_statfs;
436         char *path;
437         int error;
438
439         if (args->bufsize != sizeof(struct l_statfs64))
440                 return (EINVAL);
441
442         LCONVPATHEXIST(td, args->path, &path);
443
444         bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
445         error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
446         LFREEPATH(path);
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
455 int
456 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
457 {
458         struct l_statfs64 linux_statfs;
459         struct statfs *bsd_statfs;
460         int error;
461
462         if (args->bufsize != sizeof(struct l_statfs64))
463                 return (EINVAL);
464
465         bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
466         error = kern_fstatfs(td, args->fd, bsd_statfs);
467         if (error == 0)
468                 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
469         free(bsd_statfs, M_STATFS);
470         if (error != 0)
471                 return (error);
472         return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
473 }
474 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
475
476 int
477 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
478 {
479         struct l_statfs linux_statfs;
480         struct statfs *bsd_statfs;
481         int error;
482
483         bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
484         error = kern_fstatfs(td, args->fd, bsd_statfs);
485         if (error == 0)
486                 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
487         free(bsd_statfs, M_STATFS);
488         if (error != 0)
489                 return (error);
490         return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
491 }
492
493 struct l_ustat
494 {
495         l_daddr_t       f_tfree;
496         l_ino_t         f_tinode;
497         char            f_fname[6];
498         char            f_fpack[6];
499 };
500
501 #ifdef LINUX_LEGACY_SYSCALLS
502 int
503 linux_ustat(struct thread *td, struct linux_ustat_args *args)
504 {
505
506         return (EOPNOTSUPP);
507 }
508 #endif
509
510 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
511
512 static int
513 stat64_copyout(struct stat *buf, void *ubuf)
514 {
515         struct l_stat64 lbuf;
516
517         bzero(&lbuf, sizeof(lbuf));
518         lbuf.st_dev = dev_to_ldev(buf->st_dev);
519         lbuf.st_ino = buf->st_ino;
520         lbuf.st_mode = buf->st_mode;
521         lbuf.st_nlink = buf->st_nlink;
522         lbuf.st_uid = buf->st_uid;
523         lbuf.st_gid = buf->st_gid;
524         lbuf.st_rdev = buf->st_rdev;
525         lbuf.st_size = buf->st_size;
526         lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
527         lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
528         lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
529         lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
530         lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
531         lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
532         lbuf.st_blksize = buf->st_blksize;
533         lbuf.st_blocks = buf->st_blocks;
534
535         /*
536          * The __st_ino field makes all the difference. In the Linux kernel
537          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
538          * but without the assignment to __st_ino the runtime linker refuses
539          * to mmap(2) any shared libraries. I guess it's broken alright :-)
540          */
541         lbuf.__st_ino = buf->st_ino;
542
543         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
544 }
545
546 int
547 linux_stat64(struct thread *td, struct linux_stat64_args *args)
548 {
549         struct stat buf;
550         char *filename;
551         int error;
552
553         LCONVPATHEXIST(td, args->filename, &filename);
554
555         error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
556         LFREEPATH(filename);
557         if (error)
558                 return (error);
559         return (stat64_copyout(&buf, args->statbuf));
560 }
561
562 int
563 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
564 {
565         struct stat sb;
566         char *filename;
567         int error;
568
569         LCONVPATHEXIST(td, args->filename, &filename);
570
571         error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
572         LFREEPATH(filename);
573         if (error)
574                 return (error);
575         return (stat64_copyout(&sb, args->statbuf));
576 }
577
578 int
579 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
580 {
581         struct stat buf;
582         int error;
583
584         error = kern_fstat(td, args->fd, &buf);
585         translate_fd_major_minor(td, args->fd, &buf);
586         if (!error)
587                 error = stat64_copyout(&buf, args->statbuf);
588
589         return (error);
590 }
591
592 int
593 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
594 {
595         char *path;
596         int error, dfd, flag;
597         struct stat buf;
598
599         if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
600                 return (EINVAL);
601         flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
602             AT_SYMLINK_NOFOLLOW : 0;
603
604         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
605         LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
606
607         error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
608         if (!error)
609                 error = stat64_copyout(&buf, args->statbuf);
610         LFREEPATH(path);
611
612         return (error);
613 }
614
615 #else /* __amd64__ && !COMPAT_LINUX32 */
616
617 int
618 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
619 {
620         char *path;
621         int error, dfd, flag;
622         struct stat buf;
623
624         if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
625                 return (EINVAL);
626         flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
627             AT_SYMLINK_NOFOLLOW : 0;
628
629         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
630         LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
631
632         error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
633         if (error == 0)
634                 error = newstat_copyout(&buf, args->statbuf);
635         LFREEPATH(path);
636
637         return (error);
638 }
639
640 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
641
642 int
643 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
644 {
645         struct mount *mp;
646         struct vnode *vp;
647         int error, save;
648
649         error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
650         if (error != 0)
651                 /*
652                  * Linux syncfs() returns only EBADF, however fgetvp()
653                  * can return EINVAL in case of file descriptor does
654                  * not represent a vnode. XXX.
655                  */
656                 return (error);
657
658         mp = vp->v_mount;
659         mtx_lock(&mountlist_mtx);
660         error = vfs_busy(mp, MBF_MNTLSTLOCK);
661         if (error != 0) {
662                 /* See comment above. */
663                 mtx_unlock(&mountlist_mtx);
664                 goto out;
665         }
666         if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
667             vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
668                 save = curthread_pflags_set(TDP_SYNCIO);
669                 vfs_periodic(mp, MNT_NOWAIT);
670                 VFS_SYNC(mp, MNT_NOWAIT);
671                 curthread_pflags_restore(save);
672                 vn_finished_write(mp);
673         }
674         vfs_unbusy(mp);
675
676  out:
677         vrele(vp);
678         return (error);
679 }