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