quota.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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_XGETQSTATV:
  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 && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
  35. (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), 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);
  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. struct path *path)
  61. {
  62. if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_on_meta)
  63. return -ENOSYS;
  64. if (sb->s_qcop->quota_on_meta)
  65. return sb->s_qcop->quota_on_meta(sb, type, id);
  66. if (IS_ERR(path))
  67. return PTR_ERR(path);
  68. return sb->s_qcop->quota_on(sb, type, id, path);
  69. }
  70. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  71. {
  72. __u32 fmt;
  73. down_read(&sb_dqopt(sb)->dqptr_sem);
  74. if (!sb_has_quota_active(sb, type)) {
  75. up_read(&sb_dqopt(sb)->dqptr_sem);
  76. return -ESRCH;
  77. }
  78. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  79. up_read(&sb_dqopt(sb)->dqptr_sem);
  80. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  81. return -EFAULT;
  82. return 0;
  83. }
  84. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  85. {
  86. struct if_dqinfo info;
  87. int ret;
  88. if (!sb->s_qcop->get_info)
  89. return -ENOSYS;
  90. ret = sb->s_qcop->get_info(sb, type, &info);
  91. if (!ret && copy_to_user(addr, &info, sizeof(info)))
  92. return -EFAULT;
  93. return ret;
  94. }
  95. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  96. {
  97. struct if_dqinfo info;
  98. if (copy_from_user(&info, addr, sizeof(info)))
  99. return -EFAULT;
  100. if (!sb->s_qcop->set_info)
  101. return -ENOSYS;
  102. return sb->s_qcop->set_info(sb, type, &info);
  103. }
  104. static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src)
  105. {
  106. memset(dst, 0, sizeof(*dst));
  107. dst->dqb_bhardlimit = src->d_blk_hardlimit;
  108. dst->dqb_bsoftlimit = src->d_blk_softlimit;
  109. dst->dqb_curspace = src->d_bcount;
  110. dst->dqb_ihardlimit = src->d_ino_hardlimit;
  111. dst->dqb_isoftlimit = src->d_ino_softlimit;
  112. dst->dqb_curinodes = src->d_icount;
  113. dst->dqb_btime = src->d_btimer;
  114. dst->dqb_itime = src->d_itimer;
  115. dst->dqb_valid = QIF_ALL;
  116. }
  117. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  118. void __user *addr)
  119. {
  120. struct kqid qid;
  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. qid = make_kqid(current_user_ns(), type, id);
  127. if (!qid_valid(qid))
  128. return -EINVAL;
  129. ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
  130. if (ret)
  131. return ret;
  132. copy_to_if_dqblk(&idq, &fdq);
  133. if (copy_to_user(addr, &idq, sizeof(idq)))
  134. return -EFAULT;
  135. return 0;
  136. }
  137. static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src)
  138. {
  139. dst->d_blk_hardlimit = src->dqb_bhardlimit;
  140. dst->d_blk_softlimit = src->dqb_bsoftlimit;
  141. dst->d_bcount = src->dqb_curspace;
  142. dst->d_ino_hardlimit = src->dqb_ihardlimit;
  143. dst->d_ino_softlimit = src->dqb_isoftlimit;
  144. dst->d_icount = src->dqb_curinodes;
  145. dst->d_btimer = src->dqb_btime;
  146. dst->d_itimer = src->dqb_itime;
  147. dst->d_fieldmask = 0;
  148. if (src->dqb_valid & QIF_BLIMITS)
  149. dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD;
  150. if (src->dqb_valid & QIF_SPACE)
  151. dst->d_fieldmask |= FS_DQ_BCOUNT;
  152. if (src->dqb_valid & QIF_ILIMITS)
  153. dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD;
  154. if (src->dqb_valid & QIF_INODES)
  155. dst->d_fieldmask |= FS_DQ_ICOUNT;
  156. if (src->dqb_valid & QIF_BTIME)
  157. dst->d_fieldmask |= FS_DQ_BTIMER;
  158. if (src->dqb_valid & QIF_ITIME)
  159. dst->d_fieldmask |= FS_DQ_ITIMER;
  160. }
  161. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  162. void __user *addr)
  163. {
  164. struct fs_disk_quota fdq;
  165. struct if_dqblk idq;
  166. struct kqid qid;
  167. if (copy_from_user(&idq, addr, sizeof(idq)))
  168. return -EFAULT;
  169. if (!sb->s_qcop->set_dqblk)
  170. return -ENOSYS;
  171. qid = make_kqid(current_user_ns(), type, id);
  172. if (!qid_valid(qid))
  173. return -EINVAL;
  174. copy_from_if_dqblk(&fdq, &idq);
  175. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  176. }
  177. static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
  178. {
  179. __u32 flags;
  180. if (copy_from_user(&flags, addr, sizeof(flags)))
  181. return -EFAULT;
  182. if (!sb->s_qcop->set_xstate)
  183. return -ENOSYS;
  184. return sb->s_qcop->set_xstate(sb, flags, cmd);
  185. }
  186. static int quota_getxstate(struct super_block *sb, void __user *addr)
  187. {
  188. struct fs_quota_stat fqs;
  189. int ret;
  190. if (!sb->s_qcop->get_xstate)
  191. return -ENOSYS;
  192. ret = sb->s_qcop->get_xstate(sb, &fqs);
  193. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  194. return -EFAULT;
  195. return ret;
  196. }
  197. static int quota_getxstatev(struct super_block *sb, void __user *addr)
  198. {
  199. struct fs_quota_statv fqs;
  200. int ret;
  201. if (!sb->s_qcop->get_xstatev)
  202. return -ENOSYS;
  203. memset(&fqs, 0, sizeof(fqs));
  204. if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
  205. return -EFAULT;
  206. /* If this kernel doesn't support user specified version, fail */
  207. switch (fqs.qs_version) {
  208. case FS_QSTATV_VERSION1:
  209. break;
  210. default:
  211. return -EINVAL;
  212. }
  213. ret = sb->s_qcop->get_xstatev(sb, &fqs);
  214. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  215. return -EFAULT;
  216. return ret;
  217. }
  218. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  219. void __user *addr)
  220. {
  221. struct fs_disk_quota fdq;
  222. struct kqid qid;
  223. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  224. return -EFAULT;
  225. if (!sb->s_qcop->set_dqblk)
  226. return -ENOSYS;
  227. qid = make_kqid(current_user_ns(), type, id);
  228. if (!qid_valid(qid))
  229. return -EINVAL;
  230. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  231. }
  232. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  233. void __user *addr)
  234. {
  235. struct fs_disk_quota fdq;
  236. struct kqid qid;
  237. int ret;
  238. if (!sb->s_qcop->get_dqblk)
  239. return -ENOSYS;
  240. qid = make_kqid(current_user_ns(), type, id);
  241. if (!qid_valid(qid))
  242. return -EINVAL;
  243. ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
  244. if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
  245. return -EFAULT;
  246. return ret;
  247. }
  248. /* Copy parameters and call proper function */
  249. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  250. void __user *addr, struct path *path)
  251. {
  252. int ret;
  253. if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
  254. return -EINVAL;
  255. if (!sb->s_qcop)
  256. return -ENOSYS;
  257. ret = check_quotactl_permission(sb, type, cmd, id);
  258. if (ret < 0)
  259. return ret;
  260. switch (cmd) {
  261. case Q_QUOTAON:
  262. return quota_quotaon(sb, type, cmd, id, path);
  263. case Q_QUOTAOFF:
  264. if (!sb->s_qcop->quota_off)
  265. return -ENOSYS;
  266. return sb->s_qcop->quota_off(sb, type);
  267. case Q_GETFMT:
  268. return quota_getfmt(sb, type, addr);
  269. case Q_GETINFO:
  270. return quota_getinfo(sb, type, addr);
  271. case Q_SETINFO:
  272. return quota_setinfo(sb, type, addr);
  273. case Q_GETQUOTA:
  274. return quota_getquota(sb, type, id, addr);
  275. case Q_SETQUOTA:
  276. return quota_setquota(sb, type, id, addr);
  277. case Q_SYNC:
  278. if (!sb->s_qcop->quota_sync)
  279. return -ENOSYS;
  280. return sb->s_qcop->quota_sync(sb, type);
  281. case Q_XQUOTAON:
  282. case Q_XQUOTAOFF:
  283. case Q_XQUOTARM:
  284. return quota_setxstate(sb, cmd, addr);
  285. case Q_XGETQSTAT:
  286. return quota_getxstate(sb, addr);
  287. case Q_XGETQSTATV:
  288. return quota_getxstatev(sb, addr);
  289. case Q_XSETQLIM:
  290. return quota_setxquota(sb, type, id, addr);
  291. case Q_XGETQUOTA:
  292. return quota_getxquota(sb, type, id, addr);
  293. case Q_XQUOTASYNC:
  294. if (sb->s_flags & MS_RDONLY)
  295. return -EROFS;
  296. /* XFS quotas are fully coherent now, making this call a noop */
  297. return 0;
  298. default:
  299. return -EINVAL;
  300. }
  301. }
  302. #ifdef CONFIG_BLOCK
  303. /* Return 1 if 'cmd' will block on frozen filesystem */
  304. static int quotactl_cmd_write(int cmd)
  305. {
  306. switch (cmd) {
  307. case Q_GETFMT:
  308. case Q_GETINFO:
  309. case Q_SYNC:
  310. case Q_XGETQSTAT:
  311. case Q_XGETQSTATV:
  312. case Q_XGETQUOTA:
  313. case Q_XQUOTASYNC:
  314. return 0;
  315. }
  316. return 1;
  317. }
  318. #endif /* CONFIG_BLOCK */
  319. /*
  320. * look up a superblock on which quota ops will be performed
  321. * - use the name of a block device to find the superblock thereon
  322. */
  323. static struct super_block *quotactl_block(const char __user *special, int cmd)
  324. {
  325. #ifdef CONFIG_BLOCK
  326. struct block_device *bdev;
  327. struct super_block *sb;
  328. struct filename *tmp = getname(special);
  329. if (IS_ERR(tmp))
  330. return ERR_CAST(tmp);
  331. bdev = lookup_bdev(tmp->name);
  332. putname(tmp);
  333. if (IS_ERR(bdev))
  334. return ERR_CAST(bdev);
  335. if (quotactl_cmd_write(cmd))
  336. sb = get_super_thawed(bdev);
  337. else
  338. sb = get_super(bdev);
  339. bdput(bdev);
  340. if (!sb)
  341. return ERR_PTR(-ENODEV);
  342. return sb;
  343. #else
  344. return ERR_PTR(-ENODEV);
  345. #endif
  346. }
  347. /*
  348. * This is the system call interface. This communicates with
  349. * the user-level programs. Currently this only supports diskquota
  350. * calls. Maybe we need to add the process quotas etc. in the future,
  351. * but we probably should use rlimits for that.
  352. */
  353. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  354. qid_t, id, void __user *, addr)
  355. {
  356. uint cmds, type;
  357. struct super_block *sb = NULL;
  358. struct path path, *pathp = NULL;
  359. int ret;
  360. cmds = cmd >> SUBCMDSHIFT;
  361. type = cmd & SUBCMDMASK;
  362. /*
  363. * As a special case Q_SYNC can be called without a specific device.
  364. * It will iterate all superblocks that have quota enabled and call
  365. * the sync action on each of them.
  366. */
  367. if (!special) {
  368. if (cmds == Q_SYNC)
  369. return quota_sync_all(type);
  370. return -ENODEV;
  371. }
  372. /*
  373. * Path for quotaon has to be resolved before grabbing superblock
  374. * because that gets s_umount sem which is also possibly needed by path
  375. * resolution (think about autofs) and thus deadlocks could arise.
  376. */
  377. if (cmds == Q_QUOTAON) {
  378. ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  379. if (ret)
  380. pathp = ERR_PTR(ret);
  381. else
  382. pathp = &path;
  383. }
  384. sb = quotactl_block(special, cmds);
  385. if (IS_ERR(sb)) {
  386. ret = PTR_ERR(sb);
  387. goto out;
  388. }
  389. ret = do_quotactl(sb, type, cmds, id, addr, pathp);
  390. drop_super(sb);
  391. out:
  392. if (pathp && !IS_ERR(pathp))
  393. path_put(pathp);
  394. return ret;
  395. }