stat.c 22 KB

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