2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1994-1995 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/capsicum.h>
36 #include <sys/dirent.h>
38 #include <sys/filedesc.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/systm.h>
47 #include <sys/vnode.h>
49 #include <sys/fcntl.h>
52 #include <machine/../linux32/linux.h>
53 #include <machine/../linux32/linux32_proto.h>
55 #include <machine/../linux/linux.h>
56 #include <machine/../linux/linux_proto.h>
59 #include <compat/linux/linux_util.h>
60 #include <compat/linux/linux_file.h>
64 translate_vnhook_major_minor(struct vnode *vp, struct stat *sb)
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);
76 linux_kern_statat(struct thread *td, int flag, int fd, char *path,
77 enum uio_seg pathseg, struct stat *sbp)
80 return (kern_statat(td, flag, fd, path, pathseg, sbp,
81 translate_vnhook_major_minor));
84 #ifdef LINUX_LEGACY_SYSCALLS
86 linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
90 return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
94 linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
98 return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
104 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
112 * No capability rights required here.
114 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
115 fget(td, fd, cap_rights_init(&rights), &fp) != 0)
118 if (vp != NULL && vp->v_rdev != NULL &&
119 linux_driver_get_major_minor(devtoname(vp->v_rdev),
120 &major, &minor) == 0) {
121 buf->st_rdev = (major << 8 | minor);
122 } else if (fp->f_type == DTYPE_PTS) {
123 struct tty *tp = fp->f_data;
125 /* Convert the numbers for the slave device. */
126 if (linux_driver_get_major_minor(devtoname(tp->t_dev),
127 &major, &minor) == 0) {
128 buf->st_rdev = (major << 8 | minor);
135 newstat_copyout(struct stat *buf, void *ubuf)
137 struct l_newstat tbuf;
139 bzero(&tbuf, sizeof(tbuf));
140 tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
141 tbuf.st_ino = buf->st_ino;
142 tbuf.st_mode = buf->st_mode;
143 tbuf.st_nlink = buf->st_nlink;
144 tbuf.st_uid = buf->st_uid;
145 tbuf.st_gid = buf->st_gid;
146 tbuf.st_rdev = buf->st_rdev;
147 tbuf.st_size = buf->st_size;
148 tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
149 tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
150 tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
151 tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
152 tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
153 tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
154 tbuf.st_blksize = buf->st_blksize;
155 tbuf.st_blocks = buf->st_blocks;
157 return (copyout(&tbuf, ubuf, sizeof(tbuf)));
160 #ifdef LINUX_LEGACY_SYSCALLS
162 linux_newstat(struct thread *td, struct linux_newstat_args *args)
168 LCONVPATHEXIST(td, args->path, &path);
172 printf(ARGS(newstat, "%s, *"), path);
175 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
179 return (newstat_copyout(&buf, args->buf));
183 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
189 LCONVPATHEXIST(td, args->path, &path);
192 if (ldebug(newlstat))
193 printf(ARGS(newlstat, "%s, *"), path);
196 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
200 return (newstat_copyout(&sb, args->buf));
205 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
211 if (ldebug(newfstat))
212 printf(ARGS(newfstat, "%d, *"), args->fd);
215 error = kern_fstat(td, args->fd, &buf);
216 translate_fd_major_minor(td, args->fd, &buf);
218 error = newstat_copyout(&buf, args->buf);
223 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
225 stat_copyout(struct stat *buf, void *ubuf)
229 bzero(&lbuf, sizeof(lbuf));
230 lbuf.st_dev = buf->st_dev;
231 lbuf.st_ino = buf->st_ino;
232 lbuf.st_mode = buf->st_mode;
233 lbuf.st_nlink = buf->st_nlink;
234 lbuf.st_uid = buf->st_uid;
235 lbuf.st_gid = buf->st_gid;
236 lbuf.st_rdev = buf->st_rdev;
237 if (buf->st_size < (quad_t)1 << 32)
238 lbuf.st_size = buf->st_size;
241 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
242 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
243 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
244 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
245 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
246 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
247 lbuf.st_blksize = buf->st_blksize;
248 lbuf.st_blocks = buf->st_blocks;
249 lbuf.st_flags = buf->st_flags;
250 lbuf.st_gen = buf->st_gen;
252 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
256 linux_stat(struct thread *td, struct linux_stat_args *args)
262 LCONVPATHEXIST(td, args->path, &path);
266 printf(ARGS(stat, "%s, *"), path);
268 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
274 return (stat_copyout(&buf, args->up));
278 linux_lstat(struct thread *td, struct linux_lstat_args *args)
284 LCONVPATHEXIST(td, args->path, &path);
288 printf(ARGS(lstat, "%s, *"), path);
290 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
296 return (stat_copyout(&buf, args->up));
298 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
315 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
316 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
317 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
318 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
319 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
320 #define LINUX_NCP_SUPER_MAGIC 0x564cL
321 #define LINUX_NFS_SUPER_MAGIC 0x6969L
322 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
323 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
324 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
325 #define LINUX_ZFS_SUPER_MAGIC 0x2FC12FC1
326 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
327 #define LINUX_SHMFS_MAGIC 0x01021994
330 bsd_to_linux_ftype(const char *fstypename)
333 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
334 {"ufs", LINUX_UFS_SUPER_MAGIC},
335 {"zfs", LINUX_ZFS_SUPER_MAGIC},
336 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
337 {"nfs", LINUX_NFS_SUPER_MAGIC},
338 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
339 {"procfs", LINUX_PROC_SUPER_MAGIC},
340 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
341 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
342 {"nwfs", LINUX_NCP_SUPER_MAGIC},
343 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
344 {"coda", LINUX_CODA_SUPER_MAGIC},
345 {"devfs", LINUX_DEVFS_SUPER_MAGIC},
346 {"tmpfs", LINUX_SHMFS_MAGIC},
349 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
350 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
351 return (b2l_tbl[i].linux_type);
357 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
359 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
362 #define LINUX_HIBITS 0xffffffff00000000ULL
364 tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
366 if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
367 (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
368 (tmp & LINUX_HIBITS))
372 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
373 linux_statfs->f_bsize = bsd_statfs->f_bsize;
374 linux_statfs->f_blocks = bsd_statfs->f_blocks;
375 linux_statfs->f_bfree = bsd_statfs->f_bfree;
376 linux_statfs->f_bavail = bsd_statfs->f_bavail;
377 linux_statfs->f_ffree = bsd_statfs->f_ffree;
378 linux_statfs->f_files = bsd_statfs->f_files;
379 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
380 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
381 linux_statfs->f_namelen = MAXNAMLEN;
382 linux_statfs->f_frsize = bsd_statfs->f_bsize;
383 linux_statfs->f_flags = 0;
384 memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
390 linux_statfs(struct thread *td, struct linux_statfs_args *args)
392 struct l_statfs linux_statfs;
393 struct statfs *bsd_statfs;
397 LCONVPATHEXIST(td, args->path, &path);
401 printf(ARGS(statfs, "%s, *"), path);
403 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
404 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
407 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
408 free(bsd_statfs, M_STATFS);
411 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
414 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
416 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
419 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
420 linux_statfs->f_bsize = bsd_statfs->f_bsize;
421 linux_statfs->f_blocks = bsd_statfs->f_blocks;
422 linux_statfs->f_bfree = bsd_statfs->f_bfree;
423 linux_statfs->f_bavail = bsd_statfs->f_bavail;
424 linux_statfs->f_ffree = bsd_statfs->f_ffree;
425 linux_statfs->f_files = bsd_statfs->f_files;
426 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
427 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
428 linux_statfs->f_namelen = MAXNAMLEN;
429 linux_statfs->f_frsize = bsd_statfs->f_bsize;
430 linux_statfs->f_flags = 0;
431 memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
435 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
437 struct l_statfs64 linux_statfs;
438 struct statfs *bsd_statfs;
442 if (args->bufsize != sizeof(struct l_statfs64))
445 LCONVPATHEXIST(td, args->path, &path);
448 if (ldebug(statfs64))
449 printf(ARGS(statfs64, "%s, *"), path);
451 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
452 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
455 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
456 free(bsd_statfs, M_STATFS);
459 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
463 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
465 struct l_statfs64 linux_statfs;
466 struct statfs *bsd_statfs;
470 if (ldebug(fstatfs64))
471 printf(ARGS(fstatfs64, "%d, *"), args->fd);
473 if (args->bufsize != sizeof(struct l_statfs64))
476 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
477 error = kern_fstatfs(td, args->fd, bsd_statfs);
479 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
480 free(bsd_statfs, M_STATFS);
483 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
485 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
488 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
490 struct l_statfs linux_statfs;
491 struct statfs *bsd_statfs;
496 printf(ARGS(fstatfs, "%d, *"), args->fd);
498 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
499 error = kern_fstatfs(td, args->fd, bsd_statfs);
501 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
502 free(bsd_statfs, M_STATFS);
505 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
516 #ifdef LINUX_LEGACY_SYSCALLS
518 linux_ustat(struct thread *td, struct linux_ustat_args *args)
522 printf(ARGS(ustat, "%ju, *"), (uintmax_t)args->dev);
529 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
532 stat64_copyout(struct stat *buf, void *ubuf)
534 struct l_stat64 lbuf;
536 bzero(&lbuf, sizeof(lbuf));
537 lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
538 lbuf.st_ino = buf->st_ino;
539 lbuf.st_mode = buf->st_mode;
540 lbuf.st_nlink = buf->st_nlink;
541 lbuf.st_uid = buf->st_uid;
542 lbuf.st_gid = buf->st_gid;
543 lbuf.st_rdev = buf->st_rdev;
544 lbuf.st_size = buf->st_size;
545 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
546 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
547 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
548 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
549 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
550 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
551 lbuf.st_blksize = buf->st_blksize;
552 lbuf.st_blocks = buf->st_blocks;
555 * The __st_ino field makes all the difference. In the Linux kernel
556 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
557 * but without the assignment to __st_ino the runtime linker refuses
558 * to mmap(2) any shared libraries. I guess it's broken alright :-)
560 lbuf.__st_ino = buf->st_ino;
562 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
566 linux_stat64(struct thread *td, struct linux_stat64_args *args)
572 LCONVPATHEXIST(td, args->filename, &filename);
576 printf(ARGS(stat64, "%s, *"), filename);
579 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
583 return (stat64_copyout(&buf, args->statbuf));
587 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
593 LCONVPATHEXIST(td, args->filename, &filename);
597 printf(ARGS(lstat64, "%s, *"), args->filename);
600 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
604 return (stat64_copyout(&sb, args->statbuf));
608 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
615 printf(ARGS(fstat64, "%d, *"), args->fd);
618 error = kern_fstat(td, args->fd, &buf);
619 translate_fd_major_minor(td, args->fd, &buf);
621 error = stat64_copyout(&buf, args->statbuf);
627 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
630 int error, dfd, flag;
633 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
635 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
636 AT_SYMLINK_NOFOLLOW : 0;
638 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
639 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
642 if (ldebug(fstatat64))
643 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
646 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
648 error = stat64_copyout(&buf, args->statbuf);
654 #else /* __amd64__ && !COMPAT_LINUX32 */
657 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
660 int error, dfd, flag;
663 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
665 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
666 AT_SYMLINK_NOFOLLOW : 0;
668 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
669 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
672 if (ldebug(newfstatat))
673 printf(ARGS(newfstatat, "%i, %s, %i"), args->dfd, path, args->flag);
676 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
678 error = newstat_copyout(&buf, args->statbuf);
684 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
687 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
694 error = fgetvp(td, args->fd, cap_rights_init(&rights, CAP_FSYNC), &vp);
697 * Linux syncfs() returns only EBADF, however fgetvp()
698 * can return EINVAL in case of file descriptor does
699 * not represent a vnode. XXX.
704 mtx_lock(&mountlist_mtx);
705 error = vfs_busy(mp, MBF_MNTLSTLOCK);
707 /* See comment above. */
708 mtx_unlock(&mountlist_mtx);
711 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
712 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
713 save = curthread_pflags_set(TDP_SYNCIO);
714 vfs_msync(mp, MNT_NOWAIT);
715 VFS_SYNC(mp, MNT_NOWAIT);
716 curthread_pflags_restore(save);
717 vn_finished_write(mp);