xfs_trans_dquot.c 22 KB

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