quota.c 9.3 KB

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