quota.c 9.1 KB

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