xref: /openbmc/linux/fs/stat.c (revision c7887325230aec47d47a32562a6e26014a0fafca)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/stat.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/module.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;
301da177e4SLinus Torvalds 	stat->atime = inode->i_atime;
311da177e4SLinus Torvalds 	stat->mtime = inode->i_mtime;
321da177e4SLinus Torvalds 	stat->ctime = inode->i_ctime;
331da177e4SLinus Torvalds 	stat->size = i_size_read(inode);
341da177e4SLinus Torvalds 	stat->blocks = inode->i_blocks;
35ba52de12STheodore Ts'o 	stat->blksize = (1 << inode->i_blkbits);
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 {
601da177e4SLinus Torvalds 	struct file *f = fget(fd);
611da177e4SLinus Torvalds 	int error = -EBADF;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	if (f) {
640f7fc9e4SJosef "Jeff" Sipek 		error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, stat);
651da177e4SLinus Torvalds 		fput(f);
661da177e4SLinus Torvalds 	}
671da177e4SLinus Torvalds 	return error;
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_fstat);
701da177e4SLinus Torvalds 
71*c7887325SDavid Howells int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
72*c7887325SDavid Howells 		int flag)
730112fc22SOleg Drokin {
742eae7a18SChristoph Hellwig 	struct path path;
750112fc22SOleg Drokin 	int error = -EINVAL;
762eae7a18SChristoph Hellwig 	int lookup_flags = 0;
770112fc22SOleg Drokin 
780112fc22SOleg Drokin 	if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
790112fc22SOleg Drokin 		goto out;
800112fc22SOleg Drokin 
812eae7a18SChristoph Hellwig 	if (!(flag & AT_SYMLINK_NOFOLLOW))
822eae7a18SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
832eae7a18SChristoph Hellwig 
842eae7a18SChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
852eae7a18SChristoph Hellwig 	if (error)
862eae7a18SChristoph Hellwig 		goto out;
872eae7a18SChristoph Hellwig 
882eae7a18SChristoph Hellwig 	error = vfs_getattr(path.mnt, path.dentry, stat);
892eae7a18SChristoph Hellwig 	path_put(&path);
900112fc22SOleg Drokin out:
910112fc22SOleg Drokin 	return error;
920112fc22SOleg Drokin }
930112fc22SOleg Drokin EXPORT_SYMBOL(vfs_fstatat);
940112fc22SOleg Drokin 
95*c7887325SDavid Howells int vfs_stat(const char __user *name, struct kstat *stat)
962eae7a18SChristoph Hellwig {
972eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, 0);
982eae7a18SChristoph Hellwig }
992eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_stat);
1002eae7a18SChristoph Hellwig 
101*c7887325SDavid Howells int vfs_lstat(const char __user *name, struct kstat *stat)
1022eae7a18SChristoph Hellwig {
1032eae7a18SChristoph Hellwig 	return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
1042eae7a18SChristoph Hellwig }
1052eae7a18SChristoph Hellwig EXPORT_SYMBOL(vfs_lstat);
1062eae7a18SChristoph Hellwig 
1070112fc22SOleg Drokin 
1081da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * For backward compatibility?  Maybe this should be moved
1121da177e4SLinus Torvalds  * into arch/i386 instead?
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	static int warncount = 5;
1171da177e4SLinus Torvalds 	struct __old_kernel_stat tmp;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	if (warncount > 0) {
1201da177e4SLinus Torvalds 		warncount--;
1211da177e4SLinus Torvalds 		printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
1221da177e4SLinus Torvalds 			current->comm);
1231da177e4SLinus Torvalds 	} else if (warncount < 0) {
1241da177e4SLinus Torvalds 		/* it's laughable, but... */
1251da177e4SLinus Torvalds 		warncount = 0;
1261da177e4SLinus Torvalds 	}
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct __old_kernel_stat));
1291da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
1301da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
131afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
132afefdbb2SDavid Howells 		return -EOVERFLOW;
1331da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
1341da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
1351da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
1361da177e4SLinus Torvalds 		return -EOVERFLOW;
1371da177e4SLinus Torvalds 	SET_UID(tmp.st_uid, stat->uid);
1381da177e4SLinus Torvalds 	SET_GID(tmp.st_gid, stat->gid);
1391da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
1401da177e4SLinus Torvalds #if BITS_PER_LONG == 32
1411da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
1421da177e4SLinus Torvalds 		return -EOVERFLOW;
1431da177e4SLinus Torvalds #endif
1441da177e4SLinus Torvalds 	tmp.st_size = stat->size;
1451da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
1461da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
1471da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
1481da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
151*c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename,
152*c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct kstat stat;
1552eae7a18SChristoph Hellwig 	int error;
1561da177e4SLinus Torvalds 
1572eae7a18SChristoph Hellwig 	error = vfs_stat(filename, &stat);
1582eae7a18SChristoph Hellwig 	if (error)
1591da177e4SLinus Torvalds 		return error;
1602eae7a18SChristoph Hellwig 
1612eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
1621da177e4SLinus Torvalds }
163257ac264SHeiko Carstens 
164*c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename,
165*c7887325SDavid Howells 		struct __old_kernel_stat __user *, statbuf)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	struct kstat stat;
1682eae7a18SChristoph Hellwig 	int error;
1691da177e4SLinus Torvalds 
1702eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
1712eae7a18SChristoph Hellwig 	if (error)
1721da177e4SLinus Torvalds 		return error;
1732eae7a18SChristoph Hellwig 
1742eae7a18SChristoph Hellwig 	return cp_old_stat(&stat, statbuf);
1751da177e4SLinus Torvalds }
176257ac264SHeiko Carstens 
177257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct kstat stat;
1801da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if (!error)
1831da177e4SLinus Torvalds 		error = cp_old_stat(&stat, statbuf);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	return error;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct stat tmp;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds #if BITS_PER_LONG == 32
1951da177e4SLinus Torvalds 	if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
1961da177e4SLinus Torvalds 		return -EOVERFLOW;
1971da177e4SLinus Torvalds #else
1981da177e4SLinus Torvalds 	if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
1991da177e4SLinus Torvalds 		return -EOVERFLOW;
2001da177e4SLinus Torvalds #endif
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(tmp));
2031da177e4SLinus Torvalds #if BITS_PER_LONG == 32
2041da177e4SLinus Torvalds 	tmp.st_dev = old_encode_dev(stat->dev);
2051da177e4SLinus Torvalds #else
2061da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
2071da177e4SLinus Torvalds #endif
2081da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
209afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
210afefdbb2SDavid Howells 		return -EOVERFLOW;
2111da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
2121da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
2131da177e4SLinus Torvalds 	if (tmp.st_nlink != stat->nlink)
2141da177e4SLinus Torvalds 		return -EOVERFLOW;
2151da177e4SLinus Torvalds 	SET_UID(tmp.st_uid, stat->uid);
2161da177e4SLinus Torvalds 	SET_GID(tmp.st_gid, stat->gid);
2171da177e4SLinus Torvalds #if BITS_PER_LONG == 32
2181da177e4SLinus Torvalds 	tmp.st_rdev = old_encode_dev(stat->rdev);
2191da177e4SLinus Torvalds #else
2201da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
2211da177e4SLinus Torvalds #endif
2221da177e4SLinus Torvalds #if BITS_PER_LONG == 32
2231da177e4SLinus Torvalds 	if (stat->size > MAX_NON_LFS)
2241da177e4SLinus Torvalds 		return -EOVERFLOW;
2251da177e4SLinus Torvalds #endif
2261da177e4SLinus Torvalds 	tmp.st_size = stat->size;
2271da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
2281da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
2291da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
2301da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC
2311da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
2321da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
2331da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
2341da177e4SLinus Torvalds #endif
2351da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
2361da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
2371da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
240*c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename,
241*c7887325SDavid Howells 		struct stat __user *, statbuf)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	struct kstat stat;
2442eae7a18SChristoph Hellwig 	int error = vfs_stat(filename, &stat);
2451da177e4SLinus Torvalds 
2462eae7a18SChristoph Hellwig 	if (error)
2471da177e4SLinus Torvalds 		return error;
2482eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
2491da177e4SLinus Torvalds }
2505590ff0dSUlrich Drepper 
251*c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename,
252*c7887325SDavid Howells 		struct stat __user *, statbuf)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	struct kstat stat;
2552eae7a18SChristoph Hellwig 	int error;
2561da177e4SLinus Torvalds 
2572eae7a18SChristoph Hellwig 	error = vfs_lstat(filename, &stat);
2582eae7a18SChristoph Hellwig 	if (error)
2591da177e4SLinus Torvalds 		return error;
2602eae7a18SChristoph Hellwig 
2612eae7a18SChristoph Hellwig 	return cp_new_stat(&stat, statbuf);
2621da177e4SLinus Torvalds }
2635590ff0dSUlrich Drepper 
2642833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
265*c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
2666559eed8SHeiko Carstens 		struct stat __user *, statbuf, int, flag)
2675590ff0dSUlrich Drepper {
2685590ff0dSUlrich Drepper 	struct kstat stat;
2690112fc22SOleg Drokin 	int error;
2705590ff0dSUlrich Drepper 
2710112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
2720112fc22SOleg Drokin 	if (error)
2735590ff0dSUlrich Drepper 		return error;
2740112fc22SOleg Drokin 	return cp_new_stat(&stat, statbuf);
2755590ff0dSUlrich Drepper }
276cff2b760SUlrich Drepper #endif
2775590ff0dSUlrich Drepper 
278257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct kstat stat;
2811da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	if (!error)
2841da177e4SLinus Torvalds 		error = cp_new_stat(&stat, statbuf);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	return error;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2896559eed8SHeiko Carstens SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
2906559eed8SHeiko Carstens 		char __user *, buf, int, bufsiz)
2911da177e4SLinus Torvalds {
2922d8f3038SAl Viro 	struct path path;
2931da177e4SLinus Torvalds 	int error;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	if (bufsiz <= 0)
2961da177e4SLinus Torvalds 		return -EINVAL;
2971da177e4SLinus Torvalds 
2982d8f3038SAl Viro 	error = user_path_at(dfd, pathname, 0, &path);
2991da177e4SLinus Torvalds 	if (!error) {
3002d8f3038SAl Viro 		struct inode *inode = path.dentry->d_inode;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 		error = -EINVAL;
303acfa4380SAl Viro 		if (inode->i_op->readlink) {
3042d8f3038SAl Viro 			error = security_inode_readlink(path.dentry);
3051da177e4SLinus Torvalds 			if (!error) {
3062d8f3038SAl Viro 				touch_atime(path.mnt, path.dentry);
3072d8f3038SAl Viro 				error = inode->i_op->readlink(path.dentry,
3084ac91378SJan Blunck 							      buf, bufsiz);
3091da177e4SLinus Torvalds 			}
3101da177e4SLinus Torvalds 		}
3112d8f3038SAl Viro 		path_put(&path);
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	return error;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
316002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
317002c8976SHeiko Carstens 		int, bufsiz)
3185590ff0dSUlrich Drepper {
3195590ff0dSUlrich Drepper 	return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
3205590ff0dSUlrich Drepper }
3215590ff0dSUlrich Drepper 
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */
3241da177e4SLinus Torvalds #ifdef __ARCH_WANT_STAT64
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct stat64 tmp;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	memset(&tmp, 0, sizeof(struct stat64));
3311da177e4SLinus Torvalds #ifdef CONFIG_MIPS
3321da177e4SLinus Torvalds 	/* mips has weird padding, so we don't get 64 bits there */
3331da177e4SLinus Torvalds 	if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
3341da177e4SLinus Torvalds 		return -EOVERFLOW;
3351da177e4SLinus Torvalds 	tmp.st_dev = new_encode_dev(stat->dev);
3361da177e4SLinus Torvalds 	tmp.st_rdev = new_encode_dev(stat->rdev);
3371da177e4SLinus Torvalds #else
3381da177e4SLinus Torvalds 	tmp.st_dev = huge_encode_dev(stat->dev);
3391da177e4SLinus Torvalds 	tmp.st_rdev = huge_encode_dev(stat->rdev);
3401da177e4SLinus Torvalds #endif
3411da177e4SLinus Torvalds 	tmp.st_ino = stat->ino;
342afefdbb2SDavid Howells 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
343afefdbb2SDavid Howells 		return -EOVERFLOW;
3441da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO
3451da177e4SLinus Torvalds 	tmp.__st_ino = stat->ino;
3461da177e4SLinus Torvalds #endif
3471da177e4SLinus Torvalds 	tmp.st_mode = stat->mode;
3481da177e4SLinus Torvalds 	tmp.st_nlink = stat->nlink;
3491da177e4SLinus Torvalds 	tmp.st_uid = stat->uid;
3501da177e4SLinus Torvalds 	tmp.st_gid = stat->gid;
3511da177e4SLinus Torvalds 	tmp.st_atime = stat->atime.tv_sec;
3521da177e4SLinus Torvalds 	tmp.st_atime_nsec = stat->atime.tv_nsec;
3531da177e4SLinus Torvalds 	tmp.st_mtime = stat->mtime.tv_sec;
3541da177e4SLinus Torvalds 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
3551da177e4SLinus Torvalds 	tmp.st_ctime = stat->ctime.tv_sec;
3561da177e4SLinus Torvalds 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
3571da177e4SLinus Torvalds 	tmp.st_size = stat->size;
3581da177e4SLinus Torvalds 	tmp.st_blocks = stat->blocks;
3591da177e4SLinus Torvalds 	tmp.st_blksize = stat->blksize;
3601da177e4SLinus Torvalds 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
363*c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename,
364*c7887325SDavid Howells 		struct stat64 __user *, statbuf)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	struct kstat stat;
3671da177e4SLinus Torvalds 	int error = vfs_stat(filename, &stat);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	if (!error)
3701da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	return error;
3731da177e4SLinus Torvalds }
374257ac264SHeiko Carstens 
375*c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename,
376*c7887325SDavid Howells 		struct stat64 __user *, statbuf)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct kstat stat;
3791da177e4SLinus Torvalds 	int error = vfs_lstat(filename, &stat);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (!error)
3821da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	return error;
3851da177e4SLinus Torvalds }
386257ac264SHeiko Carstens 
387257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	struct kstat stat;
3901da177e4SLinus Torvalds 	int error = vfs_fstat(fd, &stat);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	if (!error)
3931da177e4SLinus Torvalds 		error = cp_new_stat64(&stat, statbuf);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	return error;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
398*c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
3996559eed8SHeiko Carstens 		struct stat64 __user *, statbuf, int, flag)
400cff2b760SUlrich Drepper {
401cff2b760SUlrich Drepper 	struct kstat stat;
4020112fc22SOleg Drokin 	int error;
403cff2b760SUlrich Drepper 
4040112fc22SOleg Drokin 	error = vfs_fstatat(dfd, filename, &stat, flag);
4050112fc22SOleg Drokin 	if (error)
406cff2b760SUlrich Drepper 		return error;
4070112fc22SOleg Drokin 	return cp_new_stat64(&stat, statbuf);
408cff2b760SUlrich Drepper }
4091da177e4SLinus Torvalds #endif /* __ARCH_WANT_STAT64 */
4101da177e4SLinus Torvalds 
411b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
412b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	inode->i_blocks += bytes >> 9;
4151da177e4SLinus Torvalds 	bytes &= 511;
4161da177e4SLinus Torvalds 	inode->i_bytes += bytes;
4171da177e4SLinus Torvalds 	if (inode->i_bytes >= 512) {
4181da177e4SLinus Torvalds 		inode->i_blocks++;
4191da177e4SLinus Torvalds 		inode->i_bytes -= 512;
4201da177e4SLinus Torvalds 	}
421b462707eSDmitry Monakhov }
422b462707eSDmitry Monakhov 
423b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes)
424b462707eSDmitry Monakhov {
425b462707eSDmitry Monakhov 	spin_lock(&inode->i_lock);
426b462707eSDmitry Monakhov 	__inode_add_bytes(inode, bytes);
4271da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds void inode_sub_bytes(struct inode *inode, loff_t bytes)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
4351da177e4SLinus Torvalds 	inode->i_blocks -= bytes >> 9;
4361da177e4SLinus Torvalds 	bytes &= 511;
4371da177e4SLinus Torvalds 	if (inode->i_bytes < bytes) {
4381da177e4SLinus Torvalds 		inode->i_blocks--;
4391da177e4SLinus Torvalds 		inode->i_bytes += 512;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 	inode->i_bytes -= bytes;
4421da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	loff_t ret;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
4521da177e4SLinus Torvalds 	ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
4531da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
4541da177e4SLinus Torvalds 	return ret;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	/* Caller is here responsible for sufficient locking
4621da177e4SLinus Torvalds 	 * (ie. inode->i_lock) */
4631da177e4SLinus Torvalds 	inode->i_blocks = bytes >> 9;
4641da177e4SLinus Torvalds 	inode->i_bytes = bytes & 511;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes);
468