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_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((dqp->dq_flags & XFS_DQ_USER) ? USRQUOTA : GRPQUOTA,
  518. be32_to_cpu(dqp->q_core.d_id), mp->m_super->s_dev,
  519. type);
  520. }
  521. /*
  522. * This reserves disk blocks and inodes against a dquot.
  523. * Flags indicate if the dquot is to be locked here and also
  524. * if the blk reservation is for RT or regular blocks.
  525. * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
  526. */
  527. STATIC int
  528. xfs_trans_dqresv(
  529. xfs_trans_t *tp,
  530. xfs_mount_t *mp,
  531. xfs_dquot_t *dqp,
  532. long nblks,
  533. long ninos,
  534. uint flags)
  535. {
  536. xfs_qcnt_t hardlimit;
  537. xfs_qcnt_t softlimit;
  538. time_t timer;
  539. xfs_qwarncnt_t warns;
  540. xfs_qwarncnt_t warnlimit;
  541. xfs_qcnt_t total_count;
  542. xfs_qcnt_t *resbcountp;
  543. xfs_quotainfo_t *q = mp->m_quotainfo;
  544. xfs_dqlock(dqp);
  545. if (flags & XFS_TRANS_DQ_RES_BLKS) {
  546. hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
  547. if (!hardlimit)
  548. hardlimit = q->qi_bhardlimit;
  549. softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
  550. if (!softlimit)
  551. softlimit = q->qi_bsoftlimit;
  552. timer = be32_to_cpu(dqp->q_core.d_btimer);
  553. warns = be16_to_cpu(dqp->q_core.d_bwarns);
  554. warnlimit = dqp->q_mount->m_quotainfo->qi_bwarnlimit;
  555. resbcountp = &dqp->q_res_bcount;
  556. } else {
  557. ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
  558. hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
  559. if (!hardlimit)
  560. hardlimit = q->qi_rtbhardlimit;
  561. softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
  562. if (!softlimit)
  563. softlimit = q->qi_rtbsoftlimit;
  564. timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  565. warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  566. warnlimit = dqp->q_mount->m_quotainfo->qi_rtbwarnlimit;
  567. resbcountp = &dqp->q_res_rtbcount;
  568. }
  569. if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
  570. dqp->q_core.d_id &&
  571. ((XFS_IS_UQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISUDQ(dqp)) ||
  572. (XFS_IS_OQUOTA_ENFORCED(dqp->q_mount) &&
  573. (XFS_QM_ISPDQ(dqp) || XFS_QM_ISGDQ(dqp))))) {
  574. if (nblks > 0) {
  575. /*
  576. * dquot is locked already. See if we'd go over the
  577. * hardlimit or exceed the timelimit if we allocate
  578. * nblks.
  579. */
  580. total_count = *resbcountp + nblks;
  581. if (hardlimit && total_count > hardlimit) {
  582. xfs_quota_warn(mp, dqp, QUOTA_NL_BHARDWARN);
  583. goto error_return;
  584. }
  585. if (softlimit && total_count > softlimit) {
  586. if ((timer != 0 && get_seconds() > timer) ||
  587. (warns != 0 && warns >= warnlimit)) {
  588. xfs_quota_warn(mp, dqp,
  589. QUOTA_NL_BSOFTLONGWARN);
  590. goto error_return;
  591. }
  592. xfs_quota_warn(mp, dqp, QUOTA_NL_BSOFTWARN);
  593. }
  594. }
  595. if (ninos > 0) {
  596. total_count = be64_to_cpu(dqp->q_core.d_icount) + ninos;
  597. timer = be32_to_cpu(dqp->q_core.d_itimer);
  598. warns = be16_to_cpu(dqp->q_core.d_iwarns);
  599. warnlimit = dqp->q_mount->m_quotainfo->qi_iwarnlimit;
  600. hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  601. if (!hardlimit)
  602. hardlimit = q->qi_ihardlimit;
  603. softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  604. if (!softlimit)
  605. softlimit = q->qi_isoftlimit;
  606. if (hardlimit && total_count > hardlimit) {
  607. xfs_quota_warn(mp, dqp, QUOTA_NL_IHARDWARN);
  608. goto error_return;
  609. }
  610. if (softlimit && total_count > softlimit) {
  611. if ((timer != 0 && get_seconds() > timer) ||
  612. (warns != 0 && warns >= warnlimit)) {
  613. xfs_quota_warn(mp, dqp,
  614. QUOTA_NL_ISOFTLONGWARN);
  615. goto error_return;
  616. }
  617. xfs_quota_warn(mp, dqp, QUOTA_NL_ISOFTWARN);
  618. }
  619. }
  620. }
  621. /*
  622. * Change the reservation, but not the actual usage.
  623. * Note that q_res_bcount = q_core.d_bcount + resv
  624. */
  625. (*resbcountp) += (xfs_qcnt_t)nblks;
  626. if (ninos != 0)
  627. dqp->q_res_icount += (xfs_qcnt_t)ninos;
  628. /*
  629. * note the reservation amt in the trans struct too,
  630. * so that the transaction knows how much was reserved by
  631. * it against this particular dquot.
  632. * We don't do this when we are reserving for a delayed allocation,
  633. * because we don't have the luxury of a transaction envelope then.
  634. */
  635. if (tp) {
  636. ASSERT(tp->t_dqinfo);
  637. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  638. if (nblks != 0)
  639. xfs_trans_mod_dquot(tp, dqp,
  640. flags & XFS_QMOPT_RESBLK_MASK,
  641. nblks);
  642. if (ninos != 0)
  643. xfs_trans_mod_dquot(tp, dqp,
  644. XFS_TRANS_DQ_RES_INOS,
  645. ninos);
  646. }
  647. ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
  648. ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
  649. ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
  650. xfs_dqunlock(dqp);
  651. return 0;
  652. error_return:
  653. xfs_dqunlock(dqp);
  654. if (flags & XFS_QMOPT_ENOSPC)
  655. return ENOSPC;
  656. return EDQUOT;
  657. }
  658. /*
  659. * Given dquot(s), make disk block and/or inode reservations against them.
  660. * The fact that this does the reservation against both the usr and
  661. * grp/prj quotas is important, because this follows a both-or-nothing
  662. * approach.
  663. *
  664. * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
  665. * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
  666. * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
  667. * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
  668. * dquots are unlocked on return, if they were not locked by caller.
  669. */
  670. int
  671. xfs_trans_reserve_quota_bydquots(
  672. xfs_trans_t *tp,
  673. xfs_mount_t *mp,
  674. xfs_dquot_t *udqp,
  675. xfs_dquot_t *gdqp,
  676. long nblks,
  677. long ninos,
  678. uint flags)
  679. {
  680. int resvd = 0, 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. resvd = 1;
  692. }
  693. if (gdqp) {
  694. error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
  695. if (error) {
  696. /*
  697. * can't do it, so backout previous reservation
  698. */
  699. if (resvd) {
  700. flags |= XFS_QMOPT_FORCE_RES;
  701. xfs_trans_dqresv(tp, mp, udqp,
  702. -nblks, -ninos, flags);
  703. }
  704. return error;
  705. }
  706. }
  707. /*
  708. * Didn't change anything critical, so, no need to log
  709. */
  710. return 0;
  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(ip->i_ino != mp->m_sb.sb_uquotino);
  731. ASSERT(ip->i_ino != mp->m_sb.sb_gquotino);
  732. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  733. ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  734. XFS_TRANS_DQ_RES_RTBLKS ||
  735. (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  736. XFS_TRANS_DQ_RES_BLKS);
  737. /*
  738. * Reserve nblks against these dquots, with trans as the mediator.
  739. */
  740. return xfs_trans_reserve_quota_bydquots(tp, mp,
  741. ip->i_udquot, ip->i_gdquot,
  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. }