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  * $FreeBSD: releng/5.0/sys/compat/linux/linux_stats.c 102954 2002-09-05 08:13:20Z bde $
   29  */
   30 
   31 #include "opt_mac.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/conf.h>
   35 #include <sys/dirent.h>
   36 #include <sys/file.h>
   37 #include <sys/filedesc.h>
   38 #include <sys/proc.h>
   39 #include <sys/mac.h>
   40 #include <sys/malloc.h>
   41 #include <sys/mount.h>
   42 #include <sys/namei.h>
   43 #include <sys/stat.h>
   44 #include <sys/systm.h>
   45 #include <sys/vnode.h>
   46 
   47 #include <machine/../linux/linux.h>
   48 #include <machine/../linux/linux_proto.h>
   49 
   50 #include <compat/linux/linux_util.h>
   51 
   52 static int
   53 newstat_copyout(struct stat *buf, void *ubuf)
   54 {
   55         struct l_newstat tbuf;
   56         struct cdevsw *cdevsw;
   57         dev_t dev;
   58 
   59         tbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
   60         tbuf.st_ino = buf->st_ino;
   61         tbuf.st_mode = buf->st_mode;
   62         tbuf.st_nlink = buf->st_nlink;
   63         tbuf.st_uid = buf->st_uid;
   64         tbuf.st_gid = buf->st_gid;
   65         tbuf.st_rdev = buf->st_rdev;
   66         tbuf.st_size = buf->st_size;
   67         tbuf.st_atime = buf->st_atime;
   68         tbuf.st_mtime = buf->st_mtime;
   69         tbuf.st_ctime = buf->st_ctime;
   70         tbuf.st_blksize = buf->st_blksize;
   71         tbuf.st_blocks = buf->st_blocks;
   72 
   73         /* Lie about disk drives which are character devices
   74          * in FreeBSD but block devices under Linux.
   75          */
   76         if (S_ISCHR(tbuf.st_mode) &&
   77             (dev = udev2dev(buf->st_rdev, 0)) != NODEV) {
   78                 cdevsw = devsw(dev);
   79                 if (cdevsw != NULL && (cdevsw->d_flags & D_DISK)) {
   80                         tbuf.st_mode &= ~S_IFMT;
   81                         tbuf.st_mode |= S_IFBLK;
   82 
   83                         /* XXX this may not be quite right */
   84                         /* Map major number to 0 */
   85                         tbuf.st_dev = uminor(buf->st_dev) & 0xf;
   86                         tbuf.st_rdev = buf->st_rdev & 0xff;
   87                 }
   88         }
   89 
   90         return (copyout(&tbuf, ubuf, sizeof(tbuf)));
   91 }
   92 
   93 int
   94 linux_newstat(struct thread *td, struct linux_newstat_args *args)
   95 {
   96         struct stat buf;
   97         struct nameidata nd;
   98         char *path;
   99         int error;
  100 
  101         LCONVPATHEXIST(td, args->path, &path);
  102 
  103 #ifdef DEBUG
  104         if (ldebug(newstat))
  105                 printf(ARGS(newstat, "%s, *"), path);
  106 #endif
  107 
  108         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path, td);
  109         error = namei(&nd);
  110         LFREEPATH(path);
  111         if (error)
  112                 return (error);
  113         NDFREE(&nd, NDF_ONLY_PNBUF);
  114 
  115         error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
  116         vput(nd.ni_vp);
  117         if (error)
  118                 return (error);
  119 
  120         return (newstat_copyout(&buf, args->buf));
  121 }
  122 
  123 int
  124 linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
  125 {
  126         int error;
  127         struct stat sb;
  128         struct nameidata nd;
  129         char *path;
  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         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, path,
  139             td);
  140         error = namei(&nd);
  141         LFREEPATH(path);
  142         if (error)
  143                 return (error);
  144         NDFREE(&nd, NDF_ONLY_PNBUF); 
  145 
  146         error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
  147         vput(nd.ni_vp);
  148         if (error)
  149                 return (error);
  150 
  151         return (newstat_copyout(&sb, args->buf));
  152 }
  153 
  154 int
  155 linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
  156 {
  157         struct file *fp;
  158         struct stat buf;
  159         int error;
  160 
  161 #ifdef DEBUG
  162         if (ldebug(newfstat))
  163                 printf(ARGS(newfstat, "%d, *"), args->fd);
  164 #endif
  165 
  166         if ((error = fget(td, args->fd, &fp)) != 0)
  167                 return (error);
  168 
  169         error = fo_stat(fp, &buf, td->td_ucred, td);
  170         fdrop(fp, td);
  171         if (!error)
  172                 error = newstat_copyout(&buf, args->buf);
  173 
  174         return (error);
  175 }
  176 
  177 /* XXX - All fields of type l_int are defined as l_long on i386 */
  178 struct l_statfs {
  179         l_int           f_type;
  180         l_int           f_bsize;
  181         l_int           f_blocks;
  182         l_int           f_bfree;
  183         l_int           f_bavail;
  184         l_int           f_files;
  185         l_int           f_ffree;
  186         l_fsid_t        f_fsid;
  187         l_int           f_namelen;
  188         l_int           f_spare[6];
  189 };
  190 
  191 #define LINUX_CODA_SUPER_MAGIC  0x73757245L
  192 #define LINUX_EXT2_SUPER_MAGIC  0xEF53L
  193 #define LINUX_HPFS_SUPER_MAGIC  0xf995e849L
  194 #define LINUX_ISOFS_SUPER_MAGIC 0x9660L
  195 #define LINUX_MSDOS_SUPER_MAGIC 0x4d44L
  196 #define LINUX_NCP_SUPER_MAGIC   0x564cL
  197 #define LINUX_NFS_SUPER_MAGIC   0x6969L
  198 #define LINUX_NTFS_SUPER_MAGIC  0x5346544EL
  199 #define LINUX_PROC_SUPER_MAGIC  0x9fa0L
  200 #define LINUX_UFS_SUPER_MAGIC   0x00011954L     /* XXX - UFS_MAGIC in Linux */
  201 
  202 static long
  203 bsd_to_linux_ftype(const char *fstypename)
  204 {
  205         int i;
  206         static struct {const char *bsd_name; long linux_type;} b2l_tbl[] = {
  207                 {"ufs",     LINUX_UFS_SUPER_MAGIC},
  208                 {"cd9660",  LINUX_ISOFS_SUPER_MAGIC},
  209                 {"nfs",     LINUX_NFS_SUPER_MAGIC},
  210                 {"ext2fs",  LINUX_EXT2_SUPER_MAGIC},
  211                 {"procfs",  LINUX_PROC_SUPER_MAGIC},
  212                 {"msdosfs", LINUX_MSDOS_SUPER_MAGIC},
  213                 {"ntfs",    LINUX_NTFS_SUPER_MAGIC},
  214                 {"nwfs",    LINUX_NCP_SUPER_MAGIC},
  215                 {"hpfs",    LINUX_HPFS_SUPER_MAGIC},
  216                 {"coda",    LINUX_CODA_SUPER_MAGIC},
  217                 {NULL,      0L}};
  218 
  219         for (i = 0; b2l_tbl[i].bsd_name != NULL; i++)
  220                 if (strcmp(b2l_tbl[i].bsd_name, fstypename) == 0)
  221                         return (b2l_tbl[i].linux_type);
  222 
  223         return (0L);
  224 }
  225 
  226 int
  227 linux_statfs(struct thread *td, struct linux_statfs_args *args)
  228 {
  229         struct mount *mp;
  230         struct nameidata *ndp;
  231         struct statfs *bsd_statfs;
  232         struct nameidata nd;
  233         struct l_statfs linux_statfs;
  234         char *path;
  235         int error;
  236 
  237         LCONVPATHEXIST(td, args->path, &path);
  238 
  239 #ifdef DEBUG
  240         if (ldebug(statfs))
  241                 printf(ARGS(statfs, "%s, *"), path);
  242 #endif
  243         ndp = &nd;
  244         NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, path, td);
  245         error = namei(ndp);
  246         LFREEPATH(path);
  247         if (error)
  248                 return error;
  249         NDFREE(ndp, NDF_ONLY_PNBUF);
  250         mp = ndp->ni_vp->v_mount;
  251         bsd_statfs = &mp->mnt_stat;
  252         vrele(ndp->ni_vp);
  253 #ifdef MAC
  254         error = mac_check_mount_stat(td->td_proc->p_ucred, mp);
  255         if (error)
  256                 return (error);
  257 #endif
  258         error = VFS_STATFS(mp, bsd_statfs, td);
  259         if (error)
  260                 return error;
  261         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
  262         linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
  263         linux_statfs.f_bsize = bsd_statfs->f_bsize;
  264         linux_statfs.f_blocks = bsd_statfs->f_blocks;
  265         linux_statfs.f_bfree = bsd_statfs->f_bfree;
  266         linux_statfs.f_bavail = bsd_statfs->f_bavail;
  267         linux_statfs.f_ffree = bsd_statfs->f_ffree;
  268         linux_statfs.f_files = bsd_statfs->f_files;
  269         linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
  270         linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
  271         linux_statfs.f_namelen = MAXNAMLEN;
  272         return copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
  273             sizeof(linux_statfs));
  274 }
  275 
  276 int
  277 linux_fstatfs(struct thread *td, struct linux_fstatfs_args *args)
  278 {
  279         struct file *fp;
  280         struct mount *mp;
  281         struct statfs *bsd_statfs;
  282         struct l_statfs linux_statfs;
  283         int error;
  284 
  285 #ifdef DEBUG
  286         if (ldebug(fstatfs))
  287                 printf(ARGS(fstatfs, "%d, *"), args->fd);
  288 #endif
  289         error = getvnode(td->td_proc->p_fd, args->fd, &fp);
  290         if (error)
  291                 return error;
  292         mp = ((struct vnode *)fp->f_data)->v_mount;
  293 #ifdef MAC
  294         error = mac_check_mount_stat(td->td_proc->p_ucred, mp);
  295         if (error) {
  296                 fdrop(fp, td);
  297                 return (error);
  298         }
  299 #endif
  300         bsd_statfs = &mp->mnt_stat;
  301         error = VFS_STATFS(mp, bsd_statfs, td);
  302         if (error) {
  303                 fdrop(fp, td);
  304                 return error;
  305         }
  306         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
  307         linux_statfs.f_type = bsd_to_linux_ftype(bsd_statfs->f_fstypename);
  308         linux_statfs.f_bsize = bsd_statfs->f_bsize;
  309         linux_statfs.f_blocks = bsd_statfs->f_blocks;
  310         linux_statfs.f_bfree = bsd_statfs->f_bfree;
  311         linux_statfs.f_bavail = bsd_statfs->f_bavail;
  312         linux_statfs.f_ffree = bsd_statfs->f_ffree;
  313         linux_statfs.f_files = bsd_statfs->f_files;
  314         linux_statfs.f_fsid.val[0] = bsd_statfs->f_fsid.val[0];
  315         linux_statfs.f_fsid.val[1] = bsd_statfs->f_fsid.val[1];
  316         linux_statfs.f_namelen = MAXNAMLEN;
  317         error = copyout((caddr_t)&linux_statfs, (caddr_t)args->buf,
  318             sizeof(linux_statfs));
  319         fdrop(fp, td);
  320         return error;
  321 }
  322 
  323 struct l_ustat 
  324 {
  325         l_daddr_t       f_tfree;
  326         l_ino_t         f_tinode;
  327         char            f_fname[6];
  328         char            f_fpack[6];
  329 };
  330 
  331 int
  332 linux_ustat(struct thread *td, struct linux_ustat_args *args)
  333 {
  334         struct l_ustat lu;
  335         dev_t dev;
  336         struct vnode *vp;
  337         struct statfs *stat;
  338         int error;
  339 
  340 #ifdef DEBUG
  341         if (ldebug(ustat))
  342                 printf(ARGS(ustat, "%d, *"), args->dev);
  343 #endif
  344 
  345         /*
  346          * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
  347          * lu.f_tinode and lu.f_tfree are set from the device's super block.
  348          */
  349         bzero(&lu, sizeof(lu));
  350 
  351         /*
  352          * XXX - Don't return an error if we can't find a vnode for the
  353          * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
  354          * dev_t. The dev_t that is used now may as well be a truncated
  355          * dev_t returned from previous syscalls. Just return a bzeroed
  356          * ustat in that case.
  357          */
  358         dev = makedev(args->dev >> 8, args->dev & 0xFF);
  359         if (vfinddev(dev, VCHR, &vp)) {
  360                 if (vp->v_mount == NULL)
  361                         return (EINVAL);
  362 #ifdef MAC
  363                 error = mac_check_mount_stat(td->td_proc->p_ucred,
  364                     vp->v_mount);
  365                 if (error)
  366                         return (error);
  367 #endif
  368                 stat = &(vp->v_mount->mnt_stat);
  369                 error = VFS_STATFS(vp->v_mount, stat, td);
  370                 if (error)
  371                         return (error);
  372 
  373                 lu.f_tfree = stat->f_bfree;
  374                 lu.f_tinode = stat->f_ffree;
  375         }
  376 
  377         return (copyout(&lu, args->ubuf, sizeof(lu)));
  378 }
  379 
  380 #if defined(__i386__)
  381 
  382 static int
  383 stat64_copyout(struct stat *buf, void *ubuf)
  384 {
  385         struct l_stat64 lbuf;
  386 
  387         bzero(&lbuf, sizeof(lbuf));
  388         lbuf.st_dev = uminor(buf->st_dev) | (umajor(buf->st_dev) << 8);
  389         lbuf.st_ino = buf->st_ino;
  390         lbuf.st_mode = buf->st_mode;
  391         lbuf.st_nlink = buf->st_nlink;
  392         lbuf.st_uid = buf->st_uid;
  393         lbuf.st_gid = buf->st_gid;
  394         lbuf.st_rdev = buf->st_rdev;
  395         lbuf.st_size = buf->st_size;
  396         lbuf.st_atime = buf->st_atime;
  397         lbuf.st_mtime = buf->st_mtime;
  398         lbuf.st_ctime = buf->st_ctime;
  399         lbuf.st_blksize = buf->st_blksize;
  400         lbuf.st_blocks = buf->st_blocks;
  401 
  402         /*
  403          * The __st_ino field makes all the difference. In the Linux kernel
  404          * it is conditionally compiled based on STAT64_HAS_BROKEN_ST_INO,
  405          * but without the assignment to __st_ino the runtime linker refuses
  406          * to mmap(2) any shared libraries. I guess it's broken alright :-)
  407          */
  408         lbuf.__st_ino = buf->st_ino;
  409 
  410         return (copyout(&lbuf, ubuf, sizeof(lbuf)));
  411 }
  412 
  413 int
  414 linux_stat64(struct thread *td, struct linux_stat64_args *args)
  415 {
  416         struct stat buf;
  417         struct nameidata nd;
  418         int error;
  419         char *filename;
  420 
  421         LCONVPATHEXIST(td, args->filename, &filename);
  422 
  423 #ifdef DEBUG
  424         if (ldebug(stat64))
  425                 printf(ARGS(stat64, "%s, *"), filename);
  426 #endif
  427 
  428         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
  429             td);
  430         error = namei(&nd);
  431         LFREEPATH(filename);
  432         if (error)
  433                 return (error);
  434         NDFREE(&nd, NDF_ONLY_PNBUF);
  435 
  436         error = vn_stat(nd.ni_vp, &buf, td->td_ucred, NOCRED, td);
  437         vput(nd.ni_vp);
  438         if (error)
  439                 return (error);
  440 
  441         return (stat64_copyout(&buf, args->statbuf));
  442 }
  443 
  444 int
  445 linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
  446 {
  447         int error;
  448         struct stat sb;
  449         struct nameidata nd;
  450         char *filename;
  451 
  452         LCONVPATHEXIST(td, args->filename, &filename);
  453 
  454 #ifdef DEBUG
  455         if (ldebug(lstat64))
  456                 printf(ARGS(lstat64, "%s, *"), args->filename);
  457 #endif
  458 
  459         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_SYSSPACE, filename,
  460             td);
  461         error = namei(&nd);
  462         LFREEPATH(filename);
  463         if (error)
  464                 return (error);
  465         NDFREE(&nd, NDF_ONLY_PNBUF); 
  466 
  467         error = vn_stat(nd.ni_vp, &sb, td->td_ucred, NOCRED, td);
  468         vput(nd.ni_vp);
  469         if (error)
  470                 return (error);
  471 
  472         return (stat64_copyout(&sb, args->statbuf));
  473 }
  474 
  475 int
  476 linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
  477 {
  478         struct filedesc *fdp;
  479         struct file *fp;
  480         struct stat buf;
  481         int error;
  482 
  483 #ifdef DEBUG
  484         if (ldebug(fstat64))
  485                 printf(ARGS(fstat64, "%d, *"), args->fd);
  486 #endif
  487 
  488         fdp = td->td_proc->p_fd;
  489         if ((unsigned)args->fd >= fdp->fd_nfiles ||
  490             (fp = fdp->fd_ofiles[args->fd]) == NULL)
  491                 return (EBADF);
  492 
  493         error = fo_stat(fp, &buf, td->td_ucred, td);
  494         if (!error)
  495                 error = stat64_copyout(&buf, args->statbuf);
  496 
  497         return (error);
  498 }
  499 
  500 #endif /* __i386__ */

Cache object: d7fe35492a696fba6f52e06773c62ffe


[ 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.