xfs_trans_dquot.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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_format.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_alloc.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_itable.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_rtalloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_attr.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_trans_priv.h"
  37. #include "xfs_qm.h"
  38. STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *);
  39. /*
  40. * Add the locked dquot to the transaction.
  41. * The dquot must be locked, and it cannot be associated with any
  42. * transaction.
  43. */
  44. void
  45. xfs_trans_dqjoin(
  46. xfs_trans_t *tp,
  47. xfs_dquot_t *dqp)
  48. {
  49. ASSERT(dqp->q_transp != tp);
  50. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  51. ASSERT(dqp->q_logitem.qli_dquot == dqp);
  52. /*
  53. * Get a log_item_desc to point at the new item.
  54. */
  55. xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
  56. /*
  57. * Initialize d_transp so we can later determine if this dquot is
  58. * associated with this transaction.
  59. */
  60. dqp->q_transp = tp;
  61. }
  62. /*
  63. * This is called to mark the dquot as needing
  64. * to be logged when the transaction is committed. The dquot must
  65. * already be associated with the given transaction.
  66. * Note that it marks the entire transaction as dirty. In the ordinary
  67. * case, this gets called via xfs_trans_commit, after the transaction
  68. * is already dirty. However, there's nothing stop this from getting
  69. * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
  70. * flag.
  71. */
  72. void
  73. xfs_trans_log_dquot(
  74. xfs_trans_t *tp,
  75. xfs_dquot_t *dqp)
  76. {
  77. ASSERT(dqp->q_transp == tp);
  78. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  79. tp->t_flags |= XFS_TRANS_DIRTY;
  80. dqp->q_logitem.qli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  81. }
  82. /*
  83. * Carry forward whatever is left of the quota blk reservation to
  84. * the spanky new transaction
  85. */
  86. void
  87. xfs_trans_dup_dqinfo(
  88. xfs_trans_t *otp,
  89. xfs_trans_t *ntp)
  90. {
  91. xfs_dqtrx_t *oq, *nq;
  92. int i,j;
  93. xfs_dqtrx_t *oqa, *nqa;
  94. if (!otp->t_dqinfo)
  95. return;
  96. xfs_trans_alloc_dqinfo(ntp);
  97. /*
  98. * Because the quota blk reservation is carried forward,
  99. * it is also necessary to carry forward the DQ_DIRTY flag.
  100. */
  101. if(otp->t_flags & XFS_TRANS_DQ_DIRTY)
  102. ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
  103. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  104. oqa = otp->t_dqinfo->dqs[j];
  105. nqa = ntp->t_dqinfo->dqs[j];
  106. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  107. if (oqa[i].qt_dquot == NULL)
  108. break;
  109. oq = &oqa[i];
  110. nq = &nqa[i];
  111. nq->qt_dquot = oq->qt_dquot;
  112. nq->qt_bcount_delta = nq->qt_icount_delta = 0;
  113. nq->qt_rtbcount_delta = 0;
  114. /*
  115. * Transfer whatever is left of the reservations.
  116. */
  117. nq->qt_blk_res = oq->qt_blk_res - oq->qt_blk_res_used;
  118. oq->qt_blk_res = oq->qt_blk_res_used;
  119. nq->qt_rtblk_res = oq->qt_rtblk_res -
  120. oq->qt_rtblk_res_used;
  121. oq->qt_rtblk_res = oq->qt_rtblk_res_used;
  122. nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
  123. oq->qt_ino_res = oq->qt_ino_res_used;
  124. }
  125. }
  126. }
  127. /*
  128. * Wrap around mod_dquot to account for both user and group quotas.
  129. */
  130. void
  131. xfs_trans_mod_dquot_byino(
  132. xfs_trans_t *tp,
  133. xfs_inode_t *ip,
  134. uint field,
  135. long delta)
  136. {
  137. xfs_mount_t *mp = tp->t_mountp;
  138. if (!XFS_IS_QUOTA_RUNNING(mp) ||
  139. !XFS_IS_QUOTA_ON(mp) ||
  140. xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
  141. return;
  142. if (tp->t_dqinfo == NULL)
  143. xfs_trans_alloc_dqinfo(tp);
  144. if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
  145. (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
  146. if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
  147. (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
  148. if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
  149. (void) xfs_trans_mod_dquot(tp, ip->i_pdquot, field, delta);
  150. }
  151. STATIC struct xfs_dqtrx *
  152. xfs_trans_get_dqtrx(
  153. struct xfs_trans *tp,
  154. struct xfs_dquot *dqp)
  155. {
  156. int i;
  157. struct xfs_dqtrx *qa;
  158. if (XFS_QM_ISUDQ(dqp))
  159. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
  160. else if (XFS_QM_ISGDQ(dqp))
  161. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
  162. else if (XFS_QM_ISPDQ(dqp))
  163. qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
  164. else
  165. return NULL;
  166. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  167. if (qa[i].qt_dquot == NULL ||
  168. qa[i].qt_dquot == dqp)
  169. return &qa[i];
  170. }
  171. return NULL;
  172. }
  173. /*
  174. * Make the changes in the transaction structure.
  175. * The moral equivalent to xfs_trans_mod_sb().
  176. * We don't touch any fields in the dquot, so we don't care
  177. * if it's locked or not (most of the time it won't be).
  178. */
  179. void
  180. xfs_trans_mod_dquot(
  181. xfs_trans_t *tp,
  182. xfs_dquot_t *dqp,
  183. uint field,
  184. long delta)
  185. {
  186. xfs_dqtrx_t *qtrx;
  187. ASSERT(tp);
  188. ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
  189. qtrx = NULL;
  190. if (tp->t_dqinfo == NULL)
  191. xfs_trans_alloc_dqinfo(tp);
  192. /*
  193. * Find either the first free slot or the slot that belongs
  194. * to this dquot.
  195. */
  196. qtrx = xfs_trans_get_dqtrx(tp, dqp);
  197. ASSERT(qtrx);
  198. if (qtrx->qt_dquot == NULL)
  199. qtrx->qt_dquot = dqp;
  200. switch (field) {
  201. /*
  202. * regular disk blk reservation
  203. */
  204. case XFS_TRANS_DQ_RES_BLKS:
  205. qtrx->qt_blk_res += (ulong)delta;
  206. break;
  207. /*
  208. * inode reservation
  209. */
  210. case XFS_TRANS_DQ_RES_INOS:
  211. qtrx->qt_ino_res += (ulong)delta;
  212. break;
  213. /*
  214. * disk blocks used.
  215. */
  216. case XFS_TRANS_DQ_BCOUNT:
  217. if (qtrx->qt_blk_res && delta > 0) {
  218. qtrx->qt_blk_res_used += (ulong)delta;
  219. ASSERT(qtrx->qt_blk_res >= qtrx->qt_blk_res_used);
  220. }
  221. qtrx->qt_bcount_delta += delta;
  222. break;
  223. case XFS_TRANS_DQ_DELBCOUNT:
  224. qtrx->qt_delbcnt_delta += delta;
  225. break;
  226. /*
  227. * Inode Count
  228. */
  229. case XFS_TRANS_DQ_ICOUNT:
  230. if (qtrx->qt_ino_res && delta > 0) {
  231. qtrx->qt_ino_res_used += (ulong)delta;
  232. ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
  233. }
  234. qtrx->qt_icount_delta += delta;
  235. break;
  236. /*
  237. * rtblk reservation
  238. */
  239. case XFS_TRANS_DQ_RES_RTBLKS:
  240. qtrx->qt_rtblk_res += (ulong)delta;
  241. break;
  242. /*
  243. * rtblk count
  244. */
  245. case XFS_TRANS_DQ_RTBCOUNT:
  246. if (qtrx->qt_rtblk_res && delta > 0) {
  247. qtrx->qt_rtblk_res_used += (ulong)delta;
  248. ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
  249. }
  250. qtrx->qt_rtbcount_delta += delta;
  251. break;
  252. case XFS_TRANS_DQ_DELRTBCOUNT:
  253. qtrx->qt_delrtb_delta += delta;
  254. break;
  255. default:
  256. ASSERT(0);
  257. }
  258. tp->t_flags |= XFS_TRANS_DQ_DIRTY;
  259. }
  260. /*
  261. * Given an array of dqtrx structures, lock all the dquots associated and join
  262. * them to the transaction, provided they have been modified. We know that the
  263. * highest number of dquots of one type - usr, grp OR prj - involved in a
  264. * transaction is 2 so we don't need to make this very generic.
  265. */
  266. STATIC void
  267. xfs_trans_dqlockedjoin(
  268. xfs_trans_t *tp,
  269. xfs_dqtrx_t *q)
  270. {
  271. ASSERT(q[0].qt_dquot != NULL);
  272. if (q[1].qt_dquot == NULL) {
  273. xfs_dqlock(q[0].qt_dquot);
  274. xfs_trans_dqjoin(tp, q[0].qt_dquot);
  275. } else {
  276. ASSERT(XFS_QM_TRANS_MAXDQS == 2);
  277. xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
  278. xfs_trans_dqjoin(tp, q[0].qt_dquot);
  279. xfs_trans_dqjoin(tp, q[1].qt_dquot);
  280. }
  281. }
  282. /*
  283. * Called by xfs_trans_commit() and similar in spirit to
  284. * xfs_trans_apply_sb_deltas().
  285. * Go thru all the dquots belonging to this transaction and modify the
  286. * INCORE dquot to reflect the actual usages.
  287. * Unreserve just the reservations done by this transaction.
  288. * dquot is still left locked at exit.
  289. */
  290. void
  291. xfs_trans_apply_dquot_deltas(
  292. struct xfs_trans *tp)
  293. {
  294. int i, j;
  295. struct xfs_dquot *dqp;
  296. struct xfs_dqtrx *qtrx, *qa;
  297. struct xfs_disk_dquot *d;
  298. long totalbdelta;
  299. long totalrtbdelta;
  300. if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
  301. return;
  302. ASSERT(tp->t_dqinfo);
  303. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  304. qa = tp->t_dqinfo->dqs[j];
  305. if (qa[0].qt_dquot == NULL)
  306. continue;
  307. /*
  308. * Lock all of the dquots and join them to the transaction.
  309. */
  310. xfs_trans_dqlockedjoin(tp, qa);
  311. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  312. qtrx = &qa[i];
  313. /*
  314. * The array of dquots is filled
  315. * sequentially, not sparsely.
  316. */
  317. if ((dqp = qtrx->qt_dquot) == NULL)
  318. break;
  319. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  320. ASSERT(dqp->q_transp == tp);
  321. /*
  322. * adjust the actual number of blocks used
  323. */
  324. d = &dqp->q_core;
  325. /*
  326. * The issue here is - sometimes we don't make a blkquota
  327. * reservation intentionally to be fair to users
  328. * (when the amount is small). On the other hand,
  329. * delayed allocs do make reservations, but that's
  330. * outside of a transaction, so we have no
  331. * idea how much was really reserved.
  332. * So, here we've accumulated delayed allocation blks and
  333. * non-delay blks. The assumption is that the
  334. * delayed ones are always reserved (outside of a
  335. * transaction), and the others may or may not have
  336. * quota reservations.
  337. */
  338. totalbdelta = qtrx->qt_bcount_delta +
  339. qtrx->qt_delbcnt_delta;
  340. totalrtbdelta = qtrx->qt_rtbcount_delta +
  341. qtrx->qt_delrtb_delta;
  342. #ifdef DEBUG
  343. if (totalbdelta < 0)
  344. ASSERT(be64_to_cpu(d->d_bcount) >=
  345. -totalbdelta);
  346. if (totalrtbdelta < 0)
  347. ASSERT(be64_to_cpu(d->d_rtbcount) >=
  348. -totalrtbdelta);
  349. if (qtrx->qt_icount_delta < 0)
  350. ASSERT(be64_to_cpu(d->d_icount) >=
  351. -qtrx->qt_icount_delta);
  352. #endif
  353. if (totalbdelta)
  354. be64_add_cpu(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
  355. if (qtrx->qt_icount_delta)
  356. be64_add_cpu(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
  357. if (totalrtbdelta)
  358. be64_add_cpu(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
  359. /*
  360. * Get any default limits in use.
  361. * Start/reset the timer(s) if needed.
  362. */
  363. if (d->d_id) {
  364. xfs_qm_adjust_dqlimits(tp->t_mountp, dqp);
  365. xfs_qm_adjust_dqtimers(tp->t_mountp, d);
  366. }
  367. dqp->dq_flags |= XFS_DQ_DIRTY;
  368. /*
  369. * add this to the list of items to get logged
  370. */
  371. xfs_trans_log_dquot(tp, dqp);
  372. /*
  373. * Take off what's left of the original reservation.
  374. * In case of delayed allocations, there's no
  375. * reservation that a transaction structure knows of.
  376. */
  377. if (qtrx->qt_blk_res != 0) {
  378. if (qtrx->qt_blk_res != qtrx->qt_blk_res_used) {
  379. if (qtrx->qt_blk_res >
  380. qtrx->qt_blk_res_used)
  381. dqp->q_res_bcount -= (xfs_qcnt_t)
  382. (qtrx->qt_blk_res -
  383. qtrx->qt_blk_res_used);
  384. else
  385. dqp->q_res_bcount -= (xfs_qcnt_t)
  386. (qtrx->qt_blk_res_used -
  387. qtrx->qt_blk_res);
  388. }
  389. } else {
  390. /*
  391. * These blks were never reserved, either inside
  392. * a transaction or outside one (in a delayed
  393. * allocation). Also, this isn't always a
  394. * negative number since we sometimes
  395. * deliberately skip quota reservations.
  396. */
  397. if (qtrx->qt_bcount_delta) {
  398. dqp->q_res_bcount +=
  399. (xfs_qcnt_t)qtrx->qt_bcount_delta;
  400. }
  401. }
  402. /*
  403. * Adjust the RT reservation.
  404. */
  405. if (qtrx->qt_rtblk_res != 0) {
  406. if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
  407. if (qtrx->qt_rtblk_res >
  408. qtrx->qt_rtblk_res_used)
  409. dqp->q_res_rtbcount -= (xfs_qcnt_t)
  410. (qtrx->qt_rtblk_res -
  411. qtrx->qt_rtblk_res_used);
  412. else
  413. dqp->q_res_rtbcount -= (xfs_qcnt_t)
  414. (qtrx->qt_rtblk_res_used -
  415. qtrx->qt_rtblk_res);
  416. }
  417. } else {
  418. if (qtrx->qt_rtbcount_delta)
  419. dqp->q_res_rtbcount +=
  420. (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
  421. }
  422. /*
  423. * Adjust the inode reservation.
  424. */
  425. if (qtrx->qt_ino_res != 0) {
  426. ASSERT(qtrx->qt_ino_res >=
  427. qtrx->qt_ino_res_used);
  428. if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
  429. dqp->q_res_icount -= (xfs_qcnt_t)
  430. (qtrx->qt_ino_res -
  431. qtrx->qt_ino_res_used);
  432. } else {
  433. if (qtrx->qt_icount_delta)
  434. dqp->q_res_icount +=
  435. (xfs_qcnt_t)qtrx->qt_icount_delta;
  436. }
  437. ASSERT(dqp->q_res_bcount >=
  438. be64_to_cpu(dqp->q_core.d_bcount));
  439. ASSERT(dqp->q_res_icount >=
  440. be64_to_cpu(dqp->q_core.d_icount));
  441. ASSERT(dqp->q_res_rtbcount >=
  442. be64_to_cpu(dqp->q_core.d_rtbcount));
  443. }
  444. }
  445. }
  446. /*
  447. * Release the reservations, and adjust the dquots accordingly.
  448. * This is called only when the transaction is being aborted. If by
  449. * any chance we have done dquot modifications incore (ie. deltas) already,
  450. * we simply throw those away, since that's the expected behavior
  451. * when a transaction is curtailed without a commit.
  452. */
  453. void
  454. xfs_trans_unreserve_and_mod_dquots(
  455. xfs_trans_t *tp)
  456. {
  457. int i, j;
  458. xfs_dquot_t *dqp;
  459. xfs_dqtrx_t *qtrx, *qa;
  460. bool locked;
  461. if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
  462. return;
  463. for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
  464. qa = tp->t_dqinfo->dqs[j];
  465. for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
  466. qtrx = &qa[i];
  467. /*
  468. * We assume that the array of dquots is filled
  469. * sequentially, not sparsely.
  470. */
  471. if ((dqp = qtrx->qt_dquot) == NULL)
  472. break;
  473. /*
  474. * Unreserve the original reservation. We don't care
  475. * about the number of blocks used field, or deltas.
  476. * Also we don't bother to zero the fields.
  477. */
  478. locked = false;
  479. if (qtrx->qt_blk_res) {
  480. xfs_dqlock(dqp);
  481. locked = true;
  482. dqp->q_res_bcount -=
  483. (xfs_qcnt_t)qtrx->qt_blk_res;
  484. }
  485. if (qtrx->qt_ino_res) {
  486. if (!locked) {
  487. xfs_dqlock(dqp);
  488. locked = true;
  489. }
  490. dqp->q_res_icount -=
  491. (xfs_qcnt_t)qtrx->qt_ino_res;
  492. }
  493. if (qtrx->qt_rtblk_res) {
  494. if (!locked) {
  495. xfs_dqlock(dqp);
  496. locked = true;
  497. }
  498. dqp->q_res_rtbcount -=
  499. (xfs_qcnt_t)qtrx->qt_rtblk_res;
  500. }
  501. if (locked)
  502. xfs_dqunlock(dqp);
  503. }
  504. }
  505. }
  506. STATIC void
  507. xfs_quota_warn(
  508. struct xfs_mount *mp,
  509. struct xfs_dquot *dqp,
  510. int type)
  511. {
  512. /* no warnings for project quotas - we just return ENOSPC later */
  513. if (dqp->dq_flags & XFS_DQ_PROJ)
  514. return;
  515. quota_send_warning(make_kqid(&init_user_ns,
  516. (dqp->dq_flags & XFS_DQ_USER) ?
  517. USRQUOTA : GRPQUOTA,
  518. be32_to_cpu(dqp->q_core.d_id)),
  519. mp->m_super->s_dev, 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_GQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISGDQ(dqp)) ||
  573. (XFS_IS_PQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISPDQ(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 user, group and
  661. * project quotas is important, because this follows a all-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. struct xfs_trans *tp,
  673. struct xfs_mount *mp,
  674. struct xfs_dquot *udqp,
  675. struct xfs_dquot *gdqp,
  676. struct xfs_dquot *pdqp,
  677. long nblks,
  678. long ninos,
  679. uint flags)
  680. {
  681. int error;
  682. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  683. return 0;
  684. if (tp && tp->t_dqinfo == NULL)
  685. xfs_trans_alloc_dqinfo(tp);
  686. ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
  687. if (udqp) {
  688. error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
  689. (flags & ~XFS_QMOPT_ENOSPC));
  690. if (error)
  691. return error;
  692. }
  693. if (gdqp) {
  694. error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
  695. if (error)
  696. goto unwind_usr;
  697. }
  698. if (pdqp) {
  699. error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
  700. if (error)
  701. goto unwind_grp;
  702. }
  703. /*
  704. * Didn't change anything critical, so, no need to log
  705. */
  706. return 0;
  707. unwind_grp:
  708. flags |= XFS_QMOPT_FORCE_RES;
  709. if (gdqp)
  710. xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
  711. unwind_usr:
  712. flags |= XFS_QMOPT_FORCE_RES;
  713. if (udqp)
  714. xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
  715. return error;
  716. }
  717. /*
  718. * Lock the dquot and change the reservation if we can.
  719. * This doesn't change the actual usage, just the reservation.
  720. * The inode sent in is locked.
  721. */
  722. int
  723. xfs_trans_reserve_quota_nblks(
  724. struct xfs_trans *tp,
  725. struct xfs_inode *ip,
  726. long nblks,
  727. long ninos,
  728. uint flags)
  729. {
  730. struct xfs_mount *mp = ip->i_mount;
  731. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  732. return 0;
  733. if (XFS_IS_PQUOTA_ON(mp))
  734. flags |= XFS_QMOPT_ENOSPC;
  735. ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
  736. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  737. ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  738. XFS_TRANS_DQ_RES_RTBLKS ||
  739. (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
  740. XFS_TRANS_DQ_RES_BLKS);
  741. /*
  742. * Reserve nblks against these dquots, with trans as the mediator.
  743. */
  744. return xfs_trans_reserve_quota_bydquots(tp, mp,
  745. ip->i_udquot, ip->i_gdquot,
  746. ip->i_pdquot,
  747. nblks, ninos, flags);
  748. }
  749. /*
  750. * This routine is called to allocate a quotaoff log item.
  751. */
  752. xfs_qoff_logitem_t *
  753. xfs_trans_get_qoff_item(
  754. xfs_trans_t *tp,
  755. xfs_qoff_logitem_t *startqoff,
  756. uint flags)
  757. {
  758. xfs_qoff_logitem_t *q;
  759. ASSERT(tp != NULL);
  760. q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
  761. ASSERT(q != NULL);
  762. /*
  763. * Get a log_item_desc to point at the new item.
  764. */
  765. xfs_trans_add_item(tp, &q->qql_item);
  766. return q;
  767. }
  768. /*
  769. * This is called to mark the quotaoff logitem as needing
  770. * to be logged when the transaction is committed. The logitem must
  771. * already be associated with the given transaction.
  772. */
  773. void
  774. xfs_trans_log_quotaoff_item(
  775. xfs_trans_t *tp,
  776. xfs_qoff_logitem_t *qlp)
  777. {
  778. tp->t_flags |= XFS_TRANS_DIRTY;
  779. qlp->qql_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  780. }
  781. STATIC void
  782. xfs_trans_alloc_dqinfo(
  783. xfs_trans_t *tp)
  784. {
  785. tp->t_dqinfo = kmem_zone_zalloc(xfs_qm_dqtrxzone, KM_SLEEP);
  786. }
  787. void
  788. xfs_trans_free_dqinfo(
  789. xfs_trans_t *tp)
  790. {
  791. if (!tp->t_dqinfo)
  792. return;
  793. kmem_zone_free(xfs_qm_dqtrxzone, tp->t_dqinfo);
  794. tp->t_dqinfo = NULL;
  795. }