xref: /openbmc/linux/fs/stat.c (revision 5b825c3af1d8a0af4deb4a5eb349d0d0050c62e5)
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>
15*5b825c3aSIngo Molnar #include <linux/cred.h>
161da177e4SLinus Torvalds #include <linux/syscalls.h>
17ba52de12STheodore Ts'o #include <linux/pagemap.h>
181da177e4SLinus Torvalds 
197c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
201da177e4SLinus Torvalds #include <asm/unistd.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds void generic_fillattr(struct inode *inode, struct kstat *stat)
231da177e4SLinus Torvalds {
241da177e4SLinus Torvalds 	stat->dev = inode->i_sb->s_dev;
251da177e4SLinus Torvalds 	stat->ino = inode->i_ino;
261da177e4SLinus Torvalds 	stat->mode = inode->i_mode;
271da177e4SLinus Torvalds 	stat->nlink = inode->i_nlink;
281da177e4SLinus Torvalds 	stat->uid = inode->i_uid;
291da177e4SLinus Torvalds 	stat->gid = inode->i_gid;
301da177e4SLinus Torvalds 	stat->rdev = inode->i_rdev;
313ddcd056SLinus Torvalds 	stat->size = i_size_read(inode);
321da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
331da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
341da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
3593407472SFabian Frederick 	stat->blksize = i_blocksize(inode);
363ddcd056SLinus Torvalds 	stat->blocks = inode->i_blocks;
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr);
401da177e4SLinus Torvalds 
41b7a6ec52SJ. Bruce Fields /**
42b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
43b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
44b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
45b7a6ec52SJ. Bruce Fields  *
46b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
47b7a6ec52SJ. Bruce Fields  *
48b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
49b7a6ec52SJ. Bruce Fields  * filehandle lookup code, which uses only the inode number and returns
50b7a6ec52SJ. Bruce Fields  * no attributes to any user.  Any other code probably wants
51b7a6ec52SJ. Bruce Fields  * vfs_getattr.
52b7a6ec52SJ. Bruce Fields  */
53b7a6ec52SJ. Bruce Fields int vfs_getattr_nosec(struct path *path, struct kstat *stat)
541da177e4SLinus Torvalds {
55bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	if (inode->i_op->getattr)
583dadecceSAl Viro 		return inode->i_op->getattr(path->mnt, path->dentry, stat);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	generic_fillattr(inode, stat);
611da177e4SLinus Torvalds 	return 0;
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
64b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
65b7a6ec52SJ. Bruce Fields 
66b7a6ec52SJ. Bruce Fields int vfs_getattr(struct path *path, struct kstat *stat)
67b7a6ec52SJ. Bruce Fields {
68b7a6ec52SJ. Bruce Fields 	int retval;
69b7a6ec52SJ. Bruce Fields 
703f7036a0SAl Viro 	retval = security_inode_getattr(path);
71b7a6ec52SJ. Bruce Fields 	if (retval)
72b7a6ec52SJ. Bruce Fields 		return retval;
73b7a6ec52SJ. Bruce Fields 	return vfs_getattr_nosec(path, stat);
74b7a6ec52SJ. Bruce Fields }
75b7a6ec52SJ. Bruce Fields 
761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds int vfs_fstat(unsigned int fd, struct kstat *stat)
791da177e4SLinus Torvalds {
802903ff01SAl Viro 	struct fd f = fdget_raw(fd);
811da177e4SLinus Torvalds 	int error = -EBADF;
821da177e4SLinus Torvalds 
832903ff01SAl Viro 	if (f.file) {
843dadecceSAl Viro 		error = vfs_getattr(&f.file->f_path, stat);
852903ff01SAl Viro 		fdput(f);
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 	return error;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_fstat);
901da177e4SLinus Torvalds 
91c7887325SDavid Howells int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
92c7887325SDavid Howells 		int flag)
930112fc22SOleg Drokin {
942eae7a18SChristoph Hellwig 	struct path path;
950112fc22SOleg Drokin 	int error = -EINVAL;
96836fb7e7SJeff Layton 	unsigned int lookup_flags = 0;
970112fc22SOleg Drokin 
9865cfc672SAl Viro 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
9965cfc672SAl Viro 		      AT_EMPTY_PATH)) != 0)
1000112fc22SOleg Drokin 		goto out;
1010112fc22SOleg Drokin 
1022eae7a18SChristoph Hellwig 	if (!(flag & AT_SYMLINK_NOFOLLOW))
1032eae7a18SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
10465cfc672SAl Viro 	if (flag & AT_EMPTY_PATH)
10565cfc672SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
106836fb7e7SJeff Layton retry:
1072eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
1082eae7a18SChristoph Hellwig 	if (error)
1092eae7a18SChristoph Hellwig 		goto out;
1102eae7a18SChristoph Hellwig 
1113dadecceSAl Viro 	error = vfs_getattr(&path, stat);
1122eae7a18SChristoph Hellwig 	path_put(&path);
113836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
114836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
115836fb7e7SJeff Layton 		goto retry;
116836fb7e7SJeff Layton 	}
1170112fc22SOleg Drokin out:
1180112fc22SOleg Drokin 	return error;
1190112fc22SOleg Drokin }
1200112fc22SOleg Drokin EXPORT_SYMBOL(vfs_fstatat);
1210112fc22SOleg Drokin 
122c7887325SDavid Howells int vfs_stat(const char __user *name, struct kstat *stat)
1232eae7a18SChristoph Hellwig {
1242eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, 0);
1252eae7a18SChristoph Hellwig }
1262eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_stat);
1272eae7a18SChristoph Hellwig 
128c7887325SDavid Howells int vfs_lstat(const char __user *name, struct kstat *stat)
1292eae7a18SChristoph Hellwig {
1302eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
1312eae7a18SChristoph Hellwig }
1322eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_lstat);
1332eae7a18SChristoph Hellwig 
1340112fc22SOleg Drokin 
1351da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
1391da177e4SLinus Torvalds  * into arch/i386 instead?
1401da177e4SLinus Torvalds  */
1411da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	static int warncount = 5;
1441da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	if (warncount > 0) {
1471da177e4SLinus Torvalds 		warncount--;
1481da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
1491da177e4SLinus Torvalds 			current->comm);
1501da177e4SLinus Torvalds 	} else if (warncount < 0) {
1511da177e4SLinus Torvalds 		/* it's laughable, but... */
1521da177e4SLinus Torvalds 		warncount = 0;
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
1561da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
1571da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
158afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
159afefdbb2SDavid Howells 		return -EOVERFLOW;
1601da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
1611da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
1621da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
1631da177e4SLinus Torvalds 		return -EOVERFLOW;
164a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
165a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
1661da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
1671da177e4SLinus Torvalds #if BITS_PER_LONG == 32
1681da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
1691da177e4SLinus Torvalds 		return -EOVERFLOW;
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 	tmp.st_size = stat->size;
1721da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
1731da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
1741da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
1751da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
178c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
179c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	struct kstat stat;
1822eae7a18SChristoph Hellwig 	int error;
1831da177e4SLinus Torvalds 
1842eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
1852eae7a18SChristoph Hellwig 	if (error)
1861da177e4SLinus Torvalds 		return error;
1872eae7a18SChristoph Hellwig 
1882eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
1891da177e4SLinus Torvalds }
190257ac264SHeiko Carstens 
191c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
192c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct kstat stat;
1952eae7a18SChristoph Hellwig 	int error;
1961da177e4SLinus Torvalds 
1972eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
1982eae7a18SChristoph Hellwig 	if (error)
1991da177e4SLinus Torvalds 		return error;
2002eae7a18SChristoph Hellwig 
2012eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2021da177e4SLinus Torvalds }
203257ac264SHeiko Carstens 
204257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	struct kstat stat;
2071da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	if (!error)
2101da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	return error;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
2161da177e4SLinus Torvalds 
217a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
218a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
219a52dd971SLinus Torvalds #else
220a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
221a52dd971SLinus Torvalds #endif
222a52dd971SLinus Torvalds 
2234c416f42SYaowei Bai #define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
224a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
225a52dd971SLinus Torvalds 
2268529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
2278529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
2288529f613SLinus Torvalds #endif
2298529f613SLinus Torvalds 
2301da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	struct stat tmp;
2331da177e4SLinus Torvalds 
234a52dd971SLinus Torvalds 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
2351da177e4SLinus Torvalds 		return -EOVERFLOW;
236a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
237a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2381da177e4SLinus Torvalds 		return -EOVERFLOW;
2391da177e4SLinus Torvalds #endif
2401da177e4SLinus Torvalds 
2418529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
242a52dd971SLinus Torvalds 	tmp.st_dev = encode_dev(stat->dev);
2431da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
244afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
245afefdbb2SDavid Howells 		return -EOVERFLOW;
2461da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2471da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2481da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2491da177e4SLinus Torvalds 		return -EOVERFLOW;
250a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
251a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
252a52dd971SLinus Torvalds 	tmp.st_rdev = encode_dev(stat->rdev);
2531da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2541da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2551da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2561da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2571da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
2581da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
2591da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
2601da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
2611da177e4SLinus Torvalds #endif
2621da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
2631da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
2641da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
267c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
268c7887325SDavid Howells 		struct stat __user *, statbuf)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	struct kstat stat;
2712eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
2721da177e4SLinus Torvalds 
2732eae7a18SChristoph Hellwig 	if (error)
2741da177e4SLinus Torvalds 		return error;
2752eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
2761da177e4SLinus Torvalds }
2775590ff0dSUlrich Drepper 
278c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
279c7887325SDavid Howells 		struct stat __user *, statbuf)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	struct kstat stat;
2822eae7a18SChristoph Hellwig 	int error;
2831da177e4SLinus Torvalds 
2842eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
2852eae7a18SChristoph Hellwig 	if (error)
2861da177e4SLinus Torvalds 		return error;
2872eae7a18SChristoph Hellwig 
2882eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
2891da177e4SLinus Torvalds }
2905590ff0dSUlrich Drepper 
2912833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
292c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
2936559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
2945590ff0dSUlrich Drepper {
2955590ff0dSUlrich Drepper 	struct kstat stat;
2960112fc22SOleg Drokin 	int error;
2975590ff0dSUlrich Drepper 
2980112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
2990112fc22SOleg Drokin 	if (error)
3005590ff0dSUlrich Drepper 		return error;
3010112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
3025590ff0dSUlrich Drepper }
303cff2b760SUlrich Drepper #endif
3045590ff0dSUlrich Drepper 
305257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds 	struct kstat stat;
3081da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	if (!error)
3111da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	return error;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3166559eed8SHeiko Carstens SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
3176559eed8SHeiko Carstens 		char __user *, buf, int, bufsiz)
3181da177e4SLinus Torvalds {
3192d8f3038SAl Viro 	struct path path;
3201da177e4SLinus Torvalds 	int error;
3211fa1e7f6SAndy Whitcroft 	int empty = 0;
3227955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	if (bufsiz <= 0)
3251da177e4SLinus Torvalds 		return -EINVAL;
3261da177e4SLinus Torvalds 
3277955119eSJeff Layton retry:
3287955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
3291da177e4SLinus Torvalds 	if (!error) {
330bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
3311da177e4SLinus Torvalds 
3321fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
333fd4a0edfSMiklos Szeredi 		/*
334fd4a0edfSMiklos Szeredi 		 * AFS mountpoints allow readlink(2) but are not symlinks
335fd4a0edfSMiklos Szeredi 		 */
336fd4a0edfSMiklos Szeredi 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
3372d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
3381da177e4SLinus Torvalds 			if (!error) {
33968ac1234SAl Viro 				touch_atime(&path);
340fd4a0edfSMiklos Szeredi 				error = vfs_readlink(path.dentry, buf, bufsiz);
3411da177e4SLinus Torvalds 			}
3421da177e4SLinus Torvalds 		}
3432d8f3038SAl Viro 		path_put(&path);
3447955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
3457955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
3467955119eSJeff Layton 			goto retry;
3477955119eSJeff Layton 		}
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 	return error;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
353002c8976SHeiko Carstens 		int, bufsiz)
3545590ff0dSUlrich Drepper {
3555590ff0dSUlrich Drepper 	return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
3565590ff0dSUlrich Drepper }
3575590ff0dSUlrich Drepper 
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
3600753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
3611da177e4SLinus Torvalds 
3628529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
3638529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
3648529f613SLinus Torvalds #endif
3658529f613SLinus Torvalds 
3661da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct stat64 tmp;
3691da177e4SLinus Torvalds 
3708529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
3711da177e4SLinus Torvalds #ifdef CONFIG_MIPS
3721da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
3731da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
3741da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
3751da177e4SLinus Torvalds #else
3761da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
3771da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
3781da177e4SLinus Torvalds #endif
3791da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
380afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
381afefdbb2SDavid Howells 		return -EOVERFLOW;
3821da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
3831da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
3841da177e4SLinus Torvalds #endif
3851da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3861da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
387a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
388a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
3891da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3901da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3911da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3921da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3931da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3941da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3951da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3961da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3971da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3981da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
401c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
402c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct kstat stat;
4051da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (!error)
4081da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	return error;
4111da177e4SLinus Torvalds }
412257ac264SHeiko Carstens 
413c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
414c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct kstat stat;
4171da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if (!error)
4201da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	return error;
4231da177e4SLinus Torvalds }
424257ac264SHeiko Carstens 
425257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	struct kstat stat;
4281da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (!error)
4311da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	return error;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
436c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
4376559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
438cff2b760SUlrich Drepper {
439cff2b760SUlrich Drepper 	struct kstat stat;
4400112fc22SOleg Drokin 	int error;
441cff2b760SUlrich Drepper 
4420112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
4430112fc22SOleg Drokin 	if (error)
444cff2b760SUlrich Drepper 		return error;
4450112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
446cff2b760SUlrich Drepper }
4470753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
4481da177e4SLinus Torvalds 
449b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
450b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
4531da177e4SLinus Torvalds 	bytes &= 511;
4541da177e4SLinus Torvalds 	inode->i_bytes += bytes;
4551da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
4561da177e4SLinus Torvalds 		inode->i_blocks++;
4571da177e4SLinus Torvalds 		inode->i_bytes -= 512;
4581da177e4SLinus Torvalds 	}
459b462707eSDmitry Monakhov }
460b462707eSDmitry Monakhov 
461b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
462b462707eSDmitry Monakhov {
463b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
464b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
4651da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
4691da177e4SLinus Torvalds 
4701c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
4731da177e4SLinus Torvalds 	bytes &= 511;
4741da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
4751da177e4SLinus Torvalds 		inode->i_blocks--;
4761da177e4SLinus Torvalds 		inode->i_bytes += 512;
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
4791c8924ebSJan Kara }
4801c8924ebSJan Kara 
4811c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
4821c8924ebSJan Kara 
4831c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
4841c8924ebSJan Kara {
4851c8924ebSJan Kara 	spin_lock(&inode->i_lock);
4861c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
4871da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	loff_t ret;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
4971da177e4SLinus Torvalds 	ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
4981da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4991da177e4SLinus Torvalds 	return ret;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
5071da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
5081da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
5091da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
513