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