quota.c 8.8 KB

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