xfs_dquot.c 31 KB

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