quota.c 8.8 KB

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