quota.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. case Q_XQUOTASYNC:
  114. if (!sb->s_qcop->quota_sync)
  115. return -ENOSYS;
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. /* Check privileges */
  121. if (cmd == Q_XGETQUOTA) {
  122. if (((type == XQM_USRQUOTA && current->euid != id) ||
  123. (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
  124. !capable(CAP_SYS_ADMIN))
  125. return -EPERM;
  126. } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
  127. if (!capable(CAP_SYS_ADMIN))
  128. return -EPERM;
  129. }
  130. return 0;
  131. }
  132. static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
  133. {
  134. int error;
  135. if (XQM_COMMAND(cmd))
  136. error = xqm_quotactl_valid(sb, type, cmd, id);
  137. else
  138. error = generic_quotactl_valid(sb, type, cmd, id);
  139. if (!error)
  140. error = security_quotactl(cmd, type, id, sb);
  141. return error;
  142. }
  143. static void quota_sync_sb(struct super_block *sb, int type)
  144. {
  145. int cnt;
  146. struct inode *discard[MAXQUOTAS];
  147. sb->s_qcop->quota_sync(sb, type);
  148. /* This is not very clever (and fast) but currently I don't know about
  149. * any other simple way of getting quota data to disk and we must get
  150. * them there for userspace to be visible... */
  151. if (sb->s_op->sync_fs)
  152. sb->s_op->sync_fs(sb, 1);
  153. sync_blockdev(sb->s_bdev);
  154. /* Now when everything is written we can discard the pagecache so
  155. * that userspace sees the changes. We need i_sem and so we could
  156. * not do it inside dqonoff_sem. Moreover we need to be carefull
  157. * about races with quotaoff() (that is the reason why we have own
  158. * reference to inode). */
  159. down(&sb_dqopt(sb)->dqonoff_sem);
  160. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  161. discard[cnt] = NULL;
  162. if (type != -1 && cnt != type)
  163. continue;
  164. if (!sb_has_quota_enabled(sb, cnt))
  165. continue;
  166. discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
  167. }
  168. up(&sb_dqopt(sb)->dqonoff_sem);
  169. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  170. if (discard[cnt]) {
  171. down(&discard[cnt]->i_sem);
  172. truncate_inode_pages(&discard[cnt]->i_data, 0);
  173. up(&discard[cnt]->i_sem);
  174. iput(discard[cnt]);
  175. }
  176. }
  177. }
  178. void sync_dquots(struct super_block *sb, int type)
  179. {
  180. int cnt, dirty;
  181. if (sb) {
  182. if (sb->s_qcop->quota_sync)
  183. quota_sync_sb(sb, type);
  184. return;
  185. }
  186. spin_lock(&sb_lock);
  187. restart:
  188. list_for_each_entry(sb, &super_blocks, s_list) {
  189. /* This test just improves performance so it needn't be reliable... */
  190. for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
  191. if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
  192. && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
  193. dirty = 1;
  194. if (!dirty)
  195. continue;
  196. sb->s_count++;
  197. spin_unlock(&sb_lock);
  198. down_read(&sb->s_umount);
  199. if (sb->s_root && sb->s_qcop->quota_sync)
  200. quota_sync_sb(sb, type);
  201. up_read(&sb->s_umount);
  202. spin_lock(&sb_lock);
  203. if (__put_super_and_need_restart(sb))
  204. goto restart;
  205. }
  206. spin_unlock(&sb_lock);
  207. }
  208. /* Copy parameters and call proper function */
  209. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
  210. {
  211. int ret;
  212. switch (cmd) {
  213. case Q_QUOTAON: {
  214. char *pathname;
  215. if (IS_ERR(pathname = getname(addr)))
  216. return PTR_ERR(pathname);
  217. ret = sb->s_qcop->quota_on(sb, type, id, pathname);
  218. putname(pathname);
  219. return ret;
  220. }
  221. case Q_QUOTAOFF:
  222. return sb->s_qcop->quota_off(sb, type);
  223. case Q_GETFMT: {
  224. __u32 fmt;
  225. down_read(&sb_dqopt(sb)->dqptr_sem);
  226. if (!sb_has_quota_enabled(sb, type)) {
  227. up_read(&sb_dqopt(sb)->dqptr_sem);
  228. return -ESRCH;
  229. }
  230. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  231. up_read(&sb_dqopt(sb)->dqptr_sem);
  232. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  233. return -EFAULT;
  234. return 0;
  235. }
  236. case Q_GETINFO: {
  237. struct if_dqinfo info;
  238. if ((ret = sb->s_qcop->get_info(sb, type, &info)))
  239. return ret;
  240. if (copy_to_user(addr, &info, sizeof(info)))
  241. return -EFAULT;
  242. return 0;
  243. }
  244. case Q_SETINFO: {
  245. struct if_dqinfo info;
  246. if (copy_from_user(&info, addr, sizeof(info)))
  247. return -EFAULT;
  248. return sb->s_qcop->set_info(sb, type, &info);
  249. }
  250. case Q_GETQUOTA: {
  251. struct if_dqblk idq;
  252. if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
  253. return ret;
  254. if (copy_to_user(addr, &idq, sizeof(idq)))
  255. return -EFAULT;
  256. return 0;
  257. }
  258. case Q_SETQUOTA: {
  259. struct if_dqblk idq;
  260. if (copy_from_user(&idq, addr, sizeof(idq)))
  261. return -EFAULT;
  262. return sb->s_qcop->set_dqblk(sb, type, id, &idq);
  263. }
  264. case Q_SYNC:
  265. sync_dquots(sb, type);
  266. return 0;
  267. case Q_XQUOTAON:
  268. case Q_XQUOTAOFF:
  269. case Q_XQUOTARM: {
  270. __u32 flags;
  271. if (copy_from_user(&flags, addr, sizeof(flags)))
  272. return -EFAULT;
  273. return sb->s_qcop->set_xstate(sb, flags, cmd);
  274. }
  275. case Q_XGETQSTAT: {
  276. struct fs_quota_stat fqs;
  277. if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
  278. return ret;
  279. if (copy_to_user(addr, &fqs, sizeof(fqs)))
  280. return -EFAULT;
  281. return 0;
  282. }
  283. case Q_XSETQLIM: {
  284. struct fs_disk_quota fdq;
  285. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  286. return -EFAULT;
  287. return sb->s_qcop->set_xquota(sb, type, id, &fdq);
  288. }
  289. case Q_XGETQUOTA: {
  290. struct fs_disk_quota fdq;
  291. if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
  292. return ret;
  293. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  294. return -EFAULT;
  295. return 0;
  296. }
  297. case Q_XQUOTASYNC:
  298. return sb->s_qcop->quota_sync(sb, type);
  299. /* We never reach here unless validity check is broken */
  300. default:
  301. BUG();
  302. }
  303. return 0;
  304. }
  305. /*
  306. * This is the system call interface. This communicates with
  307. * the user-level programs. Currently this only supports diskquota
  308. * calls. Maybe we need to add the process quotas etc. in the future,
  309. * but we probably should use rlimits for that.
  310. */
  311. asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
  312. {
  313. uint cmds, type;
  314. struct super_block *sb = NULL;
  315. struct block_device *bdev;
  316. char *tmp;
  317. int ret;
  318. cmds = cmd >> SUBCMDSHIFT;
  319. type = cmd & SUBCMDMASK;
  320. if (cmds != Q_SYNC || special) {
  321. tmp = getname(special);
  322. if (IS_ERR(tmp))
  323. return PTR_ERR(tmp);
  324. bdev = lookup_bdev(tmp);
  325. putname(tmp);
  326. if (IS_ERR(bdev))
  327. return PTR_ERR(bdev);
  328. sb = get_super(bdev);
  329. bdput(bdev);
  330. if (!sb)
  331. return -ENODEV;
  332. }
  333. ret = check_quotactl_valid(sb, type, cmds, id);
  334. if (ret >= 0)
  335. ret = do_quotactl(sb, type, cmds, id, addr);
  336. if (sb)
  337. drop_super(sb);
  338. return ret;
  339. }