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

Cache object: 9e8796fdfa7542ab8e79a3870685ea40


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