xfs_dquot.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_bmap.h"
  17. #include "xfs_bmap_util.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_quota.h"
  20. #include "xfs_error.h"
  21. #include "xfs_trans.h"
  22. #include "xfs_buf_item.h"
  23. #include "xfs_trans_space.h"
  24. #include "xfs_trans_priv.h"
  25. #include "xfs_qm.h"
  26. #include "xfs_cksum.h"
  27. #include "xfs_trace.h"
  28. #include "xfs_log.h"
  29. #include "xfs_bmap_btree.h"
  30. /*
  31. * Lock order:
  32. *
  33. * ip->i_lock
  34. * qi->qi_tree_lock
  35. * dquot->q_qlock (xfs_dqlock() and friends)
  36. * dquot->q_flush (xfs_dqflock() and friends)
  37. * qi->qi_lru_lock
  38. *
  39. * If two dquots need to be locked the order is user before group/project,
  40. * otherwise by the lowest id first, see xfs_dqlock2.
  41. */
  42. struct kmem_zone *xfs_qm_dqtrxzone;
  43. static struct kmem_zone *xfs_qm_dqzone;
  44. static struct lock_class_key xfs_dquot_group_class;
  45. static struct lock_class_key xfs_dquot_project_class;
  46. /*
  47. * This is called to free all the memory associated with a dquot
  48. */
  49. void
  50. xfs_qm_dqdestroy(
  51. xfs_dquot_t *dqp)
  52. {
  53. ASSERT(list_empty(&dqp->q_lru));
  54. kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
  55. mutex_destroy(&dqp->q_qlock);
  56. XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
  57. kmem_zone_free(xfs_qm_dqzone, dqp);
  58. }
  59. /*
  60. * If default limits are in force, push them into the dquot now.
  61. * We overwrite the dquot limits only if they are zero and this
  62. * is not the root dquot.
  63. */
  64. void
  65. xfs_qm_adjust_dqlimits(
  66. struct xfs_mount *mp,
  67. struct xfs_dquot *dq)
  68. {
  69. struct xfs_quotainfo *q = mp->m_quotainfo;
  70. struct xfs_disk_dquot *d = &dq->q_core;
  71. struct xfs_def_quota *defq;
  72. int prealloc = 0;
  73. ASSERT(d->d_id);
  74. defq = xfs_get_defquota(dq, q);
  75. if (defq->bsoftlimit && !d->d_blk_softlimit) {
  76. d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
  77. prealloc = 1;
  78. }
  79. if (defq->bhardlimit && !d->d_blk_hardlimit) {
  80. d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
  81. prealloc = 1;
  82. }
  83. if (defq->isoftlimit && !d->d_ino_softlimit)
  84. d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
  85. if (defq->ihardlimit && !d->d_ino_hardlimit)
  86. d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
  87. if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
  88. d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
  89. if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
  90. d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
  91. if (prealloc)
  92. xfs_dquot_set_prealloc_limits(dq);
  93. }
  94. /*
  95. * Check the limits and timers of a dquot and start or reset timers
  96. * if necessary.
  97. * This gets called even when quota enforcement is OFF, which makes our
  98. * life a little less complicated. (We just don't reject any quota
  99. * reservations in that case, when enforcement is off).
  100. * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
  101. * enforcement's off.
  102. * In contrast, warnings are a little different in that they don't
  103. * 'automatically' get started when limits get exceeded. They do
  104. * get reset to zero, however, when we find the count to be under
  105. * the soft limit (they are only ever set non-zero via userspace).
  106. */
  107. void
  108. xfs_qm_adjust_dqtimers(
  109. xfs_mount_t *mp,
  110. xfs_disk_dquot_t *d)
  111. {
  112. ASSERT(d->d_id);
  113. #ifdef DEBUG
  114. if (d->d_blk_hardlimit)
  115. ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
  116. be64_to_cpu(d->d_blk_hardlimit));
  117. if (d->d_ino_hardlimit)
  118. ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
  119. be64_to_cpu(d->d_ino_hardlimit));
  120. if (d->d_rtb_hardlimit)
  121. ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
  122. be64_to_cpu(d->d_rtb_hardlimit));
  123. #endif
  124. if (!d->d_btimer) {
  125. if ((d->d_blk_softlimit &&
  126. (be64_to_cpu(d->d_bcount) >
  127. be64_to_cpu(d->d_blk_softlimit))) ||
  128. (d->d_blk_hardlimit &&
  129. (be64_to_cpu(d->d_bcount) >
  130. be64_to_cpu(d->d_blk_hardlimit)))) {
  131. d->d_btimer = cpu_to_be32(get_seconds() +
  132. mp->m_quotainfo->qi_btimelimit);
  133. } else {
  134. d->d_bwarns = 0;
  135. }
  136. } else {
  137. if ((!d->d_blk_softlimit ||
  138. (be64_to_cpu(d->d_bcount) <=
  139. be64_to_cpu(d->d_blk_softlimit))) &&
  140. (!d->d_blk_hardlimit ||
  141. (be64_to_cpu(d->d_bcount) <=
  142. be64_to_cpu(d->d_blk_hardlimit)))) {
  143. d->d_btimer = 0;
  144. }
  145. }
  146. if (!d->d_itimer) {
  147. if ((d->d_ino_softlimit &&
  148. (be64_to_cpu(d->d_icount) >
  149. be64_to_cpu(d->d_ino_softlimit))) ||
  150. (d->d_ino_hardlimit &&
  151. (be64_to_cpu(d->d_icount) >
  152. be64_to_cpu(d->d_ino_hardlimit)))) {
  153. d->d_itimer = cpu_to_be32(get_seconds() +
  154. mp->m_quotainfo->qi_itimelimit);
  155. } else {
  156. d->d_iwarns = 0;
  157. }
  158. } else {
  159. if ((!d->d_ino_softlimit ||
  160. (be64_to_cpu(d->d_icount) <=
  161. be64_to_cpu(d->d_ino_softlimit))) &&
  162. (!d->d_ino_hardlimit ||
  163. (be64_to_cpu(d->d_icount) <=
  164. be64_to_cpu(d->d_ino_hardlimit)))) {
  165. d->d_itimer = 0;
  166. }
  167. }
  168. if (!d->d_rtbtimer) {
  169. if ((d->d_rtb_softlimit &&
  170. (be64_to_cpu(d->d_rtbcount) >
  171. be64_to_cpu(d->d_rtb_softlimit))) ||
  172. (d->d_rtb_hardlimit &&
  173. (be64_to_cpu(d->d_rtbcount) >
  174. be64_to_cpu(d->d_rtb_hardlimit)))) {
  175. d->d_rtbtimer = cpu_to_be32(get_seconds() +
  176. mp->m_quotainfo->qi_rtbtimelimit);
  177. } else {
  178. d->d_rtbwarns = 0;
  179. }
  180. } else {
  181. if ((!d->d_rtb_softlimit ||
  182. (be64_to_cpu(d->d_rtbcount) <=
  183. be64_to_cpu(d->d_rtb_softlimit))) &&
  184. (!d->d_rtb_hardlimit ||
  185. (be64_to_cpu(d->d_rtbcount) <=
  186. be64_to_cpu(d->d_rtb_hardlimit)))) {
  187. d->d_rtbtimer = 0;
  188. }
  189. }
  190. }
  191. /*
  192. * initialize a buffer full of dquots and log the whole thing
  193. */
  194. STATIC void
  195. xfs_qm_init_dquot_blk(
  196. xfs_trans_t *tp,
  197. xfs_mount_t *mp,
  198. xfs_dqid_t id,
  199. uint type,
  200. xfs_buf_t *bp)
  201. {
  202. struct xfs_quotainfo *q = mp->m_quotainfo;
  203. xfs_dqblk_t *d;
  204. xfs_dqid_t curid;
  205. int i;
  206. ASSERT(tp);
  207. ASSERT(xfs_buf_islocked(bp));
  208. d = bp->b_addr;
  209. /*
  210. * ID of the first dquot in the block - id's are zero based.
  211. */
  212. curid = id - (id % q->qi_dqperchunk);
  213. memset(d, 0, BBTOB(q->qi_dqchunklen));
  214. for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
  215. d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  216. d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  217. d->dd_diskdq.d_id = cpu_to_be32(curid);
  218. d->dd_diskdq.d_flags = type;
  219. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  220. uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
  221. xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
  222. XFS_DQUOT_CRC_OFF);
  223. }
  224. }
  225. xfs_trans_dquot_buf(tp, bp,
  226. (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
  227. ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
  228. XFS_BLF_GDQUOT_BUF)));
  229. xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
  230. }
  231. /*
  232. * Initialize the dynamic speculative preallocation thresholds. The lo/hi
  233. * watermarks correspond to the soft and hard limits by default. If a soft limit
  234. * is not specified, we use 95% of the hard limit.
  235. */
  236. void
  237. xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
  238. {
  239. uint64_t space;
  240. dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
  241. dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
  242. if (!dqp->q_prealloc_lo_wmark) {
  243. dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
  244. do_div(dqp->q_prealloc_lo_wmark, 100);
  245. dqp->q_prealloc_lo_wmark *= 95;
  246. }
  247. space = dqp->q_prealloc_hi_wmark;
  248. do_div(space, 100);
  249. dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
  250. dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
  251. dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
  252. }
  253. /*
  254. * Ensure that the given in-core dquot has a buffer on disk backing it, and
  255. * return the buffer. This is called when the bmapi finds a hole.
  256. */
  257. STATIC int
  258. xfs_dquot_disk_alloc(
  259. struct xfs_trans **tpp,
  260. struct xfs_dquot *dqp,
  261. struct xfs_buf **bpp)
  262. {
  263. struct xfs_bmbt_irec map;
  264. struct xfs_defer_ops dfops;
  265. struct xfs_mount *mp = (*tpp)->t_mountp;
  266. struct xfs_buf *bp;
  267. struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
  268. xfs_fsblock_t firstblock;
  269. int nmaps = 1;
  270. int error;
  271. trace_xfs_dqalloc(dqp);
  272. xfs_defer_init(&dfops, &firstblock);
  273. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  274. if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
  275. /*
  276. * Return if this type of quotas is turned off while we didn't
  277. * have an inode lock
  278. */
  279. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  280. return -ESRCH;
  281. }
  282. /* Create the block mapping. */
  283. xfs_trans_ijoin(*tpp, quotip, XFS_ILOCK_EXCL);
  284. error = xfs_bmapi_write(*tpp, quotip, dqp->q_fileoffset,
  285. XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
  286. &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
  287. &map, &nmaps, &dfops);
  288. if (error)
  289. goto error0;
  290. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  291. ASSERT(nmaps == 1);
  292. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  293. (map.br_startblock != HOLESTARTBLOCK));
  294. /*
  295. * Keep track of the blkno to save a lookup later
  296. */
  297. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  298. /* now we can just get the buffer (there's nothing to read yet) */
  299. bp = xfs_trans_get_buf(*tpp, mp->m_ddev_targp, dqp->q_blkno,
  300. mp->m_quotainfo->qi_dqchunklen, 0);
  301. if (!bp) {
  302. error = -ENOMEM;
  303. goto error1;
  304. }
  305. bp->b_ops = &xfs_dquot_buf_ops;
  306. /*
  307. * Make a chunk of dquots out of this buffer and log
  308. * the entire thing.
  309. */
  310. xfs_qm_init_dquot_blk(*tpp, mp, be32_to_cpu(dqp->q_core.d_id),
  311. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  312. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  313. /*
  314. * Hold the buffer and join it to the dfops so that we'll still own
  315. * the buffer when we return to the caller. The buffer disposal on
  316. * error must be paid attention to very carefully, as it has been
  317. * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
  318. * code when allocating a new dquot record" in 2005, and the later
  319. * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
  320. * the buffer locked across the _defer_finish call. We can now do
  321. * this correctly with xfs_defer_bjoin.
  322. *
  323. * Above, we allocated a disk block for the dquot information and
  324. * used get_buf to initialize the dquot. If the _defer_bjoin fails,
  325. * the buffer is still locked to *tpp, so we must _bhold_release and
  326. * then _trans_brelse the buffer. If the _defer_finish fails, the old
  327. * transaction is gone but the new buffer is not joined or held to any
  328. * transaction, so we must _buf_relse it.
  329. *
  330. * If everything succeeds, the caller of this function is returned a
  331. * buffer that is locked and held to the transaction. The caller
  332. * is responsible for unlocking any buffer passed back, either
  333. * manually or by committing the transaction.
  334. */
  335. xfs_trans_bhold(*tpp, bp);
  336. error = xfs_defer_bjoin(&dfops, bp);
  337. if (error) {
  338. xfs_trans_bhold_release(*tpp, bp);
  339. xfs_trans_brelse(*tpp, bp);
  340. goto error1;
  341. }
  342. error = xfs_defer_finish(tpp, &dfops);
  343. if (error) {
  344. xfs_buf_relse(bp);
  345. goto error1;
  346. }
  347. *bpp = bp;
  348. return 0;
  349. error1:
  350. xfs_defer_cancel(&dfops);
  351. error0:
  352. return error;
  353. }
  354. /*
  355. * Read in the in-core dquot's on-disk metadata and return the buffer.
  356. * Returns ENOENT to signal a hole.
  357. */
  358. STATIC int
  359. xfs_dquot_disk_read(
  360. struct xfs_mount *mp,
  361. struct xfs_dquot *dqp,
  362. struct xfs_buf **bpp)
  363. {
  364. struct xfs_bmbt_irec map;
  365. struct xfs_buf *bp;
  366. struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
  367. uint lock_mode;
  368. int nmaps = 1;
  369. int error;
  370. lock_mode = xfs_ilock_data_map_shared(quotip);
  371. if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
  372. /*
  373. * Return if this type of quotas is turned off while we
  374. * didn't have the quota inode lock.
  375. */
  376. xfs_iunlock(quotip, lock_mode);
  377. return -ESRCH;
  378. }
  379. /*
  380. * Find the block map; no allocations yet
  381. */
  382. error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
  383. XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
  384. xfs_iunlock(quotip, lock_mode);
  385. if (error)
  386. return error;
  387. ASSERT(nmaps == 1);
  388. ASSERT(map.br_blockcount >= 1);
  389. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  390. if (map.br_startblock == HOLESTARTBLOCK)
  391. return -ENOENT;
  392. trace_xfs_dqtobp_read(dqp);
  393. /*
  394. * store the blkno etc so that we don't have to do the
  395. * mapping all the time
  396. */
  397. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  398. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  399. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  400. &xfs_dquot_buf_ops);
  401. if (error) {
  402. ASSERT(bp == NULL);
  403. return error;
  404. }
  405. ASSERT(xfs_buf_islocked(bp));
  406. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  407. *bpp = bp;
  408. return 0;
  409. }
  410. /* Allocate and initialize everything we need for an incore dquot. */
  411. STATIC struct xfs_dquot *
  412. xfs_dquot_alloc(
  413. struct xfs_mount *mp,
  414. xfs_dqid_t id,
  415. uint type)
  416. {
  417. struct xfs_dquot *dqp;
  418. dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
  419. dqp->dq_flags = type;
  420. dqp->q_core.d_id = cpu_to_be32(id);
  421. dqp->q_mount = mp;
  422. INIT_LIST_HEAD(&dqp->q_lru);
  423. mutex_init(&dqp->q_qlock);
  424. init_waitqueue_head(&dqp->q_pinwait);
  425. dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
  426. /*
  427. * Offset of dquot in the (fixed sized) dquot chunk.
  428. */
  429. dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
  430. sizeof(xfs_dqblk_t);
  431. /*
  432. * Because we want to use a counting completion, complete
  433. * the flush completion once to allow a single access to
  434. * the flush completion without blocking.
  435. */
  436. init_completion(&dqp->q_flush);
  437. complete(&dqp->q_flush);
  438. /*
  439. * Make sure group quotas have a different lock class than user
  440. * quotas.
  441. */
  442. switch (type) {
  443. case XFS_DQ_USER:
  444. /* uses the default lock class */
  445. break;
  446. case XFS_DQ_GROUP:
  447. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
  448. break;
  449. case XFS_DQ_PROJ:
  450. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
  451. break;
  452. default:
  453. ASSERT(0);
  454. break;
  455. }
  456. xfs_qm_dquot_logitem_init(dqp);
  457. XFS_STATS_INC(mp, xs_qm_dquot);
  458. return dqp;
  459. }
  460. /* Copy the in-core quota fields in from the on-disk buffer. */
  461. STATIC void
  462. xfs_dquot_from_disk(
  463. struct xfs_dquot *dqp,
  464. struct xfs_buf *bp)
  465. {
  466. struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
  467. /* copy everything from disk dquot to the incore dquot */
  468. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  469. /*
  470. * Reservation counters are defined as reservation plus current usage
  471. * to avoid having to add every time.
  472. */
  473. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  474. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  475. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  476. /* initialize the dquot speculative prealloc thresholds */
  477. xfs_dquot_set_prealloc_limits(dqp);
  478. }
  479. /* Allocate and initialize the dquot buffer for this in-core dquot. */
  480. static int
  481. xfs_qm_dqread_alloc(
  482. struct xfs_mount *mp,
  483. struct xfs_dquot *dqp,
  484. struct xfs_buf **bpp)
  485. {
  486. struct xfs_trans *tp;
  487. struct xfs_buf *bp;
  488. int error;
  489. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
  490. XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
  491. if (error)
  492. goto err;
  493. error = xfs_dquot_disk_alloc(&tp, dqp, &bp);
  494. if (error)
  495. goto err_cancel;
  496. error = xfs_trans_commit(tp);
  497. if (error) {
  498. /*
  499. * Buffer was held to the transaction, so we have to unlock it
  500. * manually here because we're not passing it back.
  501. */
  502. xfs_buf_relse(bp);
  503. goto err;
  504. }
  505. *bpp = bp;
  506. return 0;
  507. err_cancel:
  508. xfs_trans_cancel(tp);
  509. err:
  510. return error;
  511. }
  512. /*
  513. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  514. * and release the buffer immediately. If @can_alloc is true, fill any
  515. * holes in the on-disk metadata.
  516. */
  517. static int
  518. xfs_qm_dqread(
  519. struct xfs_mount *mp,
  520. xfs_dqid_t id,
  521. uint type,
  522. bool can_alloc,
  523. struct xfs_dquot **dqpp)
  524. {
  525. struct xfs_dquot *dqp;
  526. struct xfs_buf *bp;
  527. int error;
  528. dqp = xfs_dquot_alloc(mp, id, type);
  529. trace_xfs_dqread(dqp);
  530. /* Try to read the buffer, allocating if necessary. */
  531. error = xfs_dquot_disk_read(mp, dqp, &bp);
  532. if (error == -ENOENT && can_alloc)
  533. error = xfs_qm_dqread_alloc(mp, dqp, &bp);
  534. if (error)
  535. goto err;
  536. /*
  537. * At this point we should have a clean locked buffer. Copy the data
  538. * to the incore dquot and release the buffer since the incore dquot
  539. * has its own locking protocol so we needn't tie up the buffer any
  540. * further.
  541. */
  542. ASSERT(xfs_buf_islocked(bp));
  543. xfs_dquot_from_disk(dqp, bp);
  544. xfs_buf_relse(bp);
  545. *dqpp = dqp;
  546. return error;
  547. err:
  548. trace_xfs_dqread_fail(dqp);
  549. xfs_qm_dqdestroy(dqp);
  550. *dqpp = NULL;
  551. return error;
  552. }
  553. /*
  554. * Advance to the next id in the current chunk, or if at the
  555. * end of the chunk, skip ahead to first id in next allocated chunk
  556. * using the SEEK_DATA interface.
  557. */
  558. static int
  559. xfs_dq_get_next_id(
  560. struct xfs_mount *mp,
  561. uint type,
  562. xfs_dqid_t *id)
  563. {
  564. struct xfs_inode *quotip = xfs_quota_inode(mp, type);
  565. xfs_dqid_t next_id = *id + 1; /* simple advance */
  566. uint lock_flags;
  567. struct xfs_bmbt_irec got;
  568. struct xfs_iext_cursor cur;
  569. xfs_fsblock_t start;
  570. int error = 0;
  571. /* If we'd wrap past the max ID, stop */
  572. if (next_id < *id)
  573. return -ENOENT;
  574. /* If new ID is within the current chunk, advancing it sufficed */
  575. if (next_id % mp->m_quotainfo->qi_dqperchunk) {
  576. *id = next_id;
  577. return 0;
  578. }
  579. /* Nope, next_id is now past the current chunk, so find the next one */
  580. start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
  581. lock_flags = xfs_ilock_data_map_shared(quotip);
  582. if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
  583. error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
  584. if (error)
  585. return error;
  586. }
  587. if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
  588. /* contiguous chunk, bump startoff for the id calculation */
  589. if (got.br_startoff < start)
  590. got.br_startoff = start;
  591. *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
  592. } else {
  593. error = -ENOENT;
  594. }
  595. xfs_iunlock(quotip, lock_flags);
  596. return error;
  597. }
  598. /*
  599. * Look up the dquot in the in-core cache. If found, the dquot is returned
  600. * locked and ready to go.
  601. */
  602. static struct xfs_dquot *
  603. xfs_qm_dqget_cache_lookup(
  604. struct xfs_mount *mp,
  605. struct xfs_quotainfo *qi,
  606. struct radix_tree_root *tree,
  607. xfs_dqid_t id)
  608. {
  609. struct xfs_dquot *dqp;
  610. restart:
  611. mutex_lock(&qi->qi_tree_lock);
  612. dqp = radix_tree_lookup(tree, id);
  613. if (!dqp) {
  614. mutex_unlock(&qi->qi_tree_lock);
  615. XFS_STATS_INC(mp, xs_qm_dqcachemisses);
  616. return NULL;
  617. }
  618. xfs_dqlock(dqp);
  619. if (dqp->dq_flags & XFS_DQ_FREEING) {
  620. xfs_dqunlock(dqp);
  621. mutex_unlock(&qi->qi_tree_lock);
  622. trace_xfs_dqget_freeing(dqp);
  623. delay(1);
  624. goto restart;
  625. }
  626. dqp->q_nrefs++;
  627. mutex_unlock(&qi->qi_tree_lock);
  628. trace_xfs_dqget_hit(dqp);
  629. XFS_STATS_INC(mp, xs_qm_dqcachehits);
  630. return dqp;
  631. }
  632. /*
  633. * Try to insert a new dquot into the in-core cache. If an error occurs the
  634. * caller should throw away the dquot and start over. Otherwise, the dquot
  635. * is returned locked (and held by the cache) as if there had been a cache
  636. * hit.
  637. */
  638. static int
  639. xfs_qm_dqget_cache_insert(
  640. struct xfs_mount *mp,
  641. struct xfs_quotainfo *qi,
  642. struct radix_tree_root *tree,
  643. xfs_dqid_t id,
  644. struct xfs_dquot *dqp)
  645. {
  646. int error;
  647. mutex_lock(&qi->qi_tree_lock);
  648. error = radix_tree_insert(tree, id, dqp);
  649. if (unlikely(error)) {
  650. /* Duplicate found! Caller must try again. */
  651. WARN_ON(error != -EEXIST);
  652. mutex_unlock(&qi->qi_tree_lock);
  653. trace_xfs_dqget_dup(dqp);
  654. return error;
  655. }
  656. /* Return a locked dquot to the caller, with a reference taken. */
  657. xfs_dqlock(dqp);
  658. dqp->q_nrefs = 1;
  659. qi->qi_dquots++;
  660. mutex_unlock(&qi->qi_tree_lock);
  661. return 0;
  662. }
  663. /* Check our input parameters. */
  664. static int
  665. xfs_qm_dqget_checks(
  666. struct xfs_mount *mp,
  667. uint type)
  668. {
  669. if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
  670. return -ESRCH;
  671. switch (type) {
  672. case XFS_DQ_USER:
  673. if (!XFS_IS_UQUOTA_ON(mp))
  674. return -ESRCH;
  675. return 0;
  676. case XFS_DQ_GROUP:
  677. if (!XFS_IS_GQUOTA_ON(mp))
  678. return -ESRCH;
  679. return 0;
  680. case XFS_DQ_PROJ:
  681. if (!XFS_IS_PQUOTA_ON(mp))
  682. return -ESRCH;
  683. return 0;
  684. default:
  685. WARN_ON_ONCE(0);
  686. return -EINVAL;
  687. }
  688. }
  689. /*
  690. * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
  691. * dquot, doing an allocation (if requested) as needed.
  692. */
  693. int
  694. xfs_qm_dqget(
  695. struct xfs_mount *mp,
  696. xfs_dqid_t id,
  697. uint type,
  698. bool can_alloc,
  699. struct xfs_dquot **O_dqpp)
  700. {
  701. struct xfs_quotainfo *qi = mp->m_quotainfo;
  702. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  703. struct xfs_dquot *dqp;
  704. int error;
  705. error = xfs_qm_dqget_checks(mp, type);
  706. if (error)
  707. return error;
  708. restart:
  709. dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
  710. if (dqp) {
  711. *O_dqpp = dqp;
  712. return 0;
  713. }
  714. error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
  715. if (error)
  716. return error;
  717. error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
  718. if (error) {
  719. /*
  720. * Duplicate found. Just throw away the new dquot and start
  721. * over.
  722. */
  723. xfs_qm_dqdestroy(dqp);
  724. XFS_STATS_INC(mp, xs_qm_dquot_dups);
  725. goto restart;
  726. }
  727. trace_xfs_dqget_miss(dqp);
  728. *O_dqpp = dqp;
  729. return 0;
  730. }
  731. /*
  732. * Given a dquot id and type, read and initialize a dquot from the on-disk
  733. * metadata. This function is only for use during quota initialization so
  734. * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
  735. * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
  736. */
  737. int
  738. xfs_qm_dqget_uncached(
  739. struct xfs_mount *mp,
  740. xfs_dqid_t id,
  741. uint type,
  742. struct xfs_dquot **dqpp)
  743. {
  744. int error;
  745. error = xfs_qm_dqget_checks(mp, type);
  746. if (error)
  747. return error;
  748. return xfs_qm_dqread(mp, id, type, 0, dqpp);
  749. }
  750. /* Return the quota id for a given inode and type. */
  751. xfs_dqid_t
  752. xfs_qm_id_for_quotatype(
  753. struct xfs_inode *ip,
  754. uint type)
  755. {
  756. switch (type) {
  757. case XFS_DQ_USER:
  758. return ip->i_d.di_uid;
  759. case XFS_DQ_GROUP:
  760. return ip->i_d.di_gid;
  761. case XFS_DQ_PROJ:
  762. return xfs_get_projid(ip);
  763. }
  764. ASSERT(0);
  765. return 0;
  766. }
  767. /*
  768. * Return the dquot for a given inode and type. If @can_alloc is true, then
  769. * allocate blocks if needed. The inode's ILOCK must be held and it must not
  770. * have already had an inode attached.
  771. */
  772. int
  773. xfs_qm_dqget_inode(
  774. struct xfs_inode *ip,
  775. uint type,
  776. bool can_alloc,
  777. struct xfs_dquot **O_dqpp)
  778. {
  779. struct xfs_mount *mp = ip->i_mount;
  780. struct xfs_quotainfo *qi = mp->m_quotainfo;
  781. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  782. struct xfs_dquot *dqp;
  783. xfs_dqid_t id;
  784. int error;
  785. error = xfs_qm_dqget_checks(mp, type);
  786. if (error)
  787. return error;
  788. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  789. ASSERT(xfs_inode_dquot(ip, type) == NULL);
  790. id = xfs_qm_id_for_quotatype(ip, type);
  791. restart:
  792. dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
  793. if (dqp) {
  794. *O_dqpp = dqp;
  795. return 0;
  796. }
  797. /*
  798. * Dquot cache miss. We don't want to keep the inode lock across
  799. * a (potential) disk read. Also we don't want to deal with the lock
  800. * ordering between quotainode and this inode. OTOH, dropping the inode
  801. * lock here means dealing with a chown that can happen before
  802. * we re-acquire the lock.
  803. */
  804. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  805. error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
  806. xfs_ilock(ip, XFS_ILOCK_EXCL);
  807. if (error)
  808. return error;
  809. /*
  810. * A dquot could be attached to this inode by now, since we had
  811. * dropped the ilock.
  812. */
  813. if (xfs_this_quota_on(mp, type)) {
  814. struct xfs_dquot *dqp1;
  815. dqp1 = xfs_inode_dquot(ip, type);
  816. if (dqp1) {
  817. xfs_qm_dqdestroy(dqp);
  818. dqp = dqp1;
  819. xfs_dqlock(dqp);
  820. goto dqret;
  821. }
  822. } else {
  823. /* inode stays locked on return */
  824. xfs_qm_dqdestroy(dqp);
  825. return -ESRCH;
  826. }
  827. error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
  828. if (error) {
  829. /*
  830. * Duplicate found. Just throw away the new dquot and start
  831. * over.
  832. */
  833. xfs_qm_dqdestroy(dqp);
  834. XFS_STATS_INC(mp, xs_qm_dquot_dups);
  835. goto restart;
  836. }
  837. dqret:
  838. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  839. trace_xfs_dqget_miss(dqp);
  840. *O_dqpp = dqp;
  841. return 0;
  842. }
  843. /*
  844. * Starting at @id and progressing upwards, look for an initialized incore
  845. * dquot, lock it, and return it.
  846. */
  847. int
  848. xfs_qm_dqget_next(
  849. struct xfs_mount *mp,
  850. xfs_dqid_t id,
  851. uint type,
  852. struct xfs_dquot **dqpp)
  853. {
  854. struct xfs_dquot *dqp;
  855. int error = 0;
  856. *dqpp = NULL;
  857. for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
  858. error = xfs_qm_dqget(mp, id, type, false, &dqp);
  859. if (error == -ENOENT)
  860. continue;
  861. else if (error != 0)
  862. break;
  863. if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  864. *dqpp = dqp;
  865. return 0;
  866. }
  867. xfs_qm_dqput(dqp);
  868. }
  869. return error;
  870. }
  871. /*
  872. * Release a reference to the dquot (decrement ref-count) and unlock it.
  873. *
  874. * If there is a group quota attached to this dquot, carefully release that
  875. * too without tripping over deadlocks'n'stuff.
  876. */
  877. void
  878. xfs_qm_dqput(
  879. struct xfs_dquot *dqp)
  880. {
  881. ASSERT(dqp->q_nrefs > 0);
  882. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  883. trace_xfs_dqput(dqp);
  884. if (--dqp->q_nrefs == 0) {
  885. struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
  886. trace_xfs_dqput_free(dqp);
  887. if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
  888. XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
  889. }
  890. xfs_dqunlock(dqp);
  891. }
  892. /*
  893. * Release a dquot. Flush it if dirty, then dqput() it.
  894. * dquot must not be locked.
  895. */
  896. void
  897. xfs_qm_dqrele(
  898. xfs_dquot_t *dqp)
  899. {
  900. if (!dqp)
  901. return;
  902. trace_xfs_dqrele(dqp);
  903. xfs_dqlock(dqp);
  904. /*
  905. * We don't care to flush it if the dquot is dirty here.
  906. * That will create stutters that we want to avoid.
  907. * Instead we do a delayed write when we try to reclaim
  908. * a dirty dquot. Also xfs_sync will take part of the burden...
  909. */
  910. xfs_qm_dqput(dqp);
  911. }
  912. /*
  913. * This is the dquot flushing I/O completion routine. It is called
  914. * from interrupt level when the buffer containing the dquot is
  915. * flushed to disk. It is responsible for removing the dquot logitem
  916. * from the AIL if it has not been re-logged, and unlocking the dquot's
  917. * flush lock. This behavior is very similar to that of inodes..
  918. */
  919. STATIC void
  920. xfs_qm_dqflush_done(
  921. struct xfs_buf *bp,
  922. struct xfs_log_item *lip)
  923. {
  924. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  925. xfs_dquot_t *dqp = qip->qli_dquot;
  926. struct xfs_ail *ailp = lip->li_ailp;
  927. /*
  928. * We only want to pull the item from the AIL if its
  929. * location in the log has not changed since we started the flush.
  930. * Thus, we only bother if the dquot's lsn has
  931. * not changed. First we check the lsn outside the lock
  932. * since it's cheaper, and then we recheck while
  933. * holding the lock before removing the dquot from the AIL.
  934. */
  935. if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
  936. ((lip->li_lsn == qip->qli_flush_lsn) ||
  937. test_bit(XFS_LI_FAILED, &lip->li_flags))) {
  938. /* xfs_trans_ail_delete() drops the AIL lock. */
  939. spin_lock(&ailp->ail_lock);
  940. if (lip->li_lsn == qip->qli_flush_lsn) {
  941. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  942. } else {
  943. /*
  944. * Clear the failed state since we are about to drop the
  945. * flush lock
  946. */
  947. xfs_clear_li_failed(lip);
  948. spin_unlock(&ailp->ail_lock);
  949. }
  950. }
  951. /*
  952. * Release the dq's flush lock since we're done with it.
  953. */
  954. xfs_dqfunlock(dqp);
  955. }
  956. /*
  957. * Write a modified dquot to disk.
  958. * The dquot must be locked and the flush lock too taken by caller.
  959. * The flush lock will not be unlocked until the dquot reaches the disk,
  960. * but the dquot is free to be unlocked and modified by the caller
  961. * in the interim. Dquot is still locked on return. This behavior is
  962. * identical to that of inodes.
  963. */
  964. int
  965. xfs_qm_dqflush(
  966. struct xfs_dquot *dqp,
  967. struct xfs_buf **bpp)
  968. {
  969. struct xfs_mount *mp = dqp->q_mount;
  970. struct xfs_buf *bp;
  971. struct xfs_dqblk *dqb;
  972. struct xfs_disk_dquot *ddqp;
  973. xfs_failaddr_t fa;
  974. int error;
  975. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  976. ASSERT(!completion_done(&dqp->q_flush));
  977. trace_xfs_dqflush(dqp);
  978. *bpp = NULL;
  979. xfs_qm_dqunpin_wait(dqp);
  980. /*
  981. * This may have been unpinned because the filesystem is shutting
  982. * down forcibly. If that's the case we must not write this dquot
  983. * to disk, because the log record didn't make it to disk.
  984. *
  985. * We also have to remove the log item from the AIL in this case,
  986. * as we wait for an emptry AIL as part of the unmount process.
  987. */
  988. if (XFS_FORCED_SHUTDOWN(mp)) {
  989. struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
  990. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  991. xfs_trans_ail_remove(lip, SHUTDOWN_CORRUPT_INCORE);
  992. error = -EIO;
  993. goto out_unlock;
  994. }
  995. /*
  996. * Get the buffer containing the on-disk dquot
  997. */
  998. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  999. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  1000. &xfs_dquot_buf_ops);
  1001. if (error)
  1002. goto out_unlock;
  1003. /*
  1004. * Calculate the location of the dquot inside the buffer.
  1005. */
  1006. dqb = bp->b_addr + dqp->q_bufoffset;
  1007. ddqp = &dqb->dd_diskdq;
  1008. /*
  1009. * A simple sanity check in case we got a corrupted dquot.
  1010. */
  1011. fa = xfs_dqblk_verify(mp, dqb, be32_to_cpu(ddqp->d_id), 0);
  1012. if (fa) {
  1013. xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
  1014. be32_to_cpu(ddqp->d_id), fa);
  1015. xfs_buf_relse(bp);
  1016. xfs_dqfunlock(dqp);
  1017. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1018. return -EIO;
  1019. }
  1020. /* This is the only portion of data that needs to persist */
  1021. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  1022. /*
  1023. * Clear the dirty field and remember the flush lsn for later use.
  1024. */
  1025. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  1026. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  1027. &dqp->q_logitem.qli_item.li_lsn);
  1028. /*
  1029. * copy the lsn into the on-disk dquot now while we have the in memory
  1030. * dquot here. This can't be done later in the write verifier as we
  1031. * can't get access to the log item at that point in time.
  1032. *
  1033. * We also calculate the CRC here so that the on-disk dquot in the
  1034. * buffer always has a valid CRC. This ensures there is no possibility
  1035. * of a dquot without an up-to-date CRC getting to disk.
  1036. */
  1037. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  1038. dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
  1039. xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
  1040. XFS_DQUOT_CRC_OFF);
  1041. }
  1042. /*
  1043. * Attach an iodone routine so that we can remove this dquot from the
  1044. * AIL and release the flush lock once the dquot is synced to disk.
  1045. */
  1046. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  1047. &dqp->q_logitem.qli_item);
  1048. /*
  1049. * If the buffer is pinned then push on the log so we won't
  1050. * get stuck waiting in the write for too long.
  1051. */
  1052. if (xfs_buf_ispinned(bp)) {
  1053. trace_xfs_dqflush_force(dqp);
  1054. xfs_log_force(mp, 0);
  1055. }
  1056. trace_xfs_dqflush_done(dqp);
  1057. *bpp = bp;
  1058. return 0;
  1059. out_unlock:
  1060. xfs_dqfunlock(dqp);
  1061. return -EIO;
  1062. }
  1063. /*
  1064. * Lock two xfs_dquot structures.
  1065. *
  1066. * To avoid deadlocks we always lock the quota structure with
  1067. * the lowerd id first.
  1068. */
  1069. void
  1070. xfs_dqlock2(
  1071. xfs_dquot_t *d1,
  1072. xfs_dquot_t *d2)
  1073. {
  1074. if (d1 && d2) {
  1075. ASSERT(d1 != d2);
  1076. if (be32_to_cpu(d1->q_core.d_id) >
  1077. be32_to_cpu(d2->q_core.d_id)) {
  1078. mutex_lock(&d2->q_qlock);
  1079. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  1080. } else {
  1081. mutex_lock(&d1->q_qlock);
  1082. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  1083. }
  1084. } else if (d1) {
  1085. mutex_lock(&d1->q_qlock);
  1086. } else if (d2) {
  1087. mutex_lock(&d2->q_qlock);
  1088. }
  1089. }
  1090. int __init
  1091. xfs_qm_init(void)
  1092. {
  1093. xfs_qm_dqzone =
  1094. kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
  1095. if (!xfs_qm_dqzone)
  1096. goto out;
  1097. xfs_qm_dqtrxzone =
  1098. kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
  1099. if (!xfs_qm_dqtrxzone)
  1100. goto out_free_dqzone;
  1101. return 0;
  1102. out_free_dqzone:
  1103. kmem_zone_destroy(xfs_qm_dqzone);
  1104. out:
  1105. return -ENOMEM;
  1106. }
  1107. void
  1108. xfs_qm_exit(void)
  1109. {
  1110. kmem_zone_destroy(xfs_qm_dqtrxzone);
  1111. kmem_zone_destroy(xfs_qm_dqzone);
  1112. }
  1113. /*
  1114. * Iterate every dquot of a particular type. The caller must ensure that the
  1115. * particular quota type is active. iter_fn can return negative error codes,
  1116. * or XFS_BTREE_QUERY_RANGE_ABORT to indicate that it wants to stop iterating.
  1117. */
  1118. int
  1119. xfs_qm_dqiterate(
  1120. struct xfs_mount *mp,
  1121. uint dqtype,
  1122. xfs_qm_dqiterate_fn iter_fn,
  1123. void *priv)
  1124. {
  1125. struct xfs_dquot *dq;
  1126. xfs_dqid_t id = 0;
  1127. int error;
  1128. do {
  1129. error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
  1130. if (error == -ENOENT)
  1131. return 0;
  1132. if (error)
  1133. return error;
  1134. error = iter_fn(dq, dqtype, priv);
  1135. id = be32_to_cpu(dq->q_core.d_id);
  1136. xfs_qm_dqput(dq);
  1137. id++;
  1138. } while (error == 0 && id != 0);
  1139. return error;
  1140. }