quota.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Quota code necessary even when VFS quota support is not compiled
  3. * into the kernel. The interesting stuff is over in dquot.c, here
  4. * we have symbols for initial quotactl(2) handling, the sysctl(2)
  5. * variables, etc - things needed even when quota support disabled.
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/namei.h>
  9. #include <linux/slab.h>
  10. #include <asm/current.h>
  11. #include <asm/uaccess.h>
  12. #include <linux/kernel.h>
  13. #include <linux/security.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/capability.h>
  17. #include <linux/quotaops.h>
  18. #include <linux/types.h>
  19. #include <linux/writeback.h>
  20. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  21. qid_t id)
  22. {
  23. switch (cmd) {
  24. /* these commands do not require any special privilegues */
  25. case Q_GETFMT:
  26. case Q_SYNC:
  27. case Q_GETINFO:
  28. case Q_XGETQSTAT:
  29. case Q_XQUOTASYNC:
  30. break;
  31. /* allow to query information for dquots we "own" */
  32. case Q_GETQUOTA:
  33. case Q_XGETQUOTA:
  34. if ((type == USRQUOTA && current_euid() == id) ||
  35. (type == GRPQUOTA && in_egroup_p(id)))
  36. break;
  37. /*FALLTHROUGH*/
  38. default:
  39. if (!capable(CAP_SYS_ADMIN))
  40. return -EPERM;
  41. }
  42. return security_quotactl(cmd, type, id, sb);
  43. }
  44. static int quota_sync_all(int type)
  45. {
  46. struct super_block *sb;
  47. int ret;
  48. if (type >= MAXQUOTAS)
  49. return -EINVAL;
  50. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  51. if (ret)
  52. return ret;
  53. spin_lock(&sb_lock);
  54. restart:
  55. list_for_each_entry(sb, &super_blocks, s_list) {
  56. if (!sb->s_qcop || !sb->s_qcop->quota_sync)
  57. continue;
  58. sb->s_count++;
  59. spin_unlock(&sb_lock);
  60. down_read(&sb->s_umount);
  61. if (sb->s_root)
  62. sb->s_qcop->quota_sync(sb, type, 1);
  63. up_read(&sb->s_umount);
  64. spin_lock(&sb_lock);
  65. if (__put_super_and_need_restart(sb))
  66. goto restart;
  67. }
  68. spin_unlock(&sb_lock);
  69. return 0;
  70. }
  71. static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
  72. void __user *addr)
  73. {
  74. char *pathname;
  75. int ret = -ENOSYS;
  76. pathname = getname(addr);
  77. if (IS_ERR(pathname))
  78. return PTR_ERR(pathname);
  79. if (sb->s_qcop->quota_on)
  80. ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
  81. putname(pathname);
  82. return ret;
  83. }
  84. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  85. {
  86. __u32 fmt;
  87. down_read(&sb_dqopt(sb)->dqptr_sem);
  88. if (!sb_has_quota_active(sb, type)) {
  89. up_read(&sb_dqopt(sb)->dqptr_sem);
  90. return -ESRCH;
  91. }
  92. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  93. up_read(&sb_dqopt(sb)->dqptr_sem);
  94. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  95. return -EFAULT;
  96. return 0;
  97. }
  98. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  99. {
  100. struct if_dqinfo info;
  101. int ret;
  102. if (!sb_has_quota_active(sb, type))
  103. return -ESRCH;
  104. if (!sb->s_qcop->get_info)
  105. return -ENOSYS;
  106. ret = sb->s_qcop->get_info(sb, type, &info);
  107. if (!ret && copy_to_user(addr, &info, sizeof(info)))
  108. return -EFAULT;
  109. return ret;
  110. }
  111. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  112. {
  113. struct if_dqinfo info;
  114. if (copy_from_user(&info, addr, sizeof(info)))
  115. return -EFAULT;
  116. if (!sb_has_quota_active(sb, type))
  117. return -ESRCH;
  118. if (!sb->s_qcop->set_info)
  119. return -ENOSYS;
  120. return sb->s_qcop->set_info(sb, type, &info);
  121. }
  122. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  123. void __user *addr)
  124. {
  125. struct if_dqblk idq;
  126. int ret;
  127. if (!sb_has_quota_active(sb, type))
  128. return -ESRCH;
  129. if (!sb->s_qcop->get_dqblk)
  130. return -ENOSYS;
  131. ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
  132. if (ret)
  133. return ret;
  134. if (copy_to_user(addr, &idq, sizeof(idq)))
  135. return -EFAULT;
  136. return 0;
  137. }
  138. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  139. void __user *addr)
  140. {
  141. struct if_dqblk idq;
  142. if (copy_from_user(&idq, addr, sizeof(idq)))
  143. return -EFAULT;
  144. if (!sb_has_quota_active(sb, type))
  145. return -ESRCH;
  146. if (!sb->s_qcop->set_dqblk)
  147. return -ENOSYS;
  148. return sb->s_qcop->set_dqblk(sb, type, id, &idq);
  149. }
  150. static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
  151. {
  152. __u32 flags;
  153. if (copy_from_user(&flags, addr, sizeof(flags)))
  154. return -EFAULT;
  155. if (!sb->s_qcop->set_xstate)
  156. return -ENOSYS;
  157. return sb->s_qcop->set_xstate(sb, flags, cmd);
  158. }
  159. static int quota_getxstate(struct super_block *sb, void __user *addr)
  160. {
  161. struct fs_quota_stat fqs;
  162. int ret;
  163. if (!sb->s_qcop->get_xstate)
  164. return -ENOSYS;
  165. ret = sb->s_qcop->get_xstate(sb, &fqs);
  166. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  167. return -EFAULT;
  168. return ret;
  169. }
  170. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  171. void __user *addr)
  172. {
  173. struct fs_disk_quota fdq;
  174. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  175. return -EFAULT;
  176. if (!sb->s_qcop->set_xquota)
  177. return -ENOSYS;
  178. return sb->s_qcop->set_xquota(sb, type, id, &fdq);
  179. }
  180. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  181. void __user *addr)
  182. {
  183. struct fs_disk_quota fdq;
  184. int ret;
  185. if (!sb->s_qcop->get_xquota)
  186. return -ENOSYS;
  187. ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
  188. if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
  189. return -EFAULT;
  190. return ret;
  191. }
  192. /* Copy parameters and call proper function */
  193. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  194. void __user *addr)
  195. {
  196. int ret;
  197. if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
  198. return -EINVAL;
  199. if (!sb->s_qcop)
  200. return -ENOSYS;
  201. ret = check_quotactl_permission(sb, type, cmd, id);
  202. if (ret < 0)
  203. return ret;
  204. switch (cmd) {
  205. case Q_QUOTAON:
  206. return quota_quotaon(sb, type, cmd, id, addr);
  207. case Q_QUOTAOFF:
  208. if (!sb->s_qcop->quota_off)
  209. return -ENOSYS;
  210. return sb->s_qcop->quota_off(sb, type, 0);
  211. case Q_GETFMT:
  212. return quota_getfmt(sb, type, addr);
  213. case Q_GETINFO:
  214. return quota_getinfo(sb, type, addr);
  215. case Q_SETINFO:
  216. return quota_setinfo(sb, type, addr);
  217. case Q_GETQUOTA:
  218. return quota_getquota(sb, type, id, addr);
  219. case Q_SETQUOTA:
  220. return quota_setquota(sb, type, id, addr);
  221. case Q_SYNC:
  222. if (!sb->s_qcop->quota_sync)
  223. return -ENOSYS;
  224. return sb->s_qcop->quota_sync(sb, type, 1);
  225. case Q_XQUOTAON:
  226. case Q_XQUOTAOFF:
  227. case Q_XQUOTARM:
  228. return quota_setxstate(sb, cmd, addr);
  229. case Q_XGETQSTAT:
  230. return quota_getxstate(sb, addr);
  231. case Q_XSETQLIM:
  232. return quota_setxquota(sb, type, id, addr);
  233. case Q_XGETQUOTA:
  234. return quota_getxquota(sb, type, id, addr);
  235. case Q_XQUOTASYNC:
  236. /* caller already holds s_umount */
  237. if (sb->s_flags & MS_RDONLY)
  238. return -EROFS;
  239. writeback_inodes_sb(sb);
  240. return 0;
  241. default:
  242. return -EINVAL;
  243. }
  244. }
  245. /*
  246. * look up a superblock on which quota ops will be performed
  247. * - use the name of a block device to find the superblock thereon
  248. */
  249. static struct super_block *quotactl_block(const char __user *special)
  250. {
  251. #ifdef CONFIG_BLOCK
  252. struct block_device *bdev;
  253. struct super_block *sb;
  254. char *tmp = getname(special);
  255. if (IS_ERR(tmp))
  256. return ERR_CAST(tmp);
  257. bdev = lookup_bdev(tmp);
  258. putname(tmp);
  259. if (IS_ERR(bdev))
  260. return ERR_CAST(bdev);
  261. sb = get_super(bdev);
  262. bdput(bdev);
  263. if (!sb)
  264. return ERR_PTR(-ENODEV);
  265. return sb;
  266. #else
  267. return ERR_PTR(-ENODEV);
  268. #endif
  269. }
  270. /*
  271. * This is the system call interface. This communicates with
  272. * the user-level programs. Currently this only supports diskquota
  273. * calls. Maybe we need to add the process quotas etc. in the future,
  274. * but we probably should use rlimits for that.
  275. */
  276. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  277. qid_t, id, void __user *, addr)
  278. {
  279. uint cmds, type;
  280. struct super_block *sb = NULL;
  281. int ret;
  282. cmds = cmd >> SUBCMDSHIFT;
  283. type = cmd & SUBCMDMASK;
  284. /*
  285. * As a special case Q_SYNC can be called without a specific device.
  286. * It will iterate all superblocks that have quota enabled and call
  287. * the sync action on each of them.
  288. */
  289. if (!special) {
  290. if (cmds == Q_SYNC)
  291. return quota_sync_all(type);
  292. return -ENODEV;
  293. }
  294. sb = quotactl_block(special);
  295. if (IS_ERR(sb))
  296. return PTR_ERR(sb);
  297. ret = do_quotactl(sb, type, cmds, id, addr);
  298. drop_super(sb);
  299. return ret;
  300. }