xfs_trans_dquot.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. ulong blk_res_used;
  88. if (!otp->t_dqinfo)
  89. return;
  90. xfs_trans_alloc_dqinfo(ntp);
  91. /*
  92. * Because the quota blk reservation is carried forward,
  93. * it is also necessary to carry forward the DQ_DIRTY flag.
  94. */
  95. if (otp->t_flags & XFS_TRANS_DQ_DIRTY)
  96. ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
  97. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  98. oqa = otp->t_dqinfo->dqs[j];
  99. nqa = ntp->t_dqinfo->dqs[j];
  100. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  101. blk_res_used = 0;
  102. if (oqa[i].qt_dquot == NULL)
  103. break;
  104. oq = &oqa[i];
  105. nq = &nqa[i];
  106. if (oq->qt_blk_res && oq->qt_bcount_delta > 0)
  107. blk_res_used = oq->qt_bcount_delta;
  108. nq->qt_dquot = oq->qt_dquot;
  109. nq->qt_bcount_delta = nq->qt_icount_delta = 0;
  110. nq->qt_rtbcount_delta = 0;
  111. /*
  112. * Transfer whatever is left of the reservations.
  113. */
  114. nq->qt_blk_res = oq->qt_blk_res - blk_res_used;
  115. oq->qt_blk_res = blk_res_used;
  116. nq->qt_rtblk_res = oq->qt_rtblk_res -
  117. oq->qt_rtblk_res_used;
  118. oq->qt_rtblk_res = oq->qt_rtblk_res_used;
  119. nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
  120. oq->qt_ino_res = oq->qt_ino_res_used;
  121. }
  122. }
  123. }
  124. /*
  125. * Wrap around mod_dquot to account for both user and group quotas.
  126. */
  127. void
  128. xfs_trans_mod_dquot_byino(
  129. xfs_trans_t *tp,
  130. xfs_inode_t *ip,
  131. uint field,
  132. long delta)
  133. {
  134. xfs_mount_t *mp = tp->t_mountp;
  135. if (!XFS_IS_QUOTA_RUNNING(mp) ||
  136. !XFS_IS_QUOTA_ON(mp) ||
  137. xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
  138. return;
  139. if (tp->t_dqinfo == NULL)
  140. xfs_trans_alloc_dqinfo(tp);
  141. if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
  142. (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
  143. if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
  144. (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
  145. if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
  146. (void) xfs_trans_mod_dquot(tp, ip->i_pdquot, 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 if (XFS_QM_ISGDQ(dqp))
  158. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
  159. else if (XFS_QM_ISPDQ(dqp))
  160. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
  161. else
  162. return NULL;
  163. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  164. if (qa[i].qt_dquot == NULL ||
  165. qa[i].qt_dquot == dqp)
  166. return &qa[i];
  167. }
  168. return NULL;
  169. }
  170. /*
  171. * Make the changes in the transaction structure.
  172. * The moral equivalent to xfs_trans_mod_sb().
  173. * We don't touch any fields in the dquot, so we don't care
  174. * if it's locked or not (most of the time it won't be).
  175. */
  176. void
  177. xfs_trans_mod_dquot(
  178. xfs_trans_t *tp,
  179. xfs_dquot_t *dqp,
  180. uint field,
  181. long delta)
  182. {
  183. xfs_dqtrx_t *qtrx;
  184. ASSERT(tp);
  185. ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
  186. qtrx = NULL;
  187. if (tp->t_dqinfo == NULL)
  188. xfs_trans_alloc_dqinfo(tp);
  189. /*
  190. * Find either the first free slot or the slot that belongs
  191. * to this dquot.
  192. */
  193. qtrx = xfs_trans_get_dqtrx(tp, dqp);
  194. ASSERT(qtrx);
  195. if (qtrx->qt_dquot == NULL)
  196. qtrx->qt_dquot = dqp;
  197. switch (field) {
  198. /*
  199. * regular disk blk reservation
  200. */
  201. case XFS_TRANS_DQ_RES_BLKS:
  202. qtrx->qt_blk_res += (ulong)delta;
  203. break;
  204. /*
  205. * inode reservation
  206. */
  207. case XFS_TRANS_DQ_RES_INOS:
  208. qtrx->qt_ino_res += (ulong)delta;
  209. break;
  210. /*
  211. * disk blocks used.
  212. */
  213. case XFS_TRANS_DQ_BCOUNT:
  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. ulong blk_res_used = 0;
  372. if (qtrx->qt_bcount_delta > 0)
  373. blk_res_used = qtrx->qt_bcount_delta;
  374. if (qtrx->qt_blk_res != blk_res_used) {
  375. if (qtrx->qt_blk_res > blk_res_used)
  376. dqp->q_res_bcount -= (xfs_qcnt_t)
  377. (qtrx->qt_blk_res -
  378. blk_res_used);
  379. else
  380. dqp->q_res_bcount -= (xfs_qcnt_t)
  381. (blk_res_used -
  382. qtrx->qt_blk_res);
  383. }
  384. } else {
  385. /*
  386. * These blks were never reserved, either inside
  387. * a transaction or outside one (in a delayed
  388. * allocation). Also, this isn't always a
  389. * negative number since we sometimes
  390. * deliberately skip quota reservations.
  391. */
  392. if (qtrx->qt_bcount_delta) {
  393. dqp->q_res_bcount +=
  394. (xfs_qcnt_t)qtrx->qt_bcount_delta;
  395. }
  396. }
  397. /*
  398. * Adjust the RT reservation.
  399. */
  400. if (qtrx->qt_rtblk_res != 0) {
  401. if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
  402. if (qtrx->qt_rtblk_res >
  403. qtrx->qt_rtblk_res_used)
  404. dqp->q_res_rtbcount -= (xfs_qcnt_t)
  405. (qtrx->qt_rtblk_res -
  406. qtrx->qt_rtblk_res_used);
  407. else
  408. dqp->q_res_rtbcount -= (xfs_qcnt_t)
  409. (qtrx->qt_rtblk_res_used -
  410. qtrx->qt_rtblk_res);
  411. }
  412. } else {
  413. if (qtrx->qt_rtbcount_delta)
  414. dqp->q_res_rtbcount +=
  415. (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
  416. }
  417. /*
  418. * Adjust the inode reservation.
  419. */
  420. if (qtrx->qt_ino_res != 0) {
  421. ASSERT(qtrx->qt_ino_res >=
  422. qtrx->qt_ino_res_used);
  423. if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
  424. dqp->q_res_icount -= (xfs_qcnt_t)
  425. (qtrx->qt_ino_res -
  426. qtrx->qt_ino_res_used);
  427. } else {
  428. if (qtrx->qt_icount_delta)
  429. dqp->q_res_icount +=
  430. (xfs_qcnt_t)qtrx->qt_icount_delta;
  431. }
  432. ASSERT(dqp->q_res_bcount >=
  433. be64_to_cpu(dqp->q_core.d_bcount));
  434. ASSERT(dqp->q_res_icount >=
  435. be64_to_cpu(dqp->q_core.d_icount));
  436. ASSERT(dqp->q_res_rtbcount >=
  437. be64_to_cpu(dqp->q_core.d_rtbcount));
  438. }
  439. }
  440. }
  441. /*
  442. * Release the reservations, and adjust the dquots accordingly.
  443. * This is called only when the transaction is being aborted. If by
  444. * any chance we have done dquot modifications incore (ie. deltas) already,
  445. * we simply throw those away, since that's the expected behavior
  446. * when a transaction is curtailed without a commit.
  447. */
  448. void
  449. xfs_trans_unreserve_and_mod_dquots(
  450. xfs_trans_t *tp)
  451. {
  452. int i, j;
  453. xfs_dquot_t *dqp;
  454. xfs_dqtrx_t *qtrx, *qa;
  455. bool locked;
  456. if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
  457. return;
  458. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  459. qa = tp->t_dqinfo->dqs[j];
  460. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  461. qtrx = &qa[i];
  462. /*
  463. * We assume that the array of dquots is filled
  464. * sequentially, not sparsely.
  465. */
  466. if ((dqp = qtrx->qt_dquot) == NULL)
  467. break;
  468. /*
  469. * Unreserve the original reservation. We don't care
  470. * about the number of blocks used field, or deltas.
  471. * Also we don't bother to zero the fields.
  472. */
  473. locked = false;
  474. if (qtrx->qt_blk_res) {
  475. xfs_dqlock(dqp);
  476. locked = true;
  477. dqp->q_res_bcount -=
  478. (xfs_qcnt_t)qtrx->qt_blk_res;
  479. }
  480. if (qtrx->qt_ino_res) {
  481. if (!locked) {
  482. xfs_dqlock(dqp);
  483. locked = true;
  484. }
  485. dqp->q_res_icount -=
  486. (xfs_qcnt_t)qtrx->qt_ino_res;
  487. }
  488. if (qtrx->qt_rtblk_res) {
  489. if (!locked) {
  490. xfs_dqlock(dqp);
  491. locked = true;
  492. }
  493. dqp->q_res_rtbcount -=
  494. (xfs_qcnt_t)qtrx->qt_rtblk_res;
  495. }
  496. if (locked)
  497. xfs_dqunlock(dqp);
  498. }
  499. }
  500. }
  501. STATIC void
  502. xfs_quota_warn(
  503. struct xfs_mount *mp,
  504. struct xfs_dquot *dqp,
  505. int type)
  506. {
  507. /* no warnings for project quotas - we just return ENOSPC later */
  508. if (dqp->dq_flags & XFS_DQ_PROJ)
  509. return;
  510. quota_send_warning(make_kqid(&init_user_ns,
  511. (dqp->dq_flags & XFS_DQ_USER) ?
  512. USRQUOTA : GRPQUOTA,
  513. be32_to_cpu(dqp->q_core.d_id)),
  514. mp->m_super->s_dev, type);
  515. }
  516. /*
  517. * This reserves disk blocks and inodes against a dquot.
  518. * Flags indicate if the dquot is to be locked here and also
  519. * if the blk reservation is for RT or regular blocks.
  520. * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
  521. */
  522. STATIC int
  523. xfs_trans_dqresv(
  524. xfs_trans_t *tp,
  525. xfs_mount_t *mp,
  526. xfs_dquot_t *dqp,
  527. long nblks,
  528. long ninos,
  529. uint flags)
  530. {
  531. xfs_qcnt_t hardlimit;
  532. xfs_qcnt_t softlimit;
  533. time_t timer;
  534. xfs_qwarncnt_t warns;
  535. xfs_qwarncnt_t warnlimit;
  536. xfs_qcnt_t total_count;
  537. xfs_qcnt_t *resbcountp;
  538. xfs_quotainfo_t *q = mp->m_quotainfo;
  539. xfs_dqlock(dqp);
  540. if (flags & XFS_TRANS_DQ_RES_BLKS) {
  541. hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
  542. if (!hardlimit)
  543. hardlimit = q->qi_bhardlimit;
  544. softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
  545. if (!softlimit)
  546. softlimit = q->qi_bsoftlimit;
  547. timer = be32_to_cpu(dqp->q_core.d_btimer);
  548. warns = be16_to_cpu(dqp->q_core.d_bwarns);
  549. warnlimit = dqp->q_mount->m_quotainfo->qi_bwarnlimit;
  550. resbcountp = &dqp->q_res_bcount;
  551. } else {
  552. ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
  553. hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
  554. if (!hardlimit)
  555. hardlimit = q->qi_rtbhardlimit;
  556. softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
  557. if (!softlimit)
  558. softlimit = q->qi_rtbsoftlimit;
  559. timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  560. warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  561. warnlimit = dqp->q_mount->m_quotainfo->qi_rtbwarnlimit;
  562. resbcountp = &dqp->q_res_rtbcount;
  563. }
  564. if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
  565. dqp->q_core.d_id &&
  566. ((XFS_IS_UQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISUDQ(dqp)) ||
  567. (XFS_IS_GQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISGDQ(dqp)) ||
  568. (XFS_IS_PQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISPDQ(dqp)))) {
  569. if (nblks > 0) {
  570. /*
  571. * dquot is locked already. See if we'd go over the
  572. * hardlimit or exceed the timelimit if we allocate
  573. * nblks.
  574. */
  575. total_count = *resbcountp + nblks;
  576. if (hardlimit && total_count > hardlimit) {
  577. xfs_quota_warn(mp, dqp, QUOTA_NL_BHARDWARN);
  578. goto error_return;
  579. }
  580. if (softlimit && total_count > softlimit) {
  581. if ((timer != 0 && get_seconds() > timer) ||
  582. (warns != 0 && warns >= warnlimit)) {
  583. xfs_quota_warn(mp, dqp,
  584. QUOTA_NL_BSOFTLONGWARN);
  585. goto error_return;
  586. }
  587. xfs_quota_warn(mp, dqp, QUOTA_NL_BSOFTWARN);
  588. }
  589. }
  590. if (ninos > 0) {
  591. total_count = be64_to_cpu(dqp->q_core.d_icount) + ninos;
  592. timer = be32_to_cpu(dqp->q_core.d_itimer);
  593. warns = be16_to_cpu(dqp->q_core.d_iwarns);
  594. warnlimit = dqp->q_mount->m_quotainfo->qi_iwarnlimit;
  595. hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  596. if (!hardlimit)
  597. hardlimit = q->qi_ihardlimit;
  598. softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  599. if (!softlimit)
  600. softlimit = q->qi_isoftlimit;
  601. if (hardlimit && total_count > hardlimit) {
  602. xfs_quota_warn(mp, dqp, QUOTA_NL_IHARDWARN);
  603. goto error_return;
  604. }
  605. if (softlimit && total_count > softlimit) {
  606. if ((timer != 0 && get_seconds() > timer) ||
  607. (warns != 0 && warns >= warnlimit)) {
  608. xfs_quota_warn(mp, dqp,
  609. QUOTA_NL_ISOFTLONGWARN);
  610. goto error_return;
  611. }
  612. xfs_quota_warn(mp, dqp, QUOTA_NL_ISOFTWARN);
  613. }
  614. }
  615. }
  616. /*
  617. * Change the reservation, but not the actual usage.
  618. * Note that q_res_bcount = q_core.d_bcount + resv
  619. */
  620. (*resbcountp) += (xfs_qcnt_t)nblks;
  621. if (ninos != 0)
  622. dqp->q_res_icount += (xfs_qcnt_t)ninos;
  623. /*
  624. * note the reservation amt in the trans struct too,
  625. * so that the transaction knows how much was reserved by
  626. * it against this particular dquot.
  627. * We don't do this when we are reserving for a delayed allocation,
  628. * because we don't have the luxury of a transaction envelope then.
  629. */
  630. if (tp) {
  631. ASSERT(tp->t_dqinfo);
  632. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  633. if (nblks != 0)
  634. xfs_trans_mod_dquot(tp, dqp,
  635. flags & XFS_QMOPT_RESBLK_MASK,
  636. nblks);
  637. if (ninos != 0)
  638. xfs_trans_mod_dquot(tp, dqp,
  639. XFS_TRANS_DQ_RES_INOS,
  640. ninos);
  641. }
  642. ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
  643. ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
  644. ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
  645. xfs_dqunlock(dqp);
  646. return 0;
  647. error_return:
  648. xfs_dqunlock(dqp);
  649. if (flags & XFS_QMOPT_ENOSPC)
  650. return -ENOSPC;
  651. return -EDQUOT;
  652. }
  653. /*
  654. * Given dquot(s), make disk block and/or inode reservations against them.
  655. * The fact that this does the reservation against user, group and
  656. * project quotas is important, because this follows a all-or-nothing
  657. * approach.
  658. *
  659. * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
  660. * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
  661. * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
  662. * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
  663. * dquots are unlocked on return, if they were not locked by caller.
  664. */
  665. int
  666. xfs_trans_reserve_quota_bydquots(
  667. struct xfs_trans *tp,
  668. struct xfs_mount *mp,
  669. struct xfs_dquot *udqp,
  670. struct xfs_dquot *gdqp,
  671. struct xfs_dquot *pdqp,
  672. long nblks,
  673. long ninos,
  674. uint flags)
  675. {
  676. int error;
  677. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  678. return 0;
  679. if (tp && tp->t_dqinfo == NULL)
  680. xfs_trans_alloc_dqinfo(tp);
  681. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  682. if (udqp) {
  683. error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
  684. (flags & ~XFS_QMOPT_ENOSPC));
  685. if (error)
  686. return error;
  687. }
  688. if (gdqp) {
  689. error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
  690. if (error)
  691. goto unwind_usr;
  692. }
  693. if (pdqp) {
  694. error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
  695. if (error)
  696. goto unwind_grp;
  697. }
  698. /*
  699. * Didn't change anything critical, so, no need to log
  700. */
  701. return 0;
  702. unwind_grp:
  703. flags |= XFS_QMOPT_FORCE_RES;
  704. if (gdqp)
  705. xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
  706. unwind_usr:
  707. flags |= XFS_QMOPT_FORCE_RES;
  708. if (udqp)
  709. xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
  710. return error;
  711. }
  712. /*
  713. * Lock the dquot and change the reservation if we can.
  714. * This doesn't change the actual usage, just the reservation.
  715. * The inode sent in is locked.
  716. */
  717. int
  718. xfs_trans_reserve_quota_nblks(
  719. struct xfs_trans *tp,
  720. struct xfs_inode *ip,
  721. long nblks,
  722. long ninos,
  723. uint flags)
  724. {
  725. struct xfs_mount *mp = ip->i_mount;
  726. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  727. return 0;
  728. if (XFS_IS_PQUOTA_ON(mp))
  729. flags |= XFS_QMOPT_ENOSPC;
  730. ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
  731. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  732. ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  733. XFS_TRANS_DQ_RES_RTBLKS ||
  734. (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  735. XFS_TRANS_DQ_RES_BLKS);
  736. /*
  737. * Reserve nblks against these dquots, with trans as the mediator.
  738. */
  739. return xfs_trans_reserve_quota_bydquots(tp, mp,
  740. ip->i_udquot, ip->i_gdquot,
  741. ip->i_pdquot,
  742. nblks, ninos, flags);
  743. }
  744. /*
  745. * This routine is called to allocate a quotaoff log item.
  746. */
  747. xfs_qoff_logitem_t *
  748. xfs_trans_get_qoff_item(
  749. xfs_trans_t *tp,
  750. xfs_qoff_logitem_t *startqoff,
  751. uint flags)
  752. {
  753. xfs_qoff_logitem_t *q;
  754. ASSERT(tp != NULL);
  755. q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
  756. ASSERT(q != NULL);
  757. /*
  758. * Get a log_item_desc to point at the new item.
  759. */
  760. xfs_trans_add_item(tp, &q->qql_item);
  761. return q;
  762. }
  763. /*
  764. * This is called to mark the quotaoff logitem as needing
  765. * to be logged when the transaction is committed. The logitem must
  766. * already be associated with the given transaction.
  767. */
  768. void
  769. xfs_trans_log_quotaoff_item(
  770. xfs_trans_t *tp,
  771. xfs_qoff_logitem_t *qlp)
  772. {
  773. tp->t_flags |= XFS_TRANS_DIRTY;
  774. qlp->qql_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  775. }
  776. STATIC void
  777. xfs_trans_alloc_dqinfo(
  778. xfs_trans_t *tp)
  779. {
  780. tp->t_dqinfo = kmem_zone_zalloc(xfs_qm_dqtrxzone, KM_SLEEP);
  781. }
  782. void
  783. xfs_trans_free_dqinfo(
  784. xfs_trans_t *tp)
  785. {
  786. if (!tp->t_dqinfo)
  787. return;
  788. kmem_zone_free(xfs_qm_dqtrxzone, tp->t_dqinfo);
  789. tp->t_dqinfo = NULL;
  790. }