xref: /openbmc/linux/fs/stat.c (revision 2d985f8c6b91b5007a16e640bb9c038c5fb2839b)
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 
8*2d985f8cSEric 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>
211da177e4SLinus Torvalds 
227c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
231da177e4SLinus Torvalds #include <asm/unistd.h>
241da177e4SLinus Torvalds 
253934e36fSJens Axboe #include "internal.h"
26fa2fcf4fSMiklos Szeredi #include "mount.h"
273934e36fSJens Axboe 
28a528d35eSDavid Howells /**
29a528d35eSDavid Howells  * generic_fillattr - Fill in the basic attributes from the inode struct
300d56a451SChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
31a528d35eSDavid Howells  * @inode:	Inode to use as the source
32a528d35eSDavid Howells  * @stat:	Where to fill in the attributes
33a528d35eSDavid Howells  *
34a528d35eSDavid Howells  * Fill in the basic attributes in the kstat structure from data that's to be
35a528d35eSDavid Howells  * found on the VFS inode structure.  This is the default if no getattr inode
36a528d35eSDavid Howells  * operation is supplied.
370d56a451SChristian Brauner  *
380d56a451SChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
390d56a451SChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
400d56a451SChristian Brauner  * take care to map the inode according to @mnt_userns before filling in the
410d56a451SChristian Brauner  * uid and gid filds. On non-idmapped mounts or if permission checking is to be
420d56a451SChristian Brauner  * performed on the raw inode simply passs init_user_ns.
43a528d35eSDavid Howells  */
440d56a451SChristian Brauner void generic_fillattr(struct user_namespace *mnt_userns, struct inode *inode,
450d56a451SChristian Brauner 		      struct kstat *stat)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	stat->dev = inode->i_sb->s_dev;
481da177e4SLinus Torvalds 	stat->ino = inode->i_ino;
491da177e4SLinus Torvalds 	stat->mode = inode->i_mode;
501da177e4SLinus Torvalds 	stat->nlink = inode->i_nlink;
510d56a451SChristian Brauner 	stat->uid = i_uid_into_mnt(mnt_userns, inode);
520d56a451SChristian Brauner 	stat->gid = i_gid_into_mnt(mnt_userns, inode);
531da177e4SLinus Torvalds 	stat->rdev = inode->i_rdev;
543ddcd056SLinus Torvalds 	stat->size = i_size_read(inode);
551da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
561da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
571da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
5893407472SFabian Frederick 	stat->blksize = i_blocksize(inode);
593ddcd056SLinus Torvalds 	stat->blocks = inode->i_blocks;
60a528d35eSDavid Howells }
611da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr);
621da177e4SLinus Torvalds 
63b7a6ec52SJ. Bruce Fields /**
644f911138SAmir Goldstein  * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
654f911138SAmir Goldstein  * @inode:	Inode to use as the source
664f911138SAmir Goldstein  * @stat:	Where to fill in the attribute flags
674f911138SAmir Goldstein  *
684f911138SAmir Goldstein  * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
694f911138SAmir Goldstein  * inode that are published on i_flags and enforced by the VFS.
704f911138SAmir Goldstein  */
714f911138SAmir Goldstein void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
724f911138SAmir Goldstein {
734f911138SAmir Goldstein 	if (inode->i_flags & S_IMMUTABLE)
744f911138SAmir Goldstein 		stat->attributes |= STATX_ATTR_IMMUTABLE;
754f911138SAmir Goldstein 	if (inode->i_flags & S_APPEND)
764f911138SAmir Goldstein 		stat->attributes |= STATX_ATTR_APPEND;
774f911138SAmir Goldstein 	stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
784f911138SAmir Goldstein }
794f911138SAmir Goldstein EXPORT_SYMBOL(generic_fill_statx_attr);
804f911138SAmir Goldstein 
814f911138SAmir Goldstein /**
82b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
83b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
84b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
85a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
86f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
87b7a6ec52SJ. Bruce Fields  *
88b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
89b7a6ec52SJ. Bruce Fields  *
90b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
91a528d35eSDavid Howells  * filehandle lookup code, which uses only the inode number and returns no
92a528d35eSDavid Howells  * attributes to any user.  Any other code probably wants vfs_getattr.
93b7a6ec52SJ. Bruce Fields  */
94a528d35eSDavid Howells int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
95a528d35eSDavid Howells 		      u32 request_mask, unsigned int query_flags)
961da177e4SLinus Torvalds {
97549c7297SChristian Brauner 	struct user_namespace *mnt_userns;
98bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
991da177e4SLinus Torvalds 
100a528d35eSDavid Howells 	memset(stat, 0, sizeof(*stat));
101a528d35eSDavid Howells 	stat->result_mask |= STATX_BASIC_STATS;
102f2d077ffSChristoph Hellwig 	query_flags &= AT_STATX_SYNC_TYPE;
103801e5237SChristoph Hellwig 
104801e5237SChristoph Hellwig 	/* allow the fs to override these if it really wants to */
105761e28faSMiklos Szeredi 	/* SB_NOATIME means filesystem supplies dummy atime value */
106761e28faSMiklos Szeredi 	if (inode->i_sb->s_flags & SB_NOATIME)
107801e5237SChristoph Hellwig 		stat->result_mask &= ~STATX_ATIME;
1085afa7e8bSTheodore Ts'o 
1095afa7e8bSTheodore Ts'o 	/*
1105afa7e8bSTheodore Ts'o 	 * Note: If you add another clause to set an attribute flag, please
1115afa7e8bSTheodore Ts'o 	 * update attributes_mask below.
1125afa7e8bSTheodore Ts'o 	 */
113801e5237SChristoph Hellwig 	if (IS_AUTOMOUNT(inode))
114801e5237SChristoph Hellwig 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
115801e5237SChristoph Hellwig 
116712b2698SIra Weiny 	if (IS_DAX(inode))
117712b2698SIra Weiny 		stat->attributes |= STATX_ATTR_DAX;
118712b2698SIra Weiny 
1195afa7e8bSTheodore Ts'o 	stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
1205afa7e8bSTheodore Ts'o 				  STATX_ATTR_DAX);
1215afa7e8bSTheodore Ts'o 
122549c7297SChristian Brauner 	mnt_userns = mnt_user_ns(path->mnt);
1231da177e4SLinus Torvalds 	if (inode->i_op->getattr)
124549c7297SChristian Brauner 		return inode->i_op->getattr(mnt_userns, path, stat,
125549c7297SChristian Brauner 					    request_mask, query_flags);
1261da177e4SLinus Torvalds 
127549c7297SChristian Brauner 	generic_fillattr(mnt_userns, inode, stat);
1281da177e4SLinus Torvalds 	return 0;
1291da177e4SLinus Torvalds }
130b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
131b7a6ec52SJ. Bruce Fields 
132a528d35eSDavid Howells /*
133a528d35eSDavid Howells  * vfs_getattr - Get the enhanced basic attributes of a file
134a528d35eSDavid Howells  * @path: The file of interest
135a528d35eSDavid Howells  * @stat: Where to return the statistics
136a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
137f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
138a528d35eSDavid Howells  *
139a528d35eSDavid Howells  * Ask the filesystem for a file's attributes.  The caller must indicate in
140a528d35eSDavid Howells  * request_mask and query_flags to indicate what they want.
141a528d35eSDavid Howells  *
142a528d35eSDavid Howells  * If the file is remote, the filesystem can be forced to update the attributes
143a528d35eSDavid Howells  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
144a528d35eSDavid Howells  * suppress the update by passing AT_STATX_DONT_SYNC.
145a528d35eSDavid Howells  *
146a528d35eSDavid Howells  * Bits must have been set in request_mask to indicate which attributes the
147a528d35eSDavid Howells  * caller wants retrieving.  Any such attribute not requested may be returned
148a528d35eSDavid Howells  * anyway, but the value may be approximate, and, if remote, may not have been
149a528d35eSDavid Howells  * synchronised with the server.
150a528d35eSDavid Howells  *
151a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
152a528d35eSDavid Howells  */
153a528d35eSDavid Howells int vfs_getattr(const struct path *path, struct kstat *stat,
154a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
155b7a6ec52SJ. Bruce Fields {
156b7a6ec52SJ. Bruce Fields 	int retval;
157b7a6ec52SJ. Bruce Fields 
1583f7036a0SAl Viro 	retval = security_inode_getattr(path);
159b7a6ec52SJ. Bruce Fields 	if (retval)
160b7a6ec52SJ. Bruce Fields 		return retval;
161a528d35eSDavid Howells 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
162b7a6ec52SJ. Bruce Fields }
1631da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
1641da177e4SLinus Torvalds 
165a528d35eSDavid Howells /**
166da9aa5d9SChristoph Hellwig  * vfs_fstat - Get the basic attributes by file descriptor
167a528d35eSDavid Howells  * @fd: The file descriptor referring to the file of interest
168a528d35eSDavid Howells  * @stat: The result structure to fill in.
169a528d35eSDavid Howells  *
170a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
171a528d35eSDavid Howells  * that it uses a file descriptor to determine the file location.
172a528d35eSDavid Howells  *
173a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
174a528d35eSDavid Howells  */
175da9aa5d9SChristoph Hellwig int vfs_fstat(int fd, struct kstat *stat)
1761da177e4SLinus Torvalds {
1778c7493aaSEric Biggers 	struct fd f;
178da9aa5d9SChristoph Hellwig 	int error;
1798c7493aaSEric Biggers 
1808c7493aaSEric Biggers 	f = fdget_raw(fd);
181da9aa5d9SChristoph Hellwig 	if (!f.file)
182da9aa5d9SChristoph Hellwig 		return -EBADF;
183da9aa5d9SChristoph Hellwig 	error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
1842903ff01SAl Viro 	fdput(f);
1851da177e4SLinus Torvalds 	return error;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881b6fe6e0SStefan Roesch int getname_statx_lookup_flags(int flags)
1891b6fe6e0SStefan Roesch {
1901b6fe6e0SStefan Roesch 	int lookup_flags = 0;
1911b6fe6e0SStefan Roesch 
1921b6fe6e0SStefan Roesch 	if (!(flags & AT_SYMLINK_NOFOLLOW))
1931b6fe6e0SStefan Roesch 		lookup_flags |= LOOKUP_FOLLOW;
1941b6fe6e0SStefan Roesch 	if (!(flags & AT_NO_AUTOMOUNT))
1951b6fe6e0SStefan Roesch 		lookup_flags |= LOOKUP_AUTOMOUNT;
1961b6fe6e0SStefan Roesch 	if (flags & AT_EMPTY_PATH)
1971b6fe6e0SStefan Roesch 		lookup_flags |= LOOKUP_EMPTY;
1981b6fe6e0SStefan Roesch 
1991b6fe6e0SStefan Roesch 	return lookup_flags;
2001b6fe6e0SStefan Roesch }
2011b6fe6e0SStefan Roesch 
202a528d35eSDavid Howells /**
203a528d35eSDavid Howells  * vfs_statx - Get basic and extra attributes by filename
204a528d35eSDavid Howells  * @dfd: A file descriptor representing the base dir for a relative filename
205a528d35eSDavid Howells  * @filename: The name of the file of interest
206a528d35eSDavid Howells  * @flags: Flags to control the query
207a528d35eSDavid Howells  * @stat: The result structure to fill in.
208a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
209a528d35eSDavid Howells  *
210a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
211a528d35eSDavid Howells  * that it uses a filename and base directory to determine the file location.
212a528d35eSDavid Howells  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
213a528d35eSDavid Howells  * at the given name from being referenced.
214a528d35eSDavid Howells  *
215a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
216a528d35eSDavid Howells  */
2171b6fe6e0SStefan Roesch static int vfs_statx(int dfd, struct filename *filename, int flags,
218a528d35eSDavid Howells 	      struct kstat *stat, u32 request_mask)
2190112fc22SOleg Drokin {
2202eae7a18SChristoph Hellwig 	struct path path;
2211b6fe6e0SStefan Roesch 	unsigned int lookup_flags = getname_statx_lookup_flags(flags);
222b3f05150SChristoph Hellwig 	int error;
2230112fc22SOleg Drokin 
224b3f05150SChristoph Hellwig 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
225f2d077ffSChristoph Hellwig 		      AT_STATX_SYNC_TYPE))
226a528d35eSDavid Howells 		return -EINVAL;
227b3f05150SChristoph Hellwig 
228836fb7e7SJeff Layton retry:
2291b6fe6e0SStefan Roesch 	error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
2302eae7a18SChristoph Hellwig 	if (error)
2312eae7a18SChristoph Hellwig 		goto out;
2322eae7a18SChristoph Hellwig 
233a528d35eSDavid Howells 	error = vfs_getattr(&path, stat, request_mask, flags);
234*2d985f8cSEric Biggers 
235fa2fcf4fSMiklos Szeredi 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
236fa2fcf4fSMiklos Szeredi 	stat->result_mask |= STATX_MNT_ID;
237*2d985f8cSEric Biggers 
23880340fe3SMiklos Szeredi 	if (path.mnt->mnt_root == path.dentry)
23980340fe3SMiklos Szeredi 		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
24080340fe3SMiklos Szeredi 	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
241*2d985f8cSEric Biggers 
242*2d985f8cSEric Biggers 	/* Handle STATX_DIOALIGN for block devices. */
243*2d985f8cSEric Biggers 	if (request_mask & STATX_DIOALIGN) {
244*2d985f8cSEric Biggers 		struct inode *inode = d_backing_inode(path.dentry);
245*2d985f8cSEric Biggers 
246*2d985f8cSEric Biggers 		if (S_ISBLK(inode->i_mode))
247*2d985f8cSEric Biggers 			bdev_statx_dioalign(inode, stat);
248*2d985f8cSEric Biggers 	}
249*2d985f8cSEric Biggers 
2502eae7a18SChristoph Hellwig 	path_put(&path);
251836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
252836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
253836fb7e7SJeff Layton 		goto retry;
254836fb7e7SJeff Layton 	}
2550112fc22SOleg Drokin out:
2560112fc22SOleg Drokin 	return error;
2570112fc22SOleg Drokin }
2582eae7a18SChristoph Hellwig 
25909f1bde4SChristoph Hellwig int vfs_fstatat(int dfd, const char __user *filename,
26009f1bde4SChristoph Hellwig 			      struct kstat *stat, int flags)
26109f1bde4SChristoph Hellwig {
2621b6fe6e0SStefan Roesch 	int ret;
2631b6fe6e0SStefan Roesch 	int statx_flags = flags | AT_NO_AUTOMOUNT;
2641b6fe6e0SStefan Roesch 	struct filename *name;
2651b6fe6e0SStefan Roesch 
2661b6fe6e0SStefan Roesch 	name = getname_flags(filename, getname_statx_lookup_flags(statx_flags), NULL);
2671b6fe6e0SStefan Roesch 	ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS);
2681b6fe6e0SStefan Roesch 	putname(name);
2691b6fe6e0SStefan Roesch 
2701b6fe6e0SStefan Roesch 	return ret;
27109f1bde4SChristoph Hellwig }
2720112fc22SOleg Drokin 
2731da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /*
2761da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
2771da177e4SLinus Torvalds  * into arch/i386 instead?
2781da177e4SLinus Torvalds  */
2791da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	static int warncount = 5;
2821da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	if (warncount > 0) {
2851da177e4SLinus Torvalds 		warncount--;
2861da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
2871da177e4SLinus Torvalds 			current->comm);
2881da177e4SLinus Torvalds 	} else if (warncount < 0) {
2891da177e4SLinus Torvalds 		/* it's laughable, but... */
2901da177e4SLinus Torvalds 		warncount = 0;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
2941da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
2951da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
296afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
297afefdbb2SDavid Howells 		return -EOVERFLOW;
2981da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2991da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3001da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3011da177e4SLinus Torvalds 		return -EOVERFLOW;
302a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
303a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
3041da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
3051da177e4SLinus Torvalds #if BITS_PER_LONG == 32
3061da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3071da177e4SLinus Torvalds 		return -EOVERFLOW;
3081da177e4SLinus Torvalds #endif
3091da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3101da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3111da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3121da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3131da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
316c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
317c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	struct kstat stat;
3202eae7a18SChristoph Hellwig 	int error;
3211da177e4SLinus Torvalds 
3222eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
3232eae7a18SChristoph Hellwig 	if (error)
3241da177e4SLinus Torvalds 		return error;
3252eae7a18SChristoph Hellwig 
3262eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
3271da177e4SLinus Torvalds }
328257ac264SHeiko Carstens 
329c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
330c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct kstat stat;
3332eae7a18SChristoph Hellwig 	int error;
3341da177e4SLinus Torvalds 
3352eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
3362eae7a18SChristoph Hellwig 	if (error)
3371da177e4SLinus Torvalds 		return error;
3382eae7a18SChristoph Hellwig 
3392eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
3401da177e4SLinus Torvalds }
341257ac264SHeiko Carstens 
342257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct kstat stat;
3451da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	if (!error)
3481da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	return error;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
3541da177e4SLinus Torvalds 
35582b355d1SArnd Bergmann #ifdef __ARCH_WANT_NEW_STAT
35682b355d1SArnd Bergmann 
357a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
358a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
359a52dd971SLinus Torvalds #else
360a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
361a52dd971SLinus Torvalds #endif
362a52dd971SLinus Torvalds 
3638529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
3648529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
3658529f613SLinus Torvalds #endif
3668529f613SLinus Torvalds 
3671da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct stat tmp;
3701da177e4SLinus Torvalds 
371932aba1eSMikulas Patocka 	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
372932aba1eSMikulas Patocka 		return -EOVERFLOW;
373932aba1eSMikulas Patocka 	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
3741da177e4SLinus Torvalds 		return -EOVERFLOW;
375a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
376a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3771da177e4SLinus Torvalds 		return -EOVERFLOW;
3781da177e4SLinus Torvalds #endif
3791da177e4SLinus Torvalds 
3808529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
381932aba1eSMikulas Patocka 	tmp.st_dev = new_encode_dev(stat->dev);
3821da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
383afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
384afefdbb2SDavid Howells 		return -EOVERFLOW;
3851da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3861da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3871da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3881da177e4SLinus Torvalds 		return -EOVERFLOW;
389a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
390a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
391932aba1eSMikulas Patocka 	tmp.st_rdev = new_encode_dev(stat->rdev);
3921da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3931da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3941da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3951da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3961da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
3971da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3981da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3991da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
4001da177e4SLinus Torvalds #endif
4011da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
4021da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
4031da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
406c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
407c7887325SDavid Howells 		struct stat __user *, statbuf)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	struct kstat stat;
4102eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
4111da177e4SLinus Torvalds 
4122eae7a18SChristoph Hellwig 	if (error)
4131da177e4SLinus Torvalds 		return error;
4142eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
4151da177e4SLinus Torvalds }
4165590ff0dSUlrich Drepper 
417c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
418c7887325SDavid Howells 		struct stat __user *, statbuf)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	struct kstat stat;
4212eae7a18SChristoph Hellwig 	int error;
4221da177e4SLinus Torvalds 
4232eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
4242eae7a18SChristoph Hellwig 	if (error)
4251da177e4SLinus Torvalds 		return error;
4262eae7a18SChristoph Hellwig 
4272eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
4281da177e4SLinus Torvalds }
4295590ff0dSUlrich Drepper 
4302833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
431c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
4326559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
4335590ff0dSUlrich Drepper {
4345590ff0dSUlrich Drepper 	struct kstat stat;
4350112fc22SOleg Drokin 	int error;
4365590ff0dSUlrich Drepper 
4370112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
4380112fc22SOleg Drokin 	if (error)
4395590ff0dSUlrich Drepper 		return error;
4400112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
4415590ff0dSUlrich Drepper }
442cff2b760SUlrich Drepper #endif
4435590ff0dSUlrich Drepper 
444257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct kstat stat;
4471da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (!error)
4501da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	return error;
4531da177e4SLinus Torvalds }
45482b355d1SArnd Bergmann #endif
4551da177e4SLinus Torvalds 
4562dae0248SDominik Brodowski static int do_readlinkat(int dfd, const char __user *pathname,
4572dae0248SDominik Brodowski 			 char __user *buf, int bufsiz)
4581da177e4SLinus Torvalds {
4592d8f3038SAl Viro 	struct path path;
4601da177e4SLinus Torvalds 	int error;
4611fa1e7f6SAndy Whitcroft 	int empty = 0;
4627955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (bufsiz <= 0)
4651da177e4SLinus Torvalds 		return -EINVAL;
4661da177e4SLinus Torvalds 
4677955119eSJeff Layton retry:
4687955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
4691da177e4SLinus Torvalds 	if (!error) {
470bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
4711da177e4SLinus Torvalds 
4721fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
473fd4a0edfSMiklos Szeredi 		/*
474fd4a0edfSMiklos Szeredi 		 * AFS mountpoints allow readlink(2) but are not symlinks
475fd4a0edfSMiklos Szeredi 		 */
476fd4a0edfSMiklos Szeredi 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
4772d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
4781da177e4SLinus Torvalds 			if (!error) {
47968ac1234SAl Viro 				touch_atime(&path);
480fd4a0edfSMiklos Szeredi 				error = vfs_readlink(path.dentry, buf, bufsiz);
4811da177e4SLinus Torvalds 			}
4821da177e4SLinus Torvalds 		}
4832d8f3038SAl Viro 		path_put(&path);
4847955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
4857955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
4867955119eSJeff Layton 			goto retry;
4877955119eSJeff Layton 		}
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	return error;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4922dae0248SDominik Brodowski SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
4932dae0248SDominik Brodowski 		char __user *, buf, int, bufsiz)
4942dae0248SDominik Brodowski {
4952dae0248SDominik Brodowski 	return do_readlinkat(dfd, pathname, buf, bufsiz);
4962dae0248SDominik Brodowski }
4972dae0248SDominik Brodowski 
498002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
499002c8976SHeiko Carstens 		int, bufsiz)
5005590ff0dSUlrich Drepper {
5012dae0248SDominik Brodowski 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
5025590ff0dSUlrich Drepper }
5035590ff0dSUlrich Drepper 
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
5060753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
5071da177e4SLinus Torvalds 
5088529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
5098529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
5108529f613SLinus Torvalds #endif
5118529f613SLinus Torvalds 
5121da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	struct stat64 tmp;
5151da177e4SLinus Torvalds 
5168529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
5171da177e4SLinus Torvalds #ifdef CONFIG_MIPS
5181da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
5191da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
5201da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
5211da177e4SLinus Torvalds #else
5221da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
5231da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
5241da177e4SLinus Torvalds #endif
5251da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
526afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
527afefdbb2SDavid Howells 		return -EOVERFLOW;
5281da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
5291da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
5301da177e4SLinus Torvalds #endif
5311da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
5321da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
533a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
534a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
5351da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
5361da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
5371da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
5381da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
5391da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
5401da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
5411da177e4SLinus Torvalds 	tmp.st_size = stat->size;
5421da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
5431da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
5441da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
547c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
548c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct kstat stat;
5511da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	if (!error)
5541da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	return error;
5571da177e4SLinus Torvalds }
558257ac264SHeiko Carstens 
559c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
560c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	struct kstat stat;
5631da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	if (!error)
5661da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	return error;
5691da177e4SLinus Torvalds }
570257ac264SHeiko Carstens 
571257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	struct kstat stat;
5741da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	if (!error)
5771da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	return error;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
582c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
5836559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
584cff2b760SUlrich Drepper {
585cff2b760SUlrich Drepper 	struct kstat stat;
5860112fc22SOleg Drokin 	int error;
587cff2b760SUlrich Drepper 
5880112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
5890112fc22SOleg Drokin 	if (error)
590cff2b760SUlrich Drepper 		return error;
5910112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
592cff2b760SUlrich Drepper }
5930753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
5941da177e4SLinus Torvalds 
5956f88cc17SBijan Mottahedeh static noinline_for_stack int
59664bd7204SEric Biggers cp_statx(const struct kstat *stat, struct statx __user *buffer)
597a528d35eSDavid Howells {
59864bd7204SEric Biggers 	struct statx tmp;
599a528d35eSDavid Howells 
60064bd7204SEric Biggers 	memset(&tmp, 0, sizeof(tmp));
601a528d35eSDavid Howells 
60264bd7204SEric Biggers 	tmp.stx_mask = stat->result_mask;
60364bd7204SEric Biggers 	tmp.stx_blksize = stat->blksize;
60464bd7204SEric Biggers 	tmp.stx_attributes = stat->attributes;
60564bd7204SEric Biggers 	tmp.stx_nlink = stat->nlink;
60664bd7204SEric Biggers 	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
60764bd7204SEric Biggers 	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
60864bd7204SEric Biggers 	tmp.stx_mode = stat->mode;
60964bd7204SEric Biggers 	tmp.stx_ino = stat->ino;
61064bd7204SEric Biggers 	tmp.stx_size = stat->size;
61164bd7204SEric Biggers 	tmp.stx_blocks = stat->blocks;
6123209f68bSDavid Howells 	tmp.stx_attributes_mask = stat->attributes_mask;
61364bd7204SEric Biggers 	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
61464bd7204SEric Biggers 	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
61564bd7204SEric Biggers 	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
61664bd7204SEric Biggers 	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
61764bd7204SEric Biggers 	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
61864bd7204SEric Biggers 	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
61964bd7204SEric Biggers 	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
62064bd7204SEric Biggers 	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
62164bd7204SEric Biggers 	tmp.stx_rdev_major = MAJOR(stat->rdev);
62264bd7204SEric Biggers 	tmp.stx_rdev_minor = MINOR(stat->rdev);
62364bd7204SEric Biggers 	tmp.stx_dev_major = MAJOR(stat->dev);
62464bd7204SEric Biggers 	tmp.stx_dev_minor = MINOR(stat->dev);
625fa2fcf4fSMiklos Szeredi 	tmp.stx_mnt_id = stat->mnt_id;
626825cf206SEric Biggers 	tmp.stx_dio_mem_align = stat->dio_mem_align;
627825cf206SEric Biggers 	tmp.stx_dio_offset_align = stat->dio_offset_align;
628a528d35eSDavid Howells 
62964bd7204SEric Biggers 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
630a528d35eSDavid Howells }
631a528d35eSDavid Howells 
6321b6fe6e0SStefan Roesch int do_statx(int dfd, struct filename *filename, unsigned int flags,
6330018784fSBijan Mottahedeh 	     unsigned int mask, struct statx __user *buffer)
6340018784fSBijan Mottahedeh {
6350018784fSBijan Mottahedeh 	struct kstat stat;
6360018784fSBijan Mottahedeh 	int error;
6370018784fSBijan Mottahedeh 
6380018784fSBijan Mottahedeh 	if (mask & STATX__RESERVED)
6390018784fSBijan Mottahedeh 		return -EINVAL;
6400018784fSBijan Mottahedeh 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
6410018784fSBijan Mottahedeh 		return -EINVAL;
6420018784fSBijan Mottahedeh 
6430018784fSBijan Mottahedeh 	error = vfs_statx(dfd, filename, flags, &stat, mask);
6440018784fSBijan Mottahedeh 	if (error)
6450018784fSBijan Mottahedeh 		return error;
6460018784fSBijan Mottahedeh 
6470018784fSBijan Mottahedeh 	return cp_statx(&stat, buffer);
6480018784fSBijan Mottahedeh }
6490018784fSBijan Mottahedeh 
650a528d35eSDavid Howells /**
651a528d35eSDavid Howells  * sys_statx - System call to get enhanced stats
652a528d35eSDavid Howells  * @dfd: Base directory to pathwalk from *or* fd to stat.
6531e2f82d1SDavid Howells  * @filename: File to stat or "" with AT_EMPTY_PATH
654a528d35eSDavid Howells  * @flags: AT_* flags to control pathwalk.
655a528d35eSDavid Howells  * @mask: Parts of statx struct actually required.
656a528d35eSDavid Howells  * @buffer: Result buffer.
657a528d35eSDavid Howells  *
6581e2f82d1SDavid Howells  * Note that fstat() can be emulated by setting dfd to the fd of interest,
6591e2f82d1SDavid Howells  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
660a528d35eSDavid Howells  */
661a528d35eSDavid Howells SYSCALL_DEFINE5(statx,
662a528d35eSDavid Howells 		int, dfd, const char __user *, filename, unsigned, flags,
663a528d35eSDavid Howells 		unsigned int, mask,
664a528d35eSDavid Howells 		struct statx __user *, buffer)
665a528d35eSDavid Howells {
6661b6fe6e0SStefan Roesch 	int ret;
6671b6fe6e0SStefan Roesch 	struct filename *name;
6681b6fe6e0SStefan Roesch 
6691b6fe6e0SStefan Roesch 	name = getname_flags(filename, getname_statx_lookup_flags(flags), NULL);
6701b6fe6e0SStefan Roesch 	ret = do_statx(dfd, name, flags, mask, buffer);
6711b6fe6e0SStefan Roesch 	putname(name);
6721b6fe6e0SStefan Roesch 
6731b6fe6e0SStefan Roesch 	return ret;
674a528d35eSDavid Howells }
675a528d35eSDavid Howells 
676f18ed30dSGuo Ren #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
677ac565de3SAl Viro static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
678ac565de3SAl Viro {
679ac565de3SAl Viro 	struct compat_stat tmp;
680ac565de3SAl Viro 
681932aba1eSMikulas Patocka 	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
682932aba1eSMikulas Patocka 		return -EOVERFLOW;
683932aba1eSMikulas Patocka 	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
684ac565de3SAl Viro 		return -EOVERFLOW;
685ac565de3SAl Viro 
686ac565de3SAl Viro 	memset(&tmp, 0, sizeof(tmp));
687932aba1eSMikulas Patocka 	tmp.st_dev = new_encode_dev(stat->dev);
688ac565de3SAl Viro 	tmp.st_ino = stat->ino;
689ac565de3SAl Viro 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
690ac565de3SAl Viro 		return -EOVERFLOW;
691ac565de3SAl Viro 	tmp.st_mode = stat->mode;
692ac565de3SAl Viro 	tmp.st_nlink = stat->nlink;
693ac565de3SAl Viro 	if (tmp.st_nlink != stat->nlink)
694ac565de3SAl Viro 		return -EOVERFLOW;
695ac565de3SAl Viro 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
696ac565de3SAl Viro 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
697932aba1eSMikulas Patocka 	tmp.st_rdev = new_encode_dev(stat->rdev);
698ac565de3SAl Viro 	if ((u64) stat->size > MAX_NON_LFS)
699ac565de3SAl Viro 		return -EOVERFLOW;
700ac565de3SAl Viro 	tmp.st_size = stat->size;
701ac565de3SAl Viro 	tmp.st_atime = stat->atime.tv_sec;
702ac565de3SAl Viro 	tmp.st_atime_nsec = stat->atime.tv_nsec;
703ac565de3SAl Viro 	tmp.st_mtime = stat->mtime.tv_sec;
704ac565de3SAl Viro 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
705ac565de3SAl Viro 	tmp.st_ctime = stat->ctime.tv_sec;
706ac565de3SAl Viro 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
707ac565de3SAl Viro 	tmp.st_blocks = stat->blocks;
708ac565de3SAl Viro 	tmp.st_blksize = stat->blksize;
709ac565de3SAl Viro 	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
710ac565de3SAl Viro }
711ac565de3SAl Viro 
712ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
713ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
714ac565de3SAl Viro {
715ac565de3SAl Viro 	struct kstat stat;
716ac565de3SAl Viro 	int error;
717ac565de3SAl Viro 
718ac565de3SAl Viro 	error = vfs_stat(filename, &stat);
719ac565de3SAl Viro 	if (error)
720ac565de3SAl Viro 		return error;
721ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
722ac565de3SAl Viro }
723ac565de3SAl Viro 
724ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
725ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
726ac565de3SAl Viro {
727ac565de3SAl Viro 	struct kstat stat;
728ac565de3SAl Viro 	int error;
729ac565de3SAl Viro 
730ac565de3SAl Viro 	error = vfs_lstat(filename, &stat);
731ac565de3SAl Viro 	if (error)
732ac565de3SAl Viro 		return error;
733ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
734ac565de3SAl Viro }
735ac565de3SAl Viro 
736ac565de3SAl Viro #ifndef __ARCH_WANT_STAT64
737ac565de3SAl Viro COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
738ac565de3SAl Viro 		       const char __user *, filename,
739ac565de3SAl Viro 		       struct compat_stat __user *, statbuf, int, flag)
740ac565de3SAl Viro {
741ac565de3SAl Viro 	struct kstat stat;
742ac565de3SAl Viro 	int error;
743ac565de3SAl Viro 
744ac565de3SAl Viro 	error = vfs_fstatat(dfd, filename, &stat, flag);
745ac565de3SAl Viro 	if (error)
746ac565de3SAl Viro 		return error;
747ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
748ac565de3SAl Viro }
749ac565de3SAl Viro #endif
750ac565de3SAl Viro 
751ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
752ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
753ac565de3SAl Viro {
754ac565de3SAl Viro 	struct kstat stat;
755ac565de3SAl Viro 	int error = vfs_fstat(fd, &stat);
756ac565de3SAl Viro 
757ac565de3SAl Viro 	if (!error)
758ac565de3SAl Viro 		error = cp_compat_stat(&stat, statbuf);
759ac565de3SAl Viro 	return error;
760ac565de3SAl Viro }
761ac565de3SAl Viro #endif
762ac565de3SAl Viro 
763b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
764b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
7671da177e4SLinus Torvalds 	bytes &= 511;
7681da177e4SLinus Torvalds 	inode->i_bytes += bytes;
7691da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
7701da177e4SLinus Torvalds 		inode->i_blocks++;
7711da177e4SLinus Torvalds 		inode->i_bytes -= 512;
7721da177e4SLinus Torvalds 	}
773b462707eSDmitry Monakhov }
774eb315d2aSAl Viro EXPORT_SYMBOL(__inode_add_bytes);
775b462707eSDmitry Monakhov 
776b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
777b462707eSDmitry Monakhov {
778b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
779b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
7801da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
7841da177e4SLinus Torvalds 
7851c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
7861da177e4SLinus Torvalds {
7871da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
7881da177e4SLinus Torvalds 	bytes &= 511;
7891da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
7901da177e4SLinus Torvalds 		inode->i_blocks--;
7911da177e4SLinus Torvalds 		inode->i_bytes += 512;
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
7941c8924ebSJan Kara }
7951c8924ebSJan Kara 
7961c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
7971c8924ebSJan Kara 
7981c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
7991c8924ebSJan Kara {
8001c8924ebSJan Kara 	spin_lock(&inode->i_lock);
8011c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
8021da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
8081da177e4SLinus Torvalds {
8091da177e4SLinus Torvalds 	loff_t ret;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
812f4a8116aSJan Kara 	ret = __inode_get_bytes(inode);
8131da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
8141da177e4SLinus Torvalds 	return ret;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
8221da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
8231da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
8241da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
828