xfs_dquot.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * Copyright (c) 2000-2003 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_format.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_quota.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_rtalloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_itable.h"
  35. #include "xfs_attr.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_trans_space.h"
  38. #include "xfs_trans_priv.h"
  39. #include "xfs_qm.h"
  40. #include "xfs_cksum.h"
  41. #include "xfs_trace.h"
  42. /*
  43. * Lock order:
  44. *
  45. * ip->i_lock
  46. * qi->qi_tree_lock
  47. * dquot->q_qlock (xfs_dqlock() and friends)
  48. * dquot->q_flush (xfs_dqflock() and friends)
  49. * qi->qi_lru_lock
  50. *
  51. * If two dquots need to be locked the order is user before group/project,
  52. * otherwise by the lowest id first, see xfs_dqlock2.
  53. */
  54. #ifdef DEBUG
  55. xfs_buftarg_t *xfs_dqerror_target;
  56. int xfs_do_dqerror;
  57. int xfs_dqreq_num;
  58. int xfs_dqerror_mod = 33;
  59. #endif
  60. struct kmem_zone *xfs_qm_dqtrxzone;
  61. static struct kmem_zone *xfs_qm_dqzone;
  62. static struct lock_class_key xfs_dquot_other_class;
  63. /*
  64. * This is called to free all the memory associated with a dquot
  65. */
  66. void
  67. xfs_qm_dqdestroy(
  68. xfs_dquot_t *dqp)
  69. {
  70. ASSERT(list_empty(&dqp->q_lru));
  71. mutex_destroy(&dqp->q_qlock);
  72. kmem_zone_free(xfs_qm_dqzone, dqp);
  73. XFS_STATS_DEC(xs_qm_dquot);
  74. }
  75. /*
  76. * If default limits are in force, push them into the dquot now.
  77. * We overwrite the dquot limits only if they are zero and this
  78. * is not the root dquot.
  79. */
  80. void
  81. xfs_qm_adjust_dqlimits(
  82. struct xfs_mount *mp,
  83. struct xfs_dquot *dq)
  84. {
  85. struct xfs_quotainfo *q = mp->m_quotainfo;
  86. struct xfs_disk_dquot *d = &dq->q_core;
  87. int prealloc = 0;
  88. ASSERT(d->d_id);
  89. if (q->qi_bsoftlimit && !d->d_blk_softlimit) {
  90. d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
  91. prealloc = 1;
  92. }
  93. if (q->qi_bhardlimit && !d->d_blk_hardlimit) {
  94. d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
  95. prealloc = 1;
  96. }
  97. if (q->qi_isoftlimit && !d->d_ino_softlimit)
  98. d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
  99. if (q->qi_ihardlimit && !d->d_ino_hardlimit)
  100. d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
  101. if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
  102. d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
  103. if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
  104. d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
  105. if (prealloc)
  106. xfs_dquot_set_prealloc_limits(dq);
  107. }
  108. /*
  109. * Check the limits and timers of a dquot and start or reset timers
  110. * if necessary.
  111. * This gets called even when quota enforcement is OFF, which makes our
  112. * life a little less complicated. (We just don't reject any quota
  113. * reservations in that case, when enforcement is off).
  114. * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
  115. * enforcement's off.
  116. * In contrast, warnings are a little different in that they don't
  117. * 'automatically' get started when limits get exceeded. They do
  118. * get reset to zero, however, when we find the count to be under
  119. * the soft limit (they are only ever set non-zero via userspace).
  120. */
  121. void
  122. xfs_qm_adjust_dqtimers(
  123. xfs_mount_t *mp,
  124. xfs_disk_dquot_t *d)
  125. {
  126. ASSERT(d->d_id);
  127. #ifdef DEBUG
  128. if (d->d_blk_hardlimit)
  129. ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
  130. be64_to_cpu(d->d_blk_hardlimit));
  131. if (d->d_ino_hardlimit)
  132. ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
  133. be64_to_cpu(d->d_ino_hardlimit));
  134. if (d->d_rtb_hardlimit)
  135. ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
  136. be64_to_cpu(d->d_rtb_hardlimit));
  137. #endif
  138. if (!d->d_btimer) {
  139. if ((d->d_blk_softlimit &&
  140. (be64_to_cpu(d->d_bcount) >
  141. be64_to_cpu(d->d_blk_softlimit))) ||
  142. (d->d_blk_hardlimit &&
  143. (be64_to_cpu(d->d_bcount) >
  144. be64_to_cpu(d->d_blk_hardlimit)))) {
  145. d->d_btimer = cpu_to_be32(get_seconds() +
  146. mp->m_quotainfo->qi_btimelimit);
  147. } else {
  148. d->d_bwarns = 0;
  149. }
  150. } else {
  151. if ((!d->d_blk_softlimit ||
  152. (be64_to_cpu(d->d_bcount) <=
  153. be64_to_cpu(d->d_blk_softlimit))) &&
  154. (!d->d_blk_hardlimit ||
  155. (be64_to_cpu(d->d_bcount) <=
  156. be64_to_cpu(d->d_blk_hardlimit)))) {
  157. d->d_btimer = 0;
  158. }
  159. }
  160. if (!d->d_itimer) {
  161. if ((d->d_ino_softlimit &&
  162. (be64_to_cpu(d->d_icount) >
  163. be64_to_cpu(d->d_ino_softlimit))) ||
  164. (d->d_ino_hardlimit &&
  165. (be64_to_cpu(d->d_icount) >
  166. be64_to_cpu(d->d_ino_hardlimit)))) {
  167. d->d_itimer = cpu_to_be32(get_seconds() +
  168. mp->m_quotainfo->qi_itimelimit);
  169. } else {
  170. d->d_iwarns = 0;
  171. }
  172. } else {
  173. if ((!d->d_ino_softlimit ||
  174. (be64_to_cpu(d->d_icount) <=
  175. be64_to_cpu(d->d_ino_softlimit))) &&
  176. (!d->d_ino_hardlimit ||
  177. (be64_to_cpu(d->d_icount) <=
  178. be64_to_cpu(d->d_ino_hardlimit)))) {
  179. d->d_itimer = 0;
  180. }
  181. }
  182. if (!d->d_rtbtimer) {
  183. if ((d->d_rtb_softlimit &&
  184. (be64_to_cpu(d->d_rtbcount) >
  185. be64_to_cpu(d->d_rtb_softlimit))) ||
  186. (d->d_rtb_hardlimit &&
  187. (be64_to_cpu(d->d_rtbcount) >
  188. be64_to_cpu(d->d_rtb_hardlimit)))) {
  189. d->d_rtbtimer = cpu_to_be32(get_seconds() +
  190. mp->m_quotainfo->qi_rtbtimelimit);
  191. } else {
  192. d->d_rtbwarns = 0;
  193. }
  194. } else {
  195. if ((!d->d_rtb_softlimit ||
  196. (be64_to_cpu(d->d_rtbcount) <=
  197. be64_to_cpu(d->d_rtb_softlimit))) &&
  198. (!d->d_rtb_hardlimit ||
  199. (be64_to_cpu(d->d_rtbcount) <=
  200. be64_to_cpu(d->d_rtb_hardlimit)))) {
  201. d->d_rtbtimer = 0;
  202. }
  203. }
  204. }
  205. /*
  206. * initialize a buffer full of dquots and log the whole thing
  207. */
  208. STATIC void
  209. xfs_qm_init_dquot_blk(
  210. xfs_trans_t *tp,
  211. xfs_mount_t *mp,
  212. xfs_dqid_t id,
  213. uint type,
  214. xfs_buf_t *bp)
  215. {
  216. struct xfs_quotainfo *q = mp->m_quotainfo;
  217. xfs_dqblk_t *d;
  218. int curid, i;
  219. ASSERT(tp);
  220. ASSERT(xfs_buf_islocked(bp));
  221. d = bp->b_addr;
  222. /*
  223. * ID of the first dquot in the block - id's are zero based.
  224. */
  225. curid = id - (id % q->qi_dqperchunk);
  226. ASSERT(curid >= 0);
  227. memset(d, 0, BBTOB(q->qi_dqchunklen));
  228. for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
  229. d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  230. d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  231. d->dd_diskdq.d_id = cpu_to_be32(curid);
  232. d->dd_diskdq.d_flags = type;
  233. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  234. uuid_copy(&d->dd_uuid, &mp->m_sb.sb_uuid);
  235. xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
  236. XFS_DQUOT_CRC_OFF);
  237. }
  238. }
  239. xfs_trans_dquot_buf(tp, bp,
  240. (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
  241. ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
  242. XFS_BLF_GDQUOT_BUF)));
  243. xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
  244. }
  245. /*
  246. * Initialize the dynamic speculative preallocation thresholds. The lo/hi
  247. * watermarks correspond to the soft and hard limits by default. If a soft limit
  248. * is not specified, we use 95% of the hard limit.
  249. */
  250. void
  251. xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
  252. {
  253. __uint64_t space;
  254. dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
  255. dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
  256. if (!dqp->q_prealloc_lo_wmark) {
  257. dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
  258. do_div(dqp->q_prealloc_lo_wmark, 100);
  259. dqp->q_prealloc_lo_wmark *= 95;
  260. }
  261. space = dqp->q_prealloc_hi_wmark;
  262. do_div(space, 100);
  263. dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
  264. dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
  265. dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
  266. }
  267. STATIC bool
  268. xfs_dquot_buf_verify_crc(
  269. struct xfs_mount *mp,
  270. struct xfs_buf *bp)
  271. {
  272. struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
  273. int ndquots;
  274. int i;
  275. if (!xfs_sb_version_hascrc(&mp->m_sb))
  276. return true;
  277. /*
  278. * if we are in log recovery, the quota subsystem has not been
  279. * initialised so we have no quotainfo structure. In that case, we need
  280. * to manually calculate the number of dquots in the buffer.
  281. */
  282. if (mp->m_quotainfo)
  283. ndquots = mp->m_quotainfo->qi_dqperchunk;
  284. else
  285. ndquots = xfs_qm_calc_dquots_per_chunk(mp,
  286. XFS_BB_TO_FSB(mp, bp->b_length));
  287. for (i = 0; i < ndquots; i++, d++) {
  288. if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
  289. XFS_DQUOT_CRC_OFF))
  290. return false;
  291. if (!uuid_equal(&d->dd_uuid, &mp->m_sb.sb_uuid))
  292. return false;
  293. }
  294. return true;
  295. }
  296. STATIC bool
  297. xfs_dquot_buf_verify(
  298. struct xfs_mount *mp,
  299. struct xfs_buf *bp)
  300. {
  301. struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
  302. xfs_dqid_t id = 0;
  303. int ndquots;
  304. int i;
  305. /*
  306. * if we are in log recovery, the quota subsystem has not been
  307. * initialised so we have no quotainfo structure. In that case, we need
  308. * to manually calculate the number of dquots in the buffer.
  309. */
  310. if (mp->m_quotainfo)
  311. ndquots = mp->m_quotainfo->qi_dqperchunk;
  312. else
  313. ndquots = xfs_qm_calc_dquots_per_chunk(mp, bp->b_length);
  314. /*
  315. * On the first read of the buffer, verify that each dquot is valid.
  316. * We don't know what the id of the dquot is supposed to be, just that
  317. * they should be increasing monotonically within the buffer. If the
  318. * first id is corrupt, then it will fail on the second dquot in the
  319. * buffer so corruptions could point to the wrong dquot in this case.
  320. */
  321. for (i = 0; i < ndquots; i++) {
  322. struct xfs_disk_dquot *ddq;
  323. int error;
  324. ddq = &d[i].dd_diskdq;
  325. if (i == 0)
  326. id = be32_to_cpu(ddq->d_id);
  327. error = xfs_qm_dqcheck(mp, ddq, id + i, 0, XFS_QMOPT_DOWARN,
  328. "xfs_dquot_buf_verify");
  329. if (error)
  330. return false;
  331. }
  332. return true;
  333. }
  334. static void
  335. xfs_dquot_buf_read_verify(
  336. struct xfs_buf *bp)
  337. {
  338. struct xfs_mount *mp = bp->b_target->bt_mount;
  339. if (!xfs_dquot_buf_verify_crc(mp, bp) || !xfs_dquot_buf_verify(mp, bp)) {
  340. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  341. xfs_buf_ioerror(bp, EFSCORRUPTED);
  342. }
  343. }
  344. /*
  345. * we don't calculate the CRC here as that is done when the dquot is flushed to
  346. * the buffer after the update is done. This ensures that the dquot in the
  347. * buffer always has an up-to-date CRC value.
  348. */
  349. void
  350. xfs_dquot_buf_write_verify(
  351. struct xfs_buf *bp)
  352. {
  353. struct xfs_mount *mp = bp->b_target->bt_mount;
  354. if (!xfs_dquot_buf_verify(mp, bp)) {
  355. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  356. xfs_buf_ioerror(bp, EFSCORRUPTED);
  357. return;
  358. }
  359. }
  360. const struct xfs_buf_ops xfs_dquot_buf_ops = {
  361. .verify_read = xfs_dquot_buf_read_verify,
  362. .verify_write = xfs_dquot_buf_write_verify,
  363. };
  364. /*
  365. * Allocate a block and fill it with dquots.
  366. * This is called when the bmapi finds a hole.
  367. */
  368. STATIC int
  369. xfs_qm_dqalloc(
  370. xfs_trans_t **tpp,
  371. xfs_mount_t *mp,
  372. xfs_dquot_t *dqp,
  373. xfs_inode_t *quotip,
  374. xfs_fileoff_t offset_fsb,
  375. xfs_buf_t **O_bpp)
  376. {
  377. xfs_fsblock_t firstblock;
  378. xfs_bmap_free_t flist;
  379. xfs_bmbt_irec_t map;
  380. int nmaps, error, committed;
  381. xfs_buf_t *bp;
  382. xfs_trans_t *tp = *tpp;
  383. ASSERT(tp != NULL);
  384. trace_xfs_dqalloc(dqp);
  385. /*
  386. * Initialize the bmap freelist prior to calling bmapi code.
  387. */
  388. xfs_bmap_init(&flist, &firstblock);
  389. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  390. /*
  391. * Return if this type of quotas is turned off while we didn't
  392. * have an inode lock
  393. */
  394. if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
  395. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  396. return (ESRCH);
  397. }
  398. xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
  399. nmaps = 1;
  400. error = xfs_bmapi_write(tp, quotip, offset_fsb,
  401. XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
  402. &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
  403. &map, &nmaps, &flist);
  404. if (error)
  405. goto error0;
  406. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  407. ASSERT(nmaps == 1);
  408. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  409. (map.br_startblock != HOLESTARTBLOCK));
  410. /*
  411. * Keep track of the blkno to save a lookup later
  412. */
  413. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  414. /* now we can just get the buffer (there's nothing to read yet) */
  415. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  416. dqp->q_blkno,
  417. mp->m_quotainfo->qi_dqchunklen,
  418. 0);
  419. error = xfs_buf_geterror(bp);
  420. if (error)
  421. goto error1;
  422. bp->b_ops = &xfs_dquot_buf_ops;
  423. /*
  424. * Make a chunk of dquots out of this buffer and log
  425. * the entire thing.
  426. */
  427. xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
  428. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  429. /*
  430. * xfs_bmap_finish() may commit the current transaction and
  431. * start a second transaction if the freelist is not empty.
  432. *
  433. * Since we still want to modify this buffer, we need to
  434. * ensure that the buffer is not released on commit of
  435. * the first transaction and ensure the buffer is added to the
  436. * second transaction.
  437. *
  438. * If there is only one transaction then don't stop the buffer
  439. * from being released when it commits later on.
  440. */
  441. xfs_trans_bhold(tp, bp);
  442. if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
  443. goto error1;
  444. }
  445. if (committed) {
  446. tp = *tpp;
  447. xfs_trans_bjoin(tp, bp);
  448. } else {
  449. xfs_trans_bhold_release(tp, bp);
  450. }
  451. *O_bpp = bp;
  452. return 0;
  453. error1:
  454. xfs_bmap_cancel(&flist);
  455. error0:
  456. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  457. return (error);
  458. }
  459. STATIC int
  460. xfs_qm_dqrepair(
  461. struct xfs_mount *mp,
  462. struct xfs_trans *tp,
  463. struct xfs_dquot *dqp,
  464. xfs_dqid_t firstid,
  465. struct xfs_buf **bpp)
  466. {
  467. int error;
  468. struct xfs_disk_dquot *ddq;
  469. struct xfs_dqblk *d;
  470. int i;
  471. /*
  472. * Read the buffer without verification so we get the corrupted
  473. * buffer returned to us. make sure we verify it on write, though.
  474. */
  475. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, dqp->q_blkno,
  476. mp->m_quotainfo->qi_dqchunklen,
  477. 0, bpp, NULL);
  478. if (error) {
  479. ASSERT(*bpp == NULL);
  480. return XFS_ERROR(error);
  481. }
  482. (*bpp)->b_ops = &xfs_dquot_buf_ops;
  483. ASSERT(xfs_buf_islocked(*bpp));
  484. d = (struct xfs_dqblk *)(*bpp)->b_addr;
  485. /* Do the actual repair of dquots in this buffer */
  486. for (i = 0; i < mp->m_quotainfo->qi_dqperchunk; i++) {
  487. ddq = &d[i].dd_diskdq;
  488. error = xfs_qm_dqcheck(mp, ddq, firstid + i,
  489. dqp->dq_flags & XFS_DQ_ALLTYPES,
  490. XFS_QMOPT_DQREPAIR, "xfs_qm_dqrepair");
  491. if (error) {
  492. /* repair failed, we're screwed */
  493. xfs_trans_brelse(tp, *bpp);
  494. return XFS_ERROR(EIO);
  495. }
  496. }
  497. return 0;
  498. }
  499. /*
  500. * Maps a dquot to the buffer containing its on-disk version.
  501. * This returns a ptr to the buffer containing the on-disk dquot
  502. * in the bpp param, and a ptr to the on-disk dquot within that buffer
  503. */
  504. STATIC int
  505. xfs_qm_dqtobp(
  506. xfs_trans_t **tpp,
  507. xfs_dquot_t *dqp,
  508. xfs_disk_dquot_t **O_ddpp,
  509. xfs_buf_t **O_bpp,
  510. uint flags)
  511. {
  512. struct xfs_bmbt_irec map;
  513. int nmaps = 1, error;
  514. struct xfs_buf *bp;
  515. struct xfs_inode *quotip = xfs_dq_to_quota_inode(dqp);
  516. struct xfs_mount *mp = dqp->q_mount;
  517. xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
  518. struct xfs_trans *tp = (tpp ? *tpp : NULL);
  519. dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
  520. xfs_ilock(quotip, XFS_ILOCK_SHARED);
  521. if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
  522. /*
  523. * Return if this type of quotas is turned off while we
  524. * didn't have the quota inode lock.
  525. */
  526. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  527. return ESRCH;
  528. }
  529. /*
  530. * Find the block map; no allocations yet
  531. */
  532. error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
  533. XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
  534. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  535. if (error)
  536. return error;
  537. ASSERT(nmaps == 1);
  538. ASSERT(map.br_blockcount == 1);
  539. /*
  540. * Offset of dquot in the (fixed sized) dquot chunk.
  541. */
  542. dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
  543. sizeof(xfs_dqblk_t);
  544. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  545. if (map.br_startblock == HOLESTARTBLOCK) {
  546. /*
  547. * We don't allocate unless we're asked to
  548. */
  549. if (!(flags & XFS_QMOPT_DQALLOC))
  550. return ENOENT;
  551. ASSERT(tp);
  552. error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
  553. dqp->q_fileoffset, &bp);
  554. if (error)
  555. return error;
  556. tp = *tpp;
  557. } else {
  558. trace_xfs_dqtobp_read(dqp);
  559. /*
  560. * store the blkno etc so that we don't have to do the
  561. * mapping all the time
  562. */
  563. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  564. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  565. dqp->q_blkno,
  566. mp->m_quotainfo->qi_dqchunklen,
  567. 0, &bp, &xfs_dquot_buf_ops);
  568. if (error == EFSCORRUPTED && (flags & XFS_QMOPT_DQREPAIR)) {
  569. xfs_dqid_t firstid = (xfs_dqid_t)map.br_startoff *
  570. mp->m_quotainfo->qi_dqperchunk;
  571. ASSERT(bp == NULL);
  572. error = xfs_qm_dqrepair(mp, tp, dqp, firstid, &bp);
  573. }
  574. if (error) {
  575. ASSERT(bp == NULL);
  576. return XFS_ERROR(error);
  577. }
  578. }
  579. ASSERT(xfs_buf_islocked(bp));
  580. *O_bpp = bp;
  581. *O_ddpp = bp->b_addr + dqp->q_bufoffset;
  582. return (0);
  583. }
  584. /*
  585. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  586. * and release the buffer immediately.
  587. *
  588. * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
  589. */
  590. int
  591. xfs_qm_dqread(
  592. struct xfs_mount *mp,
  593. xfs_dqid_t id,
  594. uint type,
  595. uint flags,
  596. struct xfs_dquot **O_dqpp)
  597. {
  598. struct xfs_dquot *dqp;
  599. struct xfs_disk_dquot *ddqp;
  600. struct xfs_buf *bp;
  601. struct xfs_trans *tp = NULL;
  602. int error;
  603. int cancelflags = 0;
  604. dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
  605. dqp->dq_flags = type;
  606. dqp->q_core.d_id = cpu_to_be32(id);
  607. dqp->q_mount = mp;
  608. INIT_LIST_HEAD(&dqp->q_lru);
  609. mutex_init(&dqp->q_qlock);
  610. init_waitqueue_head(&dqp->q_pinwait);
  611. /*
  612. * Because we want to use a counting completion, complete
  613. * the flush completion once to allow a single access to
  614. * the flush completion without blocking.
  615. */
  616. init_completion(&dqp->q_flush);
  617. complete(&dqp->q_flush);
  618. /*
  619. * Make sure group quotas have a different lock class than user
  620. * quotas.
  621. */
  622. if (!(type & XFS_DQ_USER))
  623. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
  624. XFS_STATS_INC(xs_qm_dquot);
  625. trace_xfs_dqread(dqp);
  626. if (flags & XFS_QMOPT_DQALLOC) {
  627. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
  628. error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
  629. XFS_QM_DQALLOC_LOG_RES(mp), 0,
  630. XFS_TRANS_PERM_LOG_RES,
  631. XFS_WRITE_LOG_COUNT);
  632. if (error)
  633. goto error1;
  634. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  635. }
  636. /*
  637. * get a pointer to the on-disk dquot and the buffer containing it
  638. * dqp already knows its own type (GROUP/USER).
  639. */
  640. error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags);
  641. if (error) {
  642. /*
  643. * This can happen if quotas got turned off (ESRCH),
  644. * or if the dquot didn't exist on disk and we ask to
  645. * allocate (ENOENT).
  646. */
  647. trace_xfs_dqread_fail(dqp);
  648. cancelflags |= XFS_TRANS_ABORT;
  649. goto error1;
  650. }
  651. /* copy everything from disk dquot to the incore dquot */
  652. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  653. xfs_qm_dquot_logitem_init(dqp);
  654. /*
  655. * Reservation counters are defined as reservation plus current usage
  656. * to avoid having to add every time.
  657. */
  658. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  659. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  660. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  661. /* initialize the dquot speculative prealloc thresholds */
  662. xfs_dquot_set_prealloc_limits(dqp);
  663. /* Mark the buf so that this will stay incore a little longer */
  664. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  665. /*
  666. * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
  667. * So we need to release with xfs_trans_brelse().
  668. * The strategy here is identical to that of inodes; we lock
  669. * the dquot in xfs_qm_dqget() before making it accessible to
  670. * others. This is because dquots, like inodes, need a good level of
  671. * concurrency, and we don't want to take locks on the entire buffers
  672. * for dquot accesses.
  673. * Note also that the dquot buffer may even be dirty at this point, if
  674. * this particular dquot was repaired. We still aren't afraid to
  675. * brelse it because we have the changes incore.
  676. */
  677. ASSERT(xfs_buf_islocked(bp));
  678. xfs_trans_brelse(tp, bp);
  679. if (tp) {
  680. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  681. if (error)
  682. goto error0;
  683. }
  684. *O_dqpp = dqp;
  685. return error;
  686. error1:
  687. if (tp)
  688. xfs_trans_cancel(tp, cancelflags);
  689. error0:
  690. xfs_qm_dqdestroy(dqp);
  691. *O_dqpp = NULL;
  692. return error;
  693. }
  694. /*
  695. * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
  696. * a locked dquot, doing an allocation (if requested) as needed.
  697. * When both an inode and an id are given, the inode's id takes precedence.
  698. * That is, if the id changes while we don't hold the ilock inside this
  699. * function, the new dquot is returned, not necessarily the one requested
  700. * in the id argument.
  701. */
  702. int
  703. xfs_qm_dqget(
  704. xfs_mount_t *mp,
  705. xfs_inode_t *ip, /* locked inode (optional) */
  706. xfs_dqid_t id, /* uid/projid/gid depending on type */
  707. uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
  708. uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
  709. xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
  710. {
  711. struct xfs_quotainfo *qi = mp->m_quotainfo;
  712. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  713. struct xfs_dquot *dqp;
  714. int error;
  715. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  716. if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
  717. (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
  718. (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
  719. return (ESRCH);
  720. }
  721. #ifdef DEBUG
  722. if (xfs_do_dqerror) {
  723. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  724. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  725. xfs_debug(mp, "Returning error in dqget");
  726. return (EIO);
  727. }
  728. }
  729. ASSERT(type == XFS_DQ_USER ||
  730. type == XFS_DQ_PROJ ||
  731. type == XFS_DQ_GROUP);
  732. if (ip) {
  733. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  734. ASSERT(xfs_inode_dquot(ip, type) == NULL);
  735. }
  736. #endif
  737. restart:
  738. mutex_lock(&qi->qi_tree_lock);
  739. dqp = radix_tree_lookup(tree, id);
  740. if (dqp) {
  741. xfs_dqlock(dqp);
  742. if (dqp->dq_flags & XFS_DQ_FREEING) {
  743. xfs_dqunlock(dqp);
  744. mutex_unlock(&qi->qi_tree_lock);
  745. trace_xfs_dqget_freeing(dqp);
  746. delay(1);
  747. goto restart;
  748. }
  749. dqp->q_nrefs++;
  750. mutex_unlock(&qi->qi_tree_lock);
  751. trace_xfs_dqget_hit(dqp);
  752. XFS_STATS_INC(xs_qm_dqcachehits);
  753. *O_dqpp = dqp;
  754. return 0;
  755. }
  756. mutex_unlock(&qi->qi_tree_lock);
  757. XFS_STATS_INC(xs_qm_dqcachemisses);
  758. /*
  759. * Dquot cache miss. We don't want to keep the inode lock across
  760. * a (potential) disk read. Also we don't want to deal with the lock
  761. * ordering between quotainode and this inode. OTOH, dropping the inode
  762. * lock here means dealing with a chown that can happen before
  763. * we re-acquire the lock.
  764. */
  765. if (ip)
  766. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  767. error = xfs_qm_dqread(mp, id, type, flags, &dqp);
  768. if (ip)
  769. xfs_ilock(ip, XFS_ILOCK_EXCL);
  770. if (error)
  771. return error;
  772. if (ip) {
  773. /*
  774. * A dquot could be attached to this inode by now, since
  775. * we had dropped the ilock.
  776. */
  777. if (xfs_this_quota_on(mp, type)) {
  778. struct xfs_dquot *dqp1;
  779. dqp1 = xfs_inode_dquot(ip, type);
  780. if (dqp1) {
  781. xfs_qm_dqdestroy(dqp);
  782. dqp = dqp1;
  783. xfs_dqlock(dqp);
  784. goto dqret;
  785. }
  786. } else {
  787. /* inode stays locked on return */
  788. xfs_qm_dqdestroy(dqp);
  789. return XFS_ERROR(ESRCH);
  790. }
  791. }
  792. mutex_lock(&qi->qi_tree_lock);
  793. error = -radix_tree_insert(tree, id, dqp);
  794. if (unlikely(error)) {
  795. WARN_ON(error != EEXIST);
  796. /*
  797. * Duplicate found. Just throw away the new dquot and start
  798. * over.
  799. */
  800. mutex_unlock(&qi->qi_tree_lock);
  801. trace_xfs_dqget_dup(dqp);
  802. xfs_qm_dqdestroy(dqp);
  803. XFS_STATS_INC(xs_qm_dquot_dups);
  804. goto restart;
  805. }
  806. /*
  807. * We return a locked dquot to the caller, with a reference taken
  808. */
  809. xfs_dqlock(dqp);
  810. dqp->q_nrefs = 1;
  811. qi->qi_dquots++;
  812. mutex_unlock(&qi->qi_tree_lock);
  813. dqret:
  814. ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
  815. trace_xfs_dqget_miss(dqp);
  816. *O_dqpp = dqp;
  817. return (0);
  818. }
  819. STATIC void
  820. xfs_qm_dqput_final(
  821. struct xfs_dquot *dqp)
  822. {
  823. struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
  824. struct xfs_dquot *gdqp;
  825. struct xfs_dquot *pdqp;
  826. trace_xfs_dqput_free(dqp);
  827. mutex_lock(&qi->qi_lru_lock);
  828. if (list_empty(&dqp->q_lru)) {
  829. list_add_tail(&dqp->q_lru, &qi->qi_lru_list);
  830. qi->qi_lru_count++;
  831. XFS_STATS_INC(xs_qm_dquot_unused);
  832. }
  833. mutex_unlock(&qi->qi_lru_lock);
  834. /*
  835. * If we just added a udquot to the freelist, then we want to release
  836. * the gdquot/pdquot reference that it (probably) has. Otherwise it'll
  837. * keep the gdquot/pdquot from getting reclaimed.
  838. */
  839. gdqp = dqp->q_gdquot;
  840. if (gdqp) {
  841. xfs_dqlock(gdqp);
  842. dqp->q_gdquot = NULL;
  843. }
  844. pdqp = dqp->q_pdquot;
  845. if (pdqp) {
  846. xfs_dqlock(pdqp);
  847. dqp->q_pdquot = NULL;
  848. }
  849. xfs_dqunlock(dqp);
  850. /*
  851. * If we had a group/project quota hint, release it now.
  852. */
  853. if (gdqp)
  854. xfs_qm_dqput(gdqp);
  855. if (pdqp)
  856. xfs_qm_dqput(pdqp);
  857. }
  858. /*
  859. * Release a reference to the dquot (decrement ref-count) and unlock it.
  860. *
  861. * If there is a group quota attached to this dquot, carefully release that
  862. * too without tripping over deadlocks'n'stuff.
  863. */
  864. void
  865. xfs_qm_dqput(
  866. struct xfs_dquot *dqp)
  867. {
  868. ASSERT(dqp->q_nrefs > 0);
  869. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  870. trace_xfs_dqput(dqp);
  871. if (--dqp->q_nrefs > 0)
  872. xfs_dqunlock(dqp);
  873. else
  874. xfs_qm_dqput_final(dqp);
  875. }
  876. /*
  877. * Release a dquot. Flush it if dirty, then dqput() it.
  878. * dquot must not be locked.
  879. */
  880. void
  881. xfs_qm_dqrele(
  882. xfs_dquot_t *dqp)
  883. {
  884. if (!dqp)
  885. return;
  886. trace_xfs_dqrele(dqp);
  887. xfs_dqlock(dqp);
  888. /*
  889. * We don't care to flush it if the dquot is dirty here.
  890. * That will create stutters that we want to avoid.
  891. * Instead we do a delayed write when we try to reclaim
  892. * a dirty dquot. Also xfs_sync will take part of the burden...
  893. */
  894. xfs_qm_dqput(dqp);
  895. }
  896. /*
  897. * This is the dquot flushing I/O completion routine. It is called
  898. * from interrupt level when the buffer containing the dquot is
  899. * flushed to disk. It is responsible for removing the dquot logitem
  900. * from the AIL if it has not been re-logged, and unlocking the dquot's
  901. * flush lock. This behavior is very similar to that of inodes..
  902. */
  903. STATIC void
  904. xfs_qm_dqflush_done(
  905. struct xfs_buf *bp,
  906. struct xfs_log_item *lip)
  907. {
  908. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  909. xfs_dquot_t *dqp = qip->qli_dquot;
  910. struct xfs_ail *ailp = lip->li_ailp;
  911. /*
  912. * We only want to pull the item from the AIL if its
  913. * location in the log has not changed since we started the flush.
  914. * Thus, we only bother if the dquot's lsn has
  915. * not changed. First we check the lsn outside the lock
  916. * since it's cheaper, and then we recheck while
  917. * holding the lock before removing the dquot from the AIL.
  918. */
  919. if ((lip->li_flags & XFS_LI_IN_AIL) &&
  920. lip->li_lsn == qip->qli_flush_lsn) {
  921. /* xfs_trans_ail_delete() drops the AIL lock. */
  922. spin_lock(&ailp->xa_lock);
  923. if (lip->li_lsn == qip->qli_flush_lsn)
  924. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  925. else
  926. spin_unlock(&ailp->xa_lock);
  927. }
  928. /*
  929. * Release the dq's flush lock since we're done with it.
  930. */
  931. xfs_dqfunlock(dqp);
  932. }
  933. /*
  934. * Write a modified dquot to disk.
  935. * The dquot must be locked and the flush lock too taken by caller.
  936. * The flush lock will not be unlocked until the dquot reaches the disk,
  937. * but the dquot is free to be unlocked and modified by the caller
  938. * in the interim. Dquot is still locked on return. This behavior is
  939. * identical to that of inodes.
  940. */
  941. int
  942. xfs_qm_dqflush(
  943. struct xfs_dquot *dqp,
  944. struct xfs_buf **bpp)
  945. {
  946. struct xfs_mount *mp = dqp->q_mount;
  947. struct xfs_buf *bp;
  948. struct xfs_disk_dquot *ddqp;
  949. int error;
  950. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  951. ASSERT(!completion_done(&dqp->q_flush));
  952. trace_xfs_dqflush(dqp);
  953. *bpp = NULL;
  954. xfs_qm_dqunpin_wait(dqp);
  955. /*
  956. * This may have been unpinned because the filesystem is shutting
  957. * down forcibly. If that's the case we must not write this dquot
  958. * to disk, because the log record didn't make it to disk.
  959. *
  960. * We also have to remove the log item from the AIL in this case,
  961. * as we wait for an emptry AIL as part of the unmount process.
  962. */
  963. if (XFS_FORCED_SHUTDOWN(mp)) {
  964. struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
  965. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  966. spin_lock(&mp->m_ail->xa_lock);
  967. if (lip->li_flags & XFS_LI_IN_AIL)
  968. xfs_trans_ail_delete(mp->m_ail, lip,
  969. SHUTDOWN_CORRUPT_INCORE);
  970. else
  971. spin_unlock(&mp->m_ail->xa_lock);
  972. error = XFS_ERROR(EIO);
  973. goto out_unlock;
  974. }
  975. /*
  976. * Get the buffer containing the on-disk dquot
  977. */
  978. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  979. mp->m_quotainfo->qi_dqchunklen, 0, &bp, NULL);
  980. if (error)
  981. goto out_unlock;
  982. /*
  983. * Calculate the location of the dquot inside the buffer.
  984. */
  985. ddqp = bp->b_addr + dqp->q_bufoffset;
  986. /*
  987. * A simple sanity check in case we got a corrupted dquot..
  988. */
  989. error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
  990. XFS_QMOPT_DOWARN, "dqflush (incore copy)");
  991. if (error) {
  992. xfs_buf_relse(bp);
  993. xfs_dqfunlock(dqp);
  994. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  995. return XFS_ERROR(EIO);
  996. }
  997. /* This is the only portion of data that needs to persist */
  998. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  999. /*
  1000. * Clear the dirty field and remember the flush lsn for later use.
  1001. */
  1002. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  1003. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  1004. &dqp->q_logitem.qli_item.li_lsn);
  1005. /*
  1006. * copy the lsn into the on-disk dquot now while we have the in memory
  1007. * dquot here. This can't be done later in the write verifier as we
  1008. * can't get access to the log item at that point in time.
  1009. *
  1010. * We also calculate the CRC here so that the on-disk dquot in the
  1011. * buffer always has a valid CRC. This ensures there is no possibility
  1012. * of a dquot without an up-to-date CRC getting to disk.
  1013. */
  1014. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  1015. struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddqp;
  1016. dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
  1017. xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
  1018. XFS_DQUOT_CRC_OFF);
  1019. }
  1020. /*
  1021. * Attach an iodone routine so that we can remove this dquot from the
  1022. * AIL and release the flush lock once the dquot is synced to disk.
  1023. */
  1024. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  1025. &dqp->q_logitem.qli_item);
  1026. /*
  1027. * If the buffer is pinned then push on the log so we won't
  1028. * get stuck waiting in the write for too long.
  1029. */
  1030. if (xfs_buf_ispinned(bp)) {
  1031. trace_xfs_dqflush_force(dqp);
  1032. xfs_log_force(mp, 0);
  1033. }
  1034. trace_xfs_dqflush_done(dqp);
  1035. *bpp = bp;
  1036. return 0;
  1037. out_unlock:
  1038. xfs_dqfunlock(dqp);
  1039. return XFS_ERROR(EIO);
  1040. }
  1041. /*
  1042. * Lock two xfs_dquot structures.
  1043. *
  1044. * To avoid deadlocks we always lock the quota structure with
  1045. * the lowerd id first.
  1046. */
  1047. void
  1048. xfs_dqlock2(
  1049. xfs_dquot_t *d1,
  1050. xfs_dquot_t *d2)
  1051. {
  1052. if (d1 && d2) {
  1053. ASSERT(d1 != d2);
  1054. if (be32_to_cpu(d1->q_core.d_id) >
  1055. be32_to_cpu(d2->q_core.d_id)) {
  1056. mutex_lock(&d2->q_qlock);
  1057. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  1058. } else {
  1059. mutex_lock(&d1->q_qlock);
  1060. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  1061. }
  1062. } else if (d1) {
  1063. mutex_lock(&d1->q_qlock);
  1064. } else if (d2) {
  1065. mutex_lock(&d2->q_qlock);
  1066. }
  1067. }
  1068. int __init
  1069. xfs_qm_init(void)
  1070. {
  1071. xfs_qm_dqzone =
  1072. kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
  1073. if (!xfs_qm_dqzone)
  1074. goto out;
  1075. xfs_qm_dqtrxzone =
  1076. kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
  1077. if (!xfs_qm_dqtrxzone)
  1078. goto out_free_dqzone;
  1079. return 0;
  1080. out_free_dqzone:
  1081. kmem_zone_destroy(xfs_qm_dqzone);
  1082. out:
  1083. return -ENOMEM;
  1084. }
  1085. void
  1086. xfs_qm_exit(void)
  1087. {
  1088. kmem_zone_destroy(xfs_qm_dqtrxzone);
  1089. kmem_zone_destroy(xfs_qm_dqzone);
  1090. }