blob: fa0be59340cc9136eb8c508434e10289acc8fc97 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/stat.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Paul Gortmaker630d9c42011-11-16 23:57:37 -05007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mm.h>
9#include <linux/errno.h>
10#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/highuid.h>
12#include <linux/fs.h>
13#include <linux/namei.h>
14#include <linux/security.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010015#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/syscalls.h>
Theodore Ts'oba52de12006-09-27 01:50:49 -070017#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/unistd.h>
21
David Howellsa528d352017-01-31 16:46:22 +000022/**
23 * generic_fillattr - Fill in the basic attributes from the inode struct
24 * @inode: Inode to use as the source
25 * @stat: Where to fill in the attributes
26 *
27 * Fill in the basic attributes in the kstat structure from data that's to be
28 * found on the VFS inode structure. This is the default if no getattr inode
29 * operation is supplied.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031void generic_fillattr(struct inode *inode, struct kstat *stat)
32{
33 stat->dev = inode->i_sb->s_dev;
34 stat->ino = inode->i_ino;
35 stat->mode = inode->i_mode;
36 stat->nlink = inode->i_nlink;
37 stat->uid = inode->i_uid;
38 stat->gid = inode->i_gid;
39 stat->rdev = inode->i_rdev;
Linus Torvalds3ddcd052011-08-06 22:45:50 -070040 stat->size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 stat->atime = inode->i_atime;
42 stat->mtime = inode->i_mtime;
43 stat->ctime = inode->i_ctime;
Fabian Frederick93407472017-02-27 14:28:32 -080044 stat->blksize = i_blocksize(inode);
Linus Torvalds3ddcd052011-08-06 22:45:50 -070045 stat->blocks = inode->i_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
David Howellsa528d352017-01-31 16:46:22 +000047 if (IS_NOATIME(inode))
48 stat->result_mask &= ~STATX_ATIME;
49 if (IS_AUTOMOUNT(inode))
50 stat->attributes |= STATX_ATTR_AUTOMOUNT;
51}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052EXPORT_SYMBOL(generic_fillattr);
53
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040054/**
55 * vfs_getattr_nosec - getattr without security checks
56 * @path: file to get attributes from
57 * @stat: structure to return attributes in
David Howellsa528d352017-01-31 16:46:22 +000058 * @request_mask: STATX_xxx flags indicating what the caller wants
59 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040060 *
61 * Get attributes without calling security_inode_getattr.
62 *
63 * Currently the only caller other than vfs_getattr is internal to the
David Howellsa528d352017-01-31 16:46:22 +000064 * filehandle lookup code, which uses only the inode number and returns no
65 * attributes to any user. Any other code probably wants vfs_getattr.
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040066 */
David Howellsa528d352017-01-31 16:46:22 +000067int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
68 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
David Howellsbb6687342015-03-17 22:26:21 +000070 struct inode *inode = d_backing_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David Howellsa528d352017-01-31 16:46:22 +000072 memset(stat, 0, sizeof(*stat));
73 stat->result_mask |= STATX_BASIC_STATS;
74 request_mask &= STATX_ALL;
75 query_flags &= KSTAT_QUERY_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (inode->i_op->getattr)
David Howellsa528d352017-01-31 16:46:22 +000077 return inode->i_op->getattr(path, stat, request_mask,
78 query_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 generic_fillattr(inode, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82}
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040083EXPORT_SYMBOL(vfs_getattr_nosec);
84
David Howellsa528d352017-01-31 16:46:22 +000085/*
86 * vfs_getattr - Get the enhanced basic attributes of a file
87 * @path: The file of interest
88 * @stat: Where to return the statistics
89 * @request_mask: STATX_xxx flags indicating what the caller wants
90 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
91 *
92 * Ask the filesystem for a file's attributes. The caller must indicate in
93 * request_mask and query_flags to indicate what they want.
94 *
95 * If the file is remote, the filesystem can be forced to update the attributes
96 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
97 * suppress the update by passing AT_STATX_DONT_SYNC.
98 *
99 * Bits must have been set in request_mask to indicate which attributes the
100 * caller wants retrieving. Any such attribute not requested may be returned
101 * anyway, but the value may be approximate, and, if remote, may not have been
102 * synchronised with the server.
103 *
104 * 0 will be returned on success, and a -ve error code if unsuccessful.
105 */
106int vfs_getattr(const struct path *path, struct kstat *stat,
107 u32 request_mask, unsigned int query_flags)
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400108{
109 int retval;
110
Al Viro3f7036a2015-03-08 19:28:30 -0400111 retval = security_inode_getattr(path);
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400112 if (retval)
113 return retval;
David Howellsa528d352017-01-31 16:46:22 +0000114 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116EXPORT_SYMBOL(vfs_getattr);
117
David Howellsa528d352017-01-31 16:46:22 +0000118/**
119 * vfs_statx_fd - Get the enhanced basic attributes by file descriptor
120 * @fd: The file descriptor referring to the file of interest
121 * @stat: The result structure to fill in.
122 * @request_mask: STATX_xxx flags indicating what the caller wants
123 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
124 *
125 * This function is a wrapper around vfs_getattr(). The main difference is
126 * that it uses a file descriptor to determine the file location.
127 *
128 * 0 will be returned on success, and a -ve error code if unsuccessful.
129 */
130int vfs_statx_fd(unsigned int fd, struct kstat *stat,
131 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Al Viro2903ff02012-08-28 12:52:22 -0400133 struct fd f = fdget_raw(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int error = -EBADF;
135
Al Viro2903ff02012-08-28 12:52:22 -0400136 if (f.file) {
David Howellsa528d352017-01-31 16:46:22 +0000137 error = vfs_getattr(&f.file->f_path, stat,
138 request_mask, query_flags);
Al Viro2903ff02012-08-28 12:52:22 -0400139 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 return error;
142}
David Howellsa528d352017-01-31 16:46:22 +0000143EXPORT_SYMBOL(vfs_statx_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Howellsa528d352017-01-31 16:46:22 +0000145/**
146 * vfs_statx - Get basic and extra attributes by filename
147 * @dfd: A file descriptor representing the base dir for a relative filename
148 * @filename: The name of the file of interest
149 * @flags: Flags to control the query
150 * @stat: The result structure to fill in.
151 * @request_mask: STATX_xxx flags indicating what the caller wants
152 *
153 * This function is a wrapper around vfs_getattr(). The main difference is
154 * that it uses a filename and base directory to determine the file location.
155 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
156 * at the given name from being referenced.
157 *
158 * The caller must have preset stat->request_mask as for vfs_getattr(). The
159 * flags are also used to load up stat->query_flags.
160 *
161 * 0 will be returned on success, and a -ve error code if unsuccessful.
162 */
163int vfs_statx(int dfd, const char __user *filename, int flags,
164 struct kstat *stat, u32 request_mask)
Oleg Drokin0112fc22009-04-08 20:05:42 +0400165{
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400166 struct path path;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400167 int error = -EINVAL;
David Howellsa528d352017-01-31 16:46:22 +0000168 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400169
David Howellsa528d352017-01-31 16:46:22 +0000170 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
171 AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
172 return -EINVAL;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400173
David Howellsa528d352017-01-31 16:46:22 +0000174 if (flags & AT_SYMLINK_NOFOLLOW)
175 lookup_flags &= ~LOOKUP_FOLLOW;
176 if (flags & AT_NO_AUTOMOUNT)
177 lookup_flags &= ~LOOKUP_AUTOMOUNT;
178 if (flags & AT_EMPTY_PATH)
Al Viro65cfc672011-03-13 15:56:26 -0400179 lookup_flags |= LOOKUP_EMPTY;
David Howellsa528d352017-01-31 16:46:22 +0000180
Jeff Layton836fb7e2012-12-11 12:10:05 -0500181retry:
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400182 error = user_path_at(dfd, filename, lookup_flags, &path);
183 if (error)
184 goto out;
185
David Howellsa528d352017-01-31 16:46:22 +0000186 error = vfs_getattr(&path, stat, request_mask, flags);
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400187 path_put(&path);
Jeff Layton836fb7e2012-12-11 12:10:05 -0500188 if (retry_estale(error, lookup_flags)) {
189 lookup_flags |= LOOKUP_REVAL;
190 goto retry;
191 }
Oleg Drokin0112fc22009-04-08 20:05:42 +0400192out:
193 return error;
194}
David Howellsa528d352017-01-31 16:46:22 +0000195EXPORT_SYMBOL(vfs_statx);
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400196
Oleg Drokin0112fc22009-04-08 20:05:42 +0400197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#ifdef __ARCH_WANT_OLD_STAT
199
200/*
201 * For backward compatibility? Maybe this should be moved
202 * into arch/i386 instead?
203 */
204static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
205{
206 static int warncount = 5;
207 struct __old_kernel_stat tmp;
David Howellsa528d352017-01-31 16:46:22 +0000208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (warncount > 0) {
210 warncount--;
211 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
212 current->comm);
213 } else if (warncount < 0) {
214 /* it's laughable, but... */
215 warncount = 0;
216 }
217
218 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
219 tmp.st_dev = old_encode_dev(stat->dev);
220 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700221 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
222 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 tmp.st_mode = stat->mode;
224 tmp.st_nlink = stat->nlink;
225 if (tmp.st_nlink != stat->nlink)
226 return -EOVERFLOW;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800227 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
228 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 tmp.st_rdev = old_encode_dev(stat->rdev);
230#if BITS_PER_LONG == 32
231 if (stat->size > MAX_NON_LFS)
232 return -EOVERFLOW;
David Howellsa528d352017-01-31 16:46:22 +0000233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 tmp.st_size = stat->size;
235 tmp.st_atime = stat->atime.tv_sec;
236 tmp.st_mtime = stat->mtime.tv_sec;
237 tmp.st_ctime = stat->ctime.tv_sec;
238 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
239}
240
David Howellsc7887322010-08-11 11:26:22 +0100241SYSCALL_DEFINE2(stat, const char __user *, filename,
242 struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400245 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400247 error = vfs_stat(filename, &stat);
248 if (error)
249 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400251 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Heiko Carstens257ac262009-01-14 14:14:13 +0100253
David Howellsc7887322010-08-11 11:26:22 +0100254SYSCALL_DEFINE2(lstat, const char __user *, filename,
255 struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400258 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400260 error = vfs_lstat(filename, &stat);
261 if (error)
262 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400264 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
Heiko Carstens257ac262009-01-14 14:14:13 +0100266
267SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct kstat stat;
270 int error = vfs_fstat(fd, &stat);
271
272 if (!error)
273 error = cp_old_stat(&stat, statbuf);
274
275 return error;
276}
277
278#endif /* __ARCH_WANT_OLD_STAT */
279
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700280#if BITS_PER_LONG == 32
281# define choose_32_64(a,b) a
282#else
283# define choose_32_64(a,b) b
284#endif
285
Yaowei Bai4c416f42016-01-15 16:58:01 -0800286#define valid_dev(x) choose_32_64(old_valid_dev(x),true)
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700287#define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
288
Linus Torvalds8529f612012-05-06 18:02:40 -0700289#ifndef INIT_STRUCT_STAT_PADDING
290# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
291#endif
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
294{
295 struct stat tmp;
296
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700297 if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -EOVERFLOW;
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700299#if BITS_PER_LONG == 32
300 if (stat->size > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -EOVERFLOW;
302#endif
303
Linus Torvalds8529f612012-05-06 18:02:40 -0700304 INIT_STRUCT_STAT_PADDING(tmp);
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700305 tmp.st_dev = encode_dev(stat->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700307 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
308 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 tmp.st_mode = stat->mode;
310 tmp.st_nlink = stat->nlink;
311 if (tmp.st_nlink != stat->nlink)
312 return -EOVERFLOW;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800313 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
314 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700315 tmp.st_rdev = encode_dev(stat->rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 tmp.st_size = stat->size;
317 tmp.st_atime = stat->atime.tv_sec;
318 tmp.st_mtime = stat->mtime.tv_sec;
319 tmp.st_ctime = stat->ctime.tv_sec;
320#ifdef STAT_HAVE_NSEC
321 tmp.st_atime_nsec = stat->atime.tv_nsec;
322 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
323 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
324#endif
325 tmp.st_blocks = stat->blocks;
326 tmp.st_blksize = stat->blksize;
327 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
328}
329
David Howellsc7887322010-08-11 11:26:22 +0100330SYSCALL_DEFINE2(newstat, const char __user *, filename,
331 struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400334 int error = vfs_stat(filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400336 if (error)
337 return error;
338 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800340
David Howellsc7887322010-08-11 11:26:22 +0100341SYSCALL_DEFINE2(newlstat, const char __user *, filename,
342 struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400345 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400347 error = vfs_lstat(filename, &stat);
348 if (error)
349 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400351 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800353
Andreas Schwab2833c282006-04-27 15:46:42 +0200354#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
David Howellsc7887322010-08-11 11:26:22 +0100355SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100356 struct stat __user *, statbuf, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800357{
358 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400359 int error;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800360
Oleg Drokin0112fc22009-04-08 20:05:42 +0400361 error = vfs_fstatat(dfd, filename, &stat, flag);
362 if (error)
363 return error;
364 return cp_new_stat(&stat, statbuf);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800365}
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800366#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800367
Heiko Carstens257ac262009-01-14 14:14:13 +0100368SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct kstat stat;
371 int error = vfs_fstat(fd, &stat);
372
373 if (!error)
374 error = cp_new_stat(&stat, statbuf);
375
376 return error;
377}
378
Heiko Carstens6559eed82009-01-14 14:14:32 +0100379SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
380 char __user *, buf, int, bufsiz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Al Viro2d8f3032008-07-22 09:59:21 -0400382 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int error;
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100384 int empty = 0;
Jeff Layton79551192012-12-11 12:10:06 -0500385 unsigned int lookup_flags = LOOKUP_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (bufsiz <= 0)
388 return -EINVAL;
389
Jeff Layton79551192012-12-11 12:10:06 -0500390retry:
391 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!error) {
David Howellsbb6687342015-03-17 22:26:21 +0000393 struct inode *inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100395 error = empty ? -ENOENT : -EINVAL;
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +0100396 /*
397 * AFS mountpoints allow readlink(2) but are not symlinks
398 */
399 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
Al Viro2d8f3032008-07-22 09:59:21 -0400400 error = security_inode_readlink(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!error) {
Al Viro68ac1232012-03-15 08:21:57 -0400402 touch_atime(&path);
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +0100403 error = vfs_readlink(path.dentry, buf, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
Al Viro2d8f3032008-07-22 09:59:21 -0400406 path_put(&path);
Jeff Layton79551192012-12-11 12:10:06 -0500407 if (retry_estale(error, lookup_flags)) {
408 lookup_flags |= LOOKUP_REVAL;
409 goto retry;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 return error;
413}
414
Heiko Carstens002c8972009-01-14 14:14:18 +0100415SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
416 int, bufsiz)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800417{
418 return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422/* ---------- LFS-64 ----------- */
Catalin Marinas0753f702012-03-19 15:13:51 +0000423#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds8529f612012-05-06 18:02:40 -0700425#ifndef INIT_STRUCT_STAT64_PADDING
426# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
427#endif
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
430{
431 struct stat64 tmp;
432
Linus Torvalds8529f612012-05-06 18:02:40 -0700433 INIT_STRUCT_STAT64_PADDING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#ifdef CONFIG_MIPS
435 /* mips has weird padding, so we don't get 64 bits there */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 tmp.st_dev = new_encode_dev(stat->dev);
437 tmp.st_rdev = new_encode_dev(stat->rdev);
438#else
439 tmp.st_dev = huge_encode_dev(stat->dev);
440 tmp.st_rdev = huge_encode_dev(stat->rdev);
441#endif
442 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700443 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
444 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445#ifdef STAT64_HAS_BROKEN_ST_INO
446 tmp.__st_ino = stat->ino;
447#endif
448 tmp.st_mode = stat->mode;
449 tmp.st_nlink = stat->nlink;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800450 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
451 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 tmp.st_atime = stat->atime.tv_sec;
453 tmp.st_atime_nsec = stat->atime.tv_nsec;
454 tmp.st_mtime = stat->mtime.tv_sec;
455 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
456 tmp.st_ctime = stat->ctime.tv_sec;
457 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
458 tmp.st_size = stat->size;
459 tmp.st_blocks = stat->blocks;
460 tmp.st_blksize = stat->blksize;
461 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
462}
463
David Howellsc7887322010-08-11 11:26:22 +0100464SYSCALL_DEFINE2(stat64, const char __user *, filename,
465 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct kstat stat;
468 int error = vfs_stat(filename, &stat);
469
470 if (!error)
471 error = cp_new_stat64(&stat, statbuf);
472
473 return error;
474}
Heiko Carstens257ac262009-01-14 14:14:13 +0100475
David Howellsc7887322010-08-11 11:26:22 +0100476SYSCALL_DEFINE2(lstat64, const char __user *, filename,
477 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct kstat stat;
480 int error = vfs_lstat(filename, &stat);
481
482 if (!error)
483 error = cp_new_stat64(&stat, statbuf);
484
485 return error;
486}
Heiko Carstens257ac262009-01-14 14:14:13 +0100487
488SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct kstat stat;
491 int error = vfs_fstat(fd, &stat);
492
493 if (!error)
494 error = cp_new_stat64(&stat, statbuf);
495
496 return error;
497}
498
David Howellsc7887322010-08-11 11:26:22 +0100499SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100500 struct stat64 __user *, statbuf, int, flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800501{
502 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400503 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800504
Oleg Drokin0112fc22009-04-08 20:05:42 +0400505 error = vfs_fstatat(dfd, filename, &stat, flag);
506 if (error)
507 return error;
508 return cp_new_stat64(&stat, statbuf);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800509}
Catalin Marinas0753f702012-03-19 15:13:51 +0000510#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
David Howellsa528d352017-01-31 16:46:22 +0000512static inline int __put_timestamp(struct timespec *kts,
513 struct statx_timestamp __user *uts)
514{
515 return (__put_user(kts->tv_sec, &uts->tv_sec ) ||
516 __put_user(kts->tv_nsec, &uts->tv_nsec ) ||
517 __put_user(0, &uts->__reserved ));
518}
519
520/*
521 * Set the statx results.
522 */
523static long statx_set_result(struct kstat *stat, struct statx __user *buffer)
524{
525 uid_t uid = from_kuid_munged(current_user_ns(), stat->uid);
526 gid_t gid = from_kgid_munged(current_user_ns(), stat->gid);
527
528 if (__put_user(stat->result_mask, &buffer->stx_mask ) ||
529 __put_user(stat->mode, &buffer->stx_mode ) ||
530 __clear_user(&buffer->__spare0, sizeof(buffer->__spare0)) ||
531 __put_user(stat->nlink, &buffer->stx_nlink ) ||
532 __put_user(uid, &buffer->stx_uid ) ||
533 __put_user(gid, &buffer->stx_gid ) ||
534 __put_user(stat->attributes, &buffer->stx_attributes ) ||
535 __put_user(stat->blksize, &buffer->stx_blksize ) ||
536 __put_user(MAJOR(stat->rdev), &buffer->stx_rdev_major ) ||
537 __put_user(MINOR(stat->rdev), &buffer->stx_rdev_minor ) ||
538 __put_user(MAJOR(stat->dev), &buffer->stx_dev_major ) ||
539 __put_user(MINOR(stat->dev), &buffer->stx_dev_minor ) ||
540 __put_timestamp(&stat->atime, &buffer->stx_atime ) ||
541 __put_timestamp(&stat->btime, &buffer->stx_btime ) ||
542 __put_timestamp(&stat->ctime, &buffer->stx_ctime ) ||
543 __put_timestamp(&stat->mtime, &buffer->stx_mtime ) ||
544 __put_user(stat->ino, &buffer->stx_ino ) ||
545 __put_user(stat->size, &buffer->stx_size ) ||
546 __put_user(stat->blocks, &buffer->stx_blocks ) ||
547 __clear_user(&buffer->__spare1, sizeof(buffer->__spare1)) ||
548 __clear_user(&buffer->__spare2, sizeof(buffer->__spare2)))
549 return -EFAULT;
550
551 return 0;
552}
553
554/**
555 * sys_statx - System call to get enhanced stats
556 * @dfd: Base directory to pathwalk from *or* fd to stat.
557 * @filename: File to stat *or* NULL.
558 * @flags: AT_* flags to control pathwalk.
559 * @mask: Parts of statx struct actually required.
560 * @buffer: Result buffer.
561 *
562 * Note that if filename is NULL, then it does the equivalent of fstat() using
563 * dfd to indicate the file of interest.
564 */
565SYSCALL_DEFINE5(statx,
566 int, dfd, const char __user *, filename, unsigned, flags,
567 unsigned int, mask,
568 struct statx __user *, buffer)
569{
570 struct kstat stat;
571 int error;
572
573 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
574 return -EINVAL;
575 if (!access_ok(VERIFY_WRITE, buffer, sizeof(*buffer)))
576 return -EFAULT;
577
578 if (filename)
579 error = vfs_statx(dfd, filename, flags, &stat, mask);
580 else
581 error = vfs_statx_fd(dfd, &stat, mask, flags);
582 if (error)
583 return error;
584 return statx_set_result(&stat, buffer);
585}
586
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300587/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
588void __inode_add_bytes(struct inode *inode, loff_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 inode->i_blocks += bytes >> 9;
591 bytes &= 511;
592 inode->i_bytes += bytes;
593 if (inode->i_bytes >= 512) {
594 inode->i_blocks++;
595 inode->i_bytes -= 512;
596 }
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300597}
598
599void inode_add_bytes(struct inode *inode, loff_t bytes)
600{
601 spin_lock(&inode->i_lock);
602 __inode_add_bytes(inode, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_unlock(&inode->i_lock);
604}
605
606EXPORT_SYMBOL(inode_add_bytes);
607
Jan Kara1c8924e2013-08-17 09:32:32 -0400608void __inode_sub_bytes(struct inode *inode, loff_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 inode->i_blocks -= bytes >> 9;
611 bytes &= 511;
612 if (inode->i_bytes < bytes) {
613 inode->i_blocks--;
614 inode->i_bytes += 512;
615 }
616 inode->i_bytes -= bytes;
Jan Kara1c8924e2013-08-17 09:32:32 -0400617}
618
619EXPORT_SYMBOL(__inode_sub_bytes);
620
621void inode_sub_bytes(struct inode *inode, loff_t bytes)
622{
623 spin_lock(&inode->i_lock);
624 __inode_sub_bytes(inode, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 spin_unlock(&inode->i_lock);
626}
627
628EXPORT_SYMBOL(inode_sub_bytes);
629
630loff_t inode_get_bytes(struct inode *inode)
631{
632 loff_t ret;
633
634 spin_lock(&inode->i_lock);
635 ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
636 spin_unlock(&inode->i_lock);
637 return ret;
638}
639
640EXPORT_SYMBOL(inode_get_bytes);
641
642void inode_set_bytes(struct inode *inode, loff_t bytes)
643{
644 /* Caller is here responsible for sufficient locking
645 * (ie. inode->i_lock) */
646 inode->i_blocks = bytes >> 9;
647 inode->i_bytes = bytes & 511;
648}
649
650EXPORT_SYMBOL(inode_set_bytes);