stat.c 19 KB

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