stat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. /**
  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,new_valid_dev)(x)
  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. if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
  311. return -EOVERFLOW;
  312. tmp.st_dev = new_encode_dev(stat->dev);
  313. tmp.st_rdev = new_encode_dev(stat->rdev);
  314. #else
  315. tmp.st_dev = huge_encode_dev(stat->dev);
  316. tmp.st_rdev = huge_encode_dev(stat->rdev);
  317. #endif
  318. tmp.st_ino = stat->ino;
  319. if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
  320. return -EOVERFLOW;
  321. #ifdef STAT64_HAS_BROKEN_ST_INO
  322. tmp.__st_ino = stat->ino;
  323. #endif
  324. tmp.st_mode = stat->mode;
  325. tmp.st_nlink = stat->nlink;
  326. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  327. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  328. tmp.st_atime = stat->atime.tv_sec;
  329. tmp.st_atime_nsec = stat->atime.tv_nsec;
  330. tmp.st_mtime = stat->mtime.tv_sec;
  331. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  332. tmp.st_ctime = stat->ctime.tv_sec;
  333. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  334. tmp.st_size = stat->size;
  335. tmp.st_blocks = stat->blocks;
  336. tmp.st_blksize = stat->blksize;
  337. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  338. }
  339. SYSCALL_DEFINE2(stat64, const char __user *, filename,
  340. struct stat64 __user *, statbuf)
  341. {
  342. struct kstat stat;
  343. int error = vfs_stat(filename, &stat);
  344. if (!error)
  345. error = cp_new_stat64(&stat, statbuf);
  346. return error;
  347. }
  348. SYSCALL_DEFINE2(lstat64, const char __user *, filename,
  349. struct stat64 __user *, statbuf)
  350. {
  351. struct kstat stat;
  352. int error = vfs_lstat(filename, &stat);
  353. if (!error)
  354. error = cp_new_stat64(&stat, statbuf);
  355. return error;
  356. }
  357. SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
  358. {
  359. struct kstat stat;
  360. int error = vfs_fstat(fd, &stat);
  361. if (!error)
  362. error = cp_new_stat64(&stat, statbuf);
  363. return error;
  364. }
  365. SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
  366. struct stat64 __user *, statbuf, int, flag)
  367. {
  368. struct kstat stat;
  369. int error;
  370. error = vfs_fstatat(dfd, filename, &stat, flag);
  371. if (error)
  372. return error;
  373. return cp_new_stat64(&stat, statbuf);
  374. }
  375. #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
  376. /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
  377. void __inode_add_bytes(struct inode *inode, loff_t bytes)
  378. {
  379. inode->i_blocks += bytes >> 9;
  380. bytes &= 511;
  381. inode->i_bytes += bytes;
  382. if (inode->i_bytes >= 512) {
  383. inode->i_blocks++;
  384. inode->i_bytes -= 512;
  385. }
  386. }
  387. void inode_add_bytes(struct inode *inode, loff_t bytes)
  388. {
  389. spin_lock(&inode->i_lock);
  390. __inode_add_bytes(inode, bytes);
  391. spin_unlock(&inode->i_lock);
  392. }
  393. EXPORT_SYMBOL(inode_add_bytes);
  394. void __inode_sub_bytes(struct inode *inode, loff_t bytes)
  395. {
  396. inode->i_blocks -= bytes >> 9;
  397. bytes &= 511;
  398. if (inode->i_bytes < bytes) {
  399. inode->i_blocks--;
  400. inode->i_bytes += 512;
  401. }
  402. inode->i_bytes -= bytes;
  403. }
  404. EXPORT_SYMBOL(__inode_sub_bytes);
  405. void inode_sub_bytes(struct inode *inode, loff_t bytes)
  406. {
  407. spin_lock(&inode->i_lock);
  408. __inode_sub_bytes(inode, bytes);
  409. spin_unlock(&inode->i_lock);
  410. }
  411. EXPORT_SYMBOL(inode_sub_bytes);
  412. loff_t inode_get_bytes(struct inode *inode)
  413. {
  414. loff_t ret;
  415. spin_lock(&inode->i_lock);
  416. ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
  417. spin_unlock(&inode->i_lock);
  418. return ret;
  419. }
  420. EXPORT_SYMBOL(inode_get_bytes);
  421. void inode_set_bytes(struct inode *inode, loff_t bytes)
  422. {
  423. /* Caller is here responsible for sufficient locking
  424. * (ie. inode->i_lock) */
  425. inode->i_blocks = bytes >> 9;
  426. inode->i_bytes = bytes & 511;
  427. }
  428. EXPORT_SYMBOL(inode_set_bytes);