18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/fs/stat.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 1991, 1992  Linus Torvalds
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/export.h>
98c2ecf20Sopenharmony_ci#include <linux/mm.h>
108c2ecf20Sopenharmony_ci#include <linux/errno.h>
118c2ecf20Sopenharmony_ci#include <linux/file.h>
128c2ecf20Sopenharmony_ci#include <linux/highuid.h>
138c2ecf20Sopenharmony_ci#include <linux/fs.h>
148c2ecf20Sopenharmony_ci#include <linux/namei.h>
158c2ecf20Sopenharmony_ci#include <linux/security.h>
168c2ecf20Sopenharmony_ci#include <linux/cred.h>
178c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
188c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
198c2ecf20Sopenharmony_ci#include <linux/compat.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
228c2ecf20Sopenharmony_ci#include <asm/unistd.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "internal.h"
258c2ecf20Sopenharmony_ci#include "mount.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/**
288c2ecf20Sopenharmony_ci * generic_fillattr - Fill in the basic attributes from the inode struct
298c2ecf20Sopenharmony_ci * @inode: Inode to use as the source
308c2ecf20Sopenharmony_ci * @stat: Where to fill in the attributes
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Fill in the basic attributes in the kstat structure from data that's to be
338c2ecf20Sopenharmony_ci * found on the VFS inode structure.  This is the default if no getattr inode
348c2ecf20Sopenharmony_ci * operation is supplied.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_civoid generic_fillattr(struct inode *inode, struct kstat *stat)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	stat->dev = inode->i_sb->s_dev;
398c2ecf20Sopenharmony_ci	stat->ino = inode->i_ino;
408c2ecf20Sopenharmony_ci	stat->mode = inode->i_mode;
418c2ecf20Sopenharmony_ci	stat->nlink = inode->i_nlink;
428c2ecf20Sopenharmony_ci	stat->uid = inode->i_uid;
438c2ecf20Sopenharmony_ci	stat->gid = inode->i_gid;
448c2ecf20Sopenharmony_ci	stat->rdev = inode->i_rdev;
458c2ecf20Sopenharmony_ci	stat->size = i_size_read(inode);
468c2ecf20Sopenharmony_ci	stat->atime = inode->i_atime;
478c2ecf20Sopenharmony_ci	stat->mtime = inode->i_mtime;
488c2ecf20Sopenharmony_ci	stat->ctime = inode->i_ctime;
498c2ecf20Sopenharmony_ci	stat->blksize = i_blocksize(inode);
508c2ecf20Sopenharmony_ci	stat->blocks = inode->i_blocks;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generic_fillattr);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/**
558c2ecf20Sopenharmony_ci * vfs_getattr_nosec - getattr without security checks
568c2ecf20Sopenharmony_ci * @path: file to get attributes from
578c2ecf20Sopenharmony_ci * @stat: structure to return attributes in
588c2ecf20Sopenharmony_ci * @request_mask: STATX_xxx flags indicating what the caller wants
598c2ecf20Sopenharmony_ci * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * Get attributes without calling security_inode_getattr.
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * Currently the only caller other than vfs_getattr is internal to the
648c2ecf20Sopenharmony_ci * filehandle lookup code, which uses only the inode number and returns no
658c2ecf20Sopenharmony_ci * attributes to any user.  Any other code probably wants vfs_getattr.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ciint vfs_getattr_nosec(const struct path *path, struct kstat *stat,
688c2ecf20Sopenharmony_ci		      u32 request_mask, unsigned int query_flags)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct inode *inode = d_backing_inode(path->dentry);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	memset(stat, 0, sizeof(*stat));
738c2ecf20Sopenharmony_ci	stat->result_mask |= STATX_BASIC_STATS;
748c2ecf20Sopenharmony_ci	query_flags &= AT_STATX_SYNC_TYPE;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* allow the fs to override these if it really wants to */
778c2ecf20Sopenharmony_ci	/* SB_NOATIME means filesystem supplies dummy atime value */
788c2ecf20Sopenharmony_ci	if (inode->i_sb->s_flags & SB_NOATIME)
798c2ecf20Sopenharmony_ci		stat->result_mask &= ~STATX_ATIME;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/*
828c2ecf20Sopenharmony_ci	 * Note: If you add another clause to set an attribute flag, please
838c2ecf20Sopenharmony_ci	 * update attributes_mask below.
848c2ecf20Sopenharmony_ci	 */
858c2ecf20Sopenharmony_ci	if (IS_AUTOMOUNT(inode))
868c2ecf20Sopenharmony_ci		stat->attributes |= STATX_ATTR_AUTOMOUNT;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (IS_DAX(inode))
898c2ecf20Sopenharmony_ci		stat->attributes |= STATX_ATTR_DAX;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
928c2ecf20Sopenharmony_ci				  STATX_ATTR_DAX);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (inode->i_op->getattr)
958c2ecf20Sopenharmony_ci		return inode->i_op->getattr(path, stat, request_mask,
968c2ecf20Sopenharmony_ci					    query_flags);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	generic_fillattr(inode, stat);
998c2ecf20Sopenharmony_ci	return 0;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(vfs_getattr_nosec);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/*
1048c2ecf20Sopenharmony_ci * vfs_getattr - Get the enhanced basic attributes of a file
1058c2ecf20Sopenharmony_ci * @path: The file of interest
1068c2ecf20Sopenharmony_ci * @stat: Where to return the statistics
1078c2ecf20Sopenharmony_ci * @request_mask: STATX_xxx flags indicating what the caller wants
1088c2ecf20Sopenharmony_ci * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * Ask the filesystem for a file's attributes.  The caller must indicate in
1118c2ecf20Sopenharmony_ci * request_mask and query_flags to indicate what they want.
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * If the file is remote, the filesystem can be forced to update the attributes
1148c2ecf20Sopenharmony_ci * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
1158c2ecf20Sopenharmony_ci * suppress the update by passing AT_STATX_DONT_SYNC.
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * Bits must have been set in request_mask to indicate which attributes the
1188c2ecf20Sopenharmony_ci * caller wants retrieving.  Any such attribute not requested may be returned
1198c2ecf20Sopenharmony_ci * anyway, but the value may be approximate, and, if remote, may not have been
1208c2ecf20Sopenharmony_ci * synchronised with the server.
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * 0 will be returned on success, and a -ve error code if unsuccessful.
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_ciint vfs_getattr(const struct path *path, struct kstat *stat,
1258c2ecf20Sopenharmony_ci		u32 request_mask, unsigned int query_flags)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	int retval;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	retval = security_inode_getattr(path);
1308c2ecf20Sopenharmony_ci	if (retval)
1318c2ecf20Sopenharmony_ci		return retval;
1328c2ecf20Sopenharmony_ci	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ciEXPORT_SYMBOL(vfs_getattr);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/**
1378c2ecf20Sopenharmony_ci * vfs_fstat - Get the basic attributes by file descriptor
1388c2ecf20Sopenharmony_ci * @fd: The file descriptor referring to the file of interest
1398c2ecf20Sopenharmony_ci * @stat: The result structure to fill in.
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * This function is a wrapper around vfs_getattr().  The main difference is
1428c2ecf20Sopenharmony_ci * that it uses a file descriptor to determine the file location.
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * 0 will be returned on success, and a -ve error code if unsuccessful.
1458c2ecf20Sopenharmony_ci */
1468c2ecf20Sopenharmony_ciint vfs_fstat(int fd, struct kstat *stat)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct fd f;
1498c2ecf20Sopenharmony_ci	int error;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	f = fdget_raw(fd);
1528c2ecf20Sopenharmony_ci	if (!f.file)
1538c2ecf20Sopenharmony_ci		return -EBADF;
1548c2ecf20Sopenharmony_ci	error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
1558c2ecf20Sopenharmony_ci	fdput(f);
1568c2ecf20Sopenharmony_ci	return error;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/**
1608c2ecf20Sopenharmony_ci * vfs_statx - Get basic and extra attributes by filename
1618c2ecf20Sopenharmony_ci * @dfd: A file descriptor representing the base dir for a relative filename
1628c2ecf20Sopenharmony_ci * @filename: The name of the file of interest
1638c2ecf20Sopenharmony_ci * @flags: Flags to control the query
1648c2ecf20Sopenharmony_ci * @stat: The result structure to fill in.
1658c2ecf20Sopenharmony_ci * @request_mask: STATX_xxx flags indicating what the caller wants
1668c2ecf20Sopenharmony_ci *
1678c2ecf20Sopenharmony_ci * This function is a wrapper around vfs_getattr().  The main difference is
1688c2ecf20Sopenharmony_ci * that it uses a filename and base directory to determine the file location.
1698c2ecf20Sopenharmony_ci * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
1708c2ecf20Sopenharmony_ci * at the given name from being referenced.
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * 0 will be returned on success, and a -ve error code if unsuccessful.
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_cistatic int vfs_statx(int dfd, const char __user *filename, int flags,
1758c2ecf20Sopenharmony_ci	      struct kstat *stat, u32 request_mask)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct path path;
1788c2ecf20Sopenharmony_ci	unsigned lookup_flags = 0;
1798c2ecf20Sopenharmony_ci	int error;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
1828c2ecf20Sopenharmony_ci		      AT_STATX_SYNC_TYPE))
1838c2ecf20Sopenharmony_ci		return -EINVAL;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (!(flags & AT_SYMLINK_NOFOLLOW))
1868c2ecf20Sopenharmony_ci		lookup_flags |= LOOKUP_FOLLOW;
1878c2ecf20Sopenharmony_ci	if (!(flags & AT_NO_AUTOMOUNT))
1888c2ecf20Sopenharmony_ci		lookup_flags |= LOOKUP_AUTOMOUNT;
1898c2ecf20Sopenharmony_ci	if (flags & AT_EMPTY_PATH)
1908c2ecf20Sopenharmony_ci		lookup_flags |= LOOKUP_EMPTY;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciretry:
1938c2ecf20Sopenharmony_ci	error = user_path_at(dfd, filename, lookup_flags, &path);
1948c2ecf20Sopenharmony_ci	if (error)
1958c2ecf20Sopenharmony_ci		goto out;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	error = vfs_getattr(&path, stat, request_mask, flags);
1988c2ecf20Sopenharmony_ci	stat->mnt_id = real_mount(path.mnt)->mnt_id;
1998c2ecf20Sopenharmony_ci	stat->result_mask |= STATX_MNT_ID;
2008c2ecf20Sopenharmony_ci	if (path.mnt->mnt_root == path.dentry)
2018c2ecf20Sopenharmony_ci		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
2028c2ecf20Sopenharmony_ci	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
2038c2ecf20Sopenharmony_ci	path_put(&path);
2048c2ecf20Sopenharmony_ci	if (retry_estale(error, lookup_flags)) {
2058c2ecf20Sopenharmony_ci		lookup_flags |= LOOKUP_REVAL;
2068c2ecf20Sopenharmony_ci		goto retry;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ciout:
2098c2ecf20Sopenharmony_ci	return error;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ciint vfs_fstatat(int dfd, const char __user *filename,
2138c2ecf20Sopenharmony_ci			      struct kstat *stat, int flags)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT,
2168c2ecf20Sopenharmony_ci			 stat, STATX_BASIC_STATS);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#ifdef __ARCH_WANT_OLD_STAT
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/*
2228c2ecf20Sopenharmony_ci * For backward compatibility?  Maybe this should be moved
2238c2ecf20Sopenharmony_ci * into arch/i386 instead?
2248c2ecf20Sopenharmony_ci */
2258c2ecf20Sopenharmony_cistatic int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	static int warncount = 5;
2288c2ecf20Sopenharmony_ci	struct __old_kernel_stat tmp;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (warncount > 0) {
2318c2ecf20Sopenharmony_ci		warncount--;
2328c2ecf20Sopenharmony_ci		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
2338c2ecf20Sopenharmony_ci			current->comm);
2348c2ecf20Sopenharmony_ci	} else if (warncount < 0) {
2358c2ecf20Sopenharmony_ci		/* it's laughable, but... */
2368c2ecf20Sopenharmony_ci		warncount = 0;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
2408c2ecf20Sopenharmony_ci	tmp.st_dev = old_encode_dev(stat->dev);
2418c2ecf20Sopenharmony_ci	tmp.st_ino = stat->ino;
2428c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
2438c2ecf20Sopenharmony_ci		return -EOVERFLOW;
2448c2ecf20Sopenharmony_ci	tmp.st_mode = stat->mode;
2458c2ecf20Sopenharmony_ci	tmp.st_nlink = stat->nlink;
2468c2ecf20Sopenharmony_ci	if (tmp.st_nlink != stat->nlink)
2478c2ecf20Sopenharmony_ci		return -EOVERFLOW;
2488c2ecf20Sopenharmony_ci	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
2498c2ecf20Sopenharmony_ci	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
2508c2ecf20Sopenharmony_ci	tmp.st_rdev = old_encode_dev(stat->rdev);
2518c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 32
2528c2ecf20Sopenharmony_ci	if (stat->size > MAX_NON_LFS)
2538c2ecf20Sopenharmony_ci		return -EOVERFLOW;
2548c2ecf20Sopenharmony_ci#endif
2558c2ecf20Sopenharmony_ci	tmp.st_size = stat->size;
2568c2ecf20Sopenharmony_ci	tmp.st_atime = stat->atime.tv_sec;
2578c2ecf20Sopenharmony_ci	tmp.st_mtime = stat->mtime.tv_sec;
2588c2ecf20Sopenharmony_ci	tmp.st_ctime = stat->ctime.tv_sec;
2598c2ecf20Sopenharmony_ci	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(stat, const char __user *, filename,
2638c2ecf20Sopenharmony_ci		struct __old_kernel_stat __user *, statbuf)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	struct kstat stat;
2668c2ecf20Sopenharmony_ci	int error;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	error = vfs_stat(filename, &stat);
2698c2ecf20Sopenharmony_ci	if (error)
2708c2ecf20Sopenharmony_ci		return error;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return cp_old_stat(&stat, statbuf);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(lstat, const char __user *, filename,
2768c2ecf20Sopenharmony_ci		struct __old_kernel_stat __user *, statbuf)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct kstat stat;
2798c2ecf20Sopenharmony_ci	int error;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	error = vfs_lstat(filename, &stat);
2828c2ecf20Sopenharmony_ci	if (error)
2838c2ecf20Sopenharmony_ci		return error;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	return cp_old_stat(&stat, statbuf);
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	struct kstat stat;
2918c2ecf20Sopenharmony_ci	int error = vfs_fstat(fd, &stat);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (!error)
2948c2ecf20Sopenharmony_ci		error = cp_old_stat(&stat, statbuf);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	return error;
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci#endif /* __ARCH_WANT_OLD_STAT */
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci#ifdef __ARCH_WANT_NEW_STAT
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 32
3048c2ecf20Sopenharmony_ci#  define choose_32_64(a,b) a
3058c2ecf20Sopenharmony_ci#else
3068c2ecf20Sopenharmony_ci#  define choose_32_64(a,b) b
3078c2ecf20Sopenharmony_ci#endif
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci#ifndef INIT_STRUCT_STAT_PADDING
3108c2ecf20Sopenharmony_ci#  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
3118c2ecf20Sopenharmony_ci#endif
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	struct stat tmp;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
3188c2ecf20Sopenharmony_ci		return -EOVERFLOW;
3198c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
3208c2ecf20Sopenharmony_ci		return -EOVERFLOW;
3218c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 32
3228c2ecf20Sopenharmony_ci	if (stat->size > MAX_NON_LFS)
3238c2ecf20Sopenharmony_ci		return -EOVERFLOW;
3248c2ecf20Sopenharmony_ci#endif
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	INIT_STRUCT_STAT_PADDING(tmp);
3278c2ecf20Sopenharmony_ci	tmp.st_dev = new_encode_dev(stat->dev);
3288c2ecf20Sopenharmony_ci	tmp.st_ino = stat->ino;
3298c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
3308c2ecf20Sopenharmony_ci		return -EOVERFLOW;
3318c2ecf20Sopenharmony_ci	tmp.st_mode = stat->mode;
3328c2ecf20Sopenharmony_ci	tmp.st_nlink = stat->nlink;
3338c2ecf20Sopenharmony_ci	if (tmp.st_nlink != stat->nlink)
3348c2ecf20Sopenharmony_ci		return -EOVERFLOW;
3358c2ecf20Sopenharmony_ci	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
3368c2ecf20Sopenharmony_ci	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
3378c2ecf20Sopenharmony_ci	tmp.st_rdev = new_encode_dev(stat->rdev);
3388c2ecf20Sopenharmony_ci	tmp.st_size = stat->size;
3398c2ecf20Sopenharmony_ci	tmp.st_atime = stat->atime.tv_sec;
3408c2ecf20Sopenharmony_ci	tmp.st_mtime = stat->mtime.tv_sec;
3418c2ecf20Sopenharmony_ci	tmp.st_ctime = stat->ctime.tv_sec;
3428c2ecf20Sopenharmony_ci#ifdef STAT_HAVE_NSEC
3438c2ecf20Sopenharmony_ci	tmp.st_atime_nsec = stat->atime.tv_nsec;
3448c2ecf20Sopenharmony_ci	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3458c2ecf20Sopenharmony_ci	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3468c2ecf20Sopenharmony_ci#endif
3478c2ecf20Sopenharmony_ci	tmp.st_blocks = stat->blocks;
3488c2ecf20Sopenharmony_ci	tmp.st_blksize = stat->blksize;
3498c2ecf20Sopenharmony_ci	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(newstat, const char __user *, filename,
3538c2ecf20Sopenharmony_ci		struct stat __user *, statbuf)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	struct kstat stat;
3568c2ecf20Sopenharmony_ci	int error = vfs_stat(filename, &stat);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (error)
3598c2ecf20Sopenharmony_ci		return error;
3608c2ecf20Sopenharmony_ci	return cp_new_stat(&stat, statbuf);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(newlstat, const char __user *, filename,
3648c2ecf20Sopenharmony_ci		struct stat __user *, statbuf)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	struct kstat stat;
3678c2ecf20Sopenharmony_ci	int error;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	error = vfs_lstat(filename, &stat);
3708c2ecf20Sopenharmony_ci	if (error)
3718c2ecf20Sopenharmony_ci		return error;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	return cp_new_stat(&stat, statbuf);
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
3778c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
3788c2ecf20Sopenharmony_ci		struct stat __user *, statbuf, int, flag)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct kstat stat;
3818c2ecf20Sopenharmony_ci	int error;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	error = vfs_fstatat(dfd, filename, &stat, flag);
3848c2ecf20Sopenharmony_ci	if (error)
3858c2ecf20Sopenharmony_ci		return error;
3868c2ecf20Sopenharmony_ci	return cp_new_stat(&stat, statbuf);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci#endif
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	struct kstat stat;
3938c2ecf20Sopenharmony_ci	int error = vfs_fstat(fd, &stat);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	if (!error)
3968c2ecf20Sopenharmony_ci		error = cp_new_stat(&stat, statbuf);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return error;
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci#endif
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic int do_readlinkat(int dfd, const char __user *pathname,
4038c2ecf20Sopenharmony_ci			 char __user *buf, int bufsiz)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	struct path path;
4068c2ecf20Sopenharmony_ci	int error;
4078c2ecf20Sopenharmony_ci	int empty = 0;
4088c2ecf20Sopenharmony_ci	unsigned int lookup_flags = LOOKUP_EMPTY;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (bufsiz <= 0)
4118c2ecf20Sopenharmony_ci		return -EINVAL;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ciretry:
4148c2ecf20Sopenharmony_ci	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
4158c2ecf20Sopenharmony_ci	if (!error) {
4168c2ecf20Sopenharmony_ci		struct inode *inode = d_backing_inode(path.dentry);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		error = empty ? -ENOENT : -EINVAL;
4198c2ecf20Sopenharmony_ci		/*
4208c2ecf20Sopenharmony_ci		 * AFS mountpoints allow readlink(2) but are not symlinks
4218c2ecf20Sopenharmony_ci		 */
4228c2ecf20Sopenharmony_ci		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
4238c2ecf20Sopenharmony_ci			error = security_inode_readlink(path.dentry);
4248c2ecf20Sopenharmony_ci			if (!error) {
4258c2ecf20Sopenharmony_ci				touch_atime(&path);
4268c2ecf20Sopenharmony_ci				error = vfs_readlink(path.dentry, buf, bufsiz);
4278c2ecf20Sopenharmony_ci			}
4288c2ecf20Sopenharmony_ci		}
4298c2ecf20Sopenharmony_ci		path_put(&path);
4308c2ecf20Sopenharmony_ci		if (retry_estale(error, lookup_flags)) {
4318c2ecf20Sopenharmony_ci			lookup_flags |= LOOKUP_REVAL;
4328c2ecf20Sopenharmony_ci			goto retry;
4338c2ecf20Sopenharmony_ci		}
4348c2ecf20Sopenharmony_ci	}
4358c2ecf20Sopenharmony_ci	return error;
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
4398c2ecf20Sopenharmony_ci		char __user *, buf, int, bufsiz)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	return do_readlinkat(dfd, pathname, buf, bufsiz);
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
4458c2ecf20Sopenharmony_ci		int, bufsiz)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci/* ---------- LFS-64 ----------- */
4528c2ecf20Sopenharmony_ci#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci#ifndef INIT_STRUCT_STAT64_PADDING
4558c2ecf20Sopenharmony_ci#  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
4568c2ecf20Sopenharmony_ci#endif
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	struct stat64 tmp;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	INIT_STRUCT_STAT64_PADDING(tmp);
4638c2ecf20Sopenharmony_ci#ifdef CONFIG_MIPS
4648c2ecf20Sopenharmony_ci	/* mips has weird padding, so we don't get 64 bits there */
4658c2ecf20Sopenharmony_ci	tmp.st_dev = new_encode_dev(stat->dev);
4668c2ecf20Sopenharmony_ci	tmp.st_rdev = new_encode_dev(stat->rdev);
4678c2ecf20Sopenharmony_ci#else
4688c2ecf20Sopenharmony_ci	tmp.st_dev = huge_encode_dev(stat->dev);
4698c2ecf20Sopenharmony_ci	tmp.st_rdev = huge_encode_dev(stat->rdev);
4708c2ecf20Sopenharmony_ci#endif
4718c2ecf20Sopenharmony_ci	tmp.st_ino = stat->ino;
4728c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
4738c2ecf20Sopenharmony_ci		return -EOVERFLOW;
4748c2ecf20Sopenharmony_ci#ifdef STAT64_HAS_BROKEN_ST_INO
4758c2ecf20Sopenharmony_ci	tmp.__st_ino = stat->ino;
4768c2ecf20Sopenharmony_ci#endif
4778c2ecf20Sopenharmony_ci	tmp.st_mode = stat->mode;
4788c2ecf20Sopenharmony_ci	tmp.st_nlink = stat->nlink;
4798c2ecf20Sopenharmony_ci	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
4808c2ecf20Sopenharmony_ci	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
4818c2ecf20Sopenharmony_ci	tmp.st_atime = stat->atime.tv_sec;
4828c2ecf20Sopenharmony_ci	tmp.st_atime_nsec = stat->atime.tv_nsec;
4838c2ecf20Sopenharmony_ci	tmp.st_mtime = stat->mtime.tv_sec;
4848c2ecf20Sopenharmony_ci	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
4858c2ecf20Sopenharmony_ci	tmp.st_ctime = stat->ctime.tv_sec;
4868c2ecf20Sopenharmony_ci	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
4878c2ecf20Sopenharmony_ci	tmp.st_size = stat->size;
4888c2ecf20Sopenharmony_ci	tmp.st_blocks = stat->blocks;
4898c2ecf20Sopenharmony_ci	tmp.st_blksize = stat->blksize;
4908c2ecf20Sopenharmony_ci	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(stat64, const char __user *, filename,
4948c2ecf20Sopenharmony_ci		struct stat64 __user *, statbuf)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct kstat stat;
4978c2ecf20Sopenharmony_ci	int error = vfs_stat(filename, &stat);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	if (!error)
5008c2ecf20Sopenharmony_ci		error = cp_new_stat64(&stat, statbuf);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	return error;
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(lstat64, const char __user *, filename,
5068c2ecf20Sopenharmony_ci		struct stat64 __user *, statbuf)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct kstat stat;
5098c2ecf20Sopenharmony_ci	int error = vfs_lstat(filename, &stat);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	if (!error)
5128c2ecf20Sopenharmony_ci		error = cp_new_stat64(&stat, statbuf);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	return error;
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	struct kstat stat;
5208c2ecf20Sopenharmony_ci	int error = vfs_fstat(fd, &stat);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	if (!error)
5238c2ecf20Sopenharmony_ci		error = cp_new_stat64(&stat, statbuf);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	return error;
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
5298c2ecf20Sopenharmony_ci		struct stat64 __user *, statbuf, int, flag)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	struct kstat stat;
5328c2ecf20Sopenharmony_ci	int error;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	error = vfs_fstatat(dfd, filename, &stat, flag);
5358c2ecf20Sopenharmony_ci	if (error)
5368c2ecf20Sopenharmony_ci		return error;
5378c2ecf20Sopenharmony_ci	return cp_new_stat64(&stat, statbuf);
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_ci#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic noinline_for_stack int
5428c2ecf20Sopenharmony_cicp_statx(const struct kstat *stat, struct statx __user *buffer)
5438c2ecf20Sopenharmony_ci{
5448c2ecf20Sopenharmony_ci	struct statx tmp;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	memset(&tmp, 0, sizeof(tmp));
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	tmp.stx_mask = stat->result_mask;
5498c2ecf20Sopenharmony_ci	tmp.stx_blksize = stat->blksize;
5508c2ecf20Sopenharmony_ci	tmp.stx_attributes = stat->attributes;
5518c2ecf20Sopenharmony_ci	tmp.stx_nlink = stat->nlink;
5528c2ecf20Sopenharmony_ci	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
5538c2ecf20Sopenharmony_ci	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
5548c2ecf20Sopenharmony_ci	tmp.stx_mode = stat->mode;
5558c2ecf20Sopenharmony_ci	tmp.stx_ino = stat->ino;
5568c2ecf20Sopenharmony_ci	tmp.stx_size = stat->size;
5578c2ecf20Sopenharmony_ci	tmp.stx_blocks = stat->blocks;
5588c2ecf20Sopenharmony_ci	tmp.stx_attributes_mask = stat->attributes_mask;
5598c2ecf20Sopenharmony_ci	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
5608c2ecf20Sopenharmony_ci	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
5618c2ecf20Sopenharmony_ci	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
5628c2ecf20Sopenharmony_ci	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
5638c2ecf20Sopenharmony_ci	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
5648c2ecf20Sopenharmony_ci	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
5658c2ecf20Sopenharmony_ci	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
5668c2ecf20Sopenharmony_ci	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
5678c2ecf20Sopenharmony_ci	tmp.stx_rdev_major = MAJOR(stat->rdev);
5688c2ecf20Sopenharmony_ci	tmp.stx_rdev_minor = MINOR(stat->rdev);
5698c2ecf20Sopenharmony_ci	tmp.stx_dev_major = MAJOR(stat->dev);
5708c2ecf20Sopenharmony_ci	tmp.stx_dev_minor = MINOR(stat->dev);
5718c2ecf20Sopenharmony_ci	tmp.stx_mnt_id = stat->mnt_id;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ciint do_statx(int dfd, const char __user *filename, unsigned flags,
5778c2ecf20Sopenharmony_ci	     unsigned int mask, struct statx __user *buffer)
5788c2ecf20Sopenharmony_ci{
5798c2ecf20Sopenharmony_ci	struct kstat stat;
5808c2ecf20Sopenharmony_ci	int error;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	if (mask & STATX__RESERVED)
5838c2ecf20Sopenharmony_ci		return -EINVAL;
5848c2ecf20Sopenharmony_ci	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
5858c2ecf20Sopenharmony_ci		return -EINVAL;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	error = vfs_statx(dfd, filename, flags, &stat, mask);
5888c2ecf20Sopenharmony_ci	if (error)
5898c2ecf20Sopenharmony_ci		return error;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	return cp_statx(&stat, buffer);
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci/**
5958c2ecf20Sopenharmony_ci * sys_statx - System call to get enhanced stats
5968c2ecf20Sopenharmony_ci * @dfd: Base directory to pathwalk from *or* fd to stat.
5978c2ecf20Sopenharmony_ci * @filename: File to stat or "" with AT_EMPTY_PATH
5988c2ecf20Sopenharmony_ci * @flags: AT_* flags to control pathwalk.
5998c2ecf20Sopenharmony_ci * @mask: Parts of statx struct actually required.
6008c2ecf20Sopenharmony_ci * @buffer: Result buffer.
6018c2ecf20Sopenharmony_ci *
6028c2ecf20Sopenharmony_ci * Note that fstat() can be emulated by setting dfd to the fd of interest,
6038c2ecf20Sopenharmony_ci * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
6048c2ecf20Sopenharmony_ci */
6058c2ecf20Sopenharmony_ciSYSCALL_DEFINE5(statx,
6068c2ecf20Sopenharmony_ci		int, dfd, const char __user *, filename, unsigned, flags,
6078c2ecf20Sopenharmony_ci		unsigned int, mask,
6088c2ecf20Sopenharmony_ci		struct statx __user *, buffer)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	return do_statx(dfd, filename, flags, mask, buffer);
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
6148c2ecf20Sopenharmony_cistatic int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	struct compat_stat tmp;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
6198c2ecf20Sopenharmony_ci		return -EOVERFLOW;
6208c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
6218c2ecf20Sopenharmony_ci		return -EOVERFLOW;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	memset(&tmp, 0, sizeof(tmp));
6248c2ecf20Sopenharmony_ci	tmp.st_dev = new_encode_dev(stat->dev);
6258c2ecf20Sopenharmony_ci	tmp.st_ino = stat->ino;
6268c2ecf20Sopenharmony_ci	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
6278c2ecf20Sopenharmony_ci		return -EOVERFLOW;
6288c2ecf20Sopenharmony_ci	tmp.st_mode = stat->mode;
6298c2ecf20Sopenharmony_ci	tmp.st_nlink = stat->nlink;
6308c2ecf20Sopenharmony_ci	if (tmp.st_nlink != stat->nlink)
6318c2ecf20Sopenharmony_ci		return -EOVERFLOW;
6328c2ecf20Sopenharmony_ci	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
6338c2ecf20Sopenharmony_ci	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
6348c2ecf20Sopenharmony_ci	tmp.st_rdev = new_encode_dev(stat->rdev);
6358c2ecf20Sopenharmony_ci	if ((u64) stat->size > MAX_NON_LFS)
6368c2ecf20Sopenharmony_ci		return -EOVERFLOW;
6378c2ecf20Sopenharmony_ci	tmp.st_size = stat->size;
6388c2ecf20Sopenharmony_ci	tmp.st_atime = stat->atime.tv_sec;
6398c2ecf20Sopenharmony_ci	tmp.st_atime_nsec = stat->atime.tv_nsec;
6408c2ecf20Sopenharmony_ci	tmp.st_mtime = stat->mtime.tv_sec;
6418c2ecf20Sopenharmony_ci	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
6428c2ecf20Sopenharmony_ci	tmp.st_ctime = stat->ctime.tv_sec;
6438c2ecf20Sopenharmony_ci	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
6448c2ecf20Sopenharmony_ci	tmp.st_blocks = stat->blocks;
6458c2ecf20Sopenharmony_ci	tmp.st_blksize = stat->blksize;
6468c2ecf20Sopenharmony_ci	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
6508c2ecf20Sopenharmony_ci		       struct compat_stat __user *, statbuf)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	struct kstat stat;
6538c2ecf20Sopenharmony_ci	int error;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	error = vfs_stat(filename, &stat);
6568c2ecf20Sopenharmony_ci	if (error)
6578c2ecf20Sopenharmony_ci		return error;
6588c2ecf20Sopenharmony_ci	return cp_compat_stat(&stat, statbuf);
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
6628c2ecf20Sopenharmony_ci		       struct compat_stat __user *, statbuf)
6638c2ecf20Sopenharmony_ci{
6648c2ecf20Sopenharmony_ci	struct kstat stat;
6658c2ecf20Sopenharmony_ci	int error;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	error = vfs_lstat(filename, &stat);
6688c2ecf20Sopenharmony_ci	if (error)
6698c2ecf20Sopenharmony_ci		return error;
6708c2ecf20Sopenharmony_ci	return cp_compat_stat(&stat, statbuf);
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci#ifndef __ARCH_WANT_STAT64
6748c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
6758c2ecf20Sopenharmony_ci		       const char __user *, filename,
6768c2ecf20Sopenharmony_ci		       struct compat_stat __user *, statbuf, int, flag)
6778c2ecf20Sopenharmony_ci{
6788c2ecf20Sopenharmony_ci	struct kstat stat;
6798c2ecf20Sopenharmony_ci	int error;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	error = vfs_fstatat(dfd, filename, &stat, flag);
6828c2ecf20Sopenharmony_ci	if (error)
6838c2ecf20Sopenharmony_ci		return error;
6848c2ecf20Sopenharmony_ci	return cp_compat_stat(&stat, statbuf);
6858c2ecf20Sopenharmony_ci}
6868c2ecf20Sopenharmony_ci#endif
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
6898c2ecf20Sopenharmony_ci		       struct compat_stat __user *, statbuf)
6908c2ecf20Sopenharmony_ci{
6918c2ecf20Sopenharmony_ci	struct kstat stat;
6928c2ecf20Sopenharmony_ci	int error = vfs_fstat(fd, &stat);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	if (!error)
6958c2ecf20Sopenharmony_ci		error = cp_compat_stat(&stat, statbuf);
6968c2ecf20Sopenharmony_ci	return error;
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci#endif
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
7018c2ecf20Sopenharmony_civoid __inode_add_bytes(struct inode *inode, loff_t bytes)
7028c2ecf20Sopenharmony_ci{
7038c2ecf20Sopenharmony_ci	inode->i_blocks += bytes >> 9;
7048c2ecf20Sopenharmony_ci	bytes &= 511;
7058c2ecf20Sopenharmony_ci	inode->i_bytes += bytes;
7068c2ecf20Sopenharmony_ci	if (inode->i_bytes >= 512) {
7078c2ecf20Sopenharmony_ci		inode->i_blocks++;
7088c2ecf20Sopenharmony_ci		inode->i_bytes -= 512;
7098c2ecf20Sopenharmony_ci	}
7108c2ecf20Sopenharmony_ci}
7118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__inode_add_bytes);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_civoid inode_add_bytes(struct inode *inode, loff_t bytes)
7148c2ecf20Sopenharmony_ci{
7158c2ecf20Sopenharmony_ci	spin_lock(&inode->i_lock);
7168c2ecf20Sopenharmony_ci	__inode_add_bytes(inode, bytes);
7178c2ecf20Sopenharmony_ci	spin_unlock(&inode->i_lock);
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(inode_add_bytes);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_civoid __inode_sub_bytes(struct inode *inode, loff_t bytes)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	inode->i_blocks -= bytes >> 9;
7258c2ecf20Sopenharmony_ci	bytes &= 511;
7268c2ecf20Sopenharmony_ci	if (inode->i_bytes < bytes) {
7278c2ecf20Sopenharmony_ci		inode->i_blocks--;
7288c2ecf20Sopenharmony_ci		inode->i_bytes += 512;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci	inode->i_bytes -= bytes;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__inode_sub_bytes);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_civoid inode_sub_bytes(struct inode *inode, loff_t bytes)
7368c2ecf20Sopenharmony_ci{
7378c2ecf20Sopenharmony_ci	spin_lock(&inode->i_lock);
7388c2ecf20Sopenharmony_ci	__inode_sub_bytes(inode, bytes);
7398c2ecf20Sopenharmony_ci	spin_unlock(&inode->i_lock);
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ciEXPORT_SYMBOL(inode_sub_bytes);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ciloff_t inode_get_bytes(struct inode *inode)
7458c2ecf20Sopenharmony_ci{
7468c2ecf20Sopenharmony_ci	loff_t ret;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	spin_lock(&inode->i_lock);
7498c2ecf20Sopenharmony_ci	ret = __inode_get_bytes(inode);
7508c2ecf20Sopenharmony_ci	spin_unlock(&inode->i_lock);
7518c2ecf20Sopenharmony_ci	return ret;
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ciEXPORT_SYMBOL(inode_get_bytes);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_civoid inode_set_bytes(struct inode *inode, loff_t bytes)
7578c2ecf20Sopenharmony_ci{
7588c2ecf20Sopenharmony_ci	/* Caller is here responsible for sufficient locking
7598c2ecf20Sopenharmony_ci	 * (ie. inode->i_lock) */
7608c2ecf20Sopenharmony_ci	inode->i_blocks = bytes >> 9;
7618c2ecf20Sopenharmony_ci	inode->i_bytes = bytes & 511;
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ciEXPORT_SYMBOL(inode_set_bytes);
765