quota.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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, int cmd, 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_valid(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. static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
  205. {
  206. dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
  207. dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
  208. dst->d_space = src->dqb_curspace;
  209. dst->d_ino_hardlimit = src->dqb_ihardlimit;
  210. dst->d_ino_softlimit = src->dqb_isoftlimit;
  211. dst->d_ino_count = src->dqb_curinodes;
  212. dst->d_spc_timer = src->dqb_btime;
  213. dst->d_ino_timer = src->dqb_itime;
  214. dst->d_fieldmask = 0;
  215. if (src->dqb_valid & QIF_BLIMITS)
  216. dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
  217. if (src->dqb_valid & QIF_SPACE)
  218. dst->d_fieldmask |= QC_SPACE;
  219. if (src->dqb_valid & QIF_ILIMITS)
  220. dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
  221. if (src->dqb_valid & QIF_INODES)
  222. dst->d_fieldmask |= QC_INO_COUNT;
  223. if (src->dqb_valid & QIF_BTIME)
  224. dst->d_fieldmask |= QC_SPC_TIMER;
  225. if (src->dqb_valid & QIF_ITIME)
  226. dst->d_fieldmask |= QC_INO_TIMER;
  227. }
  228. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  229. void __user *addr)
  230. {
  231. struct qc_dqblk fdq;
  232. struct if_dqblk idq;
  233. struct kqid qid;
  234. if (copy_from_user(&idq, addr, sizeof(idq)))
  235. return -EFAULT;
  236. if (!sb->s_qcop->set_dqblk)
  237. return -ENOSYS;
  238. qid = make_kqid(current_user_ns(), type, id);
  239. if (!qid_valid(qid))
  240. return -EINVAL;
  241. copy_from_if_dqblk(&fdq, &idq);
  242. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  243. }
  244. static int quota_enable(struct super_block *sb, void __user *addr)
  245. {
  246. __u32 flags;
  247. if (copy_from_user(&flags, addr, sizeof(flags)))
  248. return -EFAULT;
  249. if (!sb->s_qcop->quota_enable)
  250. return -ENOSYS;
  251. return sb->s_qcop->quota_enable(sb, flags);
  252. }
  253. static int quota_disable(struct super_block *sb, void __user *addr)
  254. {
  255. __u32 flags;
  256. if (copy_from_user(&flags, addr, sizeof(flags)))
  257. return -EFAULT;
  258. if (!sb->s_qcop->quota_disable)
  259. return -ENOSYS;
  260. return sb->s_qcop->quota_disable(sb, flags);
  261. }
  262. static int quota_state_to_flags(struct qc_state *state)
  263. {
  264. int flags = 0;
  265. if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED)
  266. flags |= FS_QUOTA_UDQ_ACCT;
  267. if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED)
  268. flags |= FS_QUOTA_UDQ_ENFD;
  269. if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)
  270. flags |= FS_QUOTA_GDQ_ACCT;
  271. if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED)
  272. flags |= FS_QUOTA_GDQ_ENFD;
  273. if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED)
  274. flags |= FS_QUOTA_PDQ_ACCT;
  275. if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED)
  276. flags |= FS_QUOTA_PDQ_ENFD;
  277. return flags;
  278. }
  279. static int quota_getstate(struct super_block *sb, struct fs_quota_stat *fqs)
  280. {
  281. int type;
  282. struct qc_state state;
  283. int ret;
  284. ret = sb->s_qcop->get_state(sb, &state);
  285. if (ret < 0)
  286. return ret;
  287. memset(fqs, 0, sizeof(*fqs));
  288. fqs->qs_version = FS_QSTAT_VERSION;
  289. fqs->qs_flags = quota_state_to_flags(&state);
  290. /* No quota enabled? */
  291. if (!fqs->qs_flags)
  292. return -ENOSYS;
  293. fqs->qs_incoredqs = state.s_incoredqs;
  294. /*
  295. * GETXSTATE quotactl has space for just one set of time limits so
  296. * report them for the first enabled quota type
  297. */
  298. for (type = 0; type < XQM_MAXQUOTAS; type++)
  299. if (state.s_state[type].flags & QCI_ACCT_ENABLED)
  300. break;
  301. BUG_ON(type == XQM_MAXQUOTAS);
  302. fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
  303. fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
  304. fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
  305. fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
  306. fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
  307. if (state.s_state[USRQUOTA].flags & QCI_ACCT_ENABLED) {
  308. fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
  309. fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
  310. fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
  311. }
  312. if (state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED) {
  313. fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
  314. fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
  315. fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
  316. }
  317. if (state.s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED) {
  318. /*
  319. * Q_XGETQSTAT doesn't have room for both group and project
  320. * quotas. So, allow the project quota values to be copied out
  321. * only if there is no group quota information available.
  322. */
  323. if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) {
  324. fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino;
  325. fqs->qs_gquota.qfs_nblks =
  326. state.s_state[PRJQUOTA].blocks;
  327. fqs->qs_gquota.qfs_nextents =
  328. state.s_state[PRJQUOTA].nextents;
  329. }
  330. }
  331. return 0;
  332. }
  333. static int quota_getxstate(struct super_block *sb, void __user *addr)
  334. {
  335. struct fs_quota_stat fqs;
  336. int ret;
  337. if (!sb->s_qcop->get_state)
  338. return -ENOSYS;
  339. ret = quota_getstate(sb, &fqs);
  340. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  341. return -EFAULT;
  342. return ret;
  343. }
  344. static int quota_getstatev(struct super_block *sb, struct fs_quota_statv *fqs)
  345. {
  346. int type;
  347. struct qc_state state;
  348. int ret;
  349. ret = sb->s_qcop->get_state(sb, &state);
  350. if (ret < 0)
  351. return ret;
  352. memset(fqs, 0, sizeof(*fqs));
  353. fqs->qs_version = FS_QSTAT_VERSION;
  354. fqs->qs_flags = quota_state_to_flags(&state);
  355. /* No quota enabled? */
  356. if (!fqs->qs_flags)
  357. return -ENOSYS;
  358. fqs->qs_incoredqs = state.s_incoredqs;
  359. /*
  360. * GETXSTATV quotactl has space for just one set of time limits so
  361. * report them for the first enabled quota type
  362. */
  363. for (type = 0; type < XQM_MAXQUOTAS; type++)
  364. if (state.s_state[type].flags & QCI_ACCT_ENABLED)
  365. break;
  366. BUG_ON(type == XQM_MAXQUOTAS);
  367. fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
  368. fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
  369. fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
  370. fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
  371. fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
  372. if (state.s_state[USRQUOTA].flags & QCI_ACCT_ENABLED) {
  373. fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
  374. fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
  375. fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
  376. }
  377. if (state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED) {
  378. fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
  379. fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
  380. fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
  381. }
  382. if (state.s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED) {
  383. fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino;
  384. fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks;
  385. fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents;
  386. }
  387. return 0;
  388. }
  389. static int quota_getxstatev(struct super_block *sb, void __user *addr)
  390. {
  391. struct fs_quota_statv fqs;
  392. int ret;
  393. if (!sb->s_qcop->get_state)
  394. return -ENOSYS;
  395. memset(&fqs, 0, sizeof(fqs));
  396. if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
  397. return -EFAULT;
  398. /* If this kernel doesn't support user specified version, fail */
  399. switch (fqs.qs_version) {
  400. case FS_QSTATV_VERSION1:
  401. break;
  402. default:
  403. return -EINVAL;
  404. }
  405. ret = quota_getstatev(sb, &fqs);
  406. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  407. return -EFAULT;
  408. return ret;
  409. }
  410. /*
  411. * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
  412. * out of there as xfsprogs rely on definitions being in that header file. So
  413. * just define same functions here for quota purposes.
  414. */
  415. #define XFS_BB_SHIFT 9
  416. static inline u64 quota_bbtob(u64 blocks)
  417. {
  418. return blocks << XFS_BB_SHIFT;
  419. }
  420. static inline u64 quota_btobb(u64 bytes)
  421. {
  422. return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
  423. }
  424. static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
  425. {
  426. dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
  427. dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
  428. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  429. dst->d_ino_softlimit = src->d_ino_softlimit;
  430. dst->d_space = quota_bbtob(src->d_bcount);
  431. dst->d_ino_count = src->d_icount;
  432. dst->d_ino_timer = src->d_itimer;
  433. dst->d_spc_timer = src->d_btimer;
  434. dst->d_ino_warns = src->d_iwarns;
  435. dst->d_spc_warns = src->d_bwarns;
  436. dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
  437. dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
  438. dst->d_rt_space = quota_bbtob(src->d_rtbcount);
  439. dst->d_rt_spc_timer = src->d_rtbtimer;
  440. dst->d_rt_spc_warns = src->d_rtbwarns;
  441. dst->d_fieldmask = 0;
  442. if (src->d_fieldmask & FS_DQ_ISOFT)
  443. dst->d_fieldmask |= QC_INO_SOFT;
  444. if (src->d_fieldmask & FS_DQ_IHARD)
  445. dst->d_fieldmask |= QC_INO_HARD;
  446. if (src->d_fieldmask & FS_DQ_BSOFT)
  447. dst->d_fieldmask |= QC_SPC_SOFT;
  448. if (src->d_fieldmask & FS_DQ_BHARD)
  449. dst->d_fieldmask |= QC_SPC_HARD;
  450. if (src->d_fieldmask & FS_DQ_RTBSOFT)
  451. dst->d_fieldmask |= QC_RT_SPC_SOFT;
  452. if (src->d_fieldmask & FS_DQ_RTBHARD)
  453. dst->d_fieldmask |= QC_RT_SPC_HARD;
  454. if (src->d_fieldmask & FS_DQ_BTIMER)
  455. dst->d_fieldmask |= QC_SPC_TIMER;
  456. if (src->d_fieldmask & FS_DQ_ITIMER)
  457. dst->d_fieldmask |= QC_INO_TIMER;
  458. if (src->d_fieldmask & FS_DQ_RTBTIMER)
  459. dst->d_fieldmask |= QC_RT_SPC_TIMER;
  460. if (src->d_fieldmask & FS_DQ_BWARNS)
  461. dst->d_fieldmask |= QC_SPC_WARNS;
  462. if (src->d_fieldmask & FS_DQ_IWARNS)
  463. dst->d_fieldmask |= QC_INO_WARNS;
  464. if (src->d_fieldmask & FS_DQ_RTBWARNS)
  465. dst->d_fieldmask |= QC_RT_SPC_WARNS;
  466. if (src->d_fieldmask & FS_DQ_BCOUNT)
  467. dst->d_fieldmask |= QC_SPACE;
  468. if (src->d_fieldmask & FS_DQ_ICOUNT)
  469. dst->d_fieldmask |= QC_INO_COUNT;
  470. if (src->d_fieldmask & FS_DQ_RTBCOUNT)
  471. dst->d_fieldmask |= QC_RT_SPACE;
  472. }
  473. static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
  474. struct fs_disk_quota *src)
  475. {
  476. memset(dst, 0, sizeof(*dst));
  477. dst->i_spc_timelimit = src->d_btimer;
  478. dst->i_ino_timelimit = src->d_itimer;
  479. dst->i_rt_spc_timelimit = src->d_rtbtimer;
  480. dst->i_ino_warnlimit = src->d_iwarns;
  481. dst->i_spc_warnlimit = src->d_bwarns;
  482. dst->i_rt_spc_warnlimit = src->d_rtbwarns;
  483. if (src->d_fieldmask & FS_DQ_BWARNS)
  484. dst->i_fieldmask |= QC_SPC_WARNS;
  485. if (src->d_fieldmask & FS_DQ_IWARNS)
  486. dst->i_fieldmask |= QC_INO_WARNS;
  487. if (src->d_fieldmask & FS_DQ_RTBWARNS)
  488. dst->i_fieldmask |= QC_RT_SPC_WARNS;
  489. if (src->d_fieldmask & FS_DQ_BTIMER)
  490. dst->i_fieldmask |= QC_SPC_TIMER;
  491. if (src->d_fieldmask & FS_DQ_ITIMER)
  492. dst->i_fieldmask |= QC_INO_TIMER;
  493. if (src->d_fieldmask & FS_DQ_RTBTIMER)
  494. dst->i_fieldmask |= QC_RT_SPC_TIMER;
  495. }
  496. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  497. void __user *addr)
  498. {
  499. struct fs_disk_quota fdq;
  500. struct qc_dqblk qdq;
  501. struct kqid qid;
  502. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  503. return -EFAULT;
  504. if (!sb->s_qcop->set_dqblk)
  505. return -ENOSYS;
  506. qid = make_kqid(current_user_ns(), type, id);
  507. if (!qid_valid(qid))
  508. return -EINVAL;
  509. /* Are we actually setting timer / warning limits for all users? */
  510. if (from_kqid(&init_user_ns, qid) == 0 &&
  511. fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
  512. struct qc_info qinfo;
  513. int ret;
  514. if (!sb->s_qcop->set_info)
  515. return -EINVAL;
  516. copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
  517. ret = sb->s_qcop->set_info(sb, type, &qinfo);
  518. if (ret)
  519. return ret;
  520. /* These are already done */
  521. fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
  522. }
  523. copy_from_xfs_dqblk(&qdq, &fdq);
  524. return sb->s_qcop->set_dqblk(sb, qid, &qdq);
  525. }
  526. static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
  527. int type, qid_t id)
  528. {
  529. memset(dst, 0, sizeof(*dst));
  530. dst->d_version = FS_DQUOT_VERSION;
  531. dst->d_id = id;
  532. if (type == USRQUOTA)
  533. dst->d_flags = FS_USER_QUOTA;
  534. else if (type == PRJQUOTA)
  535. dst->d_flags = FS_PROJ_QUOTA;
  536. else
  537. dst->d_flags = FS_GROUP_QUOTA;
  538. dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
  539. dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
  540. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  541. dst->d_ino_softlimit = src->d_ino_softlimit;
  542. dst->d_bcount = quota_btobb(src->d_space);
  543. dst->d_icount = src->d_ino_count;
  544. dst->d_itimer = src->d_ino_timer;
  545. dst->d_btimer = src->d_spc_timer;
  546. dst->d_iwarns = src->d_ino_warns;
  547. dst->d_bwarns = src->d_spc_warns;
  548. dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
  549. dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
  550. dst->d_rtbcount = quota_btobb(src->d_rt_space);
  551. dst->d_rtbtimer = src->d_rt_spc_timer;
  552. dst->d_rtbwarns = src->d_rt_spc_warns;
  553. }
  554. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  555. void __user *addr)
  556. {
  557. struct fs_disk_quota fdq;
  558. struct qc_dqblk qdq;
  559. struct kqid qid;
  560. int ret;
  561. if (!sb->s_qcop->get_dqblk)
  562. return -ENOSYS;
  563. qid = make_kqid(current_user_ns(), type, id);
  564. if (!qid_valid(qid))
  565. return -EINVAL;
  566. ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
  567. if (ret)
  568. return ret;
  569. copy_to_xfs_dqblk(&fdq, &qdq, type, id);
  570. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  571. return -EFAULT;
  572. return ret;
  573. }
  574. static int quota_rmxquota(struct super_block *sb, void __user *addr)
  575. {
  576. __u32 flags;
  577. if (copy_from_user(&flags, addr, sizeof(flags)))
  578. return -EFAULT;
  579. if (!sb->s_qcop->rm_xquota)
  580. return -ENOSYS;
  581. return sb->s_qcop->rm_xquota(sb, flags);
  582. }
  583. /* Copy parameters and call proper function */
  584. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  585. void __user *addr, struct path *path)
  586. {
  587. int ret;
  588. if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
  589. return -EINVAL;
  590. type = array_index_nospec(type, MAXQUOTAS);
  591. /*
  592. * Quota not supported on this fs? Check this before s_quota_types
  593. * since they needn't be set if quota is not supported at all.
  594. */
  595. if (!sb->s_qcop)
  596. return -ENOSYS;
  597. if (!(sb->s_quota_types & (1 << type)))
  598. return -EINVAL;
  599. ret = check_quotactl_permission(sb, type, cmd, id);
  600. if (ret < 0)
  601. return ret;
  602. switch (cmd) {
  603. case Q_QUOTAON:
  604. return quota_quotaon(sb, type, cmd, id, path);
  605. case Q_QUOTAOFF:
  606. return quota_quotaoff(sb, type);
  607. case Q_GETFMT:
  608. return quota_getfmt(sb, type, addr);
  609. case Q_GETINFO:
  610. return quota_getinfo(sb, type, addr);
  611. case Q_SETINFO:
  612. return quota_setinfo(sb, type, addr);
  613. case Q_GETQUOTA:
  614. return quota_getquota(sb, type, id, addr);
  615. case Q_SETQUOTA:
  616. return quota_setquota(sb, type, id, addr);
  617. case Q_SYNC:
  618. if (!sb->s_qcop->quota_sync)
  619. return -ENOSYS;
  620. return sb->s_qcop->quota_sync(sb, type);
  621. case Q_XQUOTAON:
  622. return quota_enable(sb, addr);
  623. case Q_XQUOTAOFF:
  624. return quota_disable(sb, addr);
  625. case Q_XQUOTARM:
  626. return quota_rmxquota(sb, addr);
  627. case Q_XGETQSTAT:
  628. return quota_getxstate(sb, addr);
  629. case Q_XGETQSTATV:
  630. return quota_getxstatev(sb, addr);
  631. case Q_XSETQLIM:
  632. return quota_setxquota(sb, type, id, addr);
  633. case Q_XGETQUOTA:
  634. return quota_getxquota(sb, type, id, addr);
  635. case Q_XQUOTASYNC:
  636. if (sb->s_flags & MS_RDONLY)
  637. return -EROFS;
  638. /* XFS quotas are fully coherent now, making this call a noop */
  639. return 0;
  640. default:
  641. return -EINVAL;
  642. }
  643. }
  644. #ifdef CONFIG_BLOCK
  645. /* Return 1 if 'cmd' will block on frozen filesystem */
  646. static int quotactl_cmd_write(int cmd)
  647. {
  648. switch (cmd) {
  649. case Q_GETFMT:
  650. case Q_GETINFO:
  651. case Q_SYNC:
  652. case Q_XGETQSTAT:
  653. case Q_XGETQSTATV:
  654. case Q_XGETQUOTA:
  655. case Q_XQUOTASYNC:
  656. return 0;
  657. }
  658. return 1;
  659. }
  660. #endif /* CONFIG_BLOCK */
  661. /*
  662. * look up a superblock on which quota ops will be performed
  663. * - use the name of a block device to find the superblock thereon
  664. */
  665. static struct super_block *quotactl_block(const char __user *special, int cmd)
  666. {
  667. #ifdef CONFIG_BLOCK
  668. struct block_device *bdev;
  669. struct super_block *sb;
  670. struct filename *tmp = getname(special);
  671. if (IS_ERR(tmp))
  672. return ERR_CAST(tmp);
  673. bdev = lookup_bdev(tmp->name);
  674. putname(tmp);
  675. if (IS_ERR(bdev))
  676. return ERR_CAST(bdev);
  677. if (quotactl_cmd_write(cmd))
  678. sb = get_super_thawed(bdev);
  679. else
  680. sb = get_super(bdev);
  681. bdput(bdev);
  682. if (!sb)
  683. return ERR_PTR(-ENODEV);
  684. return sb;
  685. #else
  686. return ERR_PTR(-ENODEV);
  687. #endif
  688. }
  689. /*
  690. * This is the system call interface. This communicates with
  691. * the user-level programs. Currently this only supports diskquota
  692. * calls. Maybe we need to add the process quotas etc. in the future,
  693. * but we probably should use rlimits for that.
  694. */
  695. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  696. qid_t, id, void __user *, addr)
  697. {
  698. uint cmds, type;
  699. struct super_block *sb = NULL;
  700. struct path path, *pathp = NULL;
  701. int ret;
  702. cmds = cmd >> SUBCMDSHIFT;
  703. type = cmd & SUBCMDMASK;
  704. /*
  705. * As a special case Q_SYNC can be called without a specific device.
  706. * It will iterate all superblocks that have quota enabled and call
  707. * the sync action on each of them.
  708. */
  709. if (!special) {
  710. if (cmds == Q_SYNC)
  711. return quota_sync_all(type);
  712. return -ENODEV;
  713. }
  714. /*
  715. * Path for quotaon has to be resolved before grabbing superblock
  716. * because that gets s_umount sem which is also possibly needed by path
  717. * resolution (think about autofs) and thus deadlocks could arise.
  718. */
  719. if (cmds == Q_QUOTAON) {
  720. ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  721. if (ret)
  722. pathp = ERR_PTR(ret);
  723. else
  724. pathp = &path;
  725. }
  726. sb = quotactl_block(special, cmds);
  727. if (IS_ERR(sb)) {
  728. ret = PTR_ERR(sb);
  729. goto out;
  730. }
  731. ret = do_quotactl(sb, type, cmds, id, addr, pathp);
  732. drop_super(sb);
  733. out:
  734. if (pathp && !IS_ERR(pathp))
  735. path_put(pathp);
  736. return ret;
  737. }