quota.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Quota code necessary even when VFS quota support is not compiled
  4. * into the kernel. The interesting stuff is over in dquot.c, here
  5. * we have symbols for initial quotactl(2) handling, the sysctl(2)
  6. * variables, etc - things needed even when quota support disabled.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/namei.h>
  10. #include <linux/slab.h>
  11. #include <asm/current.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/kernel.h>
  14. #include <linux/security.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/capability.h>
  17. #include <linux/quotaops.h>
  18. #include <linux/types.h>
  19. #include <linux/writeback.h>
  20. #include <linux/nospec.h>
  21. #include "compat.h"
  22. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  23. qid_t id)
  24. {
  25. switch (cmd) {
  26. /* these commands do not require any special privilegues */
  27. case Q_GETFMT:
  28. case Q_SYNC:
  29. case Q_GETINFO:
  30. case Q_XGETQSTAT:
  31. case Q_XGETQSTATV:
  32. case Q_XQUOTASYNC:
  33. break;
  34. /* allow to query information for dquots we "own" */
  35. case Q_GETQUOTA:
  36. case Q_XGETQUOTA:
  37. if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
  38. (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
  39. break;
  40. fallthrough;
  41. default:
  42. if (!capable(CAP_SYS_ADMIN))
  43. return -EPERM;
  44. }
  45. return security_quotactl(cmd, type, id, sb);
  46. }
  47. static void quota_sync_one(struct super_block *sb, void *arg)
  48. {
  49. int type = *(int *)arg;
  50. if (sb->s_qcop && sb->s_qcop->quota_sync &&
  51. (sb->s_quota_types & (1 << type)))
  52. sb->s_qcop->quota_sync(sb, type);
  53. }
  54. static int quota_sync_all(int type)
  55. {
  56. int ret;
  57. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  58. if (!ret)
  59. iterate_supers(quota_sync_one, &type);
  60. return ret;
  61. }
  62. unsigned int qtype_enforce_flag(int type)
  63. {
  64. switch (type) {
  65. case USRQUOTA:
  66. return FS_QUOTA_UDQ_ENFD;
  67. case GRPQUOTA:
  68. return FS_QUOTA_GDQ_ENFD;
  69. case PRJQUOTA:
  70. return FS_QUOTA_PDQ_ENFD;
  71. }
  72. return 0;
  73. }
  74. static int quota_quotaon(struct super_block *sb, int type, qid_t id,
  75. const struct path *path)
  76. {
  77. if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
  78. return -ENOSYS;
  79. if (sb->s_qcop->quota_enable)
  80. return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
  81. if (IS_ERR(path))
  82. return PTR_ERR(path);
  83. return sb->s_qcop->quota_on(sb, type, id, path);
  84. }
  85. static int quota_quotaoff(struct super_block *sb, int type)
  86. {
  87. if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable)
  88. return -ENOSYS;
  89. if (sb->s_qcop->quota_disable)
  90. return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type));
  91. return sb->s_qcop->quota_off(sb, type);
  92. }
  93. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  94. {
  95. __u32 fmt;
  96. if (!sb_has_quota_active(sb, type))
  97. return -ESRCH;
  98. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  99. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  100. return -EFAULT;
  101. return 0;
  102. }
  103. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  104. {
  105. struct qc_state state;
  106. struct qc_type_state *tstate;
  107. struct if_dqinfo uinfo;
  108. int ret;
  109. if (!sb->s_qcop->get_state)
  110. return -ENOSYS;
  111. ret = sb->s_qcop->get_state(sb, &state);
  112. if (ret)
  113. return ret;
  114. tstate = state.s_state + type;
  115. if (!(tstate->flags & QCI_ACCT_ENABLED))
  116. return -ESRCH;
  117. memset(&uinfo, 0, sizeof(uinfo));
  118. uinfo.dqi_bgrace = tstate->spc_timelimit;
  119. uinfo.dqi_igrace = tstate->ino_timelimit;
  120. if (tstate->flags & QCI_SYSFILE)
  121. uinfo.dqi_flags |= DQF_SYS_FILE;
  122. if (tstate->flags & QCI_ROOT_SQUASH)
  123. uinfo.dqi_flags |= DQF_ROOT_SQUASH;
  124. uinfo.dqi_valid = IIF_ALL;
  125. if (copy_to_user(addr, &uinfo, sizeof(uinfo)))
  126. return -EFAULT;
  127. return 0;
  128. }
  129. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  130. {
  131. struct if_dqinfo info;
  132. struct qc_info qinfo;
  133. if (copy_from_user(&info, addr, sizeof(info)))
  134. return -EFAULT;
  135. if (!sb->s_qcop->set_info)
  136. return -ENOSYS;
  137. if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
  138. return -EINVAL;
  139. memset(&qinfo, 0, sizeof(qinfo));
  140. if (info.dqi_valid & IIF_FLAGS) {
  141. if (info.dqi_flags & ~DQF_SETINFO_MASK)
  142. return -EINVAL;
  143. if (info.dqi_flags & DQF_ROOT_SQUASH)
  144. qinfo.i_flags |= QCI_ROOT_SQUASH;
  145. qinfo.i_fieldmask |= QC_FLAGS;
  146. }
  147. if (info.dqi_valid & IIF_BGRACE) {
  148. qinfo.i_spc_timelimit = info.dqi_bgrace;
  149. qinfo.i_fieldmask |= QC_SPC_TIMER;
  150. }
  151. if (info.dqi_valid & IIF_IGRACE) {
  152. qinfo.i_ino_timelimit = info.dqi_igrace;
  153. qinfo.i_fieldmask |= QC_INO_TIMER;
  154. }
  155. return sb->s_qcop->set_info(sb, type, &qinfo);
  156. }
  157. static inline qsize_t qbtos(qsize_t blocks)
  158. {
  159. return blocks << QIF_DQBLKSIZE_BITS;
  160. }
  161. static inline qsize_t stoqb(qsize_t space)
  162. {
  163. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  164. }
  165. static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
  166. {
  167. memset(dst, 0, sizeof(*dst));
  168. dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
  169. dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
  170. dst->dqb_curspace = src->d_space;
  171. dst->dqb_ihardlimit = src->d_ino_hardlimit;
  172. dst->dqb_isoftlimit = src->d_ino_softlimit;
  173. dst->dqb_curinodes = src->d_ino_count;
  174. dst->dqb_btime = src->d_spc_timer;
  175. dst->dqb_itime = src->d_ino_timer;
  176. dst->dqb_valid = QIF_ALL;
  177. }
  178. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  179. void __user *addr)
  180. {
  181. struct kqid qid;
  182. struct qc_dqblk fdq;
  183. struct if_dqblk idq;
  184. int ret;
  185. if (!sb->s_qcop->get_dqblk)
  186. return -ENOSYS;
  187. qid = make_kqid(current_user_ns(), type, id);
  188. if (!qid_has_mapping(sb->s_user_ns, qid))
  189. return -EINVAL;
  190. ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
  191. if (ret)
  192. return ret;
  193. copy_to_if_dqblk(&idq, &fdq);
  194. if (compat_need_64bit_alignment_fixup()) {
  195. struct compat_if_dqblk __user *compat_dqblk = addr;
  196. if (copy_to_user(compat_dqblk, &idq, sizeof(*compat_dqblk)))
  197. return -EFAULT;
  198. if (put_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
  199. return -EFAULT;
  200. } else {
  201. if (copy_to_user(addr, &idq, sizeof(idq)))
  202. return -EFAULT;
  203. }
  204. return 0;
  205. }
  206. /*
  207. * Return quota for next active quota >= this id, if any exists,
  208. * otherwise return -ENOENT via ->get_nextdqblk
  209. */
  210. static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
  211. void __user *addr)
  212. {
  213. struct kqid qid;
  214. struct qc_dqblk fdq;
  215. struct if_nextdqblk idq;
  216. int ret;
  217. if (!sb->s_qcop->get_nextdqblk)
  218. return -ENOSYS;
  219. qid = make_kqid(current_user_ns(), type, id);
  220. if (!qid_has_mapping(sb->s_user_ns, qid))
  221. return -EINVAL;
  222. ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
  223. if (ret)
  224. return ret;
  225. /* struct if_nextdqblk is a superset of struct if_dqblk */
  226. copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq);
  227. idq.dqb_id = from_kqid(current_user_ns(), qid);
  228. if (copy_to_user(addr, &idq, sizeof(idq)))
  229. return -EFAULT;
  230. return 0;
  231. }
  232. static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
  233. {
  234. dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
  235. dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
  236. dst->d_space = src->dqb_curspace;
  237. dst->d_ino_hardlimit = src->dqb_ihardlimit;
  238. dst->d_ino_softlimit = src->dqb_isoftlimit;
  239. dst->d_ino_count = src->dqb_curinodes;
  240. dst->d_spc_timer = src->dqb_btime;
  241. dst->d_ino_timer = src->dqb_itime;
  242. dst->d_fieldmask = 0;
  243. if (src->dqb_valid & QIF_BLIMITS)
  244. dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
  245. if (src->dqb_valid & QIF_SPACE)
  246. dst->d_fieldmask |= QC_SPACE;
  247. if (src->dqb_valid & QIF_ILIMITS)
  248. dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
  249. if (src->dqb_valid & QIF_INODES)
  250. dst->d_fieldmask |= QC_INO_COUNT;
  251. if (src->dqb_valid & QIF_BTIME)
  252. dst->d_fieldmask |= QC_SPC_TIMER;
  253. if (src->dqb_valid & QIF_ITIME)
  254. dst->d_fieldmask |= QC_INO_TIMER;
  255. }
  256. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  257. void __user *addr)
  258. {
  259. struct qc_dqblk fdq;
  260. struct if_dqblk idq;
  261. struct kqid qid;
  262. if (compat_need_64bit_alignment_fixup()) {
  263. struct compat_if_dqblk __user *compat_dqblk = addr;
  264. if (copy_from_user(&idq, compat_dqblk, sizeof(*compat_dqblk)) ||
  265. get_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
  266. return -EFAULT;
  267. } else {
  268. if (copy_from_user(&idq, addr, sizeof(idq)))
  269. return -EFAULT;
  270. }
  271. if (!sb->s_qcop->set_dqblk)
  272. return -ENOSYS;
  273. qid = make_kqid(current_user_ns(), type, id);
  274. if (!qid_has_mapping(sb->s_user_ns, qid))
  275. return -EINVAL;
  276. copy_from_if_dqblk(&fdq, &idq);
  277. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  278. }
  279. static int quota_enable(struct super_block *sb, void __user *addr)
  280. {
  281. __u32 flags;
  282. if (copy_from_user(&flags, addr, sizeof(flags)))
  283. return -EFAULT;
  284. if (!sb->s_qcop->quota_enable)
  285. return -ENOSYS;
  286. return sb->s_qcop->quota_enable(sb, flags);
  287. }
  288. static int quota_disable(struct super_block *sb, void __user *addr)
  289. {
  290. __u32 flags;
  291. if (copy_from_user(&flags, addr, sizeof(flags)))
  292. return -EFAULT;
  293. if (!sb->s_qcop->quota_disable)
  294. return -ENOSYS;
  295. return sb->s_qcop->quota_disable(sb, flags);
  296. }
  297. static int quota_state_to_flags(struct qc_state *state)
  298. {
  299. int flags = 0;
  300. if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED)
  301. flags |= FS_QUOTA_UDQ_ACCT;
  302. if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED)
  303. flags |= FS_QUOTA_UDQ_ENFD;
  304. if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)
  305. flags |= FS_QUOTA_GDQ_ACCT;
  306. if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED)
  307. flags |= FS_QUOTA_GDQ_ENFD;
  308. if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED)
  309. flags |= FS_QUOTA_PDQ_ACCT;
  310. if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED)
  311. flags |= FS_QUOTA_PDQ_ENFD;
  312. return flags;
  313. }
  314. static int quota_getstate(struct super_block *sb, int type,
  315. struct fs_quota_stat *fqs)
  316. {
  317. struct qc_state state;
  318. int ret;
  319. memset(&state, 0, sizeof (struct qc_state));
  320. ret = sb->s_qcop->get_state(sb, &state);
  321. if (ret < 0)
  322. return ret;
  323. memset(fqs, 0, sizeof(*fqs));
  324. fqs->qs_version = FS_QSTAT_VERSION;
  325. fqs->qs_flags = quota_state_to_flags(&state);
  326. /* No quota enabled? */
  327. if (!fqs->qs_flags)
  328. return -ENOSYS;
  329. fqs->qs_incoredqs = state.s_incoredqs;
  330. fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
  331. fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
  332. fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
  333. fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
  334. fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
  335. /* Inodes may be allocated even if inactive; copy out if present */
  336. if (state.s_state[USRQUOTA].ino) {
  337. fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
  338. fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
  339. fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
  340. }
  341. if (state.s_state[GRPQUOTA].ino) {
  342. fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
  343. fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
  344. fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
  345. }
  346. if (state.s_state[PRJQUOTA].ino) {
  347. /*
  348. * Q_XGETQSTAT doesn't have room for both group and project
  349. * quotas. So, allow the project quota values to be copied out
  350. * only if there is no group quota information available.
  351. */
  352. if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) {
  353. fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino;
  354. fqs->qs_gquota.qfs_nblks =
  355. state.s_state[PRJQUOTA].blocks;
  356. fqs->qs_gquota.qfs_nextents =
  357. state.s_state[PRJQUOTA].nextents;
  358. }
  359. }
  360. return 0;
  361. }
  362. static int compat_copy_fs_qfilestat(struct compat_fs_qfilestat __user *to,
  363. struct fs_qfilestat *from)
  364. {
  365. if (copy_to_user(to, from, sizeof(*to)) ||
  366. put_user(from->qfs_nextents, &to->qfs_nextents))
  367. return -EFAULT;
  368. return 0;
  369. }
  370. static int compat_copy_fs_quota_stat(struct compat_fs_quota_stat __user *to,
  371. struct fs_quota_stat *from)
  372. {
  373. if (put_user(from->qs_version, &to->qs_version) ||
  374. put_user(from->qs_flags, &to->qs_flags) ||
  375. put_user(from->qs_pad, &to->qs_pad) ||
  376. compat_copy_fs_qfilestat(&to->qs_uquota, &from->qs_uquota) ||
  377. compat_copy_fs_qfilestat(&to->qs_gquota, &from->qs_gquota) ||
  378. put_user(from->qs_incoredqs, &to->qs_incoredqs) ||
  379. put_user(from->qs_btimelimit, &to->qs_btimelimit) ||
  380. put_user(from->qs_itimelimit, &to->qs_itimelimit) ||
  381. put_user(from->qs_rtbtimelimit, &to->qs_rtbtimelimit) ||
  382. put_user(from->qs_bwarnlimit, &to->qs_bwarnlimit) ||
  383. put_user(from->qs_iwarnlimit, &to->qs_iwarnlimit))
  384. return -EFAULT;
  385. return 0;
  386. }
  387. static int quota_getxstate(struct super_block *sb, int type, void __user *addr)
  388. {
  389. struct fs_quota_stat fqs;
  390. int ret;
  391. if (!sb->s_qcop->get_state)
  392. return -ENOSYS;
  393. ret = quota_getstate(sb, type, &fqs);
  394. if (ret)
  395. return ret;
  396. if (compat_need_64bit_alignment_fixup())
  397. return compat_copy_fs_quota_stat(addr, &fqs);
  398. if (copy_to_user(addr, &fqs, sizeof(fqs)))
  399. return -EFAULT;
  400. return 0;
  401. }
  402. static int quota_getstatev(struct super_block *sb, int type,
  403. struct fs_quota_statv *fqs)
  404. {
  405. struct qc_state state;
  406. int ret;
  407. memset(&state, 0, sizeof (struct qc_state));
  408. ret = sb->s_qcop->get_state(sb, &state);
  409. if (ret < 0)
  410. return ret;
  411. memset(fqs, 0, sizeof(*fqs));
  412. fqs->qs_version = FS_QSTAT_VERSION;
  413. fqs->qs_flags = quota_state_to_flags(&state);
  414. /* No quota enabled? */
  415. if (!fqs->qs_flags)
  416. return -ENOSYS;
  417. fqs->qs_incoredqs = state.s_incoredqs;
  418. fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
  419. fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
  420. fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
  421. fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
  422. fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
  423. /* Inodes may be allocated even if inactive; copy out if present */
  424. if (state.s_state[USRQUOTA].ino) {
  425. fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
  426. fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
  427. fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
  428. }
  429. if (state.s_state[GRPQUOTA].ino) {
  430. fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
  431. fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
  432. fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
  433. }
  434. if (state.s_state[PRJQUOTA].ino) {
  435. fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino;
  436. fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks;
  437. fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents;
  438. }
  439. return 0;
  440. }
  441. static int quota_getxstatev(struct super_block *sb, int type, void __user *addr)
  442. {
  443. struct fs_quota_statv fqs;
  444. int ret;
  445. if (!sb->s_qcop->get_state)
  446. return -ENOSYS;
  447. memset(&fqs, 0, sizeof(fqs));
  448. if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
  449. return -EFAULT;
  450. /* If this kernel doesn't support user specified version, fail */
  451. switch (fqs.qs_version) {
  452. case FS_QSTATV_VERSION1:
  453. break;
  454. default:
  455. return -EINVAL;
  456. }
  457. ret = quota_getstatev(sb, type, &fqs);
  458. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  459. return -EFAULT;
  460. return ret;
  461. }
  462. /*
  463. * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
  464. * out of there as xfsprogs rely on definitions being in that header file. So
  465. * just define same functions here for quota purposes.
  466. */
  467. #define XFS_BB_SHIFT 9
  468. static inline u64 quota_bbtob(u64 blocks)
  469. {
  470. return blocks << XFS_BB_SHIFT;
  471. }
  472. static inline u64 quota_btobb(u64 bytes)
  473. {
  474. return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
  475. }
  476. static inline s64 copy_from_xfs_dqblk_ts(const struct fs_disk_quota *d,
  477. __s32 timer, __s8 timer_hi)
  478. {
  479. if (d->d_fieldmask & FS_DQ_BIGTIME)
  480. return (u32)timer | (s64)timer_hi << 32;
  481. return timer;
  482. }
  483. static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
  484. {
  485. dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
  486. dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
  487. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  488. dst->d_ino_softlimit = src->d_ino_softlimit;
  489. dst->d_space = quota_bbtob(src->d_bcount);
  490. dst->d_ino_count = src->d_icount;
  491. dst->d_ino_timer = copy_from_xfs_dqblk_ts(src, src->d_itimer,
  492. src->d_itimer_hi);
  493. dst->d_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_btimer,
  494. src->d_btimer_hi);
  495. dst->d_ino_warns = src->d_iwarns;
  496. dst->d_spc_warns = src->d_bwarns;
  497. dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
  498. dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
  499. dst->d_rt_space = quota_bbtob(src->d_rtbcount);
  500. dst->d_rt_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_rtbtimer,
  501. src->d_rtbtimer_hi);
  502. dst->d_rt_spc_warns = src->d_rtbwarns;
  503. dst->d_fieldmask = 0;
  504. if (src->d_fieldmask & FS_DQ_ISOFT)
  505. dst->d_fieldmask |= QC_INO_SOFT;
  506. if (src->d_fieldmask & FS_DQ_IHARD)
  507. dst->d_fieldmask |= QC_INO_HARD;
  508. if (src->d_fieldmask & FS_DQ_BSOFT)
  509. dst->d_fieldmask |= QC_SPC_SOFT;
  510. if (src->d_fieldmask & FS_DQ_BHARD)
  511. dst->d_fieldmask |= QC_SPC_HARD;
  512. if (src->d_fieldmask & FS_DQ_RTBSOFT)
  513. dst->d_fieldmask |= QC_RT_SPC_SOFT;
  514. if (src->d_fieldmask & FS_DQ_RTBHARD)
  515. dst->d_fieldmask |= QC_RT_SPC_HARD;
  516. if (src->d_fieldmask & FS_DQ_BTIMER)
  517. dst->d_fieldmask |= QC_SPC_TIMER;
  518. if (src->d_fieldmask & FS_DQ_ITIMER)
  519. dst->d_fieldmask |= QC_INO_TIMER;
  520. if (src->d_fieldmask & FS_DQ_RTBTIMER)
  521. dst->d_fieldmask |= QC_RT_SPC_TIMER;
  522. if (src->d_fieldmask & FS_DQ_BWARNS)
  523. dst->d_fieldmask |= QC_SPC_WARNS;
  524. if (src->d_fieldmask & FS_DQ_IWARNS)
  525. dst->d_fieldmask |= QC_INO_WARNS;
  526. if (src->d_fieldmask & FS_DQ_RTBWARNS)
  527. dst->d_fieldmask |= QC_RT_SPC_WARNS;
  528. if (src->d_fieldmask & FS_DQ_BCOUNT)
  529. dst->d_fieldmask |= QC_SPACE;
  530. if (src->d_fieldmask & FS_DQ_ICOUNT)
  531. dst->d_fieldmask |= QC_INO_COUNT;
  532. if (src->d_fieldmask & FS_DQ_RTBCOUNT)
  533. dst->d_fieldmask |= QC_RT_SPACE;
  534. }
  535. static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
  536. struct fs_disk_quota *src)
  537. {
  538. memset(dst, 0, sizeof(*dst));
  539. dst->i_spc_timelimit = src->d_btimer;
  540. dst->i_ino_timelimit = src->d_itimer;
  541. dst->i_rt_spc_timelimit = src->d_rtbtimer;
  542. dst->i_ino_warnlimit = src->d_iwarns;
  543. dst->i_spc_warnlimit = src->d_bwarns;
  544. dst->i_rt_spc_warnlimit = src->d_rtbwarns;
  545. if (src->d_fieldmask & FS_DQ_BWARNS)
  546. dst->i_fieldmask |= QC_SPC_WARNS;
  547. if (src->d_fieldmask & FS_DQ_IWARNS)
  548. dst->i_fieldmask |= QC_INO_WARNS;
  549. if (src->d_fieldmask & FS_DQ_RTBWARNS)
  550. dst->i_fieldmask |= QC_RT_SPC_WARNS;
  551. if (src->d_fieldmask & FS_DQ_BTIMER)
  552. dst->i_fieldmask |= QC_SPC_TIMER;
  553. if (src->d_fieldmask & FS_DQ_ITIMER)
  554. dst->i_fieldmask |= QC_INO_TIMER;
  555. if (src->d_fieldmask & FS_DQ_RTBTIMER)
  556. dst->i_fieldmask |= QC_RT_SPC_TIMER;
  557. }
  558. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  559. void __user *addr)
  560. {
  561. struct fs_disk_quota fdq;
  562. struct qc_dqblk qdq;
  563. struct kqid qid;
  564. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  565. return -EFAULT;
  566. if (!sb->s_qcop->set_dqblk)
  567. return -ENOSYS;
  568. qid = make_kqid(current_user_ns(), type, id);
  569. if (!qid_has_mapping(sb->s_user_ns, qid))
  570. return -EINVAL;
  571. /* Are we actually setting timer / warning limits for all users? */
  572. if (from_kqid(sb->s_user_ns, qid) == 0 &&
  573. fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
  574. struct qc_info qinfo;
  575. int ret;
  576. if (!sb->s_qcop->set_info)
  577. return -EINVAL;
  578. copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
  579. ret = sb->s_qcop->set_info(sb, type, &qinfo);
  580. if (ret)
  581. return ret;
  582. /* These are already done */
  583. fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
  584. }
  585. copy_from_xfs_dqblk(&qdq, &fdq);
  586. return sb->s_qcop->set_dqblk(sb, qid, &qdq);
  587. }
  588. static inline void copy_to_xfs_dqblk_ts(const struct fs_disk_quota *d,
  589. __s32 *timer_lo, __s8 *timer_hi, s64 timer)
  590. {
  591. *timer_lo = timer;
  592. if (d->d_fieldmask & FS_DQ_BIGTIME)
  593. *timer_hi = timer >> 32;
  594. }
  595. static inline bool want_bigtime(s64 timer)
  596. {
  597. return timer > S32_MAX || timer < S32_MIN;
  598. }
  599. static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
  600. int type, qid_t id)
  601. {
  602. memset(dst, 0, sizeof(*dst));
  603. if (want_bigtime(src->d_ino_timer) || want_bigtime(src->d_spc_timer) ||
  604. want_bigtime(src->d_rt_spc_timer))
  605. dst->d_fieldmask |= FS_DQ_BIGTIME;
  606. dst->d_version = FS_DQUOT_VERSION;
  607. dst->d_id = id;
  608. if (type == USRQUOTA)
  609. dst->d_flags = FS_USER_QUOTA;
  610. else if (type == PRJQUOTA)
  611. dst->d_flags = FS_PROJ_QUOTA;
  612. else
  613. dst->d_flags = FS_GROUP_QUOTA;
  614. dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
  615. dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
  616. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  617. dst->d_ino_softlimit = src->d_ino_softlimit;
  618. dst->d_bcount = quota_btobb(src->d_space);
  619. dst->d_icount = src->d_ino_count;
  620. copy_to_xfs_dqblk_ts(dst, &dst->d_itimer, &dst->d_itimer_hi,
  621. src->d_ino_timer);
  622. copy_to_xfs_dqblk_ts(dst, &dst->d_btimer, &dst->d_btimer_hi,
  623. src->d_spc_timer);
  624. dst->d_iwarns = src->d_ino_warns;
  625. dst->d_bwarns = src->d_spc_warns;
  626. dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
  627. dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
  628. dst->d_rtbcount = quota_btobb(src->d_rt_space);
  629. copy_to_xfs_dqblk_ts(dst, &dst->d_rtbtimer, &dst->d_rtbtimer_hi,
  630. src->d_rt_spc_timer);
  631. dst->d_rtbwarns = src->d_rt_spc_warns;
  632. }
  633. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  634. void __user *addr)
  635. {
  636. struct fs_disk_quota fdq;
  637. struct qc_dqblk qdq;
  638. struct kqid qid;
  639. int ret;
  640. if (!sb->s_qcop->get_dqblk)
  641. return -ENOSYS;
  642. qid = make_kqid(current_user_ns(), type, id);
  643. if (!qid_has_mapping(sb->s_user_ns, qid))
  644. return -EINVAL;
  645. ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
  646. if (ret)
  647. return ret;
  648. copy_to_xfs_dqblk(&fdq, &qdq, type, id);
  649. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  650. return -EFAULT;
  651. return ret;
  652. }
  653. /*
  654. * Return quota for next active quota >= this id, if any exists,
  655. * otherwise return -ENOENT via ->get_nextdqblk.
  656. */
  657. static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
  658. void __user *addr)
  659. {
  660. struct fs_disk_quota fdq;
  661. struct qc_dqblk qdq;
  662. struct kqid qid;
  663. qid_t id_out;
  664. int ret;
  665. if (!sb->s_qcop->get_nextdqblk)
  666. return -ENOSYS;
  667. qid = make_kqid(current_user_ns(), type, id);
  668. if (!qid_has_mapping(sb->s_user_ns, qid))
  669. return -EINVAL;
  670. ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
  671. if (ret)
  672. return ret;
  673. id_out = from_kqid(current_user_ns(), qid);
  674. copy_to_xfs_dqblk(&fdq, &qdq, type, id_out);
  675. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  676. return -EFAULT;
  677. return ret;
  678. }
  679. static int quota_rmxquota(struct super_block *sb, void __user *addr)
  680. {
  681. __u32 flags;
  682. if (copy_from_user(&flags, addr, sizeof(flags)))
  683. return -EFAULT;
  684. if (!sb->s_qcop->rm_xquota)
  685. return -ENOSYS;
  686. return sb->s_qcop->rm_xquota(sb, flags);
  687. }
  688. /* Copy parameters and call proper function */
  689. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  690. void __user *addr, const struct path *path)
  691. {
  692. int ret;
  693. type = array_index_nospec(type, MAXQUOTAS);
  694. /*
  695. * Quota not supported on this fs? Check this before s_quota_types
  696. * since they needn't be set if quota is not supported at all.
  697. */
  698. if (!sb->s_qcop)
  699. return -ENOSYS;
  700. if (!(sb->s_quota_types & (1 << type)))
  701. return -EINVAL;
  702. ret = check_quotactl_permission(sb, type, cmd, id);
  703. if (ret < 0)
  704. return ret;
  705. switch (cmd) {
  706. case Q_QUOTAON:
  707. return quota_quotaon(sb, type, id, path);
  708. case Q_QUOTAOFF:
  709. return quota_quotaoff(sb, type);
  710. case Q_GETFMT:
  711. return quota_getfmt(sb, type, addr);
  712. case Q_GETINFO:
  713. return quota_getinfo(sb, type, addr);
  714. case Q_SETINFO:
  715. return quota_setinfo(sb, type, addr);
  716. case Q_GETQUOTA:
  717. return quota_getquota(sb, type, id, addr);
  718. case Q_GETNEXTQUOTA:
  719. return quota_getnextquota(sb, type, id, addr);
  720. case Q_SETQUOTA:
  721. return quota_setquota(sb, type, id, addr);
  722. case Q_SYNC:
  723. if (!sb->s_qcop->quota_sync)
  724. return -ENOSYS;
  725. return sb->s_qcop->quota_sync(sb, type);
  726. case Q_XQUOTAON:
  727. return quota_enable(sb, addr);
  728. case Q_XQUOTAOFF:
  729. return quota_disable(sb, addr);
  730. case Q_XQUOTARM:
  731. return quota_rmxquota(sb, addr);
  732. case Q_XGETQSTAT:
  733. return quota_getxstate(sb, type, addr);
  734. case Q_XGETQSTATV:
  735. return quota_getxstatev(sb, type, addr);
  736. case Q_XSETQLIM:
  737. return quota_setxquota(sb, type, id, addr);
  738. case Q_XGETQUOTA:
  739. return quota_getxquota(sb, type, id, addr);
  740. case Q_XGETNEXTQUOTA:
  741. return quota_getnextxquota(sb, type, id, addr);
  742. case Q_XQUOTASYNC:
  743. if (sb_rdonly(sb))
  744. return -EROFS;
  745. /* XFS quotas are fully coherent now, making this call a noop */
  746. return 0;
  747. default:
  748. return -EINVAL;
  749. }
  750. }
  751. #ifdef CONFIG_BLOCK
  752. /* Return 1 if 'cmd' will block on frozen filesystem */
  753. static int quotactl_cmd_write(int cmd)
  754. {
  755. /*
  756. * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access
  757. * as dquot_acquire() may allocate space for new structure and OCFS2
  758. * needs to increment on-disk use count.
  759. */
  760. switch (cmd) {
  761. case Q_GETFMT:
  762. case Q_GETINFO:
  763. case Q_SYNC:
  764. case Q_XGETQSTAT:
  765. case Q_XGETQSTATV:
  766. case Q_XGETQUOTA:
  767. case Q_XGETNEXTQUOTA:
  768. case Q_XQUOTASYNC:
  769. return 0;
  770. }
  771. return 1;
  772. }
  773. #endif /* CONFIG_BLOCK */
  774. /* Return true if quotactl command is manipulating quota on/off state */
  775. static bool quotactl_cmd_onoff(int cmd)
  776. {
  777. return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
  778. (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
  779. }
  780. /*
  781. * look up a superblock on which quota ops will be performed
  782. * - use the name of a block device to find the superblock thereon
  783. */
  784. static struct super_block *quotactl_block(const char __user *special, int cmd)
  785. {
  786. #ifdef CONFIG_BLOCK
  787. struct block_device *bdev;
  788. struct super_block *sb;
  789. struct filename *tmp = getname(special);
  790. if (IS_ERR(tmp))
  791. return ERR_CAST(tmp);
  792. bdev = lookup_bdev(tmp->name);
  793. putname(tmp);
  794. if (IS_ERR(bdev))
  795. return ERR_CAST(bdev);
  796. if (quotactl_cmd_onoff(cmd))
  797. sb = get_super_exclusive_thawed(bdev);
  798. else if (quotactl_cmd_write(cmd))
  799. sb = get_super_thawed(bdev);
  800. else
  801. sb = get_super(bdev);
  802. bdput(bdev);
  803. if (!sb)
  804. return ERR_PTR(-ENODEV);
  805. return sb;
  806. #else
  807. return ERR_PTR(-ENODEV);
  808. #endif
  809. }
  810. /*
  811. * This is the system call interface. This communicates with
  812. * the user-level programs. Currently this only supports diskquota
  813. * calls. Maybe we need to add the process quotas etc. in the future,
  814. * but we probably should use rlimits for that.
  815. */
  816. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  817. qid_t, id, void __user *, addr)
  818. {
  819. uint cmds, type;
  820. struct super_block *sb = NULL;
  821. struct path path, *pathp = NULL;
  822. int ret;
  823. cmds = cmd >> SUBCMDSHIFT;
  824. type = cmd & SUBCMDMASK;
  825. if (type >= MAXQUOTAS)
  826. return -EINVAL;
  827. /*
  828. * As a special case Q_SYNC can be called without a specific device.
  829. * It will iterate all superblocks that have quota enabled and call
  830. * the sync action on each of them.
  831. */
  832. if (!special) {
  833. if (cmds == Q_SYNC)
  834. return quota_sync_all(type);
  835. return -ENODEV;
  836. }
  837. /*
  838. * Path for quotaon has to be resolved before grabbing superblock
  839. * because that gets s_umount sem which is also possibly needed by path
  840. * resolution (think about autofs) and thus deadlocks could arise.
  841. */
  842. if (cmds == Q_QUOTAON) {
  843. ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  844. if (ret)
  845. pathp = ERR_PTR(ret);
  846. else
  847. pathp = &path;
  848. }
  849. sb = quotactl_block(special, cmds);
  850. if (IS_ERR(sb)) {
  851. ret = PTR_ERR(sb);
  852. goto out;
  853. }
  854. ret = do_quotactl(sb, type, cmds, id, addr, pathp);
  855. if (!quotactl_cmd_onoff(cmds))
  856. drop_super(sb);
  857. else
  858. drop_super_exclusive(sb);
  859. out:
  860. if (pathp && !IS_ERR(pathp))
  861. path_put(pathp);
  862. return ret;
  863. }