Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/fs/stat.c
0004  *
0005  *  Copyright (C) 1991, 1992  Linus Torvalds
0006  */
0007 
0008 #include <linux/export.h>
0009 #include <linux/mm.h>
0010 #include <linux/errno.h>
0011 #include <linux/file.h>
0012 #include <linux/highuid.h>
0013 #include <linux/fs.h>
0014 #include <linux/namei.h>
0015 #include <linux/security.h>
0016 #include <linux/cred.h>
0017 #include <linux/syscalls.h>
0018 #include <linux/pagemap.h>
0019 #include <linux/compat.h>
0020 
0021 #include <linux/uaccess.h>
0022 #include <asm/unistd.h>
0023 
0024 #include "internal.h"
0025 #include "mount.h"
0026 
0027 /**
0028  * generic_fillattr - Fill in the basic attributes from the inode struct
0029  * @mnt_userns: user namespace of the mount the inode was found from
0030  * @inode:  Inode to use as the source
0031  * @stat:   Where to fill in the attributes
0032  *
0033  * Fill in the basic attributes in the kstat structure from data that's to be
0034  * found on the VFS inode structure.  This is the default if no getattr inode
0035  * operation is supplied.
0036  *
0037  * If the inode has been found through an idmapped mount the user namespace of
0038  * the vfsmount must be passed through @mnt_userns. This function will then
0039  * take care to map the inode according to @mnt_userns before filling in the
0040  * uid and gid filds. On non-idmapped mounts or if permission checking is to be
0041  * performed on the raw inode simply passs init_user_ns.
0042  */
0043 void generic_fillattr(struct user_namespace *mnt_userns, struct inode *inode,
0044               struct kstat *stat)
0045 {
0046     stat->dev = inode->i_sb->s_dev;
0047     stat->ino = inode->i_ino;
0048     stat->mode = inode->i_mode;
0049     stat->nlink = inode->i_nlink;
0050     stat->uid = i_uid_into_mnt(mnt_userns, inode);
0051     stat->gid = i_gid_into_mnt(mnt_userns, inode);
0052     stat->rdev = inode->i_rdev;
0053     stat->size = i_size_read(inode);
0054     stat->atime = inode->i_atime;
0055     stat->mtime = inode->i_mtime;
0056     stat->ctime = inode->i_ctime;
0057     stat->blksize = i_blocksize(inode);
0058     stat->blocks = inode->i_blocks;
0059 }
0060 EXPORT_SYMBOL(generic_fillattr);
0061 
0062 /**
0063  * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
0064  * @inode:  Inode to use as the source
0065  * @stat:   Where to fill in the attribute flags
0066  *
0067  * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
0068  * inode that are published on i_flags and enforced by the VFS.
0069  */
0070 void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
0071 {
0072     if (inode->i_flags & S_IMMUTABLE)
0073         stat->attributes |= STATX_ATTR_IMMUTABLE;
0074     if (inode->i_flags & S_APPEND)
0075         stat->attributes |= STATX_ATTR_APPEND;
0076     stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
0077 }
0078 EXPORT_SYMBOL(generic_fill_statx_attr);
0079 
0080 /**
0081  * vfs_getattr_nosec - getattr without security checks
0082  * @path: file to get attributes from
0083  * @stat: structure to return attributes in
0084  * @request_mask: STATX_xxx flags indicating what the caller wants
0085  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
0086  *
0087  * Get attributes without calling security_inode_getattr.
0088  *
0089  * Currently the only caller other than vfs_getattr is internal to the
0090  * filehandle lookup code, which uses only the inode number and returns no
0091  * attributes to any user.  Any other code probably wants vfs_getattr.
0092  */
0093 int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
0094               u32 request_mask, unsigned int query_flags)
0095 {
0096     struct user_namespace *mnt_userns;
0097     struct inode *inode = d_backing_inode(path->dentry);
0098 
0099     memset(stat, 0, sizeof(*stat));
0100     stat->result_mask |= STATX_BASIC_STATS;
0101     query_flags &= AT_STATX_SYNC_TYPE;
0102 
0103     /* allow the fs to override these if it really wants to */
0104     /* SB_NOATIME means filesystem supplies dummy atime value */
0105     if (inode->i_sb->s_flags & SB_NOATIME)
0106         stat->result_mask &= ~STATX_ATIME;
0107 
0108     /*
0109      * Note: If you add another clause to set an attribute flag, please
0110      * update attributes_mask below.
0111      */
0112     if (IS_AUTOMOUNT(inode))
0113         stat->attributes |= STATX_ATTR_AUTOMOUNT;
0114 
0115     if (IS_DAX(inode))
0116         stat->attributes |= STATX_ATTR_DAX;
0117 
0118     stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
0119                   STATX_ATTR_DAX);
0120 
0121     mnt_userns = mnt_user_ns(path->mnt);
0122     if (inode->i_op->getattr)
0123         return inode->i_op->getattr(mnt_userns, path, stat,
0124                         request_mask, query_flags);
0125 
0126     generic_fillattr(mnt_userns, inode, stat);
0127     return 0;
0128 }
0129 EXPORT_SYMBOL(vfs_getattr_nosec);
0130 
0131 /*
0132  * vfs_getattr - Get the enhanced basic attributes of a file
0133  * @path: The file of interest
0134  * @stat: Where to return the statistics
0135  * @request_mask: STATX_xxx flags indicating what the caller wants
0136  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
0137  *
0138  * Ask the filesystem for a file's attributes.  The caller must indicate in
0139  * request_mask and query_flags to indicate what they want.
0140  *
0141  * If the file is remote, the filesystem can be forced to update the attributes
0142  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
0143  * suppress the update by passing AT_STATX_DONT_SYNC.
0144  *
0145  * Bits must have been set in request_mask to indicate which attributes the
0146  * caller wants retrieving.  Any such attribute not requested may be returned
0147  * anyway, but the value may be approximate, and, if remote, may not have been
0148  * synchronised with the server.
0149  *
0150  * 0 will be returned on success, and a -ve error code if unsuccessful.
0151  */
0152 int vfs_getattr(const struct path *path, struct kstat *stat,
0153         u32 request_mask, unsigned int query_flags)
0154 {
0155     int retval;
0156 
0157     retval = security_inode_getattr(path);
0158     if (retval)
0159         return retval;
0160     return vfs_getattr_nosec(path, stat, request_mask, query_flags);
0161 }
0162 EXPORT_SYMBOL(vfs_getattr);
0163 
0164 /**
0165  * vfs_fstat - Get the basic attributes by file descriptor
0166  * @fd: The file descriptor referring to the file of interest
0167  * @stat: The result structure to fill in.
0168  *
0169  * This function is a wrapper around vfs_getattr().  The main difference is
0170  * that it uses a file descriptor to determine the file location.
0171  *
0172  * 0 will be returned on success, and a -ve error code if unsuccessful.
0173  */
0174 int vfs_fstat(int fd, struct kstat *stat)
0175 {
0176     struct fd f;
0177     int error;
0178 
0179     f = fdget_raw(fd);
0180     if (!f.file)
0181         return -EBADF;
0182     error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
0183     fdput(f);
0184     return error;
0185 }
0186 
0187 int getname_statx_lookup_flags(int flags)
0188 {
0189     int lookup_flags = 0;
0190 
0191     if (!(flags & AT_SYMLINK_NOFOLLOW))
0192         lookup_flags |= LOOKUP_FOLLOW;
0193     if (!(flags & AT_NO_AUTOMOUNT))
0194         lookup_flags |= LOOKUP_AUTOMOUNT;
0195     if (flags & AT_EMPTY_PATH)
0196         lookup_flags |= LOOKUP_EMPTY;
0197 
0198     return lookup_flags;
0199 }
0200 
0201 /**
0202  * vfs_statx - Get basic and extra attributes by filename
0203  * @dfd: A file descriptor representing the base dir for a relative filename
0204  * @filename: The name of the file of interest
0205  * @flags: Flags to control the query
0206  * @stat: The result structure to fill in.
0207  * @request_mask: STATX_xxx flags indicating what the caller wants
0208  *
0209  * This function is a wrapper around vfs_getattr().  The main difference is
0210  * that it uses a filename and base directory to determine the file location.
0211  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
0212  * at the given name from being referenced.
0213  *
0214  * 0 will be returned on success, and a -ve error code if unsuccessful.
0215  */
0216 static int vfs_statx(int dfd, struct filename *filename, int flags,
0217           struct kstat *stat, u32 request_mask)
0218 {
0219     struct path path;
0220     unsigned int lookup_flags = getname_statx_lookup_flags(flags);
0221     int error;
0222 
0223     if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
0224               AT_STATX_SYNC_TYPE))
0225         return -EINVAL;
0226 
0227 retry:
0228     error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
0229     if (error)
0230         goto out;
0231 
0232     error = vfs_getattr(&path, stat, request_mask, flags);
0233     stat->mnt_id = real_mount(path.mnt)->mnt_id;
0234     stat->result_mask |= STATX_MNT_ID;
0235     if (path.mnt->mnt_root == path.dentry)
0236         stat->attributes |= STATX_ATTR_MOUNT_ROOT;
0237     stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
0238     path_put(&path);
0239     if (retry_estale(error, lookup_flags)) {
0240         lookup_flags |= LOOKUP_REVAL;
0241         goto retry;
0242     }
0243 out:
0244     return error;
0245 }
0246 
0247 int vfs_fstatat(int dfd, const char __user *filename,
0248                   struct kstat *stat, int flags)
0249 {
0250     int ret;
0251     int statx_flags = flags | AT_NO_AUTOMOUNT;
0252     struct filename *name;
0253 
0254     name = getname_flags(filename, getname_statx_lookup_flags(statx_flags), NULL);
0255     ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS);
0256     putname(name);
0257 
0258     return ret;
0259 }
0260 
0261 #ifdef __ARCH_WANT_OLD_STAT
0262 
0263 /*
0264  * For backward compatibility?  Maybe this should be moved
0265  * into arch/i386 instead?
0266  */
0267 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
0268 {
0269     static int warncount = 5;
0270     struct __old_kernel_stat tmp;
0271 
0272     if (warncount > 0) {
0273         warncount--;
0274         printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
0275             current->comm);
0276     } else if (warncount < 0) {
0277         /* it's laughable, but... */
0278         warncount = 0;
0279     }
0280 
0281     memset(&tmp, 0, sizeof(struct __old_kernel_stat));
0282     tmp.st_dev = old_encode_dev(stat->dev);
0283     tmp.st_ino = stat->ino;
0284     if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
0285         return -EOVERFLOW;
0286     tmp.st_mode = stat->mode;
0287     tmp.st_nlink = stat->nlink;
0288     if (tmp.st_nlink != stat->nlink)
0289         return -EOVERFLOW;
0290     SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
0291     SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
0292     tmp.st_rdev = old_encode_dev(stat->rdev);
0293 #if BITS_PER_LONG == 32
0294     if (stat->size > MAX_NON_LFS)
0295         return -EOVERFLOW;
0296 #endif
0297     tmp.st_size = stat->size;
0298     tmp.st_atime = stat->atime.tv_sec;
0299     tmp.st_mtime = stat->mtime.tv_sec;
0300     tmp.st_ctime = stat->ctime.tv_sec;
0301     return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
0302 }
0303 
0304 SYSCALL_DEFINE2(stat, const char __user *, filename,
0305         struct __old_kernel_stat __user *, statbuf)
0306 {
0307     struct kstat stat;
0308     int error;
0309 
0310     error = vfs_stat(filename, &stat);
0311     if (error)
0312         return error;
0313 
0314     return cp_old_stat(&stat, statbuf);
0315 }
0316 
0317 SYSCALL_DEFINE2(lstat, const char __user *, filename,
0318         struct __old_kernel_stat __user *, statbuf)
0319 {
0320     struct kstat stat;
0321     int error;
0322 
0323     error = vfs_lstat(filename, &stat);
0324     if (error)
0325         return error;
0326 
0327     return cp_old_stat(&stat, statbuf);
0328 }
0329 
0330 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
0331 {
0332     struct kstat stat;
0333     int error = vfs_fstat(fd, &stat);
0334 
0335     if (!error)
0336         error = cp_old_stat(&stat, statbuf);
0337 
0338     return error;
0339 }
0340 
0341 #endif /* __ARCH_WANT_OLD_STAT */
0342 
0343 #ifdef __ARCH_WANT_NEW_STAT
0344 
0345 #if BITS_PER_LONG == 32
0346 #  define choose_32_64(a,b) a
0347 #else
0348 #  define choose_32_64(a,b) b
0349 #endif
0350 
0351 #ifndef INIT_STRUCT_STAT_PADDING
0352 #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
0353 #endif
0354 
0355 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
0356 {
0357     struct stat tmp;
0358 
0359     if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
0360         return -EOVERFLOW;
0361     if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
0362         return -EOVERFLOW;
0363 #if BITS_PER_LONG == 32
0364     if (stat->size > MAX_NON_LFS)
0365         return -EOVERFLOW;
0366 #endif
0367 
0368     INIT_STRUCT_STAT_PADDING(tmp);
0369     tmp.st_dev = new_encode_dev(stat->dev);
0370     tmp.st_ino = stat->ino;
0371     if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
0372         return -EOVERFLOW;
0373     tmp.st_mode = stat->mode;
0374     tmp.st_nlink = stat->nlink;
0375     if (tmp.st_nlink != stat->nlink)
0376         return -EOVERFLOW;
0377     SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
0378     SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
0379     tmp.st_rdev = new_encode_dev(stat->rdev);
0380     tmp.st_size = stat->size;
0381     tmp.st_atime = stat->atime.tv_sec;
0382     tmp.st_mtime = stat->mtime.tv_sec;
0383     tmp.st_ctime = stat->ctime.tv_sec;
0384 #ifdef STAT_HAVE_NSEC
0385     tmp.st_atime_nsec = stat->atime.tv_nsec;
0386     tmp.st_mtime_nsec = stat->mtime.tv_nsec;
0387     tmp.st_ctime_nsec = stat->ctime.tv_nsec;
0388 #endif
0389     tmp.st_blocks = stat->blocks;
0390     tmp.st_blksize = stat->blksize;
0391     return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
0392 }
0393 
0394 SYSCALL_DEFINE2(newstat, const char __user *, filename,
0395         struct stat __user *, statbuf)
0396 {
0397     struct kstat stat;
0398     int error = vfs_stat(filename, &stat);
0399 
0400     if (error)
0401         return error;
0402     return cp_new_stat(&stat, statbuf);
0403 }
0404 
0405 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
0406         struct stat __user *, statbuf)
0407 {
0408     struct kstat stat;
0409     int error;
0410 
0411     error = vfs_lstat(filename, &stat);
0412     if (error)
0413         return error;
0414 
0415     return cp_new_stat(&stat, statbuf);
0416 }
0417 
0418 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
0419 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
0420         struct stat __user *, statbuf, int, flag)
0421 {
0422     struct kstat stat;
0423     int error;
0424 
0425     error = vfs_fstatat(dfd, filename, &stat, flag);
0426     if (error)
0427         return error;
0428     return cp_new_stat(&stat, statbuf);
0429 }
0430 #endif
0431 
0432 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
0433 {
0434     struct kstat stat;
0435     int error = vfs_fstat(fd, &stat);
0436 
0437     if (!error)
0438         error = cp_new_stat(&stat, statbuf);
0439 
0440     return error;
0441 }
0442 #endif
0443 
0444 static int do_readlinkat(int dfd, const char __user *pathname,
0445              char __user *buf, int bufsiz)
0446 {
0447     struct path path;
0448     int error;
0449     int empty = 0;
0450     unsigned int lookup_flags = LOOKUP_EMPTY;
0451 
0452     if (bufsiz <= 0)
0453         return -EINVAL;
0454 
0455 retry:
0456     error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
0457     if (!error) {
0458         struct inode *inode = d_backing_inode(path.dentry);
0459 
0460         error = empty ? -ENOENT : -EINVAL;
0461         /*
0462          * AFS mountpoints allow readlink(2) but are not symlinks
0463          */
0464         if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
0465             error = security_inode_readlink(path.dentry);
0466             if (!error) {
0467                 touch_atime(&path);
0468                 error = vfs_readlink(path.dentry, buf, bufsiz);
0469             }
0470         }
0471         path_put(&path);
0472         if (retry_estale(error, lookup_flags)) {
0473             lookup_flags |= LOOKUP_REVAL;
0474             goto retry;
0475         }
0476     }
0477     return error;
0478 }
0479 
0480 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
0481         char __user *, buf, int, bufsiz)
0482 {
0483     return do_readlinkat(dfd, pathname, buf, bufsiz);
0484 }
0485 
0486 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
0487         int, bufsiz)
0488 {
0489     return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
0490 }
0491 
0492 
0493 /* ---------- LFS-64 ----------- */
0494 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
0495 
0496 #ifndef INIT_STRUCT_STAT64_PADDING
0497 #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
0498 #endif
0499 
0500 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
0501 {
0502     struct stat64 tmp;
0503 
0504     INIT_STRUCT_STAT64_PADDING(tmp);
0505 #ifdef CONFIG_MIPS
0506     /* mips has weird padding, so we don't get 64 bits there */
0507     tmp.st_dev = new_encode_dev(stat->dev);
0508     tmp.st_rdev = new_encode_dev(stat->rdev);
0509 #else
0510     tmp.st_dev = huge_encode_dev(stat->dev);
0511     tmp.st_rdev = huge_encode_dev(stat->rdev);
0512 #endif
0513     tmp.st_ino = stat->ino;
0514     if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
0515         return -EOVERFLOW;
0516 #ifdef STAT64_HAS_BROKEN_ST_INO
0517     tmp.__st_ino = stat->ino;
0518 #endif
0519     tmp.st_mode = stat->mode;
0520     tmp.st_nlink = stat->nlink;
0521     tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
0522     tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
0523     tmp.st_atime = stat->atime.tv_sec;
0524     tmp.st_atime_nsec = stat->atime.tv_nsec;
0525     tmp.st_mtime = stat->mtime.tv_sec;
0526     tmp.st_mtime_nsec = stat->mtime.tv_nsec;
0527     tmp.st_ctime = stat->ctime.tv_sec;
0528     tmp.st_ctime_nsec = stat->ctime.tv_nsec;
0529     tmp.st_size = stat->size;
0530     tmp.st_blocks = stat->blocks;
0531     tmp.st_blksize = stat->blksize;
0532     return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
0533 }
0534 
0535 SYSCALL_DEFINE2(stat64, const char __user *, filename,
0536         struct stat64 __user *, statbuf)
0537 {
0538     struct kstat stat;
0539     int error = vfs_stat(filename, &stat);
0540 
0541     if (!error)
0542         error = cp_new_stat64(&stat, statbuf);
0543 
0544     return error;
0545 }
0546 
0547 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
0548         struct stat64 __user *, statbuf)
0549 {
0550     struct kstat stat;
0551     int error = vfs_lstat(filename, &stat);
0552 
0553     if (!error)
0554         error = cp_new_stat64(&stat, statbuf);
0555 
0556     return error;
0557 }
0558 
0559 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
0560 {
0561     struct kstat stat;
0562     int error = vfs_fstat(fd, &stat);
0563 
0564     if (!error)
0565         error = cp_new_stat64(&stat, statbuf);
0566 
0567     return error;
0568 }
0569 
0570 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
0571         struct stat64 __user *, statbuf, int, flag)
0572 {
0573     struct kstat stat;
0574     int error;
0575 
0576     error = vfs_fstatat(dfd, filename, &stat, flag);
0577     if (error)
0578         return error;
0579     return cp_new_stat64(&stat, statbuf);
0580 }
0581 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
0582 
0583 static noinline_for_stack int
0584 cp_statx(const struct kstat *stat, struct statx __user *buffer)
0585 {
0586     struct statx tmp;
0587 
0588     memset(&tmp, 0, sizeof(tmp));
0589 
0590     tmp.stx_mask = stat->result_mask;
0591     tmp.stx_blksize = stat->blksize;
0592     tmp.stx_attributes = stat->attributes;
0593     tmp.stx_nlink = stat->nlink;
0594     tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
0595     tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
0596     tmp.stx_mode = stat->mode;
0597     tmp.stx_ino = stat->ino;
0598     tmp.stx_size = stat->size;
0599     tmp.stx_blocks = stat->blocks;
0600     tmp.stx_attributes_mask = stat->attributes_mask;
0601     tmp.stx_atime.tv_sec = stat->atime.tv_sec;
0602     tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
0603     tmp.stx_btime.tv_sec = stat->btime.tv_sec;
0604     tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
0605     tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
0606     tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
0607     tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
0608     tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
0609     tmp.stx_rdev_major = MAJOR(stat->rdev);
0610     tmp.stx_rdev_minor = MINOR(stat->rdev);
0611     tmp.stx_dev_major = MAJOR(stat->dev);
0612     tmp.stx_dev_minor = MINOR(stat->dev);
0613     tmp.stx_mnt_id = stat->mnt_id;
0614 
0615     return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
0616 }
0617 
0618 int do_statx(int dfd, struct filename *filename, unsigned int flags,
0619          unsigned int mask, struct statx __user *buffer)
0620 {
0621     struct kstat stat;
0622     int error;
0623 
0624     if (mask & STATX__RESERVED)
0625         return -EINVAL;
0626     if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
0627         return -EINVAL;
0628 
0629     error = vfs_statx(dfd, filename, flags, &stat, mask);
0630     if (error)
0631         return error;
0632 
0633     return cp_statx(&stat, buffer);
0634 }
0635 
0636 /**
0637  * sys_statx - System call to get enhanced stats
0638  * @dfd: Base directory to pathwalk from *or* fd to stat.
0639  * @filename: File to stat or "" with AT_EMPTY_PATH
0640  * @flags: AT_* flags to control pathwalk.
0641  * @mask: Parts of statx struct actually required.
0642  * @buffer: Result buffer.
0643  *
0644  * Note that fstat() can be emulated by setting dfd to the fd of interest,
0645  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
0646  */
0647 SYSCALL_DEFINE5(statx,
0648         int, dfd, const char __user *, filename, unsigned, flags,
0649         unsigned int, mask,
0650         struct statx __user *, buffer)
0651 {
0652     int ret;
0653     struct filename *name;
0654 
0655     name = getname_flags(filename, getname_statx_lookup_flags(flags), NULL);
0656     ret = do_statx(dfd, name, flags, mask, buffer);
0657     putname(name);
0658 
0659     return ret;
0660 }
0661 
0662 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
0663 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
0664 {
0665     struct compat_stat tmp;
0666 
0667     if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
0668         return -EOVERFLOW;
0669     if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
0670         return -EOVERFLOW;
0671 
0672     memset(&tmp, 0, sizeof(tmp));
0673     tmp.st_dev = new_encode_dev(stat->dev);
0674     tmp.st_ino = stat->ino;
0675     if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
0676         return -EOVERFLOW;
0677     tmp.st_mode = stat->mode;
0678     tmp.st_nlink = stat->nlink;
0679     if (tmp.st_nlink != stat->nlink)
0680         return -EOVERFLOW;
0681     SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
0682     SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
0683     tmp.st_rdev = new_encode_dev(stat->rdev);
0684     if ((u64) stat->size > MAX_NON_LFS)
0685         return -EOVERFLOW;
0686     tmp.st_size = stat->size;
0687     tmp.st_atime = stat->atime.tv_sec;
0688     tmp.st_atime_nsec = stat->atime.tv_nsec;
0689     tmp.st_mtime = stat->mtime.tv_sec;
0690     tmp.st_mtime_nsec = stat->mtime.tv_nsec;
0691     tmp.st_ctime = stat->ctime.tv_sec;
0692     tmp.st_ctime_nsec = stat->ctime.tv_nsec;
0693     tmp.st_blocks = stat->blocks;
0694     tmp.st_blksize = stat->blksize;
0695     return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
0696 }
0697 
0698 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
0699                struct compat_stat __user *, statbuf)
0700 {
0701     struct kstat stat;
0702     int error;
0703 
0704     error = vfs_stat(filename, &stat);
0705     if (error)
0706         return error;
0707     return cp_compat_stat(&stat, statbuf);
0708 }
0709 
0710 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
0711                struct compat_stat __user *, statbuf)
0712 {
0713     struct kstat stat;
0714     int error;
0715 
0716     error = vfs_lstat(filename, &stat);
0717     if (error)
0718         return error;
0719     return cp_compat_stat(&stat, statbuf);
0720 }
0721 
0722 #ifndef __ARCH_WANT_STAT64
0723 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
0724                const char __user *, filename,
0725                struct compat_stat __user *, statbuf, int, flag)
0726 {
0727     struct kstat stat;
0728     int error;
0729 
0730     error = vfs_fstatat(dfd, filename, &stat, flag);
0731     if (error)
0732         return error;
0733     return cp_compat_stat(&stat, statbuf);
0734 }
0735 #endif
0736 
0737 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
0738                struct compat_stat __user *, statbuf)
0739 {
0740     struct kstat stat;
0741     int error = vfs_fstat(fd, &stat);
0742 
0743     if (!error)
0744         error = cp_compat_stat(&stat, statbuf);
0745     return error;
0746 }
0747 #endif
0748 
0749 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
0750 void __inode_add_bytes(struct inode *inode, loff_t bytes)
0751 {
0752     inode->i_blocks += bytes >> 9;
0753     bytes &= 511;
0754     inode->i_bytes += bytes;
0755     if (inode->i_bytes >= 512) {
0756         inode->i_blocks++;
0757         inode->i_bytes -= 512;
0758     }
0759 }
0760 EXPORT_SYMBOL(__inode_add_bytes);
0761 
0762 void inode_add_bytes(struct inode *inode, loff_t bytes)
0763 {
0764     spin_lock(&inode->i_lock);
0765     __inode_add_bytes(inode, bytes);
0766     spin_unlock(&inode->i_lock);
0767 }
0768 
0769 EXPORT_SYMBOL(inode_add_bytes);
0770 
0771 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
0772 {
0773     inode->i_blocks -= bytes >> 9;
0774     bytes &= 511;
0775     if (inode->i_bytes < bytes) {
0776         inode->i_blocks--;
0777         inode->i_bytes += 512;
0778     }
0779     inode->i_bytes -= bytes;
0780 }
0781 
0782 EXPORT_SYMBOL(__inode_sub_bytes);
0783 
0784 void inode_sub_bytes(struct inode *inode, loff_t bytes)
0785 {
0786     spin_lock(&inode->i_lock);
0787     __inode_sub_bytes(inode, bytes);
0788     spin_unlock(&inode->i_lock);
0789 }
0790 
0791 EXPORT_SYMBOL(inode_sub_bytes);
0792 
0793 loff_t inode_get_bytes(struct inode *inode)
0794 {
0795     loff_t ret;
0796 
0797     spin_lock(&inode->i_lock);
0798     ret = __inode_get_bytes(inode);
0799     spin_unlock(&inode->i_lock);
0800     return ret;
0801 }
0802 
0803 EXPORT_SYMBOL(inode_get_bytes);
0804 
0805 void inode_set_bytes(struct inode *inode, loff_t bytes)
0806 {
0807     /* Caller is here responsible for sufficient locking
0808      * (ie. inode->i_lock) */
0809     inode->i_blocks = bytes >> 9;
0810     inode->i_bytes = bytes & 511;
0811 }
0812 
0813 EXPORT_SYMBOL(inode_set_bytes);