uapi: deprecate STATX_ALL
[linux-block.git] / fs / stat.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/stat.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
630d9c47 8#include <linux/export.h>
1da177e4
LT
9#include <linux/mm.h>
10#include <linux/errno.h>
11#include <linux/file.h>
1da177e4
LT
12#include <linux/highuid.h>
13#include <linux/fs.h>
14#include <linux/namei.h>
15#include <linux/security.h>
5b825c3a 16#include <linux/cred.h>
1da177e4 17#include <linux/syscalls.h>
ba52de12 18#include <linux/pagemap.h>
ac565de3 19#include <linux/compat.h>
1da177e4 20
7c0f6ba6 21#include <linux/uaccess.h>
1da177e4
LT
22#include <asm/unistd.h>
23
3934e36f
JA
24#include "internal.h"
25
a528d35e
DH
26/**
27 * generic_fillattr - Fill in the basic attributes from the inode struct
28 * @inode: Inode to use as the source
29 * @stat: Where to fill in the attributes
30 *
31 * Fill in the basic attributes in the kstat structure from data that's to be
32 * found on the VFS inode structure. This is the default if no getattr inode
33 * operation is supplied.
34 */
1da177e4
LT
35void generic_fillattr(struct inode *inode, struct kstat *stat)
36{
37 stat->dev = inode->i_sb->s_dev;
38 stat->ino = inode->i_ino;
39 stat->mode = inode->i_mode;
40 stat->nlink = inode->i_nlink;
41 stat->uid = inode->i_uid;
42 stat->gid = inode->i_gid;
43 stat->rdev = inode->i_rdev;
3ddcd056 44 stat->size = i_size_read(inode);
1da177e4
LT
45 stat->atime = inode->i_atime;
46 stat->mtime = inode->i_mtime;
47 stat->ctime = inode->i_ctime;
93407472 48 stat->blksize = i_blocksize(inode);
3ddcd056 49 stat->blocks = inode->i_blocks;
a528d35e 50}
1da177e4
LT
51EXPORT_SYMBOL(generic_fillattr);
52
b7a6ec52
BF
53/**
54 * vfs_getattr_nosec - getattr without security checks
55 * @path: file to get attributes from
56 * @stat: structure to return attributes in
a528d35e
DH
57 * @request_mask: STATX_xxx flags indicating what the caller wants
58 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
b7a6ec52
BF
59 *
60 * Get attributes without calling security_inode_getattr.
61 *
62 * Currently the only caller other than vfs_getattr is internal to the
a528d35e
DH
63 * filehandle lookup code, which uses only the inode number and returns no
64 * attributes to any user. Any other code probably wants vfs_getattr.
b7a6ec52 65 */
a528d35e
DH
66int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
67 u32 request_mask, unsigned int query_flags)
1da177e4 68{
bb668734 69 struct inode *inode = d_backing_inode(path->dentry);
1da177e4 70
a528d35e
DH
71 memset(stat, 0, sizeof(*stat));
72 stat->result_mask |= STATX_BASIC_STATS;
a528d35e 73 query_flags &= KSTAT_QUERY_FLAGS;
801e5237
CH
74
75 /* allow the fs to override these if it really wants to */
76 if (IS_NOATIME(inode))
77 stat->result_mask &= ~STATX_ATIME;
78 if (IS_AUTOMOUNT(inode))
79 stat->attributes |= STATX_ATTR_AUTOMOUNT;
80
1da177e4 81 if (inode->i_op->getattr)
a528d35e
DH
82 return inode->i_op->getattr(path, stat, request_mask,
83 query_flags);
1da177e4
LT
84
85 generic_fillattr(inode, stat);
1da177e4
LT
86 return 0;
87}
b7a6ec52
BF
88EXPORT_SYMBOL(vfs_getattr_nosec);
89
a528d35e
DH
90/*
91 * vfs_getattr - Get the enhanced basic attributes of a file
92 * @path: The file of interest
93 * @stat: Where to return the statistics
94 * @request_mask: STATX_xxx flags indicating what the caller wants
95 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
96 *
97 * Ask the filesystem for a file's attributes. The caller must indicate in
98 * request_mask and query_flags to indicate what they want.
99 *
100 * If the file is remote, the filesystem can be forced to update the attributes
101 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
102 * suppress the update by passing AT_STATX_DONT_SYNC.
103 *
104 * Bits must have been set in request_mask to indicate which attributes the
105 * caller wants retrieving. Any such attribute not requested may be returned
106 * anyway, but the value may be approximate, and, if remote, may not have been
107 * synchronised with the server.
108 *
109 * 0 will be returned on success, and a -ve error code if unsuccessful.
110 */
111int vfs_getattr(const struct path *path, struct kstat *stat,
112 u32 request_mask, unsigned int query_flags)
b7a6ec52
BF
113{
114 int retval;
115
3f7036a0 116 retval = security_inode_getattr(path);
b7a6ec52
BF
117 if (retval)
118 return retval;
a528d35e 119 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
b7a6ec52 120}
1da177e4
LT
121EXPORT_SYMBOL(vfs_getattr);
122
a528d35e
DH
123/**
124 * vfs_statx_fd - Get the enhanced basic attributes by file descriptor
125 * @fd: The file descriptor referring to the file of interest
126 * @stat: The result structure to fill in.
127 * @request_mask: STATX_xxx flags indicating what the caller wants
128 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
129 *
130 * This function is a wrapper around vfs_getattr(). The main difference is
131 * that it uses a file descriptor to determine the file location.
132 *
133 * 0 will be returned on success, and a -ve error code if unsuccessful.
134 */
135int vfs_statx_fd(unsigned int fd, struct kstat *stat,
136 u32 request_mask, unsigned int query_flags)
1da177e4 137{
8c7493aa 138 struct fd f;
1da177e4
LT
139 int error = -EBADF;
140
8c7493aa
EB
141 if (query_flags & ~KSTAT_QUERY_FLAGS)
142 return -EINVAL;
143
144 f = fdget_raw(fd);
2903ff01 145 if (f.file) {
a528d35e
DH
146 error = vfs_getattr(&f.file->f_path, stat,
147 request_mask, query_flags);
2903ff01 148 fdput(f);
1da177e4
LT
149 }
150 return error;
151}
a528d35e 152EXPORT_SYMBOL(vfs_statx_fd);
1da177e4 153
3934e36f
JA
154inline unsigned vfs_stat_set_lookup_flags(unsigned *lookup_flags, int flags)
155{
156 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
157 AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
158 return -EINVAL;
159
160 *lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
161 if (flags & AT_SYMLINK_NOFOLLOW)
162 *lookup_flags &= ~LOOKUP_FOLLOW;
163 if (flags & AT_NO_AUTOMOUNT)
164 *lookup_flags &= ~LOOKUP_AUTOMOUNT;
165 if (flags & AT_EMPTY_PATH)
166 *lookup_flags |= LOOKUP_EMPTY;
167
168 return 0;
169}
170
a528d35e
DH
171/**
172 * vfs_statx - Get basic and extra attributes by filename
173 * @dfd: A file descriptor representing the base dir for a relative filename
174 * @filename: The name of the file of interest
175 * @flags: Flags to control the query
176 * @stat: The result structure to fill in.
177 * @request_mask: STATX_xxx flags indicating what the caller wants
178 *
179 * This function is a wrapper around vfs_getattr(). The main difference is
180 * that it uses a filename and base directory to determine the file location.
181 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
182 * at the given name from being referenced.
183 *
a528d35e
DH
184 * 0 will be returned on success, and a -ve error code if unsuccessful.
185 */
186int vfs_statx(int dfd, const char __user *filename, int flags,
187 struct kstat *stat, u32 request_mask)
0112fc22 188{
2eae7a18 189 struct path path;
0112fc22 190 int error = -EINVAL;
3934e36f 191 unsigned lookup_flags;
0112fc22 192
3934e36f 193 if (vfs_stat_set_lookup_flags(&lookup_flags, flags))
a528d35e 194 return -EINVAL;
836fb7e7 195retry:
2eae7a18
CH
196 error = user_path_at(dfd, filename, lookup_flags, &path);
197 if (error)
198 goto out;
199
a528d35e 200 error = vfs_getattr(&path, stat, request_mask, flags);
2eae7a18 201 path_put(&path);
836fb7e7
JL
202 if (retry_estale(error, lookup_flags)) {
203 lookup_flags |= LOOKUP_REVAL;
204 goto retry;
205 }
0112fc22
OD
206out:
207 return error;
208}
a528d35e 209EXPORT_SYMBOL(vfs_statx);
2eae7a18 210
0112fc22 211
1da177e4
LT
212#ifdef __ARCH_WANT_OLD_STAT
213
214/*
215 * For backward compatibility? Maybe this should be moved
216 * into arch/i386 instead?
217 */
218static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
219{
220 static int warncount = 5;
221 struct __old_kernel_stat tmp;
a528d35e 222
1da177e4
LT
223 if (warncount > 0) {
224 warncount--;
225 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
226 current->comm);
227 } else if (warncount < 0) {
228 /* it's laughable, but... */
229 warncount = 0;
230 }
231
232 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
233 tmp.st_dev = old_encode_dev(stat->dev);
234 tmp.st_ino = stat->ino;
afefdbb2
DH
235 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
236 return -EOVERFLOW;
1da177e4
LT
237 tmp.st_mode = stat->mode;
238 tmp.st_nlink = stat->nlink;
239 if (tmp.st_nlink != stat->nlink)
240 return -EOVERFLOW;
a7c1938e
EB
241 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
242 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
1da177e4
LT
243 tmp.st_rdev = old_encode_dev(stat->rdev);
244#if BITS_PER_LONG == 32
245 if (stat->size > MAX_NON_LFS)
246 return -EOVERFLOW;
a528d35e 247#endif
1da177e4
LT
248 tmp.st_size = stat->size;
249 tmp.st_atime = stat->atime.tv_sec;
250 tmp.st_mtime = stat->mtime.tv_sec;
251 tmp.st_ctime = stat->ctime.tv_sec;
252 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
253}
254
c7887325
DH
255SYSCALL_DEFINE2(stat, const char __user *, filename,
256 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
257{
258 struct kstat stat;
2eae7a18 259 int error;
1da177e4 260
2eae7a18
CH
261 error = vfs_stat(filename, &stat);
262 if (error)
263 return error;
1da177e4 264
2eae7a18 265 return cp_old_stat(&stat, statbuf);
1da177e4 266}
257ac264 267
c7887325
DH
268SYSCALL_DEFINE2(lstat, const char __user *, filename,
269 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
270{
271 struct kstat stat;
2eae7a18 272 int error;
1da177e4 273
2eae7a18
CH
274 error = vfs_lstat(filename, &stat);
275 if (error)
276 return error;
1da177e4 277
2eae7a18 278 return cp_old_stat(&stat, statbuf);
1da177e4 279}
257ac264
HC
280
281SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
282{
283 struct kstat stat;
284 int error = vfs_fstat(fd, &stat);
285
286 if (!error)
287 error = cp_old_stat(&stat, statbuf);
288
289 return error;
290}
291
292#endif /* __ARCH_WANT_OLD_STAT */
293
82b355d1
AB
294#ifdef __ARCH_WANT_NEW_STAT
295
a52dd971
LT
296#if BITS_PER_LONG == 32
297# define choose_32_64(a,b) a
298#else
299# define choose_32_64(a,b) b
300#endif
301
4c416f42 302#define valid_dev(x) choose_32_64(old_valid_dev(x),true)
a52dd971
LT
303#define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
304
8529f613
LT
305#ifndef INIT_STRUCT_STAT_PADDING
306# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
307#endif
308
1da177e4
LT
309static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
310{
311 struct stat tmp;
312
a52dd971 313 if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
1da177e4 314 return -EOVERFLOW;
a52dd971
LT
315#if BITS_PER_LONG == 32
316 if (stat->size > MAX_NON_LFS)
1da177e4
LT
317 return -EOVERFLOW;
318#endif
319
8529f613 320 INIT_STRUCT_STAT_PADDING(tmp);
a52dd971 321 tmp.st_dev = encode_dev(stat->dev);
1da177e4 322 tmp.st_ino = stat->ino;
afefdbb2
DH
323 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
324 return -EOVERFLOW;
1da177e4
LT
325 tmp.st_mode = stat->mode;
326 tmp.st_nlink = stat->nlink;
327 if (tmp.st_nlink != stat->nlink)
328 return -EOVERFLOW;
a7c1938e
EB
329 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
330 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
a52dd971 331 tmp.st_rdev = encode_dev(stat->rdev);
1da177e4
LT
332 tmp.st_size = stat->size;
333 tmp.st_atime = stat->atime.tv_sec;
334 tmp.st_mtime = stat->mtime.tv_sec;
335 tmp.st_ctime = stat->ctime.tv_sec;
336#ifdef STAT_HAVE_NSEC
337 tmp.st_atime_nsec = stat->atime.tv_nsec;
338 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
339 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
340#endif
341 tmp.st_blocks = stat->blocks;
342 tmp.st_blksize = stat->blksize;
343 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
344}
345
c7887325
DH
346SYSCALL_DEFINE2(newstat, const char __user *, filename,
347 struct stat __user *, statbuf)
5590ff0d
UD
348{
349 struct kstat stat;
2eae7a18 350 int error = vfs_stat(filename, &stat);
5590ff0d 351
2eae7a18
CH
352 if (error)
353 return error;
354 return cp_new_stat(&stat, statbuf);
5590ff0d
UD
355}
356
c7887325
DH
357SYSCALL_DEFINE2(newlstat, const char __user *, filename,
358 struct stat __user *, statbuf)
1da177e4
LT
359{
360 struct kstat stat;
2eae7a18 361 int error;
1da177e4 362
2eae7a18
CH
363 error = vfs_lstat(filename, &stat);
364 if (error)
365 return error;
1da177e4 366
2eae7a18 367 return cp_new_stat(&stat, statbuf);
1da177e4 368}
5590ff0d 369
2833c28a 370#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
c7887325 371SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
6559eed8 372 struct stat __user *, statbuf, int, flag)
1da177e4
LT
373{
374 struct kstat stat;
0112fc22 375 int error;
1da177e4 376
0112fc22
OD
377 error = vfs_fstatat(dfd, filename, &stat, flag);
378 if (error)
379 return error;
380 return cp_new_stat(&stat, statbuf);
1da177e4 381}
cff2b760 382#endif
5590ff0d 383
257ac264 384SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
1da177e4
LT
385{
386 struct kstat stat;
387 int error = vfs_fstat(fd, &stat);
388
389 if (!error)
390 error = cp_new_stat(&stat, statbuf);
391
392 return error;
393}
82b355d1 394#endif
1da177e4 395
2dae0248
DB
396static int do_readlinkat(int dfd, const char __user *pathname,
397 char __user *buf, int bufsiz)
1da177e4 398{
2d8f3038 399 struct path path;
1da177e4 400 int error;
1fa1e7f6 401 int empty = 0;
7955119e 402 unsigned int lookup_flags = LOOKUP_EMPTY;
1da177e4
LT
403
404 if (bufsiz <= 0)
405 return -EINVAL;
406
7955119e
JL
407retry:
408 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
1da177e4 409 if (!error) {
bb668734 410 struct inode *inode = d_backing_inode(path.dentry);
1da177e4 411
1fa1e7f6 412 error = empty ? -ENOENT : -EINVAL;
fd4a0edf
MS
413 /*
414 * AFS mountpoints allow readlink(2) but are not symlinks
415 */
416 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
2d8f3038 417 error = security_inode_readlink(path.dentry);
1da177e4 418 if (!error) {
68ac1234 419 touch_atime(&path);
fd4a0edf 420 error = vfs_readlink(path.dentry, buf, bufsiz);
1da177e4
LT
421 }
422 }
2d8f3038 423 path_put(&path);
7955119e
JL
424 if (retry_estale(error, lookup_flags)) {
425 lookup_flags |= LOOKUP_REVAL;
426 goto retry;
427 }
1da177e4
LT
428 }
429 return error;
430}
431
2dae0248
DB
432SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
433 char __user *, buf, int, bufsiz)
434{
435 return do_readlinkat(dfd, pathname, buf, bufsiz);
436}
437
002c8976
HC
438SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
439 int, bufsiz)
5590ff0d 440{
2dae0248 441 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
5590ff0d
UD
442}
443
1da177e4
LT
444
445/* ---------- LFS-64 ----------- */
0753f70f 446#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
1da177e4 447
8529f613
LT
448#ifndef INIT_STRUCT_STAT64_PADDING
449# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
450#endif
451
1da177e4
LT
452static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
453{
454 struct stat64 tmp;
455
8529f613 456 INIT_STRUCT_STAT64_PADDING(tmp);
1da177e4
LT
457#ifdef CONFIG_MIPS
458 /* mips has weird padding, so we don't get 64 bits there */
1da177e4
LT
459 tmp.st_dev = new_encode_dev(stat->dev);
460 tmp.st_rdev = new_encode_dev(stat->rdev);
461#else
462 tmp.st_dev = huge_encode_dev(stat->dev);
463 tmp.st_rdev = huge_encode_dev(stat->rdev);
464#endif
465 tmp.st_ino = stat->ino;
afefdbb2
DH
466 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
467 return -EOVERFLOW;
1da177e4
LT
468#ifdef STAT64_HAS_BROKEN_ST_INO
469 tmp.__st_ino = stat->ino;
470#endif
471 tmp.st_mode = stat->mode;
472 tmp.st_nlink = stat->nlink;
a7c1938e
EB
473 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
474 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
1da177e4
LT
475 tmp.st_atime = stat->atime.tv_sec;
476 tmp.st_atime_nsec = stat->atime.tv_nsec;
477 tmp.st_mtime = stat->mtime.tv_sec;
478 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
479 tmp.st_ctime = stat->ctime.tv_sec;
480 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
481 tmp.st_size = stat->size;
482 tmp.st_blocks = stat->blocks;
483 tmp.st_blksize = stat->blksize;
484 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
485}
486
c7887325
DH
487SYSCALL_DEFINE2(stat64, const char __user *, filename,
488 struct stat64 __user *, statbuf)
1da177e4
LT
489{
490 struct kstat stat;
491 int error = vfs_stat(filename, &stat);
492
493 if (!error)
494 error = cp_new_stat64(&stat, statbuf);
495
496 return error;
497}
257ac264 498
c7887325
DH
499SYSCALL_DEFINE2(lstat64, const char __user *, filename,
500 struct stat64 __user *, statbuf)
1da177e4
LT
501{
502 struct kstat stat;
503 int error = vfs_lstat(filename, &stat);
504
505 if (!error)
506 error = cp_new_stat64(&stat, statbuf);
507
508 return error;
509}
257ac264
HC
510
511SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
1da177e4
LT
512{
513 struct kstat stat;
514 int error = vfs_fstat(fd, &stat);
515
516 if (!error)
517 error = cp_new_stat64(&stat, statbuf);
518
519 return error;
520}
521
c7887325 522SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
6559eed8 523 struct stat64 __user *, statbuf, int, flag)
cff2b760
UD
524{
525 struct kstat stat;
0112fc22 526 int error;
cff2b760 527
0112fc22
OD
528 error = vfs_fstatat(dfd, filename, &stat, flag);
529 if (error)
530 return error;
531 return cp_new_stat64(&stat, statbuf);
cff2b760 532}
0753f70f 533#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
1da177e4 534
3934e36f 535noinline_for_stack int
64bd7204 536cp_statx(const struct kstat *stat, struct statx __user *buffer)
a528d35e 537{
64bd7204
EB
538 struct statx tmp;
539
540 memset(&tmp, 0, sizeof(tmp));
541
542 tmp.stx_mask = stat->result_mask;
543 tmp.stx_blksize = stat->blksize;
544 tmp.stx_attributes = stat->attributes;
545 tmp.stx_nlink = stat->nlink;
546 tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
547 tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
548 tmp.stx_mode = stat->mode;
549 tmp.stx_ino = stat->ino;
550 tmp.stx_size = stat->size;
551 tmp.stx_blocks = stat->blocks;
3209f68b 552 tmp.stx_attributes_mask = stat->attributes_mask;
64bd7204
EB
553 tmp.stx_atime.tv_sec = stat->atime.tv_sec;
554 tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
555 tmp.stx_btime.tv_sec = stat->btime.tv_sec;
556 tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
557 tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
558 tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
559 tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
560 tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
561 tmp.stx_rdev_major = MAJOR(stat->rdev);
562 tmp.stx_rdev_minor = MINOR(stat->rdev);
563 tmp.stx_dev_major = MAJOR(stat->dev);
564 tmp.stx_dev_minor = MINOR(stat->dev);
565
566 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
a528d35e
DH
567}
568
569/**
570 * sys_statx - System call to get enhanced stats
571 * @dfd: Base directory to pathwalk from *or* fd to stat.
1e2f82d1 572 * @filename: File to stat or "" with AT_EMPTY_PATH
a528d35e
DH
573 * @flags: AT_* flags to control pathwalk.
574 * @mask: Parts of statx struct actually required.
575 * @buffer: Result buffer.
576 *
1e2f82d1
DH
577 * Note that fstat() can be emulated by setting dfd to the fd of interest,
578 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
a528d35e
DH
579 */
580SYSCALL_DEFINE5(statx,
581 int, dfd, const char __user *, filename, unsigned, flags,
582 unsigned int, mask,
583 struct statx __user *, buffer)
584{
585 struct kstat stat;
586 int error;
587
47071aee
DH
588 if (mask & STATX__RESERVED)
589 return -EINVAL;
a528d35e
DH
590 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
591 return -EINVAL;
a528d35e 592
1e2f82d1 593 error = vfs_statx(dfd, filename, flags, &stat, mask);
a528d35e
DH
594 if (error)
595 return error;
64bd7204
EB
596
597 return cp_statx(&stat, buffer);
a528d35e
DH
598}
599
ac565de3
AV
600#ifdef CONFIG_COMPAT
601static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
602{
603 struct compat_stat tmp;
604
605 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
606 return -EOVERFLOW;
607
608 memset(&tmp, 0, sizeof(tmp));
609 tmp.st_dev = old_encode_dev(stat->dev);
610 tmp.st_ino = stat->ino;
611 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
612 return -EOVERFLOW;
613 tmp.st_mode = stat->mode;
614 tmp.st_nlink = stat->nlink;
615 if (tmp.st_nlink != stat->nlink)
616 return -EOVERFLOW;
617 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
618 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
619 tmp.st_rdev = old_encode_dev(stat->rdev);
620 if ((u64) stat->size > MAX_NON_LFS)
621 return -EOVERFLOW;
622 tmp.st_size = stat->size;
623 tmp.st_atime = stat->atime.tv_sec;
624 tmp.st_atime_nsec = stat->atime.tv_nsec;
625 tmp.st_mtime = stat->mtime.tv_sec;
626 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
627 tmp.st_ctime = stat->ctime.tv_sec;
628 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
629 tmp.st_blocks = stat->blocks;
630 tmp.st_blksize = stat->blksize;
631 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
632}
633
634COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
635 struct compat_stat __user *, statbuf)
636{
637 struct kstat stat;
638 int error;
639
640 error = vfs_stat(filename, &stat);
641 if (error)
642 return error;
643 return cp_compat_stat(&stat, statbuf);
644}
645
646COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
647 struct compat_stat __user *, statbuf)
648{
649 struct kstat stat;
650 int error;
651
652 error = vfs_lstat(filename, &stat);
653 if (error)
654 return error;
655 return cp_compat_stat(&stat, statbuf);
656}
657
658#ifndef __ARCH_WANT_STAT64
659COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
660 const char __user *, filename,
661 struct compat_stat __user *, statbuf, int, flag)
662{
663 struct kstat stat;
664 int error;
665
666 error = vfs_fstatat(dfd, filename, &stat, flag);
667 if (error)
668 return error;
669 return cp_compat_stat(&stat, statbuf);
670}
671#endif
672
673COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
674 struct compat_stat __user *, statbuf)
675{
676 struct kstat stat;
677 int error = vfs_fstat(fd, &stat);
678
679 if (!error)
680 error = cp_compat_stat(&stat, statbuf);
681 return error;
682}
683#endif
684
b462707e
DM
685/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
686void __inode_add_bytes(struct inode *inode, loff_t bytes)
1da177e4 687{
1da177e4
LT
688 inode->i_blocks += bytes >> 9;
689 bytes &= 511;
690 inode->i_bytes += bytes;
691 if (inode->i_bytes >= 512) {
692 inode->i_blocks++;
693 inode->i_bytes -= 512;
694 }
b462707e 695}
eb315d2a 696EXPORT_SYMBOL(__inode_add_bytes);
b462707e
DM
697
698void inode_add_bytes(struct inode *inode, loff_t bytes)
699{
700 spin_lock(&inode->i_lock);
701 __inode_add_bytes(inode, bytes);
1da177e4
LT
702 spin_unlock(&inode->i_lock);
703}
704
705EXPORT_SYMBOL(inode_add_bytes);
706
1c8924eb 707void __inode_sub_bytes(struct inode *inode, loff_t bytes)
1da177e4 708{
1da177e4
LT
709 inode->i_blocks -= bytes >> 9;
710 bytes &= 511;
711 if (inode->i_bytes < bytes) {
712 inode->i_blocks--;
713 inode->i_bytes += 512;
714 }
715 inode->i_bytes -= bytes;
1c8924eb
JK
716}
717
718EXPORT_SYMBOL(__inode_sub_bytes);
719
720void inode_sub_bytes(struct inode *inode, loff_t bytes)
721{
722 spin_lock(&inode->i_lock);
723 __inode_sub_bytes(inode, bytes);
1da177e4
LT
724 spin_unlock(&inode->i_lock);
725}
726
727EXPORT_SYMBOL(inode_sub_bytes);
728
729loff_t inode_get_bytes(struct inode *inode)
730{
731 loff_t ret;
732
733 spin_lock(&inode->i_lock);
f4a8116a 734 ret = __inode_get_bytes(inode);
1da177e4
LT
735 spin_unlock(&inode->i_lock);
736 return ret;
737}
738
739EXPORT_SYMBOL(inode_get_bytes);
740
741void inode_set_bytes(struct inode *inode, loff_t bytes)
742{
743 /* Caller is here responsible for sufficient locking
744 * (ie. inode->i_lock) */
745 inode->i_blocks = bytes >> 9;
746 inode->i_bytes = bytes & 511;
747}
748
749EXPORT_SYMBOL(inode_set_bytes);