xfs_dquot.c 39 KB

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