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)
111 * No capability rights required here.
113 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
114 fget(td, fd, &cap_no_rights, &fp) != 0)
117 if (vp != NULL && vp->v_rdev != NULL &&
118 linux_driver_get_major_minor(devtoname(vp->v_rdev),
119 &major, &minor) == 0) {
120 buf->st_rdev = (major << 8 | minor);
121 } else if (fp->f_type == DTYPE_PTS) {
122 struct tty *tp = fp->f_data;
124 /* Convert the numbers for the slave device. */
125 if (linux_driver_get_major_minor(devtoname(tp->t_dev),
126 &major, &minor) == 0) {
127 buf->st_rdev = (major << 8 | minor);
134 * l_dev_t has the same encoding as dev_t in the latter's low 16 bits, so
135 * truncation of a dev_t to 16 bits gives the same result as unpacking
136 * using major() and minor() and repacking in the l_dev_t format. This
137 * detail is hidden in dev_to_ldev(). Overflow in conversions of dev_t's
138 * are not checked for, as for other fields.
140 * dev_to_ldev() is only used for translating st_dev. When we convert
141 * st_rdev for copying it out, it isn't really a dev_t, but has already
142 * been translated to an l_dev_t in a nontrivial way. Translating it
143 * again would be illogical but would have no effect since the low 16
144 * bits have the same encoding.
146 * The nontrivial translation for st_rdev renumbers some devices, but not
147 * ones that can be mounted on, so it is consistent with the translation
148 * for st_dev except when the renumbering or truncation causes conflicts.
150 #define dev_to_ldev(d) ((uint16_t)(d))
153 newstat_copyout(struct stat *buf, void *ubuf)
155 struct l_newstat tbuf;
157 bzero(&tbuf, sizeof(tbuf));
158 tbuf.st_dev = dev_to_ldev(buf->st_dev);
159 tbuf.st_ino = buf->st_ino;
160 tbuf.st_mode = buf->st_mode;
161 tbuf.st_nlink = buf->st_nlink;
162 tbuf.st_uid = buf->st_uid;
163 tbuf.st_gid = buf->st_gid;
164 tbuf.st_rdev = buf->st_rdev;
165 tbuf.st_size = buf->st_size;
166 tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
167 tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
168 tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
169 tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
170 tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
171 tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
172 tbuf.st_blksize = buf->st_blksize;
173 tbuf.st_blocks = buf->st_blocks;
175 return (copyout(&tbuf, ubuf, sizeof(tbuf)));
178 #ifdef LINUX_LEGACY_SYSCALLS
180 linux_newstat(struct thread *td, struct linux_newstat_args *args)
186 LCONVPATHEXIST(td, args->path, &path);
190 printf(ARGS(newstat, "%s, *"), path);
193 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
197 return (newstat_copyout(&buf, args->buf));
201 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
207 LCONVPATHEXIST(td, args->path, &path);
210 if (ldebug(newlstat))
211 printf(ARGS(newlstat, "%s, *"), path);
214 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
218 return (newstat_copyout(&sb, args->buf));
223 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
229 if (ldebug(newfstat))
230 printf(ARGS(newfstat, "%d, *"), args->fd);
233 error = kern_fstat(td, args->fd, &buf);
234 translate_fd_major_minor(td, args->fd, &buf);
236 error = newstat_copyout(&buf, args->buf);
241 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
243 stat_copyout(struct stat *buf, void *ubuf)
247 bzero(&lbuf, sizeof(lbuf));
248 lbuf.st_dev = dev_to_ldev(buf->st_dev);
249 lbuf.st_ino = buf->st_ino;
250 lbuf.st_mode = buf->st_mode;
251 lbuf.st_nlink = buf->st_nlink;
252 lbuf.st_uid = buf->st_uid;
253 lbuf.st_gid = buf->st_gid;
254 lbuf.st_rdev = buf->st_rdev;
255 lbuf.st_size = MIN(buf->st_size, INT32_MAX);
256 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
257 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
258 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
259 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
260 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
261 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
262 lbuf.st_blksize = buf->st_blksize;
263 lbuf.st_blocks = buf->st_blocks;
264 lbuf.st_flags = buf->st_flags;
265 lbuf.st_gen = buf->st_gen;
267 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
271 linux_stat(struct thread *td, struct linux_stat_args *args)
277 LCONVPATHEXIST(td, args->path, &path);
281 printf(ARGS(stat, "%s, *"), path);
283 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
289 return (stat_copyout(&buf, args->up));
293 linux_lstat(struct thread *td, struct linux_lstat_args *args)
299 LCONVPATHEXIST(td, args->path, &path);
303 printf(ARGS(lstat, "%s, *"), path);
305 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
311 return (stat_copyout(&buf, args->up));
313 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
330 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
331 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
332 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
333 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
334 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
335 #define LINUX_NCP_SUPER_MAGIC 0x564cL
336 #define LINUX_NFS_SUPER_MAGIC 0x6969L
337 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
338 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
339 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
340 #define LINUX_ZFS_SUPER_MAGIC 0x2FC12FC1
341 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
342 #define LINUX_SHMFS_MAGIC 0x01021994
345 bsd_to_linux_ftype(const char *fstypename)
348 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
349 {"ufs", LINUX_UFS_SUPER_MAGIC},
350 {"zfs", LINUX_ZFS_SUPER_MAGIC},
351 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
352 {"nfs", LINUX_NFS_SUPER_MAGIC},
353 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
354 {"procfs", LINUX_PROC_SUPER_MAGIC},
355 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
356 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
357 {"nwfs", LINUX_NCP_SUPER_MAGIC},
358 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
359 {"coda", LINUX_CODA_SUPER_MAGIC},
360 {"devfs", LINUX_DEVFS_SUPER_MAGIC},
361 {"tmpfs", LINUX_SHMFS_MAGIC},
364 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
365 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
366 return (b2l_tbl[i].linux_type);
372 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
374 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
377 #define LINUX_HIBITS 0xffffffff00000000ULL
379 tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
381 if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
382 (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
383 (tmp & LINUX_HIBITS))
387 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
388 linux_statfs->f_bsize = bsd_statfs->f_bsize;
389 linux_statfs->f_blocks = bsd_statfs->f_blocks;
390 linux_statfs->f_bfree = bsd_statfs->f_bfree;
391 linux_statfs->f_bavail = bsd_statfs->f_bavail;
392 linux_statfs->f_ffree = bsd_statfs->f_ffree;
393 linux_statfs->f_files = bsd_statfs->f_files;
394 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
395 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
396 linux_statfs->f_namelen = MAXNAMLEN;
397 linux_statfs->f_frsize = bsd_statfs->f_bsize;
398 linux_statfs->f_flags = 0;
399 memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
405 linux_statfs(struct thread *td, struct linux_statfs_args *args)
407 struct l_statfs linux_statfs;
408 struct statfs *bsd_statfs;
412 LCONVPATHEXIST(td, args->path, &path);
416 printf(ARGS(statfs, "%s, *"), path);
418 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
419 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
422 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
423 free(bsd_statfs, M_STATFS);
426 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
429 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
431 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
434 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
435 linux_statfs->f_bsize = bsd_statfs->f_bsize;
436 linux_statfs->f_blocks = bsd_statfs->f_blocks;
437 linux_statfs->f_bfree = bsd_statfs->f_bfree;
438 linux_statfs->f_bavail = bsd_statfs->f_bavail;
439 linux_statfs->f_ffree = bsd_statfs->f_ffree;
440 linux_statfs->f_files = bsd_statfs->f_files;
441 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
442 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
443 linux_statfs->f_namelen = MAXNAMLEN;
444 linux_statfs->f_frsize = bsd_statfs->f_bsize;
445 linux_statfs->f_flags = 0;
446 memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
450 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
452 struct l_statfs64 linux_statfs;
453 struct statfs *bsd_statfs;
457 if (args->bufsize != sizeof(struct l_statfs64))
460 LCONVPATHEXIST(td, args->path, &path);
463 if (ldebug(statfs64))
464 printf(ARGS(statfs64, "%s, *"), path);
466 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
467 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
470 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
471 free(bsd_statfs, M_STATFS);
474 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
478 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
480 struct l_statfs64 linux_statfs;
481 struct statfs *bsd_statfs;
485 if (ldebug(fstatfs64))
486 printf(ARGS(fstatfs64, "%d, *"), args->fd);
488 if (args->bufsize != sizeof(struct l_statfs64))
491 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
492 error = kern_fstatfs(td, args->fd, bsd_statfs);
494 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
495 free(bsd_statfs, M_STATFS);
498 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
500 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
503 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
505 struct l_statfs linux_statfs;
506 struct statfs *bsd_statfs;
511 printf(ARGS(fstatfs, "%d, *"), args->fd);
513 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
514 error = kern_fstatfs(td, args->fd, bsd_statfs);
516 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
517 free(bsd_statfs, M_STATFS);
520 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
531 #ifdef LINUX_LEGACY_SYSCALLS
533 linux_ustat(struct thread *td, struct linux_ustat_args *args)
537 printf(ARGS(ustat, "%ju, *"), (uintmax_t)args->dev);
544 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
547 stat64_copyout(struct stat *buf, void *ubuf)
549 struct l_stat64 lbuf;
551 bzero(&lbuf, sizeof(lbuf));
552 lbuf.st_dev = dev_to_ldev(buf->st_dev);
553 lbuf.st_ino = buf->st_ino;
554 lbuf.st_mode = buf->st_mode;
555 lbuf.st_nlink = buf->st_nlink;
556 lbuf.st_uid = buf->st_uid;
557 lbuf.st_gid = buf->st_gid;
558 lbuf.st_rdev = buf->st_rdev;
559 lbuf.st_size = buf->st_size;
560 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
561 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
562 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
563 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
564 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
565 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
566 lbuf.st_blksize = buf->st_blksize;
567 lbuf.st_blocks = buf->st_blocks;
570 * The __st_ino field makes all the difference. In the Linux kernel
571 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
572 * but without the assignment to __st_ino the runtime linker refuses
573 * to mmap(2) any shared libraries. I guess it's broken alright :-)
575 lbuf.__st_ino = buf->st_ino;
577 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
581 linux_stat64(struct thread *td, struct linux_stat64_args *args)
587 LCONVPATHEXIST(td, args->filename, &filename);
591 printf(ARGS(stat64, "%s, *"), filename);
594 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
598 return (stat64_copyout(&buf, args->statbuf));
602 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
608 LCONVPATHEXIST(td, args->filename, &filename);
612 printf(ARGS(lstat64, "%s, *"), args->filename);
615 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
619 return (stat64_copyout(&sb, args->statbuf));
623 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
630 printf(ARGS(fstat64, "%d, *"), args->fd);
633 error = kern_fstat(td, args->fd, &buf);
634 translate_fd_major_minor(td, args->fd, &buf);
636 error = stat64_copyout(&buf, args->statbuf);
642 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
645 int error, dfd, flag;
648 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
650 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
651 AT_SYMLINK_NOFOLLOW : 0;
653 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
654 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
657 if (ldebug(fstatat64))
658 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
661 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
663 error = stat64_copyout(&buf, args->statbuf);
669 #else /* __amd64__ && !COMPAT_LINUX32 */
672 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
675 int error, dfd, flag;
678 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
680 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
681 AT_SYMLINK_NOFOLLOW : 0;
683 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
684 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
687 if (ldebug(newfstatat))
688 printf(ARGS(newfstatat, "%i, %s, %i"), args->dfd, path, args->flag);
691 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
693 error = newstat_copyout(&buf, args->statbuf);
699 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
702 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
708 error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
711 * Linux syncfs() returns only EBADF, however fgetvp()
712 * can return EINVAL in case of file descriptor does
713 * not represent a vnode. XXX.
718 mtx_lock(&mountlist_mtx);
719 error = vfs_busy(mp, MBF_MNTLSTLOCK);
721 /* See comment above. */
722 mtx_unlock(&mountlist_mtx);
725 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
726 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
727 save = curthread_pflags_set(TDP_SYNCIO);
728 vfs_msync(mp, MNT_NOWAIT);
729 VFS_SYNC(mp, MNT_NOWAIT);
730 curthread_pflags_restore(save);
731 vn_finished_write(mp);