stat.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. #if BITS_PER_LONG == 32
  251. # define choose_32_64(a,b) a
  252. #else
  253. # define choose_32_64(a,b) b
  254. #endif
  255. #define valid_dev(x) choose_32_64(old_valid_dev(x),true)
  256. #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
  257. #ifndef INIT_STRUCT_STAT_PADDING
  258. # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
  259. #endif
  260. static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
  261. {
  262. struct stat tmp;
  263. if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
  264. return -EOVERFLOW;
  265. #if BITS_PER_LONG == 32
  266. if (stat->size > MAX_NON_LFS)
  267. return -EOVERFLOW;
  268. #endif
  269. INIT_STRUCT_STAT_PADDING(tmp);
  270. tmp.st_dev = encode_dev(stat->dev);
  271. tmp.st_ino = stat->ino;
  272. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  273. return -EOVERFLOW;
  274. tmp.st_mode = stat->mode;
  275. tmp.st_nlink = stat->nlink;
  276. if (tmp.st_nlink != stat->nlink)
  277. return -EOVERFLOW;
  278. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  279. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  280. tmp.st_rdev = encode_dev(stat->rdev);
  281. tmp.st_size = stat->size;
  282. tmp.st_atime = stat->atime.tv_sec;
  283. tmp.st_mtime = stat->mtime.tv_sec;
  284. tmp.st_ctime = stat->ctime.tv_sec;
  285. #ifdef STAT_HAVE_NSEC
  286. tmp.st_atime_nsec = stat->atime.tv_nsec;
  287. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  288. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  289. #endif
  290. tmp.st_blocks = stat->blocks;
  291. tmp.st_blksize = stat->blksize;
  292. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  293. }
  294. SYSCALL_DEFINE2(newstat, const char __user *, filename,
  295. struct stat __user *, statbuf)
  296. {
  297. struct kstat stat;
  298. int error = vfs_stat(filename, &stat);
  299. if (error)
  300. return error;
  301. return cp_new_stat(&stat, statbuf);
  302. }
  303. SYSCALL_DEFINE2(newlstat, const char __user *, filename,
  304. struct stat __user *, statbuf)
  305. {
  306. struct kstat stat;
  307. int error;
  308. error = vfs_lstat(filename, &stat);
  309. if (error)
  310. return error;
  311. return cp_new_stat(&stat, statbuf);
  312. }
  313. #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
  314. SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
  315. struct stat __user *, statbuf, int, flag)
  316. {
  317. struct kstat stat;
  318. int error;
  319. error = vfs_fstatat(dfd, filename, &stat, flag);
  320. if (error)
  321. return error;
  322. return cp_new_stat(&stat, statbuf);
  323. }
  324. #endif
  325. SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
  326. {
  327. struct kstat stat;
  328. int error = vfs_fstat(fd, &stat);
  329. if (!error)
  330. error = cp_new_stat(&stat, statbuf);
  331. return error;
  332. }
  333. static int do_readlinkat(int dfd, const char __user *pathname,
  334. char __user *buf, int bufsiz)
  335. {
  336. struct path path;
  337. int error;
  338. int empty = 0;
  339. unsigned int lookup_flags = LOOKUP_EMPTY;
  340. if (bufsiz <= 0)
  341. return -EINVAL;
  342. retry:
  343. error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
  344. if (!error) {
  345. struct inode *inode = d_backing_inode(path.dentry);
  346. error = empty ? -ENOENT : -EINVAL;
  347. /*
  348. * AFS mountpoints allow readlink(2) but are not symlinks
  349. */
  350. if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
  351. error = security_inode_readlink(path.dentry);
  352. if (!error) {
  353. touch_atime(&path);
  354. error = vfs_readlink(path.dentry, buf, bufsiz);
  355. }
  356. }
  357. path_put(&path);
  358. if (retry_estale(error, lookup_flags)) {
  359. lookup_flags |= LOOKUP_REVAL;
  360. goto retry;
  361. }
  362. }
  363. return error;
  364. }
  365. SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
  366. char __user *, buf, int, bufsiz)
  367. {
  368. return do_readlinkat(dfd, pathname, buf, bufsiz);
  369. }
  370. SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
  371. int, bufsiz)
  372. {
  373. return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
  374. }
  375. /* ---------- LFS-64 ----------- */
  376. #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
  377. #ifndef INIT_STRUCT_STAT64_PADDING
  378. # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
  379. #endif
  380. static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
  381. {
  382. struct stat64 tmp;
  383. INIT_STRUCT_STAT64_PADDING(tmp);
  384. #ifdef CONFIG_MIPS
  385. /* mips has weird padding, so we don't get 64 bits there */
  386. tmp.st_dev = new_encode_dev(stat->dev);
  387. tmp.st_rdev = new_encode_dev(stat->rdev);
  388. #else
  389. tmp.st_dev = huge_encode_dev(stat->dev);
  390. tmp.st_rdev = huge_encode_dev(stat->rdev);
  391. #endif
  392. tmp.st_ino = stat->ino;
  393. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  394. return -EOVERFLOW;
  395. #ifdef STAT64_HAS_BROKEN_ST_INO
  396. tmp.__st_ino = stat->ino;
  397. #endif
  398. tmp.st_mode = stat->mode;
  399. tmp.st_nlink = stat->nlink;
  400. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  401. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  402. tmp.st_atime = stat->atime.tv_sec;
  403. tmp.st_atime_nsec = stat->atime.tv_nsec;
  404. tmp.st_mtime = stat->mtime.tv_sec;
  405. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  406. tmp.st_ctime = stat->ctime.tv_sec;
  407. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  408. tmp.st_size = stat->size;
  409. tmp.st_blocks = stat->blocks;
  410. tmp.st_blksize = stat->blksize;
  411. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  412. }
  413. SYSCALL_DEFINE2(stat64, const char __user *, filename,
  414. struct stat64 __user *, statbuf)
  415. {
  416. struct kstat stat;
  417. int error = vfs_stat(filename, &stat);
  418. if (!error)
  419. error = cp_new_stat64(&stat, statbuf);
  420. return error;
  421. }
  422. SYSCALL_DEFINE2(lstat64, const char __user *, filename,
  423. struct stat64 __user *, statbuf)
  424. {
  425. struct kstat stat;
  426. int error = vfs_lstat(filename, &stat);
  427. if (!error)
  428. error = cp_new_stat64(&stat, statbuf);
  429. return error;
  430. }
  431. SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
  432. {
  433. struct kstat stat;
  434. int error = vfs_fstat(fd, &stat);
  435. if (!error)
  436. error = cp_new_stat64(&stat, statbuf);
  437. return error;
  438. }
  439. SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
  440. struct stat64 __user *, statbuf, int, flag)
  441. {
  442. struct kstat stat;
  443. int error;
  444. error = vfs_fstatat(dfd, filename, &stat, flag);
  445. if (error)
  446. return error;
  447. return cp_new_stat64(&stat, statbuf);
  448. }
  449. #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
  450. static noinline_for_stack int
  451. cp_statx(const struct kstat *stat, struct statx __user *buffer)
  452. {
  453. struct statx tmp;
  454. memset(&tmp, 0, sizeof(tmp));
  455. tmp.stx_mask = stat->result_mask;
  456. tmp.stx_blksize = stat->blksize;
  457. tmp.stx_attributes = stat->attributes;
  458. tmp.stx_nlink = stat->nlink;
  459. tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
  460. tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
  461. tmp.stx_mode = stat->mode;
  462. tmp.stx_ino = stat->ino;
  463. tmp.stx_size = stat->size;
  464. tmp.stx_blocks = stat->blocks;
  465. tmp.stx_attributes_mask = stat->attributes_mask;
  466. tmp.stx_atime.tv_sec = stat->atime.tv_sec;
  467. tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
  468. tmp.stx_btime.tv_sec = stat->btime.tv_sec;
  469. tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
  470. tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
  471. tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
  472. tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
  473. tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
  474. tmp.stx_rdev_major = MAJOR(stat->rdev);
  475. tmp.stx_rdev_minor = MINOR(stat->rdev);
  476. tmp.stx_dev_major = MAJOR(stat->dev);
  477. tmp.stx_dev_minor = MINOR(stat->dev);
  478. return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  479. }
  480. /**
  481. * sys_statx - System call to get enhanced stats
  482. * @dfd: Base directory to pathwalk from *or* fd to stat.
  483. * @filename: File to stat or "" with AT_EMPTY_PATH
  484. * @flags: AT_* flags to control pathwalk.
  485. * @mask: Parts of statx struct actually required.
  486. * @buffer: Result buffer.
  487. *
  488. * Note that fstat() can be emulated by setting dfd to the fd of interest,
  489. * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
  490. */
  491. SYSCALL_DEFINE5(statx,
  492. int, dfd, const char __user *, filename, unsigned, flags,
  493. unsigned int, mask,
  494. struct statx __user *, buffer)
  495. {
  496. struct kstat stat;
  497. int error;
  498. if (mask & STATX__RESERVED)
  499. return -EINVAL;
  500. if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
  501. return -EINVAL;
  502. error = vfs_statx(dfd, filename, flags, &stat, mask);
  503. if (error)
  504. return error;
  505. return cp_statx(&stat, buffer);
  506. }
  507. #ifdef CONFIG_COMPAT
  508. static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
  509. {
  510. struct compat_stat tmp;
  511. if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
  512. return -EOVERFLOW;
  513. memset(&tmp, 0, sizeof(tmp));
  514. tmp.st_dev = old_encode_dev(stat->dev);
  515. tmp.st_ino = stat->ino;
  516. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  517. return -EOVERFLOW;
  518. tmp.st_mode = stat->mode;
  519. tmp.st_nlink = stat->nlink;
  520. if (tmp.st_nlink != stat->nlink)
  521. return -EOVERFLOW;
  522. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  523. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  524. tmp.st_rdev = old_encode_dev(stat->rdev);
  525. if ((u64) stat->size > MAX_NON_LFS)
  526. return -EOVERFLOW;
  527. tmp.st_size = stat->size;
  528. tmp.st_atime = stat->atime.tv_sec;
  529. tmp.st_atime_nsec = stat->atime.tv_nsec;
  530. tmp.st_mtime = stat->mtime.tv_sec;
  531. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  532. tmp.st_ctime = stat->ctime.tv_sec;
  533. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  534. tmp.st_blocks = stat->blocks;
  535. tmp.st_blksize = stat->blksize;
  536. return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
  537. }
  538. COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
  539. struct compat_stat __user *, statbuf)
  540. {
  541. struct kstat stat;
  542. int error;
  543. error = vfs_stat(filename, &stat);
  544. if (error)
  545. return error;
  546. return cp_compat_stat(&stat, statbuf);
  547. }
  548. COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
  549. struct compat_stat __user *, statbuf)
  550. {
  551. struct kstat stat;
  552. int error;
  553. error = vfs_lstat(filename, &stat);
  554. if (error)
  555. return error;
  556. return cp_compat_stat(&stat, statbuf);
  557. }
  558. #ifndef __ARCH_WANT_STAT64
  559. COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
  560. const char __user *, filename,
  561. struct compat_stat __user *, statbuf, int, flag)
  562. {
  563. struct kstat stat;
  564. int error;
  565. error = vfs_fstatat(dfd, filename, &stat, flag);
  566. if (error)
  567. return error;
  568. return cp_compat_stat(&stat, statbuf);
  569. }
  570. #endif
  571. COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
  572. struct compat_stat __user *, statbuf)
  573. {
  574. struct kstat stat;
  575. int error = vfs_fstat(fd, &stat);
  576. if (!error)
  577. error = cp_compat_stat(&stat, statbuf);
  578. return error;
  579. }
  580. #endif
  581. /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
  582. void __inode_add_bytes(struct inode *inode, loff_t bytes)
  583. {
  584. inode->i_blocks += bytes >> 9;
  585. bytes &= 511;
  586. inode->i_bytes += bytes;
  587. if (inode->i_bytes >= 512) {
  588. inode->i_blocks++;
  589. inode->i_bytes -= 512;
  590. }
  591. }
  592. EXPORT_SYMBOL(__inode_add_bytes);
  593. void inode_add_bytes(struct inode *inode, loff_t bytes)
  594. {
  595. spin_lock(&inode->i_lock);
  596. __inode_add_bytes(inode, bytes);
  597. spin_unlock(&inode->i_lock);
  598. }
  599. EXPORT_SYMBOL(inode_add_bytes);
  600. void __inode_sub_bytes(struct inode *inode, loff_t bytes)
  601. {
  602. inode->i_blocks -= bytes >> 9;
  603. bytes &= 511;
  604. if (inode->i_bytes < bytes) {
  605. inode->i_blocks--;
  606. inode->i_bytes += 512;
  607. }
  608. inode->i_bytes -= bytes;
  609. }
  610. EXPORT_SYMBOL(__inode_sub_bytes);
  611. void inode_sub_bytes(struct inode *inode, loff_t bytes)
  612. {
  613. spin_lock(&inode->i_lock);
  614. __inode_sub_bytes(inode, bytes);
  615. spin_unlock(&inode->i_lock);
  616. }
  617. EXPORT_SYMBOL(inode_sub_bytes);
  618. loff_t inode_get_bytes(struct inode *inode)
  619. {
  620. loff_t ret;
  621. spin_lock(&inode->i_lock);
  622. ret = __inode_get_bytes(inode);
  623. spin_unlock(&inode->i_lock);
  624. return ret;
  625. }
  626. EXPORT_SYMBOL(inode_get_bytes);
  627. void inode_set_bytes(struct inode *inode, loff_t bytes)
  628. {
  629. /* Caller is here responsible for sufficient locking
  630. * (ie. inode->i_lock) */
  631. inode->i_blocks = bytes >> 9;
  632. inode->i_bytes = bytes & 511;
  633. }
  634. EXPORT_SYMBOL(inode_set_bytes);