statfs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. struct file *file = fget_raw(fd);
  80. int error = -EBADF;
  81. if (file) {
  82. error = vfs_statfs(&file->f_path, st);
  83. fput(file);
  84. }
  85. return error;
  86. }
  87. static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
  88. {
  89. struct statfs buf;
  90. if (sizeof(buf) == sizeof(*st))
  91. memcpy(&buf, st, sizeof(*st));
  92. else {
  93. if (sizeof buf.f_blocks == 4) {
  94. if ((st->f_blocks | st->f_bfree | st->f_bavail |
  95. st->f_bsize | st->f_frsize) &
  96. 0xffffffff00000000ULL)
  97. return -EOVERFLOW;
  98. /*
  99. * f_files and f_ffree may be -1; it's okay to stuff
  100. * that into 32 bits
  101. */
  102. if (st->f_files != -1 &&
  103. (st->f_files & 0xffffffff00000000ULL))
  104. return -EOVERFLOW;
  105. if (st->f_ffree != -1 &&
  106. (st->f_ffree & 0xffffffff00000000ULL))
  107. return -EOVERFLOW;
  108. }
  109. buf.f_type = st->f_type;
  110. buf.f_bsize = st->f_bsize;
  111. buf.f_blocks = st->f_blocks;
  112. buf.f_bfree = st->f_bfree;
  113. buf.f_bavail = st->f_bavail;
  114. buf.f_files = st->f_files;
  115. buf.f_ffree = st->f_ffree;
  116. buf.f_fsid = st->f_fsid;
  117. buf.f_namelen = st->f_namelen;
  118. buf.f_frsize = st->f_frsize;
  119. buf.f_flags = st->f_flags;
  120. memset(buf.f_spare, 0, sizeof(buf.f_spare));
  121. }
  122. if (copy_to_user(p, &buf, sizeof(buf)))
  123. return -EFAULT;
  124. return 0;
  125. }
  126. static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
  127. {
  128. struct statfs64 buf;
  129. if (sizeof(buf) == sizeof(*st))
  130. memcpy(&buf, st, sizeof(*st));
  131. else {
  132. buf.f_type = st->f_type;
  133. buf.f_bsize = st->f_bsize;
  134. buf.f_blocks = st->f_blocks;
  135. buf.f_bfree = st->f_bfree;
  136. buf.f_bavail = st->f_bavail;
  137. buf.f_files = st->f_files;
  138. buf.f_ffree = st->f_ffree;
  139. buf.f_fsid = st->f_fsid;
  140. buf.f_namelen = st->f_namelen;
  141. buf.f_frsize = st->f_frsize;
  142. buf.f_flags = st->f_flags;
  143. memset(buf.f_spare, 0, sizeof(buf.f_spare));
  144. }
  145. if (copy_to_user(p, &buf, sizeof(buf)))
  146. return -EFAULT;
  147. return 0;
  148. }
  149. SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
  150. {
  151. struct kstatfs st;
  152. int error = user_statfs(pathname, &st);
  153. if (!error)
  154. error = do_statfs_native(&st, buf);
  155. return error;
  156. }
  157. SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
  158. {
  159. struct kstatfs st;
  160. int error;
  161. if (sz != sizeof(*buf))
  162. return -EINVAL;
  163. error = user_statfs(pathname, &st);
  164. if (!error)
  165. error = do_statfs64(&st, buf);
  166. return error;
  167. }
  168. SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
  169. {
  170. struct kstatfs st;
  171. int error = fd_statfs(fd, &st);
  172. if (!error)
  173. error = do_statfs_native(&st, buf);
  174. return error;
  175. }
  176. SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
  177. {
  178. struct kstatfs st;
  179. int error;
  180. if (sz != sizeof(*buf))
  181. return -EINVAL;
  182. error = fd_statfs(fd, &st);
  183. if (!error)
  184. error = do_statfs64(&st, buf);
  185. return error;
  186. }
  187. int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
  188. {
  189. struct super_block *s = user_get_super(dev);
  190. int err;
  191. if (!s)
  192. return -EINVAL;
  193. err = statfs_by_dentry(s->s_root, sbuf);
  194. drop_super(s);
  195. return err;
  196. }
  197. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  198. {
  199. struct ustat tmp;
  200. struct kstatfs sbuf;
  201. int err = vfs_ustat(new_decode_dev(dev), &sbuf);
  202. if (err)
  203. return err;
  204. memset(&tmp,0,sizeof(struct ustat));
  205. tmp.f_tfree = sbuf.f_bfree;
  206. tmp.f_tinode = sbuf.f_ffree;
  207. return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
  208. }