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 187c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 191da177e4SLinus Torvalds #include <asm/unistd.h> 201da177e4SLinus Torvalds 21*a528d35eSDavid Howells /** 22*a528d35eSDavid Howells * generic_fillattr - Fill in the basic attributes from the inode struct 23*a528d35eSDavid Howells * @inode: Inode to use as the source 24*a528d35eSDavid Howells * @stat: Where to fill in the attributes 25*a528d35eSDavid Howells * 26*a528d35eSDavid Howells * Fill in the basic attributes in the kstat structure from data that's to be 27*a528d35eSDavid Howells * found on the VFS inode structure. This is the default if no getattr inode 28*a528d35eSDavid Howells * operation is supplied. 29*a528d35eSDavid Howells */ 301da177e4SLinus Torvalds void generic_fillattr(struct inode *inode, struct kstat *stat) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds stat->dev = inode->i_sb->s_dev; 331da177e4SLinus Torvalds stat->ino = inode->i_ino; 341da177e4SLinus Torvalds stat->mode = inode->i_mode; 351da177e4SLinus Torvalds stat->nlink = inode->i_nlink; 361da177e4SLinus Torvalds stat->uid = inode->i_uid; 371da177e4SLinus Torvalds stat->gid = inode->i_gid; 381da177e4SLinus Torvalds stat->rdev = inode->i_rdev; 393ddcd056SLinus Torvalds stat->size = i_size_read(inode); 401da177e4SLinus Torvalds stat->atime = inode->i_atime; 411da177e4SLinus Torvalds stat->mtime = inode->i_mtime; 421da177e4SLinus Torvalds stat->ctime = inode->i_ctime; 4393407472SFabian Frederick stat->blksize = i_blocksize(inode); 443ddcd056SLinus Torvalds stat->blocks = inode->i_blocks; 451da177e4SLinus Torvalds 46*a528d35eSDavid Howells if (IS_NOATIME(inode)) 47*a528d35eSDavid Howells stat->result_mask &= ~STATX_ATIME; 48*a528d35eSDavid Howells if (IS_AUTOMOUNT(inode)) 49*a528d35eSDavid Howells stat->attributes |= STATX_ATTR_AUTOMOUNT; 50*a528d35eSDavid Howells } 511da177e4SLinus Torvalds EXPORT_SYMBOL(generic_fillattr); 521da177e4SLinus Torvalds 53b7a6ec52SJ. Bruce Fields /** 54b7a6ec52SJ. Bruce Fields * vfs_getattr_nosec - getattr without security checks 55b7a6ec52SJ. Bruce Fields * @path: file to get attributes from 56b7a6ec52SJ. Bruce Fields * @stat: structure to return attributes in 57*a528d35eSDavid Howells * @request_mask: STATX_xxx flags indicating what the caller wants 58*a528d35eSDavid Howells * @query_flags: Query mode (KSTAT_QUERY_FLAGS) 59b7a6ec52SJ. Bruce Fields * 60b7a6ec52SJ. Bruce Fields * Get attributes without calling security_inode_getattr. 61b7a6ec52SJ. Bruce Fields * 62b7a6ec52SJ. Bruce Fields * Currently the only caller other than vfs_getattr is internal to the 63*a528d35eSDavid Howells * filehandle lookup code, which uses only the inode number and returns no 64*a528d35eSDavid Howells * attributes to any user. Any other code probably wants vfs_getattr. 65b7a6ec52SJ. Bruce Fields */ 66*a528d35eSDavid Howells int vfs_getattr_nosec(const struct path *path, struct kstat *stat, 67*a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 681da177e4SLinus Torvalds { 69bb668734SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 701da177e4SLinus Torvalds 71*a528d35eSDavid Howells memset(stat, 0, sizeof(*stat)); 72*a528d35eSDavid Howells stat->result_mask |= STATX_BASIC_STATS; 73*a528d35eSDavid Howells request_mask &= STATX_ALL; 74*a528d35eSDavid Howells query_flags &= KSTAT_QUERY_FLAGS; 751da177e4SLinus Torvalds if (inode->i_op->getattr) 76*a528d35eSDavid Howells return inode->i_op->getattr(path, stat, request_mask, 77*a528d35eSDavid Howells query_flags); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds generic_fillattr(inode, stat); 801da177e4SLinus Torvalds return 0; 811da177e4SLinus Torvalds } 82b7a6ec52SJ. Bruce Fields EXPORT_SYMBOL(vfs_getattr_nosec); 83b7a6ec52SJ. Bruce Fields 84*a528d35eSDavid Howells /* 85*a528d35eSDavid Howells * vfs_getattr - Get the enhanced basic attributes of a file 86*a528d35eSDavid Howells * @path: The file of interest 87*a528d35eSDavid Howells * @stat: Where to return the statistics 88*a528d35eSDavid Howells * @request_mask: STATX_xxx flags indicating what the caller wants 89*a528d35eSDavid Howells * @query_flags: Query mode (KSTAT_QUERY_FLAGS) 90*a528d35eSDavid Howells * 91*a528d35eSDavid Howells * Ask the filesystem for a file's attributes. The caller must indicate in 92*a528d35eSDavid Howells * request_mask and query_flags to indicate what they want. 93*a528d35eSDavid Howells * 94*a528d35eSDavid Howells * If the file is remote, the filesystem can be forced to update the attributes 95*a528d35eSDavid Howells * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can 96*a528d35eSDavid Howells * suppress the update by passing AT_STATX_DONT_SYNC. 97*a528d35eSDavid Howells * 98*a528d35eSDavid Howells * Bits must have been set in request_mask to indicate which attributes the 99*a528d35eSDavid Howells * caller wants retrieving. Any such attribute not requested may be returned 100*a528d35eSDavid Howells * anyway, but the value may be approximate, and, if remote, may not have been 101*a528d35eSDavid Howells * synchronised with the server. 102*a528d35eSDavid Howells * 103*a528d35eSDavid Howells * 0 will be returned on success, and a -ve error code if unsuccessful. 104*a528d35eSDavid Howells */ 105*a528d35eSDavid Howells int vfs_getattr(const struct path *path, struct kstat *stat, 106*a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 107b7a6ec52SJ. Bruce Fields { 108b7a6ec52SJ. Bruce Fields int retval; 109b7a6ec52SJ. Bruce Fields 1103f7036a0SAl Viro retval = security_inode_getattr(path); 111b7a6ec52SJ. Bruce Fields if (retval) 112b7a6ec52SJ. Bruce Fields return retval; 113*a528d35eSDavid Howells return vfs_getattr_nosec(path, stat, request_mask, query_flags); 114b7a6ec52SJ. Bruce Fields } 1151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_getattr); 1161da177e4SLinus Torvalds 117*a528d35eSDavid Howells /** 118*a528d35eSDavid Howells * vfs_statx_fd - Get the enhanced basic attributes by file descriptor 119*a528d35eSDavid Howells * @fd: The file descriptor referring to the file of interest 120*a528d35eSDavid Howells * @stat: The result structure to fill in. 121*a528d35eSDavid Howells * @request_mask: STATX_xxx flags indicating what the caller wants 122*a528d35eSDavid Howells * @query_flags: Query mode (KSTAT_QUERY_FLAGS) 123*a528d35eSDavid Howells * 124*a528d35eSDavid Howells * This function is a wrapper around vfs_getattr(). The main difference is 125*a528d35eSDavid Howells * that it uses a file descriptor to determine the file location. 126*a528d35eSDavid Howells * 127*a528d35eSDavid Howells * 0 will be returned on success, and a -ve error code if unsuccessful. 128*a528d35eSDavid Howells */ 129*a528d35eSDavid Howells int vfs_statx_fd(unsigned int fd, struct kstat *stat, 130*a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 1311da177e4SLinus Torvalds { 1322903ff01SAl Viro struct fd f = fdget_raw(fd); 1331da177e4SLinus Torvalds int error = -EBADF; 1341da177e4SLinus Torvalds 1352903ff01SAl Viro if (f.file) { 136*a528d35eSDavid Howells error = vfs_getattr(&f.file->f_path, stat, 137*a528d35eSDavid Howells request_mask, query_flags); 1382903ff01SAl Viro fdput(f); 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds return error; 1411da177e4SLinus Torvalds } 142*a528d35eSDavid Howells EXPORT_SYMBOL(vfs_statx_fd); 1431da177e4SLinus Torvalds 144*a528d35eSDavid Howells /** 145*a528d35eSDavid Howells * vfs_statx - Get basic and extra attributes by filename 146*a528d35eSDavid Howells * @dfd: A file descriptor representing the base dir for a relative filename 147*a528d35eSDavid Howells * @filename: The name of the file of interest 148*a528d35eSDavid Howells * @flags: Flags to control the query 149*a528d35eSDavid Howells * @stat: The result structure to fill in. 150*a528d35eSDavid Howells * @request_mask: STATX_xxx flags indicating what the caller wants 151*a528d35eSDavid Howells * 152*a528d35eSDavid Howells * This function is a wrapper around vfs_getattr(). The main difference is 153*a528d35eSDavid Howells * that it uses a filename and base directory to determine the file location. 154*a528d35eSDavid Howells * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink 155*a528d35eSDavid Howells * at the given name from being referenced. 156*a528d35eSDavid Howells * 157*a528d35eSDavid Howells * The caller must have preset stat->request_mask as for vfs_getattr(). The 158*a528d35eSDavid Howells * flags are also used to load up stat->query_flags. 159*a528d35eSDavid Howells * 160*a528d35eSDavid Howells * 0 will be returned on success, and a -ve error code if unsuccessful. 161*a528d35eSDavid Howells */ 162*a528d35eSDavid Howells int vfs_statx(int dfd, const char __user *filename, int flags, 163*a528d35eSDavid Howells struct kstat *stat, u32 request_mask) 1640112fc22SOleg Drokin { 1652eae7a18SChristoph Hellwig struct path path; 1660112fc22SOleg Drokin int error = -EINVAL; 167*a528d35eSDavid Howells unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT; 1680112fc22SOleg Drokin 169*a528d35eSDavid Howells if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | 170*a528d35eSDavid Howells AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0) 171*a528d35eSDavid Howells return -EINVAL; 1720112fc22SOleg Drokin 173*a528d35eSDavid Howells if (flags & AT_SYMLINK_NOFOLLOW) 174*a528d35eSDavid Howells lookup_flags &= ~LOOKUP_FOLLOW; 175*a528d35eSDavid Howells if (flags & AT_NO_AUTOMOUNT) 176*a528d35eSDavid Howells lookup_flags &= ~LOOKUP_AUTOMOUNT; 177*a528d35eSDavid Howells if (flags & AT_EMPTY_PATH) 17865cfc672SAl Viro lookup_flags |= LOOKUP_EMPTY; 179*a528d35eSDavid Howells 180836fb7e7SJeff Layton retry: 1812eae7a18SChristoph Hellwig error = user_path_at(dfd, filename, lookup_flags, &path); 1822eae7a18SChristoph Hellwig if (error) 1832eae7a18SChristoph Hellwig goto out; 1842eae7a18SChristoph Hellwig 185*a528d35eSDavid Howells error = vfs_getattr(&path, stat, request_mask, flags); 1862eae7a18SChristoph Hellwig path_put(&path); 187836fb7e7SJeff Layton if (retry_estale(error, lookup_flags)) { 188836fb7e7SJeff Layton lookup_flags |= LOOKUP_REVAL; 189836fb7e7SJeff Layton goto retry; 190836fb7e7SJeff Layton } 1910112fc22SOleg Drokin out: 1920112fc22SOleg Drokin return error; 1930112fc22SOleg Drokin } 194*a528d35eSDavid Howells EXPORT_SYMBOL(vfs_statx); 1952eae7a18SChristoph Hellwig 1960112fc22SOleg Drokin 1971da177e4SLinus Torvalds #ifdef __ARCH_WANT_OLD_STAT 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds /* 2001da177e4SLinus Torvalds * For backward compatibility? Maybe this should be moved 2011da177e4SLinus Torvalds * into arch/i386 instead? 2021da177e4SLinus Torvalds */ 2031da177e4SLinus Torvalds static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds static int warncount = 5; 2061da177e4SLinus Torvalds struct __old_kernel_stat tmp; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds if (warncount > 0) { 2091da177e4SLinus Torvalds warncount--; 2101da177e4SLinus Torvalds printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n", 2111da177e4SLinus Torvalds current->comm); 2121da177e4SLinus Torvalds } else if (warncount < 0) { 2131da177e4SLinus Torvalds /* it's laughable, but... */ 2141da177e4SLinus Torvalds warncount = 0; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds memset(&tmp, 0, sizeof(struct __old_kernel_stat)); 2181da177e4SLinus Torvalds tmp.st_dev = old_encode_dev(stat->dev); 2191da177e4SLinus Torvalds tmp.st_ino = stat->ino; 220afefdbb2SDavid Howells if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 221afefdbb2SDavid Howells return -EOVERFLOW; 2221da177e4SLinus Torvalds tmp.st_mode = stat->mode; 2231da177e4SLinus Torvalds tmp.st_nlink = stat->nlink; 2241da177e4SLinus Torvalds if (tmp.st_nlink != stat->nlink) 2251da177e4SLinus Torvalds return -EOVERFLOW; 226a7c1938eSEric W. Biederman SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); 227a7c1938eSEric W. Biederman SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); 2281da177e4SLinus Torvalds tmp.st_rdev = old_encode_dev(stat->rdev); 2291da177e4SLinus Torvalds #if BITS_PER_LONG == 32 2301da177e4SLinus Torvalds if (stat->size > MAX_NON_LFS) 2311da177e4SLinus Torvalds return -EOVERFLOW; 2321da177e4SLinus Torvalds #endif 2331da177e4SLinus Torvalds tmp.st_size = stat->size; 2341da177e4SLinus Torvalds tmp.st_atime = stat->atime.tv_sec; 2351da177e4SLinus Torvalds tmp.st_mtime = stat->mtime.tv_sec; 2361da177e4SLinus Torvalds tmp.st_ctime = stat->ctime.tv_sec; 2371da177e4SLinus Torvalds return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240c7887325SDavid Howells SYSCALL_DEFINE2(stat, const char __user *, filename, 241c7887325SDavid Howells struct __old_kernel_stat __user *, statbuf) 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds struct kstat stat; 2442eae7a18SChristoph Hellwig int error; 2451da177e4SLinus Torvalds 2462eae7a18SChristoph Hellwig error = vfs_stat(filename, &stat); 2472eae7a18SChristoph Hellwig if (error) 2481da177e4SLinus Torvalds return error; 2492eae7a18SChristoph Hellwig 2502eae7a18SChristoph Hellwig return cp_old_stat(&stat, statbuf); 2511da177e4SLinus Torvalds } 252257ac264SHeiko Carstens 253c7887325SDavid Howells SYSCALL_DEFINE2(lstat, const char __user *, filename, 254c7887325SDavid Howells struct __old_kernel_stat __user *, statbuf) 2551da177e4SLinus Torvalds { 2561da177e4SLinus Torvalds struct kstat stat; 2572eae7a18SChristoph Hellwig int error; 2581da177e4SLinus Torvalds 2592eae7a18SChristoph Hellwig error = vfs_lstat(filename, &stat); 2602eae7a18SChristoph Hellwig if (error) 2611da177e4SLinus Torvalds return error; 2622eae7a18SChristoph Hellwig 2632eae7a18SChristoph Hellwig return cp_old_stat(&stat, statbuf); 2641da177e4SLinus Torvalds } 265257ac264SHeiko Carstens 266257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds struct kstat stat; 2691da177e4SLinus Torvalds int error = vfs_fstat(fd, &stat); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds if (!error) 2721da177e4SLinus Torvalds error = cp_old_stat(&stat, statbuf); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds return error; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds #endif /* __ARCH_WANT_OLD_STAT */ 2781da177e4SLinus Torvalds 279a52dd971SLinus Torvalds #if BITS_PER_LONG == 32 280a52dd971SLinus Torvalds # define choose_32_64(a,b) a 281a52dd971SLinus Torvalds #else 282a52dd971SLinus Torvalds # define choose_32_64(a,b) b 283a52dd971SLinus Torvalds #endif 284a52dd971SLinus Torvalds 2854c416f42SYaowei Bai #define valid_dev(x) choose_32_64(old_valid_dev(x),true) 286a52dd971SLinus Torvalds #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x) 287a52dd971SLinus Torvalds 2888529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT_PADDING 2898529f613SLinus Torvalds # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st)) 2908529f613SLinus Torvalds #endif 2918529f613SLinus Torvalds 2921da177e4SLinus Torvalds static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds struct stat tmp; 2951da177e4SLinus Torvalds 296a52dd971SLinus Torvalds if (!valid_dev(stat->dev) || !valid_dev(stat->rdev)) 2971da177e4SLinus Torvalds return -EOVERFLOW; 298a52dd971SLinus Torvalds #if BITS_PER_LONG == 32 299a52dd971SLinus Torvalds if (stat->size > MAX_NON_LFS) 3001da177e4SLinus Torvalds return -EOVERFLOW; 3011da177e4SLinus Torvalds #endif 3021da177e4SLinus Torvalds 3038529f613SLinus Torvalds INIT_STRUCT_STAT_PADDING(tmp); 304a52dd971SLinus Torvalds tmp.st_dev = encode_dev(stat->dev); 3051da177e4SLinus Torvalds tmp.st_ino = stat->ino; 306afefdbb2SDavid Howells if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 307afefdbb2SDavid Howells return -EOVERFLOW; 3081da177e4SLinus Torvalds tmp.st_mode = stat->mode; 3091da177e4SLinus Torvalds tmp.st_nlink = stat->nlink; 3101da177e4SLinus Torvalds if (tmp.st_nlink != stat->nlink) 3111da177e4SLinus Torvalds return -EOVERFLOW; 312a7c1938eSEric W. Biederman SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); 313a7c1938eSEric W. Biederman SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); 314a52dd971SLinus Torvalds tmp.st_rdev = encode_dev(stat->rdev); 3151da177e4SLinus Torvalds tmp.st_size = stat->size; 3161da177e4SLinus Torvalds tmp.st_atime = stat->atime.tv_sec; 3171da177e4SLinus Torvalds tmp.st_mtime = stat->mtime.tv_sec; 3181da177e4SLinus Torvalds tmp.st_ctime = stat->ctime.tv_sec; 3191da177e4SLinus Torvalds #ifdef STAT_HAVE_NSEC 3201da177e4SLinus Torvalds tmp.st_atime_nsec = stat->atime.tv_nsec; 3211da177e4SLinus Torvalds tmp.st_mtime_nsec = stat->mtime.tv_nsec; 3221da177e4SLinus Torvalds tmp.st_ctime_nsec = stat->ctime.tv_nsec; 3231da177e4SLinus Torvalds #endif 3241da177e4SLinus Torvalds tmp.st_blocks = stat->blocks; 3251da177e4SLinus Torvalds tmp.st_blksize = stat->blksize; 3261da177e4SLinus Torvalds return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 329c7887325SDavid Howells SYSCALL_DEFINE2(newstat, const char __user *, filename, 330c7887325SDavid Howells struct stat __user *, statbuf) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct kstat stat; 3332eae7a18SChristoph Hellwig int error = vfs_stat(filename, &stat); 3341da177e4SLinus Torvalds 3352eae7a18SChristoph Hellwig if (error) 3361da177e4SLinus Torvalds return error; 3372eae7a18SChristoph Hellwig return cp_new_stat(&stat, statbuf); 3381da177e4SLinus Torvalds } 3395590ff0dSUlrich Drepper 340c7887325SDavid Howells SYSCALL_DEFINE2(newlstat, const char __user *, filename, 341c7887325SDavid Howells struct stat __user *, statbuf) 3421da177e4SLinus Torvalds { 3431da177e4SLinus Torvalds struct kstat stat; 3442eae7a18SChristoph Hellwig int error; 3451da177e4SLinus Torvalds 3462eae7a18SChristoph Hellwig error = vfs_lstat(filename, &stat); 3472eae7a18SChristoph Hellwig if (error) 3481da177e4SLinus Torvalds return error; 3492eae7a18SChristoph Hellwig 3502eae7a18SChristoph Hellwig return cp_new_stat(&stat, statbuf); 3511da177e4SLinus Torvalds } 3525590ff0dSUlrich Drepper 3532833c28aSAndreas Schwab #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT) 354c7887325SDavid Howells SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename, 3556559eed8SHeiko Carstens struct stat __user *, statbuf, int, flag) 3565590ff0dSUlrich Drepper { 3575590ff0dSUlrich Drepper struct kstat stat; 3580112fc22SOleg Drokin int error; 3595590ff0dSUlrich Drepper 3600112fc22SOleg Drokin error = vfs_fstatat(dfd, filename, &stat, flag); 3610112fc22SOleg Drokin if (error) 3625590ff0dSUlrich Drepper return error; 3630112fc22SOleg Drokin return cp_new_stat(&stat, statbuf); 3645590ff0dSUlrich Drepper } 365cff2b760SUlrich Drepper #endif 3665590ff0dSUlrich Drepper 367257ac264SHeiko Carstens SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds struct kstat stat; 3701da177e4SLinus Torvalds int error = vfs_fstat(fd, &stat); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds if (!error) 3731da177e4SLinus Torvalds error = cp_new_stat(&stat, statbuf); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds return error; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds 3786559eed8SHeiko Carstens SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname, 3796559eed8SHeiko Carstens char __user *, buf, int, bufsiz) 3801da177e4SLinus Torvalds { 3812d8f3038SAl Viro struct path path; 3821da177e4SLinus Torvalds int error; 3831fa1e7f6SAndy Whitcroft int empty = 0; 3847955119eSJeff Layton unsigned int lookup_flags = LOOKUP_EMPTY; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds if (bufsiz <= 0) 3871da177e4SLinus Torvalds return -EINVAL; 3881da177e4SLinus Torvalds 3897955119eSJeff Layton retry: 3907955119eSJeff Layton error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty); 3911da177e4SLinus Torvalds if (!error) { 392bb668734SDavid Howells struct inode *inode = d_backing_inode(path.dentry); 3931da177e4SLinus Torvalds 3941fa1e7f6SAndy Whitcroft error = empty ? -ENOENT : -EINVAL; 395fd4a0edfSMiklos Szeredi /* 396fd4a0edfSMiklos Szeredi * AFS mountpoints allow readlink(2) but are not symlinks 397fd4a0edfSMiklos Szeredi */ 398fd4a0edfSMiklos Szeredi if (d_is_symlink(path.dentry) || inode->i_op->readlink) { 3992d8f3038SAl Viro error = security_inode_readlink(path.dentry); 4001da177e4SLinus Torvalds if (!error) { 40168ac1234SAl Viro touch_atime(&path); 402fd4a0edfSMiklos Szeredi error = vfs_readlink(path.dentry, buf, bufsiz); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds } 4052d8f3038SAl Viro path_put(&path); 4067955119eSJeff Layton if (retry_estale(error, lookup_flags)) { 4077955119eSJeff Layton lookup_flags |= LOOKUP_REVAL; 4087955119eSJeff Layton goto retry; 4097955119eSJeff Layton } 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds return error; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 414002c8976SHeiko Carstens SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf, 415002c8976SHeiko Carstens int, bufsiz) 4165590ff0dSUlrich Drepper { 4175590ff0dSUlrich Drepper return sys_readlinkat(AT_FDCWD, path, buf, bufsiz); 4185590ff0dSUlrich Drepper } 4195590ff0dSUlrich Drepper 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* ---------- LFS-64 ----------- */ 4220753f70fSCatalin Marinas #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64) 4231da177e4SLinus Torvalds 4248529f613SLinus Torvalds #ifndef INIT_STRUCT_STAT64_PADDING 4258529f613SLinus Torvalds # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st)) 4268529f613SLinus Torvalds #endif 4278529f613SLinus Torvalds 4281da177e4SLinus Torvalds static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf) 4291da177e4SLinus Torvalds { 4301da177e4SLinus Torvalds struct stat64 tmp; 4311da177e4SLinus Torvalds 4328529f613SLinus Torvalds INIT_STRUCT_STAT64_PADDING(tmp); 4331da177e4SLinus Torvalds #ifdef CONFIG_MIPS 4341da177e4SLinus Torvalds /* mips has weird padding, so we don't get 64 bits there */ 4351da177e4SLinus Torvalds tmp.st_dev = new_encode_dev(stat->dev); 4361da177e4SLinus Torvalds tmp.st_rdev = new_encode_dev(stat->rdev); 4371da177e4SLinus Torvalds #else 4381da177e4SLinus Torvalds tmp.st_dev = huge_encode_dev(stat->dev); 4391da177e4SLinus Torvalds tmp.st_rdev = huge_encode_dev(stat->rdev); 4401da177e4SLinus Torvalds #endif 4411da177e4SLinus Torvalds tmp.st_ino = stat->ino; 442afefdbb2SDavid Howells if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) 443afefdbb2SDavid Howells return -EOVERFLOW; 4441da177e4SLinus Torvalds #ifdef STAT64_HAS_BROKEN_ST_INO 4451da177e4SLinus Torvalds tmp.__st_ino = stat->ino; 4461da177e4SLinus Torvalds #endif 4471da177e4SLinus Torvalds tmp.st_mode = stat->mode; 4481da177e4SLinus Torvalds tmp.st_nlink = stat->nlink; 449a7c1938eSEric W. Biederman tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid); 450a7c1938eSEric W. Biederman tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid); 4511da177e4SLinus Torvalds tmp.st_atime = stat->atime.tv_sec; 4521da177e4SLinus Torvalds tmp.st_atime_nsec = stat->atime.tv_nsec; 4531da177e4SLinus Torvalds tmp.st_mtime = stat->mtime.tv_sec; 4541da177e4SLinus Torvalds tmp.st_mtime_nsec = stat->mtime.tv_nsec; 4551da177e4SLinus Torvalds tmp.st_ctime = stat->ctime.tv_sec; 4561da177e4SLinus Torvalds tmp.st_ctime_nsec = stat->ctime.tv_nsec; 4571da177e4SLinus Torvalds tmp.st_size = stat->size; 4581da177e4SLinus Torvalds tmp.st_blocks = stat->blocks; 4591da177e4SLinus Torvalds tmp.st_blksize = stat->blksize; 4601da177e4SLinus Torvalds return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 463c7887325SDavid Howells SYSCALL_DEFINE2(stat64, const char __user *, filename, 464c7887325SDavid Howells struct stat64 __user *, statbuf) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct kstat stat; 4671da177e4SLinus Torvalds int error = vfs_stat(filename, &stat); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds if (!error) 4701da177e4SLinus Torvalds error = cp_new_stat64(&stat, statbuf); 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds return error; 4731da177e4SLinus Torvalds } 474257ac264SHeiko Carstens 475c7887325SDavid Howells SYSCALL_DEFINE2(lstat64, const char __user *, filename, 476c7887325SDavid Howells struct stat64 __user *, statbuf) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds struct kstat stat; 4791da177e4SLinus Torvalds int error = vfs_lstat(filename, &stat); 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds if (!error) 4821da177e4SLinus Torvalds error = cp_new_stat64(&stat, statbuf); 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds return error; 4851da177e4SLinus Torvalds } 486257ac264SHeiko Carstens 487257ac264SHeiko Carstens SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds struct kstat stat; 4901da177e4SLinus Torvalds int error = vfs_fstat(fd, &stat); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds if (!error) 4931da177e4SLinus Torvalds error = cp_new_stat64(&stat, statbuf); 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds return error; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 498c7887325SDavid Howells SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename, 4996559eed8SHeiko Carstens struct stat64 __user *, statbuf, int, flag) 500cff2b760SUlrich Drepper { 501cff2b760SUlrich Drepper struct kstat stat; 5020112fc22SOleg Drokin int error; 503cff2b760SUlrich Drepper 5040112fc22SOleg Drokin error = vfs_fstatat(dfd, filename, &stat, flag); 5050112fc22SOleg Drokin if (error) 506cff2b760SUlrich Drepper return error; 5070112fc22SOleg Drokin return cp_new_stat64(&stat, statbuf); 508cff2b760SUlrich Drepper } 5090753f70fSCatalin Marinas #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */ 5101da177e4SLinus Torvalds 511*a528d35eSDavid Howells static inline int __put_timestamp(struct timespec *kts, 512*a528d35eSDavid Howells struct statx_timestamp __user *uts) 513*a528d35eSDavid Howells { 514*a528d35eSDavid Howells return (__put_user(kts->tv_sec, &uts->tv_sec ) || 515*a528d35eSDavid Howells __put_user(kts->tv_nsec, &uts->tv_nsec ) || 516*a528d35eSDavid Howells __put_user(0, &uts->__reserved )); 517*a528d35eSDavid Howells } 518*a528d35eSDavid Howells 519*a528d35eSDavid Howells /* 520*a528d35eSDavid Howells * Set the statx results. 521*a528d35eSDavid Howells */ 522*a528d35eSDavid Howells static long statx_set_result(struct kstat *stat, struct statx __user *buffer) 523*a528d35eSDavid Howells { 524*a528d35eSDavid Howells uid_t uid = from_kuid_munged(current_user_ns(), stat->uid); 525*a528d35eSDavid Howells gid_t gid = from_kgid_munged(current_user_ns(), stat->gid); 526*a528d35eSDavid Howells 527*a528d35eSDavid Howells if (__put_user(stat->result_mask, &buffer->stx_mask ) || 528*a528d35eSDavid Howells __put_user(stat->mode, &buffer->stx_mode ) || 529*a528d35eSDavid Howells __clear_user(&buffer->__spare0, sizeof(buffer->__spare0)) || 530*a528d35eSDavid Howells __put_user(stat->nlink, &buffer->stx_nlink ) || 531*a528d35eSDavid Howells __put_user(uid, &buffer->stx_uid ) || 532*a528d35eSDavid Howells __put_user(gid, &buffer->stx_gid ) || 533*a528d35eSDavid Howells __put_user(stat->attributes, &buffer->stx_attributes ) || 534*a528d35eSDavid Howells __put_user(stat->blksize, &buffer->stx_blksize ) || 535*a528d35eSDavid Howells __put_user(MAJOR(stat->rdev), &buffer->stx_rdev_major ) || 536*a528d35eSDavid Howells __put_user(MINOR(stat->rdev), &buffer->stx_rdev_minor ) || 537*a528d35eSDavid Howells __put_user(MAJOR(stat->dev), &buffer->stx_dev_major ) || 538*a528d35eSDavid Howells __put_user(MINOR(stat->dev), &buffer->stx_dev_minor ) || 539*a528d35eSDavid Howells __put_timestamp(&stat->atime, &buffer->stx_atime ) || 540*a528d35eSDavid Howells __put_timestamp(&stat->btime, &buffer->stx_btime ) || 541*a528d35eSDavid Howells __put_timestamp(&stat->ctime, &buffer->stx_ctime ) || 542*a528d35eSDavid Howells __put_timestamp(&stat->mtime, &buffer->stx_mtime ) || 543*a528d35eSDavid Howells __put_user(stat->ino, &buffer->stx_ino ) || 544*a528d35eSDavid Howells __put_user(stat->size, &buffer->stx_size ) || 545*a528d35eSDavid Howells __put_user(stat->blocks, &buffer->stx_blocks ) || 546*a528d35eSDavid Howells __clear_user(&buffer->__spare1, sizeof(buffer->__spare1)) || 547*a528d35eSDavid Howells __clear_user(&buffer->__spare2, sizeof(buffer->__spare2))) 548*a528d35eSDavid Howells return -EFAULT; 549*a528d35eSDavid Howells 550*a528d35eSDavid Howells return 0; 551*a528d35eSDavid Howells } 552*a528d35eSDavid Howells 553*a528d35eSDavid Howells /** 554*a528d35eSDavid Howells * sys_statx - System call to get enhanced stats 555*a528d35eSDavid Howells * @dfd: Base directory to pathwalk from *or* fd to stat. 556*a528d35eSDavid Howells * @filename: File to stat *or* NULL. 557*a528d35eSDavid Howells * @flags: AT_* flags to control pathwalk. 558*a528d35eSDavid Howells * @mask: Parts of statx struct actually required. 559*a528d35eSDavid Howells * @buffer: Result buffer. 560*a528d35eSDavid Howells * 561*a528d35eSDavid Howells * Note that if filename is NULL, then it does the equivalent of fstat() using 562*a528d35eSDavid Howells * dfd to indicate the file of interest. 563*a528d35eSDavid Howells */ 564*a528d35eSDavid Howells SYSCALL_DEFINE5(statx, 565*a528d35eSDavid Howells int, dfd, const char __user *, filename, unsigned, flags, 566*a528d35eSDavid Howells unsigned int, mask, 567*a528d35eSDavid Howells struct statx __user *, buffer) 568*a528d35eSDavid Howells { 569*a528d35eSDavid Howells struct kstat stat; 570*a528d35eSDavid Howells int error; 571*a528d35eSDavid Howells 572*a528d35eSDavid Howells if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE) 573*a528d35eSDavid Howells return -EINVAL; 574*a528d35eSDavid Howells if (!access_ok(VERIFY_WRITE, buffer, sizeof(*buffer))) 575*a528d35eSDavid Howells return -EFAULT; 576*a528d35eSDavid Howells 577*a528d35eSDavid Howells if (filename) 578*a528d35eSDavid Howells error = vfs_statx(dfd, filename, flags, &stat, mask); 579*a528d35eSDavid Howells else 580*a528d35eSDavid Howells error = vfs_statx_fd(dfd, &stat, mask, flags); 581*a528d35eSDavid Howells if (error) 582*a528d35eSDavid Howells return error; 583*a528d35eSDavid Howells return statx_set_result(&stat, buffer); 584*a528d35eSDavid Howells } 585*a528d35eSDavid Howells 586b462707eSDmitry Monakhov /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */ 587b462707eSDmitry Monakhov void __inode_add_bytes(struct inode *inode, loff_t bytes) 5881da177e4SLinus Torvalds { 5891da177e4SLinus Torvalds inode->i_blocks += bytes >> 9; 5901da177e4SLinus Torvalds bytes &= 511; 5911da177e4SLinus Torvalds inode->i_bytes += bytes; 5921da177e4SLinus Torvalds if (inode->i_bytes >= 512) { 5931da177e4SLinus Torvalds inode->i_blocks++; 5941da177e4SLinus Torvalds inode->i_bytes -= 512; 5951da177e4SLinus Torvalds } 596b462707eSDmitry Monakhov } 597b462707eSDmitry Monakhov 598b462707eSDmitry Monakhov void inode_add_bytes(struct inode *inode, loff_t bytes) 599b462707eSDmitry Monakhov { 600b462707eSDmitry Monakhov spin_lock(&inode->i_lock); 601b462707eSDmitry Monakhov __inode_add_bytes(inode, bytes); 6021da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds EXPORT_SYMBOL(inode_add_bytes); 6061da177e4SLinus Torvalds 6071c8924ebSJan Kara void __inode_sub_bytes(struct inode *inode, loff_t bytes) 6081da177e4SLinus Torvalds { 6091da177e4SLinus Torvalds inode->i_blocks -= bytes >> 9; 6101da177e4SLinus Torvalds bytes &= 511; 6111da177e4SLinus Torvalds if (inode->i_bytes < bytes) { 6121da177e4SLinus Torvalds inode->i_blocks--; 6131da177e4SLinus Torvalds inode->i_bytes += 512; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds inode->i_bytes -= bytes; 6161c8924ebSJan Kara } 6171c8924ebSJan Kara 6181c8924ebSJan Kara EXPORT_SYMBOL(__inode_sub_bytes); 6191c8924ebSJan Kara 6201c8924ebSJan Kara void inode_sub_bytes(struct inode *inode, loff_t bytes) 6211c8924ebSJan Kara { 6221c8924ebSJan Kara spin_lock(&inode->i_lock); 6231c8924ebSJan Kara __inode_sub_bytes(inode, bytes); 6241da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds EXPORT_SYMBOL(inode_sub_bytes); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds loff_t inode_get_bytes(struct inode *inode) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds loff_t ret; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds spin_lock(&inode->i_lock); 6341da177e4SLinus Torvalds ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes; 6351da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 6361da177e4SLinus Torvalds return ret; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds EXPORT_SYMBOL(inode_get_bytes); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds void inode_set_bytes(struct inode *inode, loff_t bytes) 6421da177e4SLinus Torvalds { 6431da177e4SLinus Torvalds /* Caller is here responsible for sufficient locking 6441da177e4SLinus Torvalds * (ie. inode->i_lock) */ 6451da177e4SLinus Torvalds inode->i_blocks = bytes >> 9; 6461da177e4SLinus Torvalds inode->i_bytes = bytes & 511; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds EXPORT_SYMBOL(inode_set_bytes); 650