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"
35 #include <sys/param.h>
36 #include <sys/dirent.h>
38 #include <sys/filedesc.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
45 #include <sys/syscallsubr.h>
46 #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>
61 #include <security/mac/mac_framework.h>
64 * XXX: This was removed from newstat_copyout(), and almost identical
65 * XXX: code was in stat64_copyout(). findcdev() needs to be replaced
66 * XXX: with something that does lookup and locking properly.
67 * XXX: When somebody fixes this: please try to avoid duplicating it.
71 disk_foo(struct somestat *tbuf)
73 struct cdevsw *cdevsw;
76 /* Lie about disk drives which are character devices
77 * in FreeBSD but block devices under Linux.
79 if (S_ISCHR(tbuf.st_mode) &&
80 (dev = findcdev(buf->st_rdev)) != NULL) {
81 cdevsw = dev_refthread(dev);
83 if (cdevsw->d_flags & D_DISK) {
84 tbuf.st_mode &= ~S_IFMT;
85 tbuf.st_mode |= S_IFBLK;
87 /* XXX this may not be quite right */
88 /* Map major number to 0 */
89 tbuf.st_dev = uminor(buf->st_dev) & 0xf;
90 tbuf.st_rdev = buf->st_rdev & 0xff;
100 translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
105 if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) ||
106 fget(td, fd, &fp) != 0)
108 if (fp->f_vnode != NULL &&
109 fp->f_vnode->v_un.vu_cdev != NULL &&
110 linux_driver_get_major_minor(fp->f_vnode->v_un.vu_cdev->si_name,
111 &major, &minor) == 0)
112 buf->st_rdev = (major << 8 | minor);
117 translate_path_major_minor(struct thread *td, char *path, struct stat *buf)
119 struct proc *p = td->td_proc;
120 struct filedesc *fdp = p->p_fd;
124 if (!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode))
126 temp = td->td_retval[0];
127 if (kern_open(td, path, UIO_SYSSPACE, O_RDONLY, 0) != 0)
129 fd = td->td_retval[0];
130 td->td_retval[0] = temp;
131 translate_fd_major_minor(td, fd, buf);
132 fdclose(fdp, fdp->fd_ofiles[fd], fd, td);
136 newstat_copyout(struct stat *buf, void *ubuf)
138 struct l_newstat tbuf;
140 bzero(&tbuf, sizeof(tbuf));
141 tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
142 tbuf.st_ino = buf->st_ino;
143 tbuf.st_mode = buf->st_mode;
144 tbuf.st_nlink = buf->st_nlink;
145 tbuf.st_uid = buf->st_uid;
146 tbuf.st_gid = buf->st_gid;
147 tbuf.st_rdev = buf->st_rdev;
148 tbuf.st_size = buf->st_size;
149 tbuf.st_atime = buf->st_atime;
150 tbuf.st_mtime = buf->st_mtime;
151 tbuf.st_ctime = buf->st_ctime;
152 tbuf.st_blksize = buf->st_blksize;
153 tbuf.st_blocks = buf->st_blocks;
155 return (copyout(&tbuf, ubuf, sizeof(tbuf)));
159 linux_newstat(struct thread *td, struct linux_newstat_args *args)
165 LCONVPATHEXIST(td, args->path, &path);
169 printf(ARGS(newstat, "%s, *"), path);
172 error = kern_stat(td, path, UIO_SYSSPACE, &buf);
174 if (strlen(path) > strlen("/dev/pts/") &&
175 !strncmp(path, "/dev/pts/", strlen("/dev/pts/")) &&
176 path[9] >= '0' && path[9] <= '9') {
178 * Linux checks major and minors of the slave device
179 * to make sure it's a pty device, so let's make him
182 buf.st_rdev = (136 << 8);
184 translate_path_major_minor(td, path, &buf);
189 return (newstat_copyout(&buf, args->buf));
193 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
199 LCONVPATHEXIST(td, args->path, &path);
202 if (ldebug(newlstat))
203 printf(ARGS(newlstat, "%s, *"), path);
206 error = kern_lstat(td, path, UIO_SYSSPACE, &sb);
208 translate_path_major_minor(td, path, &sb);
212 return (newstat_copyout(&sb, args->buf));
216 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
222 if (ldebug(newfstat))
223 printf(ARGS(newfstat, "%d, *"), args->fd);
226 error = kern_fstat(td, args->fd, &buf);
227 translate_fd_major_minor(td, args->fd, &buf);
229 error = newstat_copyout(&buf, args->buf);
235 stat_copyout(struct stat *buf, void *ubuf)
239 bzero(&lbuf, sizeof(lbuf));
240 lbuf.st_dev = buf->st_dev;
241 lbuf.st_ino = buf->st_ino;
242 lbuf.st_mode = buf->st_mode;
243 lbuf.st_nlink = buf->st_nlink;
244 lbuf.st_uid = buf->st_uid;
245 lbuf.st_gid = buf->st_gid;
246 lbuf.st_rdev = buf->st_rdev;
247 if (buf->st_size < (quad_t)1 << 32)
248 lbuf.st_size = buf->st_size;
251 lbuf.st_atime = buf->st_atime;
252 lbuf.st_mtime = buf->st_mtime;
253 lbuf.st_ctime = buf->st_ctime;
254 lbuf.st_blksize = buf->st_blksize;
255 lbuf.st_blocks = buf->st_blocks;
256 lbuf.st_flags = buf->st_flags;
257 lbuf.st_gen = buf->st_gen;
259 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
263 linux_stat(struct thread *td, struct linux_stat_args *args)
269 LCONVPATHEXIST(td, args->path, &path);
273 printf(ARGS(stat, "%s, *"), path);
275 error = kern_stat(td, path, UIO_SYSSPACE, &buf);
280 translate_path_major_minor(td, path, &buf);
282 return(stat_copyout(&buf, args->up));
286 linux_lstat(struct thread *td, struct linux_lstat_args *args)
292 LCONVPATHEXIST(td, args->path, &path);
296 printf(ARGS(lstat, "%s, *"), path);
298 error = kern_lstat(td, path, UIO_SYSSPACE, &buf);
303 translate_path_major_minor(td, path, &buf);
305 return(stat_copyout(&buf, args->up));
308 /* XXX - All fields of type l_int are defined as l_long on i386 */
322 #define LINUX_CODA_SUPER_MAGIC 0x73757245L
323 #define LINUX_EXT2_SUPER_MAGIC 0xEF53L
324 #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L
325 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
326 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
327 #define LINUX_NCP_SUPER_MAGIC 0x564cL
328 #define LINUX_NFS_SUPER_MAGIC 0x6969L
329 #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL
330 #define LINUX_PROC_SUPER_MAGIC 0x9fa0L
331 #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */
332 #define LINUX_DEVFS_SUPER_MAGIC 0x1373L
335 bsd_to_linux_ftype(const char *fstypename)
338 static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
339 {"ufs", LINUX_UFS_SUPER_MAGIC},
340 {"cd9660", LINUX_ISOFS_SUPER_MAGIC},
341 {"nfs", LINUX_NFS_SUPER_MAGIC},
342 {"ext2fs", LINUX_EXT2_SUPER_MAGIC},
343 {"procfs", LINUX_PROC_SUPER_MAGIC},
344 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
345 {"ntfs", LINUX_NTFS_SUPER_MAGIC},
346 {"nwfs", LINUX_NCP_SUPER_MAGIC},
347 {"hpfs", LINUX_HPFS_SUPER_MAGIC},
348 {"coda", LINUX_CODA_SUPER_MAGIC},
349 {"devfs", LINUX_DEVFS_SUPER_MAGIC},
352 for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
353 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
354 return (b2l_tbl[i].linux_type);
360 bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs)
363 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
364 linux_statfs->f_bsize = bsd_statfs->f_bsize;
365 linux_statfs->f_blocks = bsd_statfs->f_blocks;
366 linux_statfs->f_bfree = bsd_statfs->f_bfree;
367 linux_statfs->f_bavail = bsd_statfs->f_bavail;
368 linux_statfs->f_ffree = bsd_statfs->f_ffree;
369 linux_statfs->f_files = bsd_statfs->f_files;
370 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
371 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
372 linux_statfs->f_namelen = MAXNAMLEN;
376 linux_statfs(struct thread *td, struct linux_statfs_args *args)
378 struct l_statfs linux_statfs;
379 struct statfs bsd_statfs;
383 LCONVPATHEXIST(td, args->path, &path);
387 printf(ARGS(statfs, "%s, *"), path);
389 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
393 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
394 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
398 bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs)
401 linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
402 linux_statfs->f_bsize = bsd_statfs->f_bsize;
403 linux_statfs->f_blocks = bsd_statfs->f_blocks;
404 linux_statfs->f_bfree = bsd_statfs->f_bfree;
405 linux_statfs->f_bavail = bsd_statfs->f_bavail;
406 linux_statfs->f_ffree = bsd_statfs->f_ffree;
407 linux_statfs->f_files = bsd_statfs->f_files;
408 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
409 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
410 linux_statfs->f_namelen = MAXNAMLEN;
414 linux_statfs64(struct thread *td, struct linux_statfs64_args *args)
416 struct l_statfs64 linux_statfs;
417 struct statfs bsd_statfs;
421 if (args->bufsize != sizeof(struct l_statfs64))
424 LCONVPATHEXIST(td, args->path, &path);
427 if (ldebug(statfs64))
428 printf(ARGS(statfs64, "%s, *"), path);
430 error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
434 bsd_to_linux_statfs64(&bsd_statfs, &linux_statfs);
435 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
439 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
441 struct l_statfs linux_statfs;
442 struct statfs bsd_statfs;
447 printf(ARGS(fstatfs, "%d, *"), args->fd);
449 error = kern_fstatfs(td, args->fd, &bsd_statfs);
452 bsd_to_linux_statfs(&bsd_statfs, &linux_statfs);
453 return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
465 linux_ustat(struct thread *td, struct linux_ustat_args *args)
469 printf(ARGS(ustat, "%d, *"), args->dev);
475 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
478 stat64_copyout(struct stat *buf, void *ubuf)
480 struct l_stat64 lbuf;
482 bzero(&lbuf, sizeof(lbuf));
483 lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
484 lbuf.st_ino = buf->st_ino;
485 lbuf.st_mode = buf->st_mode;
486 lbuf.st_nlink = buf->st_nlink;
487 lbuf.st_uid = buf->st_uid;
488 lbuf.st_gid = buf->st_gid;
489 lbuf.st_rdev = buf->st_rdev;
490 lbuf.st_size = buf->st_size;
491 lbuf.st_atime = buf->st_atime;
492 lbuf.st_mtime = buf->st_mtime;
493 lbuf.st_ctime = buf->st_ctime;
494 lbuf.st_blksize = buf->st_blksize;
495 lbuf.st_blocks = buf->st_blocks;
498 * The __st_ino field makes all the difference. In the Linux kernel
499 * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
500 * but without the assignment to __st_ino the runtime linker refuses
501 * to mmap(2) any shared libraries. I guess it's broken alright :-)
503 lbuf.__st_ino = buf->st_ino;
505 return (copyout(&lbuf, ubuf, sizeof(lbuf)));
509 linux_stat64(struct thread *td, struct linux_stat64_args *args)
515 LCONVPATHEXIST(td, args->filename, &filename);
519 printf(ARGS(stat64, "%s, *"), filename);
522 error = kern_stat(td, filename, UIO_SYSSPACE, &buf);
524 if (strlen(filename) > strlen("/dev/pts/") &&
525 !strncmp(filename, "/dev/pts/", strlen("/dev/pts/")) &&
526 filename[9] >= '0' && filename[9] <= '9') {
528 * Linux checks major and minors of the slave device
529 * to make sure it's a pty deivce, so let's make him
532 buf.st_rdev = (136 << 8);
534 translate_path_major_minor(td, filename, &buf);
539 return (stat64_copyout(&buf, args->statbuf));
543 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
549 LCONVPATHEXIST(td, args->filename, &filename);
553 printf(ARGS(lstat64, "%s, *"), args->filename);
556 error = kern_lstat(td, filename, UIO_SYSSPACE, &sb);
558 translate_path_major_minor(td, filename, &sb);
562 return (stat64_copyout(&sb, args->statbuf));
566 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
573 printf(ARGS(fstat64, "%d, *"), args->fd);
576 error = kern_fstat(td, args->fd, &buf);
577 translate_fd_major_minor(td, args->fd, &buf);
579 error = stat64_copyout(&buf, args->statbuf);
584 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */