xref: /openbmc/linux/fs/stat.c (revision bb668734c4c960c8f61f017585b323b97e5f47b5)
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 
40b7a6ec52SJ. Bruce Fields /**
41b7a6ec52SJ. Bruce Fields  * vfs_getattr_nosec - getattr without security checks
42b7a6ec52SJ. Bruce Fields  * @path: file to get attributes from
43b7a6ec52SJ. Bruce Fields  * @stat: structure to return attributes in
44b7a6ec52SJ. Bruce Fields  *
45b7a6ec52SJ. Bruce Fields  * Get attributes without calling security_inode_getattr.
46b7a6ec52SJ. Bruce Fields  *
47b7a6ec52SJ. Bruce Fields  * Currently the only caller other than vfs_getattr is internal to the
48b7a6ec52SJ. Bruce Fields  * filehandle lookup code, which uses only the inode number and returns
49b7a6ec52SJ. Bruce Fields  * no attributes to any user.  Any other code probably wants
50b7a6ec52SJ. Bruce Fields  * vfs_getattr.
51b7a6ec52SJ. Bruce Fields  */
52b7a6ec52SJ. Bruce Fields int vfs_getattr_nosec(struct path *path, struct kstat *stat)
531da177e4SLinus Torvalds {
54*bb668734SDavid Howells 	struct inode *inode = d_backing_inode(path->dentry);
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	if (inode->i_op->getattr)
573dadecceSAl Viro 		return inode->i_op->getattr(path->mnt, path->dentry, stat);
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	generic_fillattr(inode, stat);
601da177e4SLinus Torvalds 	return 0;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
63b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec);
64b7a6ec52SJ. Bruce Fields 
65b7a6ec52SJ. Bruce Fields int vfs_getattr(struct path *path, struct kstat *stat)
66b7a6ec52SJ. Bruce Fields {
67b7a6ec52SJ. Bruce Fields 	int retval;
68b7a6ec52SJ. Bruce Fields 
693f7036a0SAl Viro 	retval = security_inode_getattr(path);
70b7a6ec52SJ. Bruce Fields 	if (retval)
71b7a6ec52SJ. Bruce Fields 		return retval;
72b7a6ec52SJ. Bruce Fields 	return vfs_getattr_nosec(path, stat);
73b7a6ec52SJ. Bruce Fields }
74b7a6ec52SJ. Bruce Fields 
751da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds int vfs_fstat(unsigned int fd, struct kstat *stat)
781da177e4SLinus Torvalds {
792903ff01SAl Viro 	struct fd f = fdget_raw(fd);
801da177e4SLinus Torvalds 	int error = -EBADF;
811da177e4SLinus Torvalds 
822903ff01SAl Viro 	if (f.file) {
833dadecceSAl Viro 		error = vfs_getattr(&f.file->f_path, stat);
842903ff01SAl Viro 		fdput(f);
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 	return error;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_fstat);
891da177e4SLinus Torvalds 
90c7887325SDavid Howells int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
91c7887325SDavid Howells 		int flag)
920112fc22SOleg Drokin {
932eae7a18SChristoph Hellwig 	struct path path;
940112fc22SOleg Drokin 	int error = -EINVAL;
95836fb7e7SJeff Layton 	unsigned int lookup_flags = 0;
960112fc22SOleg Drokin 
9765cfc672SAl Viro 	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
9865cfc672SAl Viro 		      AT_EMPTY_PATH)) != 0)
990112fc22SOleg Drokin 		goto out;
1000112fc22SOleg Drokin 
1012eae7a18SChristoph Hellwig 	if (!(flag & AT_SYMLINK_NOFOLLOW))
1022eae7a18SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
10365cfc672SAl Viro 	if (flag & AT_EMPTY_PATH)
10465cfc672SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
105836fb7e7SJeff Layton retry:
1062eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
1072eae7a18SChristoph Hellwig 	if (error)
1082eae7a18SChristoph Hellwig 		goto out;
1092eae7a18SChristoph Hellwig 
1103dadecceSAl Viro 	error = vfs_getattr(&path, stat);
1112eae7a18SChristoph Hellwig 	path_put(&path);
112836fb7e7SJeff Layton 	if (retry_estale(error, lookup_flags)) {
113836fb7e7SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
114836fb7e7SJeff Layton 		goto retry;
115836fb7e7SJeff Layton 	}
1160112fc22SOleg Drokin out:
1170112fc22SOleg Drokin 	return error;
1180112fc22SOleg Drokin }
1190112fc22SOleg Drokin EXPORT_SYMBOL(vfs_fstatat);
1200112fc22SOleg Drokin 
121c7887325SDavid Howells int vfs_stat(const char __user *name, struct kstat *stat)
1222eae7a18SChristoph Hellwig {
1232eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, 0);
1242eae7a18SChristoph Hellwig }
1252eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_stat);
1262eae7a18SChristoph Hellwig 
127c7887325SDavid Howells int vfs_lstat(const char __user *name, struct kstat *stat)
1282eae7a18SChristoph Hellwig {
1292eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
1302eae7a18SChristoph Hellwig }
1312eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_lstat);
1322eae7a18SChristoph Hellwig 
1330112fc22SOleg Drokin 
1341da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*
1371da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
1381da177e4SLinus Torvalds  * into arch/i386 instead?
1391da177e4SLinus Torvalds  */
1401da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	static int warncount = 5;
1431da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	if (warncount > 0) {
1461da177e4SLinus Torvalds 		warncount--;
1471da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
1481da177e4SLinus Torvalds 			current->comm);
1491da177e4SLinus Torvalds 	} else if (warncount < 0) {
1501da177e4SLinus Torvalds 		/* it's laughable, but... */
1511da177e4SLinus Torvalds 		warncount = 0;
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
1551da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
1561da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
157afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
158afefdbb2SDavid Howells 		return -EOVERFLOW;
1591da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
1601da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
1611da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
1621da177e4SLinus Torvalds 		return -EOVERFLOW;
163a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
164a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
1651da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
1661da177e4SLinus Torvalds #if BITS_PER_LONG == 32
1671da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
1681da177e4SLinus Torvalds 		return -EOVERFLOW;
1691da177e4SLinus Torvalds #endif
1701da177e4SLinus Torvalds 	tmp.st_size = stat->size;
1711da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
1721da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
1731da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
1741da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
177c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
178c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	struct kstat stat;
1812eae7a18SChristoph Hellwig 	int error;
1821da177e4SLinus Torvalds 
1832eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
1842eae7a18SChristoph Hellwig 	if (error)
1851da177e4SLinus Torvalds 		return error;
1862eae7a18SChristoph Hellwig 
1872eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
1881da177e4SLinus Torvalds }
189257ac264SHeiko Carstens 
190c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
191c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	struct kstat stat;
1942eae7a18SChristoph Hellwig 	int error;
1951da177e4SLinus Torvalds 
1962eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
1972eae7a18SChristoph Hellwig 	if (error)
1981da177e4SLinus Torvalds 		return error;
1992eae7a18SChristoph Hellwig 
2002eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
2011da177e4SLinus Torvalds }
202257ac264SHeiko Carstens 
203257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	struct kstat stat;
2061da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	if (!error)
2091da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	return error;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
2151da177e4SLinus Torvalds 
216a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
217a52dd971SLinus Torvalds #  define choose_32_64(a,b) a
218a52dd971SLinus Torvalds #else
219a52dd971SLinus Torvalds #  define choose_32_64(a,b) b
220a52dd971SLinus Torvalds #endif
221a52dd971SLinus Torvalds 
222a52dd971SLinus Torvalds #define valid_dev(x)  choose_32_64(old_valid_dev,new_valid_dev)(x)
223a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
224a52dd971SLinus Torvalds 
2258529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING
2268529f613SLinus Torvalds #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
2278529f613SLinus Torvalds #endif
2288529f613SLinus Torvalds 
2291da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	struct stat tmp;
2321da177e4SLinus Torvalds 
233a52dd971SLinus Torvalds 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
2341da177e4SLinus Torvalds 		return -EOVERFLOW;
235a52dd971SLinus Torvalds #if BITS_PER_LONG == 32
236a52dd971SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2371da177e4SLinus Torvalds 		return -EOVERFLOW;
2381da177e4SLinus Torvalds #endif
2391da177e4SLinus Torvalds 
2408529f613SLinus Torvalds 	INIT_STRUCT_STAT_PADDING(tmp);
241a52dd971SLinus Torvalds 	tmp.st_dev = encode_dev(stat->dev);
2421da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
243afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
244afefdbb2SDavid Howells 		return -EOVERFLOW;
2451da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2461da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2471da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2481da177e4SLinus Torvalds 		return -EOVERFLOW;
249a7c1938eSEric W. Biederman 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
250a7c1938eSEric W. Biederman 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
251a52dd971SLinus Torvalds 	tmp.st_rdev = encode_dev(stat->rdev);
2521da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2531da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2541da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2551da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2561da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
2571da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
2581da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
2591da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
2601da177e4SLinus Torvalds #endif
2611da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
2621da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
2631da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
266c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
267c7887325SDavid Howells 		struct stat __user *, statbuf)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	struct kstat stat;
2702eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
2711da177e4SLinus Torvalds 
2722eae7a18SChristoph Hellwig 	if (error)
2731da177e4SLinus Torvalds 		return error;
2742eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
2751da177e4SLinus Torvalds }
2765590ff0dSUlrich Drepper 
277c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
278c7887325SDavid Howells 		struct stat __user *, statbuf)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct kstat stat;
2812eae7a18SChristoph Hellwig 	int error;
2821da177e4SLinus Torvalds 
2832eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
2842eae7a18SChristoph Hellwig 	if (error)
2851da177e4SLinus Torvalds 		return error;
2862eae7a18SChristoph Hellwig 
2872eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
2881da177e4SLinus Torvalds }
2895590ff0dSUlrich Drepper 
2902833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
291c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
2926559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
2935590ff0dSUlrich Drepper {
2945590ff0dSUlrich Drepper 	struct kstat stat;
2950112fc22SOleg Drokin 	int error;
2965590ff0dSUlrich Drepper 
2970112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
2980112fc22SOleg Drokin 	if (error)
2995590ff0dSUlrich Drepper 		return error;
3000112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
3015590ff0dSUlrich Drepper }
302cff2b760SUlrich Drepper #endif
3035590ff0dSUlrich Drepper 
304257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	struct kstat stat;
3071da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	if (!error)
3101da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	return error;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3156559eed8SHeiko Carstens SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
3166559eed8SHeiko Carstens 		char __user *, buf, int, bufsiz)
3171da177e4SLinus Torvalds {
3182d8f3038SAl Viro 	struct path path;
3191da177e4SLinus Torvalds 	int error;
3201fa1e7f6SAndy Whitcroft 	int empty = 0;
3217955119eSJeff Layton 	unsigned int lookup_flags = LOOKUP_EMPTY;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	if (bufsiz <= 0)
3241da177e4SLinus Torvalds 		return -EINVAL;
3251da177e4SLinus Torvalds 
3267955119eSJeff Layton retry:
3277955119eSJeff Layton 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
3281da177e4SLinus Torvalds 	if (!error) {
329*bb668734SDavid Howells 		struct inode *inode = d_backing_inode(path.dentry);
3301da177e4SLinus Torvalds 
3311fa1e7f6SAndy Whitcroft 		error = empty ? -ENOENT : -EINVAL;
332acfa4380SAl Viro 		if (inode->i_op->readlink) {
3332d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
3341da177e4SLinus Torvalds 			if (!error) {
33568ac1234SAl Viro 				touch_atime(&path);
3362d8f3038SAl Viro 				error = inode->i_op->readlink(path.dentry,
3374ac91378SJan Blunck 							      buf, bufsiz);
3381da177e4SLinus Torvalds 			}
3391da177e4SLinus Torvalds 		}
3402d8f3038SAl Viro 		path_put(&path);
3417955119eSJeff Layton 		if (retry_estale(error, lookup_flags)) {
3427955119eSJeff Layton 			lookup_flags |= LOOKUP_REVAL;
3437955119eSJeff Layton 			goto retry;
3447955119eSJeff Layton 		}
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 	return error;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
349002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
350002c8976SHeiko Carstens 		int, bufsiz)
3515590ff0dSUlrich Drepper {
3525590ff0dSUlrich Drepper 	return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
3535590ff0dSUlrich Drepper }
3545590ff0dSUlrich Drepper 
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
3570753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
3581da177e4SLinus Torvalds 
3598529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING
3608529f613SLinus Torvalds #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
3618529f613SLinus Torvalds #endif
3628529f613SLinus Torvalds 
3631da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	struct stat64 tmp;
3661da177e4SLinus Torvalds 
3678529f613SLinus Torvalds 	INIT_STRUCT_STAT64_PADDING(tmp);
3681da177e4SLinus Torvalds #ifdef CONFIG_MIPS
3691da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
3701da177e4SLinus Torvalds 	if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
3711da177e4SLinus Torvalds 		return -EOVERFLOW;
3721da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
3731da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
3741da177e4SLinus Torvalds #else
3751da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
3761da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
3771da177e4SLinus Torvalds #endif
3781da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
379afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
380afefdbb2SDavid Howells 		return -EOVERFLOW;
3811da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
3821da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
3831da177e4SLinus Torvalds #endif
3841da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3851da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
386a7c1938eSEric W. Biederman 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
387a7c1938eSEric W. Biederman 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
3881da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3891da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3901da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3911da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3921da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3931da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3941da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3951da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3961da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3971da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
400c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
401c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct kstat stat;
4041da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	if (!error)
4071da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	return error;
4101da177e4SLinus Torvalds }
411257ac264SHeiko Carstens 
412c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
413c7887325SDavid Howells 		struct stat64 __user *, statbuf)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	struct kstat stat;
4161da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (!error)
4191da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	return error;
4221da177e4SLinus Torvalds }
423257ac264SHeiko Carstens 
424257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	struct kstat stat;
4271da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if (!error)
4301da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	return error;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
435c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
4366559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
437cff2b760SUlrich Drepper {
438cff2b760SUlrich Drepper 	struct kstat stat;
4390112fc22SOleg Drokin 	int error;
440cff2b760SUlrich Drepper 
4410112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
4420112fc22SOleg Drokin 	if (error)
443cff2b760SUlrich Drepper 		return error;
4440112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
445cff2b760SUlrich Drepper }
4460753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
4471da177e4SLinus Torvalds 
448b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
449b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
4521da177e4SLinus Torvalds 	bytes &= 511;
4531da177e4SLinus Torvalds 	inode->i_bytes += bytes;
4541da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
4551da177e4SLinus Torvalds 		inode->i_blocks++;
4561da177e4SLinus Torvalds 		inode->i_bytes -= 512;
4571da177e4SLinus Torvalds 	}
458b462707eSDmitry Monakhov }
459b462707eSDmitry Monakhov 
460b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
461b462707eSDmitry Monakhov {
462b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
463b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
4641da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
4681da177e4SLinus Torvalds 
4691c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
4721da177e4SLinus Torvalds 	bytes &= 511;
4731da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
4741da177e4SLinus Torvalds 		inode->i_blocks--;
4751da177e4SLinus Torvalds 		inode->i_bytes += 512;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
4781c8924ebSJan Kara }
4791c8924ebSJan Kara 
4801c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes);
4811c8924ebSJan Kara 
4821c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes)
4831c8924ebSJan Kara {
4841c8924ebSJan Kara 	spin_lock(&inode->i_lock);
4851c8924ebSJan Kara 	__inode_sub_bytes(inode, bytes);
4861da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	loff_t ret;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
4961da177e4SLinus Torvalds 	ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
4971da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4981da177e4SLinus Torvalds 	return ret;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
5061da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
5071da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
5081da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
512