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 (vn_isdisk(vp, NULL)) {
69 sb->st_mode &= ~S_IFMT;
70 sb->st_mode |= S_IFBLK;
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);
81 linux_kern_statat(struct thread *td, int flag, int fd, char *path,
82 enum uio_seg pathseg, struct stat *sbp)
85 return (kern_statat(td, flag, fd, path, pathseg, sbp,
86 translate_vnhook_major_minor));
89 #ifdef LINUX_LEGACY_SYSCALLS
91 linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
95 return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
99 linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
103 return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
109 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
116 * No capability rights required here.
118 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
119 fget(td, fd, &cap_no_rights, &fp) != 0)
122 if (vp != NULL && vn_isdisk(vp, NULL)) {
123 buf->st_mode &= ~S_IFMT;
124 buf->st_mode |= S_IFBLK;
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;
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);
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.
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.
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.
159 #define dev_to_ldev(d) ((uint16_t)(d))
162 newstat_copyout(struct stat *buf, void *ubuf)
164 struct l_newstat tbuf;
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;
184 return (copyout(&tbuf, ubuf, sizeof(tbuf)));
187 #ifdef LINUX_LEGACY_SYSCALLS
189 linux_newstat(struct thread *td, struct linux_newstat_args *args)
195 LCONVPATHEXIST(td, args->path, &path);
197 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
201 return (newstat_copyout(&buf, args->buf));
205 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
211 LCONVPATHEXIST(td, args->path, &path);
213 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
217 return (newstat_copyout(&sb, args->buf));
222 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
227 error = kern_fstat(td, args->fd, &buf);
228 translate_fd_major_minor(td, args->fd, &buf);
230 error = newstat_copyout(&buf, args->buf);
235 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
237 stat_copyout(struct stat *buf, void *ubuf)
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;
261 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
265 linux_stat(struct thread *td, struct linux_stat_args *args)
271 LCONVPATHEXIST(td, args->path, &path);
273 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
279 return (stat_copyout(&buf, args->up));
283 linux_lstat(struct thread *td, struct linux_lstat_args *args)
289 LCONVPATHEXIST(td, args->path, &path);
291 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
297 return (stat_copyout(&buf, args->up));
299 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
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
331 bsd_to_linux_ftype(const char *fstypename)
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},
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);
358 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
360 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
363 #define LINUX_HIBITS 0xffffffff00000000ULL
365 tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
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))
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));
391 linux_statfs(struct thread *td, struct linux_statfs_args *args)
393 struct l_statfs linux_statfs;
394 struct statfs *bsd_statfs;
398 LCONVPATHEXIST(td, args->path, &path);
400 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
401 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
404 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
405 free(bsd_statfs, M_STATFS);
408 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
411 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
413 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
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));
432 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
434 struct l_statfs64 linux_statfs;
435 struct statfs *bsd_statfs;
439 if (args->bufsize != sizeof(struct l_statfs64))
442 LCONVPATHEXIST(td, args->path, &path);
444 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
445 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
448 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
449 free(bsd_statfs, M_STATFS);
452 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
456 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
458 struct l_statfs64 linux_statfs;
459 struct statfs *bsd_statfs;
462 if (args->bufsize != sizeof(struct l_statfs64))
465 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
466 error = kern_fstatfs(td, args->fd, bsd_statfs);
468 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
469 free(bsd_statfs, M_STATFS);
472 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
474 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
477 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
479 struct l_statfs linux_statfs;
480 struct statfs *bsd_statfs;
483 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
484 error = kern_fstatfs(td, args->fd, bsd_statfs);
486 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
487 free(bsd_statfs, M_STATFS);
490 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
501 #ifdef LINUX_LEGACY_SYSCALLS
503 linux_ustat(struct thread *td, struct linux_ustat_args *args)
510 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
513 stat64_copyout(struct stat *buf, void *ubuf)
515 struct l_stat64 lbuf;
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;
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 :-)
541 lbuf.__st_ino = buf->st_ino;
543 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
547 linux_stat64(struct thread *td, struct linux_stat64_args *args)
553 LCONVPATHEXIST(td, args->filename, &filename);
555 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
559 return (stat64_copyout(&buf, args->statbuf));
563 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
569 LCONVPATHEXIST(td, args->filename, &filename);
571 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
575 return (stat64_copyout(&sb, args->statbuf));
579 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
584 error = kern_fstat(td, args->fd, &buf);
585 translate_fd_major_minor(td, args->fd, &buf);
587 error = stat64_copyout(&buf, args->statbuf);
593 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
596 int error, dfd, flag;
599 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
601 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
602 AT_SYMLINK_NOFOLLOW : 0;
604 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
605 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
607 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
609 error = stat64_copyout(&buf, args->statbuf);
615 #else /* __amd64__ && !COMPAT_LINUX32 */
618 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
621 int error, dfd, flag;
624 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
626 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
627 AT_SYMLINK_NOFOLLOW : 0;
629 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
630 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
632 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
634 error = newstat_copyout(&buf, args->statbuf);
640 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
643 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
649 error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
652 * Linux syncfs() returns only EBADF, however fgetvp()
653 * can return EINVAL in case of file descriptor does
654 * not represent a vnode. XXX.
659 mtx_lock(&mountlist_mtx);
660 error = vfs_busy(mp, MBF_MNTLSTLOCK);
662 /* See comment above. */
663 mtx_unlock(&mountlist_mtx);
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);