xref: /openbmc/linux/fs/stat.c (revision 836fb7e7b978e5f3b8b52e40838ddc50264723f0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/stat.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
7630d9c47SPaul Gortmaker #include <linux/export.h>
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/errno.h>
101da177e4SLinus Torvalds #include <linux/file.h>
111da177e4SLinus Torvalds #include <linux/highuid.h>
121da177e4SLinus Torvalds #include <linux/fs.h>
131da177e4SLinus Torvalds #include <linux/namei.h>
141da177e4SLinus Torvalds #include <linux/security.h>
151da177e4SLinus Torvalds #include <linux/syscalls.h>
16ba52de12STheodore Ts'o #include <linux/pagemap.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <asm/uaccess.h>
191da177e4SLinus Torvalds #include <asm/unistd.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds void generic_fillattr(struct inode *inode, struct kstat *stat)
221da177e4SLinus Torvalds {
231da177e4SLinus Torvalds 	stat->dev = inode->i_sb->s_dev;
241da177e4SLinus Torvalds 	stat->ino = inode->i_ino;
251da177e4SLinus Torvalds 	stat->mode = inode->i_mode;
261da177e4SLinus Torvalds 	stat->nlink = inode->i_nlink;
271da177e4SLinus Torvalds 	stat->uid = inode->i_uid;
281da177e4SLinus Torvalds 	stat->gid = inode->i_gid;
291da177e4SLinus Torvalds 	stat->rdev = inode->i_rdev;
303ddcd056SLinus Torvalds 	stat->size = i_size_read(inode);
311da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
321da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
331da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
34ba52de12STheodore Ts'o 	stat->blksize = (1 << inode->i_blkbits);
353ddcd056SLinus Torvalds 	stat->blocks = inode->i_blocks;
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr);
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds int vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
431da177e4SLinus Torvalds 	int retval;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	retval = security_inode_getattr(mnt, dentry);
461da177e4SLinus Torvalds 	if (retval)
471da177e4SLinus Torvalds 		return retval;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	if (inode->i_op->getattr)
501da177e4SLinus Torvalds 		return inode->i_op->getattr(mnt, dentry, stat);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	generic_fillattr(inode, stat);
531da177e4SLinus Torvalds 	return 0;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds int vfs_fstat(unsigned int fd, struct kstat *stat)
591da177e4SLinus Torvalds {
602903ff01SAl Viro 	struct fd f = fdget_raw(fd);
611da177e4SLinus Torvalds 	int error = -EBADF;
621da177e4SLinus Torvalds 
632903ff01SAl Viro 	if (f.file) {
642903ff01SAl Viro 		error = vfs_getattr(f.file->f_path.mnt, f.file->f_path.dentry,
652903ff01SAl Viro 				    stat);
662903ff01SAl Viro 		fdput(f);
671da177e4SLinus Torvalds 	}
681da177e4SLinus Torvalds 	return error;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_fstat);
711da177e4SLinus Torvalds 
72c7887325SDavid Howells int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
73c7887325SDavid Howells 		int flag)
740112fc22SOleg Drokin {
752eae7a18SChristoph Hellwig 	struct path path;
760112fc22SOleg Drokin 	int error = -EINVAL;
77*836fb7e7SJeff Layton 	unsigned int lookup_flags = 0;
780112fc22SOleg Drokin 
7965cfc672SAl Viro 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
8065cfc672SAl Viro 		      AT_EMPTY_PATH)) != 0)
810112fc22SOleg Drokin 		goto out;
820112fc22SOleg Drokin 
832eae7a18SChristoph Hellwig 	if (!(flag & AT_SYMLINK_NOFOLLOW))
842eae7a18SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
8565cfc672SAl Viro 	if (flag & AT_EMPTY_PATH)
8665cfc672SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
87*836fb7e7SJeff Layton retry:
882eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
892eae7a18SChristoph Hellwig 	if (error)
902eae7a18SChristoph Hellwig 		goto out;
912eae7a18SChristoph Hellwig 
922eae7a18SChristoph Hellwig 	error = vfs_getattr(path.mnt, path.dentry, stat);
932eae7a18SChristoph Hellwig 	path_put(&path);
94*836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
95*836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
96*836fb7e7SJeff Layton 		goto retry;
97*836fb7e7SJeff Layton 	}
980112fc22SOleg Drokin out:
990112fc22SOleg Drokin 	return error;
1000112fc22SOleg Drokin }
1010112fc22SOleg Drokin EXPORT_SYMBOL(vfs_fstatat);
1020112fc22SOleg Drokin 
103c7887325SDavid Howells int vfs_stat(const char __user *name, struct kstat *stat)
1042eae7a18SChristoph Hellwig {
1052eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, 0);
1062eae7a18SChristoph Hellwig }
1072eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_stat);
1082eae7a18SChristoph Hellwig 
109c7887325SDavid Howells int vfs_lstat(const char __user *name, struct kstat *stat)
1102eae7a18SChristoph Hellwig {
1112eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
1122eae7a18SChristoph Hellwig }
1132eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_lstat);
1142eae7a18SChristoph Hellwig 
1150112fc22SOleg Drokin 
1161da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds /*
1191da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
1201da177e4SLinus Torvalds  * into arch/i386 instead?
1211da177e4SLinus Torvalds  */
1221da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	static int warncount = 5;
1251da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	if (warncount > 0) {
1281da177e4SLinus Torvalds 		warncount--;
1291da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
1301da177e4SLinus Torvalds 			current->comm);
1311da177e4SLinus Torvalds 	} else if (warncount < 0) {
1321da177e4SLinus Torvalds 		/* it's laughable, but... */
1331da177e4SLinus Torvalds 		warncount = 0;
1341da177e4SLinus Torvalds 	}
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
1371da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
1381da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
139afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
140afefdbb2SDavid Howells 		return -EOVERFLOW;
1411da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
1421da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
1431da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
1441da177e4SLinus Torvalds 		return -EOVERFLOW;
145a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
146a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
1471da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
1481da177e4SLinus Torvalds #if BITS_PER_LONG == 32
1491da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
1501da177e4SLinus Torvalds 		return -EOVERFLOW;
1511da177e4SLinus Torvalds #endif
1521da177e4SLinus Torvalds 	tmp.st_size = stat->size;
1531da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
1541da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
1551da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
1561da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
160c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	struct kstat stat;
1632eae7a18SChristoph Hellwig 	int error;
1641da177e4SLinus Torvalds 
1652eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
1662eae7a18SChristoph Hellwig 	if (error)
1671da177e4SLinus Torvalds 		return error;
1682eae7a18SChristoph Hellwig 
1692eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
1701da177e4SLinus Torvalds }
171257ac264SHeiko Carstens 
172c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
173c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct kstat stat;
1762eae7a18SChristoph Hellwig 	int error;
1771da177e4SLinus Torvalds 
1782eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
1792eae7a18SChristoph Hellwig 	if (error)
1801da177e4SLinus Torvalds 		return error;
1812eae7a18SChristoph Hellwig 
1822eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
1831da177e4SLinus Torvalds }
184257ac264SHeiko Carstens 
185257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	struct kstat stat;
1881da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	if (!error)
1911da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	return error;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
1971da177e4SLinus Torvalds 
198a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
199a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
200a52dd971SLinus Torvalds #else
201a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
202a52dd971SLinus Torvalds #endif
203a52dd971SLinus Torvalds 
204a52dd971SLinus Torvalds #define valid_dev(x)  choose_32_64(old_valid_dev,new_valid_dev)(x)
205a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
206a52dd971SLinus Torvalds 
2078529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
2088529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
2098529f613SLinus Torvalds #endif
2108529f613SLinus Torvalds 
2111da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	struct stat tmp;
2141da177e4SLinus Torvalds 
215a52dd971SLinus Torvalds 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
2161da177e4SLinus Torvalds 		return -EOVERFLOW;
217a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
218a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2191da177e4SLinus Torvalds 		return -EOVERFLOW;
2201da177e4SLinus Torvalds #endif
2211da177e4SLinus Torvalds 
2228529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
223a52dd971SLinus Torvalds 	tmp.st_dev = encode_dev(stat->dev);
2241da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
225afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
226afefdbb2SDavid Howells 		return -EOVERFLOW;
2271da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2281da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2291da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2301da177e4SLinus Torvalds 		return -EOVERFLOW;
231a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
232a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
233a52dd971SLinus Torvalds 	tmp.st_rdev = encode_dev(stat->rdev);
2341da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2351da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2361da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2371da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2381da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
2391da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
2401da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
2411da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
2421da177e4SLinus Torvalds #endif
2431da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
2441da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
2451da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
248c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
249c7887325SDavid Howells 		struct stat __user *, statbuf)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	struct kstat stat;
2522eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
2531da177e4SLinus Torvalds 
2542eae7a18SChristoph Hellwig 	if (error)
2551da177e4SLinus Torvalds 		return error;
2562eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
2571da177e4SLinus Torvalds }
2585590ff0dSUlrich Drepper 
259c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
260c7887325SDavid Howells 		struct 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_new_stat(&stat, statbuf);
2701da177e4SLinus Torvalds }
2715590ff0dSUlrich Drepper 
2722833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
273c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
2746559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
2755590ff0dSUlrich Drepper {
2765590ff0dSUlrich Drepper 	struct kstat stat;
2770112fc22SOleg Drokin 	int error;
2785590ff0dSUlrich Drepper 
2790112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
2800112fc22SOleg Drokin 	if (error)
2815590ff0dSUlrich Drepper 		return error;
2820112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
2835590ff0dSUlrich Drepper }
284cff2b760SUlrich Drepper #endif
2855590ff0dSUlrich Drepper 
286257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct kstat stat;
2891da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (!error)
2921da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	return error;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2976559eed8SHeiko Carstens SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
2986559eed8SHeiko Carstens 		char __user *, buf, int, bufsiz)
2991da177e4SLinus Torvalds {
3002d8f3038SAl Viro 	struct path path;
3011da177e4SLinus Torvalds 	int error;
3021fa1e7f6SAndy Whitcroft 	int empty = 0;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (bufsiz <= 0)
3051da177e4SLinus Torvalds 		return -EINVAL;
3061da177e4SLinus Torvalds 
3071fa1e7f6SAndy Whitcroft 	error = user_path_at_empty(dfd, pathname, LOOKUP_EMPTY, &path, &empty);
3081da177e4SLinus Torvalds 	if (!error) {
3092d8f3038SAl Viro 		struct inode *inode = path.dentry->d_inode;
3101da177e4SLinus Torvalds 
3111fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
312acfa4380SAl Viro 		if (inode->i_op->readlink) {
3132d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
3141da177e4SLinus Torvalds 			if (!error) {
31568ac1234SAl Viro 				touch_atime(&path);
3162d8f3038SAl Viro 				error = inode->i_op->readlink(path.dentry,
3174ac91378SJan Blunck 							      buf, bufsiz);
3181da177e4SLinus Torvalds 			}
3191da177e4SLinus Torvalds 		}
3202d8f3038SAl Viro 		path_put(&path);
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 	return error;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
325002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
326002c8976SHeiko Carstens 		int, bufsiz)
3275590ff0dSUlrich Drepper {
3285590ff0dSUlrich Drepper 	return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
3295590ff0dSUlrich Drepper }
3305590ff0dSUlrich Drepper 
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
3330753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
3341da177e4SLinus Torvalds 
3358529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
3368529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
3378529f613SLinus Torvalds #endif
3388529f613SLinus Torvalds 
3391da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	struct stat64 tmp;
3421da177e4SLinus Torvalds 
3438529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
3441da177e4SLinus Torvalds #ifdef CONFIG_MIPS
3451da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
3461da177e4SLinus Torvalds 	if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
3471da177e4SLinus Torvalds 		return -EOVERFLOW;
3481da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
3491da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
3501da177e4SLinus Torvalds #else
3511da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
3521da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
3531da177e4SLinus Torvalds #endif
3541da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
355afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
356afefdbb2SDavid Howells 		return -EOVERFLOW;
3571da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
3581da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
3591da177e4SLinus Torvalds #endif
3601da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3611da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
362a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
363a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
3641da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3651da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3661da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3671da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3681da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3691da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3701da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3711da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3721da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3731da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
376c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
377c7887325SDavid Howells 		struct stat64 __user *, statbuf)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	struct kstat stat;
3801da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (!error)
3831da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	return error;
3861da177e4SLinus Torvalds }
387257ac264SHeiko Carstens 
388c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
389c7887325SDavid Howells 		struct stat64 __user *, statbuf)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	struct kstat stat;
3921da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	if (!error)
3951da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	return error;
3981da177e4SLinus Torvalds }
399257ac264SHeiko Carstens 
400257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	struct kstat stat;
4031da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	if (!error)
4061da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	return error;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
411c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
4126559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
413cff2b760SUlrich Drepper {
414cff2b760SUlrich Drepper 	struct kstat stat;
4150112fc22SOleg Drokin 	int error;
416cff2b760SUlrich Drepper 
4170112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
4180112fc22SOleg Drokin 	if (error)
419cff2b760SUlrich Drepper 		return error;
4200112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
421cff2b760SUlrich Drepper }
4220753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
4231da177e4SLinus Torvalds 
424b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
425b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
4281da177e4SLinus Torvalds 	bytes &= 511;
4291da177e4SLinus Torvalds 	inode->i_bytes += bytes;
4301da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
4311da177e4SLinus Torvalds 		inode->i_blocks++;
4321da177e4SLinus Torvalds 		inode->i_bytes -= 512;
4331da177e4SLinus Torvalds 	}
434b462707eSDmitry Monakhov }
435b462707eSDmitry Monakhov 
436b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
437b462707eSDmitry Monakhov {
438b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
439b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
4401da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds void inode_sub_bytes(struct inode *inode, loff_t bytes)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
4481da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
4491da177e4SLinus Torvalds 	bytes &= 511;
4501da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
4511da177e4SLinus Torvalds 		inode->i_blocks--;
4521da177e4SLinus Torvalds 		inode->i_bytes += 512;
4531da177e4SLinus Torvalds 	}
4541da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
4551da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	loff_t ret;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
4651da177e4SLinus Torvalds 	ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
4661da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4671da177e4SLinus Torvalds 	return ret;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
4751da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
4761da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
4771da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
481