quota.c 25 KB

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