1c21dee17SSøren Schmidt /*- 2c21dee17SSøren Schmidt * Copyright (c) 1994-1995 S�ren Schmidt 3c21dee17SSøren Schmidt * All rights reserved. 4c21dee17SSøren Schmidt * 5c21dee17SSøren Schmidt * Redistribution and use in source and binary forms, with or without 6c21dee17SSøren Schmidt * modification, are permitted provided that the following conditions 7c21dee17SSøren Schmidt * are met: 8c21dee17SSøren Schmidt * 1. Redistributions of source code must retain the above copyright 9c21dee17SSøren Schmidt * notice, this list of conditions and the following disclaimer 10c21dee17SSøren Schmidt * in this position and unchanged. 11c21dee17SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 12c21dee17SSøren Schmidt * notice, this list of conditions and the following disclaimer in the 13c21dee17SSøren Schmidt * documentation and/or other materials provided with the distribution. 14c21dee17SSøren Schmidt * 3. The name of the author may not be used to endorse or promote products 1521dc7d4fSJens Schweikhardt * derived from this software without specific prior written permission 16c21dee17SSøren Schmidt * 17c21dee17SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18c21dee17SSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19c21dee17SSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20c21dee17SSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21c21dee17SSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22c21dee17SSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23c21dee17SSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24c21dee17SSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25c21dee17SSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26c21dee17SSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27c21dee17SSøren Schmidt */ 28c21dee17SSøren Schmidt 2916dbc7f2SDavid E. O'Brien #include <sys/cdefs.h> 3016dbc7f2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3116dbc7f2SDavid E. O'Brien 32eddc160eSRobert Watson #include "opt_mac.h" 33eddc160eSRobert Watson 34c21dee17SSøren Schmidt #include <sys/param.h> 35408da119SMarcel Moolenaar #include <sys/conf.h> 36c21dee17SSøren Schmidt #include <sys/dirent.h> 37c21dee17SSøren Schmidt #include <sys/file.h> 38c21dee17SSøren Schmidt #include <sys/filedesc.h> 39c21dee17SSøren Schmidt #include <sys/proc.h> 40eddc160eSRobert Watson #include <sys/mac.h> 4185422e62SBruce Evans #include <sys/malloc.h> 42c21dee17SSøren Schmidt #include <sys/mount.h> 43c21dee17SSøren Schmidt #include <sys/namei.h> 44c21dee17SSøren Schmidt #include <sys/stat.h> 45f7a25872SJohn Baldwin #include <sys/syscallsubr.h> 46408da119SMarcel Moolenaar #include <sys/systm.h> 47c21dee17SSøren Schmidt #include <sys/vnode.h> 48c21dee17SSøren Schmidt 494af27623STim J. Robbins #include "opt_compat.h" 504af27623STim J. Robbins 511997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32 524af27623STim J. Robbins #include <machine/../linux32/linux.h> 534af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h> 541997c537SDavid E. O'Brien #else 551997c537SDavid E. O'Brien #include <machine/../linux/linux.h> 561997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h> 574af27623STim J. Robbins #endif 5885422e62SBruce Evans 59ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h> 60762e6b85SEivind Eklund 61c21dee17SSøren Schmidt static int 62c21dee17SSøren Schmidt newstat_copyout(struct stat *buf, void *ubuf) 63c21dee17SSøren Schmidt { 645002a60fSMarcel Moolenaar struct l_newstat tbuf; 654c3a3ec0SJosef Karthauser struct cdevsw *cdevsw; 6689c9c53dSPoul-Henning Kamp struct cdev *dev; 67c21dee17SSøren Schmidt 68a966b13dSMartin Blapp bzero(&tbuf, sizeof(tbuf)); 695002a60fSMarcel Moolenaar tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 705002a60fSMarcel Moolenaar tbuf.st_ino = buf->st_ino; 715002a60fSMarcel Moolenaar tbuf.st_mode = buf->st_mode; 725002a60fSMarcel Moolenaar tbuf.st_nlink = buf->st_nlink; 735002a60fSMarcel Moolenaar tbuf.st_uid = buf->st_uid; 745002a60fSMarcel Moolenaar tbuf.st_gid = buf->st_gid; 755002a60fSMarcel Moolenaar tbuf.st_rdev = buf->st_rdev; 765002a60fSMarcel Moolenaar tbuf.st_size = buf->st_size; 775002a60fSMarcel Moolenaar tbuf.st_atime = buf->st_atime; 785002a60fSMarcel Moolenaar tbuf.st_mtime = buf->st_mtime; 795002a60fSMarcel Moolenaar tbuf.st_ctime = buf->st_ctime; 805002a60fSMarcel Moolenaar tbuf.st_blksize = buf->st_blksize; 815002a60fSMarcel Moolenaar tbuf.st_blocks = buf->st_blocks; 827842f151SPaul Richards 834c3a3ec0SJosef Karthauser /* Lie about disk drives which are character devices 844c3a3ec0SJosef Karthauser * in FreeBSD but block devices under Linux. 854c3a3ec0SJosef Karthauser */ 865002a60fSMarcel Moolenaar if (S_ISCHR(tbuf.st_mode) && 87f3732fd1SPoul-Henning Kamp (dev = findcdev(buf->st_rdev)) != NULL) { 88f69f5fbdSPoul-Henning Kamp cdevsw = dev_refthread(dev); 89f69f5fbdSPoul-Henning Kamp if (cdevsw != NULL) { 90f69f5fbdSPoul-Henning Kamp if (cdevsw->d_flags & D_DISK) { 915002a60fSMarcel Moolenaar tbuf.st_mode &= ~S_IFMT; 925002a60fSMarcel Moolenaar tbuf.st_mode |= S_IFBLK; 937842f151SPaul Richards 947842f151SPaul Richards /* XXX this may not be quite right */ 957842f151SPaul Richards /* Map major number to 0 */ 965002a60fSMarcel Moolenaar tbuf.st_dev = uminor(buf->st_dev) & 0xf; 975002a60fSMarcel Moolenaar tbuf.st_rdev = buf->st_rdev & 0xff; 987842f151SPaul Richards } 99f69f5fbdSPoul-Henning Kamp dev_relthread(dev); 100f69f5fbdSPoul-Henning Kamp } 1014c3a3ec0SJosef Karthauser } 1024e0eaf69SMarcel Moolenaar 1034e0eaf69SMarcel Moolenaar return (copyout(&tbuf, ubuf, sizeof(tbuf))); 104c21dee17SSøren Schmidt } 105c21dee17SSøren Schmidt 106c21dee17SSøren Schmidt int 107b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args) 108c21dee17SSøren Schmidt { 109c21dee17SSøren Schmidt struct stat buf; 110206a5d3aSIan Dowse char *path; 111c21dee17SSøren Schmidt int error; 112d66a5066SPeter Wemm 113206a5d3aSIan Dowse LCONVPATHEXIST(td, args->path, &path); 114c21dee17SSøren Schmidt 115c21dee17SSøren Schmidt #ifdef DEBUG 11624593369SJonathan Lemon if (ldebug(newstat)) 117206a5d3aSIan Dowse printf(ARGS(newstat, "%s, *"), path); 118c21dee17SSøren Schmidt #endif 1194e0eaf69SMarcel Moolenaar 120f7a25872SJohn Baldwin error = kern_stat(td, path, UIO_SYSSPACE, &buf); 121206a5d3aSIan Dowse LFREEPATH(path); 1224e0eaf69SMarcel Moolenaar if (error) 1234e0eaf69SMarcel Moolenaar return (error); 1244e0eaf69SMarcel Moolenaar return (newstat_copyout(&buf, args->buf)); 125c21dee17SSøren Schmidt } 126c21dee17SSøren Schmidt 127c21dee17SSøren Schmidt int 128b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 129c21dee17SSøren Schmidt { 1304e0eaf69SMarcel Moolenaar struct stat sb; 131206a5d3aSIan Dowse char *path; 132f7a25872SJohn Baldwin int error; 133d66a5066SPeter Wemm 134206a5d3aSIan Dowse LCONVPATHEXIST(td, args->path, &path); 135c21dee17SSøren Schmidt 136c21dee17SSøren Schmidt #ifdef DEBUG 13724593369SJonathan Lemon if (ldebug(newlstat)) 138206a5d3aSIan Dowse printf(ARGS(newlstat, "%s, *"), path); 139c21dee17SSøren Schmidt #endif 1404e0eaf69SMarcel Moolenaar 141f7a25872SJohn Baldwin error = kern_lstat(td, path, UIO_SYSSPACE, &sb); 142206a5d3aSIan Dowse LFREEPATH(path); 143d66a5066SPeter Wemm if (error) 144d66a5066SPeter Wemm return (error); 1455002a60fSMarcel Moolenaar return (newstat_copyout(&sb, args->buf)); 146d66a5066SPeter Wemm } 147c21dee17SSøren Schmidt 148c21dee17SSøren Schmidt int 149b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 150c21dee17SSøren Schmidt { 151c21dee17SSøren Schmidt struct stat buf; 152c21dee17SSøren Schmidt int error; 153c21dee17SSøren Schmidt 154c21dee17SSøren Schmidt #ifdef DEBUG 15524593369SJonathan Lemon if (ldebug(newfstat)) 15624593369SJonathan Lemon printf(ARGS(newfstat, "%d, *"), args->fd); 157c21dee17SSøren Schmidt #endif 1584e0eaf69SMarcel Moolenaar 159f7a25872SJohn Baldwin error = kern_fstat(td, args->fd, &buf); 160c21dee17SSøren Schmidt if (!error) 161c21dee17SSøren Schmidt error = newstat_copyout(&buf, args->buf); 1624e0eaf69SMarcel Moolenaar 1634e0eaf69SMarcel Moolenaar return (error); 164c21dee17SSøren Schmidt } 165c21dee17SSøren Schmidt 1665002a60fSMarcel Moolenaar /* XXX - All fields of type l_int are defined as l_long on i386 */ 1675002a60fSMarcel Moolenaar struct l_statfs { 1685002a60fSMarcel Moolenaar l_int f_type; 1695002a60fSMarcel Moolenaar l_int f_bsize; 1705002a60fSMarcel Moolenaar l_int f_blocks; 1715002a60fSMarcel Moolenaar l_int f_bfree; 1725002a60fSMarcel Moolenaar l_int f_bavail; 1735002a60fSMarcel Moolenaar l_int f_files; 1745002a60fSMarcel Moolenaar l_int f_ffree; 1755002a60fSMarcel Moolenaar l_fsid_t f_fsid; 1765002a60fSMarcel Moolenaar l_int f_namelen; 1775002a60fSMarcel Moolenaar l_int f_spare[6]; 178c21dee17SSøren Schmidt }; 179c21dee17SSøren Schmidt 180dca60efcSMarcel Moolenaar #define LINUX_CODA_SUPER_MAGIC 0x73757245L 181dca60efcSMarcel Moolenaar #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 182dca60efcSMarcel Moolenaar #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 183dca60efcSMarcel Moolenaar #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 184dca60efcSMarcel Moolenaar #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 185dca60efcSMarcel Moolenaar #define LINUX_NCP_SUPER_MAGIC 0x564cL 186dca60efcSMarcel Moolenaar #define LINUX_NFS_SUPER_MAGIC 0x6969L 187dca60efcSMarcel Moolenaar #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 188dca60efcSMarcel Moolenaar #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 189dca60efcSMarcel Moolenaar #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 190dca60efcSMarcel Moolenaar 191dca60efcSMarcel Moolenaar static long 192962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename) 193dca60efcSMarcel Moolenaar { 194962cf420SMaxim Sobolev int i; 195962cf420SMaxim Sobolev static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 196962cf420SMaxim Sobolev {"ufs", LINUX_UFS_SUPER_MAGIC}, 197962cf420SMaxim Sobolev {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 198962cf420SMaxim Sobolev {"nfs", LINUX_NFS_SUPER_MAGIC}, 199962cf420SMaxim Sobolev {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 200962cf420SMaxim Sobolev {"procfs", LINUX_PROC_SUPER_MAGIC}, 201962cf420SMaxim Sobolev {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 202962cf420SMaxim Sobolev {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 203962cf420SMaxim Sobolev {"nwfs", LINUX_NCP_SUPER_MAGIC}, 204962cf420SMaxim Sobolev {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 205962cf420SMaxim Sobolev {"coda", LINUX_CODA_SUPER_MAGIC}, 206962cf420SMaxim Sobolev {NULL, 0L}}; 207dca60efcSMarcel Moolenaar 208962cf420SMaxim Sobolev for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 209962cf420SMaxim Sobolev if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 210962cf420SMaxim Sobolev return (b2l_tbl[i].linux_type); 211dca60efcSMarcel Moolenaar 212dca60efcSMarcel Moolenaar return (0L); 213dca60efcSMarcel Moolenaar } 214dca60efcSMarcel Moolenaar 215f7a25872SJohn Baldwin static void 216f7a25872SJohn Baldwin bsd_to_linux_statfs(struct thread *td, struct statfs *bsd_statfs, 217f7a25872SJohn Baldwin struct l_statfs *linux_statfs) 218f7a25872SJohn Baldwin { 219f7a25872SJohn Baldwin 220f7a25872SJohn Baldwin linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 221f7a25872SJohn Baldwin linux_statfs->f_bsize = bsd_statfs->f_bsize; 222f7a25872SJohn Baldwin linux_statfs->f_blocks = bsd_statfs->f_blocks; 223f7a25872SJohn Baldwin linux_statfs->f_bfree = bsd_statfs->f_bfree; 224f7a25872SJohn Baldwin linux_statfs->f_bavail = bsd_statfs->f_bavail; 225f7a25872SJohn Baldwin linux_statfs->f_ffree = bsd_statfs->f_ffree; 226f7a25872SJohn Baldwin linux_statfs->f_files = bsd_statfs->f_files; 227f7a25872SJohn Baldwin if (suser(td)) { 228f7a25872SJohn Baldwin linux_statfs->f_fsid.val[0] = 0; 229f7a25872SJohn Baldwin linux_statfs->f_fsid.val[1] = 0; 230f7a25872SJohn Baldwin } else { 231f7a25872SJohn Baldwin linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 232f7a25872SJohn Baldwin linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 233f7a25872SJohn Baldwin } 234f7a25872SJohn Baldwin linux_statfs->f_namelen = MAXNAMLEN; 235f7a25872SJohn Baldwin } 236f7a25872SJohn Baldwin 237c21dee17SSøren Schmidt int 238b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args) 239c21dee17SSøren Schmidt { 2405002a60fSMarcel Moolenaar struct l_statfs linux_statfs; 241f7a25872SJohn Baldwin struct statfs bsd_statfs; 242206a5d3aSIan Dowse char *path; 243c21dee17SSøren Schmidt int error; 244d66a5066SPeter Wemm 245206a5d3aSIan Dowse LCONVPATHEXIST(td, args->path, &path); 246c21dee17SSøren Schmidt 247c21dee17SSøren Schmidt #ifdef DEBUG 24824593369SJonathan Lemon if (ldebug(statfs)) 249206a5d3aSIan Dowse printf(ARGS(statfs, "%s, *"), path); 250c21dee17SSøren Schmidt #endif 251f7a25872SJohn Baldwin error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs); 252206a5d3aSIan Dowse LFREEPATH(path); 253d5558c00SPeter Wemm if (error) 254eddc160eSRobert Watson return (error); 255f7a25872SJohn Baldwin bsd_to_linux_statfs(td, &bsd_statfs, &linux_statfs); 2564b7ef73dSDag-Erling Smørgrav return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 257c21dee17SSøren Schmidt } 258c21dee17SSøren Schmidt 259c21dee17SSøren Schmidt int 260b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 261c21dee17SSøren Schmidt { 2625002a60fSMarcel Moolenaar struct l_statfs linux_statfs; 263f7a25872SJohn Baldwin struct statfs bsd_statfs; 264c21dee17SSøren Schmidt int error; 265c21dee17SSøren Schmidt 266c21dee17SSøren Schmidt #ifdef DEBUG 26724593369SJonathan Lemon if (ldebug(fstatfs)) 26824593369SJonathan Lemon printf(ARGS(fstatfs, "%d, *"), args->fd); 269c21dee17SSøren Schmidt #endif 270f7a25872SJohn Baldwin error = kern_fstatfs(td, args->fd, &bsd_statfs); 271d5558c00SPeter Wemm if (error) 272c21dee17SSøren Schmidt return error; 273f7a25872SJohn Baldwin bsd_to_linux_statfs(td, &bsd_statfs, &linux_statfs); 274f7a25872SJohn Baldwin return copyout(&linux_statfs, args->buf, sizeof(linux_statfs)); 275c21dee17SSøren Schmidt } 276408da119SMarcel Moolenaar 2775002a60fSMarcel Moolenaar struct l_ustat 278408da119SMarcel Moolenaar { 2795002a60fSMarcel Moolenaar l_daddr_t f_tfree; 2805002a60fSMarcel Moolenaar l_ino_t f_tinode; 2815002a60fSMarcel Moolenaar char f_fname[6]; 2825002a60fSMarcel Moolenaar char f_fpack[6]; 2835002a60fSMarcel Moolenaar }; 2845002a60fSMarcel Moolenaar 2855002a60fSMarcel Moolenaar int 286b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args) 2875002a60fSMarcel Moolenaar { 2885002a60fSMarcel Moolenaar struct l_ustat lu; 28989c9c53dSPoul-Henning Kamp struct cdev *dev; 290408da119SMarcel Moolenaar struct vnode *vp; 291408da119SMarcel Moolenaar struct statfs *stat; 292408da119SMarcel Moolenaar int error; 293408da119SMarcel Moolenaar 294408da119SMarcel Moolenaar #ifdef DEBUG 29524593369SJonathan Lemon if (ldebug(ustat)) 2965002a60fSMarcel Moolenaar printf(ARGS(ustat, "%d, *"), args->dev); 297408da119SMarcel Moolenaar #endif 298408da119SMarcel Moolenaar 299408da119SMarcel Moolenaar /* 300408da119SMarcel Moolenaar * lu.f_fname and lu.f_fpack are not used. They are always zeroed. 301408da119SMarcel Moolenaar * lu.f_tinode and lu.f_tfree are set from the device's super block. 302408da119SMarcel Moolenaar */ 303408da119SMarcel Moolenaar bzero(&lu, sizeof(lu)); 304408da119SMarcel Moolenaar 305408da119SMarcel Moolenaar /* 306408da119SMarcel Moolenaar * XXX - Don't return an error if we can't find a vnode for the 30789c9c53dSPoul-Henning Kamp * device. Our struct cdev *is 32-bits whereas Linux only has a 16-bits 30889c9c53dSPoul-Henning Kamp * struct cdev *. The struct cdev *that is used now may as well be a truncated 30989c9c53dSPoul-Henning Kamp * struct cdev *returned from previous syscalls. Just return a bzeroed 310408da119SMarcel Moolenaar * ustat in that case. 31141befa53SPoul-Henning Kamp * 31241befa53SPoul-Henning Kamp * XXX: findcdev() SHALL not be used this way. Somebody (TM) will 31341befa53SPoul-Henning Kamp * have to find a better way. It may be that we should stick 31441befa53SPoul-Henning Kamp * a dev_t into struct mount, and walk the mountlist for a 31541befa53SPoul-Henning Kamp * perfect match and failing that try again looking for a 31641befa53SPoul-Henning Kamp * minor-truncated match. 317408da119SMarcel Moolenaar */ 318f3732fd1SPoul-Henning Kamp dev = findcdev(makedev(args->dev >> 8, args->dev & 0xFF)); 319f3732fd1SPoul-Henning Kamp if (dev != NULL && vfinddev(dev, &vp)) { 320408da119SMarcel Moolenaar if (vp->v_mount == NULL) 321408da119SMarcel Moolenaar return (EINVAL); 322eddc160eSRobert Watson #ifdef MAC 32331566c96SJohn Baldwin error = mac_check_mount_stat(td->td_ucred, vp->v_mount); 324eddc160eSRobert Watson if (error) 325eddc160eSRobert Watson return (error); 326eddc160eSRobert Watson #endif 327408da119SMarcel Moolenaar stat = &(vp->v_mount->mnt_stat); 328b40ce416SJulian Elischer error = VFS_STATFS(vp->v_mount, stat, td); 329408da119SMarcel Moolenaar if (error) 330408da119SMarcel Moolenaar return (error); 331408da119SMarcel Moolenaar 332408da119SMarcel Moolenaar lu.f_tfree = stat->f_bfree; 333408da119SMarcel Moolenaar lu.f_tinode = stat->f_ffree; 334408da119SMarcel Moolenaar } 335408da119SMarcel Moolenaar 3365002a60fSMarcel Moolenaar return (copyout(&lu, args->ubuf, sizeof(lu))); 337408da119SMarcel Moolenaar } 3385002a60fSMarcel Moolenaar 3391997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 3405002a60fSMarcel Moolenaar 3415002a60fSMarcel Moolenaar static int 3425002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf) 3435002a60fSMarcel Moolenaar { 3445002a60fSMarcel Moolenaar struct l_stat64 lbuf; 345616aa29aSMartin Blapp struct cdevsw *cdevsw; 34689c9c53dSPoul-Henning Kamp struct cdev *dev; 3475002a60fSMarcel Moolenaar 3485002a60fSMarcel Moolenaar bzero(&lbuf, sizeof(lbuf)); 3495002a60fSMarcel Moolenaar lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8); 3505002a60fSMarcel Moolenaar lbuf.st_ino = buf->st_ino; 3515002a60fSMarcel Moolenaar lbuf.st_mode = buf->st_mode; 3525002a60fSMarcel Moolenaar lbuf.st_nlink = buf->st_nlink; 3535002a60fSMarcel Moolenaar lbuf.st_uid = buf->st_uid; 3545002a60fSMarcel Moolenaar lbuf.st_gid = buf->st_gid; 3555002a60fSMarcel Moolenaar lbuf.st_rdev = buf->st_rdev; 3565002a60fSMarcel Moolenaar lbuf.st_size = buf->st_size; 3575002a60fSMarcel Moolenaar lbuf.st_atime = buf->st_atime; 3585002a60fSMarcel Moolenaar lbuf.st_mtime = buf->st_mtime; 3595002a60fSMarcel Moolenaar lbuf.st_ctime = buf->st_ctime; 3605002a60fSMarcel Moolenaar lbuf.st_blksize = buf->st_blksize; 3615002a60fSMarcel Moolenaar lbuf.st_blocks = buf->st_blocks; 3625002a60fSMarcel Moolenaar 363616aa29aSMartin Blapp /* Lie about disk drives which are character devices 364616aa29aSMartin Blapp * in FreeBSD but block devices under Linux. 365616aa29aSMartin Blapp */ 366616aa29aSMartin Blapp if (S_ISCHR(lbuf.st_mode) && 367f3732fd1SPoul-Henning Kamp (dev = findcdev(buf->st_rdev)) != NULL) { 368f69f5fbdSPoul-Henning Kamp cdevsw = dev_refthread(dev); 369f69f5fbdSPoul-Henning Kamp if (cdevsw != NULL) { 370f69f5fbdSPoul-Henning Kamp if (cdevsw->d_flags & D_DISK) { 371616aa29aSMartin Blapp lbuf.st_mode &= ~S_IFMT; 372616aa29aSMartin Blapp lbuf.st_mode |= S_IFBLK; 373616aa29aSMartin Blapp 374616aa29aSMartin Blapp /* XXX this may not be quite right */ 375616aa29aSMartin Blapp /* Map major number to 0 */ 376616aa29aSMartin Blapp lbuf.st_dev = uminor(buf->st_dev) & 0xf; 377616aa29aSMartin Blapp lbuf.st_rdev = buf->st_rdev & 0xff; 378616aa29aSMartin Blapp } 379f69f5fbdSPoul-Henning Kamp dev_relthread(dev); 380f69f5fbdSPoul-Henning Kamp } 381616aa29aSMartin Blapp } 382616aa29aSMartin Blapp 3835002a60fSMarcel Moolenaar /* 3845002a60fSMarcel Moolenaar * The __st_ino field makes all the difference. In the Linux kernel 3855002a60fSMarcel Moolenaar * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 3865002a60fSMarcel Moolenaar * but without the assignment to __st_ino the runtime linker refuses 3875002a60fSMarcel Moolenaar * to mmap(2) any shared libraries. I guess it's broken alright :-) 3885002a60fSMarcel Moolenaar */ 3895002a60fSMarcel Moolenaar lbuf.__st_ino = buf->st_ino; 3905002a60fSMarcel Moolenaar 3915002a60fSMarcel Moolenaar return (copyout(&lbuf, ubuf, sizeof(lbuf))); 3925002a60fSMarcel Moolenaar } 3935002a60fSMarcel Moolenaar 3945002a60fSMarcel Moolenaar int 395b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args) 3965002a60fSMarcel Moolenaar { 3975002a60fSMarcel Moolenaar struct stat buf; 398206a5d3aSIan Dowse char *filename; 399f7a25872SJohn Baldwin int error; 4005002a60fSMarcel Moolenaar 401206a5d3aSIan Dowse LCONVPATHEXIST(td, args->filename, &filename); 4025002a60fSMarcel Moolenaar 4035002a60fSMarcel Moolenaar #ifdef DEBUG 4045002a60fSMarcel Moolenaar if (ldebug(stat64)) 405206a5d3aSIan Dowse printf(ARGS(stat64, "%s, *"), filename); 4065002a60fSMarcel Moolenaar #endif 4075002a60fSMarcel Moolenaar 408f7a25872SJohn Baldwin error = kern_stat(td, filename, UIO_SYSSPACE, &buf); 409206a5d3aSIan Dowse LFREEPATH(filename); 4105002a60fSMarcel Moolenaar if (error) 4115002a60fSMarcel Moolenaar return (error); 4125002a60fSMarcel Moolenaar return (stat64_copyout(&buf, args->statbuf)); 4135002a60fSMarcel Moolenaar } 4145002a60fSMarcel Moolenaar 4155002a60fSMarcel Moolenaar int 416b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 4175002a60fSMarcel Moolenaar { 4185002a60fSMarcel Moolenaar struct stat sb; 419206a5d3aSIan Dowse char *filename; 420f7a25872SJohn Baldwin int error; 4215002a60fSMarcel Moolenaar 422206a5d3aSIan Dowse LCONVPATHEXIST(td, args->filename, &filename); 4235002a60fSMarcel Moolenaar 4245002a60fSMarcel Moolenaar #ifdef DEBUG 4255002a60fSMarcel Moolenaar if (ldebug(lstat64)) 4265002a60fSMarcel Moolenaar printf(ARGS(lstat64, "%s, *"), args->filename); 4275002a60fSMarcel Moolenaar #endif 4285002a60fSMarcel Moolenaar 429f7a25872SJohn Baldwin error = kern_lstat(td, filename, UIO_SYSSPACE, &sb); 430206a5d3aSIan Dowse LFREEPATH(filename); 4315002a60fSMarcel Moolenaar if (error) 4325002a60fSMarcel Moolenaar return (error); 4335002a60fSMarcel Moolenaar return (stat64_copyout(&sb, args->statbuf)); 4345002a60fSMarcel Moolenaar } 4355002a60fSMarcel Moolenaar 4365002a60fSMarcel Moolenaar int 437b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 4385002a60fSMarcel Moolenaar { 4395002a60fSMarcel Moolenaar struct stat buf; 4405002a60fSMarcel Moolenaar int error; 4415002a60fSMarcel Moolenaar 4425002a60fSMarcel Moolenaar #ifdef DEBUG 4435002a60fSMarcel Moolenaar if (ldebug(fstat64)) 4445002a60fSMarcel Moolenaar printf(ARGS(fstat64, "%d, *"), args->fd); 4455002a60fSMarcel Moolenaar #endif 4465002a60fSMarcel Moolenaar 447f7a25872SJohn Baldwin error = kern_fstat(td, args->fd, &buf); 4485002a60fSMarcel Moolenaar if (!error) 4495002a60fSMarcel Moolenaar error = stat64_copyout(&buf, args->statbuf); 4505002a60fSMarcel Moolenaar 4515002a60fSMarcel Moolenaar return (error); 4525002a60fSMarcel Moolenaar } 4535002a60fSMarcel Moolenaar 4541997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 455