xfs_dquot.c 35 KB

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