stat.c 27 KB

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