4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/export.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
11 #include <linux/highuid.h>
13 #include <linux/namei.h>
14 #include <linux/security.h>
15 #include <linux/cred.h>
16 #include <linux/syscalls.h>
17 #include <linux/pagemap.h>
19 #include <linux/uaccess.h>
20 #include <asm/unistd.h>
23 * generic_fillattr - Fill in the basic attributes from the inode struct
24 * @inode: Inode to use as the source
25 * @stat: Where to fill in the attributes
27 * Fill in the basic attributes in the kstat structure from data that's to be
28 * found on the VFS inode structure. This is the default if no getattr inode
29 * operation is supplied.
31 void generic_fillattr(struct inode
*inode
, struct kstat
*stat
)
33 stat
->dev
= inode
->i_sb
->s_dev
;
34 stat
->ino
= inode
->i_ino
;
35 stat
->mode
= inode
->i_mode
;
36 stat
->nlink
= inode
->i_nlink
;
37 stat
->uid
= inode
->i_uid
;
38 stat
->gid
= inode
->i_gid
;
39 stat
->rdev
= inode
->i_rdev
;
40 stat
->size
= i_size_read(inode
);
41 stat
->atime
= inode
->i_atime
;
42 stat
->mtime
= inode
->i_mtime
;
43 stat
->ctime
= inode
->i_ctime
;
44 stat
->blksize
= i_blocksize(inode
);
45 stat
->blocks
= inode
->i_blocks
;
47 if (IS_NOATIME(inode
))
48 stat
->result_mask
&= ~STATX_ATIME
;
49 if (IS_AUTOMOUNT(inode
))
50 stat
->attributes
|= STATX_ATTR_AUTOMOUNT
;
52 EXPORT_SYMBOL(generic_fillattr
);
55 * vfs_getattr_nosec - getattr without security checks
56 * @path: file to get attributes from
57 * @stat: structure to return attributes in
58 * @request_mask: STATX_xxx flags indicating what the caller wants
59 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
61 * Get attributes without calling security_inode_getattr.
63 * Currently the only caller other than vfs_getattr is internal to the
64 * filehandle lookup code, which uses only the inode number and returns no
65 * attributes to any user. Any other code probably wants vfs_getattr.
67 int vfs_getattr_nosec(const struct path
*path
, struct kstat
*stat
,
68 u32 request_mask
, unsigned int query_flags
)
70 struct inode
*inode
= d_backing_inode(path
->dentry
);
72 memset(stat
, 0, sizeof(*stat
));
73 stat
->result_mask
|= STATX_BASIC_STATS
;
74 request_mask
&= STATX_ALL
;
75 query_flags
&= KSTAT_QUERY_FLAGS
;
76 if (inode
->i_op
->getattr
)
77 return inode
->i_op
->getattr(path
, stat
, request_mask
,
80 generic_fillattr(inode
, stat
);
83 EXPORT_SYMBOL(vfs_getattr_nosec
);
86 * vfs_getattr - Get the enhanced basic attributes of a file
87 * @path: The file of interest
88 * @stat: Where to return the statistics
89 * @request_mask: STATX_xxx flags indicating what the caller wants
90 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
92 * Ask the filesystem for a file's attributes. The caller must indicate in
93 * request_mask and query_flags to indicate what they want.
95 * If the file is remote, the filesystem can be forced to update the attributes
96 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
97 * suppress the update by passing AT_STATX_DONT_SYNC.
99 * Bits must have been set in request_mask to indicate which attributes the
100 * caller wants retrieving. Any such attribute not requested may be returned
101 * anyway, but the value may be approximate, and, if remote, may not have been
102 * synchronised with the server.
104 * 0 will be returned on success, and a -ve error code if unsuccessful.
106 int vfs_getattr(const struct path
*path
, struct kstat
*stat
,
107 u32 request_mask
, unsigned int query_flags
)
111 retval
= security_inode_getattr(path
);
114 return vfs_getattr_nosec(path
, stat
, request_mask
, query_flags
);
116 EXPORT_SYMBOL(vfs_getattr
);
119 * vfs_statx_fd - Get the enhanced basic attributes by file descriptor
120 * @fd: The file descriptor referring to the file of interest
121 * @stat: The result structure to fill in.
122 * @request_mask: STATX_xxx flags indicating what the caller wants
123 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
125 * This function is a wrapper around vfs_getattr(). The main difference is
126 * that it uses a file descriptor to determine the file location.
128 * 0 will be returned on success, and a -ve error code if unsuccessful.
130 int vfs_statx_fd(unsigned int fd
, struct kstat
*stat
,
131 u32 request_mask
, unsigned int query_flags
)
133 struct fd f
= fdget_raw(fd
);
137 error
= vfs_getattr(&f
.file
->f_path
, stat
,
138 request_mask
, query_flags
);
143 EXPORT_SYMBOL(vfs_statx_fd
);
146 * vfs_statx - Get basic and extra attributes by filename
147 * @dfd: A file descriptor representing the base dir for a relative filename
148 * @filename: The name of the file of interest
149 * @flags: Flags to control the query
150 * @stat: The result structure to fill in.
151 * @request_mask: STATX_xxx flags indicating what the caller wants
153 * This function is a wrapper around vfs_getattr(). The main difference is
154 * that it uses a filename and base directory to determine the file location.
155 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
156 * at the given name from being referenced.
158 * The caller must have preset stat->request_mask as for vfs_getattr(). The
159 * flags are also used to load up stat->query_flags.
161 * 0 will be returned on success, and a -ve error code if unsuccessful.
163 int vfs_statx(int dfd
, const char __user
*filename
, int flags
,
164 struct kstat
*stat
, u32 request_mask
)
168 unsigned int lookup_flags
= LOOKUP_FOLLOW
| LOOKUP_AUTOMOUNT
;
170 if ((flags
& ~(AT_SYMLINK_NOFOLLOW
| AT_NO_AUTOMOUNT
|
171 AT_EMPTY_PATH
| KSTAT_QUERY_FLAGS
)) != 0)
174 if (flags
& AT_SYMLINK_NOFOLLOW
)
175 lookup_flags
&= ~LOOKUP_FOLLOW
;
176 if (flags
& AT_NO_AUTOMOUNT
)
177 lookup_flags
&= ~LOOKUP_AUTOMOUNT
;
178 if (flags
& AT_EMPTY_PATH
)
179 lookup_flags
|= LOOKUP_EMPTY
;
182 error
= user_path_at(dfd
, filename
, lookup_flags
, &path
);
186 error
= vfs_getattr(&path
, stat
, request_mask
, flags
);
188 if (retry_estale(error
, lookup_flags
)) {
189 lookup_flags
|= LOOKUP_REVAL
;
195 EXPORT_SYMBOL(vfs_statx
);
198 #ifdef __ARCH_WANT_OLD_STAT
201 * For backward compatibility? Maybe this should be moved
202 * into arch/i386 instead?
204 static int cp_old_stat(struct kstat
*stat
, struct __old_kernel_stat __user
* statbuf
)
206 static int warncount
= 5;
207 struct __old_kernel_stat tmp
;
211 printk(KERN_WARNING
"VFS: Warning: %s using old stat() call. Recompile your binary.\n",
213 } else if (warncount
< 0) {
214 /* it's laughable, but... */
218 memset(&tmp
, 0, sizeof(struct __old_kernel_stat
));
219 tmp
.st_dev
= old_encode_dev(stat
->dev
);
220 tmp
.st_ino
= stat
->ino
;
221 if (sizeof(tmp
.st_ino
) < sizeof(stat
->ino
) && tmp
.st_ino
!= stat
->ino
)
223 tmp
.st_mode
= stat
->mode
;
224 tmp
.st_nlink
= stat
->nlink
;
225 if (tmp
.st_nlink
!= stat
->nlink
)
227 SET_UID(tmp
.st_uid
, from_kuid_munged(current_user_ns(), stat
->uid
));
228 SET_GID(tmp
.st_gid
, from_kgid_munged(current_user_ns(), stat
->gid
));
229 tmp
.st_rdev
= old_encode_dev(stat
->rdev
);
230 #if BITS_PER_LONG == 32
231 if (stat
->size
> MAX_NON_LFS
)
234 tmp
.st_size
= stat
->size
;
235 tmp
.st_atime
= stat
->atime
.tv_sec
;
236 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
237 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
238 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
241 SYSCALL_DEFINE2(stat
, const char __user
*, filename
,
242 struct __old_kernel_stat __user
*, statbuf
)
247 error
= vfs_stat(filename
, &stat
);
251 return cp_old_stat(&stat
, statbuf
);
254 SYSCALL_DEFINE2(lstat
, const char __user
*, filename
,
255 struct __old_kernel_stat __user
*, statbuf
)
260 error
= vfs_lstat(filename
, &stat
);
264 return cp_old_stat(&stat
, statbuf
);
267 SYSCALL_DEFINE2(fstat
, unsigned int, fd
, struct __old_kernel_stat __user
*, statbuf
)
270 int error
= vfs_fstat(fd
, &stat
);
273 error
= cp_old_stat(&stat
, statbuf
);
278 #endif /* __ARCH_WANT_OLD_STAT */
280 #if BITS_PER_LONG == 32
281 # define choose_32_64(a,b) a
283 # define choose_32_64(a,b) b
286 #define valid_dev(x) choose_32_64(old_valid_dev(x),true)
287 #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
289 #ifndef INIT_STRUCT_STAT_PADDING
290 # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
293 static int cp_new_stat(struct kstat
*stat
, struct stat __user
*statbuf
)
297 if (!valid_dev(stat
->dev
) || !valid_dev(stat
->rdev
))
299 #if BITS_PER_LONG == 32
300 if (stat
->size
> MAX_NON_LFS
)
304 INIT_STRUCT_STAT_PADDING(tmp
);
305 tmp
.st_dev
= encode_dev(stat
->dev
);
306 tmp
.st_ino
= stat
->ino
;
307 if (sizeof(tmp
.st_ino
) < sizeof(stat
->ino
) && tmp
.st_ino
!= stat
->ino
)
309 tmp
.st_mode
= stat
->mode
;
310 tmp
.st_nlink
= stat
->nlink
;
311 if (tmp
.st_nlink
!= stat
->nlink
)
313 SET_UID(tmp
.st_uid
, from_kuid_munged(current_user_ns(), stat
->uid
));
314 SET_GID(tmp
.st_gid
, from_kgid_munged(current_user_ns(), stat
->gid
));
315 tmp
.st_rdev
= encode_dev(stat
->rdev
);
316 tmp
.st_size
= stat
->size
;
317 tmp
.st_atime
= stat
->atime
.tv_sec
;
318 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
319 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
320 #ifdef STAT_HAVE_NSEC
321 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
322 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
323 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
325 tmp
.st_blocks
= stat
->blocks
;
326 tmp
.st_blksize
= stat
->blksize
;
327 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
330 SYSCALL_DEFINE2(newstat
, const char __user
*, filename
,
331 struct stat __user
*, statbuf
)
334 int error
= vfs_stat(filename
, &stat
);
338 return cp_new_stat(&stat
, statbuf
);
341 SYSCALL_DEFINE2(newlstat
, const char __user
*, filename
,
342 struct stat __user
*, statbuf
)
347 error
= vfs_lstat(filename
, &stat
);
351 return cp_new_stat(&stat
, statbuf
);
354 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
355 SYSCALL_DEFINE4(newfstatat
, int, dfd
, const char __user
*, filename
,
356 struct stat __user
*, statbuf
, int, flag
)
361 error
= vfs_fstatat(dfd
, filename
, &stat
, flag
);
364 return cp_new_stat(&stat
, statbuf
);
368 SYSCALL_DEFINE2(newfstat
, unsigned int, fd
, struct stat __user
*, statbuf
)
371 int error
= vfs_fstat(fd
, &stat
);
374 error
= cp_new_stat(&stat
, statbuf
);
379 SYSCALL_DEFINE4(readlinkat
, int, dfd
, const char __user
*, pathname
,
380 char __user
*, buf
, int, bufsiz
)
385 unsigned int lookup_flags
= LOOKUP_EMPTY
;
391 error
= user_path_at_empty(dfd
, pathname
, lookup_flags
, &path
, &empty
);
393 struct inode
*inode
= d_backing_inode(path
.dentry
);
395 error
= empty
? -ENOENT
: -EINVAL
;
397 * AFS mountpoints allow readlink(2) but are not symlinks
399 if (d_is_symlink(path
.dentry
) || inode
->i_op
->readlink
) {
400 error
= security_inode_readlink(path
.dentry
);
403 error
= vfs_readlink(path
.dentry
, buf
, bufsiz
);
407 if (retry_estale(error
, lookup_flags
)) {
408 lookup_flags
|= LOOKUP_REVAL
;
415 SYSCALL_DEFINE3(readlink
, const char __user
*, path
, char __user
*, buf
,
418 return sys_readlinkat(AT_FDCWD
, path
, buf
, bufsiz
);
422 /* ---------- LFS-64 ----------- */
423 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
425 #ifndef INIT_STRUCT_STAT64_PADDING
426 # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
429 static long cp_new_stat64(struct kstat
*stat
, struct stat64 __user
*statbuf
)
433 INIT_STRUCT_STAT64_PADDING(tmp
);
435 /* mips has weird padding, so we don't get 64 bits there */
436 tmp
.st_dev
= new_encode_dev(stat
->dev
);
437 tmp
.st_rdev
= new_encode_dev(stat
->rdev
);
439 tmp
.st_dev
= huge_encode_dev(stat
->dev
);
440 tmp
.st_rdev
= huge_encode_dev(stat
->rdev
);
442 tmp
.st_ino
= stat
->ino
;
443 if (sizeof(tmp
.st_ino
) < sizeof(stat
->ino
) && tmp
.st_ino
!= stat
->ino
)
445 #ifdef STAT64_HAS_BROKEN_ST_INO
446 tmp
.__st_ino
= stat
->ino
;
448 tmp
.st_mode
= stat
->mode
;
449 tmp
.st_nlink
= stat
->nlink
;
450 tmp
.st_uid
= from_kuid_munged(current_user_ns(), stat
->uid
);
451 tmp
.st_gid
= from_kgid_munged(current_user_ns(), stat
->gid
);
452 tmp
.st_atime
= stat
->atime
.tv_sec
;
453 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
454 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
455 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
456 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
457 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
458 tmp
.st_size
= stat
->size
;
459 tmp
.st_blocks
= stat
->blocks
;
460 tmp
.st_blksize
= stat
->blksize
;
461 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
464 SYSCALL_DEFINE2(stat64
, const char __user
*, filename
,
465 struct stat64 __user
*, statbuf
)
468 int error
= vfs_stat(filename
, &stat
);
471 error
= cp_new_stat64(&stat
, statbuf
);
476 SYSCALL_DEFINE2(lstat64
, const char __user
*, filename
,
477 struct stat64 __user
*, statbuf
)
480 int error
= vfs_lstat(filename
, &stat
);
483 error
= cp_new_stat64(&stat
, statbuf
);
488 SYSCALL_DEFINE2(fstat64
, unsigned long, fd
, struct stat64 __user
*, statbuf
)
491 int error
= vfs_fstat(fd
, &stat
);
494 error
= cp_new_stat64(&stat
, statbuf
);
499 SYSCALL_DEFINE4(fstatat64
, int, dfd
, const char __user
*, filename
,
500 struct stat64 __user
*, statbuf
, int, flag
)
505 error
= vfs_fstatat(dfd
, filename
, &stat
, flag
);
508 return cp_new_stat64(&stat
, statbuf
);
510 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
512 static inline int __put_timestamp(struct timespec
*kts
,
513 struct statx_timestamp __user
*uts
)
515 return (__put_user(kts
->tv_sec
, &uts
->tv_sec
) ||
516 __put_user(kts
->tv_nsec
, &uts
->tv_nsec
) ||
517 __put_user(0, &uts
->__reserved
));
521 * Set the statx results.
523 static long statx_set_result(struct kstat
*stat
, struct statx __user
*buffer
)
525 uid_t uid
= from_kuid_munged(current_user_ns(), stat
->uid
);
526 gid_t gid
= from_kgid_munged(current_user_ns(), stat
->gid
);
528 if (__put_user(stat
->result_mask
, &buffer
->stx_mask
) ||
529 __put_user(stat
->mode
, &buffer
->stx_mode
) ||
530 __clear_user(&buffer
->__spare0
, sizeof(buffer
->__spare0
)) ||
531 __put_user(stat
->nlink
, &buffer
->stx_nlink
) ||
532 __put_user(uid
, &buffer
->stx_uid
) ||
533 __put_user(gid
, &buffer
->stx_gid
) ||
534 __put_user(stat
->attributes
, &buffer
->stx_attributes
) ||
535 __put_user(stat
->blksize
, &buffer
->stx_blksize
) ||
536 __put_user(MAJOR(stat
->rdev
), &buffer
->stx_rdev_major
) ||
537 __put_user(MINOR(stat
->rdev
), &buffer
->stx_rdev_minor
) ||
538 __put_user(MAJOR(stat
->dev
), &buffer
->stx_dev_major
) ||
539 __put_user(MINOR(stat
->dev
), &buffer
->stx_dev_minor
) ||
540 __put_timestamp(&stat
->atime
, &buffer
->stx_atime
) ||
541 __put_timestamp(&stat
->btime
, &buffer
->stx_btime
) ||
542 __put_timestamp(&stat
->ctime
, &buffer
->stx_ctime
) ||
543 __put_timestamp(&stat
->mtime
, &buffer
->stx_mtime
) ||
544 __put_user(stat
->ino
, &buffer
->stx_ino
) ||
545 __put_user(stat
->size
, &buffer
->stx_size
) ||
546 __put_user(stat
->blocks
, &buffer
->stx_blocks
) ||
547 __clear_user(&buffer
->__spare1
, sizeof(buffer
->__spare1
)) ||
548 __clear_user(&buffer
->__spare2
, sizeof(buffer
->__spare2
)))
555 * sys_statx - System call to get enhanced stats
556 * @dfd: Base directory to pathwalk from *or* fd to stat.
557 * @filename: File to stat *or* NULL.
558 * @flags: AT_* flags to control pathwalk.
559 * @mask: Parts of statx struct actually required.
560 * @buffer: Result buffer.
562 * Note that if filename is NULL, then it does the equivalent of fstat() using
563 * dfd to indicate the file of interest.
565 SYSCALL_DEFINE5(statx
,
566 int, dfd
, const char __user
*, filename
, unsigned, flags
,
568 struct statx __user
*, buffer
)
573 if ((flags
& AT_STATX_SYNC_TYPE
) == AT_STATX_SYNC_TYPE
)
575 if (!access_ok(VERIFY_WRITE
, buffer
, sizeof(*buffer
)))
579 error
= vfs_statx(dfd
, filename
, flags
, &stat
, mask
);
581 error
= vfs_statx_fd(dfd
, &stat
, mask
, flags
);
584 return statx_set_result(&stat
, buffer
);
587 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
588 void __inode_add_bytes(struct inode
*inode
, loff_t bytes
)
590 inode
->i_blocks
+= bytes
>> 9;
592 inode
->i_bytes
+= bytes
;
593 if (inode
->i_bytes
>= 512) {
595 inode
->i_bytes
-= 512;
599 void inode_add_bytes(struct inode
*inode
, loff_t bytes
)
601 spin_lock(&inode
->i_lock
);
602 __inode_add_bytes(inode
, bytes
);
603 spin_unlock(&inode
->i_lock
);
606 EXPORT_SYMBOL(inode_add_bytes
);
608 void __inode_sub_bytes(struct inode
*inode
, loff_t bytes
)
610 inode
->i_blocks
-= bytes
>> 9;
612 if (inode
->i_bytes
< bytes
) {
614 inode
->i_bytes
+= 512;
616 inode
->i_bytes
-= bytes
;
619 EXPORT_SYMBOL(__inode_sub_bytes
);
621 void inode_sub_bytes(struct inode
*inode
, loff_t bytes
)
623 spin_lock(&inode
->i_lock
);
624 __inode_sub_bytes(inode
, bytes
);
625 spin_unlock(&inode
->i_lock
);
628 EXPORT_SYMBOL(inode_sub_bytes
);
630 loff_t
inode_get_bytes(struct inode
*inode
)
634 spin_lock(&inode
->i_lock
);
635 ret
= (((loff_t
)inode
->i_blocks
) << 9) + inode
->i_bytes
;
636 spin_unlock(&inode
->i_lock
);
640 EXPORT_SYMBOL(inode_get_bytes
);
642 void inode_set_bytes(struct inode
*inode
, loff_t bytes
)
644 /* Caller is here responsible for sufficient locking
645 * (ie. inode->i_lock) */
646 inode
->i_blocks
= bytes
>> 9;
647 inode
->i_bytes
= bytes
& 511;
650 EXPORT_SYMBOL(inode_set_bytes
);