The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/linux/linux_stats.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1994-1995 Søren Schmidt
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    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
   16  *
   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.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD: src/sys/compat/linux/linux_stats.c,v 1.62.2.2 2005/04/23 21:57:43 ps Exp $");
   31 
   32 #include "opt_mac.h"
   33 
   34 #include <sys/param.h>
   35 #include <sys/conf.h>
   36 #include <sys/dirent.h>
   37 #include <sys/file.h>
   38 #include <sys/filedesc.h>
   39 #include <sys/proc.h>
   40 #include <sys/mac.h>
   41 #include <sys/malloc.h>
   42 #include <sys/mount.h>
   43 #include <sys/namei.h>
   44 #include <sys/stat.h>
   45 #include <sys/syscallsubr.h>
   46 #include <sys/systm.h>
   47 #include <sys/vnode.h>
   48 
   49 #include "opt_compat.h"
   50 
   51 #ifdef COMPAT_LINUX32
   52 #include <machine/../linux32/linux.h>
   53 #include <machine/../linux32/linux32_proto.h>
   54 #else
   55 #include <machine/../linux/linux.h>
   56 #include <machine/../linux/linux_proto.h>
   57 #endif
   58 
   59 #include <compat/linux/linux_util.h>
   60 
   61 static int
   62 newstat_copyout(struct stat *buf, void *ubuf)
   63 {
   64         struct l_newstat tbuf;
   65         struct cdevsw *cdevsw;
   66         struct cdev *dev;
   67 
   68         bzero(&tbuf, sizeof(tbuf));
   69         tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
   70         tbuf.st_ino = buf->st_ino;
   71         tbuf.st_mode = buf->st_mode;
   72         tbuf.st_nlink = buf->st_nlink;
   73         tbuf.st_uid = buf->st_uid;
   74         tbuf.st_gid = buf->st_gid;
   75         tbuf.st_rdev = buf->st_rdev;
   76         tbuf.st_size = buf->st_size;
   77         tbuf.st_atime = buf->st_atime;
   78         tbuf.st_mtime = buf->st_mtime;
   79         tbuf.st_ctime = buf->st_ctime;
   80         tbuf.st_blksize = buf->st_blksize;
   81         tbuf.st_blocks = buf->st_blocks;
   82 
   83         /* Lie about disk drives which are character devices
   84          * in FreeBSD but block devices under Linux.
   85          */
   86         if (S_ISCHR(tbuf.st_mode) &&
   87             (dev = findcdev(buf->st_rdev)) != NULL) {
   88                 cdevsw = devsw(dev);
   89                 if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
   90                         tbuf.st_mode &= ~S_IFMT;
   91                         tbuf.st_mode |= S_IFBLK;
   92 
   93                         /* XXX this may not be quite right */
   94                         /* Map major number to 0 */
   95                         tbuf.st_dev = uminor(buf->st_dev) & 0xf;
   96                         tbuf.st_rdev = buf->st_rdev & 0xff;
   97                 }
   98         }
   99 
  100         return (copyout(&tbuf, ubuf, sizeof(tbuf)));
  101 }
  102 
  103 int
  104 linux_newstat(struct thread *td, struct linux_newstat_args *args)
  105 {
  106         struct stat buf;
  107         char *path;
  108         int error;
  109 
  110         LCONVPATHEXIST(td, args->path, &path);
  111 
  112 #ifdef DEBUG
  113         if (ldebug(newstat))
  114                 printf(ARGS(newstat, "%s, *"), path);
  115 #endif
  116 
  117         error = kern_stat(td, path, UIO_SYSSPACE, &buf);
  118         LFREEPATH(path);
  119         if (error)
  120                 return (error);
  121         return (newstat_copyout(&buf, args->buf));
  122 }
  123 
  124 int
  125 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
  126 {
  127         struct stat sb;
  128         char *path;
  129         int error;
  130 
  131         LCONVPATHEXIST(td, args->path, &path);
  132 
  133 #ifdef DEBUG
  134         if (ldebug(newlstat))
  135                 printf(ARGS(newlstat, "%s, *"), path);
  136 #endif
  137 
  138         error = kern_lstat(td, path, UIO_SYSSPACE, &sb);
  139         LFREEPATH(path);
  140         if (error)
  141                 return (error);
  142         return (newstat_copyout(&sb, args->buf));
  143 }
  144 
  145 int
  146 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
  147 {
  148         struct stat buf;
  149         int error;
  150 
  151 #ifdef DEBUG
  152         if (ldebug(newfstat))
  153                 printf(ARGS(newfstat, "%d, *"), args->fd);
  154 #endif
  155 
  156         error = kern_fstat(td, args->fd, &buf);
  157         if (!error)
  158                 error = newstat_copyout(&buf, args->buf);
  159 
  160         return (error);
  161 }
  162 
  163 /* XXX - All fields of type l_int are defined as l_long on i386 */
  164 struct l_statfs {
  165         l_int           f_type;
  166         l_int           f_bsize;
  167         l_int           f_blocks;
  168         l_int           f_bfree;
  169         l_int           f_bavail;
  170         l_int           f_files;
  171         l_int           f_ffree;
  172         l_fsid_t        f_fsid;
  173         l_int           f_namelen;
  174         l_int           f_spare[6];
  175 };
  176 
  177 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
  178 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
  179 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
  180 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
  181 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
  182 #define LINUX_NCP_SUPER_MAGIC   0x564cL
  183 #define LINUX_NFS_SUPER_MAGIC   0x6969L
  184 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
  185 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
  186 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
  187 
  188 static long
  189 bsd_to_linux_ftype(const char *fstypename)
  190 {
  191         int i;
  192         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
  193                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
  194                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
  195                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
  196                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
  197                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
  198                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
  199                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
  200                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
  201                 {"hpfs",    LINUX_HPFS_SUPER_MAGIC},
  202                 {"coda",    LINUX_CODA_SUPER_MAGIC},
  203                 {NULL,      0L}};
  204 
  205         for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
  206                 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
  207                         return (b2l_tbl[i].linux_type);
  208 
  209         return (0L);
  210 }
  211 
  212 static void
  213 bsd_to_linux_statfs(struct thread *td, struct statfs *bsd_statfs,
  214     struct l_statfs *linux_statfs)
  215 {
  216 
  217         linux_statfs->f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
  218         linux_statfs->f_bsize = bsd_statfs->f_bsize;
  219         linux_statfs->f_blocks = bsd_statfs->f_blocks;
  220         linux_statfs->f_bfree = bsd_statfs->f_bfree;
  221         linux_statfs->f_bavail = bsd_statfs->f_bavail;
  222         linux_statfs->f_ffree = bsd_statfs->f_ffree;
  223         linux_statfs->f_files = bsd_statfs->f_files;
  224         if (suser(td)) {
  225                 linux_statfs->f_fsid.val[0] = 0;
  226                 linux_statfs->f_fsid.val[1] = 0;
  227         } else {
  228                 linux_statfs->f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
  229                 linux_statfs->f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
  230         }
  231         linux_statfs->f_namelen = MAXNAMLEN;
  232 }
  233 
  234 int
  235 linux_statfs(struct thread *td, struct linux_statfs_args *args)
  236 {
  237         struct l_statfs linux_statfs;
  238         struct statfs bsd_statfs;
  239         char *path;
  240         int error;
  241 
  242         LCONVPATHEXIST(td, args->path, &path);
  243 
  244 #ifdef DEBUG
  245         if (ldebug(statfs))
  246                 printf(ARGS(statfs, "%s, *"), path);
  247 #endif
  248         error = kern_statfs(td, path, UIO_SYSSPACE, &bsd_statfs);
  249         LFREEPATH(path);
  250         if (error)
  251                 return (error);
  252         bsd_to_linux_statfs(td, &bsd_statfs, &linux_statfs);
  253         return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
  254 }
  255 
  256 int
  257 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
  258 {
  259         struct l_statfs linux_statfs;
  260         struct statfs bsd_statfs;
  261         int error;
  262 
  263 #ifdef DEBUG
  264         if (ldebug(fstatfs))
  265                 printf(ARGS(fstatfs, "%d, *"), args->fd);
  266 #endif
  267         error = kern_fstatfs(td, args->fd, &bsd_statfs);
  268         if (error)
  269                 return error;
  270         bsd_to_linux_statfs(td, &bsd_statfs, &linux_statfs);
  271         return copyout(&linux_statfs, args->buf, sizeof(linux_statfs));
  272 }
  273 
  274 struct l_ustat
  275 {
  276         l_daddr_t       f_tfree;
  277         l_ino_t         f_tinode;
  278         char            f_fname[6];
  279         char            f_fpack[6];
  280 };
  281 
  282 int
  283 linux_ustat(struct thread *td, struct linux_ustat_args *args)
  284 {
  285         struct l_ustat lu;
  286         struct cdev *dev;
  287         struct vnode *vp;
  288         struct statfs *stat;
  289         int error;
  290 
  291 #ifdef DEBUG
  292         if (ldebug(ustat))
  293                 printf(ARGS(ustat, "%d, *"), args->dev);
  294 #endif
  295 
  296         /*
  297          * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
  298          * lu.f_tinode and lu.f_tfree are set from the device's super block.
  299          */
  300         bzero(&lu, sizeof(lu));
  301 
  302         /*
  303          * XXX - Don't return an error if we can't find a vnode for the
  304          * device. Our struct cdev *is 32-bits whereas Linux only has a 16-bits
  305          * struct cdev *. The struct cdev *that is used now may as well be a truncated
  306          * struct cdev *returned from previous syscalls. Just return a bzeroed
  307          * ustat in that case.
  308          *
  309          * XXX: findcdev() SHALL not be used this way.  Somebody (TM) will
  310          *      have to find a better way.  It may be that we should stick
  311          *      a dev_t into struct mount, and walk the mountlist for a
  312          *      perfect match and failing that try again looking for a
  313          *      minor-truncated match.
  314          */
  315         dev = findcdev(makedev(args->dev >> 8, args->dev & 0xFF));
  316         if (dev != NULL && vfinddev(dev, &vp)) {
  317                 if (vp->v_mount == NULL)
  318                         return (EINVAL);
  319 #ifdef MAC
  320                 error = mac_check_mount_stat(td->td_ucred, vp->v_mount);
  321                 if (error)
  322                         return (error);
  323 #endif
  324                 stat = &(vp->v_mount->mnt_stat);
  325                 error = VFS_STATFS(vp->v_mount, stat, td);
  326                 if (error)
  327                         return (error);
  328 
  329                 lu.f_tfree = stat->f_bfree;
  330                 lu.f_tinode = stat->f_ffree;
  331         }
  332 
  333         return (copyout(&lu, args->ubuf, sizeof(lu)));
  334 }
  335 
  336 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
  337 
  338 static int
  339 stat64_copyout(struct stat *buf, void *ubuf)
  340 {
  341         struct l_stat64 lbuf;
  342         struct cdevsw *cdevsw;
  343         struct cdev *dev;
  344 
  345         bzero(&lbuf, sizeof(lbuf));
  346         lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
  347         lbuf.st_ino = buf->st_ino;
  348         lbuf.st_mode = buf->st_mode;
  349         lbuf.st_nlink = buf->st_nlink;
  350         lbuf.st_uid = buf->st_uid;
  351         lbuf.st_gid = buf->st_gid;
  352         lbuf.st_rdev = buf->st_rdev;
  353         lbuf.st_size = buf->st_size;
  354         lbuf.st_atime = buf->st_atime;
  355         lbuf.st_mtime = buf->st_mtime;
  356         lbuf.st_ctime = buf->st_ctime;
  357         lbuf.st_blksize = buf->st_blksize;
  358         lbuf.st_blocks = buf->st_blocks;
  359 
  360         /* Lie about disk drives which are character devices
  361          * in FreeBSD but block devices under Linux.
  362          */
  363         if (S_ISCHR(lbuf.st_mode) &&
  364             (dev = findcdev(buf->st_rdev)) != NULL) {
  365                 cdevsw = devsw(dev);
  366                 if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
  367                         lbuf.st_mode &= ~S_IFMT;
  368                         lbuf.st_mode |= S_IFBLK;
  369 
  370                         /* XXX this may not be quite right */
  371                         /* Map major number to 0 */
  372                         lbuf.st_dev = uminor(buf->st_dev) & 0xf;
  373                         lbuf.st_rdev = buf->st_rdev & 0xff;
  374                 }
  375         }
  376 
  377         /*
  378          * The __st_ino field makes all the difference. In the Linux kernel
  379          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
  380          * but without the assignment to __st_ino the runtime linker refuses
  381          * to mmap(2) any shared libraries. I guess it's broken alright :-)
  382          */
  383         lbuf.__st_ino = buf->st_ino;
  384 
  385         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
  386 }
  387 
  388 int
  389 linux_stat64(struct thread *td, struct linux_stat64_args *args)
  390 {
  391         struct stat buf;
  392         char *filename;
  393         int error;
  394 
  395         LCONVPATHEXIST(td, args->filename, &filename);
  396 
  397 #ifdef DEBUG
  398         if (ldebug(stat64))
  399                 printf(ARGS(stat64, "%s, *"), filename);
  400 #endif
  401 
  402         error = kern_stat(td, filename, UIO_SYSSPACE, &buf);
  403         LFREEPATH(filename);
  404         if (error)
  405                 return (error);
  406         return (stat64_copyout(&buf, args->statbuf));
  407 }
  408 
  409 int
  410 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
  411 {
  412         struct stat sb;
  413         char *filename;
  414         int error;
  415 
  416         LCONVPATHEXIST(td, args->filename, &filename);
  417 
  418 #ifdef DEBUG
  419         if (ldebug(lstat64))
  420                 printf(ARGS(lstat64, "%s, *"), args->filename);
  421 #endif
  422 
  423         error = kern_lstat(td, filename, UIO_SYSSPACE, &sb);
  424         LFREEPATH(filename);
  425         if (error)
  426                 return (error);
  427         return (stat64_copyout(&sb, args->statbuf));
  428 }
  429 
  430 int
  431 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
  432 {
  433         struct stat buf;
  434         int error;
  435 
  436 #ifdef DEBUG
  437         if (ldebug(fstat64))
  438                 printf(ARGS(fstat64, "%d, *"), args->fd);
  439 #endif
  440 
  441         error = kern_fstat(td, args->fd, &buf);
  442         if (!error)
  443                 error = stat64_copyout(&buf, args->statbuf);
  444 
  445         return (error);
  446 }
  447 
  448 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */

Cache object: d66b3f4291500b0698be290e01467844


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.