xref: /openbmc/linux/fs/stat.c (revision 4f911138c8da94bcff84f1d093d28e378703c43f)
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 /**
63*4f911138SAmir Goldstein  * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
64*4f911138SAmir Goldstein  * @inode:	Inode to use as the source
65*4f911138SAmir Goldstein  * @stat:	Where to fill in the attribute flags
66*4f911138SAmir Goldstein  *
67*4f911138SAmir Goldstein  * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
68*4f911138SAmir Goldstein  * inode that are published on i_flags and enforced by the VFS.
69*4f911138SAmir Goldstein  */
70*4f911138SAmir Goldstein void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
71*4f911138SAmir Goldstein {
72*4f911138SAmir Goldstein 	if (inode->i_flags & S_IMMUTABLE)
73*4f911138SAmir Goldstein 		stat->attributes |= STATX_ATTR_IMMUTABLE;
74*4f911138SAmir Goldstein 	if (inode->i_flags & S_APPEND)
75*4f911138SAmir Goldstein 		stat->attributes |= STATX_ATTR_APPEND;
76*4f911138SAmir Goldstein 	stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
77*4f911138SAmir Goldstein }
78*4f911138SAmir Goldstein EXPORT_SYMBOL(generic_fill_statx_attr);
79*4f911138SAmir Goldstein 
80*4f911138SAmir Goldstein /**
81b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
82b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
83b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
84a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
85f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
86b7a6ec52SJ. Bruce Fields  *
87b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
88b7a6ec52SJ. Bruce Fields  *
89b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
90a528d35eSDavid Howells  * filehandle lookup code, which uses only the inode number and returns no
91a528d35eSDavid Howells  * attributes to any user.  Any other code probably wants vfs_getattr.
92b7a6ec52SJ. Bruce Fields  */
93a528d35eSDavid Howells int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
94a528d35eSDavid Howells 		      u32 request_mask, unsigned int query_flags)
951da177e4SLinus Torvalds {
96549c7297SChristian Brauner 	struct user_namespace *mnt_userns;
97bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
981da177e4SLinus Torvalds 
99a528d35eSDavid Howells 	memset(stat, 0, sizeof(*stat));
100a528d35eSDavid Howells 	stat->result_mask |= STATX_BASIC_STATS;
101f2d077ffSChristoph Hellwig 	query_flags &= AT_STATX_SYNC_TYPE;
102801e5237SChristoph Hellwig 
103801e5237SChristoph Hellwig 	/* allow the fs to override these if it really wants to */
104761e28faSMiklos Szeredi 	/* SB_NOATIME means filesystem supplies dummy atime value */
105761e28faSMiklos Szeredi 	if (inode->i_sb->s_flags & SB_NOATIME)
106801e5237SChristoph Hellwig 		stat->result_mask &= ~STATX_ATIME;
1075afa7e8bSTheodore Ts'o 
1085afa7e8bSTheodore Ts'o 	/*
1095afa7e8bSTheodore Ts'o 	 * Note: If you add another clause to set an attribute flag, please
1105afa7e8bSTheodore Ts'o 	 * update attributes_mask below.
1115afa7e8bSTheodore Ts'o 	 */
112801e5237SChristoph Hellwig 	if (IS_AUTOMOUNT(inode))
113801e5237SChristoph Hellwig 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
114801e5237SChristoph Hellwig 
115712b2698SIra Weiny 	if (IS_DAX(inode))
116712b2698SIra Weiny 		stat->attributes |= STATX_ATTR_DAX;
117712b2698SIra Weiny 
1185afa7e8bSTheodore Ts'o 	stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
1195afa7e8bSTheodore Ts'o 				  STATX_ATTR_DAX);
1205afa7e8bSTheodore Ts'o 
121549c7297SChristian Brauner 	mnt_userns = mnt_user_ns(path->mnt);
1221da177e4SLinus Torvalds 	if (inode->i_op->getattr)
123549c7297SChristian Brauner 		return inode->i_op->getattr(mnt_userns, path, stat,
124549c7297SChristian Brauner 					    request_mask, query_flags);
1251da177e4SLinus Torvalds 
126549c7297SChristian Brauner 	generic_fillattr(mnt_userns, inode, stat);
1271da177e4SLinus Torvalds 	return 0;
1281da177e4SLinus Torvalds }
129b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
130b7a6ec52SJ. Bruce Fields 
131a528d35eSDavid Howells /*
132a528d35eSDavid Howells  * vfs_getattr - Get the enhanced basic attributes of a file
133a528d35eSDavid Howells  * @path: The file of interest
134a528d35eSDavid Howells  * @stat: Where to return the statistics
135a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
136f2d077ffSChristoph Hellwig  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
137a528d35eSDavid Howells  *
138a528d35eSDavid Howells  * Ask the filesystem for a file's attributes.  The caller must indicate in
139a528d35eSDavid Howells  * request_mask and query_flags to indicate what they want.
140a528d35eSDavid Howells  *
141a528d35eSDavid Howells  * If the file is remote, the filesystem can be forced to update the attributes
142a528d35eSDavid Howells  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
143a528d35eSDavid Howells  * suppress the update by passing AT_STATX_DONT_SYNC.
144a528d35eSDavid Howells  *
145a528d35eSDavid Howells  * Bits must have been set in request_mask to indicate which attributes the
146a528d35eSDavid Howells  * caller wants retrieving.  Any such attribute not requested may be returned
147a528d35eSDavid Howells  * anyway, but the value may be approximate, and, if remote, may not have been
148a528d35eSDavid Howells  * synchronised with the server.
149a528d35eSDavid Howells  *
150a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
151a528d35eSDavid Howells  */
152a528d35eSDavid Howells int vfs_getattr(const struct path *path, struct kstat *stat,
153a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
154b7a6ec52SJ. Bruce Fields {
155b7a6ec52SJ. Bruce Fields 	int retval;
156b7a6ec52SJ. Bruce Fields 
1573f7036a0SAl Viro 	retval = security_inode_getattr(path);
158b7a6ec52SJ. Bruce Fields 	if (retval)
159b7a6ec52SJ. Bruce Fields 		return retval;
160a528d35eSDavid Howells 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
161b7a6ec52SJ. Bruce Fields }
1621da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
1631da177e4SLinus Torvalds 
164a528d35eSDavid Howells /**
165da9aa5d9SChristoph Hellwig  * vfs_fstat - Get the basic attributes by file descriptor
166a528d35eSDavid Howells  * @fd: The file descriptor referring to the file of interest
167a528d35eSDavid Howells  * @stat: The result structure to fill in.
168a528d35eSDavid Howells  *
169a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
170a528d35eSDavid Howells  * that it uses a file descriptor to determine the file location.
171a528d35eSDavid Howells  *
172a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
173a528d35eSDavid Howells  */
174da9aa5d9SChristoph Hellwig int vfs_fstat(int fd, struct kstat *stat)
1751da177e4SLinus Torvalds {
1768c7493aaSEric Biggers 	struct fd f;
177da9aa5d9SChristoph Hellwig 	int error;
1788c7493aaSEric Biggers 
1798c7493aaSEric Biggers 	f = fdget_raw(fd);
180da9aa5d9SChristoph Hellwig 	if (!f.file)
181da9aa5d9SChristoph Hellwig 		return -EBADF;
182da9aa5d9SChristoph Hellwig 	error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
1832903ff01SAl Viro 	fdput(f);
1841da177e4SLinus Torvalds 	return error;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
187a528d35eSDavid Howells /**
188a528d35eSDavid Howells  * vfs_statx - Get basic and extra attributes by filename
189a528d35eSDavid Howells  * @dfd: A file descriptor representing the base dir for a relative filename
190a528d35eSDavid Howells  * @filename: The name of the file of interest
191a528d35eSDavid Howells  * @flags: Flags to control the query
192a528d35eSDavid Howells  * @stat: The result structure to fill in.
193a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
194a528d35eSDavid Howells  *
195a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
196a528d35eSDavid Howells  * that it uses a filename and base directory to determine the file location.
197a528d35eSDavid Howells  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
198a528d35eSDavid Howells  * at the given name from being referenced.
199a528d35eSDavid Howells  *
200a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
201a528d35eSDavid Howells  */
20209f1bde4SChristoph Hellwig static int vfs_statx(int dfd, const char __user *filename, int flags,
203a528d35eSDavid Howells 	      struct kstat *stat, u32 request_mask)
2040112fc22SOleg Drokin {
2052eae7a18SChristoph Hellwig 	struct path path;
206b3f05150SChristoph Hellwig 	unsigned lookup_flags = 0;
207b3f05150SChristoph Hellwig 	int error;
2080112fc22SOleg Drokin 
209b3f05150SChristoph Hellwig 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
210f2d077ffSChristoph Hellwig 		      AT_STATX_SYNC_TYPE))
211a528d35eSDavid Howells 		return -EINVAL;
212b3f05150SChristoph Hellwig 
213b3f05150SChristoph Hellwig 	if (!(flags & AT_SYMLINK_NOFOLLOW))
214b3f05150SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
215b3f05150SChristoph Hellwig 	if (!(flags & AT_NO_AUTOMOUNT))
216b3f05150SChristoph Hellwig 		lookup_flags |= LOOKUP_AUTOMOUNT;
217b3f05150SChristoph Hellwig 	if (flags & AT_EMPTY_PATH)
218b3f05150SChristoph Hellwig 		lookup_flags |= LOOKUP_EMPTY;
219b3f05150SChristoph Hellwig 
220836fb7e7SJeff Layton retry:
2212eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
2222eae7a18SChristoph Hellwig 	if (error)
2232eae7a18SChristoph Hellwig 		goto out;
2242eae7a18SChristoph Hellwig 
225a528d35eSDavid Howells 	error = vfs_getattr(&path, stat, request_mask, flags);
226fa2fcf4fSMiklos Szeredi 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
227fa2fcf4fSMiklos Szeredi 	stat->result_mask |= STATX_MNT_ID;
22880340fe3SMiklos Szeredi 	if (path.mnt->mnt_root == path.dentry)
22980340fe3SMiklos Szeredi 		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
23080340fe3SMiklos Szeredi 	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
2312eae7a18SChristoph Hellwig 	path_put(&path);
232836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
233836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
234836fb7e7SJeff Layton 		goto retry;
235836fb7e7SJeff Layton 	}
2360112fc22SOleg Drokin out:
2370112fc22SOleg Drokin 	return error;
2380112fc22SOleg Drokin }
2392eae7a18SChristoph Hellwig 
24009f1bde4SChristoph Hellwig int vfs_fstatat(int dfd, const char __user *filename,
24109f1bde4SChristoph Hellwig 			      struct kstat *stat, int flags)
24209f1bde4SChristoph Hellwig {
24309f1bde4SChristoph Hellwig 	return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT,
24409f1bde4SChristoph Hellwig 			 stat, STATX_BASIC_STATS);
24509f1bde4SChristoph Hellwig }
2460112fc22SOleg Drokin 
2471da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds /*
2501da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
2511da177e4SLinus Torvalds  * into arch/i386 instead?
2521da177e4SLinus Torvalds  */
2531da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	static int warncount = 5;
2561da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (warncount > 0) {
2591da177e4SLinus Torvalds 		warncount--;
2601da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
2611da177e4SLinus Torvalds 			current->comm);
2621da177e4SLinus Torvalds 	} else if (warncount < 0) {
2631da177e4SLinus Torvalds 		/* it's laughable, but... */
2641da177e4SLinus Torvalds 		warncount = 0;
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
2681da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
2691da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
270afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
271afefdbb2SDavid Howells 		return -EOVERFLOW;
2721da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2731da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2741da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2751da177e4SLinus Torvalds 		return -EOVERFLOW;
276a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
277a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
2781da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
2791da177e4SLinus Torvalds #if BITS_PER_LONG == 32
2801da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2811da177e4SLinus Torvalds 		return -EOVERFLOW;
2821da177e4SLinus Torvalds #endif
2831da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2841da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2851da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2861da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2871da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
290c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
291c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	struct kstat stat;
2942eae7a18SChristoph Hellwig 	int error;
2951da177e4SLinus Torvalds 
2962eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
2972eae7a18SChristoph Hellwig 	if (error)
2981da177e4SLinus Torvalds 		return error;
2992eae7a18SChristoph Hellwig 
3002eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
3011da177e4SLinus Torvalds }
302257ac264SHeiko Carstens 
303c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
304c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	struct kstat stat;
3072eae7a18SChristoph Hellwig 	int error;
3081da177e4SLinus Torvalds 
3092eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
3102eae7a18SChristoph Hellwig 	if (error)
3111da177e4SLinus Torvalds 		return error;
3122eae7a18SChristoph Hellwig 
3132eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
3141da177e4SLinus Torvalds }
315257ac264SHeiko Carstens 
316257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	struct kstat stat;
3191da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	if (!error)
3221da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	return error;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
3281da177e4SLinus Torvalds 
32982b355d1SArnd Bergmann #ifdef __ARCH_WANT_NEW_STAT
33082b355d1SArnd Bergmann 
331a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
332a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
333a52dd971SLinus Torvalds #else
334a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
335a52dd971SLinus Torvalds #endif
336a52dd971SLinus Torvalds 
3374c416f42SYaowei Bai #define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
338a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
339a52dd971SLinus Torvalds 
3408529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
3418529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
3428529f613SLinus Torvalds #endif
3438529f613SLinus Torvalds 
3441da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	struct stat tmp;
3471da177e4SLinus Torvalds 
348a52dd971SLinus Torvalds 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
3491da177e4SLinus Torvalds 		return -EOVERFLOW;
350a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
351a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3521da177e4SLinus Torvalds 		return -EOVERFLOW;
3531da177e4SLinus Torvalds #endif
3541da177e4SLinus Torvalds 
3558529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
356a52dd971SLinus Torvalds 	tmp.st_dev = encode_dev(stat->dev);
3571da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
358afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
359afefdbb2SDavid Howells 		return -EOVERFLOW;
3601da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3611da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3621da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3631da177e4SLinus Torvalds 		return -EOVERFLOW;
364a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
365a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
366a52dd971SLinus Torvalds 	tmp.st_rdev = encode_dev(stat->rdev);
3671da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3681da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3691da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3701da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3711da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
3721da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3731da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3741da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3751da177e4SLinus Torvalds #endif
3761da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3771da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3781da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
381c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
382c7887325SDavid Howells 		struct stat __user *, statbuf)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct kstat stat;
3852eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
3861da177e4SLinus Torvalds 
3872eae7a18SChristoph Hellwig 	if (error)
3881da177e4SLinus Torvalds 		return error;
3892eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
3901da177e4SLinus Torvalds }
3915590ff0dSUlrich Drepper 
392c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
393c7887325SDavid Howells 		struct stat __user *, statbuf)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	struct kstat stat;
3962eae7a18SChristoph Hellwig 	int error;
3971da177e4SLinus Torvalds 
3982eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
3992eae7a18SChristoph Hellwig 	if (error)
4001da177e4SLinus Torvalds 		return error;
4012eae7a18SChristoph Hellwig 
4022eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
4031da177e4SLinus Torvalds }
4045590ff0dSUlrich Drepper 
4052833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
406c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
4076559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
4085590ff0dSUlrich Drepper {
4095590ff0dSUlrich Drepper 	struct kstat stat;
4100112fc22SOleg Drokin 	int error;
4115590ff0dSUlrich Drepper 
4120112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
4130112fc22SOleg Drokin 	if (error)
4145590ff0dSUlrich Drepper 		return error;
4150112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
4165590ff0dSUlrich Drepper }
417cff2b760SUlrich Drepper #endif
4185590ff0dSUlrich Drepper 
419257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	struct kstat stat;
4221da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	if (!error)
4251da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	return error;
4281da177e4SLinus Torvalds }
42982b355d1SArnd Bergmann #endif
4301da177e4SLinus Torvalds 
4312dae0248SDominik Brodowski static int do_readlinkat(int dfd, const char __user *pathname,
4322dae0248SDominik Brodowski 			 char __user *buf, int bufsiz)
4331da177e4SLinus Torvalds {
4342d8f3038SAl Viro 	struct path path;
4351da177e4SLinus Torvalds 	int error;
4361fa1e7f6SAndy Whitcroft 	int empty = 0;
4377955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (bufsiz <= 0)
4401da177e4SLinus Torvalds 		return -EINVAL;
4411da177e4SLinus Torvalds 
4427955119eSJeff Layton retry:
4437955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
4441da177e4SLinus Torvalds 	if (!error) {
445bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
4461da177e4SLinus Torvalds 
4471fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
448fd4a0edfSMiklos Szeredi 		/*
449fd4a0edfSMiklos Szeredi 		 * AFS mountpoints allow readlink(2) but are not symlinks
450fd4a0edfSMiklos Szeredi 		 */
451fd4a0edfSMiklos Szeredi 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
4522d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
4531da177e4SLinus Torvalds 			if (!error) {
45468ac1234SAl Viro 				touch_atime(&path);
455fd4a0edfSMiklos Szeredi 				error = vfs_readlink(path.dentry, buf, bufsiz);
4561da177e4SLinus Torvalds 			}
4571da177e4SLinus Torvalds 		}
4582d8f3038SAl Viro 		path_put(&path);
4597955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
4607955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
4617955119eSJeff Layton 			goto retry;
4627955119eSJeff Layton 		}
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 	return error;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4672dae0248SDominik Brodowski SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
4682dae0248SDominik Brodowski 		char __user *, buf, int, bufsiz)
4692dae0248SDominik Brodowski {
4702dae0248SDominik Brodowski 	return do_readlinkat(dfd, pathname, buf, bufsiz);
4712dae0248SDominik Brodowski }
4722dae0248SDominik Brodowski 
473002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
474002c8976SHeiko Carstens 		int, bufsiz)
4755590ff0dSUlrich Drepper {
4762dae0248SDominik Brodowski 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
4775590ff0dSUlrich Drepper }
4785590ff0dSUlrich Drepper 
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
4810753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
4821da177e4SLinus Torvalds 
4838529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
4848529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
4858529f613SLinus Torvalds #endif
4868529f613SLinus Torvalds 
4871da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	struct stat64 tmp;
4901da177e4SLinus Torvalds 
4918529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
4921da177e4SLinus Torvalds #ifdef CONFIG_MIPS
4931da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
4941da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
4951da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
4961da177e4SLinus Torvalds #else
4971da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
4981da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
4991da177e4SLinus Torvalds #endif
5001da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
501afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
502afefdbb2SDavid Howells 		return -EOVERFLOW;
5031da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
5041da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
5051da177e4SLinus Torvalds #endif
5061da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
5071da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
508a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
509a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
5101da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
5111da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
5121da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
5131da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
5141da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
5151da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
5161da177e4SLinus Torvalds 	tmp.st_size = stat->size;
5171da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
5181da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
5191da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
522c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
523c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5241da177e4SLinus Torvalds {
5251da177e4SLinus Torvalds 	struct kstat stat;
5261da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	if (!error)
5291da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	return error;
5321da177e4SLinus Torvalds }
533257ac264SHeiko Carstens 
534c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
535c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct kstat stat;
5381da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	if (!error)
5411da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	return error;
5441da177e4SLinus Torvalds }
545257ac264SHeiko Carstens 
546257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
5471da177e4SLinus Torvalds {
5481da177e4SLinus Torvalds 	struct kstat stat;
5491da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	if (!error)
5521da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	return error;
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
557c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
5586559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
559cff2b760SUlrich Drepper {
560cff2b760SUlrich Drepper 	struct kstat stat;
5610112fc22SOleg Drokin 	int error;
562cff2b760SUlrich Drepper 
5630112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
5640112fc22SOleg Drokin 	if (error)
565cff2b760SUlrich Drepper 		return error;
5660112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
567cff2b760SUlrich Drepper }
5680753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
5691da177e4SLinus Torvalds 
5706f88cc17SBijan Mottahedeh static noinline_for_stack int
57164bd7204SEric Biggers cp_statx(const struct kstat *stat, struct statx __user *buffer)
572a528d35eSDavid Howells {
57364bd7204SEric Biggers 	struct statx tmp;
574a528d35eSDavid Howells 
57564bd7204SEric Biggers 	memset(&tmp, 0, sizeof(tmp));
576a528d35eSDavid Howells 
57764bd7204SEric Biggers 	tmp.stx_mask = stat->result_mask;
57864bd7204SEric Biggers 	tmp.stx_blksize = stat->blksize;
57964bd7204SEric Biggers 	tmp.stx_attributes = stat->attributes;
58064bd7204SEric Biggers 	tmp.stx_nlink = stat->nlink;
58164bd7204SEric Biggers 	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
58264bd7204SEric Biggers 	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
58364bd7204SEric Biggers 	tmp.stx_mode = stat->mode;
58464bd7204SEric Biggers 	tmp.stx_ino = stat->ino;
58564bd7204SEric Biggers 	tmp.stx_size = stat->size;
58664bd7204SEric Biggers 	tmp.stx_blocks = stat->blocks;
5873209f68bSDavid Howells 	tmp.stx_attributes_mask = stat->attributes_mask;
58864bd7204SEric Biggers 	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
58964bd7204SEric Biggers 	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
59064bd7204SEric Biggers 	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
59164bd7204SEric Biggers 	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
59264bd7204SEric Biggers 	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
59364bd7204SEric Biggers 	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
59464bd7204SEric Biggers 	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
59564bd7204SEric Biggers 	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
59664bd7204SEric Biggers 	tmp.stx_rdev_major = MAJOR(stat->rdev);
59764bd7204SEric Biggers 	tmp.stx_rdev_minor = MINOR(stat->rdev);
59864bd7204SEric Biggers 	tmp.stx_dev_major = MAJOR(stat->dev);
59964bd7204SEric Biggers 	tmp.stx_dev_minor = MINOR(stat->dev);
600fa2fcf4fSMiklos Szeredi 	tmp.stx_mnt_id = stat->mnt_id;
601a528d35eSDavid Howells 
60264bd7204SEric Biggers 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
603a528d35eSDavid Howells }
604a528d35eSDavid Howells 
6050018784fSBijan Mottahedeh int do_statx(int dfd, const char __user *filename, unsigned flags,
6060018784fSBijan Mottahedeh 	     unsigned int mask, struct statx __user *buffer)
6070018784fSBijan Mottahedeh {
6080018784fSBijan Mottahedeh 	struct kstat stat;
6090018784fSBijan Mottahedeh 	int error;
6100018784fSBijan Mottahedeh 
6110018784fSBijan Mottahedeh 	if (mask & STATX__RESERVED)
6120018784fSBijan Mottahedeh 		return -EINVAL;
6130018784fSBijan Mottahedeh 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
6140018784fSBijan Mottahedeh 		return -EINVAL;
6150018784fSBijan Mottahedeh 
6160018784fSBijan Mottahedeh 	error = vfs_statx(dfd, filename, flags, &stat, mask);
6170018784fSBijan Mottahedeh 	if (error)
6180018784fSBijan Mottahedeh 		return error;
6190018784fSBijan Mottahedeh 
6200018784fSBijan Mottahedeh 	return cp_statx(&stat, buffer);
6210018784fSBijan Mottahedeh }
6220018784fSBijan Mottahedeh 
623a528d35eSDavid Howells /**
624a528d35eSDavid Howells  * sys_statx - System call to get enhanced stats
625a528d35eSDavid Howells  * @dfd: Base directory to pathwalk from *or* fd to stat.
6261e2f82d1SDavid Howells  * @filename: File to stat or "" with AT_EMPTY_PATH
627a528d35eSDavid Howells  * @flags: AT_* flags to control pathwalk.
628a528d35eSDavid Howells  * @mask: Parts of statx struct actually required.
629a528d35eSDavid Howells  * @buffer: Result buffer.
630a528d35eSDavid Howells  *
6311e2f82d1SDavid Howells  * Note that fstat() can be emulated by setting dfd to the fd of interest,
6321e2f82d1SDavid Howells  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
633a528d35eSDavid Howells  */
634a528d35eSDavid Howells SYSCALL_DEFINE5(statx,
635a528d35eSDavid Howells 		int, dfd, const char __user *, filename, unsigned, flags,
636a528d35eSDavid Howells 		unsigned int, mask,
637a528d35eSDavid Howells 		struct statx __user *, buffer)
638a528d35eSDavid Howells {
6390018784fSBijan Mottahedeh 	return do_statx(dfd, filename, flags, mask, buffer);
640a528d35eSDavid Howells }
641a528d35eSDavid Howells 
642ac565de3SAl Viro #ifdef CONFIG_COMPAT
643ac565de3SAl Viro static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
644ac565de3SAl Viro {
645ac565de3SAl Viro 	struct compat_stat tmp;
646ac565de3SAl Viro 
647ac565de3SAl Viro 	if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
648ac565de3SAl Viro 		return -EOVERFLOW;
649ac565de3SAl Viro 
650ac565de3SAl Viro 	memset(&tmp, 0, sizeof(tmp));
651ac565de3SAl Viro 	tmp.st_dev = old_encode_dev(stat->dev);
652ac565de3SAl Viro 	tmp.st_ino = stat->ino;
653ac565de3SAl Viro 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
654ac565de3SAl Viro 		return -EOVERFLOW;
655ac565de3SAl Viro 	tmp.st_mode = stat->mode;
656ac565de3SAl Viro 	tmp.st_nlink = stat->nlink;
657ac565de3SAl Viro 	if (tmp.st_nlink != stat->nlink)
658ac565de3SAl Viro 		return -EOVERFLOW;
659ac565de3SAl Viro 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
660ac565de3SAl Viro 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
661ac565de3SAl Viro 	tmp.st_rdev = old_encode_dev(stat->rdev);
662ac565de3SAl Viro 	if ((u64) stat->size > MAX_NON_LFS)
663ac565de3SAl Viro 		return -EOVERFLOW;
664ac565de3SAl Viro 	tmp.st_size = stat->size;
665ac565de3SAl Viro 	tmp.st_atime = stat->atime.tv_sec;
666ac565de3SAl Viro 	tmp.st_atime_nsec = stat->atime.tv_nsec;
667ac565de3SAl Viro 	tmp.st_mtime = stat->mtime.tv_sec;
668ac565de3SAl Viro 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
669ac565de3SAl Viro 	tmp.st_ctime = stat->ctime.tv_sec;
670ac565de3SAl Viro 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
671ac565de3SAl Viro 	tmp.st_blocks = stat->blocks;
672ac565de3SAl Viro 	tmp.st_blksize = stat->blksize;
673ac565de3SAl Viro 	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
674ac565de3SAl Viro }
675ac565de3SAl Viro 
676ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
677ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
678ac565de3SAl Viro {
679ac565de3SAl Viro 	struct kstat stat;
680ac565de3SAl Viro 	int error;
681ac565de3SAl Viro 
682ac565de3SAl Viro 	error = vfs_stat(filename, &stat);
683ac565de3SAl Viro 	if (error)
684ac565de3SAl Viro 		return error;
685ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
686ac565de3SAl Viro }
687ac565de3SAl Viro 
688ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
689ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
690ac565de3SAl Viro {
691ac565de3SAl Viro 	struct kstat stat;
692ac565de3SAl Viro 	int error;
693ac565de3SAl Viro 
694ac565de3SAl Viro 	error = vfs_lstat(filename, &stat);
695ac565de3SAl Viro 	if (error)
696ac565de3SAl Viro 		return error;
697ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
698ac565de3SAl Viro }
699ac565de3SAl Viro 
700ac565de3SAl Viro #ifndef __ARCH_WANT_STAT64
701ac565de3SAl Viro COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
702ac565de3SAl Viro 		       const char __user *, filename,
703ac565de3SAl Viro 		       struct compat_stat __user *, statbuf, int, flag)
704ac565de3SAl Viro {
705ac565de3SAl Viro 	struct kstat stat;
706ac565de3SAl Viro 	int error;
707ac565de3SAl Viro 
708ac565de3SAl Viro 	error = vfs_fstatat(dfd, filename, &stat, flag);
709ac565de3SAl Viro 	if (error)
710ac565de3SAl Viro 		return error;
711ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
712ac565de3SAl Viro }
713ac565de3SAl Viro #endif
714ac565de3SAl Viro 
715ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
716ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
717ac565de3SAl Viro {
718ac565de3SAl Viro 	struct kstat stat;
719ac565de3SAl Viro 	int error = vfs_fstat(fd, &stat);
720ac565de3SAl Viro 
721ac565de3SAl Viro 	if (!error)
722ac565de3SAl Viro 		error = cp_compat_stat(&stat, statbuf);
723ac565de3SAl Viro 	return error;
724ac565de3SAl Viro }
725ac565de3SAl Viro #endif
726ac565de3SAl Viro 
727b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
728b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
7311da177e4SLinus Torvalds 	bytes &= 511;
7321da177e4SLinus Torvalds 	inode->i_bytes += bytes;
7331da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
7341da177e4SLinus Torvalds 		inode->i_blocks++;
7351da177e4SLinus Torvalds 		inode->i_bytes -= 512;
7361da177e4SLinus Torvalds 	}
737b462707eSDmitry Monakhov }
738eb315d2aSAl Viro EXPORT_SYMBOL(__inode_add_bytes);
739b462707eSDmitry Monakhov 
740b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
741b462707eSDmitry Monakhov {
742b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
743b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
7441da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
7481da177e4SLinus Torvalds 
7491c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
7521da177e4SLinus Torvalds 	bytes &= 511;
7531da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
7541da177e4SLinus Torvalds 		inode->i_blocks--;
7551da177e4SLinus Torvalds 		inode->i_bytes += 512;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
7581c8924ebSJan Kara }
7591c8924ebSJan Kara 
7601c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
7611c8924ebSJan Kara 
7621c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
7631c8924ebSJan Kara {
7641c8924ebSJan Kara 	spin_lock(&inode->i_lock);
7651c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
7661da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	loff_t ret;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
776f4a8116aSJan Kara 	ret = __inode_get_bytes(inode);
7771da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7781da177e4SLinus Torvalds 	return ret;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
7861da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
7871da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
7881da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
792