xfs_dquot.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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 locked and held. This is called when the bmapi finds a
  256. * hole.
  257. */
  258. STATIC int
  259. xfs_dquot_disk_alloc(
  260. struct xfs_trans **tpp,
  261. struct xfs_dquot *dqp,
  262. struct xfs_buf **bpp)
  263. {
  264. struct xfs_bmbt_irec map;
  265. struct xfs_trans *tp = *tpp;
  266. struct xfs_mount *mp = tp->t_mountp;
  267. struct xfs_buf *bp;
  268. struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
  269. int nmaps = 1;
  270. int error;
  271. trace_xfs_dqalloc(dqp);
  272. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  273. if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
  274. /*
  275. * Return if this type of quotas is turned off while we didn't
  276. * have an inode lock
  277. */
  278. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  279. return -ESRCH;
  280. }
  281. /* Create the block mapping. */
  282. xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
  283. error = xfs_bmapi_write(tp, quotip, dqp->q_fileoffset,
  284. XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
  285. XFS_QM_DQALLOC_SPACE_RES(mp), &map, &nmaps);
  286. if (error)
  287. return error;
  288. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  289. ASSERT(nmaps == 1);
  290. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  291. (map.br_startblock != HOLESTARTBLOCK));
  292. /*
  293. * Keep track of the blkno to save a lookup later
  294. */
  295. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  296. /* now we can just get the buffer (there's nothing to read yet) */
  297. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, dqp->q_blkno,
  298. mp->m_quotainfo->qi_dqchunklen, 0);
  299. if (!bp)
  300. return -ENOMEM;
  301. bp->b_ops = &xfs_dquot_buf_ops;
  302. /*
  303. * Make a chunk of dquots out of this buffer and log
  304. * the entire thing.
  305. */
  306. xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
  307. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  308. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  309. /*
  310. * Hold the buffer and join it to the dfops so that we'll still own
  311. * the buffer when we return to the caller. The buffer disposal on
  312. * error must be paid attention to very carefully, as it has been
  313. * broken since commit efa092f3d4c6 "[XFS] Fixes a bug in the quota
  314. * code when allocating a new dquot record" in 2005, and the later
  315. * conversion to xfs_defer_ops in commit 310a75a3c6c747 failed to keep
  316. * the buffer locked across the _defer_finish call. We can now do
  317. * this correctly with xfs_defer_bjoin.
  318. *
  319. * Above, we allocated a disk block for the dquot information and used
  320. * get_buf to initialize the dquot. If the _defer_finish fails, the old
  321. * transaction is gone but the new buffer is not joined or held to any
  322. * transaction, so we must _buf_relse it.
  323. *
  324. * If everything succeeds, the caller of this function is returned a
  325. * buffer that is locked and held to the transaction. The caller
  326. * is responsible for unlocking any buffer passed back, either
  327. * manually or by committing the transaction. On error, the buffer is
  328. * released and not passed back.
  329. */
  330. xfs_trans_bhold(tp, bp);
  331. error = xfs_defer_finish(tpp);
  332. if (error) {
  333. xfs_trans_bhold_release(*tpp, bp);
  334. xfs_trans_brelse(*tpp, bp);
  335. return error;
  336. }
  337. *bpp = bp;
  338. return 0;
  339. }
  340. /*
  341. * Read in the in-core dquot's on-disk metadata and return the buffer.
  342. * Returns ENOENT to signal a hole.
  343. */
  344. STATIC int
  345. xfs_dquot_disk_read(
  346. struct xfs_mount *mp,
  347. struct xfs_dquot *dqp,
  348. struct xfs_buf **bpp)
  349. {
  350. struct xfs_bmbt_irec map;
  351. struct xfs_buf *bp;
  352. struct xfs_inode *quotip = xfs_quota_inode(mp, dqp->dq_flags);
  353. uint lock_mode;
  354. int nmaps = 1;
  355. int error;
  356. lock_mode = xfs_ilock_data_map_shared(quotip);
  357. if (!xfs_this_quota_on(mp, dqp->dq_flags)) {
  358. /*
  359. * Return if this type of quotas is turned off while we
  360. * didn't have the quota inode lock.
  361. */
  362. xfs_iunlock(quotip, lock_mode);
  363. return -ESRCH;
  364. }
  365. /*
  366. * Find the block map; no allocations yet
  367. */
  368. error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
  369. XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
  370. xfs_iunlock(quotip, lock_mode);
  371. if (error)
  372. return error;
  373. ASSERT(nmaps == 1);
  374. ASSERT(map.br_blockcount >= 1);
  375. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  376. if (map.br_startblock == HOLESTARTBLOCK)
  377. return -ENOENT;
  378. trace_xfs_dqtobp_read(dqp);
  379. /*
  380. * store the blkno etc so that we don't have to do the
  381. * mapping all the time
  382. */
  383. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  384. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  385. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  386. &xfs_dquot_buf_ops);
  387. if (error) {
  388. ASSERT(bp == NULL);
  389. return error;
  390. }
  391. ASSERT(xfs_buf_islocked(bp));
  392. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  393. *bpp = bp;
  394. return 0;
  395. }
  396. /* Allocate and initialize everything we need for an incore dquot. */
  397. STATIC struct xfs_dquot *
  398. xfs_dquot_alloc(
  399. struct xfs_mount *mp,
  400. xfs_dqid_t id,
  401. uint type)
  402. {
  403. struct xfs_dquot *dqp;
  404. dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
  405. dqp->dq_flags = type;
  406. dqp->q_core.d_id = cpu_to_be32(id);
  407. dqp->q_mount = mp;
  408. INIT_LIST_HEAD(&dqp->q_lru);
  409. mutex_init(&dqp->q_qlock);
  410. init_waitqueue_head(&dqp->q_pinwait);
  411. dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
  412. /*
  413. * Offset of dquot in the (fixed sized) dquot chunk.
  414. */
  415. dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
  416. sizeof(xfs_dqblk_t);
  417. /*
  418. * Because we want to use a counting completion, complete
  419. * the flush completion once to allow a single access to
  420. * the flush completion without blocking.
  421. */
  422. init_completion(&dqp->q_flush);
  423. complete(&dqp->q_flush);
  424. /*
  425. * Make sure group quotas have a different lock class than user
  426. * quotas.
  427. */
  428. switch (type) {
  429. case XFS_DQ_USER:
  430. /* uses the default lock class */
  431. break;
  432. case XFS_DQ_GROUP:
  433. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
  434. break;
  435. case XFS_DQ_PROJ:
  436. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
  437. break;
  438. default:
  439. ASSERT(0);
  440. break;
  441. }
  442. xfs_qm_dquot_logitem_init(dqp);
  443. XFS_STATS_INC(mp, xs_qm_dquot);
  444. return dqp;
  445. }
  446. /* Copy the in-core quota fields in from the on-disk buffer. */
  447. STATIC void
  448. xfs_dquot_from_disk(
  449. struct xfs_dquot *dqp,
  450. struct xfs_buf *bp)
  451. {
  452. struct xfs_disk_dquot *ddqp = bp->b_addr + dqp->q_bufoffset;
  453. /* copy everything from disk dquot to the incore dquot */
  454. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  455. /*
  456. * Reservation counters are defined as reservation plus current usage
  457. * to avoid having to add every time.
  458. */
  459. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  460. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  461. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  462. /* initialize the dquot speculative prealloc thresholds */
  463. xfs_dquot_set_prealloc_limits(dqp);
  464. }
  465. /* Allocate and initialize the dquot buffer for this in-core dquot. */
  466. static int
  467. xfs_qm_dqread_alloc(
  468. struct xfs_mount *mp,
  469. struct xfs_dquot *dqp,
  470. struct xfs_buf **bpp)
  471. {
  472. struct xfs_trans *tp;
  473. int error;
  474. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
  475. XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
  476. if (error)
  477. goto err;
  478. error = xfs_dquot_disk_alloc(&tp, dqp, bpp);
  479. if (error)
  480. goto err_cancel;
  481. error = xfs_trans_commit(tp);
  482. if (error) {
  483. /*
  484. * Buffer was held to the transaction, so we have to unlock it
  485. * manually here because we're not passing it back.
  486. */
  487. xfs_buf_relse(*bpp);
  488. *bpp = NULL;
  489. goto err;
  490. }
  491. return 0;
  492. err_cancel:
  493. xfs_trans_cancel(tp);
  494. err:
  495. return error;
  496. }
  497. /*
  498. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  499. * and release the buffer immediately. If @can_alloc is true, fill any
  500. * holes in the on-disk metadata.
  501. */
  502. static int
  503. xfs_qm_dqread(
  504. struct xfs_mount *mp,
  505. xfs_dqid_t id,
  506. uint type,
  507. bool can_alloc,
  508. struct xfs_dquot **dqpp)
  509. {
  510. struct xfs_dquot *dqp;
  511. struct xfs_buf *bp;
  512. int error;
  513. dqp = xfs_dquot_alloc(mp, id, type);
  514. trace_xfs_dqread(dqp);
  515. /* Try to read the buffer, allocating if necessary. */
  516. error = xfs_dquot_disk_read(mp, dqp, &bp);
  517. if (error == -ENOENT && can_alloc)
  518. error = xfs_qm_dqread_alloc(mp, dqp, &bp);
  519. if (error)
  520. goto err;
  521. /*
  522. * At this point we should have a clean locked buffer. Copy the data
  523. * to the incore dquot and release the buffer since the incore dquot
  524. * has its own locking protocol so we needn't tie up the buffer any
  525. * further.
  526. */
  527. ASSERT(xfs_buf_islocked(bp));
  528. xfs_dquot_from_disk(dqp, bp);
  529. xfs_buf_relse(bp);
  530. *dqpp = dqp;
  531. return error;
  532. err:
  533. trace_xfs_dqread_fail(dqp);
  534. xfs_qm_dqdestroy(dqp);
  535. *dqpp = NULL;
  536. return error;
  537. }
  538. /*
  539. * Advance to the next id in the current chunk, or if at the
  540. * end of the chunk, skip ahead to first id in next allocated chunk
  541. * using the SEEK_DATA interface.
  542. */
  543. static int
  544. xfs_dq_get_next_id(
  545. struct xfs_mount *mp,
  546. uint type,
  547. xfs_dqid_t *id)
  548. {
  549. struct xfs_inode *quotip = xfs_quota_inode(mp, type);
  550. xfs_dqid_t next_id = *id + 1; /* simple advance */
  551. uint lock_flags;
  552. struct xfs_bmbt_irec got;
  553. struct xfs_iext_cursor cur;
  554. xfs_fsblock_t start;
  555. int error = 0;
  556. /* If we'd wrap past the max ID, stop */
  557. if (next_id < *id)
  558. return -ENOENT;
  559. /* If new ID is within the current chunk, advancing it sufficed */
  560. if (next_id % mp->m_quotainfo->qi_dqperchunk) {
  561. *id = next_id;
  562. return 0;
  563. }
  564. /* Nope, next_id is now past the current chunk, so find the next one */
  565. start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;
  566. lock_flags = xfs_ilock_data_map_shared(quotip);
  567. if (!(quotip->i_df.if_flags & XFS_IFEXTENTS)) {
  568. error = xfs_iread_extents(NULL, quotip, XFS_DATA_FORK);
  569. if (error)
  570. return error;
  571. }
  572. if (xfs_iext_lookup_extent(quotip, &quotip->i_df, start, &cur, &got)) {
  573. /* contiguous chunk, bump startoff for the id calculation */
  574. if (got.br_startoff < start)
  575. got.br_startoff = start;
  576. *id = got.br_startoff * mp->m_quotainfo->qi_dqperchunk;
  577. } else {
  578. error = -ENOENT;
  579. }
  580. xfs_iunlock(quotip, lock_flags);
  581. return error;
  582. }
  583. /*
  584. * Look up the dquot in the in-core cache. If found, the dquot is returned
  585. * locked and ready to go.
  586. */
  587. static struct xfs_dquot *
  588. xfs_qm_dqget_cache_lookup(
  589. struct xfs_mount *mp,
  590. struct xfs_quotainfo *qi,
  591. struct radix_tree_root *tree,
  592. xfs_dqid_t id)
  593. {
  594. struct xfs_dquot *dqp;
  595. restart:
  596. mutex_lock(&qi->qi_tree_lock);
  597. dqp = radix_tree_lookup(tree, id);
  598. if (!dqp) {
  599. mutex_unlock(&qi->qi_tree_lock);
  600. XFS_STATS_INC(mp, xs_qm_dqcachemisses);
  601. return NULL;
  602. }
  603. xfs_dqlock(dqp);
  604. if (dqp->dq_flags & XFS_DQ_FREEING) {
  605. xfs_dqunlock(dqp);
  606. mutex_unlock(&qi->qi_tree_lock);
  607. trace_xfs_dqget_freeing(dqp);
  608. delay(1);
  609. goto restart;
  610. }
  611. dqp->q_nrefs++;
  612. mutex_unlock(&qi->qi_tree_lock);
  613. trace_xfs_dqget_hit(dqp);
  614. XFS_STATS_INC(mp, xs_qm_dqcachehits);
  615. return dqp;
  616. }
  617. /*
  618. * Try to insert a new dquot into the in-core cache. If an error occurs the
  619. * caller should throw away the dquot and start over. Otherwise, the dquot
  620. * is returned locked (and held by the cache) as if there had been a cache
  621. * hit.
  622. */
  623. static int
  624. xfs_qm_dqget_cache_insert(
  625. struct xfs_mount *mp,
  626. struct xfs_quotainfo *qi,
  627. struct radix_tree_root *tree,
  628. xfs_dqid_t id,
  629. struct xfs_dquot *dqp)
  630. {
  631. int error;
  632. mutex_lock(&qi->qi_tree_lock);
  633. error = radix_tree_insert(tree, id, dqp);
  634. if (unlikely(error)) {
  635. /* Duplicate found! Caller must try again. */
  636. WARN_ON(error != -EEXIST);
  637. mutex_unlock(&qi->qi_tree_lock);
  638. trace_xfs_dqget_dup(dqp);
  639. return error;
  640. }
  641. /* Return a locked dquot to the caller, with a reference taken. */
  642. xfs_dqlock(dqp);
  643. dqp->q_nrefs = 1;
  644. qi->qi_dquots++;
  645. mutex_unlock(&qi->qi_tree_lock);
  646. return 0;
  647. }
  648. /* Check our input parameters. */
  649. static int
  650. xfs_qm_dqget_checks(
  651. struct xfs_mount *mp,
  652. uint type)
  653. {
  654. if (WARN_ON_ONCE(!XFS_IS_QUOTA_RUNNING(mp)))
  655. return -ESRCH;
  656. switch (type) {
  657. case XFS_DQ_USER:
  658. if (!XFS_IS_UQUOTA_ON(mp))
  659. return -ESRCH;
  660. return 0;
  661. case XFS_DQ_GROUP:
  662. if (!XFS_IS_GQUOTA_ON(mp))
  663. return -ESRCH;
  664. return 0;
  665. case XFS_DQ_PROJ:
  666. if (!XFS_IS_PQUOTA_ON(mp))
  667. return -ESRCH;
  668. return 0;
  669. default:
  670. WARN_ON_ONCE(0);
  671. return -EINVAL;
  672. }
  673. }
  674. /*
  675. * Given the file system, id, and type (UDQUOT/GDQUOT), return a a locked
  676. * dquot, doing an allocation (if requested) as needed.
  677. */
  678. int
  679. xfs_qm_dqget(
  680. struct xfs_mount *mp,
  681. xfs_dqid_t id,
  682. uint type,
  683. bool can_alloc,
  684. struct xfs_dquot **O_dqpp)
  685. {
  686. struct xfs_quotainfo *qi = mp->m_quotainfo;
  687. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  688. struct xfs_dquot *dqp;
  689. int error;
  690. error = xfs_qm_dqget_checks(mp, type);
  691. if (error)
  692. return error;
  693. restart:
  694. dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
  695. if (dqp) {
  696. *O_dqpp = dqp;
  697. return 0;
  698. }
  699. error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
  700. if (error)
  701. return error;
  702. error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
  703. if (error) {
  704. /*
  705. * Duplicate found. Just throw away the new dquot and start
  706. * over.
  707. */
  708. xfs_qm_dqdestroy(dqp);
  709. XFS_STATS_INC(mp, xs_qm_dquot_dups);
  710. goto restart;
  711. }
  712. trace_xfs_dqget_miss(dqp);
  713. *O_dqpp = dqp;
  714. return 0;
  715. }
  716. /*
  717. * Given a dquot id and type, read and initialize a dquot from the on-disk
  718. * metadata. This function is only for use during quota initialization so
  719. * it ignores the dquot cache assuming that the dquot shrinker isn't set up.
  720. * The caller is responsible for _qm_dqdestroy'ing the returned dquot.
  721. */
  722. int
  723. xfs_qm_dqget_uncached(
  724. struct xfs_mount *mp,
  725. xfs_dqid_t id,
  726. uint type,
  727. struct xfs_dquot **dqpp)
  728. {
  729. int error;
  730. error = xfs_qm_dqget_checks(mp, type);
  731. if (error)
  732. return error;
  733. return xfs_qm_dqread(mp, id, type, 0, dqpp);
  734. }
  735. /* Return the quota id for a given inode and type. */
  736. xfs_dqid_t
  737. xfs_qm_id_for_quotatype(
  738. struct xfs_inode *ip,
  739. uint type)
  740. {
  741. switch (type) {
  742. case XFS_DQ_USER:
  743. return ip->i_d.di_uid;
  744. case XFS_DQ_GROUP:
  745. return ip->i_d.di_gid;
  746. case XFS_DQ_PROJ:
  747. return xfs_get_projid(ip);
  748. }
  749. ASSERT(0);
  750. return 0;
  751. }
  752. /*
  753. * Return the dquot for a given inode and type. If @can_alloc is true, then
  754. * allocate blocks if needed. The inode's ILOCK must be held and it must not
  755. * have already had an inode attached.
  756. */
  757. int
  758. xfs_qm_dqget_inode(
  759. struct xfs_inode *ip,
  760. uint type,
  761. bool can_alloc,
  762. struct xfs_dquot **O_dqpp)
  763. {
  764. struct xfs_mount *mp = ip->i_mount;
  765. struct xfs_quotainfo *qi = mp->m_quotainfo;
  766. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  767. struct xfs_dquot *dqp;
  768. xfs_dqid_t id;
  769. int error;
  770. error = xfs_qm_dqget_checks(mp, type);
  771. if (error)
  772. return error;
  773. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  774. ASSERT(xfs_inode_dquot(ip, type) == NULL);
  775. id = xfs_qm_id_for_quotatype(ip, type);
  776. restart:
  777. dqp = xfs_qm_dqget_cache_lookup(mp, qi, tree, id);
  778. if (dqp) {
  779. *O_dqpp = dqp;
  780. return 0;
  781. }
  782. /*
  783. * Dquot cache miss. We don't want to keep the inode lock across
  784. * a (potential) disk read. Also we don't want to deal with the lock
  785. * ordering between quotainode and this inode. OTOH, dropping the inode
  786. * lock here means dealing with a chown that can happen before
  787. * we re-acquire the lock.
  788. */
  789. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  790. error = xfs_qm_dqread(mp, id, type, can_alloc, &dqp);
  791. xfs_ilock(ip, XFS_ILOCK_EXCL);
  792. if (error)
  793. return error;
  794. /*
  795. * A dquot could be attached to this inode by now, since we had
  796. * dropped the ilock.
  797. */
  798. if (xfs_this_quota_on(mp, type)) {
  799. struct xfs_dquot *dqp1;
  800. dqp1 = xfs_inode_dquot(ip, type);
  801. if (dqp1) {
  802. xfs_qm_dqdestroy(dqp);
  803. dqp = dqp1;
  804. xfs_dqlock(dqp);
  805. goto dqret;
  806. }
  807. } else {
  808. /* inode stays locked on return */
  809. xfs_qm_dqdestroy(dqp);
  810. return -ESRCH;
  811. }
  812. error = xfs_qm_dqget_cache_insert(mp, qi, tree, id, dqp);
  813. if (error) {
  814. /*
  815. * Duplicate found. Just throw away the new dquot and start
  816. * over.
  817. */
  818. xfs_qm_dqdestroy(dqp);
  819. XFS_STATS_INC(mp, xs_qm_dquot_dups);
  820. goto restart;
  821. }
  822. dqret:
  823. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  824. trace_xfs_dqget_miss(dqp);
  825. *O_dqpp = dqp;
  826. return 0;
  827. }
  828. /*
  829. * Starting at @id and progressing upwards, look for an initialized incore
  830. * dquot, lock it, and return it.
  831. */
  832. int
  833. xfs_qm_dqget_next(
  834. struct xfs_mount *mp,
  835. xfs_dqid_t id,
  836. uint type,
  837. struct xfs_dquot **dqpp)
  838. {
  839. struct xfs_dquot *dqp;
  840. int error = 0;
  841. *dqpp = NULL;
  842. for (; !error; error = xfs_dq_get_next_id(mp, type, &id)) {
  843. error = xfs_qm_dqget(mp, id, type, false, &dqp);
  844. if (error == -ENOENT)
  845. continue;
  846. else if (error != 0)
  847. break;
  848. if (!XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  849. *dqpp = dqp;
  850. return 0;
  851. }
  852. xfs_qm_dqput(dqp);
  853. }
  854. return error;
  855. }
  856. /*
  857. * Release a reference to the dquot (decrement ref-count) and unlock it.
  858. *
  859. * If there is a group quota attached to this dquot, carefully release that
  860. * too without tripping over deadlocks'n'stuff.
  861. */
  862. void
  863. xfs_qm_dqput(
  864. struct xfs_dquot *dqp)
  865. {
  866. ASSERT(dqp->q_nrefs > 0);
  867. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  868. trace_xfs_dqput(dqp);
  869. if (--dqp->q_nrefs == 0) {
  870. struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
  871. trace_xfs_dqput_free(dqp);
  872. if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
  873. XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
  874. }
  875. xfs_dqunlock(dqp);
  876. }
  877. /*
  878. * Release a dquot. Flush it if dirty, then dqput() it.
  879. * dquot must not be locked.
  880. */
  881. void
  882. xfs_qm_dqrele(
  883. xfs_dquot_t *dqp)
  884. {
  885. if (!dqp)
  886. return;
  887. trace_xfs_dqrele(dqp);
  888. xfs_dqlock(dqp);
  889. /*
  890. * We don't care to flush it if the dquot is dirty here.
  891. * That will create stutters that we want to avoid.
  892. * Instead we do a delayed write when we try to reclaim
  893. * a dirty dquot. Also xfs_sync will take part of the burden...
  894. */
  895. xfs_qm_dqput(dqp);
  896. }
  897. /*
  898. * This is the dquot flushing I/O completion routine. It is called
  899. * from interrupt level when the buffer containing the dquot is
  900. * flushed to disk. It is responsible for removing the dquot logitem
  901. * from the AIL if it has not been re-logged, and unlocking the dquot's
  902. * flush lock. This behavior is very similar to that of inodes..
  903. */
  904. STATIC void
  905. xfs_qm_dqflush_done(
  906. struct xfs_buf *bp,
  907. struct xfs_log_item *lip)
  908. {
  909. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  910. xfs_dquot_t *dqp = qip->qli_dquot;
  911. struct xfs_ail *ailp = lip->li_ailp;
  912. /*
  913. * We only want to pull the item from the AIL if its
  914. * location in the log has not changed since we started the flush.
  915. * Thus, we only bother if the dquot's lsn has
  916. * not changed. First we check the lsn outside the lock
  917. * since it's cheaper, and then we recheck while
  918. * holding the lock before removing the dquot from the AIL.
  919. */
  920. if (test_bit(XFS_LI_IN_AIL, &lip->li_flags) &&
  921. ((lip->li_lsn == qip->qli_flush_lsn) ||
  922. test_bit(XFS_LI_FAILED, &lip->li_flags))) {
  923. /* xfs_trans_ail_delete() drops the AIL lock. */
  924. spin_lock(&ailp->ail_lock);
  925. if (lip->li_lsn == qip->qli_flush_lsn) {
  926. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  927. } else {
  928. /*
  929. * Clear the failed state since we are about to drop the
  930. * flush lock
  931. */
  932. xfs_clear_li_failed(lip);
  933. spin_unlock(&ailp->ail_lock);
  934. }
  935. }
  936. /*
  937. * Release the dq's flush lock since we're done with it.
  938. */
  939. xfs_dqfunlock(dqp);
  940. }
  941. /*
  942. * Write a modified dquot to disk.
  943. * The dquot must be locked and the flush lock too taken by caller.
  944. * The flush lock will not be unlocked until the dquot reaches the disk,
  945. * but the dquot is free to be unlocked and modified by the caller
  946. * in the interim. Dquot is still locked on return. This behavior is
  947. * identical to that of inodes.
  948. */
  949. int
  950. xfs_qm_dqflush(
  951. struct xfs_dquot *dqp,
  952. struct xfs_buf **bpp)
  953. {
  954. struct xfs_mount *mp = dqp->q_mount;
  955. struct xfs_buf *bp;
  956. struct xfs_dqblk *dqb;
  957. struct xfs_disk_dquot *ddqp;
  958. xfs_failaddr_t fa;
  959. int error;
  960. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  961. ASSERT(!completion_done(&dqp->q_flush));
  962. trace_xfs_dqflush(dqp);
  963. *bpp = NULL;
  964. xfs_qm_dqunpin_wait(dqp);
  965. /*
  966. * This may have been unpinned because the filesystem is shutting
  967. * down forcibly. If that's the case we must not write this dquot
  968. * to disk, because the log record didn't make it to disk.
  969. *
  970. * We also have to remove the log item from the AIL in this case,
  971. * as we wait for an emptry AIL as part of the unmount process.
  972. */
  973. if (XFS_FORCED_SHUTDOWN(mp)) {
  974. struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
  975. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  976. xfs_trans_ail_remove(lip, SHUTDOWN_CORRUPT_INCORE);
  977. error = -EIO;
  978. goto out_unlock;
  979. }
  980. /*
  981. * Get the buffer containing the on-disk dquot
  982. */
  983. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  984. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  985. &xfs_dquot_buf_ops);
  986. if (error)
  987. goto out_unlock;
  988. /*
  989. * Calculate the location of the dquot inside the buffer.
  990. */
  991. dqb = bp->b_addr + dqp->q_bufoffset;
  992. ddqp = &dqb->dd_diskdq;
  993. /* sanity check the in-core structure before we flush */
  994. fa = xfs_dquot_verify(mp, &dqp->q_core, be32_to_cpu(dqp->q_core.d_id),
  995. 0);
  996. if (fa) {
  997. xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
  998. be32_to_cpu(dqp->q_core.d_id), fa);
  999. xfs_buf_relse(bp);
  1000. xfs_dqfunlock(dqp);
  1001. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1002. return -EIO;
  1003. }
  1004. /* This is the only portion of data that needs to persist */
  1005. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  1006. /*
  1007. * Clear the dirty field and remember the flush lsn for later use.
  1008. */
  1009. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  1010. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  1011. &dqp->q_logitem.qli_item.li_lsn);
  1012. /*
  1013. * copy the lsn into the on-disk dquot now while we have the in memory
  1014. * dquot here. This can't be done later in the write verifier as we
  1015. * can't get access to the log item at that point in time.
  1016. *
  1017. * We also calculate the CRC here so that the on-disk dquot in the
  1018. * buffer always has a valid CRC. This ensures there is no possibility
  1019. * of a dquot without an up-to-date CRC getting to disk.
  1020. */
  1021. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  1022. dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
  1023. xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
  1024. XFS_DQUOT_CRC_OFF);
  1025. }
  1026. /*
  1027. * Attach an iodone routine so that we can remove this dquot from the
  1028. * AIL and release the flush lock once the dquot is synced to disk.
  1029. */
  1030. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  1031. &dqp->q_logitem.qli_item);
  1032. /*
  1033. * If the buffer is pinned then push on the log so we won't
  1034. * get stuck waiting in the write for too long.
  1035. */
  1036. if (xfs_buf_ispinned(bp)) {
  1037. trace_xfs_dqflush_force(dqp);
  1038. xfs_log_force(mp, 0);
  1039. }
  1040. trace_xfs_dqflush_done(dqp);
  1041. *bpp = bp;
  1042. return 0;
  1043. out_unlock:
  1044. xfs_dqfunlock(dqp);
  1045. return -EIO;
  1046. }
  1047. /*
  1048. * Lock two xfs_dquot structures.
  1049. *
  1050. * To avoid deadlocks we always lock the quota structure with
  1051. * the lowerd id first.
  1052. */
  1053. void
  1054. xfs_dqlock2(
  1055. xfs_dquot_t *d1,
  1056. xfs_dquot_t *d2)
  1057. {
  1058. if (d1 && d2) {
  1059. ASSERT(d1 != d2);
  1060. if (be32_to_cpu(d1->q_core.d_id) >
  1061. be32_to_cpu(d2->q_core.d_id)) {
  1062. mutex_lock(&d2->q_qlock);
  1063. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  1064. } else {
  1065. mutex_lock(&d1->q_qlock);
  1066. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  1067. }
  1068. } else if (d1) {
  1069. mutex_lock(&d1->q_qlock);
  1070. } else if (d2) {
  1071. mutex_lock(&d2->q_qlock);
  1072. }
  1073. }
  1074. int __init
  1075. xfs_qm_init(void)
  1076. {
  1077. xfs_qm_dqzone =
  1078. kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
  1079. if (!xfs_qm_dqzone)
  1080. goto out;
  1081. xfs_qm_dqtrxzone =
  1082. kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
  1083. if (!xfs_qm_dqtrxzone)
  1084. goto out_free_dqzone;
  1085. return 0;
  1086. out_free_dqzone:
  1087. kmem_zone_destroy(xfs_qm_dqzone);
  1088. out:
  1089. return -ENOMEM;
  1090. }
  1091. void
  1092. xfs_qm_exit(void)
  1093. {
  1094. kmem_zone_destroy(xfs_qm_dqtrxzone);
  1095. kmem_zone_destroy(xfs_qm_dqzone);
  1096. }
  1097. /*
  1098. * Iterate every dquot of a particular type. The caller must ensure that the
  1099. * particular quota type is active. iter_fn can return negative error codes,
  1100. * or XFS_BTREE_QUERY_RANGE_ABORT to indicate that it wants to stop iterating.
  1101. */
  1102. int
  1103. xfs_qm_dqiterate(
  1104. struct xfs_mount *mp,
  1105. uint dqtype,
  1106. xfs_qm_dqiterate_fn iter_fn,
  1107. void *priv)
  1108. {
  1109. struct xfs_dquot *dq;
  1110. xfs_dqid_t id = 0;
  1111. int error;
  1112. do {
  1113. error = xfs_qm_dqget_next(mp, id, dqtype, &dq);
  1114. if (error == -ENOENT)
  1115. return 0;
  1116. if (error)
  1117. return error;
  1118. error = iter_fn(dq, dqtype, priv);
  1119. id = be32_to_cpu(dq->q_core.d_id);
  1120. xfs_qm_dqput(dq);
  1121. id++;
  1122. } while (error == 0 && id != 0);
  1123. return error;
  1124. }