2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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));
85 linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg,
89 return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp));
93 linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg,
97 return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
102 * XXX: This was removed from newstat_copyout(), and almost identical
103 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced
104 * XXX: with something that does lookup and locking properly.
105 * XXX: When somebody fixes this: please try to avoid duplicating it.
109 disk_foo(struct somestat *tbuf)
111 struct cdevsw *cdevsw;
114 /* Lie about disk drives which are character devices
115 * in FreeBSD but block devices under Linux.
117 if (S_ISCHR(tbuf.st_mode) &&
118 (dev = findcdev(buf->st_rdev)) != NULL) {
119 cdevsw = dev_refthread(dev);
120 if (cdevsw != NULL) {
121 if (cdevsw->d_flags & D_DISK) {
122 tbuf.st_mode &= ~S_IFMT;
123 tbuf.st_mode |= S_IFBLK;
125 /* XXX this may not be quite right */
126 /* Map major number to 0 */
127 tbuf.st_dev = minor(buf->st_dev) & 0xf;
128 tbuf.st_rdev = buf->st_rdev & 0xff;
138 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
146 * No capability rights required here.
148 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
149 fget(td, fd, cap_rights_init(&rights), &fp) != 0)
152 if (vp != NULL && vp->v_rdev != NULL &&
153 linux_driver_get_major_minor(devtoname(vp->v_rdev),
154 &major, &minor) == 0) {
155 buf->st_rdev = (major << 8 | minor);
156 } else if (fp->f_type == DTYPE_PTS) {
157 struct tty *tp = fp->f_data;
159 /* Convert the numbers for the slave device. */
160 if (linux_driver_get_major_minor(devtoname(tp->t_dev),
161 &major, &minor) == 0) {
162 buf->st_rdev = (major << 8 | minor);
169 newstat_copyout(struct stat *buf, void *ubuf)
171 struct l_newstat tbuf;
173 bzero(&tbuf, sizeof(tbuf));
174 tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
175 tbuf.st_ino = buf->st_ino;
176 tbuf.st_mode = buf->st_mode;
177 tbuf.st_nlink = buf->st_nlink;
178 tbuf.st_uid = buf->st_uid;
179 tbuf.st_gid = buf->st_gid;
180 tbuf.st_rdev = buf->st_rdev;
181 tbuf.st_size = buf->st_size;
182 tbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
183 tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
184 tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
185 tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
186 tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
187 tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
188 tbuf.st_blksize = buf->st_blksize;
189 tbuf.st_blocks = buf->st_blocks;
191 return (copyout(&tbuf, ubuf, sizeof(tbuf)));
195 linux_newstat(struct thread *td, struct linux_newstat_args *args)
201 LCONVPATHEXIST(td, args->path, &path);
205 printf(ARGS(newstat, "%s, *"), path);
208 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
212 return (newstat_copyout(&buf, args->buf));
216 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
222 LCONVPATHEXIST(td, args->path, &path);
225 if (ldebug(newlstat))
226 printf(ARGS(newlstat, "%s, *"), path);
229 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb);
233 return (newstat_copyout(&sb, args->buf));
237 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
243 if (ldebug(newfstat))
244 printf(ARGS(newfstat, "%d, *"), args->fd);
247 error = kern_fstat(td, args->fd, &buf);
248 translate_fd_major_minor(td, args->fd, &buf);
250 error = newstat_copyout(&buf, args->buf);
255 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
257 stat_copyout(struct stat *buf, void *ubuf)
261 bzero(&lbuf, sizeof(lbuf));
262 lbuf.st_dev = buf->st_dev;
263 lbuf.st_ino = buf->st_ino;
264 lbuf.st_mode = buf->st_mode;
265 lbuf.st_nlink = buf->st_nlink;
266 lbuf.st_uid = buf->st_uid;
267 lbuf.st_gid = buf->st_gid;
268 lbuf.st_rdev = buf->st_rdev;
269 if (buf->st_size < (quad_t)1 << 32)
270 lbuf.st_size = buf->st_size;
273 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
274 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
275 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
276 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
277 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
278 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
279 lbuf.st_blksize = buf->st_blksize;
280 lbuf.st_blocks = buf->st_blocks;
281 lbuf.st_flags = buf->st_flags;
282 lbuf.st_gen = buf->st_gen;
284 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
288 linux_stat(struct thread *td, struct linux_stat_args *args)
294 LCONVPATHEXIST(td, args->path, &path);
298 printf(ARGS(stat, "%s, *"), path);
300 error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf);
306 return(stat_copyout(&buf, args->up));
310 linux_lstat(struct thread *td, struct linux_lstat_args *args)
316 LCONVPATHEXIST(td, args->path, &path);
320 printf(ARGS(lstat, "%s, *"), path);
322 error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf);
328 return(stat_copyout(&buf, args->up));
330 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
345 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
346 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
347 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
348 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
349 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
350 #define LINUX_NCP_SUPER_MAGIC 0x564cL
351 #define LINUX_NFS_SUPER_MAGIC 0x6969L
352 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
353 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
354 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
355 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
356 #define LINUX_SHMFS_MAGIC 0x01021994
359 bsd_to_linux_ftype(const char *fstypename)
362 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
363 {"ufs", LINUX_UFS_SUPER_MAGIC},
364 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
365 {"nfs", LINUX_NFS_SUPER_MAGIC},
366 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
367 {"procfs", LINUX_PROC_SUPER_MAGIC},
368 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
369 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
370 {"nwfs", LINUX_NCP_SUPER_MAGIC},
371 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
372 {"coda", LINUX_CODA_SUPER_MAGIC},
373 {"devfs", LINUX_DEVFS_SUPER_MAGIC},
374 {"tmpfs", LINUX_SHMFS_MAGIC},
377 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
378 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
379 return (b2l_tbl[i].linux_type);
385 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
388 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
389 linux_statfs->f_bsize = bsd_statfs->f_bsize;
390 linux_statfs->f_blocks = bsd_statfs->f_blocks;
391 linux_statfs->f_bfree = bsd_statfs->f_bfree;
392 linux_statfs->f_bavail = bsd_statfs->f_bavail;
393 linux_statfs->f_ffree = bsd_statfs->f_ffree;
394 linux_statfs->f_files = bsd_statfs->f_files;
395 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
396 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
397 linux_statfs->f_namelen = MAXNAMLEN;
401 linux_statfs(struct thread *td, struct linux_statfs_args *args)
403 struct l_statfs linux_statfs;
404 struct statfs bsd_statfs;
408 LCONVPATHEXIST(td, args->path, &path);
412 printf(ARGS(statfs, "%s, *"), path);
414 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
418 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
419 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
422 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
424 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
427 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
428 linux_statfs->f_bsize = bsd_statfs->f_bsize;
429 linux_statfs->f_blocks = bsd_statfs->f_blocks;
430 linux_statfs->f_bfree = bsd_statfs->f_bfree;
431 linux_statfs->f_bavail = bsd_statfs->f_bavail;
432 linux_statfs->f_ffree = bsd_statfs->f_ffree;
433 linux_statfs->f_files = bsd_statfs->f_files;
434 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
435 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
436 linux_statfs->f_namelen = MAXNAMLEN;
440 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
442 struct l_statfs64 linux_statfs;
443 struct statfs bsd_statfs;
447 if (args->bufsize != sizeof(struct l_statfs64))
450 LCONVPATHEXIST(td, args->path, &path);
453 if (ldebug(statfs64))
454 printf(ARGS(statfs64, "%s, *"), path);
456 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
460 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
461 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
463 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
466 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
468 struct l_statfs linux_statfs;
469 struct statfs bsd_statfs;
474 printf(ARGS(fstatfs, "%d, *"), args->fd);
476 error = kern_fstatfs(td, args->fd, &bsd_statfs);
479 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
480 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
492 linux_ustat(struct thread *td, struct linux_ustat_args *args)
496 printf(ARGS(ustat, "%ju, *"), (uintmax_t)args->dev);
502 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
505 stat64_copyout(struct stat *buf, void *ubuf)
507 struct l_stat64 lbuf;
509 bzero(&lbuf, sizeof(lbuf));
510 lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
511 lbuf.st_ino = buf->st_ino;
512 lbuf.st_mode = buf->st_mode;
513 lbuf.st_nlink = buf->st_nlink;
514 lbuf.st_uid = buf->st_uid;
515 lbuf.st_gid = buf->st_gid;
516 lbuf.st_rdev = buf->st_rdev;
517 lbuf.st_size = buf->st_size;
518 lbuf.st_atim.tv_sec = buf->st_atim.tv_sec;
519 lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec;
520 lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec;
521 lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec;
522 lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec;
523 lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec;
524 lbuf.st_blksize = buf->st_blksize;
525 lbuf.st_blocks = buf->st_blocks;
528 * The __st_ino field makes all the difference. In the Linux kernel
529 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
530 * but without the assignment to __st_ino the runtime linker refuses
531 * to mmap(2) any shared libraries. I guess it's broken alright :-)
533 lbuf.__st_ino = buf->st_ino;
535 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
539 linux_stat64(struct thread *td, struct linux_stat64_args *args)
545 LCONVPATHEXIST(td, args->filename, &filename);
549 printf(ARGS(stat64, "%s, *"), filename);
552 error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf);
556 return (stat64_copyout(&buf, args->statbuf));
560 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
566 LCONVPATHEXIST(td, args->filename, &filename);
570 printf(ARGS(lstat64, "%s, *"), args->filename);
573 error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb);
577 return (stat64_copyout(&sb, args->statbuf));
581 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
588 printf(ARGS(fstat64, "%d, *"), args->fd);
591 error = kern_fstat(td, args->fd, &buf);
592 translate_fd_major_minor(td, args->fd, &buf);
594 error = stat64_copyout(&buf, args->statbuf);
600 linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args)
603 int error, dfd, flag;
606 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
608 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
609 AT_SYMLINK_NOFOLLOW : 0;
611 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
612 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
615 if (ldebug(fstatat64))
616 printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag);
619 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
621 error = stat64_copyout(&buf, args->statbuf);
627 #else /* __amd64__ && !COMPAT_LINUX32 */
630 linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args)
633 int error, dfd, flag;
636 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
638 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ?
639 AT_SYMLINK_NOFOLLOW : 0;
641 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
642 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
645 if (ldebug(newfstatat))
646 printf(ARGS(newfstatat, "%i, %s, %i"), args->dfd, path, args->flag);
649 error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf);
651 error = newstat_copyout(&buf, args->statbuf);
657 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
660 linux_syncfs(struct thread *td, struct linux_syncfs_args *args)
667 error = fgetvp(td, args->fd, cap_rights_init(&rights, CAP_FSYNC), &vp);
670 * Linux syncfs() returns only EBADF, however fgetvp()
671 * can return EINVAL in case of file descriptor does
672 * not represent a vnode. XXX.
677 mtx_lock(&mountlist_mtx);
678 error = vfs_busy(mp, MBF_MNTLSTLOCK);
680 /* See comment above. */
681 mtx_unlock(&mountlist_mtx);
684 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
685 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
686 save = curthread_pflags_set(TDP_SYNCIO);
687 vfs_msync(mp, MNT_NOWAIT);
688 VFS_SYNC(mp, MNT_NOWAIT);
689 curthread_pflags_restore(save);
690 vn_finished_write(mp);