xfs_dquot.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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_IS_THIS_QUOTA_OFF(dqp)) {
  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_IS_THIS_QUOTA_OFF(dqp)) {
  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;
  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. if (type == XFS_DQ_USER)
  668. ASSERT(ip->i_udquot == NULL);
  669. else
  670. ASSERT(ip->i_gdquot == NULL);
  671. }
  672. #endif
  673. restart:
  674. mutex_lock(&h->qh_lock);
  675. /*
  676. * Look in the cache (hashtable).
  677. * The chain is kept locked during lookup.
  678. */
  679. switch (xfs_qm_dqlookup(mp, id, h, O_dqpp)) {
  680. case -1:
  681. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  682. mutex_unlock(&h->qh_lock);
  683. delay(1);
  684. goto restart;
  685. case 0:
  686. XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
  687. /*
  688. * The dquot was found, moved to the front of the chain,
  689. * taken off the freelist if it was on it, and locked
  690. * at this point. Just unlock the hashchain and return.
  691. */
  692. ASSERT(*O_dqpp);
  693. ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
  694. mutex_unlock(&h->qh_lock);
  695. trace_xfs_dqget_hit(*O_dqpp);
  696. return 0; /* success */
  697. default:
  698. XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
  699. break;
  700. }
  701. /*
  702. * Dquot cache miss. We don't want to keep the inode lock across
  703. * a (potential) disk read. Also we don't want to deal with the lock
  704. * ordering between quotainode and this inode. OTOH, dropping the inode
  705. * lock here means dealing with a chown that can happen before
  706. * we re-acquire the lock.
  707. */
  708. if (ip)
  709. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  710. /*
  711. * Save the hashchain version stamp, and unlock the chain, so that
  712. * we don't keep the lock across a disk read
  713. */
  714. version = h->qh_version;
  715. mutex_unlock(&h->qh_lock);
  716. error = xfs_qm_dqread(mp, id, type, flags, &dqp);
  717. if (ip)
  718. xfs_ilock(ip, XFS_ILOCK_EXCL);
  719. if (error)
  720. return error;
  721. /*
  722. * Dquot lock comes after hashlock in the lock ordering
  723. */
  724. if (ip) {
  725. /*
  726. * A dquot could be attached to this inode by now, since
  727. * we had dropped the ilock.
  728. */
  729. if (type == XFS_DQ_USER) {
  730. if (!XFS_IS_UQUOTA_ON(mp)) {
  731. /* inode stays locked on return */
  732. xfs_qm_dqdestroy(dqp);
  733. return XFS_ERROR(ESRCH);
  734. }
  735. if (ip->i_udquot) {
  736. xfs_qm_dqdestroy(dqp);
  737. dqp = ip->i_udquot;
  738. xfs_dqlock(dqp);
  739. goto dqret;
  740. }
  741. } else {
  742. if (!XFS_IS_OQUOTA_ON(mp)) {
  743. /* inode stays locked on return */
  744. xfs_qm_dqdestroy(dqp);
  745. return XFS_ERROR(ESRCH);
  746. }
  747. if (ip->i_gdquot) {
  748. xfs_qm_dqdestroy(dqp);
  749. dqp = ip->i_gdquot;
  750. xfs_dqlock(dqp);
  751. goto dqret;
  752. }
  753. }
  754. }
  755. /*
  756. * Hashlock comes after ilock in lock order
  757. */
  758. mutex_lock(&h->qh_lock);
  759. if (version != h->qh_version) {
  760. xfs_dquot_t *tmpdqp;
  761. /*
  762. * Now, see if somebody else put the dquot in the
  763. * hashtable before us. This can happen because we didn't
  764. * keep the hashchain lock. We don't have to worry about
  765. * lock order between the two dquots here since dqp isn't
  766. * on any findable lists yet.
  767. */
  768. switch (xfs_qm_dqlookup(mp, id, h, &tmpdqp)) {
  769. case 0:
  770. case -1:
  771. /*
  772. * Duplicate found, either in cache or on its way out.
  773. * Just throw away the new dquot and start over.
  774. */
  775. if (tmpdqp)
  776. xfs_qm_dqput(tmpdqp);
  777. mutex_unlock(&h->qh_lock);
  778. xfs_qm_dqdestroy(dqp);
  779. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  780. goto restart;
  781. default:
  782. break;
  783. }
  784. }
  785. /*
  786. * Put the dquot at the beginning of the hash-chain and mp's list
  787. * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
  788. */
  789. ASSERT(mutex_is_locked(&h->qh_lock));
  790. dqp->q_hash = h;
  791. list_add(&dqp->q_hashlist, &h->qh_list);
  792. h->qh_version++;
  793. /*
  794. * Attach this dquot to this filesystem's list of all dquots,
  795. * kept inside the mount structure in m_quotainfo field
  796. */
  797. mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
  798. /*
  799. * We return a locked dquot to the caller, with a reference taken
  800. */
  801. xfs_dqlock(dqp);
  802. dqp->q_nrefs = 1;
  803. list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
  804. mp->m_quotainfo->qi_dquots++;
  805. mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
  806. mutex_unlock(&h->qh_lock);
  807. dqret:
  808. ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
  809. trace_xfs_dqget_miss(dqp);
  810. *O_dqpp = dqp;
  811. return (0);
  812. }
  813. /*
  814. * Release a reference to the dquot (decrement ref-count)
  815. * and unlock it. If there is a group quota attached to this
  816. * dquot, carefully release that too without tripping over
  817. * deadlocks'n'stuff.
  818. */
  819. void
  820. xfs_qm_dqput(
  821. struct xfs_dquot *dqp)
  822. {
  823. struct xfs_dquot *gdqp;
  824. ASSERT(dqp->q_nrefs > 0);
  825. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  826. trace_xfs_dqput(dqp);
  827. recurse:
  828. if (--dqp->q_nrefs > 0) {
  829. xfs_dqunlock(dqp);
  830. return;
  831. }
  832. trace_xfs_dqput_free(dqp);
  833. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  834. if (list_empty(&dqp->q_freelist)) {
  835. list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
  836. xfs_Gqm->qm_dqfrlist_cnt++;
  837. }
  838. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  839. /*
  840. * If we just added a udquot to the freelist, then we want to release
  841. * the gdquot reference that it (probably) has. Otherwise it'll keep
  842. * the gdquot from getting reclaimed.
  843. */
  844. gdqp = dqp->q_gdquot;
  845. if (gdqp) {
  846. xfs_dqlock(gdqp);
  847. dqp->q_gdquot = NULL;
  848. }
  849. xfs_dqunlock(dqp);
  850. /*
  851. * If we had a group quota hint, release it now.
  852. */
  853. if (gdqp) {
  854. dqp = gdqp;
  855. goto recurse;
  856. }
  857. }
  858. /*
  859. * Release a dquot. Flush it if dirty, then dqput() it.
  860. * dquot must not be locked.
  861. */
  862. void
  863. xfs_qm_dqrele(
  864. xfs_dquot_t *dqp)
  865. {
  866. if (!dqp)
  867. return;
  868. trace_xfs_dqrele(dqp);
  869. xfs_dqlock(dqp);
  870. /*
  871. * We don't care to flush it if the dquot is dirty here.
  872. * That will create stutters that we want to avoid.
  873. * Instead we do a delayed write when we try to reclaim
  874. * a dirty dquot. Also xfs_sync will take part of the burden...
  875. */
  876. xfs_qm_dqput(dqp);
  877. }
  878. /*
  879. * This is the dquot flushing I/O completion routine. It is called
  880. * from interrupt level when the buffer containing the dquot is
  881. * flushed to disk. It is responsible for removing the dquot logitem
  882. * from the AIL if it has not been re-logged, and unlocking the dquot's
  883. * flush lock. This behavior is very similar to that of inodes..
  884. */
  885. STATIC void
  886. xfs_qm_dqflush_done(
  887. struct xfs_buf *bp,
  888. struct xfs_log_item *lip)
  889. {
  890. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  891. xfs_dquot_t *dqp = qip->qli_dquot;
  892. struct xfs_ail *ailp = lip->li_ailp;
  893. /*
  894. * We only want to pull the item from the AIL if its
  895. * location in the log has not changed since we started the flush.
  896. * Thus, we only bother if the dquot's lsn has
  897. * not changed. First we check the lsn outside the lock
  898. * since it's cheaper, and then we recheck while
  899. * holding the lock before removing the dquot from the AIL.
  900. */
  901. if ((lip->li_flags & XFS_LI_IN_AIL) &&
  902. lip->li_lsn == qip->qli_flush_lsn) {
  903. /* xfs_trans_ail_delete() drops the AIL lock. */
  904. spin_lock(&ailp->xa_lock);
  905. if (lip->li_lsn == qip->qli_flush_lsn)
  906. xfs_trans_ail_delete(ailp, lip);
  907. else
  908. spin_unlock(&ailp->xa_lock);
  909. }
  910. /*
  911. * Release the dq's flush lock since we're done with it.
  912. */
  913. xfs_dqfunlock(dqp);
  914. }
  915. /*
  916. * Write a modified dquot to disk.
  917. * The dquot must be locked and the flush lock too taken by caller.
  918. * The flush lock will not be unlocked until the dquot reaches the disk,
  919. * but the dquot is free to be unlocked and modified by the caller
  920. * in the interim. Dquot is still locked on return. This behavior is
  921. * identical to that of inodes.
  922. */
  923. int
  924. xfs_qm_dqflush(
  925. xfs_dquot_t *dqp,
  926. uint flags)
  927. {
  928. struct xfs_mount *mp = dqp->q_mount;
  929. struct xfs_buf *bp;
  930. struct xfs_disk_dquot *ddqp;
  931. int error;
  932. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  933. ASSERT(!completion_done(&dqp->q_flush));
  934. trace_xfs_dqflush(dqp);
  935. /*
  936. * If not dirty, or it's pinned and we are not supposed to block, nada.
  937. */
  938. if (!XFS_DQ_IS_DIRTY(dqp) ||
  939. ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
  940. xfs_dqfunlock(dqp);
  941. return 0;
  942. }
  943. xfs_qm_dqunpin_wait(dqp);
  944. /*
  945. * This may have been unpinned because the filesystem is shutting
  946. * down forcibly. If that's the case we must not write this dquot
  947. * to disk, because the log record didn't make it to disk!
  948. */
  949. if (XFS_FORCED_SHUTDOWN(mp)) {
  950. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  951. xfs_dqfunlock(dqp);
  952. return XFS_ERROR(EIO);
  953. }
  954. /*
  955. * Get the buffer containing the on-disk dquot
  956. */
  957. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  958. mp->m_quotainfo->qi_dqchunklen, 0, &bp);
  959. if (error) {
  960. ASSERT(error != ENOENT);
  961. xfs_dqfunlock(dqp);
  962. return error;
  963. }
  964. /*
  965. * Calculate the location of the dquot inside the buffer.
  966. */
  967. ddqp = bp->b_addr + dqp->q_bufoffset;
  968. /*
  969. * A simple sanity check in case we got a corrupted dquot..
  970. */
  971. error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
  972. XFS_QMOPT_DOWARN, "dqflush (incore copy)");
  973. if (error) {
  974. xfs_buf_relse(bp);
  975. xfs_dqfunlock(dqp);
  976. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  977. return XFS_ERROR(EIO);
  978. }
  979. /* This is the only portion of data that needs to persist */
  980. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  981. /*
  982. * Clear the dirty field and remember the flush lsn for later use.
  983. */
  984. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  985. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  986. &dqp->q_logitem.qli_item.li_lsn);
  987. /*
  988. * Attach an iodone routine so that we can remove this dquot from the
  989. * AIL and release the flush lock once the dquot is synced to disk.
  990. */
  991. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  992. &dqp->q_logitem.qli_item);
  993. /*
  994. * If the buffer is pinned then push on the log so we won't
  995. * get stuck waiting in the write for too long.
  996. */
  997. if (xfs_buf_ispinned(bp)) {
  998. trace_xfs_dqflush_force(dqp);
  999. xfs_log_force(mp, 0);
  1000. }
  1001. if (flags & SYNC_WAIT)
  1002. error = xfs_bwrite(bp);
  1003. else
  1004. xfs_buf_delwri_queue(bp);
  1005. xfs_buf_relse(bp);
  1006. trace_xfs_dqflush_done(dqp);
  1007. /*
  1008. * dqp is still locked, but caller is free to unlock it now.
  1009. */
  1010. return error;
  1011. }
  1012. void
  1013. xfs_dqunlock(
  1014. xfs_dquot_t *dqp)
  1015. {
  1016. xfs_dqunlock_nonotify(dqp);
  1017. if (dqp->q_logitem.qli_dquot == dqp) {
  1018. xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
  1019. &dqp->q_logitem.qli_item);
  1020. }
  1021. }
  1022. /*
  1023. * Lock two xfs_dquot structures.
  1024. *
  1025. * To avoid deadlocks we always lock the quota structure with
  1026. * the lowerd id first.
  1027. */
  1028. void
  1029. xfs_dqlock2(
  1030. xfs_dquot_t *d1,
  1031. xfs_dquot_t *d2)
  1032. {
  1033. if (d1 && d2) {
  1034. ASSERT(d1 != d2);
  1035. if (be32_to_cpu(d1->q_core.d_id) >
  1036. be32_to_cpu(d2->q_core.d_id)) {
  1037. mutex_lock(&d2->q_qlock);
  1038. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  1039. } else {
  1040. mutex_lock(&d1->q_qlock);
  1041. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  1042. }
  1043. } else if (d1) {
  1044. mutex_lock(&d1->q_qlock);
  1045. } else if (d2) {
  1046. mutex_lock(&d2->q_qlock);
  1047. }
  1048. }
  1049. /*
  1050. * Take a dquot out of the mount's dqlist as well as the hashlist. This is
  1051. * called via unmount as well as quotaoff, and the purge will always succeed.
  1052. */
  1053. void
  1054. xfs_qm_dqpurge(
  1055. struct xfs_dquot *dqp)
  1056. {
  1057. struct xfs_mount *mp = dqp->q_mount;
  1058. struct xfs_dqhash *qh = dqp->q_hash;
  1059. xfs_dqlock(dqp);
  1060. /*
  1061. * If we're turning off quotas, we have to make sure that, for
  1062. * example, we don't delete quota disk blocks while dquots are
  1063. * in the process of getting written to those disk blocks.
  1064. * This dquot might well be on AIL, and we can't leave it there
  1065. * if we're turning off quotas. Basically, we need this flush
  1066. * lock, and are willing to block on it.
  1067. */
  1068. if (!xfs_dqflock_nowait(dqp)) {
  1069. /*
  1070. * Block on the flush lock after nudging dquot buffer,
  1071. * if it is incore.
  1072. */
  1073. xfs_dqflock_pushbuf_wait(dqp);
  1074. }
  1075. /*
  1076. * If we are turning this type of quotas off, we don't care
  1077. * about the dirty metadata sitting in this dquot. OTOH, if
  1078. * we're unmounting, we do care, so we flush it and wait.
  1079. */
  1080. if (XFS_DQ_IS_DIRTY(dqp)) {
  1081. int error;
  1082. /*
  1083. * We don't care about getting disk errors here. We need
  1084. * to purge this dquot anyway, so we go ahead regardless.
  1085. */
  1086. error = xfs_qm_dqflush(dqp, SYNC_WAIT);
  1087. if (error)
  1088. xfs_warn(mp, "%s: dquot %p flush failed",
  1089. __func__, dqp);
  1090. xfs_dqflock(dqp);
  1091. }
  1092. ASSERT(atomic_read(&dqp->q_pincount) == 0);
  1093. ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
  1094. !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
  1095. xfs_dqfunlock(dqp);
  1096. xfs_dqunlock(dqp);
  1097. mutex_lock(&qh->qh_lock);
  1098. list_del_init(&dqp->q_hashlist);
  1099. qh->qh_version++;
  1100. mutex_unlock(&qh->qh_lock);
  1101. mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
  1102. list_del_init(&dqp->q_mplist);
  1103. mp->m_quotainfo->qi_dqreclaims++;
  1104. mp->m_quotainfo->qi_dquots--;
  1105. mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
  1106. /*
  1107. * We move dquots to the freelist as soon as their reference count
  1108. * hits zero, so it really should be on the freelist here.
  1109. */
  1110. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  1111. ASSERT(!list_empty(&dqp->q_freelist));
  1112. list_del_init(&dqp->q_freelist);
  1113. xfs_Gqm->qm_dqfrlist_cnt--;
  1114. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  1115. xfs_qm_dqdestroy(dqp);
  1116. }
  1117. /*
  1118. * Give the buffer a little push if it is incore and
  1119. * wait on the flush lock.
  1120. */
  1121. void
  1122. xfs_dqflock_pushbuf_wait(
  1123. xfs_dquot_t *dqp)
  1124. {
  1125. xfs_mount_t *mp = dqp->q_mount;
  1126. xfs_buf_t *bp;
  1127. /*
  1128. * Check to see if the dquot has been flushed delayed
  1129. * write. If so, grab its buffer and send it
  1130. * out immediately. We'll be able to acquire
  1131. * the flush lock when the I/O completes.
  1132. */
  1133. bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
  1134. mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
  1135. if (!bp)
  1136. goto out_lock;
  1137. if (XFS_BUF_ISDELAYWRITE(bp)) {
  1138. if (xfs_buf_ispinned(bp))
  1139. xfs_log_force(mp, 0);
  1140. xfs_buf_delwri_promote(bp);
  1141. wake_up_process(bp->b_target->bt_task);
  1142. }
  1143. xfs_buf_relse(bp);
  1144. out_lock:
  1145. xfs_dqflock(dqp);
  1146. }