xfs_dquot.c 31 KB

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