quota.c 25 KB

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