stat.c 11 KB

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