xfs_trans_dquot.c 22 KB

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