xfs_dquot.c 32 KB

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