xfs_trans_dquot.c 22 KB

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