quota.c 24 KB

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