1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/blkdev.h>
9 #include <linux/export.h>
11 #include <linux/errno.h>
12 #include <linux/file.h>
13 #include <linux/highuid.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>
23 #include <linux/uaccess.h>
24 #include <asm/unistd.h>
30 * generic_fillattr - Fill in the basic attributes from the inode struct
31 * @idmap: idmap of the mount the inode was found from
32 * @inode: Inode to use as the source
33 * @stat: Where to fill in the attributes
35 * Fill in the basic attributes in the kstat structure from data that's to be
36 * found on the VFS inode structure. This is the default if no getattr inode
37 * operation is supplied.
39 * If the inode has been found through an idmapped mount the idmap of
40 * the vfsmount must be passed through @idmap. This function will then
41 * take care to map the inode according to @idmap before filling in the
42 * uid and gid filds. On non-idmapped mounts or if permission checking is to be
43 * performed on the raw inode simply passs @nop_mnt_idmap.
45 void generic_fillattr(struct mnt_idmap
*idmap
, struct inode
*inode
,
48 vfsuid_t vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
49 vfsgid_t vfsgid
= i_gid_into_vfsgid(idmap
, inode
);
51 stat
->dev
= inode
->i_sb
->s_dev
;
52 stat
->ino
= inode
->i_ino
;
53 stat
->mode
= inode
->i_mode
;
54 stat
->nlink
= inode
->i_nlink
;
55 stat
->uid
= vfsuid_into_kuid(vfsuid
);
56 stat
->gid
= vfsgid_into_kgid(vfsgid
);
57 stat
->rdev
= inode
->i_rdev
;
58 stat
->size
= i_size_read(inode
);
59 stat
->atime
= inode
->i_atime
;
60 stat
->mtime
= inode
->i_mtime
;
61 stat
->ctime
= inode
->i_ctime
;
62 stat
->blksize
= i_blocksize(inode
);
63 stat
->blocks
= inode
->i_blocks
;
65 EXPORT_SYMBOL(generic_fillattr
);
68 * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
69 * @inode: Inode to use as the source
70 * @stat: Where to fill in the attribute flags
72 * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
73 * inode that are published on i_flags and enforced by the VFS.
75 void generic_fill_statx_attr(struct inode
*inode
, struct kstat
*stat
)
77 if (inode
->i_flags
& S_IMMUTABLE
)
78 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
79 if (inode
->i_flags
& S_APPEND
)
80 stat
->attributes
|= STATX_ATTR_APPEND
;
81 stat
->attributes_mask
|= KSTAT_ATTR_VFS_FLAGS
;
83 EXPORT_SYMBOL(generic_fill_statx_attr
);
86 * vfs_getattr_nosec - getattr without security checks
87 * @path: file to get attributes from
88 * @stat: structure to return attributes in
89 * @request_mask: STATX_xxx flags indicating what the caller wants
90 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
92 * Get attributes without calling security_inode_getattr.
94 * Currently the only caller other than vfs_getattr is internal to the
95 * filehandle lookup code, which uses only the inode number and returns no
96 * attributes to any user. Any other code probably wants vfs_getattr.
98 int vfs_getattr_nosec(const struct path
*path
, struct kstat
*stat
,
99 u32 request_mask
, unsigned int query_flags
)
101 struct mnt_idmap
*idmap
;
102 struct inode
*inode
= d_backing_inode(path
->dentry
);
104 memset(stat
, 0, sizeof(*stat
));
105 stat
->result_mask
|= STATX_BASIC_STATS
;
106 query_flags
&= AT_STATX_SYNC_TYPE
;
108 /* allow the fs to override these if it really wants to */
109 /* SB_NOATIME means filesystem supplies dummy atime value */
110 if (inode
->i_sb
->s_flags
& SB_NOATIME
)
111 stat
->result_mask
&= ~STATX_ATIME
;
114 * Note: If you add another clause to set an attribute flag, please
115 * update attributes_mask below.
117 if (IS_AUTOMOUNT(inode
))
118 stat
->attributes
|= STATX_ATTR_AUTOMOUNT
;
121 stat
->attributes
|= STATX_ATTR_DAX
;
123 stat
->attributes_mask
|= (STATX_ATTR_AUTOMOUNT
|
126 if ((request_mask
& STATX_CHANGE_COOKIE
) && IS_I_VERSION(inode
)) {
127 stat
->result_mask
|= STATX_CHANGE_COOKIE
;
128 stat
->change_cookie
= inode_query_iversion(inode
);
131 idmap
= mnt_idmap(path
->mnt
);
132 if (inode
->i_op
->getattr
)
133 return inode
->i_op
->getattr(idmap
, path
, stat
,
134 request_mask
, query_flags
);
136 generic_fillattr(idmap
, inode
, stat
);
139 EXPORT_SYMBOL(vfs_getattr_nosec
);
142 * vfs_getattr - Get the enhanced basic attributes of a file
143 * @path: The file of interest
144 * @stat: Where to return the statistics
145 * @request_mask: STATX_xxx flags indicating what the caller wants
146 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
148 * Ask the filesystem for a file's attributes. The caller must indicate in
149 * request_mask and query_flags to indicate what they want.
151 * If the file is remote, the filesystem can be forced to update the attributes
152 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
153 * suppress the update by passing AT_STATX_DONT_SYNC.
155 * Bits must have been set in request_mask to indicate which attributes the
156 * caller wants retrieving. Any such attribute not requested may be returned
157 * anyway, but the value may be approximate, and, if remote, may not have been
158 * synchronised with the server.
160 * 0 will be returned on success, and a -ve error code if unsuccessful.
162 int vfs_getattr(const struct path
*path
, struct kstat
*stat
,
163 u32 request_mask
, unsigned int query_flags
)
167 retval
= security_inode_getattr(path
);
170 return vfs_getattr_nosec(path
, stat
, request_mask
, query_flags
);
172 EXPORT_SYMBOL(vfs_getattr
);
175 * vfs_fstat - Get the basic attributes by file descriptor
176 * @fd: The file descriptor referring to the file of interest
177 * @stat: The result structure to fill in.
179 * This function is a wrapper around vfs_getattr(). The main difference is
180 * that it uses a file descriptor to determine the file location.
182 * 0 will be returned on success, and a -ve error code if unsuccessful.
184 int vfs_fstat(int fd
, struct kstat
*stat
)
192 error
= vfs_getattr(&f
.file
->f_path
, stat
, STATX_BASIC_STATS
, 0);
197 int getname_statx_lookup_flags(int flags
)
199 int lookup_flags
= 0;
201 if (!(flags
& AT_SYMLINK_NOFOLLOW
))
202 lookup_flags
|= LOOKUP_FOLLOW
;
203 if (!(flags
& AT_NO_AUTOMOUNT
))
204 lookup_flags
|= LOOKUP_AUTOMOUNT
;
205 if (flags
& AT_EMPTY_PATH
)
206 lookup_flags
|= LOOKUP_EMPTY
;
212 * vfs_statx - Get basic and extra attributes by filename
213 * @dfd: A file descriptor representing the base dir for a relative filename
214 * @filename: The name of the file of interest
215 * @flags: Flags to control the query
216 * @stat: The result structure to fill in.
217 * @request_mask: STATX_xxx flags indicating what the caller wants
219 * This function is a wrapper around vfs_getattr(). The main difference is
220 * that it uses a filename and base directory to determine the file location.
221 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
222 * at the given name from being referenced.
224 * 0 will be returned on success, and a -ve error code if unsuccessful.
226 static int vfs_statx(int dfd
, struct filename
*filename
, int flags
,
227 struct kstat
*stat
, u32 request_mask
)
230 unsigned int lookup_flags
= getname_statx_lookup_flags(flags
);
233 if (flags
& ~(AT_SYMLINK_NOFOLLOW
| AT_NO_AUTOMOUNT
| AT_EMPTY_PATH
|
238 error
= filename_lookup(dfd
, filename
, lookup_flags
, &path
, NULL
);
242 error
= vfs_getattr(&path
, stat
, request_mask
, flags
);
244 stat
->mnt_id
= real_mount(path
.mnt
)->mnt_id
;
245 stat
->result_mask
|= STATX_MNT_ID
;
247 if (path
.mnt
->mnt_root
== path
.dentry
)
248 stat
->attributes
|= STATX_ATTR_MOUNT_ROOT
;
249 stat
->attributes_mask
|= STATX_ATTR_MOUNT_ROOT
;
251 /* Handle STATX_DIOALIGN for block devices. */
252 if (request_mask
& STATX_DIOALIGN
) {
253 struct inode
*inode
= d_backing_inode(path
.dentry
);
255 if (S_ISBLK(inode
->i_mode
))
256 bdev_statx_dioalign(inode
, stat
);
260 if (retry_estale(error
, lookup_flags
)) {
261 lookup_flags
|= LOOKUP_REVAL
;
268 int vfs_fstatat(int dfd
, const char __user
*filename
,
269 struct kstat
*stat
, int flags
)
272 int statx_flags
= flags
| AT_NO_AUTOMOUNT
;
273 struct filename
*name
;
275 name
= getname_flags(filename
, getname_statx_lookup_flags(statx_flags
), NULL
);
276 ret
= vfs_statx(dfd
, name
, statx_flags
, stat
, STATX_BASIC_STATS
);
282 #ifdef __ARCH_WANT_OLD_STAT
285 * For backward compatibility? Maybe this should be moved
286 * into arch/i386 instead?
288 static int cp_old_stat(struct kstat
*stat
, struct __old_kernel_stat __user
* statbuf
)
290 static int warncount
= 5;
291 struct __old_kernel_stat tmp
;
295 printk(KERN_WARNING
"VFS: Warning: %s using old stat() call. Recompile your binary.\n",
297 } else if (warncount
< 0) {
298 /* it's laughable, but... */
302 memset(&tmp
, 0, sizeof(struct __old_kernel_stat
));
303 tmp
.st_dev
= old_encode_dev(stat
->dev
);
304 tmp
.st_ino
= stat
->ino
;
305 if (sizeof(tmp
.st_ino
) < sizeof(stat
->ino
) && tmp
.st_ino
!= stat
->ino
)
307 tmp
.st_mode
= stat
->mode
;
308 tmp
.st_nlink
= stat
->nlink
;
309 if (tmp
.st_nlink
!= stat
->nlink
)
311 SET_UID(tmp
.st_uid
, from_kuid_munged(current_user_ns(), stat
->uid
));
312 SET_GID(tmp
.st_gid
, from_kgid_munged(current_user_ns(), stat
->gid
));
313 tmp
.st_rdev
= old_encode_dev(stat
->rdev
);
314 #if BITS_PER_LONG == 32
315 if (stat
->size
> MAX_NON_LFS
)
318 tmp
.st_size
= stat
->size
;
319 tmp
.st_atime
= stat
->atime
.tv_sec
;
320 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
321 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
322 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
325 SYSCALL_DEFINE2(stat
, const char __user
*, filename
,
326 struct __old_kernel_stat __user
*, statbuf
)
331 error
= vfs_stat(filename
, &stat
);
335 return cp_old_stat(&stat
, statbuf
);
338 SYSCALL_DEFINE2(lstat
, const char __user
*, filename
,
339 struct __old_kernel_stat __user
*, statbuf
)
344 error
= vfs_lstat(filename
, &stat
);
348 return cp_old_stat(&stat
, statbuf
);
351 SYSCALL_DEFINE2(fstat
, unsigned int, fd
, struct __old_kernel_stat __user
*, statbuf
)
354 int error
= vfs_fstat(fd
, &stat
);
357 error
= cp_old_stat(&stat
, statbuf
);
362 #endif /* __ARCH_WANT_OLD_STAT */
364 #ifdef __ARCH_WANT_NEW_STAT
366 #if BITS_PER_LONG == 32
367 # define choose_32_64(a,b) a
369 # define choose_32_64(a,b) b
372 #ifndef INIT_STRUCT_STAT_PADDING
373 # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
376 static int cp_new_stat(struct kstat
*stat
, struct stat __user
*statbuf
)
380 if (sizeof(tmp
.st_dev
) < 4 && !old_valid_dev(stat
->dev
))
382 if (sizeof(tmp
.st_rdev
) < 4 && !old_valid_dev(stat
->rdev
))
384 #if BITS_PER_LONG == 32
385 if (stat
->size
> MAX_NON_LFS
)
389 INIT_STRUCT_STAT_PADDING(tmp
);
390 tmp
.st_dev
= new_encode_dev(stat
->dev
);
391 tmp
.st_ino
= stat
->ino
;
392 if (sizeof(tmp
.st_ino
) < sizeof(stat
->ino
) && tmp
.st_ino
!= stat
->ino
)
394 tmp
.st_mode
= stat
->mode
;
395 tmp
.st_nlink
= stat
->nlink
;
396 if (tmp
.st_nlink
!= stat
->nlink
)
398 SET_UID(tmp
.st_uid
, from_kuid_munged(current_user_ns(), stat
->uid
));
399 SET_GID(tmp
.st_gid
, from_kgid_munged(current_user_ns(), stat
->gid
));
400 tmp
.st_rdev
= new_encode_dev(stat
->rdev
);
401 tmp
.st_size
= stat
->size
;
402 tmp
.st_atime
= stat
->atime
.tv_sec
;
403 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
404 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
405 #ifdef STAT_HAVE_NSEC
406 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
407 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
408 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
410 tmp
.st_blocks
= stat
->blocks
;
411 tmp
.st_blksize
= stat
->blksize
;
412 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
415 SYSCALL_DEFINE2(newstat
, const char __user
*, filename
,
416 struct stat __user
*, statbuf
)
419 int error
= vfs_stat(filename
, &stat
);
423 return cp_new_stat(&stat
, statbuf
);
426 SYSCALL_DEFINE2(newlstat
, const char __user
*, filename
,
427 struct stat __user
*, statbuf
)
432 error
= vfs_lstat(filename
, &stat
);
436 return cp_new_stat(&stat
, statbuf
);
439 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
440 SYSCALL_DEFINE4(newfstatat
, int, dfd
, const char __user
*, filename
,
441 struct stat __user
*, statbuf
, int, flag
)
446 error
= vfs_fstatat(dfd
, filename
, &stat
, flag
);
449 return cp_new_stat(&stat
, statbuf
);
453 SYSCALL_DEFINE2(newfstat
, unsigned int, fd
, struct stat __user
*, statbuf
)
456 int error
= vfs_fstat(fd
, &stat
);
459 error
= cp_new_stat(&stat
, statbuf
);
465 static int do_readlinkat(int dfd
, const char __user
*pathname
,
466 char __user
*buf
, int bufsiz
)
471 unsigned int lookup_flags
= LOOKUP_EMPTY
;
477 error
= user_path_at_empty(dfd
, pathname
, lookup_flags
, &path
, &empty
);
479 struct inode
*inode
= d_backing_inode(path
.dentry
);
481 error
= empty
? -ENOENT
: -EINVAL
;
483 * AFS mountpoints allow readlink(2) but are not symlinks
485 if (d_is_symlink(path
.dentry
) || inode
->i_op
->readlink
) {
486 error
= security_inode_readlink(path
.dentry
);
489 error
= vfs_readlink(path
.dentry
, buf
, bufsiz
);
493 if (retry_estale(error
, lookup_flags
)) {
494 lookup_flags
|= LOOKUP_REVAL
;
501 SYSCALL_DEFINE4(readlinkat
, int, dfd
, const char __user
*, pathname
,
502 char __user
*, buf
, int, bufsiz
)
504 return do_readlinkat(dfd
, pathname
, buf
, bufsiz
);
507 SYSCALL_DEFINE3(readlink
, const char __user
*, path
, char __user
*, buf
,
510 return do_readlinkat(AT_FDCWD
, path
, buf
, bufsiz
);
514 /* ---------- LFS-64 ----------- */
515 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
517 #ifndef INIT_STRUCT_STAT64_PADDING
518 # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
521 static long cp_new_stat64(struct kstat
*stat
, struct stat64 __user
*statbuf
)
525 INIT_STRUCT_STAT64_PADDING(tmp
);
527 /* mips has weird padding, so we don't get 64 bits there */
528 tmp
.st_dev
= new_encode_dev(stat
->dev
);
529 tmp
.st_rdev
= new_encode_dev(stat
->rdev
);
531 tmp
.st_dev
= huge_encode_dev(stat
->dev
);
532 tmp
.st_rdev
= huge_encode_dev(stat
->rdev
);
534 tmp
.st_ino
= stat
->ino
;
535 if (sizeof(tmp
.st_ino
) < sizeof(stat
->ino
) && tmp
.st_ino
!= stat
->ino
)
537 #ifdef STAT64_HAS_BROKEN_ST_INO
538 tmp
.__st_ino
= stat
->ino
;
540 tmp
.st_mode
= stat
->mode
;
541 tmp
.st_nlink
= stat
->nlink
;
542 tmp
.st_uid
= from_kuid_munged(current_user_ns(), stat
->uid
);
543 tmp
.st_gid
= from_kgid_munged(current_user_ns(), stat
->gid
);
544 tmp
.st_atime
= stat
->atime
.tv_sec
;
545 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
546 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
547 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
548 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
549 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
550 tmp
.st_size
= stat
->size
;
551 tmp
.st_blocks
= stat
->blocks
;
552 tmp
.st_blksize
= stat
->blksize
;
553 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
556 SYSCALL_DEFINE2(stat64
, const char __user
*, filename
,
557 struct stat64 __user
*, statbuf
)
560 int error
= vfs_stat(filename
, &stat
);
563 error
= cp_new_stat64(&stat
, statbuf
);
568 SYSCALL_DEFINE2(lstat64
, const char __user
*, filename
,
569 struct stat64 __user
*, statbuf
)
572 int error
= vfs_lstat(filename
, &stat
);
575 error
= cp_new_stat64(&stat
, statbuf
);
580 SYSCALL_DEFINE2(fstat64
, unsigned long, fd
, struct stat64 __user
*, statbuf
)
583 int error
= vfs_fstat(fd
, &stat
);
586 error
= cp_new_stat64(&stat
, statbuf
);
591 SYSCALL_DEFINE4(fstatat64
, int, dfd
, const char __user
*, filename
,
592 struct stat64 __user
*, statbuf
, int, flag
)
597 error
= vfs_fstatat(dfd
, filename
, &stat
, flag
);
600 return cp_new_stat64(&stat
, statbuf
);
602 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
604 static noinline_for_stack
int
605 cp_statx(const struct kstat
*stat
, struct statx __user
*buffer
)
609 memset(&tmp
, 0, sizeof(tmp
));
611 /* STATX_CHANGE_COOKIE is kernel-only for now */
612 tmp
.stx_mask
= stat
->result_mask
& ~STATX_CHANGE_COOKIE
;
613 tmp
.stx_blksize
= stat
->blksize
;
614 /* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */
615 tmp
.stx_attributes
= stat
->attributes
& ~STATX_ATTR_CHANGE_MONOTONIC
;
616 tmp
.stx_nlink
= stat
->nlink
;
617 tmp
.stx_uid
= from_kuid_munged(current_user_ns(), stat
->uid
);
618 tmp
.stx_gid
= from_kgid_munged(current_user_ns(), stat
->gid
);
619 tmp
.stx_mode
= stat
->mode
;
620 tmp
.stx_ino
= stat
->ino
;
621 tmp
.stx_size
= stat
->size
;
622 tmp
.stx_blocks
= stat
->blocks
;
623 tmp
.stx_attributes_mask
= stat
->attributes_mask
;
624 tmp
.stx_atime
.tv_sec
= stat
->atime
.tv_sec
;
625 tmp
.stx_atime
.tv_nsec
= stat
->atime
.tv_nsec
;
626 tmp
.stx_btime
.tv_sec
= stat
->btime
.tv_sec
;
627 tmp
.stx_btime
.tv_nsec
= stat
->btime
.tv_nsec
;
628 tmp
.stx_ctime
.tv_sec
= stat
->ctime
.tv_sec
;
629 tmp
.stx_ctime
.tv_nsec
= stat
->ctime
.tv_nsec
;
630 tmp
.stx_mtime
.tv_sec
= stat
->mtime
.tv_sec
;
631 tmp
.stx_mtime
.tv_nsec
= stat
->mtime
.tv_nsec
;
632 tmp
.stx_rdev_major
= MAJOR(stat
->rdev
);
633 tmp
.stx_rdev_minor
= MINOR(stat
->rdev
);
634 tmp
.stx_dev_major
= MAJOR(stat
->dev
);
635 tmp
.stx_dev_minor
= MINOR(stat
->dev
);
636 tmp
.stx_mnt_id
= stat
->mnt_id
;
637 tmp
.stx_dio_mem_align
= stat
->dio_mem_align
;
638 tmp
.stx_dio_offset_align
= stat
->dio_offset_align
;
640 return copy_to_user(buffer
, &tmp
, sizeof(tmp
)) ? -EFAULT
: 0;
643 int do_statx(int dfd
, struct filename
*filename
, unsigned int flags
,
644 unsigned int mask
, struct statx __user
*buffer
)
649 if (mask
& STATX__RESERVED
)
651 if ((flags
& AT_STATX_SYNC_TYPE
) == AT_STATX_SYNC_TYPE
)
654 /* STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
657 mask
&= ~STATX_CHANGE_COOKIE
;
659 error
= vfs_statx(dfd
, filename
, flags
, &stat
, mask
);
663 return cp_statx(&stat
, buffer
);
667 * sys_statx - System call to get enhanced stats
668 * @dfd: Base directory to pathwalk from *or* fd to stat.
669 * @filename: File to stat or "" with AT_EMPTY_PATH
670 * @flags: AT_* flags to control pathwalk.
671 * @mask: Parts of statx struct actually required.
672 * @buffer: Result buffer.
674 * Note that fstat() can be emulated by setting dfd to the fd of interest,
675 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
677 SYSCALL_DEFINE5(statx
,
678 int, dfd
, const char __user
*, filename
, unsigned, flags
,
680 struct statx __user
*, buffer
)
683 struct filename
*name
;
685 name
= getname_flags(filename
, getname_statx_lookup_flags(flags
), NULL
);
686 ret
= do_statx(dfd
, name
, flags
, mask
, buffer
);
692 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
693 static int cp_compat_stat(struct kstat
*stat
, struct compat_stat __user
*ubuf
)
695 struct compat_stat tmp
;
697 if (sizeof(tmp
.st_dev
) < 4 && !old_valid_dev(stat
->dev
))
699 if (sizeof(tmp
.st_rdev
) < 4 && !old_valid_dev(stat
->rdev
))
702 memset(&tmp
, 0, sizeof(tmp
));
703 tmp
.st_dev
= new_encode_dev(stat
->dev
);
704 tmp
.st_ino
= stat
->ino
;
705 if (sizeof(tmp
.st_ino
) < sizeof(stat
->ino
) && tmp
.st_ino
!= stat
->ino
)
707 tmp
.st_mode
= stat
->mode
;
708 tmp
.st_nlink
= stat
->nlink
;
709 if (tmp
.st_nlink
!= stat
->nlink
)
711 SET_UID(tmp
.st_uid
, from_kuid_munged(current_user_ns(), stat
->uid
));
712 SET_GID(tmp
.st_gid
, from_kgid_munged(current_user_ns(), stat
->gid
));
713 tmp
.st_rdev
= new_encode_dev(stat
->rdev
);
714 if ((u64
) stat
->size
> MAX_NON_LFS
)
716 tmp
.st_size
= stat
->size
;
717 tmp
.st_atime
= stat
->atime
.tv_sec
;
718 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
719 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
720 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
721 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
722 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
723 tmp
.st_blocks
= stat
->blocks
;
724 tmp
.st_blksize
= stat
->blksize
;
725 return copy_to_user(ubuf
, &tmp
, sizeof(tmp
)) ? -EFAULT
: 0;
728 COMPAT_SYSCALL_DEFINE2(newstat
, const char __user
*, filename
,
729 struct compat_stat __user
*, statbuf
)
734 error
= vfs_stat(filename
, &stat
);
737 return cp_compat_stat(&stat
, statbuf
);
740 COMPAT_SYSCALL_DEFINE2(newlstat
, const char __user
*, filename
,
741 struct compat_stat __user
*, statbuf
)
746 error
= vfs_lstat(filename
, &stat
);
749 return cp_compat_stat(&stat
, statbuf
);
752 #ifndef __ARCH_WANT_STAT64
753 COMPAT_SYSCALL_DEFINE4(newfstatat
, unsigned int, dfd
,
754 const char __user
*, filename
,
755 struct compat_stat __user
*, statbuf
, int, flag
)
760 error
= vfs_fstatat(dfd
, filename
, &stat
, flag
);
763 return cp_compat_stat(&stat
, statbuf
);
767 COMPAT_SYSCALL_DEFINE2(newfstat
, unsigned int, fd
,
768 struct compat_stat __user
*, statbuf
)
771 int error
= vfs_fstat(fd
, &stat
);
774 error
= cp_compat_stat(&stat
, statbuf
);
779 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
780 void __inode_add_bytes(struct inode
*inode
, loff_t bytes
)
782 inode
->i_blocks
+= bytes
>> 9;
784 inode
->i_bytes
+= bytes
;
785 if (inode
->i_bytes
>= 512) {
787 inode
->i_bytes
-= 512;
790 EXPORT_SYMBOL(__inode_add_bytes
);
792 void inode_add_bytes(struct inode
*inode
, loff_t bytes
)
794 spin_lock(&inode
->i_lock
);
795 __inode_add_bytes(inode
, bytes
);
796 spin_unlock(&inode
->i_lock
);
799 EXPORT_SYMBOL(inode_add_bytes
);
801 void __inode_sub_bytes(struct inode
*inode
, loff_t bytes
)
803 inode
->i_blocks
-= bytes
>> 9;
805 if (inode
->i_bytes
< bytes
) {
807 inode
->i_bytes
+= 512;
809 inode
->i_bytes
-= bytes
;
812 EXPORT_SYMBOL(__inode_sub_bytes
);
814 void inode_sub_bytes(struct inode
*inode
, loff_t bytes
)
816 spin_lock(&inode
->i_lock
);
817 __inode_sub_bytes(inode
, bytes
);
818 spin_unlock(&inode
->i_lock
);
821 EXPORT_SYMBOL(inode_sub_bytes
);
823 loff_t
inode_get_bytes(struct inode
*inode
)
827 spin_lock(&inode
->i_lock
);
828 ret
= __inode_get_bytes(inode
);
829 spin_unlock(&inode
->i_lock
);
833 EXPORT_SYMBOL(inode_get_bytes
);
835 void inode_set_bytes(struct inode
*inode
, loff_t bytes
)
837 /* Caller is here responsible for sufficient locking
838 * (ie. inode->i_lock) */
839 inode
->i_blocks
= bytes
>> 9;
840 inode
->i_bytes
= bytes
& 511;
843 EXPORT_SYMBOL(inode_set_bytes
);