quota.c 9.3 KB

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