xfs_trans_dquot.c 23 KB

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