stat.c 11 KB

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