quota.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Quota code necessary even when VFS quota support is not compiled
  4. * into the kernel. The interesting stuff is over in dquot.c, here
  5. * we have symbols for initial quotactl(2) handling, the sysctl(2)
  6. * variables, etc - things needed even when quota support disabled.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/namei.h>
  10. #include <linux/slab.h>
  11. #include <asm/current.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/kernel.h>
  15. #include <linux/security.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/capability.h>
  18. #include <linux/quotaops.h>
  19. #include <linux/types.h>
  20. #include <linux/mount.h>
  21. #include <linux/writeback.h>
  22. #include <linux/nospec.h>
  23. #include "compat.h"
  24. #include "../internal.h"
  25. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  26. qid_t id)
  27. {
  28. switch (cmd) {
  29. /* these commands do not require any special privilegues */
  30. case Q_GETFMT:
  31. case Q_SYNC:
  32. case Q_GETINFO:
  33. case Q_XGETQSTAT:
  34. case Q_XGETQSTATV:
  35. case Q_XQUOTASYNC:
  36. break;
  37. /* allow to query information for dquots we "own" */
  38. case Q_GETQUOTA:
  39. case Q_XGETQUOTA:
  40. if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
  41. (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
  42. break;
  43. fallthrough;
  44. default:
  45. if (!capable(CAP_SYS_ADMIN))
  46. return -EPERM;
  47. }
  48. return security_quotactl(cmd, type, id, sb);
  49. }
  50. static void quota_sync_one(struct super_block *sb, void *arg)
  51. {
  52. int type = *(int *)arg;
  53. if (sb->s_qcop && sb->s_qcop->quota_sync &&
  54. (sb->s_quota_types & (1 << type)))
  55. sb->s_qcop->quota_sync(sb, type);
  56. }
  57. static int quota_sync_all(int type)
  58. {
  59. int ret;
  60. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  61. if (!ret)
  62. iterate_supers(quota_sync_one, &type);
  63. return ret;
  64. }
  65. unsigned int qtype_enforce_flag(int type)
  66. {
  67. switch (type) {
  68. case USRQUOTA:
  69. return FS_QUOTA_UDQ_ENFD;
  70. case GRPQUOTA:
  71. return FS_QUOTA_GDQ_ENFD;
  72. case PRJQUOTA:
  73. return FS_QUOTA_PDQ_ENFD;
  74. }
  75. return 0;
  76. }
  77. static int quota_quotaon(struct super_block *sb, int type, qid_t id,
  78. const struct path *path)
  79. {
  80. if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_enable)
  81. return -ENOSYS;
  82. if (sb->s_qcop->quota_enable)
  83. return sb->s_qcop->quota_enable(sb, qtype_enforce_flag(type));
  84. if (IS_ERR(path))
  85. return PTR_ERR(path);
  86. return sb->s_qcop->quota_on(sb, type, id, path);
  87. }
  88. static int quota_quotaoff(struct super_block *sb, int type)
  89. {
  90. if (!sb->s_qcop->quota_off && !sb->s_qcop->quota_disable)
  91. return -ENOSYS;
  92. if (sb->s_qcop->quota_disable)
  93. return sb->s_qcop->quota_disable(sb, qtype_enforce_flag(type));
  94. return sb->s_qcop->quota_off(sb, type);
  95. }
  96. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  97. {
  98. __u32 fmt;
  99. if (!sb_has_quota_active(sb, type))
  100. return -ESRCH;
  101. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  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. if (!sb->s_qcop->get_state)
  113. return -ENOSYS;
  114. ret = sb->s_qcop->get_state(sb, &state);
  115. if (ret)
  116. return ret;
  117. tstate = state.s_state + type;
  118. if (!(tstate->flags & QCI_ACCT_ENABLED))
  119. return -ESRCH;
  120. memset(&uinfo, 0, sizeof(uinfo));
  121. uinfo.dqi_bgrace = tstate->spc_timelimit;
  122. uinfo.dqi_igrace = tstate->ino_timelimit;
  123. if (tstate->flags & QCI_SYSFILE)
  124. uinfo.dqi_flags |= DQF_SYS_FILE;
  125. if (tstate->flags & QCI_ROOT_SQUASH)
  126. uinfo.dqi_flags |= DQF_ROOT_SQUASH;
  127. uinfo.dqi_valid = IIF_ALL;
  128. if (copy_to_user(addr, &uinfo, sizeof(uinfo)))
  129. return -EFAULT;
  130. return 0;
  131. }
  132. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  133. {
  134. struct if_dqinfo info;
  135. struct qc_info qinfo;
  136. if (copy_from_user(&info, addr, sizeof(info)))
  137. return -EFAULT;
  138. if (!sb->s_qcop->set_info)
  139. return -ENOSYS;
  140. if (info.dqi_valid & ~(IIF_FLAGS | IIF_BGRACE | IIF_IGRACE))
  141. return -EINVAL;
  142. memset(&qinfo, 0, sizeof(qinfo));
  143. if (info.dqi_valid & IIF_FLAGS) {
  144. if (info.dqi_flags & ~DQF_SETINFO_MASK)
  145. return -EINVAL;
  146. if (info.dqi_flags & DQF_ROOT_SQUASH)
  147. qinfo.i_flags |= QCI_ROOT_SQUASH;
  148. qinfo.i_fieldmask |= QC_FLAGS;
  149. }
  150. if (info.dqi_valid & IIF_BGRACE) {
  151. qinfo.i_spc_timelimit = info.dqi_bgrace;
  152. qinfo.i_fieldmask |= QC_SPC_TIMER;
  153. }
  154. if (info.dqi_valid & IIF_IGRACE) {
  155. qinfo.i_ino_timelimit = info.dqi_igrace;
  156. qinfo.i_fieldmask |= QC_INO_TIMER;
  157. }
  158. return sb->s_qcop->set_info(sb, type, &qinfo);
  159. }
  160. static inline qsize_t qbtos(qsize_t blocks)
  161. {
  162. return blocks << QIF_DQBLKSIZE_BITS;
  163. }
  164. static inline qsize_t stoqb(qsize_t space)
  165. {
  166. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  167. }
  168. static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
  169. {
  170. memset(dst, 0, sizeof(*dst));
  171. dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
  172. dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
  173. dst->dqb_curspace = src->d_space;
  174. dst->dqb_ihardlimit = src->d_ino_hardlimit;
  175. dst->dqb_isoftlimit = src->d_ino_softlimit;
  176. dst->dqb_curinodes = src->d_ino_count;
  177. dst->dqb_btime = src->d_spc_timer;
  178. dst->dqb_itime = src->d_ino_timer;
  179. dst->dqb_valid = QIF_ALL;
  180. }
  181. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  182. void __user *addr)
  183. {
  184. struct kqid qid;
  185. struct qc_dqblk fdq;
  186. struct if_dqblk idq;
  187. int ret;
  188. if (!sb->s_qcop->get_dqblk)
  189. return -ENOSYS;
  190. qid = make_kqid(current_user_ns(), type, id);
  191. if (!qid_has_mapping(sb->s_user_ns, qid))
  192. return -EINVAL;
  193. ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
  194. if (ret)
  195. return ret;
  196. copy_to_if_dqblk(&idq, &fdq);
  197. if (compat_need_64bit_alignment_fixup()) {
  198. struct compat_if_dqblk __user *compat_dqblk = addr;
  199. if (copy_to_user(compat_dqblk, &idq, sizeof(*compat_dqblk)))
  200. return -EFAULT;
  201. if (put_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
  202. return -EFAULT;
  203. } else {
  204. if (copy_to_user(addr, &idq, sizeof(idq)))
  205. return -EFAULT;
  206. }
  207. return 0;
  208. }
  209. /*
  210. * Return quota for next active quota >= this id, if any exists,
  211. * otherwise return -ENOENT via ->get_nextdqblk
  212. */
  213. static int quota_getnextquota(struct super_block *sb, int type, qid_t id,
  214. void __user *addr)
  215. {
  216. struct kqid qid;
  217. struct qc_dqblk fdq;
  218. struct if_nextdqblk idq;
  219. int ret;
  220. if (!sb->s_qcop->get_nextdqblk)
  221. return -ENOSYS;
  222. qid = make_kqid(current_user_ns(), type, id);
  223. if (!qid_has_mapping(sb->s_user_ns, qid))
  224. return -EINVAL;
  225. ret = sb->s_qcop->get_nextdqblk(sb, &qid, &fdq);
  226. if (ret)
  227. return ret;
  228. /* struct if_nextdqblk is a superset of struct if_dqblk */
  229. copy_to_if_dqblk((struct if_dqblk *)&idq, &fdq);
  230. idq.dqb_id = from_kqid(current_user_ns(), qid);
  231. if (copy_to_user(addr, &idq, sizeof(idq)))
  232. return -EFAULT;
  233. return 0;
  234. }
  235. static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
  236. {
  237. dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
  238. dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
  239. dst->d_space = src->dqb_curspace;
  240. dst->d_ino_hardlimit = src->dqb_ihardlimit;
  241. dst->d_ino_softlimit = src->dqb_isoftlimit;
  242. dst->d_ino_count = src->dqb_curinodes;
  243. dst->d_spc_timer = src->dqb_btime;
  244. dst->d_ino_timer = src->dqb_itime;
  245. dst->d_fieldmask = 0;
  246. if (src->dqb_valid & QIF_BLIMITS)
  247. dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
  248. if (src->dqb_valid & QIF_SPACE)
  249. dst->d_fieldmask |= QC_SPACE;
  250. if (src->dqb_valid & QIF_ILIMITS)
  251. dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
  252. if (src->dqb_valid & QIF_INODES)
  253. dst->d_fieldmask |= QC_INO_COUNT;
  254. if (src->dqb_valid & QIF_BTIME)
  255. dst->d_fieldmask |= QC_SPC_TIMER;
  256. if (src->dqb_valid & QIF_ITIME)
  257. dst->d_fieldmask |= QC_INO_TIMER;
  258. }
  259. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  260. void __user *addr)
  261. {
  262. struct qc_dqblk fdq;
  263. struct if_dqblk idq;
  264. struct kqid qid;
  265. if (compat_need_64bit_alignment_fixup()) {
  266. struct compat_if_dqblk __user *compat_dqblk = addr;
  267. if (copy_from_user(&idq, compat_dqblk, sizeof(*compat_dqblk)) ||
  268. get_user(idq.dqb_valid, &compat_dqblk->dqb_valid))
  269. return -EFAULT;
  270. } else {
  271. if (copy_from_user(&idq, addr, sizeof(idq)))
  272. return -EFAULT;
  273. }
  274. if (!sb->s_qcop->set_dqblk)
  275. return -ENOSYS;
  276. qid = make_kqid(current_user_ns(), type, id);
  277. if (!qid_has_mapping(sb->s_user_ns, qid))
  278. return -EINVAL;
  279. copy_from_if_dqblk(&fdq, &idq);
  280. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  281. }
  282. static int quota_enable(struct super_block *sb, void __user *addr)
  283. {
  284. __u32 flags;
  285. if (copy_from_user(&flags, addr, sizeof(flags)))
  286. return -EFAULT;
  287. if (!sb->s_qcop->quota_enable)
  288. return -ENOSYS;
  289. return sb->s_qcop->quota_enable(sb, flags);
  290. }
  291. static int quota_disable(struct super_block *sb, void __user *addr)
  292. {
  293. __u32 flags;
  294. if (copy_from_user(&flags, addr, sizeof(flags)))
  295. return -EFAULT;
  296. if (!sb->s_qcop->quota_disable)
  297. return -ENOSYS;
  298. return sb->s_qcop->quota_disable(sb, flags);
  299. }
  300. static int quota_state_to_flags(struct qc_state *state)
  301. {
  302. int flags = 0;
  303. if (state->s_state[USRQUOTA].flags & QCI_ACCT_ENABLED)
  304. flags |= FS_QUOTA_UDQ_ACCT;
  305. if (state->s_state[USRQUOTA].flags & QCI_LIMITS_ENFORCED)
  306. flags |= FS_QUOTA_UDQ_ENFD;
  307. if (state->s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)
  308. flags |= FS_QUOTA_GDQ_ACCT;
  309. if (state->s_state[GRPQUOTA].flags & QCI_LIMITS_ENFORCED)
  310. flags |= FS_QUOTA_GDQ_ENFD;
  311. if (state->s_state[PRJQUOTA].flags & QCI_ACCT_ENABLED)
  312. flags |= FS_QUOTA_PDQ_ACCT;
  313. if (state->s_state[PRJQUOTA].flags & QCI_LIMITS_ENFORCED)
  314. flags |= FS_QUOTA_PDQ_ENFD;
  315. return flags;
  316. }
  317. static int quota_getstate(struct super_block *sb, int type,
  318. struct fs_quota_stat *fqs)
  319. {
  320. struct qc_state state;
  321. int ret;
  322. memset(&state, 0, sizeof (struct qc_state));
  323. ret = sb->s_qcop->get_state(sb, &state);
  324. if (ret < 0)
  325. return ret;
  326. memset(fqs, 0, sizeof(*fqs));
  327. fqs->qs_version = FS_QSTAT_VERSION;
  328. fqs->qs_flags = quota_state_to_flags(&state);
  329. /* No quota enabled? */
  330. if (!fqs->qs_flags)
  331. return -ENOSYS;
  332. fqs->qs_incoredqs = state.s_incoredqs;
  333. fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
  334. fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
  335. fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
  336. fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
  337. fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
  338. /* Inodes may be allocated even if inactive; copy out if present */
  339. if (state.s_state[USRQUOTA].ino) {
  340. fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
  341. fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
  342. fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
  343. }
  344. if (state.s_state[GRPQUOTA].ino) {
  345. fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
  346. fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
  347. fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
  348. }
  349. if (state.s_state[PRJQUOTA].ino) {
  350. /*
  351. * Q_XGETQSTAT doesn't have room for both group and project
  352. * quotas. So, allow the project quota values to be copied out
  353. * only if there is no group quota information available.
  354. */
  355. if (!(state.s_state[GRPQUOTA].flags & QCI_ACCT_ENABLED)) {
  356. fqs->qs_gquota.qfs_ino = state.s_state[PRJQUOTA].ino;
  357. fqs->qs_gquota.qfs_nblks =
  358. state.s_state[PRJQUOTA].blocks;
  359. fqs->qs_gquota.qfs_nextents =
  360. state.s_state[PRJQUOTA].nextents;
  361. }
  362. }
  363. return 0;
  364. }
  365. static int compat_copy_fs_qfilestat(struct compat_fs_qfilestat __user *to,
  366. struct fs_qfilestat *from)
  367. {
  368. if (copy_to_user(to, from, sizeof(*to)) ||
  369. put_user(from->qfs_nextents, &to->qfs_nextents))
  370. return -EFAULT;
  371. return 0;
  372. }
  373. static int compat_copy_fs_quota_stat(struct compat_fs_quota_stat __user *to,
  374. struct fs_quota_stat *from)
  375. {
  376. if (put_user(from->qs_version, &to->qs_version) ||
  377. put_user(from->qs_flags, &to->qs_flags) ||
  378. put_user(from->qs_pad, &to->qs_pad) ||
  379. compat_copy_fs_qfilestat(&to->qs_uquota, &from->qs_uquota) ||
  380. compat_copy_fs_qfilestat(&to->qs_gquota, &from->qs_gquota) ||
  381. put_user(from->qs_incoredqs, &to->qs_incoredqs) ||
  382. put_user(from->qs_btimelimit, &to->qs_btimelimit) ||
  383. put_user(from->qs_itimelimit, &to->qs_itimelimit) ||
  384. put_user(from->qs_rtbtimelimit, &to->qs_rtbtimelimit) ||
  385. put_user(from->qs_bwarnlimit, &to->qs_bwarnlimit) ||
  386. put_user(from->qs_iwarnlimit, &to->qs_iwarnlimit))
  387. return -EFAULT;
  388. return 0;
  389. }
  390. static int quota_getxstate(struct super_block *sb, int type, void __user *addr)
  391. {
  392. struct fs_quota_stat fqs;
  393. int ret;
  394. if (!sb->s_qcop->get_state)
  395. return -ENOSYS;
  396. ret = quota_getstate(sb, type, &fqs);
  397. if (ret)
  398. return ret;
  399. if (compat_need_64bit_alignment_fixup())
  400. return compat_copy_fs_quota_stat(addr, &fqs);
  401. if (copy_to_user(addr, &fqs, sizeof(fqs)))
  402. return -EFAULT;
  403. return 0;
  404. }
  405. static int quota_getstatev(struct super_block *sb, int type,
  406. struct fs_quota_statv *fqs)
  407. {
  408. struct qc_state state;
  409. int ret;
  410. memset(&state, 0, sizeof (struct qc_state));
  411. ret = sb->s_qcop->get_state(sb, &state);
  412. if (ret < 0)
  413. return ret;
  414. memset(fqs, 0, sizeof(*fqs));
  415. fqs->qs_version = FS_QSTAT_VERSION;
  416. fqs->qs_flags = quota_state_to_flags(&state);
  417. /* No quota enabled? */
  418. if (!fqs->qs_flags)
  419. return -ENOSYS;
  420. fqs->qs_incoredqs = state.s_incoredqs;
  421. fqs->qs_btimelimit = state.s_state[type].spc_timelimit;
  422. fqs->qs_itimelimit = state.s_state[type].ino_timelimit;
  423. fqs->qs_rtbtimelimit = state.s_state[type].rt_spc_timelimit;
  424. fqs->qs_bwarnlimit = state.s_state[type].spc_warnlimit;
  425. fqs->qs_iwarnlimit = state.s_state[type].ino_warnlimit;
  426. fqs->qs_rtbwarnlimit = state.s_state[type].rt_spc_warnlimit;
  427. /* Inodes may be allocated even if inactive; copy out if present */
  428. if (state.s_state[USRQUOTA].ino) {
  429. fqs->qs_uquota.qfs_ino = state.s_state[USRQUOTA].ino;
  430. fqs->qs_uquota.qfs_nblks = state.s_state[USRQUOTA].blocks;
  431. fqs->qs_uquota.qfs_nextents = state.s_state[USRQUOTA].nextents;
  432. }
  433. if (state.s_state[GRPQUOTA].ino) {
  434. fqs->qs_gquota.qfs_ino = state.s_state[GRPQUOTA].ino;
  435. fqs->qs_gquota.qfs_nblks = state.s_state[GRPQUOTA].blocks;
  436. fqs->qs_gquota.qfs_nextents = state.s_state[GRPQUOTA].nextents;
  437. }
  438. if (state.s_state[PRJQUOTA].ino) {
  439. fqs->qs_pquota.qfs_ino = state.s_state[PRJQUOTA].ino;
  440. fqs->qs_pquota.qfs_nblks = state.s_state[PRJQUOTA].blocks;
  441. fqs->qs_pquota.qfs_nextents = state.s_state[PRJQUOTA].nextents;
  442. }
  443. return 0;
  444. }
  445. static int quota_getxstatev(struct super_block *sb, int type, void __user *addr)
  446. {
  447. struct fs_quota_statv fqs;
  448. int ret;
  449. if (!sb->s_qcop->get_state)
  450. return -ENOSYS;
  451. memset(&fqs, 0, sizeof(fqs));
  452. if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
  453. return -EFAULT;
  454. /* If this kernel doesn't support user specified version, fail */
  455. switch (fqs.qs_version) {
  456. case FS_QSTATV_VERSION1:
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. ret = quota_getstatev(sb, type, &fqs);
  462. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  463. return -EFAULT;
  464. return ret;
  465. }
  466. /*
  467. * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
  468. * out of there as xfsprogs rely on definitions being in that header file. So
  469. * just define same functions here for quota purposes.
  470. */
  471. #define XFS_BB_SHIFT 9
  472. static inline u64 quota_bbtob(u64 blocks)
  473. {
  474. return blocks << XFS_BB_SHIFT;
  475. }
  476. static inline u64 quota_btobb(u64 bytes)
  477. {
  478. return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
  479. }
  480. static inline s64 copy_from_xfs_dqblk_ts(const struct fs_disk_quota *d,
  481. __s32 timer, __s8 timer_hi)
  482. {
  483. if (d->d_fieldmask & FS_DQ_BIGTIME)
  484. return (u32)timer | (s64)timer_hi << 32;
  485. return timer;
  486. }
  487. static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
  488. {
  489. dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
  490. dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
  491. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  492. dst->d_ino_softlimit = src->d_ino_softlimit;
  493. dst->d_space = quota_bbtob(src->d_bcount);
  494. dst->d_ino_count = src->d_icount;
  495. dst->d_ino_timer = copy_from_xfs_dqblk_ts(src, src->d_itimer,
  496. src->d_itimer_hi);
  497. dst->d_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_btimer,
  498. src->d_btimer_hi);
  499. dst->d_ino_warns = src->d_iwarns;
  500. dst->d_spc_warns = src->d_bwarns;
  501. dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
  502. dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
  503. dst->d_rt_space = quota_bbtob(src->d_rtbcount);
  504. dst->d_rt_spc_timer = copy_from_xfs_dqblk_ts(src, src->d_rtbtimer,
  505. src->d_rtbtimer_hi);
  506. dst->d_rt_spc_warns = src->d_rtbwarns;
  507. dst->d_fieldmask = 0;
  508. if (src->d_fieldmask & FS_DQ_ISOFT)
  509. dst->d_fieldmask |= QC_INO_SOFT;
  510. if (src->d_fieldmask & FS_DQ_IHARD)
  511. dst->d_fieldmask |= QC_INO_HARD;
  512. if (src->d_fieldmask & FS_DQ_BSOFT)
  513. dst->d_fieldmask |= QC_SPC_SOFT;
  514. if (src->d_fieldmask & FS_DQ_BHARD)
  515. dst->d_fieldmask |= QC_SPC_HARD;
  516. if (src->d_fieldmask & FS_DQ_RTBSOFT)
  517. dst->d_fieldmask |= QC_RT_SPC_SOFT;
  518. if (src->d_fieldmask & FS_DQ_RTBHARD)
  519. dst->d_fieldmask |= QC_RT_SPC_HARD;
  520. if (src->d_fieldmask & FS_DQ_BTIMER)
  521. dst->d_fieldmask |= QC_SPC_TIMER;
  522. if (src->d_fieldmask & FS_DQ_ITIMER)
  523. dst->d_fieldmask |= QC_INO_TIMER;
  524. if (src->d_fieldmask & FS_DQ_RTBTIMER)
  525. dst->d_fieldmask |= QC_RT_SPC_TIMER;
  526. if (src->d_fieldmask & FS_DQ_BWARNS)
  527. dst->d_fieldmask |= QC_SPC_WARNS;
  528. if (src->d_fieldmask & FS_DQ_IWARNS)
  529. dst->d_fieldmask |= QC_INO_WARNS;
  530. if (src->d_fieldmask & FS_DQ_RTBWARNS)
  531. dst->d_fieldmask |= QC_RT_SPC_WARNS;
  532. if (src->d_fieldmask & FS_DQ_BCOUNT)
  533. dst->d_fieldmask |= QC_SPACE;
  534. if (src->d_fieldmask & FS_DQ_ICOUNT)
  535. dst->d_fieldmask |= QC_INO_COUNT;
  536. if (src->d_fieldmask & FS_DQ_RTBCOUNT)
  537. dst->d_fieldmask |= QC_RT_SPACE;
  538. }
  539. static void copy_qcinfo_from_xfs_dqblk(struct qc_info *dst,
  540. struct fs_disk_quota *src)
  541. {
  542. memset(dst, 0, sizeof(*dst));
  543. dst->i_spc_timelimit = src->d_btimer;
  544. dst->i_ino_timelimit = src->d_itimer;
  545. dst->i_rt_spc_timelimit = src->d_rtbtimer;
  546. dst->i_ino_warnlimit = src->d_iwarns;
  547. dst->i_spc_warnlimit = src->d_bwarns;
  548. dst->i_rt_spc_warnlimit = src->d_rtbwarns;
  549. if (src->d_fieldmask & FS_DQ_BWARNS)
  550. dst->i_fieldmask |= QC_SPC_WARNS;
  551. if (src->d_fieldmask & FS_DQ_IWARNS)
  552. dst->i_fieldmask |= QC_INO_WARNS;
  553. if (src->d_fieldmask & FS_DQ_RTBWARNS)
  554. dst->i_fieldmask |= QC_RT_SPC_WARNS;
  555. if (src->d_fieldmask & FS_DQ_BTIMER)
  556. dst->i_fieldmask |= QC_SPC_TIMER;
  557. if (src->d_fieldmask & FS_DQ_ITIMER)
  558. dst->i_fieldmask |= QC_INO_TIMER;
  559. if (src->d_fieldmask & FS_DQ_RTBTIMER)
  560. dst->i_fieldmask |= QC_RT_SPC_TIMER;
  561. }
  562. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  563. void __user *addr)
  564. {
  565. struct fs_disk_quota fdq;
  566. struct qc_dqblk qdq;
  567. struct kqid qid;
  568. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  569. return -EFAULT;
  570. if (!sb->s_qcop->set_dqblk)
  571. return -ENOSYS;
  572. qid = make_kqid(current_user_ns(), type, id);
  573. if (!qid_has_mapping(sb->s_user_ns, qid))
  574. return -EINVAL;
  575. /* Are we actually setting timer / warning limits for all users? */
  576. if (from_kqid(sb->s_user_ns, qid) == 0 &&
  577. fdq.d_fieldmask & (FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK)) {
  578. struct qc_info qinfo;
  579. int ret;
  580. if (!sb->s_qcop->set_info)
  581. return -EINVAL;
  582. copy_qcinfo_from_xfs_dqblk(&qinfo, &fdq);
  583. ret = sb->s_qcop->set_info(sb, type, &qinfo);
  584. if (ret)
  585. return ret;
  586. /* These are already done */
  587. fdq.d_fieldmask &= ~(FS_DQ_WARNS_MASK | FS_DQ_TIMER_MASK);
  588. }
  589. copy_from_xfs_dqblk(&qdq, &fdq);
  590. return sb->s_qcop->set_dqblk(sb, qid, &qdq);
  591. }
  592. static inline void copy_to_xfs_dqblk_ts(const struct fs_disk_quota *d,
  593. __s32 *timer_lo, __s8 *timer_hi, s64 timer)
  594. {
  595. *timer_lo = timer;
  596. if (d->d_fieldmask & FS_DQ_BIGTIME)
  597. *timer_hi = timer >> 32;
  598. }
  599. static inline bool want_bigtime(s64 timer)
  600. {
  601. return timer > S32_MAX || timer < S32_MIN;
  602. }
  603. static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
  604. int type, qid_t id)
  605. {
  606. memset(dst, 0, sizeof(*dst));
  607. if (want_bigtime(src->d_ino_timer) || want_bigtime(src->d_spc_timer) ||
  608. want_bigtime(src->d_rt_spc_timer))
  609. dst->d_fieldmask |= FS_DQ_BIGTIME;
  610. dst->d_version = FS_DQUOT_VERSION;
  611. dst->d_id = id;
  612. if (type == USRQUOTA)
  613. dst->d_flags = FS_USER_QUOTA;
  614. else if (type == PRJQUOTA)
  615. dst->d_flags = FS_PROJ_QUOTA;
  616. else
  617. dst->d_flags = FS_GROUP_QUOTA;
  618. dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
  619. dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
  620. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  621. dst->d_ino_softlimit = src->d_ino_softlimit;
  622. dst->d_bcount = quota_btobb(src->d_space);
  623. dst->d_icount = src->d_ino_count;
  624. copy_to_xfs_dqblk_ts(dst, &dst->d_itimer, &dst->d_itimer_hi,
  625. src->d_ino_timer);
  626. copy_to_xfs_dqblk_ts(dst, &dst->d_btimer, &dst->d_btimer_hi,
  627. src->d_spc_timer);
  628. dst->d_iwarns = src->d_ino_warns;
  629. dst->d_bwarns = src->d_spc_warns;
  630. dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
  631. dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
  632. dst->d_rtbcount = quota_btobb(src->d_rt_space);
  633. copy_to_xfs_dqblk_ts(dst, &dst->d_rtbtimer, &dst->d_rtbtimer_hi,
  634. src->d_rt_spc_timer);
  635. dst->d_rtbwarns = src->d_rt_spc_warns;
  636. }
  637. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  638. void __user *addr)
  639. {
  640. struct fs_disk_quota fdq;
  641. struct qc_dqblk qdq;
  642. struct kqid qid;
  643. int ret;
  644. if (!sb->s_qcop->get_dqblk)
  645. return -ENOSYS;
  646. qid = make_kqid(current_user_ns(), type, id);
  647. if (!qid_has_mapping(sb->s_user_ns, qid))
  648. return -EINVAL;
  649. ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
  650. if (ret)
  651. return ret;
  652. copy_to_xfs_dqblk(&fdq, &qdq, type, id);
  653. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  654. return -EFAULT;
  655. return ret;
  656. }
  657. /*
  658. * Return quota for next active quota >= this id, if any exists,
  659. * otherwise return -ENOENT via ->get_nextdqblk.
  660. */
  661. static int quota_getnextxquota(struct super_block *sb, int type, qid_t id,
  662. void __user *addr)
  663. {
  664. struct fs_disk_quota fdq;
  665. struct qc_dqblk qdq;
  666. struct kqid qid;
  667. qid_t id_out;
  668. int ret;
  669. if (!sb->s_qcop->get_nextdqblk)
  670. return -ENOSYS;
  671. qid = make_kqid(current_user_ns(), type, id);
  672. if (!qid_has_mapping(sb->s_user_ns, qid))
  673. return -EINVAL;
  674. ret = sb->s_qcop->get_nextdqblk(sb, &qid, &qdq);
  675. if (ret)
  676. return ret;
  677. id_out = from_kqid(current_user_ns(), qid);
  678. copy_to_xfs_dqblk(&fdq, &qdq, type, id_out);
  679. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  680. return -EFAULT;
  681. return ret;
  682. }
  683. static int quota_rmxquota(struct super_block *sb, void __user *addr)
  684. {
  685. __u32 flags;
  686. if (copy_from_user(&flags, addr, sizeof(flags)))
  687. return -EFAULT;
  688. if (!sb->s_qcop->rm_xquota)
  689. return -ENOSYS;
  690. return sb->s_qcop->rm_xquota(sb, flags);
  691. }
  692. /* Copy parameters and call proper function */
  693. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  694. void __user *addr, const struct path *path)
  695. {
  696. int ret;
  697. type = array_index_nospec(type, MAXQUOTAS);
  698. /*
  699. * Quota not supported on this fs? Check this before s_quota_types
  700. * since they needn't be set if quota is not supported at all.
  701. */
  702. if (!sb->s_qcop)
  703. return -ENOSYS;
  704. if (!(sb->s_quota_types & (1 << type)))
  705. return -EINVAL;
  706. ret = check_quotactl_permission(sb, type, cmd, id);
  707. if (ret < 0)
  708. return ret;
  709. switch (cmd) {
  710. case Q_QUOTAON:
  711. return quota_quotaon(sb, type, id, path);
  712. case Q_QUOTAOFF:
  713. return quota_quotaoff(sb, type);
  714. case Q_GETFMT:
  715. return quota_getfmt(sb, type, addr);
  716. case Q_GETINFO:
  717. return quota_getinfo(sb, type, addr);
  718. case Q_SETINFO:
  719. return quota_setinfo(sb, type, addr);
  720. case Q_GETQUOTA:
  721. return quota_getquota(sb, type, id, addr);
  722. case Q_GETNEXTQUOTA:
  723. return quota_getnextquota(sb, type, id, addr);
  724. case Q_SETQUOTA:
  725. return quota_setquota(sb, type, id, addr);
  726. case Q_SYNC:
  727. if (!sb->s_qcop->quota_sync)
  728. return -ENOSYS;
  729. return sb->s_qcop->quota_sync(sb, type);
  730. case Q_XQUOTAON:
  731. return quota_enable(sb, addr);
  732. case Q_XQUOTAOFF:
  733. return quota_disable(sb, addr);
  734. case Q_XQUOTARM:
  735. return quota_rmxquota(sb, addr);
  736. case Q_XGETQSTAT:
  737. return quota_getxstate(sb, type, addr);
  738. case Q_XGETQSTATV:
  739. return quota_getxstatev(sb, type, addr);
  740. case Q_XSETQLIM:
  741. return quota_setxquota(sb, type, id, addr);
  742. case Q_XGETQUOTA:
  743. return quota_getxquota(sb, type, id, addr);
  744. case Q_XGETNEXTQUOTA:
  745. return quota_getnextxquota(sb, type, id, addr);
  746. case Q_XQUOTASYNC:
  747. if (sb_rdonly(sb))
  748. return -EROFS;
  749. /* XFS quotas are fully coherent now, making this call a noop */
  750. return 0;
  751. default:
  752. return -EINVAL;
  753. }
  754. }
  755. /* Return 1 if 'cmd' will block on frozen filesystem */
  756. static int quotactl_cmd_write(int cmd)
  757. {
  758. /*
  759. * We cannot allow Q_GETQUOTA and Q_GETNEXTQUOTA without write access
  760. * as dquot_acquire() may allocate space for new structure and OCFS2
  761. * needs to increment on-disk use count.
  762. */
  763. switch (cmd) {
  764. case Q_GETFMT:
  765. case Q_GETINFO:
  766. case Q_SYNC:
  767. case Q_XGETQSTAT:
  768. case Q_XGETQSTATV:
  769. case Q_XGETQUOTA:
  770. case Q_XGETNEXTQUOTA:
  771. case Q_XQUOTASYNC:
  772. return 0;
  773. }
  774. return 1;
  775. }
  776. /* Return true if quotactl command is manipulating quota on/off state */
  777. static bool quotactl_cmd_onoff(int cmd)
  778. {
  779. return (cmd == Q_QUOTAON) || (cmd == Q_QUOTAOFF) ||
  780. (cmd == Q_XQUOTAON) || (cmd == Q_XQUOTAOFF);
  781. }
  782. /*
  783. * look up a superblock on which quota ops will be performed
  784. * - use the name of a block device to find the superblock thereon
  785. */
  786. static struct super_block *quotactl_block(const char __user *special, int cmd)
  787. {
  788. #ifdef CONFIG_BLOCK
  789. struct super_block *sb;
  790. struct filename *tmp = getname(special);
  791. bool excl = false, thawed = false;
  792. int error;
  793. dev_t dev;
  794. if (IS_ERR(tmp))
  795. return ERR_CAST(tmp);
  796. error = lookup_bdev(tmp->name, &dev);
  797. putname(tmp);
  798. if (error)
  799. return ERR_PTR(error);
  800. if (quotactl_cmd_onoff(cmd)) {
  801. excl = true;
  802. thawed = true;
  803. } else if (quotactl_cmd_write(cmd)) {
  804. thawed = true;
  805. }
  806. retry:
  807. sb = user_get_super(dev, excl);
  808. if (!sb)
  809. return ERR_PTR(-ENODEV);
  810. if (thawed && sb->s_writers.frozen != SB_UNFROZEN) {
  811. if (excl)
  812. up_write(&sb->s_umount);
  813. else
  814. up_read(&sb->s_umount);
  815. wait_event(sb->s_writers.wait_unfrozen,
  816. sb->s_writers.frozen == SB_UNFROZEN);
  817. put_super(sb);
  818. goto retry;
  819. }
  820. return sb;
  821. #else
  822. return ERR_PTR(-ENODEV);
  823. #endif
  824. }
  825. /*
  826. * This is the system call interface. This communicates with
  827. * the user-level programs. Currently this only supports diskquota
  828. * calls. Maybe we need to add the process quotas etc. in the future,
  829. * but we probably should use rlimits for that.
  830. */
  831. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  832. qid_t, id, void __user *, addr)
  833. {
  834. uint cmds, type;
  835. struct super_block *sb = NULL;
  836. struct path path, *pathp = NULL;
  837. int ret;
  838. cmds = cmd >> SUBCMDSHIFT;
  839. type = cmd & SUBCMDMASK;
  840. if (type >= MAXQUOTAS)
  841. return -EINVAL;
  842. /*
  843. * As a special case Q_SYNC can be called without a specific device.
  844. * It will iterate all superblocks that have quota enabled and call
  845. * the sync action on each of them.
  846. */
  847. if (!special) {
  848. if (cmds == Q_SYNC)
  849. return quota_sync_all(type);
  850. return -ENODEV;
  851. }
  852. /*
  853. * Path for quotaon has to be resolved before grabbing superblock
  854. * because that gets s_umount sem which is also possibly needed by path
  855. * resolution (think about autofs) and thus deadlocks could arise.
  856. */
  857. if (cmds == Q_QUOTAON) {
  858. ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  859. if (ret)
  860. pathp = ERR_PTR(ret);
  861. else
  862. pathp = &path;
  863. }
  864. sb = quotactl_block(special, cmds);
  865. if (IS_ERR(sb)) {
  866. ret = PTR_ERR(sb);
  867. goto out;
  868. }
  869. ret = do_quotactl(sb, type, cmds, id, addr, pathp);
  870. if (!quotactl_cmd_onoff(cmds))
  871. drop_super(sb);
  872. else
  873. drop_super_exclusive(sb);
  874. out:
  875. if (pathp && !IS_ERR(pathp))
  876. path_put(pathp);
  877. return ret;
  878. }
  879. SYSCALL_DEFINE4(quotactl_fd, unsigned int, fd, unsigned int, cmd,
  880. qid_t, id, void __user *, addr)
  881. {
  882. struct super_block *sb;
  883. unsigned int cmds = cmd >> SUBCMDSHIFT;
  884. unsigned int type = cmd & SUBCMDMASK;
  885. struct fd f;
  886. int ret;
  887. f = fdget_raw(fd);
  888. if (!f.file)
  889. return -EBADF;
  890. ret = -EINVAL;
  891. if (type >= MAXQUOTAS)
  892. goto out;
  893. if (quotactl_cmd_write(cmds)) {
  894. ret = mnt_want_write(f.file->f_path.mnt);
  895. if (ret)
  896. goto out;
  897. }
  898. sb = f.file->f_path.mnt->mnt_sb;
  899. if (quotactl_cmd_onoff(cmds))
  900. down_write(&sb->s_umount);
  901. else
  902. down_read(&sb->s_umount);
  903. ret = do_quotactl(sb, type, cmds, id, addr, ERR_PTR(-EINVAL));
  904. if (quotactl_cmd_onoff(cmds))
  905. up_write(&sb->s_umount);
  906. else
  907. up_read(&sb->s_umount);
  908. if (quotactl_cmd_write(cmds))
  909. mnt_drop_write(f.file->f_path.mnt);
  910. out:
  911. fdput(f);
  912. return ret;
  913. }