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 585590ff0dSUlrich Drepper int vfs_stat_fd(int dfd, char __user *name, struct kstat *stat) 591da177e4SLinus Torvalds { 602d8f3038SAl Viro struct path path; 611da177e4SLinus Torvalds int error; 621da177e4SLinus Torvalds 632d8f3038SAl Viro error = user_path_at(dfd, name, LOOKUP_FOLLOW, &path); 641da177e4SLinus Torvalds if (!error) { 652d8f3038SAl Viro error = vfs_getattr(path.mnt, path.dentry, stat); 662d8f3038SAl Viro path_put(&path); 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds return error; 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 715590ff0dSUlrich Drepper int vfs_stat(char __user *name, struct kstat *stat) 725590ff0dSUlrich Drepper { 735590ff0dSUlrich Drepper return vfs_stat_fd(AT_FDCWD, name, stat); 745590ff0dSUlrich Drepper } 755590ff0dSUlrich Drepper 761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_stat); 771da177e4SLinus Torvalds 785590ff0dSUlrich Drepper int vfs_lstat_fd(int dfd, char __user *name, struct kstat *stat) 791da177e4SLinus Torvalds { 802d8f3038SAl Viro struct path path; 811da177e4SLinus Torvalds int error; 821da177e4SLinus Torvalds 832d8f3038SAl Viro error = user_path_at(dfd, name, 0, &path); 841da177e4SLinus Torvalds if (!error) { 852d8f3038SAl Viro error = vfs_getattr(path.mnt, path.dentry, stat); 862d8f3038SAl Viro path_put(&path); 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds return error; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 915590ff0dSUlrich Drepper int vfs_lstat(char __user *name, struct kstat *stat) 925590ff0dSUlrich Drepper { 935590ff0dSUlrich Drepper return vfs_lstat_fd(AT_FDCWD, name, stat); 945590ff0dSUlrich Drepper } 955590ff0dSUlrich Drepper 961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_lstat); 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds int vfs_fstat(unsigned int fd, struct kstat *stat) 991da177e4SLinus Torvalds { 1001da177e4SLinus Torvalds struct file *f = fget(fd); 1011da177e4SLinus Torvalds int error = -EBADF; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds if (f) { 1040f7fc9e4SJosef "Jeff" Sipek error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, stat); 1051da177e4SLinus Torvalds fput(f); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds return error; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_fstat); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds /* 1151da177e4SLinus Torvalds * For backward compatibility? Maybe this should be moved 1161da177e4SLinus Torvalds * into arch/i386 instead? 1171da177e4SLinus Torvalds */ 1181da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds static int warncount = 5; 1211da177e4SLinus Torvalds struct __old_kernel_stat tmp; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (warncount > 0) { 1241da177e4SLinus Torvalds warncount--; 1251da177e4SLinus Torvalds printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n", 1261da177e4SLinus Torvalds current->comm); 1271da177e4SLinus Torvalds } else if (warncount < 0) { 1281da177e4SLinus Torvalds /* it's laughable, but... */ 1291da177e4SLinus Torvalds warncount = 0; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds memset(&tmp, 0, sizeof(struct __old_kernel_stat)); 1331da177e4SLinus Torvalds tmp.st_dev = old_encode_dev(stat->dev); 1341da177e4SLinus Torvalds tmp.st_ino = stat->ino; 135afefdbb2SDavid Howells if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 136afefdbb2SDavid Howells return -EOVERFLOW; 1371da177e4SLinus Torvalds tmp.st_mode = stat->mode; 1381da177e4SLinus Torvalds tmp.st_nlink = stat->nlink; 1391da177e4SLinus Torvalds if (tmp.st_nlink != stat->nlink) 1401da177e4SLinus Torvalds return -EOVERFLOW; 1411da177e4SLinus Torvalds SET_UID(tmp.st_uid, stat->uid); 1421da177e4SLinus Torvalds SET_GID(tmp.st_gid, stat->gid); 1431da177e4SLinus Torvalds tmp.st_rdev = old_encode_dev(stat->rdev); 1441da177e4SLinus Torvalds #if BITS_PER_LONG == 32 1451da177e4SLinus Torvalds if (stat->size > MAX_NON_LFS) 1461da177e4SLinus Torvalds return -EOVERFLOW; 1471da177e4SLinus Torvalds #endif 1481da177e4SLinus Torvalds tmp.st_size = stat->size; 1491da177e4SLinus Torvalds tmp.st_atime = stat->atime.tv_sec; 1501da177e4SLinus Torvalds tmp.st_mtime = stat->mtime.tv_sec; 1511da177e4SLinus Torvalds tmp.st_ctime = stat->ctime.tv_sec; 1521da177e4SLinus Torvalds return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 155bdc480e3SHeiko Carstens SYSCALL_DEFINE2(stat, char __user *, filename, struct __old_kernel_stat __user *, statbuf) 1561da177e4SLinus Torvalds { 1571da177e4SLinus Torvalds struct kstat stat; 1585590ff0dSUlrich Drepper int error = vfs_stat_fd(AT_FDCWD, filename, &stat); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds if (!error) 1611da177e4SLinus Torvalds error = cp_old_stat(&stat, statbuf); 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds return error; 1641da177e4SLinus Torvalds } 165257ac264SHeiko Carstens 166257ac264SHeiko Carstens SYSCALL_DEFINE2(lstat, char __user *, filename, struct __old_kernel_stat __user *, statbuf) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds struct kstat stat; 1695590ff0dSUlrich Drepper int error = vfs_lstat_fd(AT_FDCWD, filename, &stat); 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds if (!error) 1721da177e4SLinus Torvalds error = cp_old_stat(&stat, statbuf); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds return error; 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 240257ac264SHeiko Carstens SYSCALL_DEFINE2(newstat, char __user *, filename, struct stat __user *, statbuf) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds struct kstat stat; 2435590ff0dSUlrich Drepper int error = vfs_stat_fd(AT_FDCWD, filename, &stat); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds if (!error) 2461da177e4SLinus Torvalds error = cp_new_stat(&stat, statbuf); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds return error; 2491da177e4SLinus Torvalds } 2505590ff0dSUlrich Drepper 251257ac264SHeiko Carstens SYSCALL_DEFINE2(newlstat, char __user *, filename, struct stat __user *, statbuf) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds struct kstat stat; 2545590ff0dSUlrich Drepper int error = vfs_lstat_fd(AT_FDCWD, filename, &stat); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds if (!error) 2571da177e4SLinus Torvalds error = cp_new_stat(&stat, statbuf); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds return error; 2601da177e4SLinus Torvalds } 2615590ff0dSUlrich Drepper 2622833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT) 263*6559eed8SHeiko Carstens SYSCALL_DEFINE4(newfstatat, int, dfd, char __user *, filename, 264*6559eed8SHeiko Carstens struct stat __user *, statbuf, int, flag) 2655590ff0dSUlrich Drepper { 2665590ff0dSUlrich Drepper struct kstat stat; 2675590ff0dSUlrich Drepper int error = -EINVAL; 2685590ff0dSUlrich Drepper 2695590ff0dSUlrich Drepper if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0) 2705590ff0dSUlrich Drepper goto out; 2715590ff0dSUlrich Drepper 2725590ff0dSUlrich Drepper if (flag & AT_SYMLINK_NOFOLLOW) 2735590ff0dSUlrich Drepper error = vfs_lstat_fd(dfd, filename, &stat); 2745590ff0dSUlrich Drepper else 2755590ff0dSUlrich Drepper error = vfs_stat_fd(dfd, filename, &stat); 2765590ff0dSUlrich Drepper 2775590ff0dSUlrich Drepper if (!error) 2785590ff0dSUlrich Drepper error = cp_new_stat(&stat, statbuf); 2795590ff0dSUlrich Drepper 2805590ff0dSUlrich Drepper out: 2815590ff0dSUlrich Drepper return error; 2825590ff0dSUlrich Drepper } 283cff2b760SUlrich Drepper #endif 2845590ff0dSUlrich Drepper 285257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds struct kstat stat; 2881da177e4SLinus Torvalds int error = vfs_fstat(fd, &stat); 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds if (!error) 2911da177e4SLinus Torvalds error = cp_new_stat(&stat, statbuf); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds return error; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 296*6559eed8SHeiko Carstens SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname, 297*6559eed8SHeiko Carstens char __user *, buf, int, bufsiz) 2981da177e4SLinus Torvalds { 2992d8f3038SAl Viro struct path path; 3001da177e4SLinus Torvalds int error; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds if (bufsiz <= 0) 3031da177e4SLinus Torvalds return -EINVAL; 3041da177e4SLinus Torvalds 3052d8f3038SAl Viro error = user_path_at(dfd, pathname, 0, &path); 3061da177e4SLinus Torvalds if (!error) { 3072d8f3038SAl Viro struct inode *inode = path.dentry->d_inode; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds error = -EINVAL; 310acfa4380SAl Viro if (inode->i_op->readlink) { 3112d8f3038SAl Viro error = security_inode_readlink(path.dentry); 3121da177e4SLinus Torvalds if (!error) { 3132d8f3038SAl Viro touch_atime(path.mnt, path.dentry); 3142d8f3038SAl Viro error = inode->i_op->readlink(path.dentry, 3154ac91378SJan Blunck buf, bufsiz); 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds } 3182d8f3038SAl Viro path_put(&path); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds return error; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 323002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf, 324002c8976SHeiko Carstens int, bufsiz) 3255590ff0dSUlrich Drepper { 3265590ff0dSUlrich Drepper return sys_readlinkat(AT_FDCWD, path, buf, bufsiz); 3275590ff0dSUlrich Drepper } 3285590ff0dSUlrich Drepper 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */ 3311da177e4SLinus Torvalds #ifdef __ARCH_WANT_STAT64 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds struct stat64 tmp; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds memset(&tmp, 0, sizeof(struct stat64)); 3381da177e4SLinus Torvalds #ifdef CONFIG_MIPS 3391da177e4SLinus Torvalds /* mips has weird padding, so we don't get 64 bits there */ 3401da177e4SLinus Torvalds if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev)) 3411da177e4SLinus Torvalds return -EOVERFLOW; 3421da177e4SLinus Torvalds tmp.st_dev = new_encode_dev(stat->dev); 3431da177e4SLinus Torvalds tmp.st_rdev = new_encode_dev(stat->rdev); 3441da177e4SLinus Torvalds #else 3451da177e4SLinus Torvalds tmp.st_dev = huge_encode_dev(stat->dev); 3461da177e4SLinus Torvalds tmp.st_rdev = huge_encode_dev(stat->rdev); 3471da177e4SLinus Torvalds #endif 3481da177e4SLinus Torvalds tmp.st_ino = stat->ino; 349afefdbb2SDavid Howells if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 350afefdbb2SDavid Howells return -EOVERFLOW; 3511da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO 3521da177e4SLinus Torvalds tmp.__st_ino = stat->ino; 3531da177e4SLinus Torvalds #endif 3541da177e4SLinus Torvalds tmp.st_mode = stat->mode; 3551da177e4SLinus Torvalds tmp.st_nlink = stat->nlink; 3561da177e4SLinus Torvalds tmp.st_uid = stat->uid; 3571da177e4SLinus Torvalds tmp.st_gid = stat->gid; 3581da177e4SLinus Torvalds tmp.st_atime = stat->atime.tv_sec; 3591da177e4SLinus Torvalds tmp.st_atime_nsec = stat->atime.tv_nsec; 3601da177e4SLinus Torvalds tmp.st_mtime = stat->mtime.tv_sec; 3611da177e4SLinus Torvalds tmp.st_mtime_nsec = stat->mtime.tv_nsec; 3621da177e4SLinus Torvalds tmp.st_ctime = stat->ctime.tv_sec; 3631da177e4SLinus Torvalds tmp.st_ctime_nsec = stat->ctime.tv_nsec; 3641da177e4SLinus Torvalds tmp.st_size = stat->size; 3651da177e4SLinus Torvalds tmp.st_blocks = stat->blocks; 3661da177e4SLinus Torvalds tmp.st_blksize = stat->blksize; 3671da177e4SLinus Torvalds return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 370257ac264SHeiko Carstens SYSCALL_DEFINE2(stat64, char __user *, filename, struct stat64 __user *, statbuf) 3711da177e4SLinus Torvalds { 3721da177e4SLinus Torvalds struct kstat stat; 3731da177e4SLinus Torvalds int error = vfs_stat(filename, &stat); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (!error) 3761da177e4SLinus Torvalds error = cp_new_stat64(&stat, statbuf); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds return error; 3791da177e4SLinus Torvalds } 380257ac264SHeiko Carstens 381257ac264SHeiko Carstens SYSCALL_DEFINE2(lstat64, char __user *, filename, struct stat64 __user *, statbuf) 3821da177e4SLinus Torvalds { 3831da177e4SLinus Torvalds struct kstat stat; 3841da177e4SLinus Torvalds int error = vfs_lstat(filename, &stat); 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds if (!error) 3871da177e4SLinus Torvalds error = cp_new_stat64(&stat, statbuf); 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds return error; 3901da177e4SLinus Torvalds } 391257ac264SHeiko Carstens 392257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds struct kstat stat; 3951da177e4SLinus Torvalds int error = vfs_fstat(fd, &stat); 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds if (!error) 3981da177e4SLinus Torvalds error = cp_new_stat64(&stat, statbuf); 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds return error; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 403*6559eed8SHeiko Carstens SYSCALL_DEFINE4(fstatat64, int, dfd, char __user *, filename, 404*6559eed8SHeiko Carstens struct stat64 __user *, statbuf, int, flag) 405cff2b760SUlrich Drepper { 406cff2b760SUlrich Drepper struct kstat stat; 407cff2b760SUlrich Drepper int error = -EINVAL; 408cff2b760SUlrich Drepper 409cff2b760SUlrich Drepper if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0) 410cff2b760SUlrich Drepper goto out; 411cff2b760SUlrich Drepper 412cff2b760SUlrich Drepper if (flag & AT_SYMLINK_NOFOLLOW) 413cff2b760SUlrich Drepper error = vfs_lstat_fd(dfd, filename, &stat); 414cff2b760SUlrich Drepper else 415cff2b760SUlrich Drepper error = vfs_stat_fd(dfd, filename, &stat); 416cff2b760SUlrich Drepper 417cff2b760SUlrich Drepper if (!error) 418cff2b760SUlrich Drepper error = cp_new_stat64(&stat, statbuf); 419cff2b760SUlrich Drepper 420cff2b760SUlrich Drepper out: 421cff2b760SUlrich Drepper return error; 422cff2b760SUlrich Drepper } 4231da177e4SLinus Torvalds #endif /* __ARCH_WANT_STAT64 */ 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds void inode_add_bytes(struct inode *inode, loff_t bytes) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds spin_lock(&inode->i_lock); 4281da177e4SLinus Torvalds inode->i_blocks += bytes >> 9; 4291da177e4SLinus Torvalds bytes &= 511; 4301da177e4SLinus Torvalds inode->i_bytes += bytes; 4311da177e4SLinus Torvalds if (inode->i_bytes >= 512) { 4321da177e4SLinus Torvalds inode->i_blocks++; 4331da177e4SLinus Torvalds inode->i_bytes -= 512; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes); 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds void inode_sub_bytes(struct inode *inode, loff_t bytes) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds spin_lock(&inode->i_lock); 4431da177e4SLinus Torvalds inode->i_blocks -= bytes >> 9; 4441da177e4SLinus Torvalds bytes &= 511; 4451da177e4SLinus Torvalds if (inode->i_bytes < bytes) { 4461da177e4SLinus Torvalds inode->i_blocks--; 4471da177e4SLinus Torvalds inode->i_bytes += 512; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds inode->i_bytes -= bytes; 4501da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes); 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds loff_t ret; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds spin_lock(&inode->i_lock); 4601da177e4SLinus Torvalds ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes; 4611da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 4621da177e4SLinus Torvalds return ret; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes); 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds /* Caller is here responsible for sufficient locking 4701da177e4SLinus Torvalds * (ie. inode->i_lock) */ 4711da177e4SLinus Torvalds inode->i_blocks = bytes >> 9; 4721da177e4SLinus Torvalds inode->i_bytes = bytes & 511; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes); 476