xref: /openbmc/linux/fs/stat.c (revision 801e523796004a3aaeae7dfc83fe81888f055287)
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 
24a528d35eSDavid Howells /**
25a528d35eSDavid Howells  * generic_fillattr - Fill in the basic attributes from the inode struct
26a528d35eSDavid Howells  * @inode: Inode to use as the source
27a528d35eSDavid Howells  * @stat: Where to fill in the attributes
28a528d35eSDavid Howells  *
29a528d35eSDavid Howells  * Fill in the basic attributes in the kstat structure from data that's to be
30a528d35eSDavid Howells  * found on the VFS inode structure.  This is the default if no getattr inode
31a528d35eSDavid Howells  * operation is supplied.
32a528d35eSDavid Howells  */
331da177e4SLinus Torvalds void generic_fillattr(struct inode *inode, struct kstat *stat)
341da177e4SLinus Torvalds {
351da177e4SLinus Torvalds 	stat->dev = inode->i_sb->s_dev;
361da177e4SLinus Torvalds 	stat->ino = inode->i_ino;
371da177e4SLinus Torvalds 	stat->mode = inode->i_mode;
381da177e4SLinus Torvalds 	stat->nlink = inode->i_nlink;
391da177e4SLinus Torvalds 	stat->uid = inode->i_uid;
401da177e4SLinus Torvalds 	stat->gid = inode->i_gid;
411da177e4SLinus Torvalds 	stat->rdev = inode->i_rdev;
423ddcd056SLinus Torvalds 	stat->size = i_size_read(inode);
431da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
441da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
451da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
4693407472SFabian Frederick 	stat->blksize = i_blocksize(inode);
473ddcd056SLinus Torvalds 	stat->blocks = inode->i_blocks;
48a528d35eSDavid Howells }
491da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr);
501da177e4SLinus Torvalds 
51b7a6ec52SJ. Bruce Fields /**
52b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
53b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
54b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
55a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
56a528d35eSDavid Howells  * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
57b7a6ec52SJ. Bruce Fields  *
58b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
59b7a6ec52SJ. Bruce Fields  *
60b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
61a528d35eSDavid Howells  * filehandle lookup code, which uses only the inode number and returns no
62a528d35eSDavid Howells  * attributes to any user.  Any other code probably wants vfs_getattr.
63b7a6ec52SJ. Bruce Fields  */
64a528d35eSDavid Howells int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
65a528d35eSDavid Howells 		      u32 request_mask, unsigned int query_flags)
661da177e4SLinus Torvalds {
67bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
681da177e4SLinus Torvalds 
69a528d35eSDavid Howells 	memset(stat, 0, sizeof(*stat));
70a528d35eSDavid Howells 	stat->result_mask |= STATX_BASIC_STATS;
71a528d35eSDavid Howells 	request_mask &= STATX_ALL;
72a528d35eSDavid Howells 	query_flags &= KSTAT_QUERY_FLAGS;
73*801e5237SChristoph Hellwig 
74*801e5237SChristoph Hellwig 	/* allow the fs to override these if it really wants to */
75*801e5237SChristoph Hellwig 	if (IS_NOATIME(inode))
76*801e5237SChristoph Hellwig 		stat->result_mask &= ~STATX_ATIME;
77*801e5237SChristoph Hellwig 	if (IS_AUTOMOUNT(inode))
78*801e5237SChristoph Hellwig 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
79*801e5237SChristoph Hellwig 
801da177e4SLinus Torvalds 	if (inode->i_op->getattr)
81a528d35eSDavid Howells 		return inode->i_op->getattr(path, stat, request_mask,
82a528d35eSDavid Howells 					    query_flags);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	generic_fillattr(inode, stat);
851da177e4SLinus Torvalds 	return 0;
861da177e4SLinus Torvalds }
87b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
88b7a6ec52SJ. Bruce Fields 
89a528d35eSDavid Howells /*
90a528d35eSDavid Howells  * vfs_getattr - Get the enhanced basic attributes of a file
91a528d35eSDavid Howells  * @path: The file of interest
92a528d35eSDavid Howells  * @stat: Where to return the statistics
93a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
94a528d35eSDavid Howells  * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
95a528d35eSDavid Howells  *
96a528d35eSDavid Howells  * Ask the filesystem for a file's attributes.  The caller must indicate in
97a528d35eSDavid Howells  * request_mask and query_flags to indicate what they want.
98a528d35eSDavid Howells  *
99a528d35eSDavid Howells  * If the file is remote, the filesystem can be forced to update the attributes
100a528d35eSDavid Howells  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
101a528d35eSDavid Howells  * suppress the update by passing AT_STATX_DONT_SYNC.
102a528d35eSDavid Howells  *
103a528d35eSDavid Howells  * Bits must have been set in request_mask to indicate which attributes the
104a528d35eSDavid Howells  * caller wants retrieving.  Any such attribute not requested may be returned
105a528d35eSDavid Howells  * anyway, but the value may be approximate, and, if remote, may not have been
106a528d35eSDavid Howells  * synchronised with the server.
107a528d35eSDavid Howells  *
108a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
109a528d35eSDavid Howells  */
110a528d35eSDavid Howells int vfs_getattr(const struct path *path, struct kstat *stat,
111a528d35eSDavid Howells 		u32 request_mask, unsigned int query_flags)
112b7a6ec52SJ. Bruce Fields {
113b7a6ec52SJ. Bruce Fields 	int retval;
114b7a6ec52SJ. Bruce Fields 
1153f7036a0SAl Viro 	retval = security_inode_getattr(path);
116b7a6ec52SJ. Bruce Fields 	if (retval)
117b7a6ec52SJ. Bruce Fields 		return retval;
118a528d35eSDavid Howells 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
119b7a6ec52SJ. Bruce Fields }
1201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
1211da177e4SLinus Torvalds 
122a528d35eSDavid Howells /**
123a528d35eSDavid Howells  * vfs_statx_fd - Get the enhanced basic attributes by file descriptor
124a528d35eSDavid Howells  * @fd: The file descriptor referring to the file of interest
125a528d35eSDavid Howells  * @stat: The result structure to fill in.
126a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
127a528d35eSDavid Howells  * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
128a528d35eSDavid Howells  *
129a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
130a528d35eSDavid Howells  * that it uses a file descriptor to determine the file location.
131a528d35eSDavid Howells  *
132a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
133a528d35eSDavid Howells  */
134a528d35eSDavid Howells int vfs_statx_fd(unsigned int fd, struct kstat *stat,
135a528d35eSDavid Howells 		 u32 request_mask, unsigned int query_flags)
1361da177e4SLinus Torvalds {
1378c7493aaSEric Biggers 	struct fd f;
1381da177e4SLinus Torvalds 	int error = -EBADF;
1391da177e4SLinus Torvalds 
1408c7493aaSEric Biggers 	if (query_flags & ~KSTAT_QUERY_FLAGS)
1418c7493aaSEric Biggers 		return -EINVAL;
1428c7493aaSEric Biggers 
1438c7493aaSEric Biggers 	f = fdget_raw(fd);
1442903ff01SAl Viro 	if (f.file) {
145a528d35eSDavid Howells 		error = vfs_getattr(&f.file->f_path, stat,
146a528d35eSDavid Howells 				    request_mask, query_flags);
1472903ff01SAl Viro 		fdput(f);
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds 	return error;
1501da177e4SLinus Torvalds }
151a528d35eSDavid Howells EXPORT_SYMBOL(vfs_statx_fd);
1521da177e4SLinus Torvalds 
153a528d35eSDavid Howells /**
154a528d35eSDavid Howells  * vfs_statx - Get basic and extra attributes by filename
155a528d35eSDavid Howells  * @dfd: A file descriptor representing the base dir for a relative filename
156a528d35eSDavid Howells  * @filename: The name of the file of interest
157a528d35eSDavid Howells  * @flags: Flags to control the query
158a528d35eSDavid Howells  * @stat: The result structure to fill in.
159a528d35eSDavid Howells  * @request_mask: STATX_xxx flags indicating what the caller wants
160a528d35eSDavid Howells  *
161a528d35eSDavid Howells  * This function is a wrapper around vfs_getattr().  The main difference is
162a528d35eSDavid Howells  * that it uses a filename and base directory to determine the file location.
163a528d35eSDavid Howells  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
164a528d35eSDavid Howells  * at the given name from being referenced.
165a528d35eSDavid Howells  *
166a528d35eSDavid Howells  * 0 will be returned on success, and a -ve error code if unsuccessful.
167a528d35eSDavid Howells  */
168a528d35eSDavid Howells int vfs_statx(int dfd, const char __user *filename, int flags,
169a528d35eSDavid Howells 	      struct kstat *stat, u32 request_mask)
1700112fc22SOleg Drokin {
1712eae7a18SChristoph Hellwig 	struct path path;
1720112fc22SOleg Drokin 	int error = -EINVAL;
173a528d35eSDavid Howells 	unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
1740112fc22SOleg Drokin 
175a528d35eSDavid Howells 	if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
176a528d35eSDavid Howells 		       AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
177a528d35eSDavid Howells 		return -EINVAL;
1780112fc22SOleg Drokin 
179a528d35eSDavid Howells 	if (flags & AT_SYMLINK_NOFOLLOW)
180a528d35eSDavid Howells 		lookup_flags &= ~LOOKUP_FOLLOW;
181a528d35eSDavid Howells 	if (flags & AT_NO_AUTOMOUNT)
182a528d35eSDavid Howells 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
183a528d35eSDavid Howells 	if (flags & AT_EMPTY_PATH)
18465cfc672SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
185a528d35eSDavid Howells 
186836fb7e7SJeff Layton retry:
1872eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
1882eae7a18SChristoph Hellwig 	if (error)
1892eae7a18SChristoph Hellwig 		goto out;
1902eae7a18SChristoph Hellwig 
191a528d35eSDavid Howells 	error = vfs_getattr(&path, stat, request_mask, flags);
1922eae7a18SChristoph Hellwig 	path_put(&path);
193836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
194836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
195836fb7e7SJeff Layton 		goto retry;
196836fb7e7SJeff Layton 	}
1970112fc22SOleg Drokin out:
1980112fc22SOleg Drokin 	return error;
1990112fc22SOleg Drokin }
200a528d35eSDavid Howells EXPORT_SYMBOL(vfs_statx);
2012eae7a18SChristoph Hellwig 
2020112fc22SOleg Drokin 
2031da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
2071da177e4SLinus Torvalds  * into arch/i386 instead?
2081da177e4SLinus Torvalds  */
2091da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	static int warncount = 5;
2121da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (warncount > 0) {
2151da177e4SLinus Torvalds 		warncount--;
2161da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
2171da177e4SLinus Torvalds 			current->comm);
2181da177e4SLinus Torvalds 	} else if (warncount < 0) {
2191da177e4SLinus Torvalds 		/* it's laughable, but... */
2201da177e4SLinus Torvalds 		warncount = 0;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
2241da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
2251da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
226afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
227afefdbb2SDavid Howells 		return -EOVERFLOW;
2281da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2291da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2301da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2311da177e4SLinus Torvalds 		return -EOVERFLOW;
232a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
233a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
2341da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
2351da177e4SLinus Torvalds #if BITS_PER_LONG == 32
2361da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2371da177e4SLinus Torvalds 		return -EOVERFLOW;
2381da177e4SLinus Torvalds #endif
2391da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2401da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2411da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2421da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2431da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
246c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
247c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	struct kstat stat;
2502eae7a18SChristoph Hellwig 	int error;
2511da177e4SLinus Torvalds 
2522eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
2532eae7a18SChristoph Hellwig 	if (error)
2541da177e4SLinus Torvalds 		return error;
2552eae7a18SChristoph Hellwig 
2562eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2571da177e4SLinus Torvalds }
258257ac264SHeiko Carstens 
259c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
260c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	struct kstat stat;
2632eae7a18SChristoph Hellwig 	int error;
2641da177e4SLinus Torvalds 
2652eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
2662eae7a18SChristoph Hellwig 	if (error)
2671da177e4SLinus Torvalds 		return error;
2682eae7a18SChristoph Hellwig 
2692eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2701da177e4SLinus Torvalds }
271257ac264SHeiko Carstens 
272257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct kstat stat;
2751da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (!error)
2781da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	return error;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
2841da177e4SLinus Torvalds 
28582b355d1SArnd Bergmann #ifdef __ARCH_WANT_NEW_STAT
28682b355d1SArnd Bergmann 
287a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
288a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
289a52dd971SLinus Torvalds #else
290a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
291a52dd971SLinus Torvalds #endif
292a52dd971SLinus Torvalds 
2934c416f42SYaowei Bai #define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
294a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
295a52dd971SLinus Torvalds 
2968529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
2978529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
2988529f613SLinus Torvalds #endif
2998529f613SLinus Torvalds 
3001da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	struct stat tmp;
3031da177e4SLinus Torvalds 
304a52dd971SLinus Torvalds 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
3051da177e4SLinus Torvalds 		return -EOVERFLOW;
306a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
307a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
3081da177e4SLinus Torvalds 		return -EOVERFLOW;
3091da177e4SLinus Torvalds #endif
3101da177e4SLinus Torvalds 
3118529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
312a52dd971SLinus Torvalds 	tmp.st_dev = encode_dev(stat->dev);
3131da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
314afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
315afefdbb2SDavid Howells 		return -EOVERFLOW;
3161da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3171da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3181da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
3191da177e4SLinus Torvalds 		return -EOVERFLOW;
320a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
321a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
322a52dd971SLinus Torvalds 	tmp.st_rdev = encode_dev(stat->rdev);
3231da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3241da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3251da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3261da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3271da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
3281da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3291da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3301da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3311da177e4SLinus Torvalds #endif
3321da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3331da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3341da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
337c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
338c7887325SDavid Howells 		struct stat __user *, statbuf)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	struct kstat stat;
3412eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
3421da177e4SLinus Torvalds 
3432eae7a18SChristoph Hellwig 	if (error)
3441da177e4SLinus Torvalds 		return error;
3452eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
3461da177e4SLinus Torvalds }
3475590ff0dSUlrich Drepper 
348c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
349c7887325SDavid Howells 		struct stat __user *, statbuf)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	struct kstat stat;
3522eae7a18SChristoph Hellwig 	int error;
3531da177e4SLinus Torvalds 
3542eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
3552eae7a18SChristoph Hellwig 	if (error)
3561da177e4SLinus Torvalds 		return error;
3572eae7a18SChristoph Hellwig 
3582eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
3591da177e4SLinus Torvalds }
3605590ff0dSUlrich Drepper 
3612833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
362c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
3636559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
3645590ff0dSUlrich Drepper {
3655590ff0dSUlrich Drepper 	struct kstat stat;
3660112fc22SOleg Drokin 	int error;
3675590ff0dSUlrich Drepper 
3680112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
3690112fc22SOleg Drokin 	if (error)
3705590ff0dSUlrich Drepper 		return error;
3710112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
3725590ff0dSUlrich Drepper }
373cff2b760SUlrich Drepper #endif
3745590ff0dSUlrich Drepper 
375257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	struct kstat stat;
3781da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	if (!error)
3811da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	return error;
3841da177e4SLinus Torvalds }
38582b355d1SArnd Bergmann #endif
3861da177e4SLinus Torvalds 
3872dae0248SDominik Brodowski static int do_readlinkat(int dfd, const char __user *pathname,
3882dae0248SDominik Brodowski 			 char __user *buf, int bufsiz)
3891da177e4SLinus Torvalds {
3902d8f3038SAl Viro 	struct path path;
3911da177e4SLinus Torvalds 	int error;
3921fa1e7f6SAndy Whitcroft 	int empty = 0;
3937955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	if (bufsiz <= 0)
3961da177e4SLinus Torvalds 		return -EINVAL;
3971da177e4SLinus Torvalds 
3987955119eSJeff Layton retry:
3997955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
4001da177e4SLinus Torvalds 	if (!error) {
401bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
4021da177e4SLinus Torvalds 
4031fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
404fd4a0edfSMiklos Szeredi 		/*
405fd4a0edfSMiklos Szeredi 		 * AFS mountpoints allow readlink(2) but are not symlinks
406fd4a0edfSMiklos Szeredi 		 */
407fd4a0edfSMiklos Szeredi 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
4082d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
4091da177e4SLinus Torvalds 			if (!error) {
41068ac1234SAl Viro 				touch_atime(&path);
411fd4a0edfSMiklos Szeredi 				error = vfs_readlink(path.dentry, buf, bufsiz);
4121da177e4SLinus Torvalds 			}
4131da177e4SLinus Torvalds 		}
4142d8f3038SAl Viro 		path_put(&path);
4157955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
4167955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
4177955119eSJeff Layton 			goto retry;
4187955119eSJeff Layton 		}
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 	return error;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4232dae0248SDominik Brodowski SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
4242dae0248SDominik Brodowski 		char __user *, buf, int, bufsiz)
4252dae0248SDominik Brodowski {
4262dae0248SDominik Brodowski 	return do_readlinkat(dfd, pathname, buf, bufsiz);
4272dae0248SDominik Brodowski }
4282dae0248SDominik Brodowski 
429002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
430002c8976SHeiko Carstens 		int, bufsiz)
4315590ff0dSUlrich Drepper {
4322dae0248SDominik Brodowski 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
4335590ff0dSUlrich Drepper }
4345590ff0dSUlrich Drepper 
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
4370753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
4381da177e4SLinus Torvalds 
4398529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
4408529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
4418529f613SLinus Torvalds #endif
4428529f613SLinus Torvalds 
4431da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	struct stat64 tmp;
4461da177e4SLinus Torvalds 
4478529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
4481da177e4SLinus Torvalds #ifdef CONFIG_MIPS
4491da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
4501da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
4511da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
4521da177e4SLinus Torvalds #else
4531da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
4541da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
4551da177e4SLinus Torvalds #endif
4561da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
457afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
458afefdbb2SDavid Howells 		return -EOVERFLOW;
4591da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
4601da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
4611da177e4SLinus Torvalds #endif
4621da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
4631da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
464a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
465a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
4661da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
4671da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
4681da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
4691da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
4701da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
4711da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
4721da177e4SLinus Torvalds 	tmp.st_size = stat->size;
4731da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
4741da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
4751da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
479c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	struct kstat stat;
4821da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	if (!error)
4851da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	return error;
4881da177e4SLinus Torvalds }
489257ac264SHeiko Carstens 
490c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
491c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	struct kstat stat;
4941da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	if (!error)
4971da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	return error;
5001da177e4SLinus Torvalds }
501257ac264SHeiko Carstens 
502257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct kstat stat;
5051da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	if (!error)
5081da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	return error;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
513c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
5146559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
515cff2b760SUlrich Drepper {
516cff2b760SUlrich Drepper 	struct kstat stat;
5170112fc22SOleg Drokin 	int error;
518cff2b760SUlrich Drepper 
5190112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
5200112fc22SOleg Drokin 	if (error)
521cff2b760SUlrich Drepper 		return error;
5220112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
523cff2b760SUlrich Drepper }
5240753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
5251da177e4SLinus Torvalds 
52664bd7204SEric Biggers static noinline_for_stack int
52764bd7204SEric Biggers cp_statx(const struct kstat *stat, struct statx __user *buffer)
528a528d35eSDavid Howells {
52964bd7204SEric Biggers 	struct statx tmp;
530a528d35eSDavid Howells 
53164bd7204SEric Biggers 	memset(&tmp, 0, sizeof(tmp));
532a528d35eSDavid Howells 
53364bd7204SEric Biggers 	tmp.stx_mask = stat->result_mask;
53464bd7204SEric Biggers 	tmp.stx_blksize = stat->blksize;
53564bd7204SEric Biggers 	tmp.stx_attributes = stat->attributes;
53664bd7204SEric Biggers 	tmp.stx_nlink = stat->nlink;
53764bd7204SEric Biggers 	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
53864bd7204SEric Biggers 	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
53964bd7204SEric Biggers 	tmp.stx_mode = stat->mode;
54064bd7204SEric Biggers 	tmp.stx_ino = stat->ino;
54164bd7204SEric Biggers 	tmp.stx_size = stat->size;
54264bd7204SEric Biggers 	tmp.stx_blocks = stat->blocks;
5433209f68bSDavid Howells 	tmp.stx_attributes_mask = stat->attributes_mask;
54464bd7204SEric Biggers 	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
54564bd7204SEric Biggers 	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
54664bd7204SEric Biggers 	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
54764bd7204SEric Biggers 	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
54864bd7204SEric Biggers 	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
54964bd7204SEric Biggers 	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
55064bd7204SEric Biggers 	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
55164bd7204SEric Biggers 	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
55264bd7204SEric Biggers 	tmp.stx_rdev_major = MAJOR(stat->rdev);
55364bd7204SEric Biggers 	tmp.stx_rdev_minor = MINOR(stat->rdev);
55464bd7204SEric Biggers 	tmp.stx_dev_major = MAJOR(stat->dev);
55564bd7204SEric Biggers 	tmp.stx_dev_minor = MINOR(stat->dev);
556a528d35eSDavid Howells 
55764bd7204SEric Biggers 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
558a528d35eSDavid Howells }
559a528d35eSDavid Howells 
560a528d35eSDavid Howells /**
561a528d35eSDavid Howells  * sys_statx - System call to get enhanced stats
562a528d35eSDavid Howells  * @dfd: Base directory to pathwalk from *or* fd to stat.
5631e2f82d1SDavid Howells  * @filename: File to stat or "" with AT_EMPTY_PATH
564a528d35eSDavid Howells  * @flags: AT_* flags to control pathwalk.
565a528d35eSDavid Howells  * @mask: Parts of statx struct actually required.
566a528d35eSDavid Howells  * @buffer: Result buffer.
567a528d35eSDavid Howells  *
5681e2f82d1SDavid Howells  * Note that fstat() can be emulated by setting dfd to the fd of interest,
5691e2f82d1SDavid Howells  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
570a528d35eSDavid Howells  */
571a528d35eSDavid Howells SYSCALL_DEFINE5(statx,
572a528d35eSDavid Howells 		int, dfd, const char __user *, filename, unsigned, flags,
573a528d35eSDavid Howells 		unsigned int, mask,
574a528d35eSDavid Howells 		struct statx __user *, buffer)
575a528d35eSDavid Howells {
576a528d35eSDavid Howells 	struct kstat stat;
577a528d35eSDavid Howells 	int error;
578a528d35eSDavid Howells 
57947071aeeSDavid Howells 	if (mask & STATX__RESERVED)
58047071aeeSDavid Howells 		return -EINVAL;
581a528d35eSDavid Howells 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
582a528d35eSDavid Howells 		return -EINVAL;
583a528d35eSDavid Howells 
584a528d35eSDavid Howells 	error = vfs_statx(dfd, filename, flags, &stat, mask);
585a528d35eSDavid Howells 	if (error)
586a528d35eSDavid Howells 		return error;
58764bd7204SEric Biggers 
58864bd7204SEric Biggers 	return cp_statx(&stat, buffer);
589a528d35eSDavid Howells }
590a528d35eSDavid Howells 
591ac565de3SAl Viro #ifdef CONFIG_COMPAT
592ac565de3SAl Viro static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
593ac565de3SAl Viro {
594ac565de3SAl Viro 	struct compat_stat tmp;
595ac565de3SAl Viro 
596ac565de3SAl Viro 	if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
597ac565de3SAl Viro 		return -EOVERFLOW;
598ac565de3SAl Viro 
599ac565de3SAl Viro 	memset(&tmp, 0, sizeof(tmp));
600ac565de3SAl Viro 	tmp.st_dev = old_encode_dev(stat->dev);
601ac565de3SAl Viro 	tmp.st_ino = stat->ino;
602ac565de3SAl Viro 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
603ac565de3SAl Viro 		return -EOVERFLOW;
604ac565de3SAl Viro 	tmp.st_mode = stat->mode;
605ac565de3SAl Viro 	tmp.st_nlink = stat->nlink;
606ac565de3SAl Viro 	if (tmp.st_nlink != stat->nlink)
607ac565de3SAl Viro 		return -EOVERFLOW;
608ac565de3SAl Viro 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
609ac565de3SAl Viro 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
610ac565de3SAl Viro 	tmp.st_rdev = old_encode_dev(stat->rdev);
611ac565de3SAl Viro 	if ((u64) stat->size > MAX_NON_LFS)
612ac565de3SAl Viro 		return -EOVERFLOW;
613ac565de3SAl Viro 	tmp.st_size = stat->size;
614ac565de3SAl Viro 	tmp.st_atime = stat->atime.tv_sec;
615ac565de3SAl Viro 	tmp.st_atime_nsec = stat->atime.tv_nsec;
616ac565de3SAl Viro 	tmp.st_mtime = stat->mtime.tv_sec;
617ac565de3SAl Viro 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
618ac565de3SAl Viro 	tmp.st_ctime = stat->ctime.tv_sec;
619ac565de3SAl Viro 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
620ac565de3SAl Viro 	tmp.st_blocks = stat->blocks;
621ac565de3SAl Viro 	tmp.st_blksize = stat->blksize;
622ac565de3SAl Viro 	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
623ac565de3SAl Viro }
624ac565de3SAl Viro 
625ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
626ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
627ac565de3SAl Viro {
628ac565de3SAl Viro 	struct kstat stat;
629ac565de3SAl Viro 	int error;
630ac565de3SAl Viro 
631ac565de3SAl Viro 	error = vfs_stat(filename, &stat);
632ac565de3SAl Viro 	if (error)
633ac565de3SAl Viro 		return error;
634ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
635ac565de3SAl Viro }
636ac565de3SAl Viro 
637ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
638ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
639ac565de3SAl Viro {
640ac565de3SAl Viro 	struct kstat stat;
641ac565de3SAl Viro 	int error;
642ac565de3SAl Viro 
643ac565de3SAl Viro 	error = vfs_lstat(filename, &stat);
644ac565de3SAl Viro 	if (error)
645ac565de3SAl Viro 		return error;
646ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
647ac565de3SAl Viro }
648ac565de3SAl Viro 
649ac565de3SAl Viro #ifndef __ARCH_WANT_STAT64
650ac565de3SAl Viro COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
651ac565de3SAl Viro 		       const char __user *, filename,
652ac565de3SAl Viro 		       struct compat_stat __user *, statbuf, int, flag)
653ac565de3SAl Viro {
654ac565de3SAl Viro 	struct kstat stat;
655ac565de3SAl Viro 	int error;
656ac565de3SAl Viro 
657ac565de3SAl Viro 	error = vfs_fstatat(dfd, filename, &stat, flag);
658ac565de3SAl Viro 	if (error)
659ac565de3SAl Viro 		return error;
660ac565de3SAl Viro 	return cp_compat_stat(&stat, statbuf);
661ac565de3SAl Viro }
662ac565de3SAl Viro #endif
663ac565de3SAl Viro 
664ac565de3SAl Viro COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
665ac565de3SAl Viro 		       struct compat_stat __user *, statbuf)
666ac565de3SAl Viro {
667ac565de3SAl Viro 	struct kstat stat;
668ac565de3SAl Viro 	int error = vfs_fstat(fd, &stat);
669ac565de3SAl Viro 
670ac565de3SAl Viro 	if (!error)
671ac565de3SAl Viro 		error = cp_compat_stat(&stat, statbuf);
672ac565de3SAl Viro 	return error;
673ac565de3SAl Viro }
674ac565de3SAl Viro #endif
675ac565de3SAl Viro 
676b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
677b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
6801da177e4SLinus Torvalds 	bytes &= 511;
6811da177e4SLinus Torvalds 	inode->i_bytes += bytes;
6821da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
6831da177e4SLinus Torvalds 		inode->i_blocks++;
6841da177e4SLinus Torvalds 		inode->i_bytes -= 512;
6851da177e4SLinus Torvalds 	}
686b462707eSDmitry Monakhov }
687eb315d2aSAl Viro EXPORT_SYMBOL(__inode_add_bytes);
688b462707eSDmitry Monakhov 
689b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
690b462707eSDmitry Monakhov {
691b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
692b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
6931da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
6971da177e4SLinus Torvalds 
6981c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
7011da177e4SLinus Torvalds 	bytes &= 511;
7021da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
7031da177e4SLinus Torvalds 		inode->i_blocks--;
7041da177e4SLinus Torvalds 		inode->i_bytes += 512;
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
7071c8924ebSJan Kara }
7081c8924ebSJan Kara 
7091c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
7101c8924ebSJan Kara 
7111c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
7121c8924ebSJan Kara {
7131c8924ebSJan Kara 	spin_lock(&inode->i_lock);
7141c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
7151da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	loff_t ret;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
725f4a8116aSJan Kara 	ret = __inode_get_bytes(inode);
7261da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
7271da177e4SLinus Torvalds 	return ret;
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
7351da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
7361da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
7371da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
741