stat.c 18 KB

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