stat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <asm/uaccess.h>
  17. #include <asm/unistd.h>
  18. void generic_fillattr(struct inode *inode, struct kstat *stat)
  19. {
  20. stat->dev = inode->i_sb->s_dev;
  21. stat->ino = inode->i_ino;
  22. stat->mode = inode->i_mode;
  23. stat->nlink = inode->i_nlink;
  24. stat->uid = inode->i_uid;
  25. stat->gid = inode->i_gid;
  26. stat->rdev = inode->i_rdev;
  27. stat->size = i_size_read(inode);
  28. stat->atime = inode->i_atime;
  29. stat->mtime = inode->i_mtime;
  30. stat->ctime = inode->i_ctime;
  31. stat->blksize = i_blocksize(inode);
  32. stat->blocks = inode->i_blocks;
  33. }
  34. EXPORT_SYMBOL(generic_fillattr);
  35. /**
  36. * vfs_getattr_nosec - getattr without security checks
  37. * @path: file to get attributes from
  38. * @stat: structure to return attributes in
  39. *
  40. * Get attributes without calling security_inode_getattr.
  41. *
  42. * Currently the only caller other than vfs_getattr is internal to the
  43. * filehandle lookup code, which uses only the inode number and returns
  44. * no attributes to any user. Any other code probably wants
  45. * vfs_getattr.
  46. */
  47. int vfs_getattr_nosec(struct path *path, struct kstat *stat)
  48. {
  49. struct inode *inode = d_backing_inode(path->dentry);
  50. if (inode->i_op->getattr)
  51. return inode->i_op->getattr(path->mnt, path->dentry, stat);
  52. generic_fillattr(inode, stat);
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(vfs_getattr_nosec);
  56. int vfs_getattr(struct path *path, struct kstat *stat)
  57. {
  58. int retval;
  59. retval = security_inode_getattr(path);
  60. if (retval)
  61. return retval;
  62. return vfs_getattr_nosec(path, stat);
  63. }
  64. EXPORT_SYMBOL(vfs_getattr);
  65. int vfs_fstat(unsigned int fd, struct kstat *stat)
  66. {
  67. struct fd f = fdget_raw(fd);
  68. int error = -EBADF;
  69. if (f.file) {
  70. error = vfs_getattr(&f.file->f_path, stat);
  71. fdput(f);
  72. }
  73. return error;
  74. }
  75. EXPORT_SYMBOL(vfs_fstat);
  76. int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
  77. int flag)
  78. {
  79. struct path path;
  80. int error = -EINVAL;
  81. unsigned int lookup_flags = 0;
  82. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
  83. AT_EMPTY_PATH)) != 0)
  84. goto out;
  85. if (!(flag & AT_SYMLINK_NOFOLLOW))
  86. lookup_flags |= LOOKUP_FOLLOW;
  87. if (flag & AT_EMPTY_PATH)
  88. lookup_flags |= LOOKUP_EMPTY;
  89. retry:
  90. error = user_path_at(dfd, filename, lookup_flags, &path);
  91. if (error)
  92. goto out;
  93. error = vfs_getattr(&path, stat);
  94. path_put(&path);
  95. if (retry_estale(error, lookup_flags)) {
  96. lookup_flags |= LOOKUP_REVAL;
  97. goto retry;
  98. }
  99. out:
  100. return error;
  101. }
  102. EXPORT_SYMBOL(vfs_fstatat);
  103. int vfs_stat(const char __user *name, struct kstat *stat)
  104. {
  105. return vfs_fstatat(AT_FDCWD, name, stat, 0);
  106. }
  107. EXPORT_SYMBOL(vfs_stat);
  108. int vfs_lstat(const char __user *name, struct kstat *stat)
  109. {
  110. return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
  111. }
  112. EXPORT_SYMBOL(vfs_lstat);
  113. #ifdef __ARCH_WANT_OLD_STAT
  114. /*
  115. * For backward compatibility? Maybe this should be moved
  116. * into arch/i386 instead?
  117. */
  118. static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
  119. {
  120. static int warncount = 5;
  121. struct __old_kernel_stat tmp;
  122. if (warncount > 0) {
  123. warncount--;
  124. printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
  125. current->comm);
  126. } else if (warncount < 0) {
  127. /* it's laughable, but... */
  128. warncount = 0;
  129. }
  130. memset(&tmp, 0, sizeof(struct __old_kernel_stat));
  131. tmp.st_dev = old_encode_dev(stat->dev);
  132. tmp.st_ino = stat->ino;
  133. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  134. return -EOVERFLOW;
  135. tmp.st_mode = stat->mode;
  136. tmp.st_nlink = stat->nlink;
  137. if (tmp.st_nlink != stat->nlink)
  138. return -EOVERFLOW;
  139. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  140. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  141. tmp.st_rdev = old_encode_dev(stat->rdev);
  142. #if BITS_PER_LONG == 32
  143. if (stat->size > MAX_NON_LFS)
  144. return -EOVERFLOW;
  145. #endif
  146. tmp.st_size = stat->size;
  147. tmp.st_atime = stat->atime.tv_sec;
  148. tmp.st_mtime = stat->mtime.tv_sec;
  149. tmp.st_ctime = stat->ctime.tv_sec;
  150. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  151. }
  152. SYSCALL_DEFINE2(stat, const char __user *, filename,
  153. struct __old_kernel_stat __user *, statbuf)
  154. {
  155. struct kstat stat;
  156. int error;
  157. error = vfs_stat(filename, &stat);
  158. if (error)
  159. return error;
  160. return cp_old_stat(&stat, statbuf);
  161. }
  162. SYSCALL_DEFINE2(lstat, const char __user *, filename,
  163. struct __old_kernel_stat __user *, statbuf)
  164. {
  165. struct kstat stat;
  166. int error;
  167. error = vfs_lstat(filename, &stat);
  168. if (error)
  169. return error;
  170. return cp_old_stat(&stat, statbuf);
  171. }
  172. SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
  173. {
  174. struct kstat stat;
  175. int error = vfs_fstat(fd, &stat);
  176. if (!error)
  177. error = cp_old_stat(&stat, statbuf);
  178. return error;
  179. }
  180. #endif /* __ARCH_WANT_OLD_STAT */
  181. #if BITS_PER_LONG == 32
  182. # define choose_32_64(a,b) a
  183. #else
  184. # define choose_32_64(a,b) b
  185. #endif
  186. #define valid_dev(x) choose_32_64(old_valid_dev(x),true)
  187. #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
  188. #ifndef INIT_STRUCT_STAT_PADDING
  189. # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
  190. #endif
  191. static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
  192. {
  193. struct stat tmp;
  194. if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
  195. return -EOVERFLOW;
  196. #if BITS_PER_LONG == 32
  197. if (stat->size > MAX_NON_LFS)
  198. return -EOVERFLOW;
  199. #endif
  200. INIT_STRUCT_STAT_PADDING(tmp);
  201. tmp.st_dev = encode_dev(stat->dev);
  202. tmp.st_ino = stat->ino;
  203. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  204. return -EOVERFLOW;
  205. tmp.st_mode = stat->mode;
  206. tmp.st_nlink = stat->nlink;
  207. if (tmp.st_nlink != stat->nlink)
  208. return -EOVERFLOW;
  209. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  210. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  211. tmp.st_rdev = encode_dev(stat->rdev);
  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. #ifdef STAT_HAVE_NSEC
  217. tmp.st_atime_nsec = stat->atime.tv_nsec;
  218. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  219. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  220. #endif
  221. tmp.st_blocks = stat->blocks;
  222. tmp.st_blksize = stat->blksize;
  223. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  224. }
  225. SYSCALL_DEFINE2(newstat, const char __user *, filename,
  226. struct stat __user *, statbuf)
  227. {
  228. struct kstat stat;
  229. int error = vfs_stat(filename, &stat);
  230. if (error)
  231. return error;
  232. return cp_new_stat(&stat, statbuf);
  233. }
  234. SYSCALL_DEFINE2(newlstat, const char __user *, filename,
  235. struct stat __user *, statbuf)
  236. {
  237. struct kstat stat;
  238. int error;
  239. error = vfs_lstat(filename, &stat);
  240. if (error)
  241. return error;
  242. return cp_new_stat(&stat, statbuf);
  243. }
  244. #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
  245. SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
  246. struct stat __user *, statbuf, int, flag)
  247. {
  248. struct kstat stat;
  249. int error;
  250. error = vfs_fstatat(dfd, filename, &stat, flag);
  251. if (error)
  252. return error;
  253. return cp_new_stat(&stat, statbuf);
  254. }
  255. #endif
  256. SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
  257. {
  258. struct kstat stat;
  259. int error = vfs_fstat(fd, &stat);
  260. if (!error)
  261. error = cp_new_stat(&stat, statbuf);
  262. return error;
  263. }
  264. SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
  265. char __user *, buf, int, bufsiz)
  266. {
  267. struct path path;
  268. int error;
  269. int empty = 0;
  270. unsigned int lookup_flags = LOOKUP_EMPTY;
  271. if (bufsiz <= 0)
  272. return -EINVAL;
  273. retry:
  274. error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
  275. if (!error) {
  276. struct inode *inode = d_backing_inode(path.dentry);
  277. error = empty ? -ENOENT : -EINVAL;
  278. if (inode->i_op->readlink) {
  279. error = security_inode_readlink(path.dentry);
  280. if (!error) {
  281. touch_atime(&path);
  282. error = inode->i_op->readlink(path.dentry,
  283. buf, bufsiz);
  284. }
  285. }
  286. path_put(&path);
  287. if (retry_estale(error, lookup_flags)) {
  288. lookup_flags |= LOOKUP_REVAL;
  289. goto retry;
  290. }
  291. }
  292. return error;
  293. }
  294. SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
  295. int, bufsiz)
  296. {
  297. return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
  298. }
  299. /* ---------- LFS-64 ----------- */
  300. #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
  301. #ifndef INIT_STRUCT_STAT64_PADDING
  302. # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
  303. #endif
  304. static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
  305. {
  306. struct stat64 tmp;
  307. INIT_STRUCT_STAT64_PADDING(tmp);
  308. #ifdef CONFIG_MIPS
  309. /* mips has weird padding, so we don't get 64 bits there */
  310. tmp.st_dev = new_encode_dev(stat->dev);
  311. tmp.st_rdev = new_encode_dev(stat->rdev);
  312. #else
  313. tmp.st_dev = huge_encode_dev(stat->dev);
  314. tmp.st_rdev = huge_encode_dev(stat->rdev);
  315. #endif
  316. tmp.st_ino = stat->ino;
  317. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  318. return -EOVERFLOW;
  319. #ifdef STAT64_HAS_BROKEN_ST_INO
  320. tmp.__st_ino = stat->ino;
  321. #endif
  322. tmp.st_mode = stat->mode;
  323. tmp.st_nlink = stat->nlink;
  324. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  325. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  326. tmp.st_atime = stat->atime.tv_sec;
  327. tmp.st_atime_nsec = stat->atime.tv_nsec;
  328. tmp.st_mtime = stat->mtime.tv_sec;
  329. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  330. tmp.st_ctime = stat->ctime.tv_sec;
  331. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  332. tmp.st_size = stat->size;
  333. tmp.st_blocks = stat->blocks;
  334. tmp.st_blksize = stat->blksize;
  335. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  336. }
  337. SYSCALL_DEFINE2(stat64, const char __user *, filename,
  338. struct stat64 __user *, statbuf)
  339. {
  340. struct kstat stat;
  341. int error = vfs_stat(filename, &stat);
  342. if (!error)
  343. error = cp_new_stat64(&stat, statbuf);
  344. return error;
  345. }
  346. SYSCALL_DEFINE2(lstat64, const char __user *, filename,
  347. struct stat64 __user *, statbuf)
  348. {
  349. struct kstat stat;
  350. int error = vfs_lstat(filename, &stat);
  351. if (!error)
  352. error = cp_new_stat64(&stat, statbuf);
  353. return error;
  354. }
  355. SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
  356. {
  357. struct kstat stat;
  358. int error = vfs_fstat(fd, &stat);
  359. if (!error)
  360. error = cp_new_stat64(&stat, statbuf);
  361. return error;
  362. }
  363. SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
  364. struct stat64 __user *, statbuf, int, flag)
  365. {
  366. struct kstat stat;
  367. int error;
  368. error = vfs_fstatat(dfd, filename, &stat, flag);
  369. if (error)
  370. return error;
  371. return cp_new_stat64(&stat, statbuf);
  372. }
  373. #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
  374. /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
  375. void __inode_add_bytes(struct inode *inode, loff_t bytes)
  376. {
  377. inode->i_blocks += bytes >> 9;
  378. bytes &= 511;
  379. inode->i_bytes += bytes;
  380. if (inode->i_bytes >= 512) {
  381. inode->i_blocks++;
  382. inode->i_bytes -= 512;
  383. }
  384. }
  385. EXPORT_SYMBOL(__inode_add_bytes);
  386. void inode_add_bytes(struct inode *inode, loff_t bytes)
  387. {
  388. spin_lock(&inode->i_lock);
  389. __inode_add_bytes(inode, bytes);
  390. spin_unlock(&inode->i_lock);
  391. }
  392. EXPORT_SYMBOL(inode_add_bytes);
  393. void __inode_sub_bytes(struct inode *inode, loff_t bytes)
  394. {
  395. inode->i_blocks -= bytes >> 9;
  396. bytes &= 511;
  397. if (inode->i_bytes < bytes) {
  398. inode->i_blocks--;
  399. inode->i_bytes += 512;
  400. }
  401. inode->i_bytes -= bytes;
  402. }
  403. EXPORT_SYMBOL(__inode_sub_bytes);
  404. void inode_sub_bytes(struct inode *inode, loff_t bytes)
  405. {
  406. spin_lock(&inode->i_lock);
  407. __inode_sub_bytes(inode, bytes);
  408. spin_unlock(&inode->i_lock);
  409. }
  410. EXPORT_SYMBOL(inode_sub_bytes);
  411. loff_t inode_get_bytes(struct inode *inode)
  412. {
  413. loff_t ret;
  414. spin_lock(&inode->i_lock);
  415. ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
  416. spin_unlock(&inode->i_lock);
  417. return ret;
  418. }
  419. EXPORT_SYMBOL(inode_get_bytes);
  420. void inode_set_bytes(struct inode *inode, loff_t bytes)
  421. {
  422. /* Caller is here responsible for sufficient locking
  423. * (ie. inode->i_lock) */
  424. inode->i_blocks = bytes >> 9;
  425. inode->i_bytes = bytes & 511;
  426. }
  427. EXPORT_SYMBOL(inode_set_bytes);