xfs_trans_dquot.c 22 KB

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