xfs_dquot.c 31 KB

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