xref: /openbmc/linux/fs/stat.c (revision 6f88cc176a3358c54bb6c38c8afee3f3a42faf54)
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"
253934e36fSJens Axboe 
26a528d35eSDavid Howells /**
27a528d35eSDavid Howells  * generic_fillattr - Fill in the basic attributes from the inode struct
28a528d35eSDavid Howells  * @inode: Inode to use as the source
29a528d35eSDavid Howells  * @stat: Where to fill in the attributes
30a528d35eSDavid Howells  *
31a528d35eSDavid Howells  * Fill in the basic attributes in the kstat structure from data that's to be
32a528d35eSDavid Howells  * found on the VFS inode structure.  This is the default if no getattr inode
33a528d35eSDavid Howells  * operation is supplied.
34a528d35eSDavid Howells  */
351da177e4SLinus Torvalds void generic_fillattr(struct inode *inode, struct kstat *stat)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	stat->dev = inode->i_sb->s_dev;
381da177e4SLinus Torvalds 	stat->ino = inode->i_ino;
391da177e4SLinus Torvalds 	stat->mode = inode->i_mode;
401da177e4SLinus Torvalds 	stat->nlink = inode->i_nlink;
411da177e4SLinus Torvalds 	stat->uid = inode->i_uid;
421da177e4SLinus Torvalds 	stat->gid = inode->i_gid;
431da177e4SLinus Torvalds 	stat->rdev = inode->i_rdev;
443ddcd056SLinus Torvalds 	stat->size = i_size_read(inode);
451da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
461da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
471da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
4893407472SFabian Frederick 	stat->blksize = i_blocksize(inode);
493ddcd056SLinus Torvalds 	stat->blocks = inode->i_blocks;
50a528d35eSDavid Howells }
511da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr);
521da177e4SLinus Torvalds 
53b7a6ec52SJ. Bruce Fields /**
54b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
55b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
56b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
57a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
58a528d35eSDavid Howells  * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
59b7a6ec52SJ. Bruce Fields  *
60b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
61b7a6ec52SJ. Bruce Fields  *
62b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
63a528d35eSDavid Howells  * filehandle lookup code, which uses only the inode number and returns no
64a528d35eSDavid Howells  * attributes to any user.  Any other code probably wants vfs_getattr.
65b7a6ec52SJ. Bruce Fields  */
66a528d35eSDavid Howells int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
67a528d35eSDavid Howells 		      u32 request_mask, unsigned int query_flags)
681da177e4SLinus Torvalds {
69bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
701da177e4SLinus Torvalds 
71a528d35eSDavid Howells 	memset(stat, 0, sizeof(*stat));
72a528d35eSDavid Howells 	stat->result_mask |= STATX_BASIC_STATS;
73a528d35eSDavid Howells 	request_mask &= STATX_ALL;
74a528d35eSDavid Howells 	query_flags &= KSTAT_QUERY_FLAGS;
75801e5237SChristoph Hellwig 
76801e5237SChristoph Hellwig 	/* allow the fs to override these if it really wants to */
77801e5237SChristoph Hellwig 	if (IS_NOATIME(inode))
78801e5237SChristoph Hellwig 		stat->result_mask &= ~STATX_ATIME;
79801e5237SChristoph Hellwig 	if (IS_AUTOMOUNT(inode))
80801e5237SChristoph Hellwig 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
81801e5237SChristoph Hellwig 
821da177e4SLinus Torvalds 	if (inode->i_op->getattr)
83a528d35eSDavid Howells 		return inode->i_op->getattr(path, stat, request_mask,
84a528d35eSDavid Howells 					    query_flags);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	generic_fillattr(inode, stat);
871da177e4SLinus Torvalds 	return 0;
881da177e4SLinus Torvalds }
89b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
90b7a6ec52SJ. Bruce Fields 
91a528d35eSDavid Howells /*
92a528d35eSDavid Howells  * vfs_getattr - Get the enhanced basic attributes of a file
93a528d35eSDavid Howells  * @path: The file of interest
94a528d35eSDavid Howells  * @stat: Where to return the statistics
95a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
96a528d35eSDavid Howells  * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
97a528d35eSDavid Howells  *
98a528d35eSDavid Howells  * Ask the filesystem for a file's attributes.  The caller must indicate in
99a528d35eSDavid Howells  * request_mask and query_flags to indicate what they want.
100a528d35eSDavid Howells  *
101a528d35eSDavid Howells  * If the file is remote, the filesystem can be forced to update the attributes
102a528d35eSDavid Howells  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
103a528d35eSDavid Howells  * suppress the update by passing AT_STATX_DONT_SYNC.
104a528d35eSDavid Howells  *
105a528d35eSDavid Howells  * Bits must have been set in request_mask to indicate which attributes the
106a528d35eSDavid Howells  * caller wants retrieving.  Any such attribute not requested may be returned
107a528d35eSDavid Howells  * anyway, but the value may be approximate, and, if remote, may not have been
108a528d35eSDavid Howells  * synchronised with the server.
109a528d35eSDavid Howells  *
110a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
111a528d35eSDavid Howells  */
112a528d35eSDavid Howells int vfs_getattr(const struct path *path, struct kstat *stat,
113a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
114b7a6ec52SJ. Bruce Fields {
115b7a6ec52SJ. Bruce Fields 	int retval;
116b7a6ec52SJ. Bruce Fields 
1173f7036a0SAl Viro 	retval = security_inode_getattr(path);
118b7a6ec52SJ. Bruce Fields 	if (retval)
119b7a6ec52SJ. Bruce Fields 		return retval;
120a528d35eSDavid Howells 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
121b7a6ec52SJ. Bruce Fields }
1221da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
1231da177e4SLinus Torvalds 
124a528d35eSDavid Howells /**
125a528d35eSDavid Howells  * vfs_statx_fd - Get the enhanced basic attributes by file descriptor
126a528d35eSDavid Howells  * @fd: The file descriptor referring to the file of interest
127a528d35eSDavid Howells  * @stat: The result structure to fill in.
128a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
129a528d35eSDavid Howells  * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
130a528d35eSDavid Howells  *
131a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
132a528d35eSDavid Howells  * that it uses a file descriptor to determine the file location.
133a528d35eSDavid Howells  *
134a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
135a528d35eSDavid Howells  */
136a528d35eSDavid Howells int vfs_statx_fd(unsigned int fd, struct kstat *stat,
137a528d35eSDavid Howells 		 u32 request_mask, unsigned int query_flags)
1381da177e4SLinus Torvalds {
1398c7493aaSEric Biggers 	struct fd f;
1401da177e4SLinus Torvalds 	int error = -EBADF;
1411da177e4SLinus Torvalds 
1428c7493aaSEric Biggers 	if (query_flags & ~KSTAT_QUERY_FLAGS)
1438c7493aaSEric Biggers 		return -EINVAL;
1448c7493aaSEric Biggers 
1458c7493aaSEric Biggers 	f = fdget_raw(fd);
1462903ff01SAl Viro 	if (f.file) {
147a528d35eSDavid Howells 		error = vfs_getattr(&f.file->f_path, stat,
148a528d35eSDavid Howells 				    request_mask, query_flags);
1492903ff01SAl Viro 		fdput(f);
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	return error;
1521da177e4SLinus Torvalds }
153a528d35eSDavid Howells EXPORT_SYMBOL(vfs_statx_fd);
1541da177e4SLinus Torvalds 
155*6f88cc17SBijan Mottahedeh static inline unsigned vfs_stat_set_lookup_flags(unsigned *lookup_flags,
156*6f88cc17SBijan Mottahedeh 						 int flags)
1573934e36fSJens Axboe {
1583934e36fSJens Axboe 	if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
1593934e36fSJens Axboe 		       AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
1603934e36fSJens Axboe 		return -EINVAL;
1613934e36fSJens Axboe 
1623934e36fSJens Axboe 	*lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
1633934e36fSJens Axboe 	if (flags & AT_SYMLINK_NOFOLLOW)
1643934e36fSJens Axboe 		*lookup_flags &= ~LOOKUP_FOLLOW;
1653934e36fSJens Axboe 	if (flags & AT_NO_AUTOMOUNT)
1663934e36fSJens Axboe 		*lookup_flags &= ~LOOKUP_AUTOMOUNT;
1673934e36fSJens Axboe 	if (flags & AT_EMPTY_PATH)
1683934e36fSJens Axboe 		*lookup_flags |= LOOKUP_EMPTY;
1693934e36fSJens Axboe 
1703934e36fSJens Axboe 	return 0;
1713934e36fSJens Axboe }
1723934e36fSJens Axboe 
173a528d35eSDavid Howells /**
174a528d35eSDavid Howells  * vfs_statx - Get basic and extra attributes by filename
175a528d35eSDavid Howells  * @dfd: A file descriptor representing the base dir for a relative filename
176a528d35eSDavid Howells  * @filename: The name of the file of interest
177a528d35eSDavid Howells  * @flags: Flags to control the query
178a528d35eSDavid Howells  * @stat: The result structure to fill in.
179a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
180a528d35eSDavid Howells  *
181a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
182a528d35eSDavid Howells  * that it uses a filename and base directory to determine the file location.
183a528d35eSDavid Howells  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
184a528d35eSDavid Howells  * at the given name from being referenced.
185a528d35eSDavid Howells  *
186a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
187a528d35eSDavid Howells  */
188a528d35eSDavid Howells int vfs_statx(int dfd, const char __user *filename, int flags,
189a528d35eSDavid Howells 	      struct kstat *stat, u32 request_mask)
1900112fc22SOleg Drokin {
1912eae7a18SChristoph Hellwig 	struct path path;
1920112fc22SOleg Drokin 	int error = -EINVAL;
1933934e36fSJens Axboe 	unsigned lookup_flags;
1940112fc22SOleg Drokin 
1953934e36fSJens Axboe 	if (vfs_stat_set_lookup_flags(&lookup_flags, flags))
196a528d35eSDavid Howells 		return -EINVAL;
197836fb7e7SJeff Layton retry:
1982eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
1992eae7a18SChristoph Hellwig 	if (error)
2002eae7a18SChristoph Hellwig 		goto out;
2012eae7a18SChristoph Hellwig 
202a528d35eSDavid Howells 	error = vfs_getattr(&path, stat, request_mask, flags);
2032eae7a18SChristoph Hellwig 	path_put(&path);
204836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
205836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
206836fb7e7SJeff Layton 		goto retry;
207836fb7e7SJeff Layton 	}
2080112fc22SOleg Drokin out:
2090112fc22SOleg Drokin 	return error;
2100112fc22SOleg Drokin }
211a528d35eSDavid Howells EXPORT_SYMBOL(vfs_statx);
2122eae7a18SChristoph Hellwig 
2130112fc22SOleg Drokin 
2141da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
2181da177e4SLinus Torvalds  * into arch/i386 instead?
2191da177e4SLinus Torvalds  */
2201da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	static int warncount = 5;
2231da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (warncount > 0) {
2261da177e4SLinus Torvalds 		warncount--;
2271da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
2281da177e4SLinus Torvalds 			current->comm);
2291da177e4SLinus Torvalds 	} else if (warncount < 0) {
2301da177e4SLinus Torvalds 		/* it's laughable, but... */
2311da177e4SLinus Torvalds 		warncount = 0;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
2351da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
2361da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
237afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
238afefdbb2SDavid Howells 		return -EOVERFLOW;
2391da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2401da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2411da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2421da177e4SLinus Torvalds 		return -EOVERFLOW;
243a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
244a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
2451da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
2461da177e4SLinus Torvalds #if BITS_PER_LONG == 32
2471da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2481da177e4SLinus Torvalds 		return -EOVERFLOW;
2491da177e4SLinus Torvalds #endif
2501da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2511da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2521da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2531da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2541da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
257c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
258c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	struct kstat stat;
2612eae7a18SChristoph Hellwig 	int error;
2621da177e4SLinus Torvalds 
2632eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
2642eae7a18SChristoph Hellwig 	if (error)
2651da177e4SLinus Torvalds 		return error;
2662eae7a18SChristoph Hellwig 
2672eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2681da177e4SLinus Torvalds }
269257ac264SHeiko Carstens 
270c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
271c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct kstat stat;
2742eae7a18SChristoph Hellwig 	int error;
2751da177e4SLinus Torvalds 
2762eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
2772eae7a18SChristoph Hellwig 	if (error)
2781da177e4SLinus Torvalds 		return error;
2792eae7a18SChristoph Hellwig 
2802eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2811da177e4SLinus Torvalds }
282257ac264SHeiko Carstens 
283257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	struct kstat stat;
2861da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (!error)
2891da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	return error;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
2951da177e4SLinus Torvalds 
29682b355d1SArnd Bergmann #ifdef __ARCH_WANT_NEW_STAT
29782b355d1SArnd Bergmann 
298a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
299a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
300a52dd971SLinus Torvalds #else
301a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
302a52dd971SLinus Torvalds #endif
303a52dd971SLinus Torvalds 
3044c416f42SYaowei Bai #define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
305a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
306a52dd971SLinus Torvalds 
3078529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
3088529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
3098529f613SLinus Torvalds #endif
3108529f613SLinus Torvalds 
3111da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	struct stat tmp;
3141da177e4SLinus Torvalds 
315a52dd971SLinus Torvalds 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
3161da177e4SLinus Torvalds 		return -EOVERFLOW;
317a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
318a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3191da177e4SLinus Torvalds 		return -EOVERFLOW;
3201da177e4SLinus Torvalds #endif
3211da177e4SLinus Torvalds 
3228529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
323a52dd971SLinus Torvalds 	tmp.st_dev = encode_dev(stat->dev);
3241da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
325afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
326afefdbb2SDavid Howells 		return -EOVERFLOW;
3271da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3281da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3291da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3301da177e4SLinus Torvalds 		return -EOVERFLOW;
331a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
332a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
333a52dd971SLinus Torvalds 	tmp.st_rdev = encode_dev(stat->rdev);
3341da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3351da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3361da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3371da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3381da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
3391da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3401da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3411da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3421da177e4SLinus Torvalds #endif
3431da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3441da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3451da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
349c7887325SDavid Howells 		struct stat __user *, statbuf)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	struct kstat stat;
3522eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
3531da177e4SLinus Torvalds 
3542eae7a18SChristoph Hellwig 	if (error)
3551da177e4SLinus Torvalds 		return error;
3562eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
3571da177e4SLinus Torvalds }
3585590ff0dSUlrich Drepper 
359c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
360c7887325SDavid Howells 		struct stat __user *, statbuf)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	struct kstat stat;
3632eae7a18SChristoph Hellwig 	int error;
3641da177e4SLinus Torvalds 
3652eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
3662eae7a18SChristoph Hellwig 	if (error)
3671da177e4SLinus Torvalds 		return error;
3682eae7a18SChristoph Hellwig 
3692eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
3701da177e4SLinus Torvalds }
3715590ff0dSUlrich Drepper 
3722833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
373c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
3746559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
3755590ff0dSUlrich Drepper {
3765590ff0dSUlrich Drepper 	struct kstat stat;
3770112fc22SOleg Drokin 	int error;
3785590ff0dSUlrich Drepper 
3790112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
3800112fc22SOleg Drokin 	if (error)
3815590ff0dSUlrich Drepper 		return error;
3820112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
3835590ff0dSUlrich Drepper }
384cff2b760SUlrich Drepper #endif
3855590ff0dSUlrich Drepper 
386257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct kstat stat;
3891da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	if (!error)
3921da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	return error;
3951da177e4SLinus Torvalds }
39682b355d1SArnd Bergmann #endif
3971da177e4SLinus Torvalds 
3982dae0248SDominik Brodowski static int do_readlinkat(int dfd, const char __user *pathname,
3992dae0248SDominik Brodowski 			 char __user *buf, int bufsiz)
4001da177e4SLinus Torvalds {
4012d8f3038SAl Viro 	struct path path;
4021da177e4SLinus Torvalds 	int error;
4031fa1e7f6SAndy Whitcroft 	int empty = 0;
4047955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	if (bufsiz <= 0)
4071da177e4SLinus Torvalds 		return -EINVAL;
4081da177e4SLinus Torvalds 
4097955119eSJeff Layton retry:
4107955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
4111da177e4SLinus Torvalds 	if (!error) {
412bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
4131da177e4SLinus Torvalds 
4141fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
415fd4a0edfSMiklos Szeredi 		/*
416fd4a0edfSMiklos Szeredi 		 * AFS mountpoints allow readlink(2) but are not symlinks
417fd4a0edfSMiklos Szeredi 		 */
418fd4a0edfSMiklos Szeredi 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
4192d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
4201da177e4SLinus Torvalds 			if (!error) {
42168ac1234SAl Viro 				touch_atime(&path);
422fd4a0edfSMiklos Szeredi 				error = vfs_readlink(path.dentry, buf, bufsiz);
4231da177e4SLinus Torvalds 			}
4241da177e4SLinus Torvalds 		}
4252d8f3038SAl Viro 		path_put(&path);
4267955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
4277955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
4287955119eSJeff Layton 			goto retry;
4297955119eSJeff Layton 		}
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	return error;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4342dae0248SDominik Brodowski SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
4352dae0248SDominik Brodowski 		char __user *, buf, int, bufsiz)
4362dae0248SDominik Brodowski {
4372dae0248SDominik Brodowski 	return do_readlinkat(dfd, pathname, buf, bufsiz);
4382dae0248SDominik Brodowski }
4392dae0248SDominik Brodowski 
440002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
441002c8976SHeiko Carstens 		int, bufsiz)
4425590ff0dSUlrich Drepper {
4432dae0248SDominik Brodowski 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
4445590ff0dSUlrich Drepper }
4455590ff0dSUlrich Drepper 
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
4480753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
4491da177e4SLinus Torvalds 
4508529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
4518529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
4528529f613SLinus Torvalds #endif
4538529f613SLinus Torvalds 
4541da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds 	struct stat64 tmp;
4571da177e4SLinus Torvalds 
4588529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
4591da177e4SLinus Torvalds #ifdef CONFIG_MIPS
4601da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
4611da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
4621da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
4631da177e4SLinus Torvalds #else
4641da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
4651da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
4661da177e4SLinus Torvalds #endif
4671da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
468afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
469afefdbb2SDavid Howells 		return -EOVERFLOW;
4701da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
4711da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
4721da177e4SLinus Torvalds #endif
4731da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
4741da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
475a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
476a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
4771da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
4781da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
4791da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
4801da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
4811da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
4821da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
4831da177e4SLinus Torvalds 	tmp.st_size = stat->size;
4841da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
4851da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
4861da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
490c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	struct kstat stat;
4931da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (!error)
4961da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	return error;
4991da177e4SLinus Torvalds }
500257ac264SHeiko Carstens 
501c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
502c7887325SDavid Howells 		struct stat64 __user *, statbuf)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct kstat stat;
5051da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	if (!error)
5081da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	return error;
5111da177e4SLinus Torvalds }
512257ac264SHeiko Carstens 
513257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	struct kstat stat;
5161da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (!error)
5191da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	return error;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
524c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
5256559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
526cff2b760SUlrich Drepper {
527cff2b760SUlrich Drepper 	struct kstat stat;
5280112fc22SOleg Drokin 	int error;
529cff2b760SUlrich Drepper 
5300112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
5310112fc22SOleg Drokin 	if (error)
532cff2b760SUlrich Drepper 		return error;
5330112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
534cff2b760SUlrich Drepper }
5350753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
5361da177e4SLinus Torvalds 
537*6f88cc17SBijan Mottahedeh static noinline_for_stack int
53864bd7204SEric Biggers cp_statx(const struct kstat *stat, struct statx __user *buffer)
539a528d35eSDavid Howells {
54064bd7204SEric Biggers 	struct statx tmp;
541a528d35eSDavid Howells 
54264bd7204SEric Biggers 	memset(&tmp, 0, sizeof(tmp));
543a528d35eSDavid Howells 
54464bd7204SEric Biggers 	tmp.stx_mask = stat->result_mask;
54564bd7204SEric Biggers 	tmp.stx_blksize = stat->blksize;
54664bd7204SEric Biggers 	tmp.stx_attributes = stat->attributes;
54764bd7204SEric Biggers 	tmp.stx_nlink = stat->nlink;
54864bd7204SEric Biggers 	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
54964bd7204SEric Biggers 	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
55064bd7204SEric Biggers 	tmp.stx_mode = stat->mode;
55164bd7204SEric Biggers 	tmp.stx_ino = stat->ino;
55264bd7204SEric Biggers 	tmp.stx_size = stat->size;
55364bd7204SEric Biggers 	tmp.stx_blocks = stat->blocks;
5543209f68bSDavid Howells 	tmp.stx_attributes_mask = stat->attributes_mask;
55564bd7204SEric Biggers 	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
55664bd7204SEric Biggers 	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
55764bd7204SEric Biggers 	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
55864bd7204SEric Biggers 	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
55964bd7204SEric Biggers 	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
56064bd7204SEric Biggers 	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
56164bd7204SEric Biggers 	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
56264bd7204SEric Biggers 	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
56364bd7204SEric Biggers 	tmp.stx_rdev_major = MAJOR(stat->rdev);
56464bd7204SEric Biggers 	tmp.stx_rdev_minor = MINOR(stat->rdev);
56564bd7204SEric Biggers 	tmp.stx_dev_major = MAJOR(stat->dev);
56664bd7204SEric Biggers 	tmp.stx_dev_minor = MINOR(stat->dev);
567a528d35eSDavid Howells 
56864bd7204SEric Biggers 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
569a528d35eSDavid Howells }
570a528d35eSDavid Howells 
5710018784fSBijan Mottahedeh int do_statx(int dfd, const char __user *filename, unsigned flags,
5720018784fSBijan Mottahedeh 	     unsigned int mask, struct statx __user *buffer)
5730018784fSBijan Mottahedeh {
5740018784fSBijan Mottahedeh 	struct kstat stat;
5750018784fSBijan Mottahedeh 	int error;
5760018784fSBijan Mottahedeh 
5770018784fSBijan Mottahedeh 	if (mask & STATX__RESERVED)
5780018784fSBijan Mottahedeh 		return -EINVAL;
5790018784fSBijan Mottahedeh 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
5800018784fSBijan Mottahedeh 		return -EINVAL;
5810018784fSBijan Mottahedeh 
5820018784fSBijan Mottahedeh 	error = vfs_statx(dfd, filename, flags, &stat, mask);
5830018784fSBijan Mottahedeh 	if (error)
5840018784fSBijan Mottahedeh 		return error;
5850018784fSBijan Mottahedeh 
5860018784fSBijan Mottahedeh 	return cp_statx(&stat, buffer);
5870018784fSBijan Mottahedeh }
5880018784fSBijan Mottahedeh 
589a528d35eSDavid Howells /**
590a528d35eSDavid Howells  * sys_statx - System call to get enhanced stats
591a528d35eSDavid Howells  * @dfd: Base directory to pathwalk from *or* fd to stat.
5921e2f82d1SDavid Howells  * @filename: File to stat or "" with AT_EMPTY_PATH
593a528d35eSDavid Howells  * @flags: AT_* flags to control pathwalk.
594a528d35eSDavid Howells  * @mask: Parts of statx struct actually required.
595a528d35eSDavid Howells  * @buffer: Result buffer.
596a528d35eSDavid Howells  *
5971e2f82d1SDavid Howells  * Note that fstat() can be emulated by setting dfd to the fd of interest,
5981e2f82d1SDavid Howells  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
599a528d35eSDavid Howells  */
600a528d35eSDavid Howells SYSCALL_DEFINE5(statx,
601a528d35eSDavid Howells 		int, dfd, const char __user *, filename, unsigned, flags,
602a528d35eSDavid Howells 		unsigned int, mask,
603a528d35eSDavid Howells 		struct statx __user *, buffer)
604a528d35eSDavid Howells {
6050018784fSBijan Mottahedeh 	return do_statx(dfd, filename, flags, mask, buffer);
606a528d35eSDavid Howells }
607a528d35eSDavid Howells 
608ac565de3SAl Viro #ifdef CONFIG_COMPAT
609ac565de3SAl Viro static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
610ac565de3SAl Viro {
611ac565de3SAl Viro 	struct compat_stat tmp;
612ac565de3SAl Viro 
613ac565de3SAl Viro 	if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
614ac565de3SAl Viro 		return -EOVERFLOW;
615ac565de3SAl Viro 
616ac565de3SAl Viro 	memset(&tmp, 0, sizeof(tmp));
617ac565de3SAl Viro 	tmp.st_dev = old_encode_dev(stat->dev);
618ac565de3SAl Viro 	tmp.st_ino = stat->ino;
619ac565de3SAl Viro 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
620ac565de3SAl Viro 		return -EOVERFLOW;
621ac565de3SAl Viro 	tmp.st_mode = stat->mode;
622ac565de3SAl Viro 	tmp.st_nlink = stat->nlink;
623ac565de3SAl Viro 	if (tmp.st_nlink != stat->nlink)
624ac565de3SAl Viro 		return -EOVERFLOW;
625ac565de3SAl Viro 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
626ac565de3SAl Viro 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
627ac565de3SAl Viro 	tmp.st_rdev = old_encode_dev(stat->rdev);
628ac565de3SAl Viro 	if ((u64) stat->size > MAX_NON_LFS)
629ac565de3SAl Viro 		return -EOVERFLOW;
630ac565de3SAl Viro 	tmp.st_size = stat->size;
631ac565de3SAl Viro 	tmp.st_atime = stat->atime.tv_sec;
632ac565de3SAl Viro 	tmp.st_atime_nsec = stat->atime.tv_nsec;
633ac565de3SAl Viro 	tmp.st_mtime = stat->mtime.tv_sec;
634ac565de3SAl Viro 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
635ac565de3SAl Viro 	tmp.st_ctime = stat->ctime.tv_sec;
636ac565de3SAl Viro 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
637ac565de3SAl Viro 	tmp.st_blocks = stat->blocks;
638ac565de3SAl Viro 	tmp.st_blksize = stat->blksize;
639ac565de3SAl Viro 	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
640ac565de3SAl Viro }
641ac565de3SAl Viro 
642ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
643ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
644ac565de3SAl Viro {
645ac565de3SAl Viro 	struct kstat stat;
646ac565de3SAl Viro 	int error;
647ac565de3SAl Viro 
648ac565de3SAl Viro 	error = vfs_stat(filename, &stat);
649ac565de3SAl Viro 	if (error)
650ac565de3SAl Viro 		return error;
651ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
652ac565de3SAl Viro }
653ac565de3SAl Viro 
654ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
655ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
656ac565de3SAl Viro {
657ac565de3SAl Viro 	struct kstat stat;
658ac565de3SAl Viro 	int error;
659ac565de3SAl Viro 
660ac565de3SAl Viro 	error = vfs_lstat(filename, &stat);
661ac565de3SAl Viro 	if (error)
662ac565de3SAl Viro 		return error;
663ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
664ac565de3SAl Viro }
665ac565de3SAl Viro 
666ac565de3SAl Viro #ifndef __ARCH_WANT_STAT64
667ac565de3SAl Viro COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
668ac565de3SAl Viro 		       const char __user *, filename,
669ac565de3SAl Viro 		       struct compat_stat __user *, statbuf, int, flag)
670ac565de3SAl Viro {
671ac565de3SAl Viro 	struct kstat stat;
672ac565de3SAl Viro 	int error;
673ac565de3SAl Viro 
674ac565de3SAl Viro 	error = vfs_fstatat(dfd, filename, &stat, flag);
675ac565de3SAl Viro 	if (error)
676ac565de3SAl Viro 		return error;
677ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
678ac565de3SAl Viro }
679ac565de3SAl Viro #endif
680ac565de3SAl Viro 
681ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
682ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
683ac565de3SAl Viro {
684ac565de3SAl Viro 	struct kstat stat;
685ac565de3SAl Viro 	int error = vfs_fstat(fd, &stat);
686ac565de3SAl Viro 
687ac565de3SAl Viro 	if (!error)
688ac565de3SAl Viro 		error = cp_compat_stat(&stat, statbuf);
689ac565de3SAl Viro 	return error;
690ac565de3SAl Viro }
691ac565de3SAl Viro #endif
692ac565de3SAl Viro 
693b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
694b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
6971da177e4SLinus Torvalds 	bytes &= 511;
6981da177e4SLinus Torvalds 	inode->i_bytes += bytes;
6991da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
7001da177e4SLinus Torvalds 		inode->i_blocks++;
7011da177e4SLinus Torvalds 		inode->i_bytes -= 512;
7021da177e4SLinus Torvalds 	}
703b462707eSDmitry Monakhov }
704eb315d2aSAl Viro EXPORT_SYMBOL(__inode_add_bytes);
705b462707eSDmitry Monakhov 
706b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
707b462707eSDmitry Monakhov {
708b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
709b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
7101da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
7141da177e4SLinus Torvalds 
7151c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
7181da177e4SLinus Torvalds 	bytes &= 511;
7191da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
7201da177e4SLinus Torvalds 		inode->i_blocks--;
7211da177e4SLinus Torvalds 		inode->i_bytes += 512;
7221da177e4SLinus Torvalds 	}
7231da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
7241c8924ebSJan Kara }
7251c8924ebSJan Kara 
7261c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
7271c8924ebSJan Kara 
7281c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
7291c8924ebSJan Kara {
7301c8924ebSJan Kara 	spin_lock(&inode->i_lock);
7311c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
7321da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	loff_t ret;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
742f4a8116aSJan Kara 	ret = __inode_get_bytes(inode);
7431da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7441da177e4SLinus Torvalds 	return ret;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
7521da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
7531da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
7541da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
758