1c21dee17SSøren Schmidt /*- 29a14aa01SUlrich Spörlein * 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 32aefce619SRuslan Ermilov #include "opt_compat.h" 33eddc160eSRobert Watson 34c21dee17SSøren Schmidt #include <sys/param.h> 359802eb9eSDmitry Chagin #include <sys/capsicum.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> 4085422e62SBruce Evans #include <sys/malloc.h> 41c21dee17SSøren Schmidt #include <sys/mount.h> 42c21dee17SSøren Schmidt #include <sys/namei.h> 43c21dee17SSøren Schmidt #include <sys/stat.h> 44f7a25872SJohn Baldwin #include <sys/syscallsubr.h> 45408da119SMarcel Moolenaar #include <sys/systm.h> 46bc093719SEd Schouten #include <sys/tty.h> 47c21dee17SSøren Schmidt #include <sys/vnode.h> 48060e4882SDoug Ambrisko #include <sys/conf.h> 49060e4882SDoug Ambrisko #include <sys/fcntl.h> 50c21dee17SSøren Schmidt 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> 6048b05c3fSKonstantin Belousov #include <compat/linux/linux_file.h> 61762e6b85SEivind Eklund 62529844c7SAlexander Leidinger 630eee862aSEd Schouten static void 640eee862aSEd Schouten translate_vnhook_major_minor(struct vnode *vp, struct stat *sb) 650eee862aSEd Schouten { 660eee862aSEd Schouten int major, minor; 670eee862aSEd Schouten 680eee862aSEd Schouten if (vp->v_type == VCHR && vp->v_rdev != NULL && 697870adb6SEd Schouten linux_driver_get_major_minor(devtoname(vp->v_rdev), 700eee862aSEd Schouten &major, &minor) == 0) { 710eee862aSEd Schouten sb->st_rdev = (major << 8 | minor); 720eee862aSEd Schouten } 730eee862aSEd Schouten } 740eee862aSEd Schouten 750eee862aSEd Schouten static int 760eee862aSEd Schouten linux_kern_statat(struct thread *td, int flag, int fd, char *path, 770eee862aSEd Schouten enum uio_seg pathseg, struct stat *sbp) 780eee862aSEd Schouten { 790eee862aSEd Schouten 806e646651SKonstantin Belousov return (kern_statat(td, flag, fd, path, pathseg, sbp, 810eee862aSEd Schouten translate_vnhook_major_minor)); 820eee862aSEd Schouten } 830eee862aSEd Schouten 840eee862aSEd Schouten static int 850eee862aSEd Schouten linux_kern_stat(struct thread *td, char *path, enum uio_seg pathseg, 860eee862aSEd Schouten struct stat *sbp) 870eee862aSEd Schouten { 880eee862aSEd Schouten 890eee862aSEd Schouten return (linux_kern_statat(td, 0, AT_FDCWD, path, pathseg, sbp)); 900eee862aSEd Schouten } 910eee862aSEd Schouten 920eee862aSEd Schouten static int 930eee862aSEd Schouten linux_kern_lstat(struct thread *td, char *path, enum uio_seg pathseg, 940eee862aSEd Schouten struct stat *sbp) 950eee862aSEd Schouten { 960eee862aSEd Schouten 970eee862aSEd Schouten return (linux_kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path, 980eee862aSEd Schouten pathseg, sbp)); 990eee862aSEd Schouten } 1000eee862aSEd Schouten 101bbbc2d96SPoul-Henning Kamp /* 102bbbc2d96SPoul-Henning Kamp * XXX: This was removed from newstat_copyout(), and almost identical 103bbbc2d96SPoul-Henning Kamp * XXX: code was in stat64_copyout(). findcdev() needs to be replaced 104bbbc2d96SPoul-Henning Kamp * XXX: with something that does lookup and locking properly. 105bbbc2d96SPoul-Henning Kamp * XXX: When somebody fixes this: please try to avoid duplicating it. 106bbbc2d96SPoul-Henning Kamp */ 107bbbc2d96SPoul-Henning Kamp #if 0 108bbbc2d96SPoul-Henning Kamp static void 109bbbc2d96SPoul-Henning Kamp disk_foo(struct somestat *tbuf) 110c21dee17SSøren Schmidt { 1114c3a3ec0SJosef Karthauser struct cdevsw *cdevsw; 11289c9c53dSPoul-Henning Kamp struct cdev *dev; 113c21dee17SSøren Schmidt 1144c3a3ec0SJosef Karthauser /* Lie about disk drives which are character devices 1154c3a3ec0SJosef Karthauser * in FreeBSD but block devices under Linux. 1164c3a3ec0SJosef Karthauser */ 1175002a60fSMarcel Moolenaar if (S_ISCHR(tbuf.st_mode) && 118f3732fd1SPoul-Henning Kamp (dev = findcdev(buf->st_rdev)) != NULL) { 119f69f5fbdSPoul-Henning Kamp cdevsw = dev_refthread(dev); 120f69f5fbdSPoul-Henning Kamp if (cdevsw != NULL) { 121f69f5fbdSPoul-Henning Kamp if (cdevsw->d_flags & D_DISK) { 1225002a60fSMarcel Moolenaar tbuf.st_mode &= ~S_IFMT; 1235002a60fSMarcel Moolenaar tbuf.st_mode |= S_IFBLK; 1247842f151SPaul Richards 1257842f151SPaul Richards /* XXX this may not be quite right */ 1267842f151SPaul Richards /* Map major number to 0 */ 127a4611ab6SEd Schouten tbuf.st_dev = minor(buf->st_dev) & 0xf; 1285002a60fSMarcel Moolenaar tbuf.st_rdev = buf->st_rdev & 0xff; 1297842f151SPaul Richards } 130f69f5fbdSPoul-Henning Kamp dev_relthread(dev); 131f69f5fbdSPoul-Henning Kamp } 1324c3a3ec0SJosef Karthauser } 1334e0eaf69SMarcel Moolenaar 134bbbc2d96SPoul-Henning Kamp } 135bbbc2d96SPoul-Henning Kamp #endif 136bbbc2d96SPoul-Henning Kamp 137060e4882SDoug Ambrisko static void 138060e4882SDoug Ambrisko translate_fd_major_minor(struct thread *td, int fd, struct stat *buf) 139060e4882SDoug Ambrisko { 140060e4882SDoug Ambrisko struct file *fp; 1410eee862aSEd Schouten struct vnode *vp; 142f131759fSMateusz Guzik cap_rights_t rights; 143060e4882SDoug Ambrisko int major, minor; 144060e4882SDoug Ambrisko 145a9d2f8d8SRobert Watson /* 146a9d2f8d8SRobert Watson * No capability rights required here. 147a9d2f8d8SRobert Watson */ 148b256a1e1SJung-uk Kim if ((!S_ISCHR(buf->st_mode) && !S_ISBLK(buf->st_mode)) || 149f131759fSMateusz Guzik fget(td, fd, cap_rights_init(&rights), &fp) != 0) 150060e4882SDoug Ambrisko return; 1510eee862aSEd Schouten vp = fp->f_vnode; 1520eee862aSEd Schouten if (vp != NULL && vp->v_rdev != NULL && 1537870adb6SEd Schouten linux_driver_get_major_minor(devtoname(vp->v_rdev), 154bc093719SEd Schouten &major, &minor) == 0) { 155060e4882SDoug Ambrisko buf->st_rdev = (major << 8 | minor); 156bc093719SEd Schouten } else if (fp->f_type == DTYPE_PTS) { 157bc093719SEd Schouten struct tty *tp = fp->f_data; 158bc093719SEd Schouten 159bc093719SEd Schouten /* Convert the numbers for the slave device. */ 1607870adb6SEd Schouten if (linux_driver_get_major_minor(devtoname(tp->t_dev), 161bc093719SEd Schouten &major, &minor) == 0) { 162bc093719SEd Schouten buf->st_rdev = (major << 8 | minor); 163bc093719SEd Schouten } 164bc093719SEd Schouten } 165060e4882SDoug Ambrisko fdrop(fp, td); 166060e4882SDoug Ambrisko } 167060e4882SDoug Ambrisko 168bbbc2d96SPoul-Henning Kamp static int 169bbbc2d96SPoul-Henning Kamp newstat_copyout(struct stat *buf, void *ubuf) 170bbbc2d96SPoul-Henning Kamp { 171bbbc2d96SPoul-Henning Kamp struct l_newstat tbuf; 172bbbc2d96SPoul-Henning Kamp 173bbbc2d96SPoul-Henning Kamp bzero(&tbuf, sizeof(tbuf)); 174a4611ab6SEd Schouten tbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8); 175bbbc2d96SPoul-Henning Kamp tbuf.st_ino = buf->st_ino; 176bbbc2d96SPoul-Henning Kamp tbuf.st_mode = buf->st_mode; 177bbbc2d96SPoul-Henning Kamp tbuf.st_nlink = buf->st_nlink; 178bbbc2d96SPoul-Henning Kamp tbuf.st_uid = buf->st_uid; 179bbbc2d96SPoul-Henning Kamp tbuf.st_gid = buf->st_gid; 180bbbc2d96SPoul-Henning Kamp tbuf.st_rdev = buf->st_rdev; 181bbbc2d96SPoul-Henning Kamp tbuf.st_size = buf->st_size; 182510ea843SEd Schouten tbuf.st_atim.tv_sec = buf->st_atim.tv_sec; 183510ea843SEd Schouten tbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec; 184510ea843SEd Schouten tbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec; 185510ea843SEd Schouten tbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec; 186510ea843SEd Schouten tbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec; 187510ea843SEd Schouten tbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec; 188bbbc2d96SPoul-Henning Kamp tbuf.st_blksize = buf->st_blksize; 189bbbc2d96SPoul-Henning Kamp tbuf.st_blocks = buf->st_blocks; 190bbbc2d96SPoul-Henning Kamp 1914e0eaf69SMarcel Moolenaar return (copyout(&tbuf, ubuf, sizeof(tbuf))); 192c21dee17SSøren Schmidt } 193c21dee17SSøren Schmidt 194c21dee17SSøren Schmidt int 195b40ce416SJulian Elischer linux_newstat(struct thread *td, struct linux_newstat_args *args) 196c21dee17SSøren Schmidt { 197c21dee17SSøren Schmidt struct stat buf; 198206a5d3aSIan Dowse char *path; 199c21dee17SSøren Schmidt int error; 200d66a5066SPeter Wemm 201206a5d3aSIan Dowse LCONVPATHEXIST(td, args->path, &path); 202c21dee17SSøren Schmidt 203c21dee17SSøren Schmidt #ifdef DEBUG 20424593369SJonathan Lemon if (ldebug(newstat)) 205206a5d3aSIan Dowse printf(ARGS(newstat, "%s, *"), path); 206c21dee17SSøren Schmidt #endif 2074e0eaf69SMarcel Moolenaar 2080eee862aSEd Schouten error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf); 209206a5d3aSIan Dowse LFREEPATH(path); 2104e0eaf69SMarcel Moolenaar if (error) 2114e0eaf69SMarcel Moolenaar return (error); 2124e0eaf69SMarcel Moolenaar return (newstat_copyout(&buf, args->buf)); 213c21dee17SSøren Schmidt } 214c21dee17SSøren Schmidt 215c21dee17SSøren Schmidt int 216b40ce416SJulian Elischer linux_newlstat(struct thread *td, struct linux_newlstat_args *args) 217c21dee17SSøren Schmidt { 2184e0eaf69SMarcel Moolenaar struct stat sb; 219206a5d3aSIan Dowse char *path; 220f7a25872SJohn Baldwin int error; 221d66a5066SPeter Wemm 222206a5d3aSIan Dowse LCONVPATHEXIST(td, args->path, &path); 223c21dee17SSøren Schmidt 224c21dee17SSøren Schmidt #ifdef DEBUG 22524593369SJonathan Lemon if (ldebug(newlstat)) 226206a5d3aSIan Dowse printf(ARGS(newlstat, "%s, *"), path); 227c21dee17SSøren Schmidt #endif 2284e0eaf69SMarcel Moolenaar 2290eee862aSEd Schouten error = linux_kern_lstat(td, path, UIO_SYSSPACE, &sb); 230206a5d3aSIan Dowse LFREEPATH(path); 231d66a5066SPeter Wemm if (error) 232d66a5066SPeter Wemm return (error); 2335002a60fSMarcel Moolenaar return (newstat_copyout(&sb, args->buf)); 234d66a5066SPeter Wemm } 235c21dee17SSøren Schmidt 236c21dee17SSøren Schmidt int 237b40ce416SJulian Elischer linux_newfstat(struct thread *td, struct linux_newfstat_args *args) 238c21dee17SSøren Schmidt { 239c21dee17SSøren Schmidt struct stat buf; 240c21dee17SSøren Schmidt int error; 241c21dee17SSøren Schmidt 242c21dee17SSøren Schmidt #ifdef DEBUG 24324593369SJonathan Lemon if (ldebug(newfstat)) 24424593369SJonathan Lemon printf(ARGS(newfstat, "%d, *"), args->fd); 245c21dee17SSøren Schmidt #endif 2464e0eaf69SMarcel Moolenaar 247f7a25872SJohn Baldwin error = kern_fstat(td, args->fd, &buf); 248060e4882SDoug Ambrisko translate_fd_major_minor(td, args->fd, &buf); 249c21dee17SSøren Schmidt if (!error) 250c21dee17SSøren Schmidt error = newstat_copyout(&buf, args->buf); 2514e0eaf69SMarcel Moolenaar 2524e0eaf69SMarcel Moolenaar return (error); 253c21dee17SSøren Schmidt } 254c21dee17SSøren Schmidt 2557f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2565c8919adSAlexander Leidinger static int 2575c8919adSAlexander Leidinger stat_copyout(struct stat *buf, void *ubuf) 2585c8919adSAlexander Leidinger { 2595c8919adSAlexander Leidinger struct l_stat lbuf; 2605c8919adSAlexander Leidinger 2615c8919adSAlexander Leidinger bzero(&lbuf, sizeof(lbuf)); 2625c8919adSAlexander Leidinger lbuf.st_dev = buf->st_dev; 2635c8919adSAlexander Leidinger lbuf.st_ino = buf->st_ino; 2645c8919adSAlexander Leidinger lbuf.st_mode = buf->st_mode; 2655c8919adSAlexander Leidinger lbuf.st_nlink = buf->st_nlink; 2665c8919adSAlexander Leidinger lbuf.st_uid = buf->st_uid; 2675c8919adSAlexander Leidinger lbuf.st_gid = buf->st_gid; 2685c8919adSAlexander Leidinger lbuf.st_rdev = buf->st_rdev; 2695c8919adSAlexander Leidinger if (buf->st_size < (quad_t)1 << 32) 2705c8919adSAlexander Leidinger lbuf.st_size = buf->st_size; 2715c8919adSAlexander Leidinger else 2725c8919adSAlexander Leidinger lbuf.st_size = -2; 273510ea843SEd Schouten lbuf.st_atim.tv_sec = buf->st_atim.tv_sec; 274510ea843SEd Schouten lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec; 275510ea843SEd Schouten lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec; 276510ea843SEd Schouten lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec; 277510ea843SEd Schouten lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec; 278510ea843SEd Schouten lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec; 2795c8919adSAlexander Leidinger lbuf.st_blksize = buf->st_blksize; 2805c8919adSAlexander Leidinger lbuf.st_blocks = buf->st_blocks; 2815c8919adSAlexander Leidinger lbuf.st_flags = buf->st_flags; 2825c8919adSAlexander Leidinger lbuf.st_gen = buf->st_gen; 2835c8919adSAlexander Leidinger 2845c8919adSAlexander Leidinger return (copyout(&lbuf, ubuf, sizeof(lbuf))); 2855c8919adSAlexander Leidinger } 2865c8919adSAlexander Leidinger 2875c8919adSAlexander Leidinger int 2885c8919adSAlexander Leidinger linux_stat(struct thread *td, struct linux_stat_args *args) 2895c8919adSAlexander Leidinger { 2905c8919adSAlexander Leidinger struct stat buf; 29115b78ac5SKonstantin Belousov char *path; 2925c8919adSAlexander Leidinger int error; 29315b78ac5SKonstantin Belousov 29415b78ac5SKonstantin Belousov LCONVPATHEXIST(td, args->path, &path); 29515b78ac5SKonstantin Belousov 2965c8919adSAlexander Leidinger #ifdef DEBUG 2975c8919adSAlexander Leidinger if (ldebug(stat)) 298d075105dSKonstantin Belousov printf(ARGS(stat, "%s, *"), path); 2995c8919adSAlexander Leidinger #endif 3000eee862aSEd Schouten error = linux_kern_stat(td, path, UIO_SYSSPACE, &buf); 301d075105dSKonstantin Belousov if (error) { 30215b78ac5SKonstantin Belousov LFREEPATH(path); 3035c8919adSAlexander Leidinger return (error); 304d075105dSKonstantin Belousov } 305d075105dSKonstantin Belousov LFREEPATH(path); 3065c8919adSAlexander Leidinger return (stat_copyout(&buf, args->up)); 3075c8919adSAlexander Leidinger } 3085c8919adSAlexander Leidinger 3095c8919adSAlexander Leidinger int 3105c8919adSAlexander Leidinger linux_lstat(struct thread *td, struct linux_lstat_args *args) 3115c8919adSAlexander Leidinger { 3125c8919adSAlexander Leidinger struct stat buf; 31315b78ac5SKonstantin Belousov char *path; 3145c8919adSAlexander Leidinger int error; 3155c8919adSAlexander Leidinger 31615b78ac5SKonstantin Belousov LCONVPATHEXIST(td, args->path, &path); 31715b78ac5SKonstantin Belousov 3185c8919adSAlexander Leidinger #ifdef DEBUG 3195c8919adSAlexander Leidinger if (ldebug(lstat)) 320d075105dSKonstantin Belousov printf(ARGS(lstat, "%s, *"), path); 3215c8919adSAlexander Leidinger #endif 3220eee862aSEd Schouten error = linux_kern_lstat(td, path, UIO_SYSSPACE, &buf); 323d075105dSKonstantin Belousov if (error) { 32415b78ac5SKonstantin Belousov LFREEPATH(path); 3255c8919adSAlexander Leidinger return (error); 326d075105dSKonstantin Belousov } 327d075105dSKonstantin Belousov LFREEPATH(path); 3285c8919adSAlexander Leidinger return (stat_copyout(&buf, args->up)); 3295c8919adSAlexander Leidinger } 3307f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 3315c8919adSAlexander Leidinger 3325002a60fSMarcel Moolenaar struct l_statfs { 333297f61ccSDmitry Chagin l_long f_type; 334297f61ccSDmitry Chagin l_long f_bsize; 335297f61ccSDmitry Chagin l_long f_blocks; 336297f61ccSDmitry Chagin l_long f_bfree; 337297f61ccSDmitry Chagin l_long f_bavail; 338297f61ccSDmitry Chagin l_long f_files; 339297f61ccSDmitry Chagin l_long f_ffree; 3405002a60fSMarcel Moolenaar l_fsid_t f_fsid; 341297f61ccSDmitry Chagin l_long f_namelen; 342297f61ccSDmitry Chagin l_long f_spare[6]; 343c21dee17SSøren Schmidt }; 344c21dee17SSøren Schmidt 345dca60efcSMarcel Moolenaar #define LINUX_CODA_SUPER_MAGIC 0x73757245L 346dca60efcSMarcel Moolenaar #define LINUX_EXT2_SUPER_MAGIC 0xEF53L 347dca60efcSMarcel Moolenaar #define LINUX_HPFS_SUPER_MAGIC 0xf995e849L 348dca60efcSMarcel Moolenaar #define LINUX_ISOFS_SUPER_MAGIC 0x9660L 349dca60efcSMarcel Moolenaar #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L 350dca60efcSMarcel Moolenaar #define LINUX_NCP_SUPER_MAGIC 0x564cL 351dca60efcSMarcel Moolenaar #define LINUX_NFS_SUPER_MAGIC 0x6969L 352dca60efcSMarcel Moolenaar #define LINUX_NTFS_SUPER_MAGIC 0x5346544EL 353dca60efcSMarcel Moolenaar #define LINUX_PROC_SUPER_MAGIC 0x9fa0L 354dca60efcSMarcel Moolenaar #define LINUX_UFS_SUPER_MAGIC 0x00011954L /* XXX - UFS_MAGIC in Linux */ 355e83d253bSOlivier Houchard #define LINUX_DEVFS_SUPER_MAGIC 0x1373L 3562166e4e0SDmitry Chagin #define LINUX_SHMFS_MAGIC 0x01021994 357dca60efcSMarcel Moolenaar 358dca60efcSMarcel Moolenaar static long 359962cf420SMaxim Sobolev bsd_to_linux_ftype(const char *fstypename) 360dca60efcSMarcel Moolenaar { 361962cf420SMaxim Sobolev int i; 362962cf420SMaxim Sobolev static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = { 363962cf420SMaxim Sobolev {"ufs", LINUX_UFS_SUPER_MAGIC}, 364962cf420SMaxim Sobolev {"cd9660", LINUX_ISOFS_SUPER_MAGIC}, 365962cf420SMaxim Sobolev {"nfs", LINUX_NFS_SUPER_MAGIC}, 366962cf420SMaxim Sobolev {"ext2fs", LINUX_EXT2_SUPER_MAGIC}, 367962cf420SMaxim Sobolev {"procfs", LINUX_PROC_SUPER_MAGIC}, 368962cf420SMaxim Sobolev {"msdosfs", LINUX_MSDOS_SUPER_MAGIC}, 369962cf420SMaxim Sobolev {"ntfs", LINUX_NTFS_SUPER_MAGIC}, 370962cf420SMaxim Sobolev {"nwfs", LINUX_NCP_SUPER_MAGIC}, 371962cf420SMaxim Sobolev {"hpfs", LINUX_HPFS_SUPER_MAGIC}, 372962cf420SMaxim Sobolev {"coda", LINUX_CODA_SUPER_MAGIC}, 373e83d253bSOlivier Houchard {"devfs", LINUX_DEVFS_SUPER_MAGIC}, 3742166e4e0SDmitry Chagin {"tmpfs", LINUX_SHMFS_MAGIC}, 375962cf420SMaxim Sobolev {NULL, 0L}}; 376dca60efcSMarcel Moolenaar 377962cf420SMaxim Sobolev for (i = 0; b2l_tbl[i].bsd_name != NULL; i++) 378962cf420SMaxim Sobolev if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0) 379962cf420SMaxim Sobolev return (b2l_tbl[i].linux_type); 380dca60efcSMarcel Moolenaar 381dca60efcSMarcel Moolenaar return (0L); 382dca60efcSMarcel Moolenaar } 383dca60efcSMarcel Moolenaar 384525c9796SDmitry Chagin static int 385d0cad55dSPawel Jakub Dawidek bsd_to_linux_statfs(struct statfs *bsd_statfs, struct l_statfs *linux_statfs) 386f7a25872SJohn Baldwin { 387525c9796SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 388525c9796SDmitry Chagin uint64_t tmp; 389f7a25872SJohn Baldwin 390525c9796SDmitry Chagin #define LINUX_HIBITS 0xffffffff00000000ULL 391525c9796SDmitry Chagin 392525c9796SDmitry Chagin tmp = bsd_statfs->f_blocks | bsd_statfs->f_bfree | bsd_statfs->f_files | 393525c9796SDmitry Chagin bsd_statfs->f_bsize; 394525c9796SDmitry Chagin if ((bsd_statfs->f_bavail != -1 && (bsd_statfs->f_bavail & LINUX_HIBITS)) || 395525c9796SDmitry Chagin (bsd_statfs->f_ffree != -1 && (bsd_statfs->f_ffree & LINUX_HIBITS)) || 396525c9796SDmitry Chagin (tmp & LINUX_HIBITS)) 397525c9796SDmitry Chagin return (EOVERFLOW); 398525c9796SDmitry Chagin #undef LINUX_HIBITS 399525c9796SDmitry Chagin #endif 400f7a25872SJohn Baldwin linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 401f7a25872SJohn Baldwin linux_statfs->f_bsize = bsd_statfs->f_bsize; 402f7a25872SJohn Baldwin linux_statfs->f_blocks = bsd_statfs->f_blocks; 403f7a25872SJohn Baldwin linux_statfs->f_bfree = bsd_statfs->f_bfree; 404f7a25872SJohn Baldwin linux_statfs->f_bavail = bsd_statfs->f_bavail; 405f7a25872SJohn Baldwin linux_statfs->f_ffree = bsd_statfs->f_ffree; 406f7a25872SJohn Baldwin linux_statfs->f_files = bsd_statfs->f_files; 407f7a25872SJohn Baldwin linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 408f7a25872SJohn Baldwin linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 409f7a25872SJohn Baldwin linux_statfs->f_namelen = MAXNAMLEN; 410525c9796SDmitry Chagin 411525c9796SDmitry Chagin return (0); 412f7a25872SJohn Baldwin } 413f7a25872SJohn Baldwin 414c21dee17SSøren Schmidt int 415b40ce416SJulian Elischer linux_statfs(struct thread *td, struct linux_statfs_args *args) 416c21dee17SSøren Schmidt { 4175002a60fSMarcel Moolenaar struct l_statfs linux_statfs; 418*2f304845SKonstantin Belousov struct statfs *bsd_statfs; 419206a5d3aSIan Dowse char *path; 4202166e4e0SDmitry Chagin int error; 421d66a5066SPeter Wemm 422206a5d3aSIan Dowse LCONVPATHEXIST(td, args->path, &path); 423c21dee17SSøren Schmidt 424c21dee17SSøren Schmidt #ifdef DEBUG 42524593369SJonathan Lemon if (ldebug(statfs)) 426206a5d3aSIan Dowse printf(ARGS(statfs, "%s, *"), path); 427c21dee17SSøren Schmidt #endif 428*2f304845SKonstantin Belousov bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 429*2f304845SKonstantin Belousov error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs); 430206a5d3aSIan Dowse LFREEPATH(path); 431*2f304845SKonstantin Belousov if (error == 0) 432*2f304845SKonstantin Belousov error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs); 433*2f304845SKonstantin Belousov free(bsd_statfs, M_STATFS); 434*2f304845SKonstantin Belousov if (error != 0) 4352ad02313SDmitry Chagin return (error); 4367958a34cSDmitry Chagin return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs))); 437c21dee17SSøren Schmidt } 438c21dee17SSøren Schmidt 4397f8f1d7fSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 440835e5061SAlexander Leidinger static void 441835e5061SAlexander Leidinger bsd_to_linux_statfs64(struct statfs *bsd_statfs, struct l_statfs64 *linux_statfs) 442835e5061SAlexander Leidinger { 443835e5061SAlexander Leidinger 444835e5061SAlexander Leidinger linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename); 445835e5061SAlexander Leidinger linux_statfs->f_bsize = bsd_statfs->f_bsize; 446835e5061SAlexander Leidinger linux_statfs->f_blocks = bsd_statfs->f_blocks; 447835e5061SAlexander Leidinger linux_statfs->f_bfree = bsd_statfs->f_bfree; 448835e5061SAlexander Leidinger linux_statfs->f_bavail = bsd_statfs->f_bavail; 449835e5061SAlexander Leidinger linux_statfs->f_ffree = bsd_statfs->f_ffree; 450835e5061SAlexander Leidinger linux_statfs->f_files = bsd_statfs->f_files; 451835e5061SAlexander Leidinger linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0]; 452835e5061SAlexander Leidinger linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1]; 453835e5061SAlexander Leidinger linux_statfs->f_namelen = MAXNAMLEN; 454835e5061SAlexander Leidinger } 455835e5061SAlexander Leidinger 456835e5061SAlexander Leidinger int 457835e5061SAlexander Leidinger linux_statfs64(struct thread *td, struct linux_statfs64_args *args) 458835e5061SAlexander Leidinger { 459835e5061SAlexander Leidinger struct l_statfs64 linux_statfs; 460*2f304845SKonstantin Belousov struct statfs *bsd_statfs; 461835e5061SAlexander Leidinger char *path; 462835e5061SAlexander Leidinger int error; 463835e5061SAlexander Leidinger 4643ab85269SDavid Malone if (args->bufsize != sizeof(struct l_statfs64)) 4653ab85269SDavid Malone return EINVAL; 4663ab85269SDavid Malone 467835e5061SAlexander Leidinger LCONVPATHEXIST(td, args->path, &path); 468835e5061SAlexander Leidinger 469835e5061SAlexander Leidinger #ifdef DEBUG 470835e5061SAlexander Leidinger if (ldebug(statfs64)) 471835e5061SAlexander Leidinger printf(ARGS(statfs64, "%s, *"), path); 472835e5061SAlexander Leidinger #endif 473*2f304845SKonstantin Belousov bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 474*2f304845SKonstantin Belousov error = kern_statfs(td, path, UIO_SYSSPACE, bsd_statfs); 475835e5061SAlexander Leidinger LFREEPATH(path); 476*2f304845SKonstantin Belousov if (error == 0) 477*2f304845SKonstantin Belousov bsd_to_linux_statfs64(bsd_statfs, &linux_statfs); 478*2f304845SKonstantin Belousov free(bsd_statfs, M_STATFS); 479*2f304845SKonstantin Belousov if (error != 0) 480835e5061SAlexander Leidinger return (error); 4817958a34cSDmitry Chagin return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs))); 482835e5061SAlexander Leidinger } 48399546279SDmitry Chagin 48499546279SDmitry Chagin int 48599546279SDmitry Chagin linux_fstatfs64(struct thread *td, struct linux_fstatfs64_args *args) 48699546279SDmitry Chagin { 48799546279SDmitry Chagin struct l_statfs64 linux_statfs; 488*2f304845SKonstantin Belousov struct statfs *bsd_statfs; 48999546279SDmitry Chagin int error; 49099546279SDmitry Chagin 49199546279SDmitry Chagin #ifdef DEBUG 49299546279SDmitry Chagin if (ldebug(fstatfs64)) 49399546279SDmitry Chagin printf(ARGS(fstatfs64, "%d, *"), args->fd); 49499546279SDmitry Chagin #endif 49599546279SDmitry Chagin if (args->bufsize != sizeof(struct l_statfs64)) 49699546279SDmitry Chagin return (EINVAL); 49799546279SDmitry Chagin 498*2f304845SKonstantin Belousov bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 499*2f304845SKonstantin Belousov error = kern_fstatfs(td, args->fd, bsd_statfs); 500*2f304845SKonstantin Belousov if (error == 0) 501*2f304845SKonstantin Belousov bsd_to_linux_statfs64(bsd_statfs, &linux_statfs); 502*2f304845SKonstantin Belousov free(bsd_statfs, M_STATFS); 503*2f304845SKonstantin Belousov if (error != 0) 504*2f304845SKonstantin Belousov return (error); 50599546279SDmitry Chagin return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs))); 50699546279SDmitry Chagin } 5077f8f1d7fSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 508835e5061SAlexander Leidinger 509c21dee17SSøren Schmidt int 510b40ce416SJulian Elischer linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args) 511c21dee17SSøren Schmidt { 5125002a60fSMarcel Moolenaar struct l_statfs linux_statfs; 513*2f304845SKonstantin Belousov struct statfs *bsd_statfs; 514c21dee17SSøren Schmidt int error; 515c21dee17SSøren Schmidt 516c21dee17SSøren Schmidt #ifdef DEBUG 51724593369SJonathan Lemon if (ldebug(fstatfs)) 51824593369SJonathan Lemon printf(ARGS(fstatfs, "%d, *"), args->fd); 519c21dee17SSøren Schmidt #endif 520*2f304845SKonstantin Belousov bsd_statfs = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 521*2f304845SKonstantin Belousov error = kern_fstatfs(td, args->fd, bsd_statfs); 522*2f304845SKonstantin Belousov if (error == 0) 523*2f304845SKonstantin Belousov error = bsd_to_linux_statfs(bsd_statfs, &linux_statfs); 524*2f304845SKonstantin Belousov free(bsd_statfs, M_STATFS); 525*2f304845SKonstantin Belousov if (error != 0) 5262ad02313SDmitry Chagin return (error); 5277958a34cSDmitry Chagin return (copyout(&linux_statfs, args->buf, sizeof(linux_statfs))); 528c21dee17SSøren Schmidt } 529408da119SMarcel Moolenaar 5305002a60fSMarcel Moolenaar struct l_ustat 531408da119SMarcel Moolenaar { 5325002a60fSMarcel Moolenaar l_daddr_t f_tfree; 5335002a60fSMarcel Moolenaar l_ino_t f_tinode; 5345002a60fSMarcel Moolenaar char f_fname[6]; 5355002a60fSMarcel Moolenaar char f_fpack[6]; 5365002a60fSMarcel Moolenaar }; 5375002a60fSMarcel Moolenaar 5385002a60fSMarcel Moolenaar int 539b40ce416SJulian Elischer linux_ustat(struct thread *td, struct linux_ustat_args *args) 5405002a60fSMarcel Moolenaar { 5411e247cc2SPoul-Henning Kamp #ifdef DEBUG 5421e247cc2SPoul-Henning Kamp if (ldebug(ustat)) 5434ca75bedSDmitry Chagin printf(ARGS(ustat, "%ju, *"), (uintmax_t)args->dev); 5441e247cc2SPoul-Henning Kamp #endif 5451e247cc2SPoul-Henning Kamp 5461e247cc2SPoul-Henning Kamp return (EOPNOTSUPP); 547408da119SMarcel Moolenaar } 5485002a60fSMarcel Moolenaar 5491997c537SDavid E. O'Brien #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 5505002a60fSMarcel Moolenaar 5515002a60fSMarcel Moolenaar static int 5525002a60fSMarcel Moolenaar stat64_copyout(struct stat *buf, void *ubuf) 5535002a60fSMarcel Moolenaar { 5545002a60fSMarcel Moolenaar struct l_stat64 lbuf; 5555002a60fSMarcel Moolenaar 5565002a60fSMarcel Moolenaar bzero(&lbuf, sizeof(lbuf)); 557a4611ab6SEd Schouten lbuf.st_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8); 5585002a60fSMarcel Moolenaar lbuf.st_ino = buf->st_ino; 5595002a60fSMarcel Moolenaar lbuf.st_mode = buf->st_mode; 5605002a60fSMarcel Moolenaar lbuf.st_nlink = buf->st_nlink; 5615002a60fSMarcel Moolenaar lbuf.st_uid = buf->st_uid; 5625002a60fSMarcel Moolenaar lbuf.st_gid = buf->st_gid; 5635002a60fSMarcel Moolenaar lbuf.st_rdev = buf->st_rdev; 5645002a60fSMarcel Moolenaar lbuf.st_size = buf->st_size; 565510ea843SEd Schouten lbuf.st_atim.tv_sec = buf->st_atim.tv_sec; 566510ea843SEd Schouten lbuf.st_atim.tv_nsec = buf->st_atim.tv_nsec; 567510ea843SEd Schouten lbuf.st_mtim.tv_sec = buf->st_mtim.tv_sec; 568510ea843SEd Schouten lbuf.st_mtim.tv_nsec = buf->st_mtim.tv_nsec; 569510ea843SEd Schouten lbuf.st_ctim.tv_sec = buf->st_ctim.tv_sec; 570510ea843SEd Schouten lbuf.st_ctim.tv_nsec = buf->st_ctim.tv_nsec; 5715002a60fSMarcel Moolenaar lbuf.st_blksize = buf->st_blksize; 5725002a60fSMarcel Moolenaar lbuf.st_blocks = buf->st_blocks; 5735002a60fSMarcel Moolenaar 5745002a60fSMarcel Moolenaar /* 5755002a60fSMarcel Moolenaar * The __st_ino field makes all the difference. In the Linux kernel 5765002a60fSMarcel Moolenaar * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO, 5775002a60fSMarcel Moolenaar * but without the assignment to __st_ino the runtime linker refuses 5785002a60fSMarcel Moolenaar * to mmap(2) any shared libraries. I guess it's broken alright :-) 5795002a60fSMarcel Moolenaar */ 5805002a60fSMarcel Moolenaar lbuf.__st_ino = buf->st_ino; 5815002a60fSMarcel Moolenaar 5825002a60fSMarcel Moolenaar return (copyout(&lbuf, ubuf, sizeof(lbuf))); 5835002a60fSMarcel Moolenaar } 5845002a60fSMarcel Moolenaar 5855002a60fSMarcel Moolenaar int 586b40ce416SJulian Elischer linux_stat64(struct thread *td, struct linux_stat64_args *args) 5875002a60fSMarcel Moolenaar { 5885002a60fSMarcel Moolenaar struct stat buf; 589206a5d3aSIan Dowse char *filename; 590f7a25872SJohn Baldwin int error; 5915002a60fSMarcel Moolenaar 592206a5d3aSIan Dowse LCONVPATHEXIST(td, args->filename, &filename); 5935002a60fSMarcel Moolenaar 5945002a60fSMarcel Moolenaar #ifdef DEBUG 5955002a60fSMarcel Moolenaar if (ldebug(stat64)) 596206a5d3aSIan Dowse printf(ARGS(stat64, "%s, *"), filename); 5975002a60fSMarcel Moolenaar #endif 5985002a60fSMarcel Moolenaar 5990eee862aSEd Schouten error = linux_kern_stat(td, filename, UIO_SYSSPACE, &buf); 600206a5d3aSIan Dowse LFREEPATH(filename); 6015002a60fSMarcel Moolenaar if (error) 6025002a60fSMarcel Moolenaar return (error); 6035002a60fSMarcel Moolenaar return (stat64_copyout(&buf, args->statbuf)); 6045002a60fSMarcel Moolenaar } 6055002a60fSMarcel Moolenaar 6065002a60fSMarcel Moolenaar int 607b40ce416SJulian Elischer linux_lstat64(struct thread *td, struct linux_lstat64_args *args) 6085002a60fSMarcel Moolenaar { 6095002a60fSMarcel Moolenaar struct stat sb; 610206a5d3aSIan Dowse char *filename; 611f7a25872SJohn Baldwin int error; 6125002a60fSMarcel Moolenaar 613206a5d3aSIan Dowse LCONVPATHEXIST(td, args->filename, &filename); 6145002a60fSMarcel Moolenaar 6155002a60fSMarcel Moolenaar #ifdef DEBUG 6165002a60fSMarcel Moolenaar if (ldebug(lstat64)) 6175002a60fSMarcel Moolenaar printf(ARGS(lstat64, "%s, *"), args->filename); 6185002a60fSMarcel Moolenaar #endif 6195002a60fSMarcel Moolenaar 6200eee862aSEd Schouten error = linux_kern_lstat(td, filename, UIO_SYSSPACE, &sb); 621206a5d3aSIan Dowse LFREEPATH(filename); 6225002a60fSMarcel Moolenaar if (error) 6235002a60fSMarcel Moolenaar return (error); 6245002a60fSMarcel Moolenaar return (stat64_copyout(&sb, args->statbuf)); 6255002a60fSMarcel Moolenaar } 6265002a60fSMarcel Moolenaar 6275002a60fSMarcel Moolenaar int 628b40ce416SJulian Elischer linux_fstat64(struct thread *td, struct linux_fstat64_args *args) 6295002a60fSMarcel Moolenaar { 6305002a60fSMarcel Moolenaar struct stat buf; 6315002a60fSMarcel Moolenaar int error; 6325002a60fSMarcel Moolenaar 6335002a60fSMarcel Moolenaar #ifdef DEBUG 6345002a60fSMarcel Moolenaar if (ldebug(fstat64)) 6355002a60fSMarcel Moolenaar printf(ARGS(fstat64, "%d, *"), args->fd); 6365002a60fSMarcel Moolenaar #endif 6375002a60fSMarcel Moolenaar 638f7a25872SJohn Baldwin error = kern_fstat(td, args->fd, &buf); 639060e4882SDoug Ambrisko translate_fd_major_minor(td, args->fd, &buf); 6405002a60fSMarcel Moolenaar if (!error) 6415002a60fSMarcel Moolenaar error = stat64_copyout(&buf, args->statbuf); 6425002a60fSMarcel Moolenaar 6435002a60fSMarcel Moolenaar return (error); 6445002a60fSMarcel Moolenaar } 6455002a60fSMarcel Moolenaar 64648b05c3fSKonstantin Belousov int 64748b05c3fSKonstantin Belousov linux_fstatat64(struct thread *td, struct linux_fstatat64_args *args) 64848b05c3fSKonstantin Belousov { 64948b05c3fSKonstantin Belousov char *path; 65048b05c3fSKonstantin Belousov int error, dfd, flag; 65148b05c3fSKonstantin Belousov struct stat buf; 65248b05c3fSKonstantin Belousov 65348b05c3fSKonstantin Belousov if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 65448b05c3fSKonstantin Belousov return (EINVAL); 65548b05c3fSKonstantin Belousov flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 65648b05c3fSKonstantin Belousov AT_SYMLINK_NOFOLLOW : 0; 65748b05c3fSKonstantin Belousov 65848b05c3fSKonstantin Belousov dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 65948b05c3fSKonstantin Belousov LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 66048b05c3fSKonstantin Belousov 66148b05c3fSKonstantin Belousov #ifdef DEBUG 66248b05c3fSKonstantin Belousov if (ldebug(fstatat64)) 66348b05c3fSKonstantin Belousov printf(ARGS(fstatat64, "%i, %s, %i"), args->dfd, path, args->flag); 66448b05c3fSKonstantin Belousov #endif 66548b05c3fSKonstantin Belousov 6660eee862aSEd Schouten error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf); 66748b05c3fSKonstantin Belousov if (!error) 66848b05c3fSKonstantin Belousov error = stat64_copyout(&buf, args->statbuf); 66948b05c3fSKonstantin Belousov LFREEPATH(path); 67048b05c3fSKonstantin Belousov 67148b05c3fSKonstantin Belousov return (error); 67248b05c3fSKonstantin Belousov } 67348b05c3fSKonstantin Belousov 674606bcc17SDmitry Chagin #else /* __amd64__ && !COMPAT_LINUX32 */ 675606bcc17SDmitry Chagin 676606bcc17SDmitry Chagin int 677606bcc17SDmitry Chagin linux_newfstatat(struct thread *td, struct linux_newfstatat_args *args) 678606bcc17SDmitry Chagin { 679606bcc17SDmitry Chagin char *path; 680606bcc17SDmitry Chagin int error, dfd, flag; 681606bcc17SDmitry Chagin struct stat buf; 682606bcc17SDmitry Chagin 683606bcc17SDmitry Chagin if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 684606bcc17SDmitry Chagin return (EINVAL); 685606bcc17SDmitry Chagin flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) ? 686606bcc17SDmitry Chagin AT_SYMLINK_NOFOLLOW : 0; 687606bcc17SDmitry Chagin 688606bcc17SDmitry Chagin dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 689606bcc17SDmitry Chagin LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 690606bcc17SDmitry Chagin 691606bcc17SDmitry Chagin #ifdef DEBUG 692606bcc17SDmitry Chagin if (ldebug(newfstatat)) 693606bcc17SDmitry Chagin printf(ARGS(newfstatat, "%i, %s, %i"), args->dfd, path, args->flag); 694606bcc17SDmitry Chagin #endif 695606bcc17SDmitry Chagin 696606bcc17SDmitry Chagin error = linux_kern_statat(td, flag, dfd, path, UIO_SYSSPACE, &buf); 697606bcc17SDmitry Chagin if (error == 0) 698606bcc17SDmitry Chagin error = newstat_copyout(&buf, args->statbuf); 699606bcc17SDmitry Chagin LFREEPATH(path); 700606bcc17SDmitry Chagin 701606bcc17SDmitry Chagin return (error); 702606bcc17SDmitry Chagin } 703606bcc17SDmitry Chagin 7041997c537SDavid E. O'Brien #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 7059802eb9eSDmitry Chagin 7069802eb9eSDmitry Chagin int 7079802eb9eSDmitry Chagin linux_syncfs(struct thread *td, struct linux_syncfs_args *args) 7089802eb9eSDmitry Chagin { 7099802eb9eSDmitry Chagin cap_rights_t rights; 7109802eb9eSDmitry Chagin struct mount *mp; 7119802eb9eSDmitry Chagin struct vnode *vp; 7129802eb9eSDmitry Chagin int error, save; 7139802eb9eSDmitry Chagin 7149802eb9eSDmitry Chagin error = fgetvp(td, args->fd, cap_rights_init(&rights, CAP_FSYNC), &vp); 7159802eb9eSDmitry Chagin if (error != 0) 7169802eb9eSDmitry Chagin /* 7179802eb9eSDmitry Chagin * Linux syncfs() returns only EBADF, however fgetvp() 7189802eb9eSDmitry Chagin * can return EINVAL in case of file descriptor does 7199802eb9eSDmitry Chagin * not represent a vnode. XXX. 7209802eb9eSDmitry Chagin */ 7219802eb9eSDmitry Chagin return (error); 7229802eb9eSDmitry Chagin 7239802eb9eSDmitry Chagin mp = vp->v_mount; 7249802eb9eSDmitry Chagin mtx_lock(&mountlist_mtx); 7259802eb9eSDmitry Chagin error = vfs_busy(mp, MBF_MNTLSTLOCK); 7269802eb9eSDmitry Chagin if (error != 0) { 7279802eb9eSDmitry Chagin /* See comment above. */ 7289802eb9eSDmitry Chagin mtx_unlock(&mountlist_mtx); 7299802eb9eSDmitry Chagin goto out; 7309802eb9eSDmitry Chagin } 7319802eb9eSDmitry Chagin if ((mp->mnt_flag & MNT_RDONLY) == 0 && 7329802eb9eSDmitry Chagin vn_start_write(NULL, &mp, V_NOWAIT) == 0) { 7339802eb9eSDmitry Chagin save = curthread_pflags_set(TDP_SYNCIO); 7349802eb9eSDmitry Chagin vfs_msync(mp, MNT_NOWAIT); 7359802eb9eSDmitry Chagin VFS_SYNC(mp, MNT_NOWAIT); 7369802eb9eSDmitry Chagin curthread_pflags_restore(save); 7379802eb9eSDmitry Chagin vn_finished_write(mp); 7389802eb9eSDmitry Chagin } 7399802eb9eSDmitry Chagin vfs_unbusy(mp); 7409802eb9eSDmitry Chagin 7419802eb9eSDmitry Chagin out: 7429802eb9eSDmitry Chagin vrele(vp); 7439802eb9eSDmitry Chagin return (error); 7449802eb9eSDmitry Chagin } 745