linux/fs/stat.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *  linux/fs/stat.c
   4 *
   5 *  Copyright (C) 1991, 1992  Linus Torvalds
   6 */
   7
   8#include <linux/export.h>
   9#include <linux/mm.h>
  10#include <linux/errno.h>
  11#include <linux/file.h>
  12#include <linux/highuid.h>
  13#include <linux/fs.h>
  14#include <linux/namei.h>
  15#include <linux/security.h>
  16#include <linux/cred.h>
  17#include <linux/syscalls.h>
  18#include <linux/pagemap.h>
  19#include <linux/compat.h>
  20
  21#include <linux/uaccess.h>
  22#include <asm/unistd.h>
  23
  24#include "internal.h"
  25
  26/**
  27 * generic_fillattr - Fill in the basic attributes from the inode struct
  28 * @inode: Inode to use as the source
  29 * @stat: Where to fill in the attributes
  30 *
  31 * Fill in the basic attributes in the kstat structure from data that's to be
  32 * found on the VFS inode structure.  This is the default if no getattr inode
  33 * operation is supplied.
  34 */
  35void generic_fillattr(struct inode *inode, struct kstat *stat)
  36{
  37        stat->dev = inode->i_sb->s_dev;
  38        stat->ino = inode->i_ino;
  39        stat->mode = inode->i_mode;
  40        stat->nlink = inode->i_nlink;
  41        stat->uid = inode->i_uid;
  42        stat->gid = inode->i_gid;
  43        stat->rdev = inode->i_rdev;
  44        stat->size = i_size_read(inode);
  45        stat->atime = inode->i_atime;
  46        stat->mtime = inode->i_mtime;
  47        stat->ctime = inode->i_ctime;
  48        stat->blksize = i_blocksize(inode);
  49        stat->blocks = inode->i_blocks;
  50
  51        if (IS_NOATIME(inode))
  52                stat->result_mask &= ~STATX_ATIME;
  53        if (IS_AUTOMOUNT(inode))
  54                stat->attributes |= STATX_ATTR_AUTOMOUNT;
  55}
  56EXPORT_SYMBOL(generic_fillattr);
  57
  58/**
  59 * vfs_getattr_nosec - getattr without security checks
  60 * @path: file to get attributes from
  61 * @stat: structure to return attributes in
  62 * @request_mask: STATX_xxx flags indicating what the caller wants
  63 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
  64 *
  65 * Get attributes without calling security_inode_getattr.
  66 *
  67 * Currently the only caller other than vfs_getattr is internal to the
  68 * filehandle lookup code, which uses only the inode number and returns no
  69 * attributes to any user.  Any other code probably wants vfs_getattr.
  70 */
  71int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
  72                      u32 request_mask, unsigned int query_flags)
  73{
  74        struct inode *inode = d_backing_inode(path->dentry);
  75
  76        memset(stat, 0, sizeof(*stat));
  77        stat->result_mask |= STATX_BASIC_STATS;
  78        request_mask &= STATX_ALL;
  79        query_flags &= KSTAT_QUERY_FLAGS;
  80
  81        if (IS_DAX(inode))
  82                stat->attributes |= STATX_ATTR_DAX;
  83
  84        if (inode->i_op->getattr)
  85                return inode->i_op->getattr(path, stat, request_mask,
  86                                            query_flags);
  87
  88        generic_fillattr(inode, stat);
  89        return 0;
  90}
  91EXPORT_SYMBOL(vfs_getattr_nosec);
  92
  93/*
  94 * vfs_getattr - Get the enhanced basic attributes of a file
  95 * @path: The file of interest
  96 * @stat: Where to return the statistics
  97 * @request_mask: STATX_xxx flags indicating what the caller wants
  98 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
  99 *
 100 * Ask the filesystem for a file's attributes.  The caller must indicate in
 101 * request_mask and query_flags to indicate what they want.
 102 *
 103 * If the file is remote, the filesystem can be forced to update the attributes
 104 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
 105 * suppress the update by passing AT_STATX_DONT_SYNC.
 106 *
 107 * Bits must have been set in request_mask to indicate which attributes the
 108 * caller wants retrieving.  Any such attribute not requested may be returned
 109 * anyway, but the value may be approximate, and, if remote, may not have been
 110 * synchronised with the server.
 111 *
 112 * 0 will be returned on success, and a -ve error code if unsuccessful.
 113 */
 114int vfs_getattr(const struct path *path, struct kstat *stat,
 115                u32 request_mask, unsigned int query_flags)
 116{
 117        int retval;
 118
 119        retval = security_inode_getattr(path);
 120        if (retval)
 121                return retval;
 122        return vfs_getattr_nosec(path, stat, request_mask, query_flags);
 123}
 124EXPORT_SYMBOL(vfs_getattr);
 125
 126/**
 127 * vfs_statx_fd - Get the enhanced basic attributes by file descriptor
 128 * @fd: The file descriptor referring to the file of interest
 129 * @stat: The result structure to fill in.
 130 * @request_mask: STATX_xxx flags indicating what the caller wants
 131 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
 132 *
 133 * This function is a wrapper around vfs_getattr().  The main difference is
 134 * that it uses a file descriptor to determine the file location.
 135 *
 136 * 0 will be returned on success, and a -ve error code if unsuccessful.
 137 */
 138int vfs_statx_fd(unsigned int fd, struct kstat *stat,
 139                 u32 request_mask, unsigned int query_flags)
 140{
 141        struct fd f;
 142        int error = -EBADF;
 143
 144        if (query_flags & ~KSTAT_QUERY_FLAGS)
 145                return -EINVAL;
 146
 147        f = fdget_raw(fd);
 148        if (f.file) {
 149                error = vfs_getattr(&f.file->f_path, stat,
 150                                    request_mask, query_flags);
 151                fdput(f);
 152        }
 153        return error;
 154}
 155EXPORT_SYMBOL(vfs_statx_fd);
 156
 157inline unsigned vfs_stat_set_lookup_flags(unsigned *lookup_flags, int flags)
 158{
 159        if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
 160                       AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
 161                return -EINVAL;
 162
 163        *lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
 164        if (flags & AT_SYMLINK_NOFOLLOW)
 165                *lookup_flags &= ~LOOKUP_FOLLOW;
 166        if (flags & AT_NO_AUTOMOUNT)
 167                *lookup_flags &= ~LOOKUP_AUTOMOUNT;
 168        if (flags & AT_EMPTY_PATH)
 169                *lookup_flags |= LOOKUP_EMPTY;
 170
 171        return 0;
 172}
 173
 174/**
 175 * vfs_statx - Get basic and extra attributes by filename
 176 * @dfd: A file descriptor representing the base dir for a relative filename
 177 * @filename: The name of the file of interest
 178 * @flags: Flags to control the query
 179 * @stat: The result structure to fill in.
 180 * @request_mask: STATX_xxx flags indicating what the caller wants
 181 *
 182 * This function is a wrapper around vfs_getattr().  The main difference is
 183 * that it uses a filename and base directory to determine the file location.
 184 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
 185 * at the given name from being referenced.
 186 *
 187 * 0 will be returned on success, and a -ve error code if unsuccessful.
 188 */
 189int vfs_statx(int dfd, const char __user *filename, int flags,
 190              struct kstat *stat, u32 request_mask)
 191{
 192        struct path path;
 193        int error = -EINVAL;
 194        unsigned lookup_flags;
 195
 196        if (vfs_stat_set_lookup_flags(&lookup_flags, flags))
 197                return -EINVAL;
 198retry:
 199        error = user_path_at(dfd, filename, lookup_flags, &path);
 200        if (error)
 201                goto out;
 202
 203        error = vfs_getattr(&path, stat, request_mask, flags);
 204        path_put(&path);
 205        if (retry_estale(error, lookup_flags)) {
 206                lookup_flags |= LOOKUP_REVAL;
 207                goto retry;
 208        }
 209out:
 210        return error;
 211}
 212EXPORT_SYMBOL(vfs_statx);
 213
 214
 215#ifdef __ARCH_WANT_OLD_STAT
 216
 217/*
 218 * For backward compatibility?  Maybe this should be moved
 219 * into arch/i386 instead?
 220 */
 221static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
 222{
 223        static int warncount = 5;
 224        struct __old_kernel_stat tmp;
 225
 226        if (warncount > 0) {
 227                warncount--;
 228                printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
 229                        current->comm);
 230        } else if (warncount < 0) {
 231                /* it's laughable, but... */
 232                warncount = 0;
 233        }
 234
 235        memset(&tmp, 0, sizeof(struct __old_kernel_stat));
 236        tmp.st_dev = old_encode_dev(stat->dev);
 237        tmp.st_ino = stat->ino;
 238        if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
 239                return -EOVERFLOW;
 240        tmp.st_mode = stat->mode;
 241        tmp.st_nlink = stat->nlink;
 242        if (tmp.st_nlink != stat->nlink)
 243                return -EOVERFLOW;
 244        SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
 245        SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
 246        tmp.st_rdev = old_encode_dev(stat->rdev);
 247#if BITS_PER_LONG == 32
 248        if (stat->size > MAX_NON_LFS)
 249                return -EOVERFLOW;
 250#endif
 251        tmp.st_size = stat->size;
 252        tmp.st_atime = stat->atime.tv_sec;
 253        tmp.st_mtime = stat->mtime.tv_sec;
 254        tmp.st_ctime = stat->ctime.tv_sec;
 255        return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
 256}
 257
 258SYSCALL_DEFINE2(stat, const char __user *, filename,
 259                struct __old_kernel_stat __user *, statbuf)
 260{
 261        struct kstat stat;
 262        int error;
 263
 264        error = vfs_stat(filename, &stat);
 265        if (error)
 266                return error;
 267
 268        return cp_old_stat(&stat, statbuf);
 269}
 270
 271SYSCALL_DEFINE2(lstat, const char __user *, filename,
 272                struct __old_kernel_stat __user *, statbuf)
 273{
 274        struct kstat stat;
 275        int error;
 276
 277        error = vfs_lstat(filename, &stat);
 278        if (error)
 279                return error;
 280
 281        return cp_old_stat(&stat, statbuf);
 282}
 283
 284SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
 285{
 286        struct kstat stat;
 287        int error = vfs_fstat(fd, &stat);
 288
 289        if (!error)
 290                error = cp_old_stat(&stat, statbuf);
 291
 292        return error;
 293}
 294
 295#endif /* __ARCH_WANT_OLD_STAT */
 296
 297#if BITS_PER_LONG == 32
 298#  define choose_32_64(a,b) a
 299#else
 300#  define choose_32_64(a,b) b
 301#endif
 302
 303#define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
 304#define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
 305
 306#ifndef INIT_STRUCT_STAT_PADDING
 307#  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
 308#endif
 309
 310static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
 311{
 312        struct stat tmp;
 313
 314        if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
 315                return -EOVERFLOW;
 316#if BITS_PER_LONG == 32
 317        if (stat->size > MAX_NON_LFS)
 318                return -EOVERFLOW;
 319#endif
 320
 321        INIT_STRUCT_STAT_PADDING(tmp);
 322        tmp.st_dev = encode_dev(stat->dev);
 323        tmp.st_ino = stat->ino;
 324        if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
 325                return -EOVERFLOW;
 326        tmp.st_mode = stat->mode;
 327        tmp.st_nlink = stat->nlink;
 328        if (tmp.st_nlink != stat->nlink)
 329                return -EOVERFLOW;
 330        SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
 331        SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
 332        tmp.st_rdev = encode_dev(stat->rdev);
 333        tmp.st_size = stat->size;
 334        tmp.st_atime = stat->atime.tv_sec;
 335        tmp.st_mtime = stat->mtime.tv_sec;
 336        tmp.st_ctime = stat->ctime.tv_sec;
 337#ifdef STAT_HAVE_NSEC
 338        tmp.st_atime_nsec = stat->atime.tv_nsec;
 339        tmp.st_mtime_nsec = stat->mtime.tv_nsec;
 340        tmp.st_ctime_nsec = stat->ctime.tv_nsec;
 341#endif
 342        tmp.st_blocks = stat->blocks;
 343        tmp.st_blksize = stat->blksize;
 344        return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
 345}
 346
 347SYSCALL_DEFINE2(newstat, const char __user *, filename,
 348                struct stat __user *, statbuf)
 349{
 350        struct kstat stat;
 351        int error = vfs_stat(filename, &stat);
 352
 353        if (error)
 354                return error;
 355        return cp_new_stat(&stat, statbuf);
 356}
 357
 358SYSCALL_DEFINE2(newlstat, const char __user *, filename,
 359                struct stat __user *, statbuf)
 360{
 361        struct kstat stat;
 362        int error;
 363
 364        error = vfs_lstat(filename, &stat);
 365        if (error)
 366                return error;
 367
 368        return cp_new_stat(&stat, statbuf);
 369}
 370
 371#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
 372SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
 373                struct stat __user *, statbuf, int, flag)
 374{
 375        struct kstat stat;
 376        int error;
 377
 378        error = vfs_fstatat(dfd, filename, &stat, flag);
 379        if (error)
 380                return error;
 381        return cp_new_stat(&stat, statbuf);
 382}
 383#endif
 384
 385SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
 386{
 387        struct kstat stat;
 388        int error = vfs_fstat(fd, &stat);
 389
 390        if (!error)
 391                error = cp_new_stat(&stat, statbuf);
 392
 393        return error;
 394}
 395
 396static int do_readlinkat(int dfd, const char __user *pathname,
 397                         char __user *buf, int bufsiz)
 398{
 399        struct path path;
 400        int error;
 401        int empty = 0;
 402        unsigned int lookup_flags = LOOKUP_EMPTY;
 403
 404        if (bufsiz <= 0)
 405                return -EINVAL;
 406
 407retry:
 408        error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
 409        if (!error) {
 410                struct inode *inode = d_backing_inode(path.dentry);
 411
 412                error = empty ? -ENOENT : -EINVAL;
 413                /*
 414                 * AFS mountpoints allow readlink(2) but are not symlinks
 415                 */
 416                if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
 417                        error = security_inode_readlink(path.dentry);
 418                        if (!error) {
 419                                touch_atime(&path);
 420                                error = vfs_readlink(path.dentry, buf, bufsiz);
 421                        }
 422                }
 423                path_put(&path);
 424                if (retry_estale(error, lookup_flags)) {
 425                        lookup_flags |= LOOKUP_REVAL;
 426                        goto retry;
 427                }
 428        }
 429        return error;
 430}
 431
 432SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
 433                char __user *, buf, int, bufsiz)
 434{
 435        return do_readlinkat(dfd, pathname, buf, bufsiz);
 436}
 437
 438SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
 439                int, bufsiz)
 440{
 441        return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
 442}
 443
 444
 445/* ---------- LFS-64 ----------- */
 446#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
 447
 448#ifndef INIT_STRUCT_STAT64_PADDING
 449#  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
 450#endif
 451
 452static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
 453{
 454        struct stat64 tmp;
 455
 456        INIT_STRUCT_STAT64_PADDING(tmp);
 457#ifdef CONFIG_MIPS
 458        /* mips has weird padding, so we don't get 64 bits there */
 459        tmp.st_dev = new_encode_dev(stat->dev);
 460        tmp.st_rdev = new_encode_dev(stat->rdev);
 461#else
 462        tmp.st_dev = huge_encode_dev(stat->dev);
 463        tmp.st_rdev = huge_encode_dev(stat->rdev);
 464#endif
 465        tmp.st_ino = stat->ino;
 466        if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
 467                return -EOVERFLOW;
 468#ifdef STAT64_HAS_BROKEN_ST_INO
 469        tmp.__st_ino = stat->ino;
 470#endif
 471        tmp.st_mode = stat->mode;
 472        tmp.st_nlink = stat->nlink;
 473        tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
 474        tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
 475        tmp.st_atime = stat->atime.tv_sec;
 476        tmp.st_atime_nsec = stat->atime.tv_nsec;
 477        tmp.st_mtime = stat->mtime.tv_sec;
 478        tmp.st_mtime_nsec = stat->mtime.tv_nsec;
 479        tmp.st_ctime = stat->ctime.tv_sec;
 480        tmp.st_ctime_nsec = stat->ctime.tv_nsec;
 481        tmp.st_size = stat->size;
 482        tmp.st_blocks = stat->blocks;
 483        tmp.st_blksize = stat->blksize;
 484        return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
 485}
 486
 487SYSCALL_DEFINE2(stat64, const char __user *, filename,
 488                struct stat64 __user *, statbuf)
 489{
 490        struct kstat stat;
 491        int error = vfs_stat(filename, &stat);
 492
 493        if (!error)
 494                error = cp_new_stat64(&stat, statbuf);
 495
 496        return error;
 497}
 498
 499SYSCALL_DEFINE2(lstat64, const char __user *, filename,
 500                struct stat64 __user *, statbuf)
 501{
 502        struct kstat stat;
 503        int error = vfs_lstat(filename, &stat);
 504
 505        if (!error)
 506                error = cp_new_stat64(&stat, statbuf);
 507
 508        return error;
 509}
 510
 511SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
 512{
 513        struct kstat stat;
 514        int error = vfs_fstat(fd, &stat);
 515
 516        if (!error)
 517                error = cp_new_stat64(&stat, statbuf);
 518
 519        return error;
 520}
 521
 522SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
 523                struct stat64 __user *, statbuf, int, flag)
 524{
 525        struct kstat stat;
 526        int error;
 527
 528        error = vfs_fstatat(dfd, filename, &stat, flag);
 529        if (error)
 530                return error;
 531        return cp_new_stat64(&stat, statbuf);
 532}
 533#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
 534
 535noinline_for_stack int
 536cp_statx(const struct kstat *stat, struct statx __user *buffer)
 537{
 538        struct statx tmp;
 539
 540        memset(&tmp, 0, sizeof(tmp));
 541
 542        tmp.stx_mask = stat->result_mask;
 543        tmp.stx_blksize = stat->blksize;
 544        tmp.stx_attributes = stat->attributes;
 545        tmp.stx_nlink = stat->nlink;
 546        tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
 547        tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
 548        tmp.stx_mode = stat->mode;
 549        tmp.stx_ino = stat->ino;
 550        tmp.stx_size = stat->size;
 551        tmp.stx_blocks = stat->blocks;
 552        tmp.stx_attributes_mask = stat->attributes_mask;
 553        tmp.stx_atime.tv_sec = stat->atime.tv_sec;
 554        tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
 555        tmp.stx_btime.tv_sec = stat->btime.tv_sec;
 556        tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
 557        tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
 558        tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
 559        tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
 560        tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
 561        tmp.stx_rdev_major = MAJOR(stat->rdev);
 562        tmp.stx_rdev_minor = MINOR(stat->rdev);
 563        tmp.stx_dev_major = MAJOR(stat->dev);
 564        tmp.stx_dev_minor = MINOR(stat->dev);
 565
 566        return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
 567}
 568
 569/**
 570 * sys_statx - System call to get enhanced stats
 571 * @dfd: Base directory to pathwalk from *or* fd to stat.
 572 * @filename: File to stat or "" with AT_EMPTY_PATH
 573 * @flags: AT_* flags to control pathwalk.
 574 * @mask: Parts of statx struct actually required.
 575 * @buffer: Result buffer.
 576 *
 577 * Note that fstat() can be emulated by setting dfd to the fd of interest,
 578 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
 579 */
 580SYSCALL_DEFINE5(statx,
 581                int, dfd, const char __user *, filename, unsigned, flags,
 582                unsigned int, mask,
 583                struct statx __user *, buffer)
 584{
 585        struct kstat stat;
 586        int error;
 587
 588        if (mask & STATX__RESERVED)
 589                return -EINVAL;
 590        if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
 591                return -EINVAL;
 592
 593        error = vfs_statx(dfd, filename, flags, &stat, mask);
 594        if (error)
 595                return error;
 596
 597        return cp_statx(&stat, buffer);
 598}
 599
 600#ifdef CONFIG_COMPAT
 601static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
 602{
 603        struct compat_stat tmp;
 604
 605        if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
 606                return -EOVERFLOW;
 607
 608        memset(&tmp, 0, sizeof(tmp));
 609        tmp.st_dev = old_encode_dev(stat->dev);
 610        tmp.st_ino = stat->ino;
 611        if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
 612                return -EOVERFLOW;
 613        tmp.st_mode = stat->mode;
 614        tmp.st_nlink = stat->nlink;
 615        if (tmp.st_nlink != stat->nlink)
 616                return -EOVERFLOW;
 617        SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
 618        SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
 619        tmp.st_rdev = old_encode_dev(stat->rdev);
 620        if ((u64) stat->size > MAX_NON_LFS)
 621                return -EOVERFLOW;
 622        tmp.st_size = stat->size;
 623        tmp.st_atime = stat->atime.tv_sec;
 624        tmp.st_atime_nsec = stat->atime.tv_nsec;
 625        tmp.st_mtime = stat->mtime.tv_sec;
 626        tmp.st_mtime_nsec = stat->mtime.tv_nsec;
 627        tmp.st_ctime = stat->ctime.tv_sec;
 628        tmp.st_ctime_nsec = stat->ctime.tv_nsec;
 629        tmp.st_blocks = stat->blocks;
 630        tmp.st_blksize = stat->blksize;
 631        return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
 632}
 633
 634COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
 635                       struct compat_stat __user *, statbuf)
 636{
 637        struct kstat stat;
 638        int error;
 639
 640        error = vfs_stat(filename, &stat);
 641        if (error)
 642                return error;
 643        return cp_compat_stat(&stat, statbuf);
 644}
 645
 646COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
 647                       struct compat_stat __user *, statbuf)
 648{
 649        struct kstat stat;
 650        int error;
 651
 652        error = vfs_lstat(filename, &stat);
 653        if (error)
 654                return error;
 655        return cp_compat_stat(&stat, statbuf);
 656}
 657
 658#ifndef __ARCH_WANT_STAT64
 659COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
 660                       const char __user *, filename,
 661                       struct compat_stat __user *, statbuf, int, flag)
 662{
 663        struct kstat stat;
 664        int error;
 665
 666        error = vfs_fstatat(dfd, filename, &stat, flag);
 667        if (error)
 668                return error;
 669        return cp_compat_stat(&stat, statbuf);
 670}
 671#endif
 672
 673COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
 674                       struct compat_stat __user *, statbuf)
 675{
 676        struct kstat stat;
 677        int error = vfs_fstat(fd, &stat);
 678
 679        if (!error)
 680                error = cp_compat_stat(&stat, statbuf);
 681        return error;
 682}
 683#endif
 684
 685/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
 686void __inode_add_bytes(struct inode *inode, loff_t bytes)
 687{
 688        inode->i_blocks += bytes >> 9;
 689        bytes &= 511;
 690        inode->i_bytes += bytes;
 691        if (inode->i_bytes >= 512) {
 692                inode->i_blocks++;
 693                inode->i_bytes -= 512;
 694        }
 695}
 696EXPORT_SYMBOL(__inode_add_bytes);
 697
 698void inode_add_bytes(struct inode *inode, loff_t bytes)
 699{
 700        spin_lock(&inode->i_lock);
 701        __inode_add_bytes(inode, bytes);
 702        spin_unlock(&inode->i_lock);
 703}
 704
 705EXPORT_SYMBOL(inode_add_bytes);
 706
 707void __inode_sub_bytes(struct inode *inode, loff_t bytes)
 708{
 709        inode->i_blocks -= bytes >> 9;
 710        bytes &= 511;
 711        if (inode->i_bytes < bytes) {
 712                inode->i_blocks--;
 713                inode->i_bytes += 512;
 714        }
 715        inode->i_bytes -= bytes;
 716}
 717
 718EXPORT_SYMBOL(__inode_sub_bytes);
 719
 720void inode_sub_bytes(struct inode *inode, loff_t bytes)
 721{
 722        spin_lock(&inode->i_lock);
 723        __inode_sub_bytes(inode, bytes);
 724        spin_unlock(&inode->i_lock);
 725}
 726
 727EXPORT_SYMBOL(inode_sub_bytes);
 728
 729loff_t inode_get_bytes(struct inode *inode)
 730{
 731        loff_t ret;
 732
 733        spin_lock(&inode->i_lock);
 734        ret = __inode_get_bytes(inode);
 735        spin_unlock(&inode->i_lock);
 736        return ret;
 737}
 738
 739EXPORT_SYMBOL(inode_get_bytes);
 740
 741void inode_set_bytes(struct inode *inode, loff_t bytes)
 742{
 743        /* Caller is here responsible for sufficient locking
 744         * (ie. inode->i_lock) */
 745        inode->i_blocks = bytes >> 9;
 746        inode->i_bytes = bytes & 511;
 747}
 748
 749EXPORT_SYMBOL(inode_set_bytes);
 750