stat.c 17 KB

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