xfs_dquot.c 31 KB

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