xfs_trans_dquot.c 22 KB

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