xfs_dquot.c 34 KB

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