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);
188 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
192 return (newstat_copyout(&buf, args->buf));
196 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
202 LCONVPATHEXIST(td, args->path, &path);
204 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
208 return (newstat_copyout(&sb, args->buf));
213 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
218 error = kern_fstat(td, args->fd, &buf);
219 translate_fd_major_minor(td, args->fd, &buf);
221 error = newstat_copyout(&buf, args->buf);
226 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
228 stat_copyout(struct stat *buf, void *ubuf)
232 bzero(&lbuf, sizeof(lbuf));
233 lbuf.st_dev = dev_to_ldev(buf->st_dev);
234 lbuf.st_ino = buf->st_ino;
235 lbuf.st_mode = buf->st_mode;
236 lbuf.st_nlink = buf->st_nlink;
237 lbuf.st_uid = buf->st_uid;
238 lbuf.st_gid = buf->st_gid;
239 lbuf.st_rdev = buf->st_rdev;
240 lbuf.st_size = MIN(buf->st_size, INT32_MAX);
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);
264 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
270 return (stat_copyout(&buf, args->up));
274 linux_lstat(struct thread *td, struct linux_lstat_args *args)
280 LCONVPATHEXIST(td, args->path, &path);
282 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
288 return (stat_copyout(&buf, args->up));
290 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
307 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
308 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
309 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
310 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
311 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
312 #define LINUX_NCP_SUPER_MAGIC 0x564cL
313 #define LINUX_NFS_SUPER_MAGIC 0x6969L
314 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
315 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
316 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
317 #define LINUX_ZFS_SUPER_MAGIC 0x2FC12FC1
318 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
319 #define LINUX_SHMFS_MAGIC 0x01021994
322 bsd_to_linux_ftype(const char *fstypename)
325 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
326 {"ufs", LINUX_UFS_SUPER_MAGIC},
327 {"zfs", LINUX_ZFS_SUPER_MAGIC},
328 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
329 {"nfs", LINUX_NFS_SUPER_MAGIC},
330 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
331 {"procfs", LINUX_PROC_SUPER_MAGIC},
332 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
333 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
334 {"nwfs", LINUX_NCP_SUPER_MAGIC},
335 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
336 {"coda", LINUX_CODA_SUPER_MAGIC},
337 {"devfs", LINUX_DEVFS_SUPER_MAGIC},
338 {"tmpfs", LINUX_SHMFS_MAGIC},
341 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
342 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
343 return (b2l_tbl[i].linux_type);
349 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
351 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
354 #define LINUX_HIBITS 0xffffffff00000000ULL
356 tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files |
358 if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) ||
359 (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) ||
360 (tmp & LINUX_HIBITS))
364 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
365 linux_statfs->f_bsize = bsd_statfs->f_bsize;
366 linux_statfs->f_blocks = bsd_statfs->f_blocks;
367 linux_statfs->f_bfree = bsd_statfs->f_bfree;
368 linux_statfs->f_bavail = bsd_statfs->f_bavail;
369 linux_statfs->f_ffree = bsd_statfs->f_ffree;
370 linux_statfs->f_files = bsd_statfs->f_files;
371 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
372 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
373 linux_statfs->f_namelen = MAXNAMLEN;
374 linux_statfs->f_frsize = bsd_statfs->f_bsize;
375 linux_statfs->f_flags = 0;
376 memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
382 linux_statfs(struct thread *td, struct linux_statfs_args *args)
384 struct l_statfs linux_statfs;
385 struct statfs *bsd_statfs;
389 LCONVPATHEXIST(td, args->path, &path);
391 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
392 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
395 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
396 free(bsd_statfs, M_STATFS);
399 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
402 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
404 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
407 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
408 linux_statfs->f_bsize = bsd_statfs->f_bsize;
409 linux_statfs->f_blocks = bsd_statfs->f_blocks;
410 linux_statfs->f_bfree = bsd_statfs->f_bfree;
411 linux_statfs->f_bavail = bsd_statfs->f_bavail;
412 linux_statfs->f_ffree = bsd_statfs->f_ffree;
413 linux_statfs->f_files = bsd_statfs->f_files;
414 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
415 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
416 linux_statfs->f_namelen = MAXNAMLEN;
417 linux_statfs->f_frsize = bsd_statfs->f_bsize;
418 linux_statfs->f_flags = 0;
419 memset(linux_statfs->f_spare, 0, sizeof(linux_statfs->f_spare));
423 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
425 struct l_statfs64 linux_statfs;
426 struct statfs *bsd_statfs;
430 if (args->bufsize != sizeof(struct l_statfs64))
433 LCONVPATHEXIST(td, args->path, &path);
435 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
436 error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs);
439 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
440 free(bsd_statfs, M_STATFS);
443 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
447 linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args)
449 struct l_statfs64 linux_statfs;
450 struct statfs *bsd_statfs;
453 if (args->bufsize != sizeof(struct l_statfs64))
456 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
457 error = kern_fstatfs(td, args->fd, bsd_statfs);
459 bsd_to_linux_statfs64(bsd_statfs, &linux_statfs);
460 free(bsd_statfs, M_STATFS);
463 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
465 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
468 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
470 struct l_statfs linux_statfs;
471 struct statfs *bsd_statfs;
474 bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
475 error = kern_fstatfs(td, args->fd, bsd_statfs);
477 error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs);
478 free(bsd_statfs, M_STATFS);
481 return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs)));
492 #ifdef LINUX_LEGACY_SYSCALLS
494 linux_ustat(struct thread *td, struct linux_ustat_args *args)
501 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
504 stat64_copyout(struct stat *buf, void *ubuf)
506 struct l_stat64 lbuf;
508 bzero(&lbuf, sizeof(lbuf));
509 lbuf.st_dev = dev_to_ldev(buf->st_dev);
510 lbuf.st_ino = buf->st_ino;
511 lbuf.st_mode = buf->st_mode;
512 lbuf.st_nlink = buf->st_nlink;
513 lbuf.st_uid = buf->st_uid;
514 lbuf.st_gid = buf->st_gid;
515 lbuf.st_rdev = buf->st_rdev;
516 lbuf.st_size = buf->st_size;
517 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
518 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
519 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
520 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
521 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
522 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
523 lbuf.st_blksize = buf->st_blksize;
524 lbuf.st_blocks = buf->st_blocks;
527 * The __st_ino field makes all the difference. In the Linux kernel
528 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
529 * but without the assignment to __st_ino the runtime linker refuses
530 * to mmap(2) any shared libraries. I guess it's broken alright :-)
532 lbuf.__st_ino = buf->st_ino;
534 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
538 linux_stat64(struct thread *td, struct linux_stat64_args *args)
544 LCONVPATHEXIST(td, args->filename, &filename);
546 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
550 return (stat64_copyout(&buf, args->statbuf));
554 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
560 LCONVPATHEXIST(td, args->filename, &filename);
562 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
566 return (stat64_copyout(&sb, args->statbuf));
570 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
575 error = kern_fstat(td, args->fd, &buf);
576 translate_fd_major_minor(td, args->fd, &buf);
578 error = stat64_copyout(&buf, args->statbuf);
584 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
587 int error, dfd, flag;
590 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
592 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
593 AT_SYMLINK_NOFOLLOW : 0;
595 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
596 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
598 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
600 error = stat64_copyout(&buf, args->statbuf);
606 #else /* __amd64__ && !COMPAT_LINUX32 */
609 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
612 int error, dfd, flag;
615 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
617 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
618 AT_SYMLINK_NOFOLLOW : 0;
620 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
621 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
623 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
625 error = newstat_copyout(&buf, args->statbuf);
631 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
634 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
640 error = fgetvp(td, args->fd, &cap_fsync_rights, &vp);
643 * Linux syncfs() returns only EBADF, however fgetvp()
644 * can return EINVAL in case of file descriptor does
645 * not represent a vnode. XXX.
650 mtx_lock(&mountlist_mtx);
651 error = vfs_busy(mp, MBF_MNTLSTLOCK);
653 /* See comment above. */
654 mtx_unlock(&mountlist_mtx);
657 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
658 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
659 save = curthread_pflags_set(TDP_SYNCIO);
660 vfs_msync(mp, MNT_NOWAIT);
661 VFS_SYNC(mp, MNT_NOWAIT);
662 curthread_pflags_restore(save);
663 vn_finished_write(mp);