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