xfs_trans_dquot.c 22 KB

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