xfs_trans_dquot.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_error.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_trans_priv.h"
  17. #include "xfs_quota.h"
  18. #include "xfs_qm.h"
  19. STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *);
  20. /*
  21. * Add the locked dquot to the transaction.
  22. * The dquot must be locked, and it cannot be associated with any
  23. * transaction.
  24. */
  25. void
  26. xfs_trans_dqjoin(
  27. xfs_trans_t *tp,
  28. xfs_dquot_t *dqp)
  29. {
  30. ASSERT(dqp->q_transp != tp);
  31. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  32. ASSERT(dqp->q_logitem.qli_dquot == dqp);
  33. /*
  34. * Get a log_item_desc to point at the new item.
  35. */
  36. xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
  37. /*
  38. * Initialize d_transp so we can later determine if this dquot is
  39. * associated with this transaction.
  40. */
  41. dqp->q_transp = tp;
  42. }
  43. /*
  44. * This is called to mark the dquot as needing
  45. * to be logged when the transaction is committed. The dquot must
  46. * already be associated with the given transaction.
  47. * Note that it marks the entire transaction as dirty. In the ordinary
  48. * case, this gets called via xfs_trans_commit, after the transaction
  49. * is already dirty. However, there's nothing stop this from getting
  50. * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
  51. * flag.
  52. */
  53. void
  54. xfs_trans_log_dquot(
  55. xfs_trans_t *tp,
  56. xfs_dquot_t *dqp)
  57. {
  58. ASSERT(dqp->q_transp == tp);
  59. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  60. tp->t_flags |= XFS_TRANS_DIRTY;
  61. set_bit(XFS_LI_DIRTY, &dqp->q_logitem.qli_item.li_flags);
  62. }
  63. /*
  64. * Carry forward whatever is left of the quota blk reservation to
  65. * the spanky new transaction
  66. */
  67. void
  68. xfs_trans_dup_dqinfo(
  69. xfs_trans_t *otp,
  70. xfs_trans_t *ntp)
  71. {
  72. xfs_dqtrx_t *oq, *nq;
  73. int i, j;
  74. xfs_dqtrx_t *oqa, *nqa;
  75. ulong blk_res_used;
  76. if (!otp->t_dqinfo)
  77. return;
  78. xfs_trans_alloc_dqinfo(ntp);
  79. /*
  80. * Because the quota blk reservation is carried forward,
  81. * it is also necessary to carry forward the DQ_DIRTY flag.
  82. */
  83. if (otp->t_flags & XFS_TRANS_DQ_DIRTY)
  84. ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
  85. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  86. oqa = otp->t_dqinfo->dqs[j];
  87. nqa = ntp->t_dqinfo->dqs[j];
  88. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  89. blk_res_used = 0;
  90. if (oqa[i].qt_dquot == NULL)
  91. break;
  92. oq = &oqa[i];
  93. nq = &nqa[i];
  94. if (oq->qt_blk_res && oq->qt_bcount_delta > 0)
  95. blk_res_used = oq->qt_bcount_delta;
  96. nq->qt_dquot = oq->qt_dquot;
  97. nq->qt_bcount_delta = nq->qt_icount_delta = 0;
  98. nq->qt_rtbcount_delta = 0;
  99. /*
  100. * Transfer whatever is left of the reservations.
  101. */
  102. nq->qt_blk_res = oq->qt_blk_res - blk_res_used;
  103. oq->qt_blk_res = blk_res_used;
  104. nq->qt_rtblk_res = oq->qt_rtblk_res -
  105. oq->qt_rtblk_res_used;
  106. oq->qt_rtblk_res = oq->qt_rtblk_res_used;
  107. nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
  108. oq->qt_ino_res = oq->qt_ino_res_used;
  109. }
  110. }
  111. }
  112. /*
  113. * Wrap around mod_dquot to account for both user and group quotas.
  114. */
  115. void
  116. xfs_trans_mod_dquot_byino(
  117. xfs_trans_t *tp,
  118. xfs_inode_t *ip,
  119. uint field,
  120. long delta)
  121. {
  122. xfs_mount_t *mp = tp->t_mountp;
  123. if (!XFS_IS_QUOTA_RUNNING(mp) ||
  124. !XFS_IS_QUOTA_ON(mp) ||
  125. xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
  126. return;
  127. if (tp->t_dqinfo == NULL)
  128. xfs_trans_alloc_dqinfo(tp);
  129. if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
  130. (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
  131. if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
  132. (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
  133. if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
  134. (void) xfs_trans_mod_dquot(tp, ip->i_pdquot, field, delta);
  135. }
  136. STATIC struct xfs_dqtrx *
  137. xfs_trans_get_dqtrx(
  138. struct xfs_trans *tp,
  139. struct xfs_dquot *dqp)
  140. {
  141. int i;
  142. struct xfs_dqtrx *qa;
  143. if (XFS_QM_ISUDQ(dqp))
  144. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
  145. else if (XFS_QM_ISGDQ(dqp))
  146. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
  147. else if (XFS_QM_ISPDQ(dqp))
  148. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
  149. else
  150. return NULL;
  151. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  152. if (qa[i].qt_dquot == NULL ||
  153. qa[i].qt_dquot == dqp)
  154. return &qa[i];
  155. }
  156. return NULL;
  157. }
  158. /*
  159. * Make the changes in the transaction structure.
  160. * The moral equivalent to xfs_trans_mod_sb().
  161. * We don't touch any fields in the dquot, so we don't care
  162. * if it's locked or not (most of the time it won't be).
  163. */
  164. void
  165. xfs_trans_mod_dquot(
  166. xfs_trans_t *tp,
  167. xfs_dquot_t *dqp,
  168. uint field,
  169. long delta)
  170. {
  171. xfs_dqtrx_t *qtrx;
  172. ASSERT(tp);
  173. ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
  174. qtrx = NULL;
  175. if (tp->t_dqinfo == NULL)
  176. xfs_trans_alloc_dqinfo(tp);
  177. /*
  178. * Find either the first free slot or the slot that belongs
  179. * to this dquot.
  180. */
  181. qtrx = xfs_trans_get_dqtrx(tp, dqp);
  182. ASSERT(qtrx);
  183. if (qtrx->qt_dquot == NULL)
  184. qtrx->qt_dquot = dqp;
  185. switch (field) {
  186. /*
  187. * regular disk blk reservation
  188. */
  189. case XFS_TRANS_DQ_RES_BLKS:
  190. qtrx->qt_blk_res += (ulong)delta;
  191. break;
  192. /*
  193. * inode reservation
  194. */
  195. case XFS_TRANS_DQ_RES_INOS:
  196. qtrx->qt_ino_res += (ulong)delta;
  197. break;
  198. /*
  199. * disk blocks used.
  200. */
  201. case XFS_TRANS_DQ_BCOUNT:
  202. qtrx->qt_bcount_delta += delta;
  203. break;
  204. case XFS_TRANS_DQ_DELBCOUNT:
  205. qtrx->qt_delbcnt_delta += delta;
  206. break;
  207. /*
  208. * Inode Count
  209. */
  210. case XFS_TRANS_DQ_ICOUNT:
  211. if (qtrx->qt_ino_res && delta > 0) {
  212. qtrx->qt_ino_res_used += (ulong)delta;
  213. ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
  214. }
  215. qtrx->qt_icount_delta += delta;
  216. break;
  217. /*
  218. * rtblk reservation
  219. */
  220. case XFS_TRANS_DQ_RES_RTBLKS:
  221. qtrx->qt_rtblk_res += (ulong)delta;
  222. break;
  223. /*
  224. * rtblk count
  225. */
  226. case XFS_TRANS_DQ_RTBCOUNT:
  227. if (qtrx->qt_rtblk_res && delta > 0) {
  228. qtrx->qt_rtblk_res_used += (ulong)delta;
  229. ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
  230. }
  231. qtrx->qt_rtbcount_delta += delta;
  232. break;
  233. case XFS_TRANS_DQ_DELRTBCOUNT:
  234. qtrx->qt_delrtb_delta += delta;
  235. break;
  236. default:
  237. ASSERT(0);
  238. }
  239. tp->t_flags |= XFS_TRANS_DQ_DIRTY;
  240. }
  241. /*
  242. * Given an array of dqtrx structures, lock all the dquots associated and join
  243. * them to the transaction, provided they have been modified. We know that the
  244. * highest number of dquots of one type - usr, grp and prj - involved in a
  245. * transaction is 3 so we don't need to make this very generic.
  246. */
  247. STATIC void
  248. xfs_trans_dqlockedjoin(
  249. xfs_trans_t *tp,
  250. xfs_dqtrx_t *q)
  251. {
  252. ASSERT(q[0].qt_dquot != NULL);
  253. if (q[1].qt_dquot == NULL) {
  254. xfs_dqlock(q[0].qt_dquot);
  255. xfs_trans_dqjoin(tp, q[0].qt_dquot);
  256. } else {
  257. ASSERT(XFS_QM_TRANS_MAXDQS == 2);
  258. xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
  259. xfs_trans_dqjoin(tp, q[0].qt_dquot);
  260. xfs_trans_dqjoin(tp, q[1].qt_dquot);
  261. }
  262. }
  263. /*
  264. * Called by xfs_trans_commit() and similar in spirit to
  265. * xfs_trans_apply_sb_deltas().
  266. * Go thru all the dquots belonging to this transaction and modify the
  267. * INCORE dquot to reflect the actual usages.
  268. * Unreserve just the reservations done by this transaction.
  269. * dquot is still left locked at exit.
  270. */
  271. void
  272. xfs_trans_apply_dquot_deltas(
  273. struct xfs_trans *tp)
  274. {
  275. int i, j;
  276. struct xfs_dquot *dqp;
  277. struct xfs_dqtrx *qtrx, *qa;
  278. struct xfs_disk_dquot *d;
  279. long totalbdelta;
  280. long totalrtbdelta;
  281. if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
  282. return;
  283. ASSERT(tp->t_dqinfo);
  284. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  285. qa = tp->t_dqinfo->dqs[j];
  286. if (qa[0].qt_dquot == NULL)
  287. continue;
  288. /*
  289. * Lock all of the dquots and join them to the transaction.
  290. */
  291. xfs_trans_dqlockedjoin(tp, qa);
  292. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  293. qtrx = &qa[i];
  294. /*
  295. * The array of dquots is filled
  296. * sequentially, not sparsely.
  297. */
  298. if ((dqp = qtrx->qt_dquot) == NULL)
  299. break;
  300. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  301. ASSERT(dqp->q_transp == tp);
  302. /*
  303. * adjust the actual number of blocks used
  304. */
  305. d = &dqp->q_core;
  306. /*
  307. * The issue here is - sometimes we don't make a blkquota
  308. * reservation intentionally to be fair to users
  309. * (when the amount is small). On the other hand,
  310. * delayed allocs do make reservations, but that's
  311. * outside of a transaction, so we have no
  312. * idea how much was really reserved.
  313. * So, here we've accumulated delayed allocation blks and
  314. * non-delay blks. The assumption is that the
  315. * delayed ones are always reserved (outside of a
  316. * transaction), and the others may or may not have
  317. * quota reservations.
  318. */
  319. totalbdelta = qtrx->qt_bcount_delta +
  320. qtrx->qt_delbcnt_delta;
  321. totalrtbdelta = qtrx->qt_rtbcount_delta +
  322. qtrx->qt_delrtb_delta;
  323. #ifdef DEBUG
  324. if (totalbdelta < 0)
  325. ASSERT(be64_to_cpu(d->d_bcount) >=
  326. -totalbdelta);
  327. if (totalrtbdelta < 0)
  328. ASSERT(be64_to_cpu(d->d_rtbcount) >=
  329. -totalrtbdelta);
  330. if (qtrx->qt_icount_delta < 0)
  331. ASSERT(be64_to_cpu(d->d_icount) >=
  332. -qtrx->qt_icount_delta);
  333. #endif
  334. if (totalbdelta)
  335. be64_add_cpu(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
  336. if (qtrx->qt_icount_delta)
  337. be64_add_cpu(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
  338. if (totalrtbdelta)
  339. be64_add_cpu(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
  340. /*
  341. * Get any default limits in use.
  342. * Start/reset the timer(s) if needed.
  343. */
  344. if (d->d_id) {
  345. xfs_qm_adjust_dqlimits(tp->t_mountp, dqp);
  346. xfs_qm_adjust_dqtimers(tp->t_mountp, d);
  347. }
  348. dqp->dq_flags |= XFS_DQ_DIRTY;
  349. /*
  350. * add this to the list of items to get logged
  351. */
  352. xfs_trans_log_dquot(tp, dqp);
  353. /*
  354. * Take off what's left of the original reservation.
  355. * In case of delayed allocations, there's no
  356. * reservation that a transaction structure knows of.
  357. */
  358. if (qtrx->qt_blk_res != 0) {
  359. ulong blk_res_used = 0;
  360. if (qtrx->qt_bcount_delta > 0)
  361. blk_res_used = qtrx->qt_bcount_delta;
  362. if (qtrx->qt_blk_res != blk_res_used) {
  363. if (qtrx->qt_blk_res > blk_res_used)
  364. dqp->q_res_bcount -= (xfs_qcnt_t)
  365. (qtrx->qt_blk_res -
  366. blk_res_used);
  367. else
  368. dqp->q_res_bcount -= (xfs_qcnt_t)
  369. (blk_res_used -
  370. qtrx->qt_blk_res);
  371. }
  372. } else {
  373. /*
  374. * These blks were never reserved, either inside
  375. * a transaction or outside one (in a delayed
  376. * allocation). Also, this isn't always a
  377. * negative number since we sometimes
  378. * deliberately skip quota reservations.
  379. */
  380. if (qtrx->qt_bcount_delta) {
  381. dqp->q_res_bcount +=
  382. (xfs_qcnt_t)qtrx->qt_bcount_delta;
  383. }
  384. }
  385. /*
  386. * Adjust the RT reservation.
  387. */
  388. if (qtrx->qt_rtblk_res != 0) {
  389. if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
  390. if (qtrx->qt_rtblk_res >
  391. qtrx->qt_rtblk_res_used)
  392. dqp->q_res_rtbcount -= (xfs_qcnt_t)
  393. (qtrx->qt_rtblk_res -
  394. qtrx->qt_rtblk_res_used);
  395. else
  396. dqp->q_res_rtbcount -= (xfs_qcnt_t)
  397. (qtrx->qt_rtblk_res_used -
  398. qtrx->qt_rtblk_res);
  399. }
  400. } else {
  401. if (qtrx->qt_rtbcount_delta)
  402. dqp->q_res_rtbcount +=
  403. (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
  404. }
  405. /*
  406. * Adjust the inode reservation.
  407. */
  408. if (qtrx->qt_ino_res != 0) {
  409. ASSERT(qtrx->qt_ino_res >=
  410. qtrx->qt_ino_res_used);
  411. if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
  412. dqp->q_res_icount -= (xfs_qcnt_t)
  413. (qtrx->qt_ino_res -
  414. qtrx->qt_ino_res_used);
  415. } else {
  416. if (qtrx->qt_icount_delta)
  417. dqp->q_res_icount +=
  418. (xfs_qcnt_t)qtrx->qt_icount_delta;
  419. }
  420. ASSERT(dqp->q_res_bcount >=
  421. be64_to_cpu(dqp->q_core.d_bcount));
  422. ASSERT(dqp->q_res_icount >=
  423. be64_to_cpu(dqp->q_core.d_icount));
  424. ASSERT(dqp->q_res_rtbcount >=
  425. be64_to_cpu(dqp->q_core.d_rtbcount));
  426. }
  427. }
  428. }
  429. /*
  430. * Release the reservations, and adjust the dquots accordingly.
  431. * This is called only when the transaction is being aborted. If by
  432. * any chance we have done dquot modifications incore (ie. deltas) already,
  433. * we simply throw those away, since that's the expected behavior
  434. * when a transaction is curtailed without a commit.
  435. */
  436. void
  437. xfs_trans_unreserve_and_mod_dquots(
  438. xfs_trans_t *tp)
  439. {
  440. int i, j;
  441. xfs_dquot_t *dqp;
  442. xfs_dqtrx_t *qtrx, *qa;
  443. bool locked;
  444. if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
  445. return;
  446. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  447. qa = tp->t_dqinfo->dqs[j];
  448. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  449. qtrx = &qa[i];
  450. /*
  451. * We assume that the array of dquots is filled
  452. * sequentially, not sparsely.
  453. */
  454. if ((dqp = qtrx->qt_dquot) == NULL)
  455. break;
  456. /*
  457. * Unreserve the original reservation. We don't care
  458. * about the number of blocks used field, or deltas.
  459. * Also we don't bother to zero the fields.
  460. */
  461. locked = false;
  462. if (qtrx->qt_blk_res) {
  463. xfs_dqlock(dqp);
  464. locked = true;
  465. dqp->q_res_bcount -=
  466. (xfs_qcnt_t)qtrx->qt_blk_res;
  467. }
  468. if (qtrx->qt_ino_res) {
  469. if (!locked) {
  470. xfs_dqlock(dqp);
  471. locked = true;
  472. }
  473. dqp->q_res_icount -=
  474. (xfs_qcnt_t)qtrx->qt_ino_res;
  475. }
  476. if (qtrx->qt_rtblk_res) {
  477. if (!locked) {
  478. xfs_dqlock(dqp);
  479. locked = true;
  480. }
  481. dqp->q_res_rtbcount -=
  482. (xfs_qcnt_t)qtrx->qt_rtblk_res;
  483. }
  484. if (locked)
  485. xfs_dqunlock(dqp);
  486. }
  487. }
  488. }
  489. STATIC void
  490. xfs_quota_warn(
  491. struct xfs_mount *mp,
  492. struct xfs_dquot *dqp,
  493. int type)
  494. {
  495. enum quota_type qtype;
  496. if (dqp->dq_flags & XFS_DQ_PROJ)
  497. qtype = PRJQUOTA;
  498. else if (dqp->dq_flags & XFS_DQ_USER)
  499. qtype = USRQUOTA;
  500. else
  501. qtype = GRPQUOTA;
  502. quota_send_warning(make_kqid(&init_user_ns, qtype,
  503. be32_to_cpu(dqp->q_core.d_id)),
  504. mp->m_super->s_dev, type);
  505. }
  506. /*
  507. * This reserves disk blocks and inodes against a dquot.
  508. * Flags indicate if the dquot is to be locked here and also
  509. * if the blk reservation is for RT or regular blocks.
  510. * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
  511. */
  512. STATIC int
  513. xfs_trans_dqresv(
  514. xfs_trans_t *tp,
  515. xfs_mount_t *mp,
  516. xfs_dquot_t *dqp,
  517. long nblks,
  518. long ninos,
  519. uint flags)
  520. {
  521. xfs_qcnt_t hardlimit;
  522. xfs_qcnt_t softlimit;
  523. time_t timer;
  524. xfs_qwarncnt_t warns;
  525. xfs_qwarncnt_t warnlimit;
  526. xfs_qcnt_t total_count;
  527. xfs_qcnt_t *resbcountp;
  528. xfs_quotainfo_t *q = mp->m_quotainfo;
  529. struct xfs_def_quota *defq;
  530. xfs_dqlock(dqp);
  531. defq = xfs_get_defquota(dqp, q);
  532. if (flags & XFS_TRANS_DQ_RES_BLKS) {
  533. hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
  534. if (!hardlimit)
  535. hardlimit = defq->bhardlimit;
  536. softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
  537. if (!softlimit)
  538. softlimit = defq->bsoftlimit;
  539. timer = be32_to_cpu(dqp->q_core.d_btimer);
  540. warns = be16_to_cpu(dqp->q_core.d_bwarns);
  541. warnlimit = dqp->q_mount->m_quotainfo->qi_bwarnlimit;
  542. resbcountp = &dqp->q_res_bcount;
  543. } else {
  544. ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
  545. hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
  546. if (!hardlimit)
  547. hardlimit = defq->rtbhardlimit;
  548. softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
  549. if (!softlimit)
  550. softlimit = defq->rtbsoftlimit;
  551. timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  552. warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  553. warnlimit = dqp->q_mount->m_quotainfo->qi_rtbwarnlimit;
  554. resbcountp = &dqp->q_res_rtbcount;
  555. }
  556. if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
  557. dqp->q_core.d_id &&
  558. ((XFS_IS_UQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISUDQ(dqp)) ||
  559. (XFS_IS_GQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISGDQ(dqp)) ||
  560. (XFS_IS_PQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISPDQ(dqp)))) {
  561. if (nblks > 0) {
  562. /*
  563. * dquot is locked already. See if we'd go over the
  564. * hardlimit or exceed the timelimit if we allocate
  565. * nblks.
  566. */
  567. total_count = *resbcountp + nblks;
  568. if (hardlimit && total_count > hardlimit) {
  569. xfs_quota_warn(mp, dqp, QUOTA_NL_BHARDWARN);
  570. goto error_return;
  571. }
  572. if (softlimit && total_count > softlimit) {
  573. if ((timer != 0 && get_seconds() > timer) ||
  574. (warns != 0 && warns >= warnlimit)) {
  575. xfs_quota_warn(mp, dqp,
  576. QUOTA_NL_BSOFTLONGWARN);
  577. goto error_return;
  578. }
  579. xfs_quota_warn(mp, dqp, QUOTA_NL_BSOFTWARN);
  580. }
  581. }
  582. if (ninos > 0) {
  583. total_count = dqp->q_res_icount + ninos;
  584. timer = be32_to_cpu(dqp->q_core.d_itimer);
  585. warns = be16_to_cpu(dqp->q_core.d_iwarns);
  586. warnlimit = dqp->q_mount->m_quotainfo->qi_iwarnlimit;
  587. hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  588. if (!hardlimit)
  589. hardlimit = defq->ihardlimit;
  590. softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  591. if (!softlimit)
  592. softlimit = defq->isoftlimit;
  593. if (hardlimit && total_count > hardlimit) {
  594. xfs_quota_warn(mp, dqp, QUOTA_NL_IHARDWARN);
  595. goto error_return;
  596. }
  597. if (softlimit && total_count > softlimit) {
  598. if ((timer != 0 && get_seconds() > timer) ||
  599. (warns != 0 && warns >= warnlimit)) {
  600. xfs_quota_warn(mp, dqp,
  601. QUOTA_NL_ISOFTLONGWARN);
  602. goto error_return;
  603. }
  604. xfs_quota_warn(mp, dqp, QUOTA_NL_ISOFTWARN);
  605. }
  606. }
  607. }
  608. /*
  609. * Change the reservation, but not the actual usage.
  610. * Note that q_res_bcount = q_core.d_bcount + resv
  611. */
  612. (*resbcountp) += (xfs_qcnt_t)nblks;
  613. if (ninos != 0)
  614. dqp->q_res_icount += (xfs_qcnt_t)ninos;
  615. /*
  616. * note the reservation amt in the trans struct too,
  617. * so that the transaction knows how much was reserved by
  618. * it against this particular dquot.
  619. * We don't do this when we are reserving for a delayed allocation,
  620. * because we don't have the luxury of a transaction envelope then.
  621. */
  622. if (tp) {
  623. ASSERT(tp->t_dqinfo);
  624. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  625. if (nblks != 0)
  626. xfs_trans_mod_dquot(tp, dqp,
  627. flags & XFS_QMOPT_RESBLK_MASK,
  628. nblks);
  629. if (ninos != 0)
  630. xfs_trans_mod_dquot(tp, dqp,
  631. XFS_TRANS_DQ_RES_INOS,
  632. ninos);
  633. }
  634. ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
  635. ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
  636. ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
  637. xfs_dqunlock(dqp);
  638. return 0;
  639. error_return:
  640. xfs_dqunlock(dqp);
  641. if (flags & XFS_QMOPT_ENOSPC)
  642. return -ENOSPC;
  643. return -EDQUOT;
  644. }
  645. /*
  646. * Given dquot(s), make disk block and/or inode reservations against them.
  647. * The fact that this does the reservation against user, group and
  648. * project quotas is important, because this follows a all-or-nothing
  649. * approach.
  650. *
  651. * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
  652. * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
  653. * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
  654. * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
  655. * dquots are unlocked on return, if they were not locked by caller.
  656. */
  657. int
  658. xfs_trans_reserve_quota_bydquots(
  659. struct xfs_trans *tp,
  660. struct xfs_mount *mp,
  661. struct xfs_dquot *udqp,
  662. struct xfs_dquot *gdqp,
  663. struct xfs_dquot *pdqp,
  664. long nblks,
  665. long ninos,
  666. uint flags)
  667. {
  668. int error;
  669. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  670. return 0;
  671. if (tp && tp->t_dqinfo == NULL)
  672. xfs_trans_alloc_dqinfo(tp);
  673. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  674. if (udqp) {
  675. error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
  676. (flags & ~XFS_QMOPT_ENOSPC));
  677. if (error)
  678. return error;
  679. }
  680. if (gdqp) {
  681. error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
  682. if (error)
  683. goto unwind_usr;
  684. }
  685. if (pdqp) {
  686. error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
  687. if (error)
  688. goto unwind_grp;
  689. }
  690. /*
  691. * Didn't change anything critical, so, no need to log
  692. */
  693. return 0;
  694. unwind_grp:
  695. flags |= XFS_QMOPT_FORCE_RES;
  696. if (gdqp)
  697. xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
  698. unwind_usr:
  699. flags |= XFS_QMOPT_FORCE_RES;
  700. if (udqp)
  701. xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
  702. return error;
  703. }
  704. /*
  705. * Lock the dquot and change the reservation if we can.
  706. * This doesn't change the actual usage, just the reservation.
  707. * The inode sent in is locked.
  708. */
  709. int
  710. xfs_trans_reserve_quota_nblks(
  711. struct xfs_trans *tp,
  712. struct xfs_inode *ip,
  713. long nblks,
  714. long ninos,
  715. uint flags)
  716. {
  717. struct xfs_mount *mp = ip->i_mount;
  718. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  719. return 0;
  720. if (XFS_IS_PQUOTA_ON(mp))
  721. flags |= XFS_QMOPT_ENOSPC;
  722. ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
  723. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  724. ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  725. XFS_TRANS_DQ_RES_RTBLKS ||
  726. (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  727. XFS_TRANS_DQ_RES_BLKS);
  728. /*
  729. * Reserve nblks against these dquots, with trans as the mediator.
  730. */
  731. return xfs_trans_reserve_quota_bydquots(tp, mp,
  732. ip->i_udquot, ip->i_gdquot,
  733. ip->i_pdquot,
  734. nblks, ninos, flags);
  735. }
  736. /*
  737. * This routine is called to allocate a quotaoff log item.
  738. */
  739. xfs_qoff_logitem_t *
  740. xfs_trans_get_qoff_item(
  741. xfs_trans_t *tp,
  742. xfs_qoff_logitem_t *startqoff,
  743. uint flags)
  744. {
  745. xfs_qoff_logitem_t *q;
  746. ASSERT(tp != NULL);
  747. q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
  748. ASSERT(q != NULL);
  749. /*
  750. * Get a log_item_desc to point at the new item.
  751. */
  752. xfs_trans_add_item(tp, &q->qql_item);
  753. return q;
  754. }
  755. /*
  756. * This is called to mark the quotaoff logitem as needing
  757. * to be logged when the transaction is committed. The logitem must
  758. * already be associated with the given transaction.
  759. */
  760. void
  761. xfs_trans_log_quotaoff_item(
  762. xfs_trans_t *tp,
  763. xfs_qoff_logitem_t *qlp)
  764. {
  765. tp->t_flags |= XFS_TRANS_DIRTY;
  766. set_bit(XFS_LI_DIRTY, &qlp->qql_item.li_flags);
  767. }
  768. STATIC void
  769. xfs_trans_alloc_dqinfo(
  770. xfs_trans_t *tp)
  771. {
  772. tp->t_dqinfo = kmem_zone_zalloc(xfs_qm_dqtrxzone, KM_SLEEP);
  773. }
  774. void
  775. xfs_trans_free_dqinfo(
  776. xfs_trans_t *tp)
  777. {
  778. if (!tp->t_dqinfo)
  779. return;
  780. kmem_zone_free(xfs_qm_dqtrxzone, tp->t_dqinfo);
  781. tp->t_dqinfo = NULL;
  782. }