vfs: mostly undo glibc turning 'fstat()' into 'fstatat(AT_EMPTY_PATH)'
[linux-2.6-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
2d985f8c 8#include <linux/blkdev.h>
630d9c47 9#include <linux/export.h>
1da177e4
LT
10#include <linux/mm.h>
11#include <linux/errno.h>
12#include <linux/file.h>
1da177e4
LT
13#include <linux/highuid.h>
14#include <linux/fs.h>
15#include <linux/namei.h>
16#include <linux/security.h>
5b825c3a 17#include <linux/cred.h>
1da177e4 18#include <linux/syscalls.h>
ba52de12 19#include <linux/pagemap.h>
ac565de3 20#include <linux/compat.h>
a1175d6b 21#include <linux/iversion.h>
1da177e4 22
7c0f6ba6 23#include <linux/uaccess.h>
1da177e4
LT
24#include <asm/unistd.h>
25
3934e36f 26#include "internal.h"
fa2fcf4f 27#include "mount.h"
3934e36f 28
ffb6cf19
JL
29/**
30 * fill_mg_cmtime - Fill in the mtime and ctime and flag ctime as QUERIED
31 * @stat: where to store the resulting values
32 * @request_mask: STATX_* values requested
33 * @inode: inode from which to grab the c/mtime
34 *
35 * Given @inode, grab the ctime and mtime out if it and store the result
36 * in @stat. When fetching the value, flag it as queried so the next write
37 * will use a fine-grained timestamp.
38 */
39void fill_mg_cmtime(struct kstat *stat, u32 request_mask, struct inode *inode)
40{
41 atomic_long_t *pnsec = (atomic_long_t *)&inode->__i_ctime.tv_nsec;
42
43 /* If neither time was requested, then don't report them */
44 if (!(request_mask & (STATX_CTIME|STATX_MTIME))) {
45 stat->result_mask &= ~(STATX_CTIME|STATX_MTIME);
46 return;
47 }
48
49 stat->mtime = inode->i_mtime;
50 stat->ctime.tv_sec = inode->__i_ctime.tv_sec;
51 /*
52 * Atomically set the QUERIED flag and fetch the new value with
53 * the flag masked off.
54 */
55 stat->ctime.tv_nsec = atomic_long_fetch_or(I_CTIME_QUERIED, pnsec) &
56 ~I_CTIME_QUERIED;
57}
58EXPORT_SYMBOL(fill_mg_cmtime);
59
a528d35e
DH
60/**
61 * generic_fillattr - Fill in the basic attributes from the inode struct
0d72b928
JL
62 * @idmap: idmap of the mount the inode was found from
63 * @request_mask: statx request_mask
64 * @inode: Inode to use as the source
65 * @stat: Where to fill in the attributes
a528d35e
DH
66 *
67 * Fill in the basic attributes in the kstat structure from data that's to be
68 * found on the VFS inode structure. This is the default if no getattr inode
69 * operation is supplied.
0d56a451 70 *
b74d24f7
CB
71 * If the inode has been found through an idmapped mount the idmap of
72 * the vfsmount must be passed through @idmap. This function will then
73 * take care to map the inode according to @idmap before filling in the
0d56a451 74 * uid and gid filds. On non-idmapped mounts or if permission checking is to be
b74d24f7 75 * performed on the raw inode simply passs @nop_mnt_idmap.
a528d35e 76 */
0d72b928
JL
77void generic_fillattr(struct mnt_idmap *idmap, u32 request_mask,
78 struct inode *inode, struct kstat *stat)
1da177e4 79{
e67fe633
CB
80 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
81 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
a2bd096f 82
1da177e4
LT
83 stat->dev = inode->i_sb->s_dev;
84 stat->ino = inode->i_ino;
85 stat->mode = inode->i_mode;
86 stat->nlink = inode->i_nlink;
a2bd096f
CB
87 stat->uid = vfsuid_into_kuid(vfsuid);
88 stat->gid = vfsgid_into_kgid(vfsgid);
1da177e4 89 stat->rdev = inode->i_rdev;
3ddcd056 90 stat->size = i_size_read(inode);
1da177e4 91 stat->atime = inode->i_atime;
ffb6cf19
JL
92
93 if (is_mgtime(inode)) {
94 fill_mg_cmtime(stat, request_mask, inode);
95 } else {
96 stat->mtime = inode->i_mtime;
97 stat->ctime = inode_get_ctime(inode);
98 }
99
93407472 100 stat->blksize = i_blocksize(inode);
3ddcd056 101 stat->blocks = inode->i_blocks;
0d72b928
JL
102
103 if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
104 stat->result_mask |= STATX_CHANGE_COOKIE;
105 stat->change_cookie = inode_query_iversion(inode);
106 }
107
a528d35e 108}
1da177e4
LT
109EXPORT_SYMBOL(generic_fillattr);
110
4f911138
AG
111/**
112 * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
113 * @inode: Inode to use as the source
114 * @stat: Where to fill in the attribute flags
115 *
116 * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
117 * inode that are published on i_flags and enforced by the VFS.
118 */
119void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
120{
121 if (inode->i_flags & S_IMMUTABLE)
122 stat->attributes |= STATX_ATTR_IMMUTABLE;
123 if (inode->i_flags & S_APPEND)
124 stat->attributes |= STATX_ATTR_APPEND;
125 stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
126}
127EXPORT_SYMBOL(generic_fill_statx_attr);
128
b7a6ec52
BF
129/**
130 * vfs_getattr_nosec - getattr without security checks
131 * @path: file to get attributes from
132 * @stat: structure to return attributes in
a528d35e 133 * @request_mask: STATX_xxx flags indicating what the caller wants
f2d077ff 134 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
b7a6ec52
BF
135 *
136 * Get attributes without calling security_inode_getattr.
137 *
138 * Currently the only caller other than vfs_getattr is internal to the
a528d35e
DH
139 * filehandle lookup code, which uses only the inode number and returns no
140 * attributes to any user. Any other code probably wants vfs_getattr.
b7a6ec52 141 */
a528d35e
DH
142int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
143 u32 request_mask, unsigned int query_flags)
1da177e4 144{
b74d24f7 145 struct mnt_idmap *idmap;
bb668734 146 struct inode *inode = d_backing_inode(path->dentry);
1da177e4 147
a528d35e
DH
148 memset(stat, 0, sizeof(*stat));
149 stat->result_mask |= STATX_BASIC_STATS;
f2d077ff 150 query_flags &= AT_STATX_SYNC_TYPE;
801e5237
CH
151
152 /* allow the fs to override these if it really wants to */
761e28fa
MS
153 /* SB_NOATIME means filesystem supplies dummy atime value */
154 if (inode->i_sb->s_flags & SB_NOATIME)
801e5237 155 stat->result_mask &= ~STATX_ATIME;
5afa7e8b
TT
156
157 /*
158 * Note: If you add another clause to set an attribute flag, please
159 * update attributes_mask below.
160 */
801e5237
CH
161 if (IS_AUTOMOUNT(inode))
162 stat->attributes |= STATX_ATTR_AUTOMOUNT;
163
712b2698
IW
164 if (IS_DAX(inode))
165 stat->attributes |= STATX_ATTR_DAX;
166
5afa7e8b
TT
167 stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
168 STATX_ATTR_DAX);
169
b74d24f7 170 idmap = mnt_idmap(path->mnt);
1da177e4 171 if (inode->i_op->getattr)
b74d24f7 172 return inode->i_op->getattr(idmap, path, stat,
549c7297 173 request_mask, query_flags);
1da177e4 174
0d72b928 175 generic_fillattr(idmap, request_mask, inode, stat);
1da177e4
LT
176 return 0;
177}
b7a6ec52
BF
178EXPORT_SYMBOL(vfs_getattr_nosec);
179
a528d35e
DH
180/*
181 * vfs_getattr - Get the enhanced basic attributes of a file
182 * @path: The file of interest
183 * @stat: Where to return the statistics
184 * @request_mask: STATX_xxx flags indicating what the caller wants
f2d077ff 185 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
a528d35e
DH
186 *
187 * Ask the filesystem for a file's attributes. The caller must indicate in
188 * request_mask and query_flags to indicate what they want.
189 *
190 * If the file is remote, the filesystem can be forced to update the attributes
191 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
192 * suppress the update by passing AT_STATX_DONT_SYNC.
193 *
194 * Bits must have been set in request_mask to indicate which attributes the
195 * caller wants retrieving. Any such attribute not requested may be returned
196 * anyway, but the value may be approximate, and, if remote, may not have been
197 * synchronised with the server.
198 *
199 * 0 will be returned on success, and a -ve error code if unsuccessful.
200 */
201int vfs_getattr(const struct path *path, struct kstat *stat,
202 u32 request_mask, unsigned int query_flags)
b7a6ec52
BF
203{
204 int retval;
205
3f7036a0 206 retval = security_inode_getattr(path);
b7a6ec52
BF
207 if (retval)
208 return retval;
a528d35e 209 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
b7a6ec52 210}
1da177e4
LT
211EXPORT_SYMBOL(vfs_getattr);
212
a528d35e 213/**
da9aa5d9 214 * vfs_fstat - Get the basic attributes by file descriptor
a528d35e
DH
215 * @fd: The file descriptor referring to the file of interest
216 * @stat: The result structure to fill in.
a528d35e
DH
217 *
218 * This function is a wrapper around vfs_getattr(). The main difference is
219 * that it uses a file descriptor to determine the file location.
220 *
221 * 0 will be returned on success, and a -ve error code if unsuccessful.
222 */
da9aa5d9 223int vfs_fstat(int fd, struct kstat *stat)
1da177e4 224{
8c7493aa 225 struct fd f;
da9aa5d9 226 int error;
8c7493aa
EB
227
228 f = fdget_raw(fd);
da9aa5d9
CH
229 if (!f.file)
230 return -EBADF;
231 error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
232 fdput(f);
1da177e4
LT
233 return error;
234}
1da177e4 235
1b6fe6e0
SR
236int getname_statx_lookup_flags(int flags)
237{
238 int lookup_flags = 0;
239
240 if (!(flags & AT_SYMLINK_NOFOLLOW))
241 lookup_flags |= LOOKUP_FOLLOW;
242 if (!(flags & AT_NO_AUTOMOUNT))
243 lookup_flags |= LOOKUP_AUTOMOUNT;
244 if (flags & AT_EMPTY_PATH)
245 lookup_flags |= LOOKUP_EMPTY;
246
247 return lookup_flags;
248}
249
a528d35e
DH
250/**
251 * vfs_statx - Get basic and extra attributes by filename
252 * @dfd: A file descriptor representing the base dir for a relative filename
253 * @filename: The name of the file of interest
254 * @flags: Flags to control the query
255 * @stat: The result structure to fill in.
256 * @request_mask: STATX_xxx flags indicating what the caller wants
257 *
258 * This function is a wrapper around vfs_getattr(). The main difference is
259 * that it uses a filename and base directory to determine the file location.
260 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
261 * at the given name from being referenced.
262 *
a528d35e
DH
263 * 0 will be returned on success, and a -ve error code if unsuccessful.
264 */
1b6fe6e0 265static int vfs_statx(int dfd, struct filename *filename, int flags,
a528d35e 266 struct kstat *stat, u32 request_mask)
0112fc22 267{
2eae7a18 268 struct path path;
1b6fe6e0 269 unsigned int lookup_flags = getname_statx_lookup_flags(flags);
b3f05150 270 int error;
0112fc22 271
b3f05150 272 if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
f2d077ff 273 AT_STATX_SYNC_TYPE))
a528d35e 274 return -EINVAL;
b3f05150 275
836fb7e7 276retry:
1b6fe6e0 277 error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
2eae7a18
CH
278 if (error)
279 goto out;
280
a528d35e 281 error = vfs_getattr(&path, stat, request_mask, flags);
2d985f8c 282
fa2fcf4f
MS
283 stat->mnt_id = real_mount(path.mnt)->mnt_id;
284 stat->result_mask |= STATX_MNT_ID;
2d985f8c 285
80340fe3
MS
286 if (path.mnt->mnt_root == path.dentry)
287 stat->attributes |= STATX_ATTR_MOUNT_ROOT;
288 stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
2d985f8c
EB
289
290 /* Handle STATX_DIOALIGN for block devices. */
291 if (request_mask & STATX_DIOALIGN) {
292 struct inode *inode = d_backing_inode(path.dentry);
293
294 if (S_ISBLK(inode->i_mode))
295 bdev_statx_dioalign(inode, stat);
296 }
297
2eae7a18 298 path_put(&path);
836fb7e7
JL
299 if (retry_estale(error, lookup_flags)) {
300 lookup_flags |= LOOKUP_REVAL;
301 goto retry;
302 }
0112fc22
OD
303out:
304 return error;
305}
2eae7a18 306
09f1bde4
CH
307int vfs_fstatat(int dfd, const char __user *filename,
308 struct kstat *stat, int flags)
309{
1b6fe6e0
SR
310 int ret;
311 int statx_flags = flags | AT_NO_AUTOMOUNT;
312 struct filename *name;
313
9013c51c
LT
314 /*
315 * Work around glibc turning fstat() into fstatat(AT_EMPTY_PATH)
316 *
317 * If AT_EMPTY_PATH is set, we expect the common case to be that
318 * empty path, and avoid doing all the extra pathname work.
319 */
320 if (dfd >= 0 && flags == AT_EMPTY_PATH) {
321 char c;
322
323 ret = get_user(c, filename);
324 if (unlikely(ret))
325 return ret;
326
327 if (likely(!c))
328 return vfs_fstat(dfd, stat);
329 }
330
1b6fe6e0
SR
331 name = getname_flags(filename, getname_statx_lookup_flags(statx_flags), NULL);
332 ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS);
333 putname(name);
334
335 return ret;
09f1bde4 336}
0112fc22 337
1da177e4
LT
338#ifdef __ARCH_WANT_OLD_STAT
339
340/*
341 * For backward compatibility? Maybe this should be moved
342 * into arch/i386 instead?
343 */
344static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
345{
346 static int warncount = 5;
347 struct __old_kernel_stat tmp;
a528d35e 348
1da177e4
LT
349 if (warncount > 0) {
350 warncount--;
351 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
352 current->comm);
353 } else if (warncount < 0) {
354 /* it's laughable, but... */
355 warncount = 0;
356 }
357
358 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
359 tmp.st_dev = old_encode_dev(stat->dev);
360 tmp.st_ino = stat->ino;
afefdbb2
DH
361 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
362 return -EOVERFLOW;
1da177e4
LT
363 tmp.st_mode = stat->mode;
364 tmp.st_nlink = stat->nlink;
365 if (tmp.st_nlink != stat->nlink)
366 return -EOVERFLOW;
a7c1938e
EB
367 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
368 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
1da177e4
LT
369 tmp.st_rdev = old_encode_dev(stat->rdev);
370#if BITS_PER_LONG == 32
371 if (stat->size > MAX_NON_LFS)
372 return -EOVERFLOW;
a528d35e 373#endif
1da177e4
LT
374 tmp.st_size = stat->size;
375 tmp.st_atime = stat->atime.tv_sec;
376 tmp.st_mtime = stat->mtime.tv_sec;
377 tmp.st_ctime = stat->ctime.tv_sec;
378 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
379}
380
c7887325
DH
381SYSCALL_DEFINE2(stat, const char __user *, filename,
382 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
383{
384 struct kstat stat;
2eae7a18 385 int error;
1da177e4 386
2eae7a18
CH
387 error = vfs_stat(filename, &stat);
388 if (error)
389 return error;
1da177e4 390
2eae7a18 391 return cp_old_stat(&stat, statbuf);
1da177e4 392}
257ac264 393
c7887325
DH
394SYSCALL_DEFINE2(lstat, const char __user *, filename,
395 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
396{
397 struct kstat stat;
2eae7a18 398 int error;
1da177e4 399
2eae7a18
CH
400 error = vfs_lstat(filename, &stat);
401 if (error)
402 return error;
1da177e4 403
2eae7a18 404 return cp_old_stat(&stat, statbuf);
1da177e4 405}
257ac264
HC
406
407SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
408{
409 struct kstat stat;
410 int error = vfs_fstat(fd, &stat);
411
412 if (!error)
413 error = cp_old_stat(&stat, statbuf);
414
415 return error;
416}
417
418#endif /* __ARCH_WANT_OLD_STAT */
419
82b355d1
AB
420#ifdef __ARCH_WANT_NEW_STAT
421
a52dd971
LT
422#if BITS_PER_LONG == 32
423# define choose_32_64(a,b) a
424#else
425# define choose_32_64(a,b) b
426#endif
427
8529f613
LT
428#ifndef INIT_STRUCT_STAT_PADDING
429# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
430#endif
431
1da177e4
LT
432static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
433{
434 struct stat tmp;
435
932aba1e
MP
436 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
437 return -EOVERFLOW;
438 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
1da177e4 439 return -EOVERFLOW;
a52dd971
LT
440#if BITS_PER_LONG == 32
441 if (stat->size > MAX_NON_LFS)
1da177e4
LT
442 return -EOVERFLOW;
443#endif
444
8529f613 445 INIT_STRUCT_STAT_PADDING(tmp);
932aba1e 446 tmp.st_dev = new_encode_dev(stat->dev);
1da177e4 447 tmp.st_ino = stat->ino;
afefdbb2
DH
448 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
449 return -EOVERFLOW;
1da177e4
LT
450 tmp.st_mode = stat->mode;
451 tmp.st_nlink = stat->nlink;
452 if (tmp.st_nlink != stat->nlink)
453 return -EOVERFLOW;
a7c1938e
EB
454 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
455 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
932aba1e 456 tmp.st_rdev = new_encode_dev(stat->rdev);
1da177e4
LT
457 tmp.st_size = stat->size;
458 tmp.st_atime = stat->atime.tv_sec;
459 tmp.st_mtime = stat->mtime.tv_sec;
460 tmp.st_ctime = stat->ctime.tv_sec;
461#ifdef STAT_HAVE_NSEC
462 tmp.st_atime_nsec = stat->atime.tv_nsec;
463 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
464 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
465#endif
466 tmp.st_blocks = stat->blocks;
467 tmp.st_blksize = stat->blksize;
468 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
469}
470
c7887325
DH
471SYSCALL_DEFINE2(newstat, const char __user *, filename,
472 struct stat __user *, statbuf)
5590ff0d
UD
473{
474 struct kstat stat;
2eae7a18 475 int error = vfs_stat(filename, &stat);
5590ff0d 476
2eae7a18
CH
477 if (error)
478 return error;
479 return cp_new_stat(&stat, statbuf);
5590ff0d
UD
480}
481
c7887325
DH
482SYSCALL_DEFINE2(newlstat, const char __user *, filename,
483 struct stat __user *, statbuf)
1da177e4
LT
484{
485 struct kstat stat;
2eae7a18 486 int error;
1da177e4 487
2eae7a18
CH
488 error = vfs_lstat(filename, &stat);
489 if (error)
490 return error;
1da177e4 491
2eae7a18 492 return cp_new_stat(&stat, statbuf);
1da177e4 493}
5590ff0d 494
2833c28a 495#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
c7887325 496SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
6559eed8 497 struct stat __user *, statbuf, int, flag)
1da177e4
LT
498{
499 struct kstat stat;
0112fc22 500 int error;
1da177e4 501
0112fc22
OD
502 error = vfs_fstatat(dfd, filename, &stat, flag);
503 if (error)
504 return error;
505 return cp_new_stat(&stat, statbuf);
1da177e4 506}
cff2b760 507#endif
5590ff0d 508
257ac264 509SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
1da177e4
LT
510{
511 struct kstat stat;
512 int error = vfs_fstat(fd, &stat);
513
514 if (!error)
515 error = cp_new_stat(&stat, statbuf);
516
517 return error;
518}
82b355d1 519#endif
1da177e4 520
2dae0248
DB
521static int do_readlinkat(int dfd, const char __user *pathname,
522 char __user *buf, int bufsiz)
1da177e4 523{
2d8f3038 524 struct path path;
1da177e4 525 int error;
1fa1e7f6 526 int empty = 0;
7955119e 527 unsigned int lookup_flags = LOOKUP_EMPTY;
1da177e4
LT
528
529 if (bufsiz <= 0)
530 return -EINVAL;
531
7955119e
JL
532retry:
533 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
1da177e4 534 if (!error) {
bb668734 535 struct inode *inode = d_backing_inode(path.dentry);
1da177e4 536
1fa1e7f6 537 error = empty ? -ENOENT : -EINVAL;
fd4a0edf
MS
538 /*
539 * AFS mountpoints allow readlink(2) but are not symlinks
540 */
541 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
2d8f3038 542 error = security_inode_readlink(path.dentry);
1da177e4 543 if (!error) {
68ac1234 544 touch_atime(&path);
fd4a0edf 545 error = vfs_readlink(path.dentry, buf, bufsiz);
1da177e4
LT
546 }
547 }
2d8f3038 548 path_put(&path);
7955119e
JL
549 if (retry_estale(error, lookup_flags)) {
550 lookup_flags |= LOOKUP_REVAL;
551 goto retry;
552 }
1da177e4
LT
553 }
554 return error;
555}
556
2dae0248
DB
557SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
558 char __user *, buf, int, bufsiz)
559{
560 return do_readlinkat(dfd, pathname, buf, bufsiz);
561}
562
002c8976
HC
563SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
564 int, bufsiz)
5590ff0d 565{
2dae0248 566 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
5590ff0d
UD
567}
568
1da177e4
LT
569
570/* ---------- LFS-64 ----------- */
0753f70f 571#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
1da177e4 572
8529f613
LT
573#ifndef INIT_STRUCT_STAT64_PADDING
574# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
575#endif
576
1da177e4
LT
577static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
578{
579 struct stat64 tmp;
580
8529f613 581 INIT_STRUCT_STAT64_PADDING(tmp);
1da177e4
LT
582#ifdef CONFIG_MIPS
583 /* mips has weird padding, so we don't get 64 bits there */
1da177e4
LT
584 tmp.st_dev = new_encode_dev(stat->dev);
585 tmp.st_rdev = new_encode_dev(stat->rdev);
586#else
587 tmp.st_dev = huge_encode_dev(stat->dev);
588 tmp.st_rdev = huge_encode_dev(stat->rdev);
589#endif
590 tmp.st_ino = stat->ino;
afefdbb2
DH
591 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
592 return -EOVERFLOW;
1da177e4
LT
593#ifdef STAT64_HAS_BROKEN_ST_INO
594 tmp.__st_ino = stat->ino;
595#endif
596 tmp.st_mode = stat->mode;
597 tmp.st_nlink = stat->nlink;
a7c1938e
EB
598 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
599 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
1da177e4
LT
600 tmp.st_atime = stat->atime.tv_sec;
601 tmp.st_atime_nsec = stat->atime.tv_nsec;
602 tmp.st_mtime = stat->mtime.tv_sec;
603 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
604 tmp.st_ctime = stat->ctime.tv_sec;
605 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
606 tmp.st_size = stat->size;
607 tmp.st_blocks = stat->blocks;
608 tmp.st_blksize = stat->blksize;
609 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
610}
611
c7887325
DH
612SYSCALL_DEFINE2(stat64, const char __user *, filename,
613 struct stat64 __user *, statbuf)
1da177e4
LT
614{
615 struct kstat stat;
616 int error = vfs_stat(filename, &stat);
617
618 if (!error)
619 error = cp_new_stat64(&stat, statbuf);
620
621 return error;
622}
257ac264 623
c7887325
DH
624SYSCALL_DEFINE2(lstat64, const char __user *, filename,
625 struct stat64 __user *, statbuf)
1da177e4
LT
626{
627 struct kstat stat;
628 int error = vfs_lstat(filename, &stat);
629
630 if (!error)
631 error = cp_new_stat64(&stat, statbuf);
632
633 return error;
634}
257ac264
HC
635
636SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
1da177e4
LT
637{
638 struct kstat stat;
639 int error = vfs_fstat(fd, &stat);
640
641 if (!error)
642 error = cp_new_stat64(&stat, statbuf);
643
644 return error;
645}
646
c7887325 647SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
6559eed8 648 struct stat64 __user *, statbuf, int, flag)
cff2b760
UD
649{
650 struct kstat stat;
0112fc22 651 int error;
cff2b760 652
0112fc22
OD
653 error = vfs_fstatat(dfd, filename, &stat, flag);
654 if (error)
655 return error;
656 return cp_new_stat64(&stat, statbuf);
cff2b760 657}
0753f70f 658#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
1da177e4 659
6f88cc17 660static noinline_for_stack int
64bd7204 661cp_statx(const struct kstat *stat, struct statx __user *buffer)
a528d35e 662{
64bd7204
EB
663 struct statx tmp;
664
665 memset(&tmp, 0, sizeof(tmp));
666
a1175d6b
JL
667 /* STATX_CHANGE_COOKIE is kernel-only for now */
668 tmp.stx_mask = stat->result_mask & ~STATX_CHANGE_COOKIE;
64bd7204 669 tmp.stx_blksize = stat->blksize;
a1175d6b
JL
670 /* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */
671 tmp.stx_attributes = stat->attributes & ~STATX_ATTR_CHANGE_MONOTONIC;
64bd7204
EB
672 tmp.stx_nlink = stat->nlink;
673 tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
674 tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
675 tmp.stx_mode = stat->mode;
676 tmp.stx_ino = stat->ino;
677 tmp.stx_size = stat->size;
678 tmp.stx_blocks = stat->blocks;
3209f68b 679 tmp.stx_attributes_mask = stat->attributes_mask;
64bd7204
EB
680 tmp.stx_atime.tv_sec = stat->atime.tv_sec;
681 tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
682 tmp.stx_btime.tv_sec = stat->btime.tv_sec;
683 tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
684 tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
685 tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
686 tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
687 tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
688 tmp.stx_rdev_major = MAJOR(stat->rdev);
689 tmp.stx_rdev_minor = MINOR(stat->rdev);
690 tmp.stx_dev_major = MAJOR(stat->dev);
691 tmp.stx_dev_minor = MINOR(stat->dev);
fa2fcf4f 692 tmp.stx_mnt_id = stat->mnt_id;
825cf206
EB
693 tmp.stx_dio_mem_align = stat->dio_mem_align;
694 tmp.stx_dio_offset_align = stat->dio_offset_align;
64bd7204
EB
695
696 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
a528d35e
DH
697}
698
1b6fe6e0 699int do_statx(int dfd, struct filename *filename, unsigned int flags,
0018784f
BM
700 unsigned int mask, struct statx __user *buffer)
701{
702 struct kstat stat;
703 int error;
704
705 if (mask & STATX__RESERVED)
706 return -EINVAL;
707 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
708 return -EINVAL;
709
a1175d6b
JL
710 /* STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
711 * from userland.
712 */
713 mask &= ~STATX_CHANGE_COOKIE;
714
0018784f
BM
715 error = vfs_statx(dfd, filename, flags, &stat, mask);
716 if (error)
717 return error;
718
719 return cp_statx(&stat, buffer);
720}
721
a528d35e
DH
722/**
723 * sys_statx - System call to get enhanced stats
724 * @dfd: Base directory to pathwalk from *or* fd to stat.
1e2f82d1 725 * @filename: File to stat or "" with AT_EMPTY_PATH
a528d35e
DH
726 * @flags: AT_* flags to control pathwalk.
727 * @mask: Parts of statx struct actually required.
728 * @buffer: Result buffer.
729 *
1e2f82d1
DH
730 * Note that fstat() can be emulated by setting dfd to the fd of interest,
731 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
a528d35e
DH
732 */
733SYSCALL_DEFINE5(statx,
734 int, dfd, const char __user *, filename, unsigned, flags,
735 unsigned int, mask,
736 struct statx __user *, buffer)
737{
1b6fe6e0
SR
738 int ret;
739 struct filename *name;
740
741 name = getname_flags(filename, getname_statx_lookup_flags(flags), NULL);
742 ret = do_statx(dfd, name, flags, mask, buffer);
743 putname(name);
744
745 return ret;
a528d35e
DH
746}
747
f18ed30d 748#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
ac565de3
AV
749static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
750{
751 struct compat_stat tmp;
752
932aba1e
MP
753 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
754 return -EOVERFLOW;
755 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
ac565de3
AV
756 return -EOVERFLOW;
757
758 memset(&tmp, 0, sizeof(tmp));
932aba1e 759 tmp.st_dev = new_encode_dev(stat->dev);
ac565de3
AV
760 tmp.st_ino = stat->ino;
761 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
762 return -EOVERFLOW;
763 tmp.st_mode = stat->mode;
764 tmp.st_nlink = stat->nlink;
765 if (tmp.st_nlink != stat->nlink)
766 return -EOVERFLOW;
767 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
768 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
932aba1e 769 tmp.st_rdev = new_encode_dev(stat->rdev);
ac565de3
AV
770 if ((u64) stat->size > MAX_NON_LFS)
771 return -EOVERFLOW;
772 tmp.st_size = stat->size;
773 tmp.st_atime = stat->atime.tv_sec;
774 tmp.st_atime_nsec = stat->atime.tv_nsec;
775 tmp.st_mtime = stat->mtime.tv_sec;
776 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
777 tmp.st_ctime = stat->ctime.tv_sec;
778 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
779 tmp.st_blocks = stat->blocks;
780 tmp.st_blksize = stat->blksize;
781 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
782}
783
784COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
785 struct compat_stat __user *, statbuf)
786{
787 struct kstat stat;
788 int error;
789
790 error = vfs_stat(filename, &stat);
791 if (error)
792 return error;
793 return cp_compat_stat(&stat, statbuf);
794}
795
796COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
797 struct compat_stat __user *, statbuf)
798{
799 struct kstat stat;
800 int error;
801
802 error = vfs_lstat(filename, &stat);
803 if (error)
804 return error;
805 return cp_compat_stat(&stat, statbuf);
806}
807
808#ifndef __ARCH_WANT_STAT64
809COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
810 const char __user *, filename,
811 struct compat_stat __user *, statbuf, int, flag)
812{
813 struct kstat stat;
814 int error;
815
816 error = vfs_fstatat(dfd, filename, &stat, flag);
817 if (error)
818 return error;
819 return cp_compat_stat(&stat, statbuf);
820}
821#endif
822
823COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
824 struct compat_stat __user *, statbuf)
825{
826 struct kstat stat;
827 int error = vfs_fstat(fd, &stat);
828
829 if (!error)
830 error = cp_compat_stat(&stat, statbuf);
831 return error;
832}
833#endif
834
b462707e
DM
835/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
836void __inode_add_bytes(struct inode *inode, loff_t bytes)
1da177e4 837{
1da177e4
LT
838 inode->i_blocks += bytes >> 9;
839 bytes &= 511;
840 inode->i_bytes += bytes;
841 if (inode->i_bytes >= 512) {
842 inode->i_blocks++;
843 inode->i_bytes -= 512;
844 }
b462707e 845}
eb315d2a 846EXPORT_SYMBOL(__inode_add_bytes);
b462707e
DM
847
848void inode_add_bytes(struct inode *inode, loff_t bytes)
849{
850 spin_lock(&inode->i_lock);
851 __inode_add_bytes(inode, bytes);
1da177e4
LT
852 spin_unlock(&inode->i_lock);
853}
854
855EXPORT_SYMBOL(inode_add_bytes);
856
1c8924eb 857void __inode_sub_bytes(struct inode *inode, loff_t bytes)
1da177e4 858{
1da177e4
LT
859 inode->i_blocks -= bytes >> 9;
860 bytes &= 511;
861 if (inode->i_bytes < bytes) {
862 inode->i_blocks--;
863 inode->i_bytes += 512;
864 }
865 inode->i_bytes -= bytes;
1c8924eb
JK
866}
867
868EXPORT_SYMBOL(__inode_sub_bytes);
869
870void inode_sub_bytes(struct inode *inode, loff_t bytes)
871{
872 spin_lock(&inode->i_lock);
873 __inode_sub_bytes(inode, bytes);
1da177e4
LT
874 spin_unlock(&inode->i_lock);
875}
876
877EXPORT_SYMBOL(inode_sub_bytes);
878
879loff_t inode_get_bytes(struct inode *inode)
880{
881 loff_t ret;
882
883 spin_lock(&inode->i_lock);
f4a8116a 884 ret = __inode_get_bytes(inode);
1da177e4
LT
885 spin_unlock(&inode->i_lock);
886 return ret;
887}
888
889EXPORT_SYMBOL(inode_get_bytes);
890
891void inode_set_bytes(struct inode *inode, loff_t bytes)
892{
893 /* Caller is here responsible for sufficient locking
894 * (ie. inode->i_lock) */
895 inode->i_blocks = bytes >> 9;
896 inode->i_bytes = bytes & 511;
897}
898
899EXPORT_SYMBOL(inode_set_bytes);