xfs_trans_dquot.c 22 KB

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