quota.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <linux/uaccess.h>
  12. #include <linux/kernel.h>
  13. #include <linux/security.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/capability.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/types.h>
  18. #include <linux/writeback.h>
  19. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  20. qid_t id)
  21. {
  22. switch (cmd) {
  23. /* these commands do not require any special privilegues */
  24. case Q_GETFMT:
  25. case Q_SYNC:
  26. case Q_GETINFO:
  27. case Q_XGETQSTAT:
  28. case Q_XQUOTASYNC:
  29. break;
  30. /* allow to query information for dquots we "own" */
  31. case Q_GETQUOTA:
  32. case Q_XGETQUOTA:
  33. if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
  34. (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
  35. break;
  36. /*FALLTHROUGH*/
  37. default:
  38. if (!capable(CAP_SYS_ADMIN))
  39. return -EPERM;
  40. }
  41. return security_quotactl(cmd, type, id, sb);
  42. }
  43. static void quota_sync_one(struct super_block *sb, void *arg)
  44. {
  45. if (sb->s_qcop && sb->s_qcop->quota_sync)
  46. sb->s_qcop->quota_sync(sb, *(int *)arg);
  47. }
  48. static int quota_sync_all(int type)
  49. {
  50. int ret;
  51. if (type >= MAXQUOTAS)
  52. return -EINVAL;
  53. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  54. if (!ret)
  55. iterate_supers(quota_sync_one, &type);
  56. return ret;
  57. }
  58. static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
  59. struct path *path)
  60. {
  61. if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_on_meta)
  62. return -ENOSYS;
  63. if (sb->s_qcop->quota_on_meta)
  64. return sb->s_qcop->quota_on_meta(sb, type, id);
  65. if (IS_ERR(path))
  66. return PTR_ERR(path);
  67. return sb->s_qcop->quota_on(sb, type, id, path);
  68. }
  69. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  70. {
  71. __u32 fmt;
  72. down_read(&sb_dqopt(sb)->dqptr_sem);
  73. if (!sb_has_quota_active(sb, type)) {
  74. up_read(&sb_dqopt(sb)->dqptr_sem);
  75. return -ESRCH;
  76. }
  77. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  78. up_read(&sb_dqopt(sb)->dqptr_sem);
  79. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  80. return -EFAULT;
  81. return 0;
  82. }
  83. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  84. {
  85. struct if_dqinfo info;
  86. int ret;
  87. if (!sb->s_qcop->get_info)
  88. return -ENOSYS;
  89. ret = sb->s_qcop->get_info(sb, type, &info);
  90. if (!ret && copy_to_user(addr, &info, sizeof(info)))
  91. return -EFAULT;
  92. return ret;
  93. }
  94. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  95. {
  96. struct if_dqinfo info;
  97. if (copy_from_user(&info, addr, sizeof(info)))
  98. return -EFAULT;
  99. if (!sb->s_qcop->set_info)
  100. return -ENOSYS;
  101. return sb->s_qcop->set_info(sb, type, &info);
  102. }
  103. static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src)
  104. {
  105. dst->dqb_bhardlimit = src->d_blk_hardlimit;
  106. dst->dqb_bsoftlimit = src->d_blk_softlimit;
  107. dst->dqb_curspace = src->d_bcount;
  108. dst->dqb_ihardlimit = src->d_ino_hardlimit;
  109. dst->dqb_isoftlimit = src->d_ino_softlimit;
  110. dst->dqb_curinodes = src->d_icount;
  111. dst->dqb_btime = src->d_btimer;
  112. dst->dqb_itime = src->d_itimer;
  113. dst->dqb_valid = QIF_ALL;
  114. }
  115. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  116. void __user *addr)
  117. {
  118. struct kqid qid;
  119. struct fs_disk_quota fdq;
  120. struct if_dqblk idq;
  121. int ret;
  122. if (!sb->s_qcop->get_dqblk)
  123. return -ENOSYS;
  124. qid = make_kqid(current_user_ns(), type, id);
  125. if (!qid_valid(qid))
  126. return -EINVAL;
  127. ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
  128. if (ret)
  129. return ret;
  130. copy_to_if_dqblk(&idq, &fdq);
  131. if (copy_to_user(addr, &idq, sizeof(idq)))
  132. return -EFAULT;
  133. return 0;
  134. }
  135. static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src)
  136. {
  137. dst->d_blk_hardlimit = src->dqb_bhardlimit;
  138. dst->d_blk_softlimit = src->dqb_bsoftlimit;
  139. dst->d_bcount = src->dqb_curspace;
  140. dst->d_ino_hardlimit = src->dqb_ihardlimit;
  141. dst->d_ino_softlimit = src->dqb_isoftlimit;
  142. dst->d_icount = src->dqb_curinodes;
  143. dst->d_btimer = src->dqb_btime;
  144. dst->d_itimer = src->dqb_itime;
  145. dst->d_fieldmask = 0;
  146. if (src->dqb_valid & QIF_BLIMITS)
  147. dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD;
  148. if (src->dqb_valid & QIF_SPACE)
  149. dst->d_fieldmask |= FS_DQ_BCOUNT;
  150. if (src->dqb_valid & QIF_ILIMITS)
  151. dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD;
  152. if (src->dqb_valid & QIF_INODES)
  153. dst->d_fieldmask |= FS_DQ_ICOUNT;
  154. if (src->dqb_valid & QIF_BTIME)
  155. dst->d_fieldmask |= FS_DQ_BTIMER;
  156. if (src->dqb_valid & QIF_ITIME)
  157. dst->d_fieldmask |= FS_DQ_ITIMER;
  158. }
  159. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  160. void __user *addr)
  161. {
  162. struct fs_disk_quota fdq;
  163. struct if_dqblk idq;
  164. struct kqid qid;
  165. if (copy_from_user(&idq, addr, sizeof(idq)))
  166. return -EFAULT;
  167. if (!sb->s_qcop->set_dqblk)
  168. return -ENOSYS;
  169. qid = make_kqid(current_user_ns(), type, id);
  170. if (!qid_valid(qid))
  171. return -EINVAL;
  172. copy_from_if_dqblk(&fdq, &idq);
  173. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  174. }
  175. static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
  176. {
  177. __u32 flags;
  178. if (copy_from_user(&flags, addr, sizeof(flags)))
  179. return -EFAULT;
  180. if (!sb->s_qcop->set_xstate)
  181. return -ENOSYS;
  182. return sb->s_qcop->set_xstate(sb, flags, cmd);
  183. }
  184. static int quota_getxstate(struct super_block *sb, void __user *addr)
  185. {
  186. struct fs_quota_stat fqs;
  187. int ret;
  188. if (!sb->s_qcop->get_xstate)
  189. return -ENOSYS;
  190. ret = sb->s_qcop->get_xstate(sb, &fqs);
  191. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  192. return -EFAULT;
  193. return ret;
  194. }
  195. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  196. void __user *addr)
  197. {
  198. struct fs_disk_quota fdq;
  199. struct kqid qid;
  200. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  201. return -EFAULT;
  202. if (!sb->s_qcop->set_dqblk)
  203. return -ENOSYS;
  204. qid = make_kqid(current_user_ns(), type, id);
  205. if (!qid_valid(qid))
  206. return -EINVAL;
  207. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  208. }
  209. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  210. void __user *addr)
  211. {
  212. struct fs_disk_quota fdq;
  213. struct kqid qid;
  214. int ret;
  215. if (!sb->s_qcop->get_dqblk)
  216. return -ENOSYS;
  217. qid = make_kqid(current_user_ns(), type, id);
  218. if (!qid_valid(qid))
  219. return -EINVAL;
  220. ret = sb->s_qcop->get_dqblk(sb, qid, &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, struct path *path)
  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, path);
  240. case Q_QUOTAOFF:
  241. if (!sb->s_qcop->quota_off)
  242. return -ENOSYS;
  243. return sb->s_qcop->quota_off(sb, type);
  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);
  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. if (sb->s_flags & MS_RDONLY)
  270. return -EROFS;
  271. /* XFS quotas are fully coherent now, making this call a noop */
  272. return 0;
  273. default:
  274. return -EINVAL;
  275. }
  276. }
  277. #ifdef CONFIG_BLOCK
  278. /* Return 1 if 'cmd' will block on frozen filesystem */
  279. static int quotactl_cmd_write(int cmd)
  280. {
  281. switch (cmd) {
  282. case Q_GETFMT:
  283. case Q_GETINFO:
  284. case Q_SYNC:
  285. case Q_XGETQSTAT:
  286. case Q_XGETQUOTA:
  287. case Q_XQUOTASYNC:
  288. return 0;
  289. }
  290. return 1;
  291. }
  292. #endif /* CONFIG_BLOCK */
  293. /*
  294. * look up a superblock on which quota ops will be performed
  295. * - use the name of a block device to find the superblock thereon
  296. */
  297. static struct super_block *quotactl_block(const char __user *special, int cmd)
  298. {
  299. #ifdef CONFIG_BLOCK
  300. struct block_device *bdev;
  301. struct super_block *sb;
  302. struct filename *tmp = getname(special);
  303. if (IS_ERR(tmp))
  304. return ERR_CAST(tmp);
  305. bdev = lookup_bdev(tmp->name);
  306. putname(tmp);
  307. if (IS_ERR(bdev))
  308. return ERR_CAST(bdev);
  309. if (quotactl_cmd_write(cmd))
  310. sb = get_super_thawed(bdev);
  311. else
  312. sb = get_super(bdev);
  313. bdput(bdev);
  314. if (!sb)
  315. return ERR_PTR(-ENODEV);
  316. return sb;
  317. #else
  318. return ERR_PTR(-ENODEV);
  319. #endif
  320. }
  321. /*
  322. * This is the system call interface. This communicates with
  323. * the user-level programs. Currently this only supports diskquota
  324. * calls. Maybe we need to add the process quotas etc. in the future,
  325. * but we probably should use rlimits for that.
  326. */
  327. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  328. qid_t, id, void __user *, addr)
  329. {
  330. uint cmds, type;
  331. struct super_block *sb = NULL;
  332. struct path path, *pathp = NULL;
  333. int ret;
  334. cmds = cmd >> SUBCMDSHIFT;
  335. type = cmd & SUBCMDMASK;
  336. /*
  337. * As a special case Q_SYNC can be called without a specific device.
  338. * It will iterate all superblocks that have quota enabled and call
  339. * the sync action on each of them.
  340. */
  341. if (!special) {
  342. if (cmds == Q_SYNC)
  343. return quota_sync_all(type);
  344. return -ENODEV;
  345. }
  346. /*
  347. * Path for quotaon has to be resolved before grabbing superblock
  348. * because that gets s_umount sem which is also possibly needed by path
  349. * resolution (think about autofs) and thus deadlocks could arise.
  350. */
  351. if (cmds == Q_QUOTAON) {
  352. ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  353. if (ret)
  354. pathp = ERR_PTR(ret);
  355. else
  356. pathp = &path;
  357. }
  358. sb = quotactl_block(special, cmds);
  359. if (IS_ERR(sb)) {
  360. ret = PTR_ERR(sb);
  361. goto out;
  362. }
  363. ret = do_quotactl(sb, type, cmds, id, addr, pathp);
  364. drop_super(sb);
  365. out:
  366. if (pathp && !IS_ERR(pathp))
  367. path_put(pathp);
  368. return ret;
  369. }