xfs_trans_dquot.c 22 KB

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