quota.c 9.0 KB

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