xref: /openbmc/linux/fs/stat.c (revision 549c7297717c32ee53f156cd949e055e601f67bb)
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 
8630d9c47SPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include <linux/mm.h>
101da177e4SLinus Torvalds #include <linux/errno.h>
111da177e4SLinus Torvalds #include <linux/file.h>
121da177e4SLinus Torvalds #include <linux/highuid.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/namei.h>
151da177e4SLinus Torvalds #include <linux/security.h>
165b825c3aSIngo Molnar #include <linux/cred.h>
171da177e4SLinus Torvalds #include <linux/syscalls.h>
18ba52de12STheodore Ts'o #include <linux/pagemap.h>
19ac565de3SAl Viro #include <linux/compat.h>
201da177e4SLinus Torvalds 
217c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
221da177e4SLinus Torvalds #include <asm/unistd.h>
231da177e4SLinus Torvalds 
243934e36fSJens Axboe #include "internal.h"
25fa2fcf4fSMiklos Szeredi #include "mount.h"
263934e36fSJens Axboe 
27a528d35eSDavid Howells /**
28a528d35eSDavid Howells  * generic_fillattr - Fill in the basic attributes from the inode struct
290d56a451SChristian Brauner  * @mnt_userns:	user namespace of the mount the inode was found from
30a528d35eSDavid Howells  * @inode:	Inode to use as the source
31a528d35eSDavid Howells  * @stat:	Where to fill in the attributes
32a528d35eSDavid Howells  *
33a528d35eSDavid Howells  * Fill in the basic attributes in the kstat structure from data that's to be
34a528d35eSDavid Howells  * found on the VFS inode structure.  This is the default if no getattr inode
35a528d35eSDavid Howells  * operation is supplied.
360d56a451SChristian Brauner  *
370d56a451SChristian Brauner  * If the inode has been found through an idmapped mount the user namespace of
380d56a451SChristian Brauner  * the vfsmount must be passed through @mnt_userns. This function will then
390d56a451SChristian Brauner  * take care to map the inode according to @mnt_userns before filling in the
400d56a451SChristian Brauner  * uid and gid filds. On non-idmapped mounts or if permission checking is to be
410d56a451SChristian Brauner  * performed on the raw inode simply passs init_user_ns.
42a528d35eSDavid Howells  */
430d56a451SChristian Brauner void generic_fillattr(struct user_namespace *mnt_userns, struct inode *inode,
440d56a451SChristian Brauner 		      struct kstat *stat)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	stat->dev = inode->i_sb->s_dev;
471da177e4SLinus Torvalds 	stat->ino = inode->i_ino;
481da177e4SLinus Torvalds 	stat->mode = inode->i_mode;
491da177e4SLinus Torvalds 	stat->nlink = inode->i_nlink;
500d56a451SChristian Brauner 	stat->uid = i_uid_into_mnt(mnt_userns, inode);
510d56a451SChristian Brauner 	stat->gid = i_gid_into_mnt(mnt_userns, inode);
521da177e4SLinus Torvalds 	stat->rdev = inode->i_rdev;
533ddcd056SLinus Torvalds 	stat->size = i_size_read(inode);
541da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
551da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
561da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
5793407472SFabian Frederick 	stat->blksize = i_blocksize(inode);
583ddcd056SLinus Torvalds 	stat->blocks = inode->i_blocks;
59a528d35eSDavid Howells }
601da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr);
611da177e4SLinus Torvalds 
62b7a6ec52SJ. Bruce Fields /**
63b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
64b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
65b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
66a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
67f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
68b7a6ec52SJ. Bruce Fields  *
69b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
70b7a6ec52SJ. Bruce Fields  *
71b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
72a528d35eSDavid Howells  * filehandle lookup code, which uses only the inode number and returns no
73a528d35eSDavid Howells  * attributes to any user.  Any other code probably wants vfs_getattr.
74b7a6ec52SJ. Bruce Fields  */
75a528d35eSDavid Howells int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
76a528d35eSDavid Howells 		      u32 request_mask, unsigned int query_flags)
771da177e4SLinus Torvalds {
78*549c7297SChristian Brauner 	struct user_namespace *mnt_userns;
79bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
801da177e4SLinus Torvalds 
81a528d35eSDavid Howells 	memset(stat, 0, sizeof(*stat));
82a528d35eSDavid Howells 	stat->result_mask |= STATX_BASIC_STATS;
83f2d077ffSChristoph Hellwig 	query_flags &= AT_STATX_SYNC_TYPE;
84801e5237SChristoph Hellwig 
85801e5237SChristoph Hellwig 	/* allow the fs to override these if it really wants to */
86761e28faSMiklos Szeredi 	/* SB_NOATIME means filesystem supplies dummy atime value */
87761e28faSMiklos Szeredi 	if (inode->i_sb->s_flags & SB_NOATIME)
88801e5237SChristoph Hellwig 		stat->result_mask &= ~STATX_ATIME;
89801e5237SChristoph Hellwig 	if (IS_AUTOMOUNT(inode))
90801e5237SChristoph Hellwig 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
91801e5237SChristoph Hellwig 
92712b2698SIra Weiny 	if (IS_DAX(inode))
93712b2698SIra Weiny 		stat->attributes |= STATX_ATTR_DAX;
94712b2698SIra Weiny 
95*549c7297SChristian Brauner 	mnt_userns = mnt_user_ns(path->mnt);
961da177e4SLinus Torvalds 	if (inode->i_op->getattr)
97*549c7297SChristian Brauner 		return inode->i_op->getattr(mnt_userns, path, stat,
98*549c7297SChristian Brauner 					    request_mask, query_flags);
991da177e4SLinus Torvalds 
100*549c7297SChristian Brauner 	generic_fillattr(mnt_userns, inode, stat);
1011da177e4SLinus Torvalds 	return 0;
1021da177e4SLinus Torvalds }
103b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
104b7a6ec52SJ. Bruce Fields 
105a528d35eSDavid Howells /*
106a528d35eSDavid Howells  * vfs_getattr - Get the enhanced basic attributes of a file
107a528d35eSDavid Howells  * @path: The file of interest
108a528d35eSDavid Howells  * @stat: Where to return the statistics
109a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
110f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
111a528d35eSDavid Howells  *
112a528d35eSDavid Howells  * Ask the filesystem for a file's attributes.  The caller must indicate in
113a528d35eSDavid Howells  * request_mask and query_flags to indicate what they want.
114a528d35eSDavid Howells  *
115a528d35eSDavid Howells  * If the file is remote, the filesystem can be forced to update the attributes
116a528d35eSDavid Howells  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
117a528d35eSDavid Howells  * suppress the update by passing AT_STATX_DONT_SYNC.
118a528d35eSDavid Howells  *
119a528d35eSDavid Howells  * Bits must have been set in request_mask to indicate which attributes the
120a528d35eSDavid Howells  * caller wants retrieving.  Any such attribute not requested may be returned
121a528d35eSDavid Howells  * anyway, but the value may be approximate, and, if remote, may not have been
122a528d35eSDavid Howells  * synchronised with the server.
123a528d35eSDavid Howells  *
124a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
125a528d35eSDavid Howells  */
126a528d35eSDavid Howells int vfs_getattr(const struct path *path, struct kstat *stat,
127a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
128b7a6ec52SJ. Bruce Fields {
129b7a6ec52SJ. Bruce Fields 	int retval;
130b7a6ec52SJ. Bruce Fields 
1313f7036a0SAl Viro 	retval = security_inode_getattr(path);
132b7a6ec52SJ. Bruce Fields 	if (retval)
133b7a6ec52SJ. Bruce Fields 		return retval;
134a528d35eSDavid Howells 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
135b7a6ec52SJ. Bruce Fields }
1361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
1371da177e4SLinus Torvalds 
138a528d35eSDavid Howells /**
139da9aa5d9SChristoph Hellwig  * vfs_fstat - Get the basic attributes by file descriptor
140a528d35eSDavid Howells  * @fd: The file descriptor referring to the file of interest
141a528d35eSDavid Howells  * @stat: The result structure to fill in.
142a528d35eSDavid Howells  *
143a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
144a528d35eSDavid Howells  * that it uses a file descriptor to determine the file location.
145a528d35eSDavid Howells  *
146a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
147a528d35eSDavid Howells  */
148da9aa5d9SChristoph Hellwig int vfs_fstat(int fd, struct kstat *stat)
1491da177e4SLinus Torvalds {
1508c7493aaSEric Biggers 	struct fd f;
151da9aa5d9SChristoph Hellwig 	int error;
1528c7493aaSEric Biggers 
1538c7493aaSEric Biggers 	f = fdget_raw(fd);
154da9aa5d9SChristoph Hellwig 	if (!f.file)
155da9aa5d9SChristoph Hellwig 		return -EBADF;
156da9aa5d9SChristoph Hellwig 	error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
1572903ff01SAl Viro 	fdput(f);
1581da177e4SLinus Torvalds 	return error;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
161a528d35eSDavid Howells /**
162a528d35eSDavid Howells  * vfs_statx - Get basic and extra attributes by filename
163a528d35eSDavid Howells  * @dfd: A file descriptor representing the base dir for a relative filename
164a528d35eSDavid Howells  * @filename: The name of the file of interest
165a528d35eSDavid Howells  * @flags: Flags to control the query
166a528d35eSDavid Howells  * @stat: The result structure to fill in.
167a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
168a528d35eSDavid Howells  *
169a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
170a528d35eSDavid Howells  * that it uses a filename and base directory to determine the file location.
171a528d35eSDavid Howells  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
172a528d35eSDavid Howells  * at the given name from being referenced.
173a528d35eSDavid Howells  *
174a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
175a528d35eSDavid Howells  */
17609f1bde4SChristoph Hellwig static int vfs_statx(int dfd, const char __user *filename, int flags,
177a528d35eSDavid Howells 	      struct kstat *stat, u32 request_mask)
1780112fc22SOleg Drokin {
1792eae7a18SChristoph Hellwig 	struct path path;
180b3f05150SChristoph Hellwig 	unsigned lookup_flags = 0;
181b3f05150SChristoph Hellwig 	int error;
1820112fc22SOleg Drokin 
183b3f05150SChristoph Hellwig 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
184f2d077ffSChristoph Hellwig 		      AT_STATX_SYNC_TYPE))
185a528d35eSDavid Howells 		return -EINVAL;
186b3f05150SChristoph Hellwig 
187b3f05150SChristoph Hellwig 	if (!(flags & AT_SYMLINK_NOFOLLOW))
188b3f05150SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
189b3f05150SChristoph Hellwig 	if (!(flags & AT_NO_AUTOMOUNT))
190b3f05150SChristoph Hellwig 		lookup_flags |= LOOKUP_AUTOMOUNT;
191b3f05150SChristoph Hellwig 	if (flags & AT_EMPTY_PATH)
192b3f05150SChristoph Hellwig 		lookup_flags |= LOOKUP_EMPTY;
193b3f05150SChristoph Hellwig 
194836fb7e7SJeff Layton retry:
1952eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
1962eae7a18SChristoph Hellwig 	if (error)
1972eae7a18SChristoph Hellwig 		goto out;
1982eae7a18SChristoph Hellwig 
199a528d35eSDavid Howells 	error = vfs_getattr(&path, stat, request_mask, flags);
200fa2fcf4fSMiklos Szeredi 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
201fa2fcf4fSMiklos Szeredi 	stat->result_mask |= STATX_MNT_ID;
20280340fe3SMiklos Szeredi 	if (path.mnt->mnt_root == path.dentry)
20380340fe3SMiklos Szeredi 		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
20480340fe3SMiklos Szeredi 	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
2052eae7a18SChristoph Hellwig 	path_put(&path);
206836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
207836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
208836fb7e7SJeff Layton 		goto retry;
209836fb7e7SJeff Layton 	}
2100112fc22SOleg Drokin out:
2110112fc22SOleg Drokin 	return error;
2120112fc22SOleg Drokin }
2132eae7a18SChristoph Hellwig 
21409f1bde4SChristoph Hellwig int vfs_fstatat(int dfd, const char __user *filename,
21509f1bde4SChristoph Hellwig 			      struct kstat *stat, int flags)
21609f1bde4SChristoph Hellwig {
21709f1bde4SChristoph Hellwig 	return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT,
21809f1bde4SChristoph Hellwig 			 stat, STATX_BASIC_STATS);
21909f1bde4SChristoph Hellwig }
2200112fc22SOleg Drokin 
2211da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /*
2241da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
2251da177e4SLinus Torvalds  * into arch/i386 instead?
2261da177e4SLinus Torvalds  */
2271da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	static int warncount = 5;
2301da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	if (warncount > 0) {
2331da177e4SLinus Torvalds 		warncount--;
2341da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
2351da177e4SLinus Torvalds 			current->comm);
2361da177e4SLinus Torvalds 	} else if (warncount < 0) {
2371da177e4SLinus Torvalds 		/* it's laughable, but... */
2381da177e4SLinus Torvalds 		warncount = 0;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
2421da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
2431da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
244afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
245afefdbb2SDavid Howells 		return -EOVERFLOW;
2461da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2471da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2481da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2491da177e4SLinus Torvalds 		return -EOVERFLOW;
250a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
251a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
2521da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
2531da177e4SLinus Torvalds #if BITS_PER_LONG == 32
2541da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2551da177e4SLinus Torvalds 		return -EOVERFLOW;
2561da177e4SLinus Torvalds #endif
2571da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2581da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2591da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2601da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2611da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
264c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
265c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	struct kstat stat;
2682eae7a18SChristoph Hellwig 	int error;
2691da177e4SLinus Torvalds 
2702eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
2712eae7a18SChristoph Hellwig 	if (error)
2721da177e4SLinus Torvalds 		return error;
2732eae7a18SChristoph Hellwig 
2742eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2751da177e4SLinus Torvalds }
276257ac264SHeiko Carstens 
277c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
278c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct kstat stat;
2812eae7a18SChristoph Hellwig 	int error;
2821da177e4SLinus Torvalds 
2832eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
2842eae7a18SChristoph Hellwig 	if (error)
2851da177e4SLinus Torvalds 		return error;
2862eae7a18SChristoph Hellwig 
2872eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2881da177e4SLinus Torvalds }
289257ac264SHeiko Carstens 
290257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct kstat stat;
2931da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (!error)
2961da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	return error;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
3021da177e4SLinus Torvalds 
30382b355d1SArnd Bergmann #ifdef __ARCH_WANT_NEW_STAT
30482b355d1SArnd Bergmann 
305a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
306a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
307a52dd971SLinus Torvalds #else
308a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
309a52dd971SLinus Torvalds #endif
310a52dd971SLinus Torvalds 
3114c416f42SYaowei Bai #define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
312a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
313a52dd971SLinus Torvalds 
3148529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
3158529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
3168529f613SLinus Torvalds #endif
3178529f613SLinus Torvalds 
3181da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	struct stat tmp;
3211da177e4SLinus Torvalds 
322a52dd971SLinus Torvalds 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
3231da177e4SLinus Torvalds 		return -EOVERFLOW;
324a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
325a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3261da177e4SLinus Torvalds 		return -EOVERFLOW;
3271da177e4SLinus Torvalds #endif
3281da177e4SLinus Torvalds 
3298529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
330a52dd971SLinus Torvalds 	tmp.st_dev = encode_dev(stat->dev);
3311da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
332afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
333afefdbb2SDavid Howells 		return -EOVERFLOW;
3341da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3351da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3361da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3371da177e4SLinus Torvalds 		return -EOVERFLOW;
338a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
339a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
340a52dd971SLinus Torvalds 	tmp.st_rdev = encode_dev(stat->rdev);
3411da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3421da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3431da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3441da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3451da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
3461da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3471da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3481da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3491da177e4SLinus Torvalds #endif
3501da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3511da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3521da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
355c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
356c7887325SDavid Howells 		struct stat __user *, statbuf)
3571da177e4SLinus Torvalds {
3581da177e4SLinus Torvalds 	struct kstat stat;
3592eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
3601da177e4SLinus Torvalds 
3612eae7a18SChristoph Hellwig 	if (error)
3621da177e4SLinus Torvalds 		return error;
3632eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
3641da177e4SLinus Torvalds }
3655590ff0dSUlrich Drepper 
366c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
367c7887325SDavid Howells 		struct stat __user *, statbuf)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct kstat stat;
3702eae7a18SChristoph Hellwig 	int error;
3711da177e4SLinus Torvalds 
3722eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
3732eae7a18SChristoph Hellwig 	if (error)
3741da177e4SLinus Torvalds 		return error;
3752eae7a18SChristoph Hellwig 
3762eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
3771da177e4SLinus Torvalds }
3785590ff0dSUlrich Drepper 
3792833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
380c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
3816559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
3825590ff0dSUlrich Drepper {
3835590ff0dSUlrich Drepper 	struct kstat stat;
3840112fc22SOleg Drokin 	int error;
3855590ff0dSUlrich Drepper 
3860112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
3870112fc22SOleg Drokin 	if (error)
3885590ff0dSUlrich Drepper 		return error;
3890112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
3905590ff0dSUlrich Drepper }
391cff2b760SUlrich Drepper #endif
3925590ff0dSUlrich Drepper 
393257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	struct kstat stat;
3961da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	if (!error)
3991da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	return error;
4021da177e4SLinus Torvalds }
40382b355d1SArnd Bergmann #endif
4041da177e4SLinus Torvalds 
4052dae0248SDominik Brodowski static int do_readlinkat(int dfd, const char __user *pathname,
4062dae0248SDominik Brodowski 			 char __user *buf, int bufsiz)
4071da177e4SLinus Torvalds {
4082d8f3038SAl Viro 	struct path path;
4091da177e4SLinus Torvalds 	int error;
4101fa1e7f6SAndy Whitcroft 	int empty = 0;
4117955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	if (bufsiz <= 0)
4141da177e4SLinus Torvalds 		return -EINVAL;
4151da177e4SLinus Torvalds 
4167955119eSJeff Layton retry:
4177955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
4181da177e4SLinus Torvalds 	if (!error) {
419bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
4201da177e4SLinus Torvalds 
4211fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
422fd4a0edfSMiklos Szeredi 		/*
423fd4a0edfSMiklos Szeredi 		 * AFS mountpoints allow readlink(2) but are not symlinks
424fd4a0edfSMiklos Szeredi 		 */
425fd4a0edfSMiklos Szeredi 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
4262d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
4271da177e4SLinus Torvalds 			if (!error) {
42868ac1234SAl Viro 				touch_atime(&path);
429fd4a0edfSMiklos Szeredi 				error = vfs_readlink(path.dentry, buf, bufsiz);
4301da177e4SLinus Torvalds 			}
4311da177e4SLinus Torvalds 		}
4322d8f3038SAl Viro 		path_put(&path);
4337955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
4347955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
4357955119eSJeff Layton 			goto retry;
4367955119eSJeff Layton 		}
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 	return error;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4412dae0248SDominik Brodowski SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
4422dae0248SDominik Brodowski 		char __user *, buf, int, bufsiz)
4432dae0248SDominik Brodowski {
4442dae0248SDominik Brodowski 	return do_readlinkat(dfd, pathname, buf, bufsiz);
4452dae0248SDominik Brodowski }
4462dae0248SDominik Brodowski 
447002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
448002c8976SHeiko Carstens 		int, bufsiz)
4495590ff0dSUlrich Drepper {
4502dae0248SDominik Brodowski 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
4515590ff0dSUlrich Drepper }
4525590ff0dSUlrich Drepper 
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
4550753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
4561da177e4SLinus Torvalds 
4578529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
4588529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
4598529f613SLinus Torvalds #endif
4608529f613SLinus Torvalds 
4611da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	struct stat64 tmp;
4641da177e4SLinus Torvalds 
4658529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
4661da177e4SLinus Torvalds #ifdef CONFIG_MIPS
4671da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
4681da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
4691da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
4701da177e4SLinus Torvalds #else
4711da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
4721da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
4731da177e4SLinus Torvalds #endif
4741da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
475afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
476afefdbb2SDavid Howells 		return -EOVERFLOW;
4771da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
4781da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
4791da177e4SLinus Torvalds #endif
4801da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
4811da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
482a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
483a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
4841da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
4851da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
4861da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
4871da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
4881da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
4891da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
4901da177e4SLinus Torvalds 	tmp.st_size = stat->size;
4911da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
4921da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
4931da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
496c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
497c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	struct kstat stat;
5001da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	if (!error)
5031da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	return error;
5061da177e4SLinus Torvalds }
507257ac264SHeiko Carstens 
508c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
509c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	struct kstat stat;
5121da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (!error)
5151da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	return error;
5181da177e4SLinus Torvalds }
519257ac264SHeiko Carstens 
520257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	struct kstat stat;
5231da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	if (!error)
5261da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	return error;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
531c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
5326559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
533cff2b760SUlrich Drepper {
534cff2b760SUlrich Drepper 	struct kstat stat;
5350112fc22SOleg Drokin 	int error;
536cff2b760SUlrich Drepper 
5370112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
5380112fc22SOleg Drokin 	if (error)
539cff2b760SUlrich Drepper 		return error;
5400112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
541cff2b760SUlrich Drepper }
5420753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
5431da177e4SLinus Torvalds 
5446f88cc17SBijan Mottahedeh static noinline_for_stack int
54564bd7204SEric Biggers cp_statx(const struct kstat *stat, struct statx __user *buffer)
546a528d35eSDavid Howells {
54764bd7204SEric Biggers 	struct statx tmp;
548a528d35eSDavid Howells 
54964bd7204SEric Biggers 	memset(&tmp, 0, sizeof(tmp));
550a528d35eSDavid Howells 
55164bd7204SEric Biggers 	tmp.stx_mask = stat->result_mask;
55264bd7204SEric Biggers 	tmp.stx_blksize = stat->blksize;
55364bd7204SEric Biggers 	tmp.stx_attributes = stat->attributes;
55464bd7204SEric Biggers 	tmp.stx_nlink = stat->nlink;
55564bd7204SEric Biggers 	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
55664bd7204SEric Biggers 	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
55764bd7204SEric Biggers 	tmp.stx_mode = stat->mode;
55864bd7204SEric Biggers 	tmp.stx_ino = stat->ino;
55964bd7204SEric Biggers 	tmp.stx_size = stat->size;
56064bd7204SEric Biggers 	tmp.stx_blocks = stat->blocks;
5613209f68bSDavid Howells 	tmp.stx_attributes_mask = stat->attributes_mask;
56264bd7204SEric Biggers 	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
56364bd7204SEric Biggers 	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
56464bd7204SEric Biggers 	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
56564bd7204SEric Biggers 	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
56664bd7204SEric Biggers 	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
56764bd7204SEric Biggers 	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
56864bd7204SEric Biggers 	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
56964bd7204SEric Biggers 	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
57064bd7204SEric Biggers 	tmp.stx_rdev_major = MAJOR(stat->rdev);
57164bd7204SEric Biggers 	tmp.stx_rdev_minor = MINOR(stat->rdev);
57264bd7204SEric Biggers 	tmp.stx_dev_major = MAJOR(stat->dev);
57364bd7204SEric Biggers 	tmp.stx_dev_minor = MINOR(stat->dev);
574fa2fcf4fSMiklos Szeredi 	tmp.stx_mnt_id = stat->mnt_id;
575a528d35eSDavid Howells 
57664bd7204SEric Biggers 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
577a528d35eSDavid Howells }
578a528d35eSDavid Howells 
5790018784fSBijan Mottahedeh int do_statx(int dfd, const char __user *filename, unsigned flags,
5800018784fSBijan Mottahedeh 	     unsigned int mask, struct statx __user *buffer)
5810018784fSBijan Mottahedeh {
5820018784fSBijan Mottahedeh 	struct kstat stat;
5830018784fSBijan Mottahedeh 	int error;
5840018784fSBijan Mottahedeh 
5850018784fSBijan Mottahedeh 	if (mask & STATX__RESERVED)
5860018784fSBijan Mottahedeh 		return -EINVAL;
5870018784fSBijan Mottahedeh 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
5880018784fSBijan Mottahedeh 		return -EINVAL;
5890018784fSBijan Mottahedeh 
5900018784fSBijan Mottahedeh 	error = vfs_statx(dfd, filename, flags, &stat, mask);
5910018784fSBijan Mottahedeh 	if (error)
5920018784fSBijan Mottahedeh 		return error;
5930018784fSBijan Mottahedeh 
5940018784fSBijan Mottahedeh 	return cp_statx(&stat, buffer);
5950018784fSBijan Mottahedeh }
5960018784fSBijan Mottahedeh 
597a528d35eSDavid Howells /**
598a528d35eSDavid Howells  * sys_statx - System call to get enhanced stats
599a528d35eSDavid Howells  * @dfd: Base directory to pathwalk from *or* fd to stat.
6001e2f82d1SDavid Howells  * @filename: File to stat or "" with AT_EMPTY_PATH
601a528d35eSDavid Howells  * @flags: AT_* flags to control pathwalk.
602a528d35eSDavid Howells  * @mask: Parts of statx struct actually required.
603a528d35eSDavid Howells  * @buffer: Result buffer.
604a528d35eSDavid Howells  *
6051e2f82d1SDavid Howells  * Note that fstat() can be emulated by setting dfd to the fd of interest,
6061e2f82d1SDavid Howells  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
607a528d35eSDavid Howells  */
608a528d35eSDavid Howells SYSCALL_DEFINE5(statx,
609a528d35eSDavid Howells 		int, dfd, const char __user *, filename, unsigned, flags,
610a528d35eSDavid Howells 		unsigned int, mask,
611a528d35eSDavid Howells 		struct statx __user *, buffer)
612a528d35eSDavid Howells {
6130018784fSBijan Mottahedeh 	return do_statx(dfd, filename, flags, mask, buffer);
614a528d35eSDavid Howells }
615a528d35eSDavid Howells 
616ac565de3SAl Viro #ifdef CONFIG_COMPAT
617ac565de3SAl Viro static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
618ac565de3SAl Viro {
619ac565de3SAl Viro 	struct compat_stat tmp;
620ac565de3SAl Viro 
621ac565de3SAl Viro 	if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
622ac565de3SAl Viro 		return -EOVERFLOW;
623ac565de3SAl Viro 
624ac565de3SAl Viro 	memset(&tmp, 0, sizeof(tmp));
625ac565de3SAl Viro 	tmp.st_dev = old_encode_dev(stat->dev);
626ac565de3SAl Viro 	tmp.st_ino = stat->ino;
627ac565de3SAl Viro 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
628ac565de3SAl Viro 		return -EOVERFLOW;
629ac565de3SAl Viro 	tmp.st_mode = stat->mode;
630ac565de3SAl Viro 	tmp.st_nlink = stat->nlink;
631ac565de3SAl Viro 	if (tmp.st_nlink != stat->nlink)
632ac565de3SAl Viro 		return -EOVERFLOW;
633ac565de3SAl Viro 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
634ac565de3SAl Viro 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
635ac565de3SAl Viro 	tmp.st_rdev = old_encode_dev(stat->rdev);
636ac565de3SAl Viro 	if ((u64) stat->size > MAX_NON_LFS)
637ac565de3SAl Viro 		return -EOVERFLOW;
638ac565de3SAl Viro 	tmp.st_size = stat->size;
639ac565de3SAl Viro 	tmp.st_atime = stat->atime.tv_sec;
640ac565de3SAl Viro 	tmp.st_atime_nsec = stat->atime.tv_nsec;
641ac565de3SAl Viro 	tmp.st_mtime = stat->mtime.tv_sec;
642ac565de3SAl Viro 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
643ac565de3SAl Viro 	tmp.st_ctime = stat->ctime.tv_sec;
644ac565de3SAl Viro 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
645ac565de3SAl Viro 	tmp.st_blocks = stat->blocks;
646ac565de3SAl Viro 	tmp.st_blksize = stat->blksize;
647ac565de3SAl Viro 	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
648ac565de3SAl Viro }
649ac565de3SAl Viro 
650ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
651ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
652ac565de3SAl Viro {
653ac565de3SAl Viro 	struct kstat stat;
654ac565de3SAl Viro 	int error;
655ac565de3SAl Viro 
656ac565de3SAl Viro 	error = vfs_stat(filename, &stat);
657ac565de3SAl Viro 	if (error)
658ac565de3SAl Viro 		return error;
659ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
660ac565de3SAl Viro }
661ac565de3SAl Viro 
662ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
663ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
664ac565de3SAl Viro {
665ac565de3SAl Viro 	struct kstat stat;
666ac565de3SAl Viro 	int error;
667ac565de3SAl Viro 
668ac565de3SAl Viro 	error = vfs_lstat(filename, &stat);
669ac565de3SAl Viro 	if (error)
670ac565de3SAl Viro 		return error;
671ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
672ac565de3SAl Viro }
673ac565de3SAl Viro 
674ac565de3SAl Viro #ifndef __ARCH_WANT_STAT64
675ac565de3SAl Viro COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
676ac565de3SAl Viro 		       const char __user *, filename,
677ac565de3SAl Viro 		       struct compat_stat __user *, statbuf, int, flag)
678ac565de3SAl Viro {
679ac565de3SAl Viro 	struct kstat stat;
680ac565de3SAl Viro 	int error;
681ac565de3SAl Viro 
682ac565de3SAl Viro 	error = vfs_fstatat(dfd, filename, &stat, flag);
683ac565de3SAl Viro 	if (error)
684ac565de3SAl Viro 		return error;
685ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
686ac565de3SAl Viro }
687ac565de3SAl Viro #endif
688ac565de3SAl Viro 
689ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
690ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
691ac565de3SAl Viro {
692ac565de3SAl Viro 	struct kstat stat;
693ac565de3SAl Viro 	int error = vfs_fstat(fd, &stat);
694ac565de3SAl Viro 
695ac565de3SAl Viro 	if (!error)
696ac565de3SAl Viro 		error = cp_compat_stat(&stat, statbuf);
697ac565de3SAl Viro 	return error;
698ac565de3SAl Viro }
699ac565de3SAl Viro #endif
700ac565de3SAl Viro 
701b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
702b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
7051da177e4SLinus Torvalds 	bytes &= 511;
7061da177e4SLinus Torvalds 	inode->i_bytes += bytes;
7071da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
7081da177e4SLinus Torvalds 		inode->i_blocks++;
7091da177e4SLinus Torvalds 		inode->i_bytes -= 512;
7101da177e4SLinus Torvalds 	}
711b462707eSDmitry Monakhov }
712eb315d2aSAl Viro EXPORT_SYMBOL(__inode_add_bytes);
713b462707eSDmitry Monakhov 
714b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
715b462707eSDmitry Monakhov {
716b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
717b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
7181da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
7221da177e4SLinus Torvalds 
7231c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
7261da177e4SLinus Torvalds 	bytes &= 511;
7271da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
7281da177e4SLinus Torvalds 		inode->i_blocks--;
7291da177e4SLinus Torvalds 		inode->i_bytes += 512;
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
7321c8924ebSJan Kara }
7331c8924ebSJan Kara 
7341c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
7351c8924ebSJan Kara 
7361c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
7371c8924ebSJan Kara {
7381c8924ebSJan Kara 	spin_lock(&inode->i_lock);
7391c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
7401da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	loff_t ret;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
750f4a8116aSJan Kara 	ret = __inode_get_bytes(inode);
7511da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7521da177e4SLinus Torvalds 	return ret;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
7601da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
7611da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
7621da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
766