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/i386/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 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$
   29  */
   30 
   31 #include <sys/param.h>
   32 #include <sys/systm.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/socketvar.h>
   40 #include <sys/stat.h>
   41 #include <sys/vnode.h>
   42 #include <sys/pipe.h>
   43 
   44 #include <i386/linux/linux.h>
   45 #include <i386/linux/linux_proto.h>
   46 #include <i386/linux/linux_util.h>
   47 
   48 struct linux_newstat {
   49     unsigned short stat_dev;
   50     unsigned short __pad1;
   51     unsigned long stat_ino;
   52     unsigned short stat_mode;
   53     unsigned short stat_nlink;
   54     unsigned short stat_uid;
   55     unsigned short stat_gid;
   56     unsigned short stat_rdev;
   57     unsigned short __pad2;
   58     unsigned long stat_size;
   59     unsigned long stat_blksize;
   60     unsigned long stat_blocks;
   61     unsigned long stat_atime;
   62     unsigned long __unused1;
   63     unsigned long stat_mtime;
   64     unsigned long __unused2;
   65     unsigned long stat_ctime;
   66     unsigned long __unused3;
   67     unsigned long __unused4;
   68     unsigned long __unused5;
   69 };
   70 
   71 struct linux_ustat 
   72 {
   73         int     f_tfree;
   74         u_long  f_tinode;
   75         char    f_fname[6];
   76         char    f_fpack[6];
   77 };
   78 
   79 static int
   80 newstat_copyout(struct stat *buf, void *ubuf)
   81 {
   82         struct linux_newstat tbuf;
   83 
   84         tbuf.stat_dev = minor(buf->st_dev) | (major(buf->st_dev) << 8);
   85         tbuf.stat_ino = buf->st_ino;
   86         tbuf.stat_mode = buf->st_mode;
   87         tbuf.stat_nlink = buf->st_nlink;
   88         tbuf.stat_uid = buf->st_uid;
   89         tbuf.stat_gid = buf->st_gid;
   90         tbuf.stat_rdev = buf->st_rdev;
   91         tbuf.stat_size = buf->st_size;
   92         tbuf.stat_atime = buf->st_atime;
   93         tbuf.stat_mtime = buf->st_mtime;
   94         tbuf.stat_ctime = buf->st_ctime;
   95         tbuf.stat_blksize = buf->st_blksize;
   96         tbuf.stat_blocks = buf->st_blocks;
   97 
   98         return copyout(&tbuf, ubuf, sizeof(tbuf));
   99 }
  100 
  101 int
  102 linux_newstat(struct proc *p, struct linux_newstat_args *args)
  103 {
  104         struct stat buf;
  105         struct nameidata nd;
  106         int error;
  107         caddr_t sg;
  108 
  109         sg = stackgap_init();
  110         CHECKALTEXIST(p, &sg, args->path);
  111 
  112 #ifdef DEBUG
  113         printf("Linux-emul(%ld): newstat(%s, *)\n", (long)p->p_pid,
  114                args->path);
  115 #endif
  116 
  117         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
  118                args->path, p);
  119         error = namei(&nd);
  120         if (error)
  121                 return (error);
  122 
  123         error = vn_stat(nd.ni_vp, &buf, p);
  124         vput(nd.ni_vp);
  125         if (error)
  126                 return (error);
  127 
  128         return (newstat_copyout(&buf, args->buf));
  129 }
  130 
  131 /*
  132  * Get file status; this version does not follow links.
  133  */
  134 int
  135 linux_newlstat(p, uap)
  136         struct proc *p;
  137         struct linux_newlstat_args *uap;
  138 {
  139         int error;
  140         struct vnode *vp;
  141         struct stat sb;
  142         struct nameidata nd;
  143         caddr_t sg;
  144 
  145         sg = stackgap_init();
  146         CHECKALTEXIST(p, &sg, uap->path);
  147   
  148 #ifdef DEBUG
  149         printf("Linux-emul(%ld): newlstat(%s, *)\n", (long)p->p_pid,
  150                uap->path);
  151 #endif
  152 
  153         NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
  154                uap->path, p);
  155         error = namei(&nd);
  156         if (error)
  157                 return (error);
  158 
  159         vp = nd.ni_vp;
  160         error = vn_stat(vp, &sb, p);
  161         vput(vp);
  162         if (error)
  163                 return (error);
  164 
  165         return (newstat_copyout(&sb, uap->buf));
  166 }
  167 
  168 int
  169 linux_newfstat(struct proc *p, struct linux_newfstat_args *args)
  170 {
  171     struct filedesc *fdp = p->p_fd;
  172     struct file *fp;
  173     struct stat buf;
  174     int error;
  175   
  176 #ifdef DEBUG
  177     printf("Linux-emul(%d): newfstat(%d, *)\n", p->p_pid, args->fd);
  178 #endif
  179     if ((unsigned)args->fd >= fdp->fd_nfiles 
  180         || (fp = fdp->fd_ofiles[args->fd]) == NULL)
  181         return EBADF;
  182     switch (fp->f_type) {
  183     case DTYPE_FIFO:
  184     case DTYPE_VNODE:
  185         error = vn_stat((struct vnode *)fp->f_data, &buf, p);
  186         break;
  187     case DTYPE_SOCKET:
  188         error = soo_stat((struct socket *)fp->f_data, &buf);
  189         break;
  190     case DTYPE_PIPE:
  191         error = pipe_stat((struct pipe *)fp->f_data, &buf);
  192         break;
  193     default:
  194         panic("LINUX newfstat");
  195     }
  196     if (!error)
  197         error = newstat_copyout(&buf, args->buf);
  198     return error;
  199 }
  200 
  201 struct linux_statfs_buf {
  202         long ftype;
  203         long fbsize;
  204         long fblocks;
  205         long fbfree;
  206         long fbavail;
  207         long ffiles;
  208         long fffree;
  209         linux_fsid_t ffsid;
  210         long fnamelen;
  211         long fspare[6];
  212 };
  213 
  214 int
  215 linux_statfs(struct proc *p, struct linux_statfs_args *args)
  216 {
  217         struct mount *mp;
  218         struct nameidata *ndp;
  219         struct statfs *bsd_statfs;
  220         struct nameidata nd;
  221         struct linux_statfs_buf linux_statfs_buf;
  222         int error;
  223         caddr_t sg;
  224 
  225         sg = stackgap_init();
  226         CHECKALTEXIST(p, &sg, args->path);
  227 
  228 #ifdef DEBUG
  229         printf("Linux-emul(%d): statfs(%s, *)\n", p->p_pid, args->path);
  230 #endif
  231         ndp = &nd;
  232         NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->path, curproc);
  233         if (error = namei(ndp))
  234                 return error;
  235         mp = ndp->ni_vp->v_mount;
  236         bsd_statfs = &mp->mnt_stat;
  237         vrele(ndp->ni_vp);
  238         if (error = VFS_STATFS(mp, bsd_statfs, p))
  239                 return error;
  240         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
  241         linux_statfs_buf.ftype = bsd_statfs->f_type;
  242         linux_statfs_buf.fbsize = bsd_statfs->f_bsize;
  243         linux_statfs_buf.fblocks = bsd_statfs->f_blocks;
  244         linux_statfs_buf.fbfree = bsd_statfs->f_bfree;
  245         linux_statfs_buf.fbavail = bsd_statfs->f_bavail;
  246         linux_statfs_buf.fffree = bsd_statfs->f_ffree;
  247         linux_statfs_buf.ffiles = bsd_statfs->f_files;
  248         linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0];
  249         linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1];
  250         linux_statfs_buf.fnamelen = MAXNAMLEN;
  251         return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf,
  252                        sizeof(struct linux_statfs_buf));
  253 }
  254 
  255 int
  256 linux_fstatfs(struct proc *p, struct linux_fstatfs_args *args)
  257 {
  258         struct file *fp;
  259         struct mount *mp;
  260         struct statfs *bsd_statfs;
  261         struct linux_statfs_buf linux_statfs_buf;
  262         int error;
  263 
  264 #ifdef DEBUG
  265         printf("Linux-emul(%d): fstatfs(%d, *)\n", p->p_pid, args->fd);
  266 #endif
  267         if (error = getvnode(p->p_fd, args->fd, &fp))
  268                 return error;
  269         mp = ((struct vnode *)fp->f_data)->v_mount;
  270         bsd_statfs = &mp->mnt_stat;
  271         if (error = VFS_STATFS(mp, bsd_statfs, p))
  272                 return error;
  273         bsd_statfs->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
  274         linux_statfs_buf.ftype = bsd_statfs->f_type;
  275         linux_statfs_buf.fbsize = bsd_statfs->f_bsize;
  276         linux_statfs_buf.fblocks = bsd_statfs->f_blocks;
  277         linux_statfs_buf.fbfree = bsd_statfs->f_bfree;
  278         linux_statfs_buf.fbavail = bsd_statfs->f_bavail;
  279         linux_statfs_buf.fffree = bsd_statfs->f_ffree;
  280         linux_statfs_buf.ffiles = bsd_statfs->f_files;
  281         linux_statfs_buf.ffsid.val[0] = bsd_statfs->f_fsid.val[0];
  282         linux_statfs_buf.ffsid.val[1] = bsd_statfs->f_fsid.val[1];
  283         linux_statfs_buf.fnamelen = MAXNAMLEN;
  284         return copyout((caddr_t)&linux_statfs_buf, (caddr_t)args->buf,
  285                        sizeof(struct linux_statfs_buf));
  286 }
  287 
  288 int
  289 linux_ustat(p, uap)
  290         struct proc *p;
  291         struct linux_ustat_args *uap;
  292 {
  293         struct linux_ustat lu;
  294         dev_t dev;
  295         struct vnode *vp;
  296         struct statfs *stat;
  297         int error;
  298 
  299 #ifdef DEBUG
  300         printf("Linux-emul(%ld): ustat(%d, *)\n", (long)p->p_pid, uap->dev);
  301 #endif
  302 
  303         /*
  304          * lu.f_fname and lu.f_fpack are not used. They are always zeroed.
  305          * lu.f_tinode and lu.f_tfree are set from the device's super block.
  306          */
  307         bzero(&lu, sizeof(lu));
  308 
  309         /*
  310          * XXX - Don't return an error if we can't find a vnode for the
  311          * device. Our dev_t is 32-bits whereas Linux only has a 16-bits
  312          * dev_t. The dev_t that is used now may as well be a truncated
  313          * dev_t returned from previous syscalls. Just return a bzeroed
  314          * ustat in that case.
  315          */
  316         dev = makedev(uap->dev >> 8, uap->dev & 0xFF);
  317         if (vfinddev(dev, VBLK, &vp)) {
  318                 if (vp->v_mount == NULL)
  319                         return (EINVAL);
  320                 stat = &(vp->v_mount->mnt_stat);
  321                 error = VFS_STATFS(vp->v_mount, stat, p);
  322                 if (error)
  323                         return (error);
  324 
  325                 lu.f_tfree = stat->f_bfree;
  326                 lu.f_tinode = stat->f_ffree;
  327         }
  328 
  329         return (copyout(&lu, uap->ubuf, sizeof(lu)));
  330 }

Cache object: b900ff3b9aa4e22490db2df4846fb197


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