xref: /openbmc/linux/fs/stat.c (revision a1175d6b1bdaf4f74eda47ab18eb44194f9cb796)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/stat.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
82d985f8cSEric Biggers #include <linux/blkdev.h>
9630d9c47SPaul Gortmaker #include <linux/export.h>
101da177e4SLinus Torvalds #include <linux/mm.h>
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/file.h>
131da177e4SLinus Torvalds #include <linux/highuid.h>
141da177e4SLinus Torvalds #include <linux/fs.h>
151da177e4SLinus Torvalds #include <linux/namei.h>
161da177e4SLinus Torvalds #include <linux/security.h>
175b825c3aSIngo Molnar #include <linux/cred.h>
181da177e4SLinus Torvalds #include <linux/syscalls.h>
19ba52de12STheodore Ts'o #include <linux/pagemap.h>
20ac565de3SAl Viro #include <linux/compat.h>
21*a1175d6bSJeff Layton #include <linux/iversion.h>
221da177e4SLinus Torvalds 
237c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
241da177e4SLinus Torvalds #include <asm/unistd.h>
251da177e4SLinus Torvalds 
263934e36fSJens Axboe #include "internal.h"
27fa2fcf4fSMiklos Szeredi #include "mount.h"
283934e36fSJens Axboe 
29a528d35eSDavid Howells /**
30a528d35eSDavid Howells  * generic_fillattr - Fill in the basic attributes from the inode struct
310d56a451SChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
32a528d35eSDavid Howells  * @inode:	Inode to use as the source
33a528d35eSDavid Howells  * @stat:	Where to fill in the attributes
34a528d35eSDavid Howells  *
35a528d35eSDavid Howells  * Fill in the basic attributes in the kstat structure from data that's to be
36a528d35eSDavid Howells  * found on the VFS inode structure.  This is the default if no getattr inode
37a528d35eSDavid Howells  * operation is supplied.
380d56a451SChristian Brauner  *
390d56a451SChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
400d56a451SChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
410d56a451SChristian Brauner  * take care to map the inode according to @mnt_userns before filling in the
420d56a451SChristian Brauner  * uid and gid filds. On non-idmapped mounts or if permission checking is to be
430d56a451SChristian Brauner  * performed on the raw inode simply passs init_user_ns.
44a528d35eSDavid Howells  */
450d56a451SChristian Brauner void generic_fillattr(struct user_namespace *mnt_userns, struct inode *inode,
460d56a451SChristian Brauner 		      struct kstat *stat)
471da177e4SLinus Torvalds {
48a2bd096fSChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
49a2bd096fSChristian Brauner 	vfsgid_t vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
50a2bd096fSChristian Brauner 
511da177e4SLinus Torvalds 	stat->dev = inode->i_sb->s_dev;
521da177e4SLinus Torvalds 	stat->ino = inode->i_ino;
531da177e4SLinus Torvalds 	stat->mode = inode->i_mode;
541da177e4SLinus Torvalds 	stat->nlink = inode->i_nlink;
55a2bd096fSChristian Brauner 	stat->uid = vfsuid_into_kuid(vfsuid);
56a2bd096fSChristian Brauner 	stat->gid = vfsgid_into_kgid(vfsgid);
571da177e4SLinus Torvalds 	stat->rdev = inode->i_rdev;
583ddcd056SLinus Torvalds 	stat->size = i_size_read(inode);
591da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
601da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
611da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
6293407472SFabian Frederick 	stat->blksize = i_blocksize(inode);
633ddcd056SLinus Torvalds 	stat->blocks = inode->i_blocks;
64a528d35eSDavid Howells }
651da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr);
661da177e4SLinus Torvalds 
67b7a6ec52SJ. Bruce Fields /**
684f911138SAmir Goldstein  * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
694f911138SAmir Goldstein  * @inode:	Inode to use as the source
704f911138SAmir Goldstein  * @stat:	Where to fill in the attribute flags
714f911138SAmir Goldstein  *
724f911138SAmir Goldstein  * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
734f911138SAmir Goldstein  * inode that are published on i_flags and enforced by the VFS.
744f911138SAmir Goldstein  */
754f911138SAmir Goldstein void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
764f911138SAmir Goldstein {
774f911138SAmir Goldstein 	if (inode->i_flags & S_IMMUTABLE)
784f911138SAmir Goldstein 		stat->attributes |= STATX_ATTR_IMMUTABLE;
794f911138SAmir Goldstein 	if (inode->i_flags & S_APPEND)
804f911138SAmir Goldstein 		stat->attributes |= STATX_ATTR_APPEND;
814f911138SAmir Goldstein 	stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
824f911138SAmir Goldstein }
834f911138SAmir Goldstein EXPORT_SYMBOL(generic_fill_statx_attr);
844f911138SAmir Goldstein 
854f911138SAmir Goldstein /**
86b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
87b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
88b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
89a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
90f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
91b7a6ec52SJ. Bruce Fields  *
92b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
93b7a6ec52SJ. Bruce Fields  *
94b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
95a528d35eSDavid Howells  * filehandle lookup code, which uses only the inode number and returns no
96a528d35eSDavid Howells  * attributes to any user.  Any other code probably wants vfs_getattr.
97b7a6ec52SJ. Bruce Fields  */
98a528d35eSDavid Howells int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
99a528d35eSDavid Howells 		      u32 request_mask, unsigned int query_flags)
1001da177e4SLinus Torvalds {
101549c7297SChristian Brauner 	struct user_namespace *mnt_userns;
102bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
1031da177e4SLinus Torvalds 
104a528d35eSDavid Howells 	memset(stat, 0, sizeof(*stat));
105a528d35eSDavid Howells 	stat->result_mask |= STATX_BASIC_STATS;
106f2d077ffSChristoph Hellwig 	query_flags &= AT_STATX_SYNC_TYPE;
107801e5237SChristoph Hellwig 
108801e5237SChristoph Hellwig 	/* allow the fs to override these if it really wants to */
109761e28faSMiklos Szeredi 	/* SB_NOATIME means filesystem supplies dummy atime value */
110761e28faSMiklos Szeredi 	if (inode->i_sb->s_flags & SB_NOATIME)
111801e5237SChristoph Hellwig 		stat->result_mask &= ~STATX_ATIME;
1125afa7e8bSTheodore Ts'o 
1135afa7e8bSTheodore Ts'o 	/*
1145afa7e8bSTheodore Ts'o 	 * Note: If you add another clause to set an attribute flag, please
1155afa7e8bSTheodore Ts'o 	 * update attributes_mask below.
1165afa7e8bSTheodore Ts'o 	 */
117801e5237SChristoph Hellwig 	if (IS_AUTOMOUNT(inode))
118801e5237SChristoph Hellwig 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
119801e5237SChristoph Hellwig 
120712b2698SIra Weiny 	if (IS_DAX(inode))
121712b2698SIra Weiny 		stat->attributes |= STATX_ATTR_DAX;
122712b2698SIra Weiny 
1235afa7e8bSTheodore Ts'o 	stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
1245afa7e8bSTheodore Ts'o 				  STATX_ATTR_DAX);
1255afa7e8bSTheodore Ts'o 
126*a1175d6bSJeff Layton 	if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
127*a1175d6bSJeff Layton 		stat->result_mask |= STATX_CHANGE_COOKIE;
128*a1175d6bSJeff Layton 		stat->change_cookie = inode_query_iversion(inode);
129*a1175d6bSJeff Layton 	}
130*a1175d6bSJeff Layton 
131549c7297SChristian Brauner 	mnt_userns = mnt_user_ns(path->mnt);
1321da177e4SLinus Torvalds 	if (inode->i_op->getattr)
133549c7297SChristian Brauner 		return inode->i_op->getattr(mnt_userns, path, stat,
134549c7297SChristian Brauner 					    request_mask, query_flags);
1351da177e4SLinus Torvalds 
136549c7297SChristian Brauner 	generic_fillattr(mnt_userns, inode, stat);
1371da177e4SLinus Torvalds 	return 0;
1381da177e4SLinus Torvalds }
139b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
140b7a6ec52SJ. Bruce Fields 
141a528d35eSDavid Howells /*
142a528d35eSDavid Howells  * vfs_getattr - Get the enhanced basic attributes of a file
143a528d35eSDavid Howells  * @path: The file of interest
144a528d35eSDavid Howells  * @stat: Where to return the statistics
145a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
146f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
147a528d35eSDavid Howells  *
148a528d35eSDavid Howells  * Ask the filesystem for a file's attributes.  The caller must indicate in
149a528d35eSDavid Howells  * request_mask and query_flags to indicate what they want.
150a528d35eSDavid Howells  *
151a528d35eSDavid Howells  * If the file is remote, the filesystem can be forced to update the attributes
152a528d35eSDavid Howells  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
153a528d35eSDavid Howells  * suppress the update by passing AT_STATX_DONT_SYNC.
154a528d35eSDavid Howells  *
155a528d35eSDavid Howells  * Bits must have been set in request_mask to indicate which attributes the
156a528d35eSDavid Howells  * caller wants retrieving.  Any such attribute not requested may be returned
157a528d35eSDavid Howells  * anyway, but the value may be approximate, and, if remote, may not have been
158a528d35eSDavid Howells  * synchronised with the server.
159a528d35eSDavid Howells  *
160a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
161a528d35eSDavid Howells  */
162a528d35eSDavid Howells int vfs_getattr(const struct path *path, struct kstat *stat,
163a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
164b7a6ec52SJ. Bruce Fields {
165b7a6ec52SJ. Bruce Fields 	int retval;
166b7a6ec52SJ. Bruce Fields 
1673f7036a0SAl Viro 	retval = security_inode_getattr(path);
168b7a6ec52SJ. Bruce Fields 	if (retval)
169b7a6ec52SJ. Bruce Fields 		return retval;
170a528d35eSDavid Howells 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
171b7a6ec52SJ. Bruce Fields }
1721da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
1731da177e4SLinus Torvalds 
174a528d35eSDavid Howells /**
175da9aa5d9SChristoph Hellwig  * vfs_fstat - Get the basic attributes by file descriptor
176a528d35eSDavid Howells  * @fd: The file descriptor referring to the file of interest
177a528d35eSDavid Howells  * @stat: The result structure to fill in.
178a528d35eSDavid Howells  *
179a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
180a528d35eSDavid Howells  * that it uses a file descriptor to determine the file location.
181a528d35eSDavid Howells  *
182a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
183a528d35eSDavid Howells  */
184da9aa5d9SChristoph Hellwig int vfs_fstat(int fd, struct kstat *stat)
1851da177e4SLinus Torvalds {
1868c7493aaSEric Biggers 	struct fd f;
187da9aa5d9SChristoph Hellwig 	int error;
1888c7493aaSEric Biggers 
1898c7493aaSEric Biggers 	f = fdget_raw(fd);
190da9aa5d9SChristoph Hellwig 	if (!f.file)
191da9aa5d9SChristoph Hellwig 		return -EBADF;
192da9aa5d9SChristoph Hellwig 	error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
1932903ff01SAl Viro 	fdput(f);
1941da177e4SLinus Torvalds 	return error;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971b6fe6e0SStefan Roesch int getname_statx_lookup_flags(int flags)
1981b6fe6e0SStefan Roesch {
1991b6fe6e0SStefan Roesch 	int lookup_flags = 0;
2001b6fe6e0SStefan Roesch 
2011b6fe6e0SStefan Roesch 	if (!(flags & AT_SYMLINK_NOFOLLOW))
2021b6fe6e0SStefan Roesch 		lookup_flags |= LOOKUP_FOLLOW;
2031b6fe6e0SStefan Roesch 	if (!(flags & AT_NO_AUTOMOUNT))
2041b6fe6e0SStefan Roesch 		lookup_flags |= LOOKUP_AUTOMOUNT;
2051b6fe6e0SStefan Roesch 	if (flags & AT_EMPTY_PATH)
2061b6fe6e0SStefan Roesch 		lookup_flags |= LOOKUP_EMPTY;
2071b6fe6e0SStefan Roesch 
2081b6fe6e0SStefan Roesch 	return lookup_flags;
2091b6fe6e0SStefan Roesch }
2101b6fe6e0SStefan Roesch 
211a528d35eSDavid Howells /**
212a528d35eSDavid Howells  * vfs_statx - Get basic and extra attributes by filename
213a528d35eSDavid Howells  * @dfd: A file descriptor representing the base dir for a relative filename
214a528d35eSDavid Howells  * @filename: The name of the file of interest
215a528d35eSDavid Howells  * @flags: Flags to control the query
216a528d35eSDavid Howells  * @stat: The result structure to fill in.
217a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
218a528d35eSDavid Howells  *
219a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
220a528d35eSDavid Howells  * that it uses a filename and base directory to determine the file location.
221a528d35eSDavid Howells  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
222a528d35eSDavid Howells  * at the given name from being referenced.
223a528d35eSDavid Howells  *
224a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
225a528d35eSDavid Howells  */
2261b6fe6e0SStefan Roesch static int vfs_statx(int dfd, struct filename *filename, int flags,
227a528d35eSDavid Howells 	      struct kstat *stat, u32 request_mask)
2280112fc22SOleg Drokin {
2292eae7a18SChristoph Hellwig 	struct path path;
2301b6fe6e0SStefan Roesch 	unsigned int lookup_flags = getname_statx_lookup_flags(flags);
231b3f05150SChristoph Hellwig 	int error;
2320112fc22SOleg Drokin 
233b3f05150SChristoph Hellwig 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
234f2d077ffSChristoph Hellwig 		      AT_STATX_SYNC_TYPE))
235a528d35eSDavid Howells 		return -EINVAL;
236b3f05150SChristoph Hellwig 
237836fb7e7SJeff Layton retry:
2381b6fe6e0SStefan Roesch 	error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
2392eae7a18SChristoph Hellwig 	if (error)
2402eae7a18SChristoph Hellwig 		goto out;
2412eae7a18SChristoph Hellwig 
242a528d35eSDavid Howells 	error = vfs_getattr(&path, stat, request_mask, flags);
2432d985f8cSEric Biggers 
244fa2fcf4fSMiklos Szeredi 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
245fa2fcf4fSMiklos Szeredi 	stat->result_mask |= STATX_MNT_ID;
2462d985f8cSEric Biggers 
24780340fe3SMiklos Szeredi 	if (path.mnt->mnt_root == path.dentry)
24880340fe3SMiklos Szeredi 		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
24980340fe3SMiklos Szeredi 	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
2502d985f8cSEric Biggers 
2512d985f8cSEric Biggers 	/* Handle STATX_DIOALIGN for block devices. */
2522d985f8cSEric Biggers 	if (request_mask & STATX_DIOALIGN) {
2532d985f8cSEric Biggers 		struct inode *inode = d_backing_inode(path.dentry);
2542d985f8cSEric Biggers 
2552d985f8cSEric Biggers 		if (S_ISBLK(inode->i_mode))
2562d985f8cSEric Biggers 			bdev_statx_dioalign(inode, stat);
2572d985f8cSEric Biggers 	}
2582d985f8cSEric Biggers 
2592eae7a18SChristoph Hellwig 	path_put(&path);
260836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
261836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
262836fb7e7SJeff Layton 		goto retry;
263836fb7e7SJeff Layton 	}
2640112fc22SOleg Drokin out:
2650112fc22SOleg Drokin 	return error;
2660112fc22SOleg Drokin }
2672eae7a18SChristoph Hellwig 
26809f1bde4SChristoph Hellwig int vfs_fstatat(int dfd, const char __user *filename,
26909f1bde4SChristoph Hellwig 			      struct kstat *stat, int flags)
27009f1bde4SChristoph Hellwig {
2711b6fe6e0SStefan Roesch 	int ret;
2721b6fe6e0SStefan Roesch 	int statx_flags = flags | AT_NO_AUTOMOUNT;
2731b6fe6e0SStefan Roesch 	struct filename *name;
2741b6fe6e0SStefan Roesch 
2751b6fe6e0SStefan Roesch 	name = getname_flags(filename, getname_statx_lookup_flags(statx_flags), NULL);
2761b6fe6e0SStefan Roesch 	ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS);
2771b6fe6e0SStefan Roesch 	putname(name);
2781b6fe6e0SStefan Roesch 
2791b6fe6e0SStefan Roesch 	return ret;
28009f1bde4SChristoph Hellwig }
2810112fc22SOleg Drokin 
2821da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
2861da177e4SLinus Torvalds  * into arch/i386 instead?
2871da177e4SLinus Torvalds  */
2881da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	static int warncount = 5;
2911da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (warncount > 0) {
2941da177e4SLinus Torvalds 		warncount--;
2951da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
2961da177e4SLinus Torvalds 			current->comm);
2971da177e4SLinus Torvalds 	} else if (warncount < 0) {
2981da177e4SLinus Torvalds 		/* it's laughable, but... */
2991da177e4SLinus Torvalds 		warncount = 0;
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
3031da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
3041da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
305afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
306afefdbb2SDavid Howells 		return -EOVERFLOW;
3071da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3081da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3091da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3101da177e4SLinus Torvalds 		return -EOVERFLOW;
311a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
312a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
3131da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
3141da177e4SLinus Torvalds #if BITS_PER_LONG == 32
3151da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3161da177e4SLinus Torvalds 		return -EOVERFLOW;
3171da177e4SLinus Torvalds #endif
3181da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3191da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3201da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3211da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3221da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
325c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
326c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct kstat stat;
3292eae7a18SChristoph Hellwig 	int error;
3301da177e4SLinus Torvalds 
3312eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
3322eae7a18SChristoph Hellwig 	if (error)
3331da177e4SLinus Torvalds 		return error;
3342eae7a18SChristoph Hellwig 
3352eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
3361da177e4SLinus Torvalds }
337257ac264SHeiko Carstens 
338c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
339c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	struct kstat stat;
3422eae7a18SChristoph Hellwig 	int error;
3431da177e4SLinus Torvalds 
3442eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
3452eae7a18SChristoph Hellwig 	if (error)
3461da177e4SLinus Torvalds 		return error;
3472eae7a18SChristoph Hellwig 
3482eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
3491da177e4SLinus Torvalds }
350257ac264SHeiko Carstens 
351257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	struct kstat stat;
3541da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	if (!error)
3571da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	return error;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
3631da177e4SLinus Torvalds 
36482b355d1SArnd Bergmann #ifdef __ARCH_WANT_NEW_STAT
36582b355d1SArnd Bergmann 
366a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
367a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
368a52dd971SLinus Torvalds #else
369a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
370a52dd971SLinus Torvalds #endif
371a52dd971SLinus Torvalds 
3728529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
3738529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
3748529f613SLinus Torvalds #endif
3758529f613SLinus Torvalds 
3761da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct stat tmp;
3791da177e4SLinus Torvalds 
380932aba1eSMikulas Patocka 	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
381932aba1eSMikulas Patocka 		return -EOVERFLOW;
382932aba1eSMikulas Patocka 	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
3831da177e4SLinus Torvalds 		return -EOVERFLOW;
384a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
385a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3861da177e4SLinus Torvalds 		return -EOVERFLOW;
3871da177e4SLinus Torvalds #endif
3881da177e4SLinus Torvalds 
3898529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
390932aba1eSMikulas Patocka 	tmp.st_dev = new_encode_dev(stat->dev);
3911da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
392afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
393afefdbb2SDavid Howells 		return -EOVERFLOW;
3941da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3951da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3961da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3971da177e4SLinus Torvalds 		return -EOVERFLOW;
398a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
399a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
400932aba1eSMikulas Patocka 	tmp.st_rdev = new_encode_dev(stat->rdev);
4011da177e4SLinus Torvalds 	tmp.st_size = stat->size;
4021da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
4031da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
4041da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
4051da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
4061da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
4071da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
4081da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
4091da177e4SLinus Torvalds #endif
4101da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
4111da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
4121da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
415c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
416c7887325SDavid Howells 		struct stat __user *, statbuf)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	struct kstat stat;
4192eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
4201da177e4SLinus Torvalds 
4212eae7a18SChristoph Hellwig 	if (error)
4221da177e4SLinus Torvalds 		return error;
4232eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
4241da177e4SLinus Torvalds }
4255590ff0dSUlrich Drepper 
426c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
427c7887325SDavid Howells 		struct stat __user *, statbuf)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	struct kstat stat;
4302eae7a18SChristoph Hellwig 	int error;
4311da177e4SLinus Torvalds 
4322eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
4332eae7a18SChristoph Hellwig 	if (error)
4341da177e4SLinus Torvalds 		return error;
4352eae7a18SChristoph Hellwig 
4362eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
4371da177e4SLinus Torvalds }
4385590ff0dSUlrich Drepper 
4392833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
440c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
4416559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
4425590ff0dSUlrich Drepper {
4435590ff0dSUlrich Drepper 	struct kstat stat;
4440112fc22SOleg Drokin 	int error;
4455590ff0dSUlrich Drepper 
4460112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
4470112fc22SOleg Drokin 	if (error)
4485590ff0dSUlrich Drepper 		return error;
4490112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
4505590ff0dSUlrich Drepper }
451cff2b760SUlrich Drepper #endif
4525590ff0dSUlrich Drepper 
453257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct kstat stat;
4561da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	if (!error)
4591da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	return error;
4621da177e4SLinus Torvalds }
46382b355d1SArnd Bergmann #endif
4641da177e4SLinus Torvalds 
4652dae0248SDominik Brodowski static int do_readlinkat(int dfd, const char __user *pathname,
4662dae0248SDominik Brodowski 			 char __user *buf, int bufsiz)
4671da177e4SLinus Torvalds {
4682d8f3038SAl Viro 	struct path path;
4691da177e4SLinus Torvalds 	int error;
4701fa1e7f6SAndy Whitcroft 	int empty = 0;
4717955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	if (bufsiz <= 0)
4741da177e4SLinus Torvalds 		return -EINVAL;
4751da177e4SLinus Torvalds 
4767955119eSJeff Layton retry:
4777955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
4781da177e4SLinus Torvalds 	if (!error) {
479bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
4801da177e4SLinus Torvalds 
4811fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
482fd4a0edfSMiklos Szeredi 		/*
483fd4a0edfSMiklos Szeredi 		 * AFS mountpoints allow readlink(2) but are not symlinks
484fd4a0edfSMiklos Szeredi 		 */
485fd4a0edfSMiklos Szeredi 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
4862d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
4871da177e4SLinus Torvalds 			if (!error) {
48868ac1234SAl Viro 				touch_atime(&path);
489fd4a0edfSMiklos Szeredi 				error = vfs_readlink(path.dentry, buf, bufsiz);
4901da177e4SLinus Torvalds 			}
4911da177e4SLinus Torvalds 		}
4922d8f3038SAl Viro 		path_put(&path);
4937955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
4947955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
4957955119eSJeff Layton 			goto retry;
4967955119eSJeff Layton 		}
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 	return error;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5012dae0248SDominik Brodowski SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
5022dae0248SDominik Brodowski 		char __user *, buf, int, bufsiz)
5032dae0248SDominik Brodowski {
5042dae0248SDominik Brodowski 	return do_readlinkat(dfd, pathname, buf, bufsiz);
5052dae0248SDominik Brodowski }
5062dae0248SDominik Brodowski 
507002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
508002c8976SHeiko Carstens 		int, bufsiz)
5095590ff0dSUlrich Drepper {
5102dae0248SDominik Brodowski 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
5115590ff0dSUlrich Drepper }
5125590ff0dSUlrich Drepper 
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
5150753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
5161da177e4SLinus Torvalds 
5178529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
5188529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
5198529f613SLinus Torvalds #endif
5208529f613SLinus Torvalds 
5211da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	struct stat64 tmp;
5241da177e4SLinus Torvalds 
5258529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
5261da177e4SLinus Torvalds #ifdef CONFIG_MIPS
5271da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
5281da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
5291da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
5301da177e4SLinus Torvalds #else
5311da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
5321da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
5331da177e4SLinus Torvalds #endif
5341da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
535afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
536afefdbb2SDavid Howells 		return -EOVERFLOW;
5371da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
5381da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
5391da177e4SLinus Torvalds #endif
5401da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
5411da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
542a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
543a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
5441da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
5451da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
5461da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
5471da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
5481da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
5491da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
5501da177e4SLinus Torvalds 	tmp.st_size = stat->size;
5511da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
5521da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
5531da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
556c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
557c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	struct kstat stat;
5601da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (!error)
5631da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	return error;
5661da177e4SLinus Torvalds }
567257ac264SHeiko Carstens 
568c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
569c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	struct kstat stat;
5721da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	if (!error)
5751da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	return error;
5781da177e4SLinus Torvalds }
579257ac264SHeiko Carstens 
580257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	struct kstat stat;
5831da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	if (!error)
5861da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	return error;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
591c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
5926559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
593cff2b760SUlrich Drepper {
594cff2b760SUlrich Drepper 	struct kstat stat;
5950112fc22SOleg Drokin 	int error;
596cff2b760SUlrich Drepper 
5970112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
5980112fc22SOleg Drokin 	if (error)
599cff2b760SUlrich Drepper 		return error;
6000112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
601cff2b760SUlrich Drepper }
6020753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
6031da177e4SLinus Torvalds 
6046f88cc17SBijan Mottahedeh static noinline_for_stack int
60564bd7204SEric Biggers cp_statx(const struct kstat *stat, struct statx __user *buffer)
606a528d35eSDavid Howells {
60764bd7204SEric Biggers 	struct statx tmp;
608a528d35eSDavid Howells 
60964bd7204SEric Biggers 	memset(&tmp, 0, sizeof(tmp));
610a528d35eSDavid Howells 
611*a1175d6bSJeff Layton 	/* STATX_CHANGE_COOKIE is kernel-only for now */
612*a1175d6bSJeff Layton 	tmp.stx_mask = stat->result_mask & ~STATX_CHANGE_COOKIE;
61364bd7204SEric Biggers 	tmp.stx_blksize = stat->blksize;
614*a1175d6bSJeff Layton 	/* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */
615*a1175d6bSJeff Layton 	tmp.stx_attributes = stat->attributes & ~STATX_ATTR_CHANGE_MONOTONIC;
61664bd7204SEric Biggers 	tmp.stx_nlink = stat->nlink;
61764bd7204SEric Biggers 	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
61864bd7204SEric Biggers 	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
61964bd7204SEric Biggers 	tmp.stx_mode = stat->mode;
62064bd7204SEric Biggers 	tmp.stx_ino = stat->ino;
62164bd7204SEric Biggers 	tmp.stx_size = stat->size;
62264bd7204SEric Biggers 	tmp.stx_blocks = stat->blocks;
6233209f68bSDavid Howells 	tmp.stx_attributes_mask = stat->attributes_mask;
62464bd7204SEric Biggers 	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
62564bd7204SEric Biggers 	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
62664bd7204SEric Biggers 	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
62764bd7204SEric Biggers 	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
62864bd7204SEric Biggers 	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
62964bd7204SEric Biggers 	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
63064bd7204SEric Biggers 	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
63164bd7204SEric Biggers 	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
63264bd7204SEric Biggers 	tmp.stx_rdev_major = MAJOR(stat->rdev);
63364bd7204SEric Biggers 	tmp.stx_rdev_minor = MINOR(stat->rdev);
63464bd7204SEric Biggers 	tmp.stx_dev_major = MAJOR(stat->dev);
63564bd7204SEric Biggers 	tmp.stx_dev_minor = MINOR(stat->dev);
636fa2fcf4fSMiklos Szeredi 	tmp.stx_mnt_id = stat->mnt_id;
637825cf206SEric Biggers 	tmp.stx_dio_mem_align = stat->dio_mem_align;
638825cf206SEric Biggers 	tmp.stx_dio_offset_align = stat->dio_offset_align;
639a528d35eSDavid Howells 
64064bd7204SEric Biggers 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
641a528d35eSDavid Howells }
642a528d35eSDavid Howells 
6431b6fe6e0SStefan Roesch int do_statx(int dfd, struct filename *filename, unsigned int flags,
6440018784fSBijan Mottahedeh 	     unsigned int mask, struct statx __user *buffer)
6450018784fSBijan Mottahedeh {
6460018784fSBijan Mottahedeh 	struct kstat stat;
6470018784fSBijan Mottahedeh 	int error;
6480018784fSBijan Mottahedeh 
6490018784fSBijan Mottahedeh 	if (mask & STATX__RESERVED)
6500018784fSBijan Mottahedeh 		return -EINVAL;
6510018784fSBijan Mottahedeh 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
6520018784fSBijan Mottahedeh 		return -EINVAL;
6530018784fSBijan Mottahedeh 
654*a1175d6bSJeff Layton 	/* STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
655*a1175d6bSJeff Layton 	 * from userland.
656*a1175d6bSJeff Layton 	 */
657*a1175d6bSJeff Layton 	mask &= ~STATX_CHANGE_COOKIE;
658*a1175d6bSJeff Layton 
6590018784fSBijan Mottahedeh 	error = vfs_statx(dfd, filename, flags, &stat, mask);
6600018784fSBijan Mottahedeh 	if (error)
6610018784fSBijan Mottahedeh 		return error;
6620018784fSBijan Mottahedeh 
6630018784fSBijan Mottahedeh 	return cp_statx(&stat, buffer);
6640018784fSBijan Mottahedeh }
6650018784fSBijan Mottahedeh 
666a528d35eSDavid Howells /**
667a528d35eSDavid Howells  * sys_statx - System call to get enhanced stats
668a528d35eSDavid Howells  * @dfd: Base directory to pathwalk from *or* fd to stat.
6691e2f82d1SDavid Howells  * @filename: File to stat or "" with AT_EMPTY_PATH
670a528d35eSDavid Howells  * @flags: AT_* flags to control pathwalk.
671a528d35eSDavid Howells  * @mask: Parts of statx struct actually required.
672a528d35eSDavid Howells  * @buffer: Result buffer.
673a528d35eSDavid Howells  *
6741e2f82d1SDavid Howells  * Note that fstat() can be emulated by setting dfd to the fd of interest,
6751e2f82d1SDavid Howells  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
676a528d35eSDavid Howells  */
677a528d35eSDavid Howells SYSCALL_DEFINE5(statx,
678a528d35eSDavid Howells 		int, dfd, const char __user *, filename, unsigned, flags,
679a528d35eSDavid Howells 		unsigned int, mask,
680a528d35eSDavid Howells 		struct statx __user *, buffer)
681a528d35eSDavid Howells {
6821b6fe6e0SStefan Roesch 	int ret;
6831b6fe6e0SStefan Roesch 	struct filename *name;
6841b6fe6e0SStefan Roesch 
6851b6fe6e0SStefan Roesch 	name = getname_flags(filename, getname_statx_lookup_flags(flags), NULL);
6861b6fe6e0SStefan Roesch 	ret = do_statx(dfd, name, flags, mask, buffer);
6871b6fe6e0SStefan Roesch 	putname(name);
6881b6fe6e0SStefan Roesch 
6891b6fe6e0SStefan Roesch 	return ret;
690a528d35eSDavid Howells }
691a528d35eSDavid Howells 
692f18ed30dSGuo Ren #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
693ac565de3SAl Viro static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
694ac565de3SAl Viro {
695ac565de3SAl Viro 	struct compat_stat tmp;
696ac565de3SAl Viro 
697932aba1eSMikulas Patocka 	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
698932aba1eSMikulas Patocka 		return -EOVERFLOW;
699932aba1eSMikulas Patocka 	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
700ac565de3SAl Viro 		return -EOVERFLOW;
701ac565de3SAl Viro 
702ac565de3SAl Viro 	memset(&tmp, 0, sizeof(tmp));
703932aba1eSMikulas Patocka 	tmp.st_dev = new_encode_dev(stat->dev);
704ac565de3SAl Viro 	tmp.st_ino = stat->ino;
705ac565de3SAl Viro 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
706ac565de3SAl Viro 		return -EOVERFLOW;
707ac565de3SAl Viro 	tmp.st_mode = stat->mode;
708ac565de3SAl Viro 	tmp.st_nlink = stat->nlink;
709ac565de3SAl Viro 	if (tmp.st_nlink != stat->nlink)
710ac565de3SAl Viro 		return -EOVERFLOW;
711ac565de3SAl Viro 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
712ac565de3SAl Viro 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
713932aba1eSMikulas Patocka 	tmp.st_rdev = new_encode_dev(stat->rdev);
714ac565de3SAl Viro 	if ((u64) stat->size > MAX_NON_LFS)
715ac565de3SAl Viro 		return -EOVERFLOW;
716ac565de3SAl Viro 	tmp.st_size = stat->size;
717ac565de3SAl Viro 	tmp.st_atime = stat->atime.tv_sec;
718ac565de3SAl Viro 	tmp.st_atime_nsec = stat->atime.tv_nsec;
719ac565de3SAl Viro 	tmp.st_mtime = stat->mtime.tv_sec;
720ac565de3SAl Viro 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
721ac565de3SAl Viro 	tmp.st_ctime = stat->ctime.tv_sec;
722ac565de3SAl Viro 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
723ac565de3SAl Viro 	tmp.st_blocks = stat->blocks;
724ac565de3SAl Viro 	tmp.st_blksize = stat->blksize;
725ac565de3SAl Viro 	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
726ac565de3SAl Viro }
727ac565de3SAl Viro 
728ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
729ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
730ac565de3SAl Viro {
731ac565de3SAl Viro 	struct kstat stat;
732ac565de3SAl Viro 	int error;
733ac565de3SAl Viro 
734ac565de3SAl Viro 	error = vfs_stat(filename, &stat);
735ac565de3SAl Viro 	if (error)
736ac565de3SAl Viro 		return error;
737ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
738ac565de3SAl Viro }
739ac565de3SAl Viro 
740ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
741ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
742ac565de3SAl Viro {
743ac565de3SAl Viro 	struct kstat stat;
744ac565de3SAl Viro 	int error;
745ac565de3SAl Viro 
746ac565de3SAl Viro 	error = vfs_lstat(filename, &stat);
747ac565de3SAl Viro 	if (error)
748ac565de3SAl Viro 		return error;
749ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
750ac565de3SAl Viro }
751ac565de3SAl Viro 
752ac565de3SAl Viro #ifndef __ARCH_WANT_STAT64
753ac565de3SAl Viro COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
754ac565de3SAl Viro 		       const char __user *, filename,
755ac565de3SAl Viro 		       struct compat_stat __user *, statbuf, int, flag)
756ac565de3SAl Viro {
757ac565de3SAl Viro 	struct kstat stat;
758ac565de3SAl Viro 	int error;
759ac565de3SAl Viro 
760ac565de3SAl Viro 	error = vfs_fstatat(dfd, filename, &stat, flag);
761ac565de3SAl Viro 	if (error)
762ac565de3SAl Viro 		return error;
763ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
764ac565de3SAl Viro }
765ac565de3SAl Viro #endif
766ac565de3SAl Viro 
767ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
768ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
769ac565de3SAl Viro {
770ac565de3SAl Viro 	struct kstat stat;
771ac565de3SAl Viro 	int error = vfs_fstat(fd, &stat);
772ac565de3SAl Viro 
773ac565de3SAl Viro 	if (!error)
774ac565de3SAl Viro 		error = cp_compat_stat(&stat, statbuf);
775ac565de3SAl Viro 	return error;
776ac565de3SAl Viro }
777ac565de3SAl Viro #endif
778ac565de3SAl Viro 
779b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
780b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
7831da177e4SLinus Torvalds 	bytes &= 511;
7841da177e4SLinus Torvalds 	inode->i_bytes += bytes;
7851da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
7861da177e4SLinus Torvalds 		inode->i_blocks++;
7871da177e4SLinus Torvalds 		inode->i_bytes -= 512;
7881da177e4SLinus Torvalds 	}
789b462707eSDmitry Monakhov }
790eb315d2aSAl Viro EXPORT_SYMBOL(__inode_add_bytes);
791b462707eSDmitry Monakhov 
792b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
793b462707eSDmitry Monakhov {
794b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
795b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
7961da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
8001da177e4SLinus Torvalds 
8011c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
8021da177e4SLinus Torvalds {
8031da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
8041da177e4SLinus Torvalds 	bytes &= 511;
8051da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
8061da177e4SLinus Torvalds 		inode->i_blocks--;
8071da177e4SLinus Torvalds 		inode->i_bytes += 512;
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
8101c8924ebSJan Kara }
8111c8924ebSJan Kara 
8121c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
8131c8924ebSJan Kara 
8141c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
8151c8924ebSJan Kara {
8161c8924ebSJan Kara 	spin_lock(&inode->i_lock);
8171c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
8181da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds 	loff_t ret;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
828f4a8116aSJan Kara 	ret = __inode_get_bytes(inode);
8291da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
8301da177e4SLinus Torvalds 	return ret;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
8381da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
8391da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
8401da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
844