xref: /openbmc/linux/fs/stat.c (revision 581701b7efd60ba13d8a7eed60cbdd7fefaf6696)
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/export.h>
9 #include <linux/mm.h>
10 #include <linux/errno.h>
11 #include <linux/file.h>
12 #include <linux/highuid.h>
13 #include <linux/fs.h>
14 #include <linux/namei.h>
15 #include <linux/security.h>
16 #include <linux/cred.h>
17 #include <linux/syscalls.h>
18 #include <linux/pagemap.h>
19 #include <linux/compat.h>
20 
21 #include <linux/uaccess.h>
22 #include <asm/unistd.h>
23 
24 #include "internal.h"
25 
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  */
35 void 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;
44 	stat->size = i_size_read(inode);
45 	stat->atime = inode->i_atime;
46 	stat->mtime = inode->i_mtime;
47 	stat->ctime = inode->i_ctime;
48 	stat->blksize = i_blocksize(inode);
49 	stat->blocks = inode->i_blocks;
50 }
51 EXPORT_SYMBOL(generic_fillattr);
52 
53 /**
54  * vfs_getattr_nosec - getattr without security checks
55  * @path: file to get attributes from
56  * @stat: structure to return attributes in
57  * @request_mask: STATX_xxx flags indicating what the caller wants
58  * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
59  *
60  * Get attributes without calling security_inode_getattr.
61  *
62  * Currently the only caller other than vfs_getattr is internal to the
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.
65  */
66 int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
67 		      u32 request_mask, unsigned int query_flags)
68 {
69 	struct inode *inode = d_backing_inode(path->dentry);
70 
71 	memset(stat, 0, sizeof(*stat));
72 	stat->result_mask |= STATX_BASIC_STATS;
73 	query_flags &= KSTAT_QUERY_FLAGS;
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 
81 	if (inode->i_op->getattr)
82 		return inode->i_op->getattr(path, stat, request_mask,
83 					    query_flags);
84 
85 	generic_fillattr(inode, stat);
86 	return 0;
87 }
88 EXPORT_SYMBOL(vfs_getattr_nosec);
89 
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  */
111 int vfs_getattr(const struct path *path, struct kstat *stat,
112 		u32 request_mask, unsigned int query_flags)
113 {
114 	int retval;
115 
116 	retval = security_inode_getattr(path);
117 	if (retval)
118 		return retval;
119 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
120 }
121 EXPORT_SYMBOL(vfs_getattr);
122 
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  */
135 int vfs_statx_fd(unsigned int fd, struct kstat *stat,
136 		 u32 request_mask, unsigned int query_flags)
137 {
138 	struct fd f;
139 	int error = -EBADF;
140 
141 	if (query_flags & ~KSTAT_QUERY_FLAGS)
142 		return -EINVAL;
143 
144 	f = fdget_raw(fd);
145 	if (f.file) {
146 		error = vfs_getattr(&f.file->f_path, stat,
147 				    request_mask, query_flags);
148 		fdput(f);
149 	}
150 	return error;
151 }
152 EXPORT_SYMBOL(vfs_statx_fd);
153 
154 inline 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 
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  *
184  * 0 will be returned on success, and a -ve error code if unsuccessful.
185  */
186 int vfs_statx(int dfd, const char __user *filename, int flags,
187 	      struct kstat *stat, u32 request_mask)
188 {
189 	struct path path;
190 	int error = -EINVAL;
191 	unsigned lookup_flags;
192 
193 	if (vfs_stat_set_lookup_flags(&lookup_flags, flags))
194 		return -EINVAL;
195 retry:
196 	error = user_path_at(dfd, filename, lookup_flags, &path);
197 	if (error)
198 		goto out;
199 
200 	error = vfs_getattr(&path, stat, request_mask, flags);
201 	path_put(&path);
202 	if (retry_estale(error, lookup_flags)) {
203 		lookup_flags |= LOOKUP_REVAL;
204 		goto retry;
205 	}
206 out:
207 	return error;
208 }
209 EXPORT_SYMBOL(vfs_statx);
210 
211 
212 #ifdef __ARCH_WANT_OLD_STAT
213 
214 /*
215  * For backward compatibility?  Maybe this should be moved
216  * into arch/i386 instead?
217  */
218 static 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;
222 
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;
235 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
236 		return -EOVERFLOW;
237 	tmp.st_mode = stat->mode;
238 	tmp.st_nlink = stat->nlink;
239 	if (tmp.st_nlink != stat->nlink)
240 		return -EOVERFLOW;
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));
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;
247 #endif
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 
255 SYSCALL_DEFINE2(stat, const char __user *, filename,
256 		struct __old_kernel_stat __user *, statbuf)
257 {
258 	struct kstat stat;
259 	int error;
260 
261 	error = vfs_stat(filename, &stat);
262 	if (error)
263 		return error;
264 
265 	return cp_old_stat(&stat, statbuf);
266 }
267 
268 SYSCALL_DEFINE2(lstat, const char __user *, filename,
269 		struct __old_kernel_stat __user *, statbuf)
270 {
271 	struct kstat stat;
272 	int error;
273 
274 	error = vfs_lstat(filename, &stat);
275 	if (error)
276 		return error;
277 
278 	return cp_old_stat(&stat, statbuf);
279 }
280 
281 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
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 
294 #ifdef __ARCH_WANT_NEW_STAT
295 
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 
302 #define valid_dev(x)  choose_32_64(old_valid_dev(x),true)
303 #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
304 
305 #ifndef INIT_STRUCT_STAT_PADDING
306 #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
307 #endif
308 
309 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
310 {
311 	struct stat tmp;
312 
313 	if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
314 		return -EOVERFLOW;
315 #if BITS_PER_LONG == 32
316 	if (stat->size > MAX_NON_LFS)
317 		return -EOVERFLOW;
318 #endif
319 
320 	INIT_STRUCT_STAT_PADDING(tmp);
321 	tmp.st_dev = encode_dev(stat->dev);
322 	tmp.st_ino = stat->ino;
323 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
324 		return -EOVERFLOW;
325 	tmp.st_mode = stat->mode;
326 	tmp.st_nlink = stat->nlink;
327 	if (tmp.st_nlink != stat->nlink)
328 		return -EOVERFLOW;
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));
331 	tmp.st_rdev = encode_dev(stat->rdev);
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 
346 SYSCALL_DEFINE2(newstat, const char __user *, filename,
347 		struct stat __user *, statbuf)
348 {
349 	struct kstat stat;
350 	int error = vfs_stat(filename, &stat);
351 
352 	if (error)
353 		return error;
354 	return cp_new_stat(&stat, statbuf);
355 }
356 
357 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
358 		struct stat __user *, statbuf)
359 {
360 	struct kstat stat;
361 	int error;
362 
363 	error = vfs_lstat(filename, &stat);
364 	if (error)
365 		return error;
366 
367 	return cp_new_stat(&stat, statbuf);
368 }
369 
370 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
371 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
372 		struct stat __user *, statbuf, int, flag)
373 {
374 	struct kstat stat;
375 	int error;
376 
377 	error = vfs_fstatat(dfd, filename, &stat, flag);
378 	if (error)
379 		return error;
380 	return cp_new_stat(&stat, statbuf);
381 }
382 #endif
383 
384 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
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 }
394 #endif
395 
396 static int do_readlinkat(int dfd, const char __user *pathname,
397 			 char __user *buf, int bufsiz)
398 {
399 	struct path path;
400 	int error;
401 	int empty = 0;
402 	unsigned int lookup_flags = LOOKUP_EMPTY;
403 
404 	if (bufsiz <= 0)
405 		return -EINVAL;
406 
407 retry:
408 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
409 	if (!error) {
410 		struct inode *inode = d_backing_inode(path.dentry);
411 
412 		error = empty ? -ENOENT : -EINVAL;
413 		/*
414 		 * AFS mountpoints allow readlink(2) but are not symlinks
415 		 */
416 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
417 			error = security_inode_readlink(path.dentry);
418 			if (!error) {
419 				touch_atime(&path);
420 				error = vfs_readlink(path.dentry, buf, bufsiz);
421 			}
422 		}
423 		path_put(&path);
424 		if (retry_estale(error, lookup_flags)) {
425 			lookup_flags |= LOOKUP_REVAL;
426 			goto retry;
427 		}
428 	}
429 	return error;
430 }
431 
432 SYSCALL_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 
438 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
439 		int, bufsiz)
440 {
441 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
442 }
443 
444 
445 /* ---------- LFS-64 ----------- */
446 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
447 
448 #ifndef INIT_STRUCT_STAT64_PADDING
449 #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
450 #endif
451 
452 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
453 {
454 	struct stat64 tmp;
455 
456 	INIT_STRUCT_STAT64_PADDING(tmp);
457 #ifdef CONFIG_MIPS
458 	/* mips has weird padding, so we don't get 64 bits there */
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;
466 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
467 		return -EOVERFLOW;
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;
473 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
474 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
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 
487 SYSCALL_DEFINE2(stat64, const char __user *, filename,
488 		struct stat64 __user *, statbuf)
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 }
498 
499 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
500 		struct stat64 __user *, statbuf)
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 }
510 
511 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
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 
522 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
523 		struct stat64 __user *, statbuf, int, flag)
524 {
525 	struct kstat stat;
526 	int error;
527 
528 	error = vfs_fstatat(dfd, filename, &stat, flag);
529 	if (error)
530 		return error;
531 	return cp_new_stat64(&stat, statbuf);
532 }
533 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
534 
535 noinline_for_stack int
536 cp_statx(const struct kstat *stat, struct statx __user *buffer)
537 {
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;
552 	tmp.stx_attributes_mask = stat->attributes_mask;
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;
567 }
568 
569 /**
570  * sys_statx - System call to get enhanced stats
571  * @dfd: Base directory to pathwalk from *or* fd to stat.
572  * @filename: File to stat or "" with AT_EMPTY_PATH
573  * @flags: AT_* flags to control pathwalk.
574  * @mask: Parts of statx struct actually required.
575  * @buffer: Result buffer.
576  *
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.
579  */
580 SYSCALL_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 
588 	if (mask & STATX__RESERVED)
589 		return -EINVAL;
590 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
591 		return -EINVAL;
592 
593 	error = vfs_statx(dfd, filename, flags, &stat, mask);
594 	if (error)
595 		return error;
596 
597 	return cp_statx(&stat, buffer);
598 }
599 
600 #ifdef CONFIG_COMPAT
601 static 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 
634 COMPAT_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 
646 COMPAT_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
659 COMPAT_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 
673 COMPAT_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 
685 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
686 void __inode_add_bytes(struct inode *inode, loff_t bytes)
687 {
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 	}
695 }
696 EXPORT_SYMBOL(__inode_add_bytes);
697 
698 void inode_add_bytes(struct inode *inode, loff_t bytes)
699 {
700 	spin_lock(&inode->i_lock);
701 	__inode_add_bytes(inode, bytes);
702 	spin_unlock(&inode->i_lock);
703 }
704 
705 EXPORT_SYMBOL(inode_add_bytes);
706 
707 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
708 {
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;
716 }
717 
718 EXPORT_SYMBOL(__inode_sub_bytes);
719 
720 void inode_sub_bytes(struct inode *inode, loff_t bytes)
721 {
722 	spin_lock(&inode->i_lock);
723 	__inode_sub_bytes(inode, bytes);
724 	spin_unlock(&inode->i_lock);
725 }
726 
727 EXPORT_SYMBOL(inode_sub_bytes);
728 
729 loff_t inode_get_bytes(struct inode *inode)
730 {
731 	loff_t ret;
732 
733 	spin_lock(&inode->i_lock);
734 	ret = __inode_get_bytes(inode);
735 	spin_unlock(&inode->i_lock);
736 	return ret;
737 }
738 
739 EXPORT_SYMBOL(inode_get_bytes);
740 
741 void 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 
749 EXPORT_SYMBOL(inode_set_bytes);
750