xfs_trans_dquot.c 22 KB

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