statfs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <linux/syscalls.h>
  2. #include <linux/export.h>
  3. #include <linux/fs.h>
  4. #include <linux/file.h>
  5. #include <linux/mount.h>
  6. #include <linux/namei.h>
  7. #include <linux/statfs.h>
  8. #include <linux/security.h>
  9. #include <linux/uaccess.h>
  10. #include "internal.h"
  11. static int flags_by_mnt(int mnt_flags)
  12. {
  13. int flags = 0;
  14. if (mnt_flags & MNT_READONLY)
  15. flags |= ST_RDONLY;
  16. if (mnt_flags & MNT_NOSUID)
  17. flags |= ST_NOSUID;
  18. if (mnt_flags & MNT_NODEV)
  19. flags |= ST_NODEV;
  20. if (mnt_flags & MNT_NOEXEC)
  21. flags |= ST_NOEXEC;
  22. if (mnt_flags & MNT_NOATIME)
  23. flags |= ST_NOATIME;
  24. if (mnt_flags & MNT_NODIRATIME)
  25. flags |= ST_NODIRATIME;
  26. if (mnt_flags & MNT_RELATIME)
  27. flags |= ST_RELATIME;
  28. return flags;
  29. }
  30. static int flags_by_sb(int s_flags)
  31. {
  32. int flags = 0;
  33. if (s_flags & MS_SYNCHRONOUS)
  34. flags |= ST_SYNCHRONOUS;
  35. if (s_flags & MS_MANDLOCK)
  36. flags |= ST_MANDLOCK;
  37. return flags;
  38. }
  39. static int calculate_f_flags(struct vfsmount *mnt)
  40. {
  41. return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
  42. flags_by_sb(mnt->mnt_sb->s_flags);
  43. }
  44. static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
  45. {
  46. int retval;
  47. if (!dentry->d_sb->s_op->statfs)
  48. return -ENOSYS;
  49. memset(buf, 0, sizeof(*buf));
  50. retval = security_sb_statfs(dentry);
  51. if (retval)
  52. return retval;
  53. retval = dentry->d_sb->s_op->statfs(dentry, buf);
  54. if (retval == 0 && buf->f_frsize == 0)
  55. buf->f_frsize = buf->f_bsize;
  56. return retval;
  57. }
  58. int vfs_statfs(struct path *path, struct kstatfs *buf)
  59. {
  60. int error;
  61. error = statfs_by_dentry(path->dentry, buf);
  62. if (!error)
  63. buf->f_flags = calculate_f_flags(path->mnt);
  64. return error;
  65. }
  66. EXPORT_SYMBOL(vfs_statfs);
  67. int user_statfs(const char __user *pathname, struct kstatfs *st)
  68. {
  69. struct path path;
  70. int error = user_path_at(AT_FDCWD, pathname, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  71. if (!error) {
  72. error = vfs_statfs(&path, st);
  73. path_put(&path);
  74. }
  75. return error;
  76. }
  77. int fd_statfs(int fd, struct kstatfs *st)
  78. {
  79. int fput_needed;
  80. struct file *file = fget_raw_light(fd, &fput_needed);
  81. int error = -EBADF;
  82. if (file) {
  83. error = vfs_statfs(&file->f_path, st);
  84. fput_light(file, fput_needed);
  85. }
  86. return error;
  87. }
  88. static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
  89. {
  90. struct statfs buf;
  91. if (sizeof(buf) == sizeof(*st))
  92. memcpy(&buf, st, sizeof(*st));
  93. else {
  94. if (sizeof buf.f_blocks == 4) {
  95. if ((st->f_blocks | st->f_bfree | st->f_bavail |
  96. st->f_bsize | st->f_frsize) &
  97. 0xffffffff00000000ULL)
  98. return -EOVERFLOW;
  99. /*
  100. * f_files and f_ffree may be -1; it's okay to stuff
  101. * that into 32 bits
  102. */
  103. if (st->f_files != -1 &&
  104. (st->f_files & 0xffffffff00000000ULL))
  105. return -EOVERFLOW;
  106. if (st->f_ffree != -1 &&
  107. (st->f_ffree & 0xffffffff00000000ULL))
  108. return -EOVERFLOW;
  109. }
  110. buf.f_type = st->f_type;
  111. buf.f_bsize = st->f_bsize;
  112. buf.f_blocks = st->f_blocks;
  113. buf.f_bfree = st->f_bfree;
  114. buf.f_bavail = st->f_bavail;
  115. buf.f_files = st->f_files;
  116. buf.f_ffree = st->f_ffree;
  117. buf.f_fsid = st->f_fsid;
  118. buf.f_namelen = st->f_namelen;
  119. buf.f_frsize = st->f_frsize;
  120. buf.f_flags = st->f_flags;
  121. memset(buf.f_spare, 0, sizeof(buf.f_spare));
  122. }
  123. if (copy_to_user(p, &buf, sizeof(buf)))
  124. return -EFAULT;
  125. return 0;
  126. }
  127. static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
  128. {
  129. struct statfs64 buf;
  130. if (sizeof(buf) == sizeof(*st))
  131. memcpy(&buf, st, sizeof(*st));
  132. else {
  133. buf.f_type = st->f_type;
  134. buf.f_bsize = st->f_bsize;
  135. buf.f_blocks = st->f_blocks;
  136. buf.f_bfree = st->f_bfree;
  137. buf.f_bavail = st->f_bavail;
  138. buf.f_files = st->f_files;
  139. buf.f_ffree = st->f_ffree;
  140. buf.f_fsid = st->f_fsid;
  141. buf.f_namelen = st->f_namelen;
  142. buf.f_frsize = st->f_frsize;
  143. buf.f_flags = st->f_flags;
  144. memset(buf.f_spare, 0, sizeof(buf.f_spare));
  145. }
  146. if (copy_to_user(p, &buf, sizeof(buf)))
  147. return -EFAULT;
  148. return 0;
  149. }
  150. SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
  151. {
  152. struct kstatfs st;
  153. int error = user_statfs(pathname, &st);
  154. if (!error)
  155. error = do_statfs_native(&st, buf);
  156. return error;
  157. }
  158. SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
  159. {
  160. struct kstatfs st;
  161. int error;
  162. if (sz != sizeof(*buf))
  163. return -EINVAL;
  164. error = user_statfs(pathname, &st);
  165. if (!error)
  166. error = do_statfs64(&st, buf);
  167. return error;
  168. }
  169. SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
  170. {
  171. struct kstatfs st;
  172. int error = fd_statfs(fd, &st);
  173. if (!error)
  174. error = do_statfs_native(&st, buf);
  175. return error;
  176. }
  177. SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
  178. {
  179. struct kstatfs st;
  180. int error;
  181. if (sz != sizeof(*buf))
  182. return -EINVAL;
  183. error = fd_statfs(fd, &st);
  184. if (!error)
  185. error = do_statfs64(&st, buf);
  186. return error;
  187. }
  188. int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
  189. {
  190. struct super_block *s = user_get_super(dev);
  191. int err;
  192. if (!s)
  193. return -EINVAL;
  194. err = statfs_by_dentry(s->s_root, sbuf);
  195. drop_super(s);
  196. return err;
  197. }
  198. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  199. {
  200. struct ustat tmp;
  201. struct kstatfs sbuf;
  202. int err = vfs_ustat(new_decode_dev(dev), &sbuf);
  203. if (err)
  204. return err;
  205. memset(&tmp,0,sizeof(struct ustat));
  206. tmp.f_tfree = sbuf.f_bfree;
  207. tmp.f_tinode = sbuf.f_ffree;
  208. return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
  209. }