xfs_dquot.c 31 KB

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