quota.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/smp_lock.h>
  14. #include <linux/security.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/buffer_head.h>
  17. /* Check validity of generic quotactl commands */
  18. static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
  19. {
  20. if (type >= MAXQUOTAS)
  21. return -EINVAL;
  22. if (!sb && cmd != Q_SYNC)
  23. return -ENODEV;
  24. /* Is operation supported? */
  25. if (sb && !sb->s_qcop)
  26. return -ENOSYS;
  27. switch (cmd) {
  28. case Q_GETFMT:
  29. break;
  30. case Q_QUOTAON:
  31. if (!sb->s_qcop->quota_on)
  32. return -ENOSYS;
  33. break;
  34. case Q_QUOTAOFF:
  35. if (!sb->s_qcop->quota_off)
  36. return -ENOSYS;
  37. break;
  38. case Q_SETINFO:
  39. if (!sb->s_qcop->set_info)
  40. return -ENOSYS;
  41. break;
  42. case Q_GETINFO:
  43. if (!sb->s_qcop->get_info)
  44. return -ENOSYS;
  45. break;
  46. case Q_SETQUOTA:
  47. if (!sb->s_qcop->set_dqblk)
  48. return -ENOSYS;
  49. break;
  50. case Q_GETQUOTA:
  51. if (!sb->s_qcop->get_dqblk)
  52. return -ENOSYS;
  53. break;
  54. case Q_SYNC:
  55. if (sb && !sb->s_qcop->quota_sync)
  56. return -ENOSYS;
  57. break;
  58. default:
  59. return -EINVAL;
  60. }
  61. /* Is quota turned on for commands which need it? */
  62. switch (cmd) {
  63. case Q_GETFMT:
  64. case Q_GETINFO:
  65. case Q_QUOTAOFF:
  66. case Q_SETINFO:
  67. case Q_SETQUOTA:
  68. case Q_GETQUOTA:
  69. /* This is just informative test so we are satisfied without a lock */
  70. if (!sb_has_quota_enabled(sb, type))
  71. return -ESRCH;
  72. }
  73. /* Check privileges */
  74. if (cmd == Q_GETQUOTA) {
  75. if (((type == USRQUOTA && current->euid != id) ||
  76. (type == GRPQUOTA && !in_egroup_p(id))) &&
  77. !capable(CAP_SYS_ADMIN))
  78. return -EPERM;
  79. }
  80. else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
  81. if (!capable(CAP_SYS_ADMIN))
  82. return -EPERM;
  83. return 0;
  84. }
  85. /* Check validity of XFS Quota Manager commands */
  86. static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
  87. {
  88. if (type >= XQM_MAXQUOTAS)
  89. return -EINVAL;
  90. if (!sb)
  91. return -ENODEV;
  92. if (!sb->s_qcop)
  93. return -ENOSYS;
  94. switch (cmd) {
  95. case Q_XQUOTAON:
  96. case Q_XQUOTAOFF:
  97. case Q_XQUOTARM:
  98. if (!sb->s_qcop->set_xstate)
  99. return -ENOSYS;
  100. break;
  101. case Q_XGETQSTAT:
  102. if (!sb->s_qcop->get_xstate)
  103. return -ENOSYS;
  104. break;
  105. case Q_XSETQLIM:
  106. if (!sb->s_qcop->set_xquota)
  107. return -ENOSYS;
  108. break;
  109. case Q_XGETQUOTA:
  110. if (!sb->s_qcop->get_xquota)
  111. return -ENOSYS;
  112. break;
  113. default:
  114. return -EINVAL;
  115. }
  116. /* Check privileges */
  117. if (cmd == Q_XGETQUOTA) {
  118. if (((type == XQM_USRQUOTA && current->euid != id) ||
  119. (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
  120. !capable(CAP_SYS_ADMIN))
  121. return -EPERM;
  122. } else if (cmd != Q_XGETQSTAT) {
  123. if (!capable(CAP_SYS_ADMIN))
  124. return -EPERM;
  125. }
  126. return 0;
  127. }
  128. static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
  129. {
  130. int error;
  131. if (XQM_COMMAND(cmd))
  132. error = xqm_quotactl_valid(sb, type, cmd, id);
  133. else
  134. error = generic_quotactl_valid(sb, type, cmd, id);
  135. if (!error)
  136. error = security_quotactl(cmd, type, id, sb);
  137. return error;
  138. }
  139. static void quota_sync_sb(struct super_block *sb, int type)
  140. {
  141. int cnt;
  142. struct inode *discard[MAXQUOTAS];
  143. sb->s_qcop->quota_sync(sb, type);
  144. /* This is not very clever (and fast) but currently I don't know about
  145. * any other simple way of getting quota data to disk and we must get
  146. * them there for userspace to be visible... */
  147. if (sb->s_op->sync_fs)
  148. sb->s_op->sync_fs(sb, 1);
  149. sync_blockdev(sb->s_bdev);
  150. /* Now when everything is written we can discard the pagecache so
  151. * that userspace sees the changes. We need i_sem and so we could
  152. * not do it inside dqonoff_sem. Moreover we need to be carefull
  153. * about races with quotaoff() (that is the reason why we have own
  154. * reference to inode). */
  155. down(&sb_dqopt(sb)->dqonoff_sem);
  156. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  157. discard[cnt] = NULL;
  158. if (type != -1 && cnt != type)
  159. continue;
  160. if (!sb_has_quota_enabled(sb, cnt))
  161. continue;
  162. discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
  163. }
  164. up(&sb_dqopt(sb)->dqonoff_sem);
  165. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  166. if (discard[cnt]) {
  167. down(&discard[cnt]->i_sem);
  168. truncate_inode_pages(&discard[cnt]->i_data, 0);
  169. up(&discard[cnt]->i_sem);
  170. iput(discard[cnt]);
  171. }
  172. }
  173. }
  174. void sync_dquots(struct super_block *sb, int type)
  175. {
  176. int cnt, dirty;
  177. if (sb) {
  178. if (sb->s_qcop->quota_sync)
  179. quota_sync_sb(sb, type);
  180. return;
  181. }
  182. spin_lock(&sb_lock);
  183. restart:
  184. list_for_each_entry(sb, &super_blocks, s_list) {
  185. /* This test just improves performance so it needn't be reliable... */
  186. for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
  187. if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
  188. && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
  189. dirty = 1;
  190. if (!dirty)
  191. continue;
  192. sb->s_count++;
  193. spin_unlock(&sb_lock);
  194. down_read(&sb->s_umount);
  195. if (sb->s_root && sb->s_qcop->quota_sync)
  196. quota_sync_sb(sb, type);
  197. up_read(&sb->s_umount);
  198. spin_lock(&sb_lock);
  199. if (__put_super_and_need_restart(sb))
  200. goto restart;
  201. }
  202. spin_unlock(&sb_lock);
  203. }
  204. /* Copy parameters and call proper function */
  205. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
  206. {
  207. int ret;
  208. switch (cmd) {
  209. case Q_QUOTAON: {
  210. char *pathname;
  211. if (IS_ERR(pathname = getname(addr)))
  212. return PTR_ERR(pathname);
  213. ret = sb->s_qcop->quota_on(sb, type, id, pathname);
  214. putname(pathname);
  215. return ret;
  216. }
  217. case Q_QUOTAOFF:
  218. return sb->s_qcop->quota_off(sb, type);
  219. case Q_GETFMT: {
  220. __u32 fmt;
  221. down_read(&sb_dqopt(sb)->dqptr_sem);
  222. if (!sb_has_quota_enabled(sb, type)) {
  223. up_read(&sb_dqopt(sb)->dqptr_sem);
  224. return -ESRCH;
  225. }
  226. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  227. up_read(&sb_dqopt(sb)->dqptr_sem);
  228. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  229. return -EFAULT;
  230. return 0;
  231. }
  232. case Q_GETINFO: {
  233. struct if_dqinfo info;
  234. if ((ret = sb->s_qcop->get_info(sb, type, &info)))
  235. return ret;
  236. if (copy_to_user(addr, &info, sizeof(info)))
  237. return -EFAULT;
  238. return 0;
  239. }
  240. case Q_SETINFO: {
  241. struct if_dqinfo info;
  242. if (copy_from_user(&info, addr, sizeof(info)))
  243. return -EFAULT;
  244. return sb->s_qcop->set_info(sb, type, &info);
  245. }
  246. case Q_GETQUOTA: {
  247. struct if_dqblk idq;
  248. if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
  249. return ret;
  250. if (copy_to_user(addr, &idq, sizeof(idq)))
  251. return -EFAULT;
  252. return 0;
  253. }
  254. case Q_SETQUOTA: {
  255. struct if_dqblk idq;
  256. if (copy_from_user(&idq, addr, sizeof(idq)))
  257. return -EFAULT;
  258. return sb->s_qcop->set_dqblk(sb, type, id, &idq);
  259. }
  260. case Q_SYNC:
  261. sync_dquots(sb, type);
  262. return 0;
  263. case Q_XQUOTAON:
  264. case Q_XQUOTAOFF:
  265. case Q_XQUOTARM: {
  266. __u32 flags;
  267. if (copy_from_user(&flags, addr, sizeof(flags)))
  268. return -EFAULT;
  269. return sb->s_qcop->set_xstate(sb, flags, cmd);
  270. }
  271. case Q_XGETQSTAT: {
  272. struct fs_quota_stat fqs;
  273. if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
  274. return ret;
  275. if (copy_to_user(addr, &fqs, sizeof(fqs)))
  276. return -EFAULT;
  277. return 0;
  278. }
  279. case Q_XSETQLIM: {
  280. struct fs_disk_quota fdq;
  281. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  282. return -EFAULT;
  283. return sb->s_qcop->set_xquota(sb, type, id, &fdq);
  284. }
  285. case Q_XGETQUOTA: {
  286. struct fs_disk_quota fdq;
  287. if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
  288. return ret;
  289. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  290. return -EFAULT;
  291. return 0;
  292. }
  293. /* We never reach here unless validity check is broken */
  294. default:
  295. BUG();
  296. }
  297. return 0;
  298. }
  299. /*
  300. * This is the system call interface. This communicates with
  301. * the user-level programs. Currently this only supports diskquota
  302. * calls. Maybe we need to add the process quotas etc. in the future,
  303. * but we probably should use rlimits for that.
  304. */
  305. asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
  306. {
  307. uint cmds, type;
  308. struct super_block *sb = NULL;
  309. struct block_device *bdev;
  310. char *tmp;
  311. int ret;
  312. cmds = cmd >> SUBCMDSHIFT;
  313. type = cmd & SUBCMDMASK;
  314. if (cmds != Q_SYNC || special) {
  315. tmp = getname(special);
  316. if (IS_ERR(tmp))
  317. return PTR_ERR(tmp);
  318. bdev = lookup_bdev(tmp);
  319. putname(tmp);
  320. if (IS_ERR(bdev))
  321. return PTR_ERR(bdev);
  322. sb = get_super(bdev);
  323. bdput(bdev);
  324. if (!sb)
  325. return -ENODEV;
  326. }
  327. ret = check_quotactl_valid(sb, type, cmds, id);
  328. if (ret >= 0)
  329. ret = do_quotactl(sb, type, cmds, id, addr);
  330. if (sb)
  331. drop_super(sb);
  332. return ret;
  333. }