xref: /linux/fs/stat.c (revision ba6e0e5cb5b2c2e736e16b4aead816450a8718e6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/stat.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7 
8 #include <linux/blkdev.h>
9 #include <linux/export.h>
10 #include <linux/mm.h>
11 #include <linux/errno.h>
12 #include <linux/file.h>
13 #include <linux/highuid.h>
14 #include <linux/fs.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/cred.h>
18 #include <linux/syscalls.h>
19 #include <linux/pagemap.h>
20 #include <linux/compat.h>
21 #include <linux/iversion.h>
22 
23 #include <linux/uaccess.h>
24 #include <asm/unistd.h>
25 
26 #include "internal.h"
27 #include "mount.h"
28 
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  */
39 void 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 }
58 EXPORT_SYMBOL(fill_mg_cmtime);
59 
60 /**
61  * generic_fillattr - Fill in the basic attributes from the inode struct
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
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.
70  *
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
74  * uid and gid filds. On non-idmapped mounts or if permission checking is to be
75  * performed on the raw inode simply passs @nop_mnt_idmap.
76  */
77 void generic_fillattr(struct mnt_idmap *idmap, u32 request_mask,
78 		      struct inode *inode, struct kstat *stat)
79 {
80 	vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
81 	vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
82 
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;
87 	stat->uid = vfsuid_into_kuid(vfsuid);
88 	stat->gid = vfsgid_into_kgid(vfsgid);
89 	stat->rdev = inode->i_rdev;
90 	stat->size = i_size_read(inode);
91 	stat->atime = inode->i_atime;
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 
100 	stat->blksize = i_blocksize(inode);
101 	stat->blocks = inode->i_blocks;
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 
108 }
109 EXPORT_SYMBOL(generic_fillattr);
110 
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  */
119 void 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 }
127 EXPORT_SYMBOL(generic_fill_statx_attr);
128 
129 /**
130  * vfs_getattr_nosec - getattr without security checks
131  * @path: file to get attributes from
132  * @stat: structure to return attributes in
133  * @request_mask: STATX_xxx flags indicating what the caller wants
134  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
135  *
136  * Get attributes without calling security_inode_getattr.
137  *
138  * Currently the only caller other than vfs_getattr is internal to the
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.
141  */
142 int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
143 		      u32 request_mask, unsigned int query_flags)
144 {
145 	struct mnt_idmap *idmap;
146 	struct inode *inode = d_backing_inode(path->dentry);
147 
148 	memset(stat, 0, sizeof(*stat));
149 	stat->result_mask |= STATX_BASIC_STATS;
150 	query_flags &= AT_STATX_SYNC_TYPE;
151 
152 	/* allow the fs to override these if it really wants to */
153 	/* SB_NOATIME means filesystem supplies dummy atime value */
154 	if (inode->i_sb->s_flags & SB_NOATIME)
155 		stat->result_mask &= ~STATX_ATIME;
156 
157 	/*
158 	 * Note: If you add another clause to set an attribute flag, please
159 	 * update attributes_mask below.
160 	 */
161 	if (IS_AUTOMOUNT(inode))
162 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
163 
164 	if (IS_DAX(inode))
165 		stat->attributes |= STATX_ATTR_DAX;
166 
167 	stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
168 				  STATX_ATTR_DAX);
169 
170 	idmap = mnt_idmap(path->mnt);
171 	if (inode->i_op->getattr)
172 		return inode->i_op->getattr(idmap, path, stat,
173 					    request_mask, query_flags);
174 
175 	generic_fillattr(idmap, request_mask, inode, stat);
176 	return 0;
177 }
178 EXPORT_SYMBOL(vfs_getattr_nosec);
179 
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
185  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
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  */
201 int vfs_getattr(const struct path *path, struct kstat *stat,
202 		u32 request_mask, unsigned int query_flags)
203 {
204 	int retval;
205 
206 	retval = security_inode_getattr(path);
207 	if (retval)
208 		return retval;
209 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
210 }
211 EXPORT_SYMBOL(vfs_getattr);
212 
213 /**
214  * vfs_fstat - Get the basic attributes by file descriptor
215  * @fd: The file descriptor referring to the file of interest
216  * @stat: The result structure to fill in.
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  */
223 int vfs_fstat(int fd, struct kstat *stat)
224 {
225 	struct fd f;
226 	int error;
227 
228 	f = fdget_raw(fd);
229 	if (!f.file)
230 		return -EBADF;
231 	error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
232 	fdput(f);
233 	return error;
234 }
235 
236 int 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 
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  *
263  * 0 will be returned on success, and a -ve error code if unsuccessful.
264  */
265 static int vfs_statx(int dfd, struct filename *filename, int flags,
266 	      struct kstat *stat, u32 request_mask)
267 {
268 	struct path path;
269 	unsigned int lookup_flags = getname_statx_lookup_flags(flags);
270 	int error;
271 
272 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
273 		      AT_STATX_SYNC_TYPE))
274 		return -EINVAL;
275 
276 retry:
277 	error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
278 	if (error)
279 		goto out;
280 
281 	error = vfs_getattr(&path, stat, request_mask, flags);
282 
283 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
284 	stat->result_mask |= STATX_MNT_ID;
285 
286 	if (path.mnt->mnt_root == path.dentry)
287 		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
288 	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
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 
298 	path_put(&path);
299 	if (retry_estale(error, lookup_flags)) {
300 		lookup_flags |= LOOKUP_REVAL;
301 		goto retry;
302 	}
303 out:
304 	return error;
305 }
306 
307 int vfs_fstatat(int dfd, const char __user *filename,
308 			      struct kstat *stat, int flags)
309 {
310 	int ret;
311 	int statx_flags = flags | AT_NO_AUTOMOUNT;
312 	struct filename *name;
313 
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 
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;
336 }
337 
338 #ifdef __ARCH_WANT_OLD_STAT
339 
340 /*
341  * For backward compatibility?  Maybe this should be moved
342  * into arch/i386 instead?
343  */
344 static 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;
348 
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;
361 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
362 		return -EOVERFLOW;
363 	tmp.st_mode = stat->mode;
364 	tmp.st_nlink = stat->nlink;
365 	if (tmp.st_nlink != stat->nlink)
366 		return -EOVERFLOW;
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));
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;
373 #endif
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 
381 SYSCALL_DEFINE2(stat, const char __user *, filename,
382 		struct __old_kernel_stat __user *, statbuf)
383 {
384 	struct kstat stat;
385 	int error;
386 
387 	error = vfs_stat(filename, &stat);
388 	if (error)
389 		return error;
390 
391 	return cp_old_stat(&stat, statbuf);
392 }
393 
394 SYSCALL_DEFINE2(lstat, const char __user *, filename,
395 		struct __old_kernel_stat __user *, statbuf)
396 {
397 	struct kstat stat;
398 	int error;
399 
400 	error = vfs_lstat(filename, &stat);
401 	if (error)
402 		return error;
403 
404 	return cp_old_stat(&stat, statbuf);
405 }
406 
407 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
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 
420 #ifdef __ARCH_WANT_NEW_STAT
421 
422 #ifndef INIT_STRUCT_STAT_PADDING
423 #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
424 #endif
425 
426 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
427 {
428 	struct stat tmp;
429 
430 	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
431 		return -EOVERFLOW;
432 	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
433 		return -EOVERFLOW;
434 #if BITS_PER_LONG == 32
435 	if (stat->size > MAX_NON_LFS)
436 		return -EOVERFLOW;
437 #endif
438 
439 	INIT_STRUCT_STAT_PADDING(tmp);
440 	tmp.st_dev = new_encode_dev(stat->dev);
441 	tmp.st_ino = stat->ino;
442 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
443 		return -EOVERFLOW;
444 	tmp.st_mode = stat->mode;
445 	tmp.st_nlink = stat->nlink;
446 	if (tmp.st_nlink != stat->nlink)
447 		return -EOVERFLOW;
448 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
449 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
450 	tmp.st_rdev = new_encode_dev(stat->rdev);
451 	tmp.st_size = stat->size;
452 	tmp.st_atime = stat->atime.tv_sec;
453 	tmp.st_mtime = stat->mtime.tv_sec;
454 	tmp.st_ctime = stat->ctime.tv_sec;
455 #ifdef STAT_HAVE_NSEC
456 	tmp.st_atime_nsec = stat->atime.tv_nsec;
457 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
458 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
459 #endif
460 	tmp.st_blocks = stat->blocks;
461 	tmp.st_blksize = stat->blksize;
462 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
463 }
464 
465 SYSCALL_DEFINE2(newstat, const char __user *, filename,
466 		struct stat __user *, statbuf)
467 {
468 	struct kstat stat;
469 	int error = vfs_stat(filename, &stat);
470 
471 	if (error)
472 		return error;
473 	return cp_new_stat(&stat, statbuf);
474 }
475 
476 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
477 		struct stat __user *, statbuf)
478 {
479 	struct kstat stat;
480 	int error;
481 
482 	error = vfs_lstat(filename, &stat);
483 	if (error)
484 		return error;
485 
486 	return cp_new_stat(&stat, statbuf);
487 }
488 
489 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
490 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
491 		struct stat __user *, statbuf, int, flag)
492 {
493 	struct kstat stat;
494 	int error;
495 
496 	error = vfs_fstatat(dfd, filename, &stat, flag);
497 	if (error)
498 		return error;
499 	return cp_new_stat(&stat, statbuf);
500 }
501 #endif
502 
503 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
504 {
505 	struct kstat stat;
506 	int error = vfs_fstat(fd, &stat);
507 
508 	if (!error)
509 		error = cp_new_stat(&stat, statbuf);
510 
511 	return error;
512 }
513 #endif
514 
515 static int do_readlinkat(int dfd, const char __user *pathname,
516 			 char __user *buf, int bufsiz)
517 {
518 	struct path path;
519 	int error;
520 	int empty = 0;
521 	unsigned int lookup_flags = LOOKUP_EMPTY;
522 
523 	if (bufsiz <= 0)
524 		return -EINVAL;
525 
526 retry:
527 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
528 	if (!error) {
529 		struct inode *inode = d_backing_inode(path.dentry);
530 
531 		error = empty ? -ENOENT : -EINVAL;
532 		/*
533 		 * AFS mountpoints allow readlink(2) but are not symlinks
534 		 */
535 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
536 			error = security_inode_readlink(path.dentry);
537 			if (!error) {
538 				touch_atime(&path);
539 				error = vfs_readlink(path.dentry, buf, bufsiz);
540 			}
541 		}
542 		path_put(&path);
543 		if (retry_estale(error, lookup_flags)) {
544 			lookup_flags |= LOOKUP_REVAL;
545 			goto retry;
546 		}
547 	}
548 	return error;
549 }
550 
551 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
552 		char __user *, buf, int, bufsiz)
553 {
554 	return do_readlinkat(dfd, pathname, buf, bufsiz);
555 }
556 
557 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
558 		int, bufsiz)
559 {
560 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
561 }
562 
563 
564 /* ---------- LFS-64 ----------- */
565 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
566 
567 #ifndef INIT_STRUCT_STAT64_PADDING
568 #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
569 #endif
570 
571 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
572 {
573 	struct stat64 tmp;
574 
575 	INIT_STRUCT_STAT64_PADDING(tmp);
576 #ifdef CONFIG_MIPS
577 	/* mips has weird padding, so we don't get 64 bits there */
578 	tmp.st_dev = new_encode_dev(stat->dev);
579 	tmp.st_rdev = new_encode_dev(stat->rdev);
580 #else
581 	tmp.st_dev = huge_encode_dev(stat->dev);
582 	tmp.st_rdev = huge_encode_dev(stat->rdev);
583 #endif
584 	tmp.st_ino = stat->ino;
585 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
586 		return -EOVERFLOW;
587 #ifdef STAT64_HAS_BROKEN_ST_INO
588 	tmp.__st_ino = stat->ino;
589 #endif
590 	tmp.st_mode = stat->mode;
591 	tmp.st_nlink = stat->nlink;
592 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
593 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
594 	tmp.st_atime = stat->atime.tv_sec;
595 	tmp.st_atime_nsec = stat->atime.tv_nsec;
596 	tmp.st_mtime = stat->mtime.tv_sec;
597 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
598 	tmp.st_ctime = stat->ctime.tv_sec;
599 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
600 	tmp.st_size = stat->size;
601 	tmp.st_blocks = stat->blocks;
602 	tmp.st_blksize = stat->blksize;
603 	return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
604 }
605 
606 SYSCALL_DEFINE2(stat64, const char __user *, filename,
607 		struct stat64 __user *, statbuf)
608 {
609 	struct kstat stat;
610 	int error = vfs_stat(filename, &stat);
611 
612 	if (!error)
613 		error = cp_new_stat64(&stat, statbuf);
614 
615 	return error;
616 }
617 
618 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
619 		struct stat64 __user *, statbuf)
620 {
621 	struct kstat stat;
622 	int error = vfs_lstat(filename, &stat);
623 
624 	if (!error)
625 		error = cp_new_stat64(&stat, statbuf);
626 
627 	return error;
628 }
629 
630 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
631 {
632 	struct kstat stat;
633 	int error = vfs_fstat(fd, &stat);
634 
635 	if (!error)
636 		error = cp_new_stat64(&stat, statbuf);
637 
638 	return error;
639 }
640 
641 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
642 		struct stat64 __user *, statbuf, int, flag)
643 {
644 	struct kstat stat;
645 	int error;
646 
647 	error = vfs_fstatat(dfd, filename, &stat, flag);
648 	if (error)
649 		return error;
650 	return cp_new_stat64(&stat, statbuf);
651 }
652 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
653 
654 static noinline_for_stack int
655 cp_statx(const struct kstat *stat, struct statx __user *buffer)
656 {
657 	struct statx tmp;
658 
659 	memset(&tmp, 0, sizeof(tmp));
660 
661 	/* STATX_CHANGE_COOKIE is kernel-only for now */
662 	tmp.stx_mask = stat->result_mask & ~STATX_CHANGE_COOKIE;
663 	tmp.stx_blksize = stat->blksize;
664 	/* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */
665 	tmp.stx_attributes = stat->attributes & ~STATX_ATTR_CHANGE_MONOTONIC;
666 	tmp.stx_nlink = stat->nlink;
667 	tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
668 	tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
669 	tmp.stx_mode = stat->mode;
670 	tmp.stx_ino = stat->ino;
671 	tmp.stx_size = stat->size;
672 	tmp.stx_blocks = stat->blocks;
673 	tmp.stx_attributes_mask = stat->attributes_mask;
674 	tmp.stx_atime.tv_sec = stat->atime.tv_sec;
675 	tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
676 	tmp.stx_btime.tv_sec = stat->btime.tv_sec;
677 	tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
678 	tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
679 	tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
680 	tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
681 	tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
682 	tmp.stx_rdev_major = MAJOR(stat->rdev);
683 	tmp.stx_rdev_minor = MINOR(stat->rdev);
684 	tmp.stx_dev_major = MAJOR(stat->dev);
685 	tmp.stx_dev_minor = MINOR(stat->dev);
686 	tmp.stx_mnt_id = stat->mnt_id;
687 	tmp.stx_dio_mem_align = stat->dio_mem_align;
688 	tmp.stx_dio_offset_align = stat->dio_offset_align;
689 
690 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
691 }
692 
693 int do_statx(int dfd, struct filename *filename, unsigned int flags,
694 	     unsigned int mask, struct statx __user *buffer)
695 {
696 	struct kstat stat;
697 	int error;
698 
699 	if (mask & STATX__RESERVED)
700 		return -EINVAL;
701 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
702 		return -EINVAL;
703 
704 	/* STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
705 	 * from userland.
706 	 */
707 	mask &= ~STATX_CHANGE_COOKIE;
708 
709 	error = vfs_statx(dfd, filename, flags, &stat, mask);
710 	if (error)
711 		return error;
712 
713 	return cp_statx(&stat, buffer);
714 }
715 
716 /**
717  * sys_statx - System call to get enhanced stats
718  * @dfd: Base directory to pathwalk from *or* fd to stat.
719  * @filename: File to stat or "" with AT_EMPTY_PATH
720  * @flags: AT_* flags to control pathwalk.
721  * @mask: Parts of statx struct actually required.
722  * @buffer: Result buffer.
723  *
724  * Note that fstat() can be emulated by setting dfd to the fd of interest,
725  * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
726  */
727 SYSCALL_DEFINE5(statx,
728 		int, dfd, const char __user *, filename, unsigned, flags,
729 		unsigned int, mask,
730 		struct statx __user *, buffer)
731 {
732 	int ret;
733 	struct filename *name;
734 
735 	name = getname_flags(filename, getname_statx_lookup_flags(flags), NULL);
736 	ret = do_statx(dfd, name, flags, mask, buffer);
737 	putname(name);
738 
739 	return ret;
740 }
741 
742 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
743 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
744 {
745 	struct compat_stat tmp;
746 
747 	if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
748 		return -EOVERFLOW;
749 	if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
750 		return -EOVERFLOW;
751 
752 	memset(&tmp, 0, sizeof(tmp));
753 	tmp.st_dev = new_encode_dev(stat->dev);
754 	tmp.st_ino = stat->ino;
755 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
756 		return -EOVERFLOW;
757 	tmp.st_mode = stat->mode;
758 	tmp.st_nlink = stat->nlink;
759 	if (tmp.st_nlink != stat->nlink)
760 		return -EOVERFLOW;
761 	SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
762 	SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
763 	tmp.st_rdev = new_encode_dev(stat->rdev);
764 	if ((u64) stat->size > MAX_NON_LFS)
765 		return -EOVERFLOW;
766 	tmp.st_size = stat->size;
767 	tmp.st_atime = stat->atime.tv_sec;
768 	tmp.st_atime_nsec = stat->atime.tv_nsec;
769 	tmp.st_mtime = stat->mtime.tv_sec;
770 	tmp.st_mtime_nsec = stat->mtime.tv_nsec;
771 	tmp.st_ctime = stat->ctime.tv_sec;
772 	tmp.st_ctime_nsec = stat->ctime.tv_nsec;
773 	tmp.st_blocks = stat->blocks;
774 	tmp.st_blksize = stat->blksize;
775 	return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
776 }
777 
778 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
779 		       struct compat_stat __user *, statbuf)
780 {
781 	struct kstat stat;
782 	int error;
783 
784 	error = vfs_stat(filename, &stat);
785 	if (error)
786 		return error;
787 	return cp_compat_stat(&stat, statbuf);
788 }
789 
790 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
791 		       struct compat_stat __user *, statbuf)
792 {
793 	struct kstat stat;
794 	int error;
795 
796 	error = vfs_lstat(filename, &stat);
797 	if (error)
798 		return error;
799 	return cp_compat_stat(&stat, statbuf);
800 }
801 
802 #ifndef __ARCH_WANT_STAT64
803 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
804 		       const char __user *, filename,
805 		       struct compat_stat __user *, statbuf, int, flag)
806 {
807 	struct kstat stat;
808 	int error;
809 
810 	error = vfs_fstatat(dfd, filename, &stat, flag);
811 	if (error)
812 		return error;
813 	return cp_compat_stat(&stat, statbuf);
814 }
815 #endif
816 
817 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
818 		       struct compat_stat __user *, statbuf)
819 {
820 	struct kstat stat;
821 	int error = vfs_fstat(fd, &stat);
822 
823 	if (!error)
824 		error = cp_compat_stat(&stat, statbuf);
825 	return error;
826 }
827 #endif
828 
829 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
830 void __inode_add_bytes(struct inode *inode, loff_t bytes)
831 {
832 	inode->i_blocks += bytes >> 9;
833 	bytes &= 511;
834 	inode->i_bytes += bytes;
835 	if (inode->i_bytes >= 512) {
836 		inode->i_blocks++;
837 		inode->i_bytes -= 512;
838 	}
839 }
840 EXPORT_SYMBOL(__inode_add_bytes);
841 
842 void inode_add_bytes(struct inode *inode, loff_t bytes)
843 {
844 	spin_lock(&inode->i_lock);
845 	__inode_add_bytes(inode, bytes);
846 	spin_unlock(&inode->i_lock);
847 }
848 
849 EXPORT_SYMBOL(inode_add_bytes);
850 
851 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
852 {
853 	inode->i_blocks -= bytes >> 9;
854 	bytes &= 511;
855 	if (inode->i_bytes < bytes) {
856 		inode->i_blocks--;
857 		inode->i_bytes += 512;
858 	}
859 	inode->i_bytes -= bytes;
860 }
861 
862 EXPORT_SYMBOL(__inode_sub_bytes);
863 
864 void inode_sub_bytes(struct inode *inode, loff_t bytes)
865 {
866 	spin_lock(&inode->i_lock);
867 	__inode_sub_bytes(inode, bytes);
868 	spin_unlock(&inode->i_lock);
869 }
870 
871 EXPORT_SYMBOL(inode_sub_bytes);
872 
873 loff_t inode_get_bytes(struct inode *inode)
874 {
875 	loff_t ret;
876 
877 	spin_lock(&inode->i_lock);
878 	ret = __inode_get_bytes(inode);
879 	spin_unlock(&inode->i_lock);
880 	return ret;
881 }
882 
883 EXPORT_SYMBOL(inode_get_bytes);
884 
885 void inode_set_bytes(struct inode *inode, loff_t bytes)
886 {
887 	/* Caller is here responsible for sufficient locking
888 	 * (ie. inode->i_lock) */
889 	inode->i_blocks = bytes >> 9;
890 	inode->i_bytes = bytes & 511;
891 }
892 
893 EXPORT_SYMBOL(inode_set_bytes);
894