quota.c 24 KB

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