stat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 path *path, struct kstat *stat)
  36. {
  37. struct inode *inode = path->dentry->d_inode;
  38. int retval;
  39. retval = security_inode_getattr(path->mnt, path->dentry);
  40. if (retval)
  41. return retval;
  42. if (inode->i_op->getattr)
  43. return inode->i_op->getattr(path->mnt, path->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 fd f = fdget_raw(fd);
  51. int error = -EBADF;
  52. if (f.file) {
  53. error = vfs_getattr(&f.file->f_path, stat);
  54. fdput(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. unsigned 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_EMPTY_PATH)
  71. lookup_flags |= LOOKUP_EMPTY;
  72. retry:
  73. error = user_path_at(dfd, filename, lookup_flags, &path);
  74. if (error)
  75. goto out;
  76. error = vfs_getattr(&path, stat);
  77. path_put(&path);
  78. if (retry_estale(error, lookup_flags)) {
  79. lookup_flags |= LOOKUP_REVAL;
  80. goto retry;
  81. }
  82. out:
  83. return error;
  84. }
  85. EXPORT_SYMBOL(vfs_fstatat);
  86. int vfs_stat(const char __user *name, struct kstat *stat)
  87. {
  88. return vfs_fstatat(AT_FDCWD, name, stat, 0);
  89. }
  90. EXPORT_SYMBOL(vfs_stat);
  91. int vfs_lstat(const char __user *name, struct kstat *stat)
  92. {
  93. return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
  94. }
  95. EXPORT_SYMBOL(vfs_lstat);
  96. #ifdef __ARCH_WANT_OLD_STAT
  97. /*
  98. * For backward compatibility? Maybe this should be moved
  99. * into arch/i386 instead?
  100. */
  101. static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
  102. {
  103. static int warncount = 5;
  104. struct __old_kernel_stat tmp;
  105. if (warncount > 0) {
  106. warncount--;
  107. printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
  108. current->comm);
  109. } else if (warncount < 0) {
  110. /* it's laughable, but... */
  111. warncount = 0;
  112. }
  113. memset(&tmp, 0, sizeof(struct __old_kernel_stat));
  114. tmp.st_dev = old_encode_dev(stat->dev);
  115. tmp.st_ino = stat->ino;
  116. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  117. return -EOVERFLOW;
  118. tmp.st_mode = stat->mode;
  119. tmp.st_nlink = stat->nlink;
  120. if (tmp.st_nlink != stat->nlink)
  121. return -EOVERFLOW;
  122. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  123. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  124. tmp.st_rdev = old_encode_dev(stat->rdev);
  125. #if BITS_PER_LONG == 32
  126. if (stat->size > MAX_NON_LFS)
  127. return -EOVERFLOW;
  128. #endif
  129. tmp.st_size = stat->size;
  130. tmp.st_atime = stat->atime.tv_sec;
  131. tmp.st_mtime = stat->mtime.tv_sec;
  132. tmp.st_ctime = stat->ctime.tv_sec;
  133. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  134. }
  135. SYSCALL_DEFINE2(stat, const char __user *, filename,
  136. struct __old_kernel_stat __user *, statbuf)
  137. {
  138. struct kstat stat;
  139. int error;
  140. error = vfs_stat(filename, &stat);
  141. if (error)
  142. return error;
  143. return cp_old_stat(&stat, statbuf);
  144. }
  145. SYSCALL_DEFINE2(lstat, const char __user *, filename,
  146. struct __old_kernel_stat __user *, statbuf)
  147. {
  148. struct kstat stat;
  149. int error;
  150. error = vfs_lstat(filename, &stat);
  151. if (error)
  152. return error;
  153. return cp_old_stat(&stat, statbuf);
  154. }
  155. SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
  156. {
  157. struct kstat stat;
  158. int error = vfs_fstat(fd, &stat);
  159. if (!error)
  160. error = cp_old_stat(&stat, statbuf);
  161. return error;
  162. }
  163. #endif /* __ARCH_WANT_OLD_STAT */
  164. #if BITS_PER_LONG == 32
  165. # define choose_32_64(a,b) a
  166. #else
  167. # define choose_32_64(a,b) b
  168. #endif
  169. #define valid_dev(x) choose_32_64(old_valid_dev,new_valid_dev)(x)
  170. #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
  171. #ifndef INIT_STRUCT_STAT_PADDING
  172. # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
  173. #endif
  174. static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
  175. {
  176. struct stat tmp;
  177. if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
  178. return -EOVERFLOW;
  179. #if BITS_PER_LONG == 32
  180. if (stat->size > MAX_NON_LFS)
  181. return -EOVERFLOW;
  182. #endif
  183. INIT_STRUCT_STAT_PADDING(tmp);
  184. tmp.st_dev = encode_dev(stat->dev);
  185. tmp.st_ino = stat->ino;
  186. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  187. return -EOVERFLOW;
  188. tmp.st_mode = stat->mode;
  189. tmp.st_nlink = stat->nlink;
  190. if (tmp.st_nlink != stat->nlink)
  191. return -EOVERFLOW;
  192. SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
  193. SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
  194. tmp.st_rdev = encode_dev(stat->rdev);
  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. unsigned int lookup_flags = LOOKUP_EMPTY;
  254. if (bufsiz <= 0)
  255. return -EINVAL;
  256. retry:
  257. error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
  258. if (!error) {
  259. struct inode *inode = path.dentry->d_inode;
  260. error = empty ? -ENOENT : -EINVAL;
  261. if (inode->i_op->readlink) {
  262. error = security_inode_readlink(path.dentry);
  263. if (!error) {
  264. touch_atime(&path);
  265. error = inode->i_op->readlink(path.dentry,
  266. buf, bufsiz);
  267. }
  268. }
  269. path_put(&path);
  270. if (retry_estale(error, lookup_flags)) {
  271. lookup_flags |= LOOKUP_REVAL;
  272. goto retry;
  273. }
  274. }
  275. return error;
  276. }
  277. SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
  278. int, bufsiz)
  279. {
  280. return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
  281. }
  282. /* ---------- LFS-64 ----------- */
  283. #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
  284. #ifndef INIT_STRUCT_STAT64_PADDING
  285. # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
  286. #endif
  287. static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
  288. {
  289. struct stat64 tmp;
  290. INIT_STRUCT_STAT64_PADDING(tmp);
  291. #ifdef CONFIG_MIPS
  292. /* mips has weird padding, so we don't get 64 bits there */
  293. if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
  294. return -EOVERFLOW;
  295. tmp.st_dev = new_encode_dev(stat->dev);
  296. tmp.st_rdev = new_encode_dev(stat->rdev);
  297. #else
  298. tmp.st_dev = huge_encode_dev(stat->dev);
  299. tmp.st_rdev = huge_encode_dev(stat->rdev);
  300. #endif
  301. tmp.st_ino = stat->ino;
  302. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  303. return -EOVERFLOW;
  304. #ifdef STAT64_HAS_BROKEN_ST_INO
  305. tmp.__st_ino = stat->ino;
  306. #endif
  307. tmp.st_mode = stat->mode;
  308. tmp.st_nlink = stat->nlink;
  309. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  310. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  311. tmp.st_atime = stat->atime.tv_sec;
  312. tmp.st_atime_nsec = stat->atime.tv_nsec;
  313. tmp.st_mtime = stat->mtime.tv_sec;
  314. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  315. tmp.st_ctime = stat->ctime.tv_sec;
  316. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  317. tmp.st_size = stat->size;
  318. tmp.st_blocks = stat->blocks;
  319. tmp.st_blksize = stat->blksize;
  320. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  321. }
  322. SYSCALL_DEFINE2(stat64, const char __user *, filename,
  323. struct stat64 __user *, statbuf)
  324. {
  325. struct kstat stat;
  326. int error = vfs_stat(filename, &stat);
  327. if (!error)
  328. error = cp_new_stat64(&stat, statbuf);
  329. return error;
  330. }
  331. SYSCALL_DEFINE2(lstat64, const char __user *, filename,
  332. struct stat64 __user *, statbuf)
  333. {
  334. struct kstat stat;
  335. int error = vfs_lstat(filename, &stat);
  336. if (!error)
  337. error = cp_new_stat64(&stat, statbuf);
  338. return error;
  339. }
  340. SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
  341. {
  342. struct kstat stat;
  343. int error = vfs_fstat(fd, &stat);
  344. if (!error)
  345. error = cp_new_stat64(&stat, statbuf);
  346. return error;
  347. }
  348. SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
  349. struct stat64 __user *, statbuf, int, flag)
  350. {
  351. struct kstat stat;
  352. int error;
  353. error = vfs_fstatat(dfd, filename, &stat, flag);
  354. if (error)
  355. return error;
  356. return cp_new_stat64(&stat, statbuf);
  357. }
  358. #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
  359. /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
  360. void __inode_add_bytes(struct inode *inode, loff_t bytes)
  361. {
  362. inode->i_blocks += bytes >> 9;
  363. bytes &= 511;
  364. inode->i_bytes += bytes;
  365. if (inode->i_bytes >= 512) {
  366. inode->i_blocks++;
  367. inode->i_bytes -= 512;
  368. }
  369. }
  370. void inode_add_bytes(struct inode *inode, loff_t bytes)
  371. {
  372. spin_lock(&inode->i_lock);
  373. __inode_add_bytes(inode, bytes);
  374. spin_unlock(&inode->i_lock);
  375. }
  376. EXPORT_SYMBOL(inode_add_bytes);
  377. void inode_sub_bytes(struct inode *inode, loff_t bytes)
  378. {
  379. spin_lock(&inode->i_lock);
  380. inode->i_blocks -= bytes >> 9;
  381. bytes &= 511;
  382. if (inode->i_bytes < bytes) {
  383. inode->i_blocks--;
  384. inode->i_bytes += 512;
  385. }
  386. inode->i_bytes -= bytes;
  387. spin_unlock(&inode->i_lock);
  388. }
  389. EXPORT_SYMBOL(inode_sub_bytes);
  390. loff_t inode_get_bytes(struct inode *inode)
  391. {
  392. loff_t ret;
  393. spin_lock(&inode->i_lock);
  394. ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
  395. spin_unlock(&inode->i_lock);
  396. return ret;
  397. }
  398. EXPORT_SYMBOL(inode_get_bytes);
  399. void inode_set_bytes(struct inode *inode, loff_t bytes)
  400. {
  401. /* Caller is here responsible for sufficient locking
  402. * (ie. inode->i_lock) */
  403. inode->i_blocks = bytes >> 9;
  404. inode->i_bytes = bytes & 511;
  405. }
  406. EXPORT_SYMBOL(inode_set_bytes);