quota.c 22 KB

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