quota.c 9.0 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/security.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/capability.h>
  17. #include <linux/quotaops.h>
  18. #include <linux/types.h>
  19. #include <linux/writeback.h>
  20. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  21. qid_t id)
  22. {
  23. switch (cmd) {
  24. /* these commands do not require any special privilegues */
  25. case Q_GETFMT:
  26. case Q_SYNC:
  27. case Q_GETINFO:
  28. case Q_XGETQSTAT:
  29. case Q_XQUOTASYNC:
  30. break;
  31. /* allow to query information for dquots we "own" */
  32. case Q_GETQUOTA:
  33. case Q_XGETQUOTA:
  34. if ((type == USRQUOTA && current_euid() == id) ||
  35. (type == GRPQUOTA && in_egroup_p(id)))
  36. break;
  37. /*FALLTHROUGH*/
  38. default:
  39. if (!capable(CAP_SYS_ADMIN))
  40. return -EPERM;
  41. }
  42. return security_quotactl(cmd, type, id, sb);
  43. }
  44. static int quota_sync_all(int type)
  45. {
  46. struct super_block *sb;
  47. int ret;
  48. if (type >= MAXQUOTAS)
  49. return -EINVAL;
  50. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  51. if (ret)
  52. return ret;
  53. spin_lock(&sb_lock);
  54. restart:
  55. list_for_each_entry(sb, &super_blocks, s_list) {
  56. if (!sb->s_qcop || !sb->s_qcop->quota_sync)
  57. continue;
  58. sb->s_count++;
  59. spin_unlock(&sb_lock);
  60. down_read(&sb->s_umount);
  61. if (sb->s_root)
  62. sb->s_qcop->quota_sync(sb, type, 1);
  63. up_read(&sb->s_umount);
  64. spin_lock(&sb_lock);
  65. if (__put_super_and_need_restart(sb))
  66. goto restart;
  67. }
  68. spin_unlock(&sb_lock);
  69. return 0;
  70. }
  71. static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
  72. void __user *addr)
  73. {
  74. char *pathname;
  75. int ret = -ENOSYS;
  76. pathname = getname(addr);
  77. if (IS_ERR(pathname))
  78. return PTR_ERR(pathname);
  79. if (sb->s_qcop->quota_on)
  80. ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
  81. putname(pathname);
  82. return ret;
  83. }
  84. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  85. {
  86. __u32 fmt;
  87. down_read(&sb_dqopt(sb)->dqptr_sem);
  88. if (!sb_has_quota_active(sb, type)) {
  89. up_read(&sb_dqopt(sb)->dqptr_sem);
  90. return -ESRCH;
  91. }
  92. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  93. up_read(&sb_dqopt(sb)->dqptr_sem);
  94. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  95. return -EFAULT;
  96. return 0;
  97. }
  98. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  99. {
  100. struct if_dqinfo info;
  101. int ret;
  102. if (!sb->s_qcop->get_info)
  103. return -ENOSYS;
  104. ret = sb->s_qcop->get_info(sb, type, &info);
  105. if (!ret && copy_to_user(addr, &info, sizeof(info)))
  106. return -EFAULT;
  107. return ret;
  108. }
  109. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  110. {
  111. struct if_dqinfo info;
  112. if (copy_from_user(&info, addr, sizeof(info)))
  113. return -EFAULT;
  114. if (!sb->s_qcop->set_info)
  115. return -ENOSYS;
  116. return sb->s_qcop->set_info(sb, type, &info);
  117. }
  118. static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src)
  119. {
  120. dst->dqb_bhardlimit = src->d_blk_hardlimit;
  121. dst->dqb_bsoftlimit = src->d_blk_softlimit;
  122. dst->dqb_curspace = src->d_bcount;
  123. dst->dqb_ihardlimit = src->d_ino_hardlimit;
  124. dst->dqb_isoftlimit = src->d_ino_softlimit;
  125. dst->dqb_curinodes = src->d_icount;
  126. dst->dqb_btime = src->d_btimer;
  127. dst->dqb_itime = src->d_itimer;
  128. dst->dqb_valid = QIF_ALL;
  129. }
  130. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  131. void __user *addr)
  132. {
  133. struct fs_disk_quota fdq;
  134. struct if_dqblk idq;
  135. int ret;
  136. if (!sb->s_qcop->get_dqblk)
  137. return -ENOSYS;
  138. ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
  139. if (ret)
  140. return ret;
  141. copy_to_if_dqblk(&idq, &fdq);
  142. if (copy_to_user(addr, &idq, sizeof(idq)))
  143. return -EFAULT;
  144. return 0;
  145. }
  146. static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src)
  147. {
  148. dst->d_blk_hardlimit = src->dqb_bhardlimit;
  149. dst->d_blk_softlimit = src->dqb_bsoftlimit;
  150. dst->d_bcount = src->dqb_curspace;
  151. dst->d_ino_hardlimit = src->dqb_ihardlimit;
  152. dst->d_ino_softlimit = src->dqb_isoftlimit;
  153. dst->d_icount = src->dqb_curinodes;
  154. dst->d_btimer = src->dqb_btime;
  155. dst->d_itimer = src->dqb_itime;
  156. dst->d_fieldmask = 0;
  157. if (src->dqb_valid & QIF_BLIMITS)
  158. dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD;
  159. if (src->dqb_valid & QIF_SPACE)
  160. dst->d_fieldmask |= FS_DQ_BCOUNT;
  161. if (src->dqb_valid & QIF_ILIMITS)
  162. dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD;
  163. if (src->dqb_valid & QIF_INODES)
  164. dst->d_fieldmask |= FS_DQ_ICOUNT;
  165. if (src->dqb_valid & QIF_BTIME)
  166. dst->d_fieldmask |= FS_DQ_BTIMER;
  167. if (src->dqb_valid & QIF_ITIME)
  168. dst->d_fieldmask |= FS_DQ_ITIMER;
  169. }
  170. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  171. void __user *addr)
  172. {
  173. struct fs_disk_quota fdq;
  174. struct if_dqblk idq;
  175. if (copy_from_user(&idq, addr, sizeof(idq)))
  176. return -EFAULT;
  177. if (!sb->s_qcop->set_dqblk)
  178. return -ENOSYS;
  179. copy_from_if_dqblk(&fdq, &idq);
  180. return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
  181. }
  182. static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
  183. {
  184. __u32 flags;
  185. if (copy_from_user(&flags, addr, sizeof(flags)))
  186. return -EFAULT;
  187. if (!sb->s_qcop->set_xstate)
  188. return -ENOSYS;
  189. return sb->s_qcop->set_xstate(sb, flags, cmd);
  190. }
  191. static int quota_getxstate(struct super_block *sb, void __user *addr)
  192. {
  193. struct fs_quota_stat fqs;
  194. int ret;
  195. if (!sb->s_qcop->get_xstate)
  196. return -ENOSYS;
  197. ret = sb->s_qcop->get_xstate(sb, &fqs);
  198. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  199. return -EFAULT;
  200. return ret;
  201. }
  202. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  203. void __user *addr)
  204. {
  205. struct fs_disk_quota fdq;
  206. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  207. return -EFAULT;
  208. if (!sb->s_qcop->set_dqblk)
  209. return -ENOSYS;
  210. return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
  211. }
  212. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  213. void __user *addr)
  214. {
  215. struct fs_disk_quota fdq;
  216. int ret;
  217. if (!sb->s_qcop->get_dqblk)
  218. return -ENOSYS;
  219. ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
  220. if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
  221. return -EFAULT;
  222. return ret;
  223. }
  224. /* Copy parameters and call proper function */
  225. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  226. void __user *addr)
  227. {
  228. int ret;
  229. if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
  230. return -EINVAL;
  231. if (!sb->s_qcop)
  232. return -ENOSYS;
  233. ret = check_quotactl_permission(sb, type, cmd, id);
  234. if (ret < 0)
  235. return ret;
  236. switch (cmd) {
  237. case Q_QUOTAON:
  238. return quota_quotaon(sb, type, cmd, id, addr);
  239. case Q_QUOTAOFF:
  240. if (!sb->s_qcop->quota_off)
  241. return -ENOSYS;
  242. return sb->s_qcop->quota_off(sb, type, 0);
  243. case Q_GETFMT:
  244. return quota_getfmt(sb, type, addr);
  245. case Q_GETINFO:
  246. return quota_getinfo(sb, type, addr);
  247. case Q_SETINFO:
  248. return quota_setinfo(sb, type, addr);
  249. case Q_GETQUOTA:
  250. return quota_getquota(sb, type, id, addr);
  251. case Q_SETQUOTA:
  252. return quota_setquota(sb, type, id, addr);
  253. case Q_SYNC:
  254. if (!sb->s_qcop->quota_sync)
  255. return -ENOSYS;
  256. return sb->s_qcop->quota_sync(sb, type, 1);
  257. case Q_XQUOTAON:
  258. case Q_XQUOTAOFF:
  259. case Q_XQUOTARM:
  260. return quota_setxstate(sb, cmd, addr);
  261. case Q_XGETQSTAT:
  262. return quota_getxstate(sb, addr);
  263. case Q_XSETQLIM:
  264. return quota_setxquota(sb, type, id, addr);
  265. case Q_XGETQUOTA:
  266. return quota_getxquota(sb, type, id, addr);
  267. case Q_XQUOTASYNC:
  268. /* caller already holds s_umount */
  269. if (sb->s_flags & MS_RDONLY)
  270. return -EROFS;
  271. writeback_inodes_sb(sb);
  272. return 0;
  273. default:
  274. return -EINVAL;
  275. }
  276. }
  277. /*
  278. * look up a superblock on which quota ops will be performed
  279. * - use the name of a block device to find the superblock thereon
  280. */
  281. static struct super_block *quotactl_block(const char __user *special)
  282. {
  283. #ifdef CONFIG_BLOCK
  284. struct block_device *bdev;
  285. struct super_block *sb;
  286. char *tmp = getname(special);
  287. if (IS_ERR(tmp))
  288. return ERR_CAST(tmp);
  289. bdev = lookup_bdev(tmp);
  290. putname(tmp);
  291. if (IS_ERR(bdev))
  292. return ERR_CAST(bdev);
  293. sb = get_super(bdev);
  294. bdput(bdev);
  295. if (!sb)
  296. return ERR_PTR(-ENODEV);
  297. return sb;
  298. #else
  299. return ERR_PTR(-ENODEV);
  300. #endif
  301. }
  302. /*
  303. * This is the system call interface. This communicates with
  304. * the user-level programs. Currently this only supports diskquota
  305. * calls. Maybe we need to add the process quotas etc. in the future,
  306. * but we probably should use rlimits for that.
  307. */
  308. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  309. qid_t, id, void __user *, addr)
  310. {
  311. uint cmds, type;
  312. struct super_block *sb = NULL;
  313. int ret;
  314. cmds = cmd >> SUBCMDSHIFT;
  315. type = cmd & SUBCMDMASK;
  316. /*
  317. * As a special case Q_SYNC can be called without a specific device.
  318. * It will iterate all superblocks that have quota enabled and call
  319. * the sync action on each of them.
  320. */
  321. if (!special) {
  322. if (cmds == Q_SYNC)
  323. return quota_sync_all(type);
  324. return -ENODEV;
  325. }
  326. sb = quotactl_block(special);
  327. if (IS_ERR(sb))
  328. return PTR_ERR(sb);
  329. ret = do_quotactl(sb, type, cmds, id, addr);
  330. drop_super(sb);
  331. return ret;
  332. }