stat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/stat.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/export.h>
  8. #include <linux/mm.h>
  9. #include <linux/errno.h>
  10. #include <linux/file.h>
  11. #include <linux/highuid.h>
  12. #include <linux/fs.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>
  18. #include <linux/compat.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/unistd.h>
  21. #include "internal.h"
  22. #include "mount.h"
  23. /**
  24. * generic_fillattr - Fill in the basic attributes from the inode struct
  25. * @inode: Inode to use as the source
  26. * @stat: Where to fill in the attributes
  27. *
  28. * Fill in the basic attributes in the kstat structure from data that's to be
  29. * found on the VFS inode structure. This is the default if no getattr inode
  30. * operation is supplied.
  31. */
  32. void generic_fillattr(struct inode *inode, struct kstat *stat)
  33. {
  34. stat->dev = inode->i_sb->s_dev;
  35. stat->ino = inode->i_ino;
  36. stat->mode = inode->i_mode;
  37. stat->nlink = inode->i_nlink;
  38. stat->uid = inode->i_uid;
  39. stat->gid = inode->i_gid;
  40. stat->rdev = inode->i_rdev;
  41. stat->size = i_size_read(inode);
  42. stat->atime = inode->i_atime;
  43. stat->mtime = inode->i_mtime;
  44. stat->ctime = inode->i_ctime;
  45. stat->blksize = i_blocksize(inode);
  46. stat->blocks = inode->i_blocks;
  47. }
  48. EXPORT_SYMBOL_NS(generic_fillattr, ANDROID_GKI_VFS_EXPORT_ONLY);
  49. /**
  50. * vfs_getattr_nosec - getattr without security checks
  51. * @path: file to get attributes from
  52. * @stat: structure to return attributes in
  53. * @request_mask: STATX_xxx flags indicating what the caller wants
  54. * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
  55. *
  56. * Get attributes without calling security_inode_getattr.
  57. *
  58. * Currently the only caller other than vfs_getattr is internal to the
  59. * filehandle lookup code, which uses only the inode number and returns no
  60. * attributes to any user. Any other code probably wants vfs_getattr.
  61. */
  62. int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
  63. u32 request_mask, unsigned int query_flags)
  64. {
  65. struct inode *inode = d_backing_inode(path->dentry);
  66. memset(stat, 0, sizeof(*stat));
  67. stat->result_mask |= STATX_BASIC_STATS;
  68. query_flags &= AT_STATX_SYNC_TYPE;
  69. /* allow the fs to override these if it really wants to */
  70. /* SB_NOATIME means filesystem supplies dummy atime value */
  71. if (inode->i_sb->s_flags & SB_NOATIME)
  72. stat->result_mask &= ~STATX_ATIME;
  73. /*
  74. * Note: If you add another clause to set an attribute flag, please
  75. * update attributes_mask below.
  76. */
  77. if (IS_AUTOMOUNT(inode))
  78. stat->attributes |= STATX_ATTR_AUTOMOUNT;
  79. if (IS_DAX(inode))
  80. stat->attributes |= STATX_ATTR_DAX;
  81. stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
  82. STATX_ATTR_DAX);
  83. if (inode->i_op->getattr)
  84. return inode->i_op->getattr(path, stat, request_mask,
  85. query_flags);
  86. generic_fillattr(inode, stat);
  87. return 0;
  88. }
  89. EXPORT_SYMBOL(vfs_getattr_nosec);
  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 (AT_STATX_SYNC_TYPE)
  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. retval = security_inode_getattr(path);
  116. if (retval)
  117. return retval;
  118. return vfs_getattr_nosec(path, stat, request_mask, query_flags);
  119. }
  120. EXPORT_SYMBOL(vfs_getattr);
  121. /**
  122. * vfs_fstat - Get the basic attributes by file descriptor
  123. * @fd: The file descriptor referring to the file of interest
  124. * @stat: The result structure to fill in.
  125. *
  126. * This function is a wrapper around vfs_getattr(). The main difference is
  127. * that it uses a file descriptor to determine the file location.
  128. *
  129. * 0 will be returned on success, and a -ve error code if unsuccessful.
  130. */
  131. int vfs_fstat(int fd, struct kstat *stat)
  132. {
  133. struct fd f;
  134. int error;
  135. f = fdget_raw(fd);
  136. if (!f.file)
  137. return -EBADF;
  138. error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
  139. fdput(f);
  140. return error;
  141. }
  142. /**
  143. * vfs_statx - Get basic and extra attributes by filename
  144. * @dfd: A file descriptor representing the base dir for a relative filename
  145. * @filename: The name of the file of interest
  146. * @flags: Flags to control the query
  147. * @stat: The result structure to fill in.
  148. * @request_mask: STATX_xxx flags indicating what the caller wants
  149. *
  150. * This function is a wrapper around vfs_getattr(). The main difference is
  151. * that it uses a filename and base directory to determine the file location.
  152. * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
  153. * at the given name from being referenced.
  154. *
  155. * 0 will be returned on success, and a -ve error code if unsuccessful.
  156. */
  157. static int vfs_statx(int dfd, const char __user *filename, int flags,
  158. struct kstat *stat, u32 request_mask)
  159. {
  160. struct path path;
  161. unsigned lookup_flags = 0;
  162. int error;
  163. if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
  164. AT_STATX_SYNC_TYPE))
  165. return -EINVAL;
  166. if (!(flags & AT_SYMLINK_NOFOLLOW))
  167. lookup_flags |= LOOKUP_FOLLOW;
  168. if (!(flags & AT_NO_AUTOMOUNT))
  169. lookup_flags |= LOOKUP_AUTOMOUNT;
  170. if (flags & AT_EMPTY_PATH)
  171. lookup_flags |= LOOKUP_EMPTY;
  172. retry:
  173. error = user_path_at(dfd, filename, lookup_flags, &path);
  174. if (error)
  175. goto out;
  176. error = vfs_getattr(&path, stat, request_mask, flags);
  177. stat->mnt_id = real_mount(path.mnt)->mnt_id;
  178. stat->result_mask |= STATX_MNT_ID;
  179. if (path.mnt->mnt_root == path.dentry)
  180. stat->attributes |= STATX_ATTR_MOUNT_ROOT;
  181. stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
  182. path_put(&path);
  183. if (retry_estale(error, lookup_flags)) {
  184. lookup_flags |= LOOKUP_REVAL;
  185. goto retry;
  186. }
  187. out:
  188. return error;
  189. }
  190. int vfs_fstatat(int dfd, const char __user *filename,
  191. struct kstat *stat, int flags)
  192. {
  193. return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT,
  194. stat, STATX_BASIC_STATS);
  195. }
  196. #ifdef __ARCH_WANT_OLD_STAT
  197. /*
  198. * For backward compatibility? Maybe this should be moved
  199. * into arch/i386 instead?
  200. */
  201. static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
  202. {
  203. static int warncount = 5;
  204. struct __old_kernel_stat tmp;
  205. if (warncount > 0) {
  206. warncount--;
  207. printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
  208. current->comm);
  209. } else if (warncount < 0) {
  210. /* it's laughable, but... */
  211. warncount = 0;
  212. }
  213. memset(&tmp, 0, sizeof(struct __old_kernel_stat));
  214. tmp.st_dev = old_encode_dev(stat->dev);
  215. tmp.st_ino = stat->ino;
  216. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  217. return -EOVERFLOW;
  218. tmp.st_mode = stat->mode;
  219. tmp.st_nlink = stat->nlink;
  220. if (tmp.st_nlink != stat->nlink)
  221. return -EOVERFLOW;
  222. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  223. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  224. tmp.st_rdev = old_encode_dev(stat->rdev);
  225. #if BITS_PER_LONG == 32
  226. if (stat->size > MAX_NON_LFS)
  227. return -EOVERFLOW;
  228. #endif
  229. tmp.st_size = stat->size;
  230. tmp.st_atime = stat->atime.tv_sec;
  231. tmp.st_mtime = stat->mtime.tv_sec;
  232. tmp.st_ctime = stat->ctime.tv_sec;
  233. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  234. }
  235. SYSCALL_DEFINE2(stat, const char __user *, filename,
  236. struct __old_kernel_stat __user *, statbuf)
  237. {
  238. struct kstat stat;
  239. int error;
  240. error = vfs_stat(filename, &stat);
  241. if (error)
  242. return error;
  243. return cp_old_stat(&stat, statbuf);
  244. }
  245. SYSCALL_DEFINE2(lstat, const char __user *, filename,
  246. struct __old_kernel_stat __user *, statbuf)
  247. {
  248. struct kstat stat;
  249. int error;
  250. error = vfs_lstat(filename, &stat);
  251. if (error)
  252. return error;
  253. return cp_old_stat(&stat, statbuf);
  254. }
  255. SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
  256. {
  257. struct kstat stat;
  258. int error = vfs_fstat(fd, &stat);
  259. if (!error)
  260. error = cp_old_stat(&stat, statbuf);
  261. return error;
  262. }
  263. #endif /* __ARCH_WANT_OLD_STAT */
  264. #ifdef __ARCH_WANT_NEW_STAT
  265. #if BITS_PER_LONG == 32
  266. # define choose_32_64(a,b) a
  267. #else
  268. # define choose_32_64(a,b) b
  269. #endif
  270. #ifndef INIT_STRUCT_STAT_PADDING
  271. # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
  272. #endif
  273. static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
  274. {
  275. struct stat tmp;
  276. if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
  277. return -EOVERFLOW;
  278. if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
  279. return -EOVERFLOW;
  280. #if BITS_PER_LONG == 32
  281. if (stat->size > MAX_NON_LFS)
  282. return -EOVERFLOW;
  283. #endif
  284. INIT_STRUCT_STAT_PADDING(tmp);
  285. tmp.st_dev = new_encode_dev(stat->dev);
  286. tmp.st_ino = stat->ino;
  287. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  288. return -EOVERFLOW;
  289. tmp.st_mode = stat->mode;
  290. tmp.st_nlink = stat->nlink;
  291. if (tmp.st_nlink != stat->nlink)
  292. return -EOVERFLOW;
  293. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  294. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  295. tmp.st_rdev = new_encode_dev(stat->rdev);
  296. tmp.st_size = stat->size;
  297. tmp.st_atime = stat->atime.tv_sec;
  298. tmp.st_mtime = stat->mtime.tv_sec;
  299. tmp.st_ctime = stat->ctime.tv_sec;
  300. #ifdef STAT_HAVE_NSEC
  301. tmp.st_atime_nsec = stat->atime.tv_nsec;
  302. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  303. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  304. #endif
  305. tmp.st_blocks = stat->blocks;
  306. tmp.st_blksize = stat->blksize;
  307. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  308. }
  309. SYSCALL_DEFINE2(newstat, const char __user *, filename,
  310. struct stat __user *, statbuf)
  311. {
  312. struct kstat stat;
  313. int error = vfs_stat(filename, &stat);
  314. if (error)
  315. return error;
  316. return cp_new_stat(&stat, statbuf);
  317. }
  318. SYSCALL_DEFINE2(newlstat, const char __user *, filename,
  319. struct stat __user *, statbuf)
  320. {
  321. struct kstat stat;
  322. int error;
  323. error = vfs_lstat(filename, &stat);
  324. if (error)
  325. return error;
  326. return cp_new_stat(&stat, statbuf);
  327. }
  328. #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
  329. SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
  330. struct stat __user *, statbuf, int, flag)
  331. {
  332. struct kstat stat;
  333. int error;
  334. error = vfs_fstatat(dfd, filename, &stat, flag);
  335. if (error)
  336. return error;
  337. return cp_new_stat(&stat, statbuf);
  338. }
  339. #endif
  340. SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
  341. {
  342. struct kstat stat;
  343. int error = vfs_fstat(fd, &stat);
  344. if (!error)
  345. error = cp_new_stat(&stat, statbuf);
  346. return error;
  347. }
  348. #endif
  349. static int do_readlinkat(int dfd, const char __user *pathname,
  350. char __user *buf, int bufsiz)
  351. {
  352. struct path path;
  353. int error;
  354. int empty = 0;
  355. unsigned int lookup_flags = LOOKUP_EMPTY;
  356. if (bufsiz <= 0)
  357. return -EINVAL;
  358. retry:
  359. error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
  360. if (!error) {
  361. struct inode *inode = d_backing_inode(path.dentry);
  362. error = empty ? -ENOENT : -EINVAL;
  363. /*
  364. * AFS mountpoints allow readlink(2) but are not symlinks
  365. */
  366. if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
  367. error = security_inode_readlink(path.dentry);
  368. if (!error) {
  369. touch_atime(&path);
  370. error = vfs_readlink(path.dentry, buf, bufsiz);
  371. }
  372. }
  373. path_put(&path);
  374. if (retry_estale(error, lookup_flags)) {
  375. lookup_flags |= LOOKUP_REVAL;
  376. goto retry;
  377. }
  378. }
  379. return error;
  380. }
  381. SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
  382. char __user *, buf, int, bufsiz)
  383. {
  384. return do_readlinkat(dfd, pathname, buf, bufsiz);
  385. }
  386. SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
  387. int, bufsiz)
  388. {
  389. return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
  390. }
  391. /* ---------- LFS-64 ----------- */
  392. #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
  393. #ifndef INIT_STRUCT_STAT64_PADDING
  394. # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
  395. #endif
  396. static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
  397. {
  398. struct stat64 tmp;
  399. INIT_STRUCT_STAT64_PADDING(tmp);
  400. #ifdef CONFIG_MIPS
  401. /* mips has weird padding, so we don't get 64 bits there */
  402. tmp.st_dev = new_encode_dev(stat->dev);
  403. tmp.st_rdev = new_encode_dev(stat->rdev);
  404. #else
  405. tmp.st_dev = huge_encode_dev(stat->dev);
  406. tmp.st_rdev = huge_encode_dev(stat->rdev);
  407. #endif
  408. tmp.st_ino = stat->ino;
  409. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  410. return -EOVERFLOW;
  411. #ifdef STAT64_HAS_BROKEN_ST_INO
  412. tmp.__st_ino = stat->ino;
  413. #endif
  414. tmp.st_mode = stat->mode;
  415. tmp.st_nlink = stat->nlink;
  416. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  417. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  418. tmp.st_atime = stat->atime.tv_sec;
  419. tmp.st_atime_nsec = stat->atime.tv_nsec;
  420. tmp.st_mtime = stat->mtime.tv_sec;
  421. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  422. tmp.st_ctime = stat->ctime.tv_sec;
  423. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  424. tmp.st_size = stat->size;
  425. tmp.st_blocks = stat->blocks;
  426. tmp.st_blksize = stat->blksize;
  427. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  428. }
  429. SYSCALL_DEFINE2(stat64, const char __user *, filename,
  430. struct stat64 __user *, statbuf)
  431. {
  432. struct kstat stat;
  433. int error = vfs_stat(filename, &stat);
  434. if (!error)
  435. error = cp_new_stat64(&stat, statbuf);
  436. return error;
  437. }
  438. SYSCALL_DEFINE2(lstat64, const char __user *, filename,
  439. struct stat64 __user *, statbuf)
  440. {
  441. struct kstat stat;
  442. int error = vfs_lstat(filename, &stat);
  443. if (!error)
  444. error = cp_new_stat64(&stat, statbuf);
  445. return error;
  446. }
  447. SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
  448. {
  449. struct kstat stat;
  450. int error = vfs_fstat(fd, &stat);
  451. if (!error)
  452. error = cp_new_stat64(&stat, statbuf);
  453. return error;
  454. }
  455. SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
  456. struct stat64 __user *, statbuf, int, flag)
  457. {
  458. struct kstat stat;
  459. int error;
  460. error = vfs_fstatat(dfd, filename, &stat, flag);
  461. if (error)
  462. return error;
  463. return cp_new_stat64(&stat, statbuf);
  464. }
  465. #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
  466. static noinline_for_stack int
  467. cp_statx(const struct kstat *stat, struct statx __user *buffer)
  468. {
  469. struct statx tmp;
  470. memset(&tmp, 0, sizeof(tmp));
  471. tmp.stx_mask = stat->result_mask;
  472. tmp.stx_blksize = stat->blksize;
  473. tmp.stx_attributes = stat->attributes;
  474. tmp.stx_nlink = stat->nlink;
  475. tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
  476. tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
  477. tmp.stx_mode = stat->mode;
  478. tmp.stx_ino = stat->ino;
  479. tmp.stx_size = stat->size;
  480. tmp.stx_blocks = stat->blocks;
  481. tmp.stx_attributes_mask = stat->attributes_mask;
  482. tmp.stx_atime.tv_sec = stat->atime.tv_sec;
  483. tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
  484. tmp.stx_btime.tv_sec = stat->btime.tv_sec;
  485. tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
  486. tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
  487. tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
  488. tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
  489. tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
  490. tmp.stx_rdev_major = MAJOR(stat->rdev);
  491. tmp.stx_rdev_minor = MINOR(stat->rdev);
  492. tmp.stx_dev_major = MAJOR(stat->dev);
  493. tmp.stx_dev_minor = MINOR(stat->dev);
  494. tmp.stx_mnt_id = stat->mnt_id;
  495. return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  496. }
  497. int do_statx(int dfd, const char __user *filename, unsigned flags,
  498. unsigned int mask, struct statx __user *buffer)
  499. {
  500. struct kstat stat;
  501. int error;
  502. if (mask & STATX__RESERVED)
  503. return -EINVAL;
  504. if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
  505. return -EINVAL;
  506. error = vfs_statx(dfd, filename, flags, &stat, mask);
  507. if (error)
  508. return error;
  509. return cp_statx(&stat, buffer);
  510. }
  511. /**
  512. * sys_statx - System call to get enhanced stats
  513. * @dfd: Base directory to pathwalk from *or* fd to stat.
  514. * @filename: File to stat or "" with AT_EMPTY_PATH
  515. * @flags: AT_* flags to control pathwalk.
  516. * @mask: Parts of statx struct actually required.
  517. * @buffer: Result buffer.
  518. *
  519. * Note that fstat() can be emulated by setting dfd to the fd of interest,
  520. * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
  521. */
  522. SYSCALL_DEFINE5(statx,
  523. int, dfd, const char __user *, filename, unsigned, flags,
  524. unsigned int, mask,
  525. struct statx __user *, buffer)
  526. {
  527. return do_statx(dfd, filename, flags, mask, buffer);
  528. }
  529. #ifdef CONFIG_COMPAT
  530. static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
  531. {
  532. struct compat_stat tmp;
  533. if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
  534. return -EOVERFLOW;
  535. if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
  536. return -EOVERFLOW;
  537. memset(&tmp, 0, sizeof(tmp));
  538. tmp.st_dev = new_encode_dev(stat->dev);
  539. tmp.st_ino = stat->ino;
  540. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  541. return -EOVERFLOW;
  542. tmp.st_mode = stat->mode;
  543. tmp.st_nlink = stat->nlink;
  544. if (tmp.st_nlink != stat->nlink)
  545. return -EOVERFLOW;
  546. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  547. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  548. tmp.st_rdev = new_encode_dev(stat->rdev);
  549. if ((u64) stat->size > MAX_NON_LFS)
  550. return -EOVERFLOW;
  551. tmp.st_size = stat->size;
  552. tmp.st_atime = stat->atime.tv_sec;
  553. tmp.st_atime_nsec = stat->atime.tv_nsec;
  554. tmp.st_mtime = stat->mtime.tv_sec;
  555. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  556. tmp.st_ctime = stat->ctime.tv_sec;
  557. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  558. tmp.st_blocks = stat->blocks;
  559. tmp.st_blksize = stat->blksize;
  560. return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  561. }
  562. COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
  563. struct compat_stat __user *, statbuf)
  564. {
  565. struct kstat stat;
  566. int error;
  567. error = vfs_stat(filename, &stat);
  568. if (error)
  569. return error;
  570. return cp_compat_stat(&stat, statbuf);
  571. }
  572. COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
  573. struct compat_stat __user *, statbuf)
  574. {
  575. struct kstat stat;
  576. int error;
  577. error = vfs_lstat(filename, &stat);
  578. if (error)
  579. return error;
  580. return cp_compat_stat(&stat, statbuf);
  581. }
  582. #ifndef __ARCH_WANT_STAT64
  583. COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
  584. const char __user *, filename,
  585. struct compat_stat __user *, statbuf, int, flag)
  586. {
  587. struct kstat stat;
  588. int error;
  589. error = vfs_fstatat(dfd, filename, &stat, flag);
  590. if (error)
  591. return error;
  592. return cp_compat_stat(&stat, statbuf);
  593. }
  594. #endif
  595. COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
  596. struct compat_stat __user *, statbuf)
  597. {
  598. struct kstat stat;
  599. int error = vfs_fstat(fd, &stat);
  600. if (!error)
  601. error = cp_compat_stat(&stat, statbuf);
  602. return error;
  603. }
  604. #endif
  605. /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
  606. void __inode_add_bytes(struct inode *inode, loff_t bytes)
  607. {
  608. inode->i_blocks += bytes >> 9;
  609. bytes &= 511;
  610. inode->i_bytes += bytes;
  611. if (inode->i_bytes >= 512) {
  612. inode->i_blocks++;
  613. inode->i_bytes -= 512;
  614. }
  615. }
  616. EXPORT_SYMBOL(__inode_add_bytes);
  617. void inode_add_bytes(struct inode *inode, loff_t bytes)
  618. {
  619. spin_lock(&inode->i_lock);
  620. __inode_add_bytes(inode, bytes);
  621. spin_unlock(&inode->i_lock);
  622. }
  623. EXPORT_SYMBOL(inode_add_bytes);
  624. void __inode_sub_bytes(struct inode *inode, loff_t bytes)
  625. {
  626. inode->i_blocks -= bytes >> 9;
  627. bytes &= 511;
  628. if (inode->i_bytes < bytes) {
  629. inode->i_blocks--;
  630. inode->i_bytes += 512;
  631. }
  632. inode->i_bytes -= bytes;
  633. }
  634. EXPORT_SYMBOL(__inode_sub_bytes);
  635. void inode_sub_bytes(struct inode *inode, loff_t bytes)
  636. {
  637. spin_lock(&inode->i_lock);
  638. __inode_sub_bytes(inode, bytes);
  639. spin_unlock(&inode->i_lock);
  640. }
  641. EXPORT_SYMBOL(inode_sub_bytes);
  642. loff_t inode_get_bytes(struct inode *inode)
  643. {
  644. loff_t ret;
  645. spin_lock(&inode->i_lock);
  646. ret = __inode_get_bytes(inode);
  647. spin_unlock(&inode->i_lock);
  648. return ret;
  649. }
  650. EXPORT_SYMBOL(inode_get_bytes);
  651. void inode_set_bytes(struct inode *inode, loff_t bytes)
  652. {
  653. /* Caller is here responsible for sufficient locking
  654. * (ie. inode->i_lock) */
  655. inode->i_blocks = bytes >> 9;
  656. inode->i_bytes = bytes & 511;
  657. }
  658. EXPORT_SYMBOL(inode_set_bytes);