xfs_trans_dquot.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_quota.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_itable.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_rtalloc.h"
  34. #include "xfs_error.h"
  35. #include "xfs_attr.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_trans_priv.h"
  38. #include "xfs_qm.h"
  39. STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *);
  40. /*
  41. * Add the locked dquot to the transaction.
  42. * The dquot must be locked, and it cannot be associated with any
  43. * transaction.
  44. */
  45. void
  46. xfs_trans_dqjoin(
  47. xfs_trans_t *tp,
  48. xfs_dquot_t *dqp)
  49. {
  50. ASSERT(dqp->q_transp != tp);
  51. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  52. ASSERT(dqp->q_logitem.qli_dquot == dqp);
  53. /*
  54. * Get a log_item_desc to point at the new item.
  55. */
  56. xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
  57. /*
  58. * Initialize i_transp so we can later determine if this dquot is
  59. * associated with this transaction.
  60. */
  61. dqp->q_transp = tp;
  62. }
  63. /*
  64. * This is called to mark the dquot as needing
  65. * to be logged when the transaction is committed. The dquot must
  66. * already be associated with the given transaction.
  67. * Note that it marks the entire transaction as dirty. In the ordinary
  68. * case, this gets called via xfs_trans_commit, after the transaction
  69. * is already dirty. However, there's nothing stop this from getting
  70. * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
  71. * flag.
  72. */
  73. void
  74. xfs_trans_log_dquot(
  75. xfs_trans_t *tp,
  76. xfs_dquot_t *dqp)
  77. {
  78. ASSERT(dqp->q_transp == tp);
  79. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  80. tp->t_flags |= XFS_TRANS_DIRTY;
  81. dqp->q_logitem.qli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  82. }
  83. /*
  84. * Carry forward whatever is left of the quota blk reservation to
  85. * the spanky new transaction
  86. */
  87. void
  88. xfs_trans_dup_dqinfo(
  89. xfs_trans_t *otp,
  90. xfs_trans_t *ntp)
  91. {
  92. xfs_dqtrx_t *oq, *nq;
  93. int i,j;
  94. xfs_dqtrx_t *oqa, *nqa;
  95. if (!otp->t_dqinfo)
  96. return;
  97. xfs_trans_alloc_dqinfo(ntp);
  98. oqa = otp->t_dqinfo->dqa_usrdquots;
  99. nqa = ntp->t_dqinfo->dqa_usrdquots;
  100. /*
  101. * Because the quota blk reservation is carried forward,
  102. * it is also necessary to carry forward the DQ_DIRTY flag.
  103. */
  104. if(otp->t_flags & XFS_TRANS_DQ_DIRTY)
  105. ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
  106. for (j = 0; j < 2; j++) {
  107. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  108. if (oqa[i].qt_dquot == NULL)
  109. break;
  110. oq = &oqa[i];
  111. nq = &nqa[i];
  112. nq->qt_dquot = oq->qt_dquot;
  113. nq->qt_bcount_delta = nq->qt_icount_delta = 0;
  114. nq->qt_rtbcount_delta = 0;
  115. /*
  116. * Transfer whatever is left of the reservations.
  117. */
  118. nq->qt_blk_res = oq->qt_blk_res - oq->qt_blk_res_used;
  119. oq->qt_blk_res = oq->qt_blk_res_used;
  120. nq->qt_rtblk_res = oq->qt_rtblk_res -
  121. oq->qt_rtblk_res_used;
  122. oq->qt_rtblk_res = oq->qt_rtblk_res_used;
  123. nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
  124. oq->qt_ino_res = oq->qt_ino_res_used;
  125. }
  126. oqa = otp->t_dqinfo->dqa_grpdquots;
  127. nqa = ntp->t_dqinfo->dqa_grpdquots;
  128. }
  129. }
  130. /*
  131. * Wrap around mod_dquot to account for both user and group quotas.
  132. */
  133. void
  134. xfs_trans_mod_dquot_byino(
  135. xfs_trans_t *tp,
  136. xfs_inode_t *ip,
  137. uint field,
  138. long delta)
  139. {
  140. xfs_mount_t *mp = tp->t_mountp;
  141. if (!XFS_IS_QUOTA_RUNNING(mp) ||
  142. !XFS_IS_QUOTA_ON(mp) ||
  143. ip->i_ino == mp->m_sb.sb_uquotino ||
  144. ip->i_ino == mp->m_sb.sb_gquotino)
  145. return;
  146. if (tp->t_dqinfo == NULL)
  147. xfs_trans_alloc_dqinfo(tp);
  148. if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
  149. (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
  150. if (XFS_IS_OQUOTA_ON(mp) && ip->i_gdquot)
  151. (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
  152. }
  153. STATIC xfs_dqtrx_t *
  154. xfs_trans_get_dqtrx(
  155. xfs_trans_t *tp,
  156. xfs_dquot_t *dqp)
  157. {
  158. int i;
  159. xfs_dqtrx_t *qa;
  160. qa = XFS_QM_ISUDQ(dqp) ?
  161. tp->t_dqinfo->dqa_usrdquots : tp->t_dqinfo->dqa_grpdquots;
  162. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  163. if (qa[i].qt_dquot == NULL ||
  164. qa[i].qt_dquot == dqp)
  165. return &qa[i];
  166. }
  167. return NULL;
  168. }
  169. /*
  170. * Make the changes in the transaction structure.
  171. * The moral equivalent to xfs_trans_mod_sb().
  172. * We don't touch any fields in the dquot, so we don't care
  173. * if it's locked or not (most of the time it won't be).
  174. */
  175. void
  176. xfs_trans_mod_dquot(
  177. xfs_trans_t *tp,
  178. xfs_dquot_t *dqp,
  179. uint field,
  180. long delta)
  181. {
  182. xfs_dqtrx_t *qtrx;
  183. ASSERT(tp);
  184. ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
  185. qtrx = NULL;
  186. if (tp->t_dqinfo == NULL)
  187. xfs_trans_alloc_dqinfo(tp);
  188. /*
  189. * Find either the first free slot or the slot that belongs
  190. * to this dquot.
  191. */
  192. qtrx = xfs_trans_get_dqtrx(tp, dqp);
  193. ASSERT(qtrx);
  194. if (qtrx->qt_dquot == NULL)
  195. qtrx->qt_dquot = dqp;
  196. switch (field) {
  197. /*
  198. * regular disk blk reservation
  199. */
  200. case XFS_TRANS_DQ_RES_BLKS:
  201. qtrx->qt_blk_res += (ulong)delta;
  202. break;
  203. /*
  204. * inode reservation
  205. */
  206. case XFS_TRANS_DQ_RES_INOS:
  207. qtrx->qt_ino_res += (ulong)delta;
  208. break;
  209. /*
  210. * disk blocks used.
  211. */
  212. case XFS_TRANS_DQ_BCOUNT:
  213. if (qtrx->qt_blk_res && delta > 0) {
  214. qtrx->qt_blk_res_used += (ulong)delta;
  215. ASSERT(qtrx->qt_blk_res >= qtrx->qt_blk_res_used);
  216. }
  217. qtrx->qt_bcount_delta += delta;
  218. break;
  219. case XFS_TRANS_DQ_DELBCOUNT:
  220. qtrx->qt_delbcnt_delta += delta;
  221. break;
  222. /*
  223. * Inode Count
  224. */
  225. case XFS_TRANS_DQ_ICOUNT:
  226. if (qtrx->qt_ino_res && delta > 0) {
  227. qtrx->qt_ino_res_used += (ulong)delta;
  228. ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
  229. }
  230. qtrx->qt_icount_delta += delta;
  231. break;
  232. /*
  233. * rtblk reservation
  234. */
  235. case XFS_TRANS_DQ_RES_RTBLKS:
  236. qtrx->qt_rtblk_res += (ulong)delta;
  237. break;
  238. /*
  239. * rtblk count
  240. */
  241. case XFS_TRANS_DQ_RTBCOUNT:
  242. if (qtrx->qt_rtblk_res && delta > 0) {
  243. qtrx->qt_rtblk_res_used += (ulong)delta;
  244. ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
  245. }
  246. qtrx->qt_rtbcount_delta += delta;
  247. break;
  248. case XFS_TRANS_DQ_DELRTBCOUNT:
  249. qtrx->qt_delrtb_delta += delta;
  250. break;
  251. default:
  252. ASSERT(0);
  253. }
  254. tp->t_flags |= XFS_TRANS_DQ_DIRTY;
  255. }
  256. /*
  257. * Given an array of dqtrx structures, lock all the dquots associated
  258. * and join them to the transaction, provided they have been modified.
  259. * We know that the highest number of dquots (of one type - usr OR grp),
  260. * involved in a transaction is 2 and that both usr and grp combined - 3.
  261. * So, we don't attempt to make this very generic.
  262. */
  263. STATIC void
  264. xfs_trans_dqlockedjoin(
  265. xfs_trans_t *tp,
  266. xfs_dqtrx_t *q)
  267. {
  268. ASSERT(q[0].qt_dquot != NULL);
  269. if (q[1].qt_dquot == NULL) {
  270. xfs_dqlock(q[0].qt_dquot);
  271. xfs_trans_dqjoin(tp, q[0].qt_dquot);
  272. } else {
  273. ASSERT(XFS_QM_TRANS_MAXDQS == 2);
  274. xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
  275. xfs_trans_dqjoin(tp, q[0].qt_dquot);
  276. xfs_trans_dqjoin(tp, q[1].qt_dquot);
  277. }
  278. }
  279. /*
  280. * Called by xfs_trans_commit() and similar in spirit to
  281. * xfs_trans_apply_sb_deltas().
  282. * Go thru all the dquots belonging to this transaction and modify the
  283. * INCORE dquot to reflect the actual usages.
  284. * Unreserve just the reservations done by this transaction.
  285. * dquot is still left locked at exit.
  286. */
  287. void
  288. xfs_trans_apply_dquot_deltas(
  289. xfs_trans_t *tp)
  290. {
  291. int i, j;
  292. xfs_dquot_t *dqp;
  293. xfs_dqtrx_t *qtrx, *qa;
  294. xfs_disk_dquot_t *d;
  295. long totalbdelta;
  296. long totalrtbdelta;
  297. if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
  298. return;
  299. ASSERT(tp->t_dqinfo);
  300. qa = tp->t_dqinfo->dqa_usrdquots;
  301. for (j = 0; j < 2; j++) {
  302. if (qa[0].qt_dquot == NULL) {
  303. qa = tp->t_dqinfo->dqa_grpdquots;
  304. continue;
  305. }
  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 QUOTADEBUG
  342. if (totalbdelta < 0)
  343. ASSERT(be64_to_cpu(d->d_bcount) >=
  344. (xfs_qcnt_t) -totalbdelta);
  345. if (totalrtbdelta < 0)
  346. ASSERT(be64_to_cpu(d->d_rtbcount) >=
  347. (xfs_qcnt_t) -totalrtbdelta);
  348. if (qtrx->qt_icount_delta < 0)
  349. ASSERT(be64_to_cpu(d->d_icount) >=
  350. (xfs_qcnt_t) -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, d);
  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. * Do the group quotas next
  445. */
  446. qa = tp->t_dqinfo->dqa_grpdquots;
  447. }
  448. }
  449. /*
  450. * Release the reservations, and adjust the dquots accordingly.
  451. * This is called only when the transaction is being aborted. If by
  452. * any chance we have done dquot modifications incore (ie. deltas) already,
  453. * we simply throw those away, since that's the expected behavior
  454. * when a transaction is curtailed without a commit.
  455. */
  456. void
  457. xfs_trans_unreserve_and_mod_dquots(
  458. xfs_trans_t *tp)
  459. {
  460. int i, j;
  461. xfs_dquot_t *dqp;
  462. xfs_dqtrx_t *qtrx, *qa;
  463. boolean_t locked;
  464. if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
  465. return;
  466. qa = tp->t_dqinfo->dqa_usrdquots;
  467. for (j = 0; j < 2; j++) {
  468. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  469. qtrx = &qa[i];
  470. /*
  471. * We assume that the array of dquots is filled
  472. * sequentially, not sparsely.
  473. */
  474. if ((dqp = qtrx->qt_dquot) == NULL)
  475. break;
  476. /*
  477. * Unreserve the original reservation. We don't care
  478. * about the number of blocks used field, or deltas.
  479. * Also we don't bother to zero the fields.
  480. */
  481. locked = B_FALSE;
  482. if (qtrx->qt_blk_res) {
  483. xfs_dqlock(dqp);
  484. locked = B_TRUE;
  485. dqp->q_res_bcount -=
  486. (xfs_qcnt_t)qtrx->qt_blk_res;
  487. }
  488. if (qtrx->qt_ino_res) {
  489. if (!locked) {
  490. xfs_dqlock(dqp);
  491. locked = B_TRUE;
  492. }
  493. dqp->q_res_icount -=
  494. (xfs_qcnt_t)qtrx->qt_ino_res;
  495. }
  496. if (qtrx->qt_rtblk_res) {
  497. if (!locked) {
  498. xfs_dqlock(dqp);
  499. locked = B_TRUE;
  500. }
  501. dqp->q_res_rtbcount -=
  502. (xfs_qcnt_t)qtrx->qt_rtblk_res;
  503. }
  504. if (locked)
  505. xfs_dqunlock(dqp);
  506. }
  507. qa = tp->t_dqinfo->dqa_grpdquots;
  508. }
  509. }
  510. STATIC void
  511. xfs_quota_warn(
  512. struct xfs_mount *mp,
  513. struct xfs_dquot *dqp,
  514. int type)
  515. {
  516. /* no warnings for project quotas - we just return ENOSPC later */
  517. if (dqp->dq_flags & XFS_DQ_PROJ)
  518. return;
  519. quota_send_warning((dqp->dq_flags & XFS_DQ_USER) ? USRQUOTA : GRPQUOTA,
  520. be32_to_cpu(dqp->q_core.d_id), mp->m_super->s_dev,
  521. 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 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. #ifdef QUOTADEBUG
  577. xfs_debug(mp,
  578. "BLK Res: nblks=%ld + resbcount=%Ld > hardlimit=%Ld?",
  579. nblks, *resbcountp, hardlimit);
  580. #endif
  581. if (nblks > 0) {
  582. /*
  583. * dquot is locked already. See if we'd go over the
  584. * hardlimit or exceed the timelimit if we allocate
  585. * nblks.
  586. */
  587. if (hardlimit > 0ULL &&
  588. hardlimit <= nblks + *resbcountp) {
  589. xfs_quota_warn(mp, dqp, QUOTA_NL_BHARDWARN);
  590. goto error_return;
  591. }
  592. if (softlimit > 0ULL &&
  593. softlimit <= nblks + *resbcountp) {
  594. if ((timer != 0 && get_seconds() > timer) ||
  595. (warns != 0 && warns >= warnlimit)) {
  596. xfs_quota_warn(mp, dqp,
  597. QUOTA_NL_BSOFTLONGWARN);
  598. goto error_return;
  599. }
  600. xfs_quota_warn(mp, dqp, QUOTA_NL_BSOFTWARN);
  601. }
  602. }
  603. if (ninos > 0) {
  604. count = be64_to_cpu(dqp->q_core.d_icount);
  605. timer = be32_to_cpu(dqp->q_core.d_itimer);
  606. warns = be16_to_cpu(dqp->q_core.d_iwarns);
  607. warnlimit = dqp->q_mount->m_quotainfo->qi_iwarnlimit;
  608. hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  609. if (!hardlimit)
  610. hardlimit = q->qi_ihardlimit;
  611. softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  612. if (!softlimit)
  613. softlimit = q->qi_isoftlimit;
  614. if (hardlimit > 0ULL && count >= hardlimit) {
  615. xfs_quota_warn(mp, dqp, QUOTA_NL_IHARDWARN);
  616. goto error_return;
  617. }
  618. if (softlimit > 0ULL && count >= softlimit) {
  619. if ((timer != 0 && get_seconds() > timer) ||
  620. (warns != 0 && warns >= warnlimit)) {
  621. xfs_quota_warn(mp, dqp,
  622. QUOTA_NL_ISOFTLONGWARN);
  623. goto error_return;
  624. }
  625. xfs_quota_warn(mp, dqp, QUOTA_NL_ISOFTWARN);
  626. }
  627. }
  628. }
  629. /*
  630. * Change the reservation, but not the actual usage.
  631. * Note that q_res_bcount = q_core.d_bcount + resv
  632. */
  633. (*resbcountp) += (xfs_qcnt_t)nblks;
  634. if (ninos != 0)
  635. dqp->q_res_icount += (xfs_qcnt_t)ninos;
  636. /*
  637. * note the reservation amt in the trans struct too,
  638. * so that the transaction knows how much was reserved by
  639. * it against this particular dquot.
  640. * We don't do this when we are reserving for a delayed allocation,
  641. * because we don't have the luxury of a transaction envelope then.
  642. */
  643. if (tp) {
  644. ASSERT(tp->t_dqinfo);
  645. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  646. if (nblks != 0)
  647. xfs_trans_mod_dquot(tp, dqp,
  648. flags & XFS_QMOPT_RESBLK_MASK,
  649. nblks);
  650. if (ninos != 0)
  651. xfs_trans_mod_dquot(tp, dqp,
  652. XFS_TRANS_DQ_RES_INOS,
  653. ninos);
  654. }
  655. ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
  656. ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
  657. ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
  658. xfs_dqunlock(dqp);
  659. return 0;
  660. error_return:
  661. xfs_dqunlock(dqp);
  662. if (flags & XFS_QMOPT_ENOSPC)
  663. return ENOSPC;
  664. return EDQUOT;
  665. }
  666. /*
  667. * Given dquot(s), make disk block and/or inode reservations against them.
  668. * The fact that this does the reservation against both the usr and
  669. * grp/prj quotas is important, because this follows a both-or-nothing
  670. * approach.
  671. *
  672. * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
  673. * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
  674. * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
  675. * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
  676. * dquots are unlocked on return, if they were not locked by caller.
  677. */
  678. int
  679. xfs_trans_reserve_quota_bydquots(
  680. xfs_trans_t *tp,
  681. xfs_mount_t *mp,
  682. xfs_dquot_t *udqp,
  683. xfs_dquot_t *gdqp,
  684. long nblks,
  685. long ninos,
  686. uint flags)
  687. {
  688. int resvd = 0, error;
  689. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  690. return 0;
  691. if (tp && tp->t_dqinfo == NULL)
  692. xfs_trans_alloc_dqinfo(tp);
  693. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  694. if (udqp) {
  695. error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
  696. (flags & ~XFS_QMOPT_ENOSPC));
  697. if (error)
  698. return error;
  699. resvd = 1;
  700. }
  701. if (gdqp) {
  702. error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
  703. if (error) {
  704. /*
  705. * can't do it, so backout previous reservation
  706. */
  707. if (resvd) {
  708. flags |= XFS_QMOPT_FORCE_RES;
  709. xfs_trans_dqresv(tp, mp, udqp,
  710. -nblks, -ninos, flags);
  711. }
  712. return error;
  713. }
  714. }
  715. /*
  716. * Didn't change anything critical, so, no need to log
  717. */
  718. return 0;
  719. }
  720. /*
  721. * Lock the dquot and change the reservation if we can.
  722. * This doesn't change the actual usage, just the reservation.
  723. * The inode sent in is locked.
  724. */
  725. int
  726. xfs_trans_reserve_quota_nblks(
  727. struct xfs_trans *tp,
  728. struct xfs_inode *ip,
  729. long nblks,
  730. long ninos,
  731. uint flags)
  732. {
  733. struct xfs_mount *mp = ip->i_mount;
  734. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  735. return 0;
  736. if (XFS_IS_PQUOTA_ON(mp))
  737. flags |= XFS_QMOPT_ENOSPC;
  738. ASSERT(ip->i_ino != mp->m_sb.sb_uquotino);
  739. ASSERT(ip->i_ino != mp->m_sb.sb_gquotino);
  740. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  741. ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  742. XFS_TRANS_DQ_RES_RTBLKS ||
  743. (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  744. XFS_TRANS_DQ_RES_BLKS);
  745. /*
  746. * Reserve nblks against these dquots, with trans as the mediator.
  747. */
  748. return xfs_trans_reserve_quota_bydquots(tp, mp,
  749. ip->i_udquot, ip->i_gdquot,
  750. nblks, ninos, flags);
  751. }
  752. /*
  753. * This routine is called to allocate a quotaoff log item.
  754. */
  755. xfs_qoff_logitem_t *
  756. xfs_trans_get_qoff_item(
  757. xfs_trans_t *tp,
  758. xfs_qoff_logitem_t *startqoff,
  759. uint flags)
  760. {
  761. xfs_qoff_logitem_t *q;
  762. ASSERT(tp != NULL);
  763. q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
  764. ASSERT(q != NULL);
  765. /*
  766. * Get a log_item_desc to point at the new item.
  767. */
  768. xfs_trans_add_item(tp, &q->qql_item);
  769. return q;
  770. }
  771. /*
  772. * This is called to mark the quotaoff logitem as needing
  773. * to be logged when the transaction is committed. The logitem must
  774. * already be associated with the given transaction.
  775. */
  776. void
  777. xfs_trans_log_quotaoff_item(
  778. xfs_trans_t *tp,
  779. xfs_qoff_logitem_t *qlp)
  780. {
  781. tp->t_flags |= XFS_TRANS_DIRTY;
  782. qlp->qql_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  783. }
  784. STATIC void
  785. xfs_trans_alloc_dqinfo(
  786. xfs_trans_t *tp)
  787. {
  788. tp->t_dqinfo = kmem_zone_zalloc(xfs_Gqm->qm_dqtrxzone, KM_SLEEP);
  789. }
  790. void
  791. xfs_trans_free_dqinfo(
  792. xfs_trans_t *tp)
  793. {
  794. if (!tp->t_dqinfo)
  795. return;
  796. kmem_zone_free(xfs_Gqm->qm_dqtrxzone, tp->t_dqinfo);
  797. tp->t_dqinfo = NULL;
  798. }