xfs_dquot.c 35 KB

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