xfs_trans_dquot.c 22 KB

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