stat.c 11 KB

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