xfs_dquot.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * Copyright (c) 2000-2003 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_quota.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_rtalloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_itable.h"
  35. #include "xfs_attr.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_trans_space.h"
  38. #include "xfs_trans_priv.h"
  39. #include "xfs_qm.h"
  40. #include "xfs_trace.h"
  41. /*
  42. * Lock order:
  43. *
  44. * ip->i_lock
  45. * qh->qh_lock
  46. * qi->qi_dqlist_lock
  47. * dquot->q_qlock (xfs_dqlock() and friends)
  48. * dquot->q_flush (xfs_dqflock() and friends)
  49. * xfs_Gqm->qm_dqfrlist_lock
  50. *
  51. * If two dquots need to be locked the order is user before group/project,
  52. * otherwise by the lowest id first, see xfs_dqlock2.
  53. */
  54. #ifdef DEBUG
  55. xfs_buftarg_t *xfs_dqerror_target;
  56. int xfs_do_dqerror;
  57. int xfs_dqreq_num;
  58. int xfs_dqerror_mod = 33;
  59. #endif
  60. static struct lock_class_key xfs_dquot_other_class;
  61. /*
  62. * Allocate and initialize a dquot. We don't always allocate fresh memory;
  63. * we try to reclaim a free dquot if the number of incore dquots are above
  64. * a threshold.
  65. * The only field inside the core that gets initialized at this point
  66. * is the d_id field. The idea is to fill in the entire q_core
  67. * when we read in the on disk dquot.
  68. */
  69. STATIC xfs_dquot_t *
  70. xfs_qm_dqinit(
  71. xfs_mount_t *mp,
  72. xfs_dqid_t id,
  73. uint type)
  74. {
  75. xfs_dquot_t *dqp;
  76. boolean_t brandnewdquot;
  77. brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
  78. dqp->dq_flags = type;
  79. dqp->q_core.d_id = cpu_to_be32(id);
  80. dqp->q_mount = mp;
  81. /*
  82. * No need to re-initialize these if this is a reclaimed dquot.
  83. */
  84. if (brandnewdquot) {
  85. INIT_LIST_HEAD(&dqp->q_freelist);
  86. mutex_init(&dqp->q_qlock);
  87. init_waitqueue_head(&dqp->q_pinwait);
  88. /*
  89. * Because we want to use a counting completion, complete
  90. * the flush completion once to allow a single access to
  91. * the flush completion without blocking.
  92. */
  93. init_completion(&dqp->q_flush);
  94. complete(&dqp->q_flush);
  95. trace_xfs_dqinit(dqp);
  96. } else {
  97. /*
  98. * Only the q_core portion was zeroed in dqreclaim_one().
  99. * So, we need to reset others.
  100. */
  101. dqp->q_nrefs = 0;
  102. dqp->q_blkno = 0;
  103. INIT_LIST_HEAD(&dqp->q_mplist);
  104. INIT_LIST_HEAD(&dqp->q_hashlist);
  105. dqp->q_bufoffset = 0;
  106. dqp->q_fileoffset = 0;
  107. dqp->q_transp = NULL;
  108. dqp->q_gdquot = NULL;
  109. dqp->q_res_bcount = 0;
  110. dqp->q_res_icount = 0;
  111. dqp->q_res_rtbcount = 0;
  112. atomic_set(&dqp->q_pincount, 0);
  113. dqp->q_hash = NULL;
  114. ASSERT(list_empty(&dqp->q_freelist));
  115. trace_xfs_dqreuse(dqp);
  116. }
  117. /*
  118. * In either case we need to make sure group quotas have a different
  119. * lock class than user quotas, to make sure lockdep knows we can
  120. * locks of one of each at the same time.
  121. */
  122. if (!(type & XFS_DQ_USER))
  123. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
  124. /*
  125. * log item gets initialized later
  126. */
  127. return (dqp);
  128. }
  129. /*
  130. * This is called to free all the memory associated with a dquot
  131. */
  132. void
  133. xfs_qm_dqdestroy(
  134. xfs_dquot_t *dqp)
  135. {
  136. ASSERT(list_empty(&dqp->q_freelist));
  137. mutex_destroy(&dqp->q_qlock);
  138. kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
  139. atomic_dec(&xfs_Gqm->qm_totaldquots);
  140. }
  141. /*
  142. * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
  143. */
  144. STATIC void
  145. xfs_qm_dqinit_core(
  146. xfs_dqid_t id,
  147. uint type,
  148. xfs_dqblk_t *d)
  149. {
  150. /*
  151. * Caller has zero'd the entire dquot 'chunk' already.
  152. */
  153. d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  154. d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  155. d->dd_diskdq.d_id = cpu_to_be32(id);
  156. d->dd_diskdq.d_flags = type;
  157. }
  158. /*
  159. * If default limits are in force, push them into the dquot now.
  160. * We overwrite the dquot limits only if they are zero and this
  161. * is not the root dquot.
  162. */
  163. void
  164. xfs_qm_adjust_dqlimits(
  165. xfs_mount_t *mp,
  166. xfs_disk_dquot_t *d)
  167. {
  168. xfs_quotainfo_t *q = mp->m_quotainfo;
  169. ASSERT(d->d_id);
  170. if (q->qi_bsoftlimit && !d->d_blk_softlimit)
  171. d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
  172. if (q->qi_bhardlimit && !d->d_blk_hardlimit)
  173. d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
  174. if (q->qi_isoftlimit && !d->d_ino_softlimit)
  175. d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
  176. if (q->qi_ihardlimit && !d->d_ino_hardlimit)
  177. d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
  178. if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
  179. d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
  180. if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
  181. d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
  182. }
  183. /*
  184. * Check the limits and timers of a dquot and start or reset timers
  185. * if necessary.
  186. * This gets called even when quota enforcement is OFF, which makes our
  187. * life a little less complicated. (We just don't reject any quota
  188. * reservations in that case, when enforcement is off).
  189. * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
  190. * enforcement's off.
  191. * In contrast, warnings are a little different in that they don't
  192. * 'automatically' get started when limits get exceeded. They do
  193. * get reset to zero, however, when we find the count to be under
  194. * the soft limit (they are only ever set non-zero via userspace).
  195. */
  196. void
  197. xfs_qm_adjust_dqtimers(
  198. xfs_mount_t *mp,
  199. xfs_disk_dquot_t *d)
  200. {
  201. ASSERT(d->d_id);
  202. #ifdef DEBUG
  203. if (d->d_blk_hardlimit)
  204. ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
  205. be64_to_cpu(d->d_blk_hardlimit));
  206. if (d->d_ino_hardlimit)
  207. ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
  208. be64_to_cpu(d->d_ino_hardlimit));
  209. if (d->d_rtb_hardlimit)
  210. ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
  211. be64_to_cpu(d->d_rtb_hardlimit));
  212. #endif
  213. if (!d->d_btimer) {
  214. if ((d->d_blk_softlimit &&
  215. (be64_to_cpu(d->d_bcount) >=
  216. be64_to_cpu(d->d_blk_softlimit))) ||
  217. (d->d_blk_hardlimit &&
  218. (be64_to_cpu(d->d_bcount) >=
  219. be64_to_cpu(d->d_blk_hardlimit)))) {
  220. d->d_btimer = cpu_to_be32(get_seconds() +
  221. mp->m_quotainfo->qi_btimelimit);
  222. } else {
  223. d->d_bwarns = 0;
  224. }
  225. } else {
  226. if ((!d->d_blk_softlimit ||
  227. (be64_to_cpu(d->d_bcount) <
  228. be64_to_cpu(d->d_blk_softlimit))) &&
  229. (!d->d_blk_hardlimit ||
  230. (be64_to_cpu(d->d_bcount) <
  231. be64_to_cpu(d->d_blk_hardlimit)))) {
  232. d->d_btimer = 0;
  233. }
  234. }
  235. if (!d->d_itimer) {
  236. if ((d->d_ino_softlimit &&
  237. (be64_to_cpu(d->d_icount) >=
  238. be64_to_cpu(d->d_ino_softlimit))) ||
  239. (d->d_ino_hardlimit &&
  240. (be64_to_cpu(d->d_icount) >=
  241. be64_to_cpu(d->d_ino_hardlimit)))) {
  242. d->d_itimer = cpu_to_be32(get_seconds() +
  243. mp->m_quotainfo->qi_itimelimit);
  244. } else {
  245. d->d_iwarns = 0;
  246. }
  247. } else {
  248. if ((!d->d_ino_softlimit ||
  249. (be64_to_cpu(d->d_icount) <
  250. be64_to_cpu(d->d_ino_softlimit))) &&
  251. (!d->d_ino_hardlimit ||
  252. (be64_to_cpu(d->d_icount) <
  253. be64_to_cpu(d->d_ino_hardlimit)))) {
  254. d->d_itimer = 0;
  255. }
  256. }
  257. if (!d->d_rtbtimer) {
  258. if ((d->d_rtb_softlimit &&
  259. (be64_to_cpu(d->d_rtbcount) >=
  260. be64_to_cpu(d->d_rtb_softlimit))) ||
  261. (d->d_rtb_hardlimit &&
  262. (be64_to_cpu(d->d_rtbcount) >=
  263. be64_to_cpu(d->d_rtb_hardlimit)))) {
  264. d->d_rtbtimer = cpu_to_be32(get_seconds() +
  265. mp->m_quotainfo->qi_rtbtimelimit);
  266. } else {
  267. d->d_rtbwarns = 0;
  268. }
  269. } else {
  270. if ((!d->d_rtb_softlimit ||
  271. (be64_to_cpu(d->d_rtbcount) <
  272. be64_to_cpu(d->d_rtb_softlimit))) &&
  273. (!d->d_rtb_hardlimit ||
  274. (be64_to_cpu(d->d_rtbcount) <
  275. be64_to_cpu(d->d_rtb_hardlimit)))) {
  276. d->d_rtbtimer = 0;
  277. }
  278. }
  279. }
  280. /*
  281. * initialize a buffer full of dquots and log the whole thing
  282. */
  283. STATIC void
  284. xfs_qm_init_dquot_blk(
  285. xfs_trans_t *tp,
  286. xfs_mount_t *mp,
  287. xfs_dqid_t id,
  288. uint type,
  289. xfs_buf_t *bp)
  290. {
  291. struct xfs_quotainfo *q = mp->m_quotainfo;
  292. xfs_dqblk_t *d;
  293. int curid, i;
  294. ASSERT(tp);
  295. ASSERT(xfs_buf_islocked(bp));
  296. d = bp->b_addr;
  297. /*
  298. * ID of the first dquot in the block - id's are zero based.
  299. */
  300. curid = id - (id % q->qi_dqperchunk);
  301. ASSERT(curid >= 0);
  302. memset(d, 0, BBTOB(q->qi_dqchunklen));
  303. for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++)
  304. xfs_qm_dqinit_core(curid, type, d);
  305. xfs_trans_dquot_buf(tp, bp,
  306. (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
  307. ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
  308. XFS_BLF_GDQUOT_BUF)));
  309. xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
  310. }
  311. /*
  312. * Allocate a block and fill it with dquots.
  313. * This is called when the bmapi finds a hole.
  314. */
  315. STATIC int
  316. xfs_qm_dqalloc(
  317. xfs_trans_t **tpp,
  318. xfs_mount_t *mp,
  319. xfs_dquot_t *dqp,
  320. xfs_inode_t *quotip,
  321. xfs_fileoff_t offset_fsb,
  322. xfs_buf_t **O_bpp)
  323. {
  324. xfs_fsblock_t firstblock;
  325. xfs_bmap_free_t flist;
  326. xfs_bmbt_irec_t map;
  327. int nmaps, error, committed;
  328. xfs_buf_t *bp;
  329. xfs_trans_t *tp = *tpp;
  330. ASSERT(tp != NULL);
  331. trace_xfs_dqalloc(dqp);
  332. /*
  333. * Initialize the bmap freelist prior to calling bmapi code.
  334. */
  335. xfs_bmap_init(&flist, &firstblock);
  336. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  337. /*
  338. * Return if this type of quotas is turned off while we didn't
  339. * have an inode lock
  340. */
  341. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  342. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  343. return (ESRCH);
  344. }
  345. xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
  346. nmaps = 1;
  347. error = xfs_bmapi_write(tp, quotip, offset_fsb,
  348. XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
  349. &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
  350. &map, &nmaps, &flist);
  351. if (error)
  352. goto error0;
  353. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  354. ASSERT(nmaps == 1);
  355. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  356. (map.br_startblock != HOLESTARTBLOCK));
  357. /*
  358. * Keep track of the blkno to save a lookup later
  359. */
  360. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  361. /* now we can just get the buffer (there's nothing to read yet) */
  362. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  363. dqp->q_blkno,
  364. mp->m_quotainfo->qi_dqchunklen,
  365. 0);
  366. error = xfs_buf_geterror(bp);
  367. if (error)
  368. goto error1;
  369. /*
  370. * Make a chunk of dquots out of this buffer and log
  371. * the entire thing.
  372. */
  373. xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
  374. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  375. /*
  376. * xfs_bmap_finish() may commit the current transaction and
  377. * start a second transaction if the freelist is not empty.
  378. *
  379. * Since we still want to modify this buffer, we need to
  380. * ensure that the buffer is not released on commit of
  381. * the first transaction and ensure the buffer is added to the
  382. * second transaction.
  383. *
  384. * If there is only one transaction then don't stop the buffer
  385. * from being released when it commits later on.
  386. */
  387. xfs_trans_bhold(tp, bp);
  388. if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
  389. goto error1;
  390. }
  391. if (committed) {
  392. tp = *tpp;
  393. xfs_trans_bjoin(tp, bp);
  394. } else {
  395. xfs_trans_bhold_release(tp, bp);
  396. }
  397. *O_bpp = bp;
  398. return 0;
  399. error1:
  400. xfs_bmap_cancel(&flist);
  401. error0:
  402. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  403. return (error);
  404. }
  405. /*
  406. * Maps a dquot to the buffer containing its on-disk version.
  407. * This returns a ptr to the buffer containing the on-disk dquot
  408. * in the bpp param, and a ptr to the on-disk dquot within that buffer
  409. */
  410. STATIC int
  411. xfs_qm_dqtobp(
  412. xfs_trans_t **tpp,
  413. xfs_dquot_t *dqp,
  414. xfs_disk_dquot_t **O_ddpp,
  415. xfs_buf_t **O_bpp,
  416. uint flags)
  417. {
  418. xfs_bmbt_irec_t map;
  419. int nmaps = 1, error;
  420. xfs_buf_t *bp;
  421. xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp);
  422. xfs_mount_t *mp = dqp->q_mount;
  423. xfs_disk_dquot_t *ddq;
  424. xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
  425. xfs_trans_t *tp = (tpp ? *tpp : NULL);
  426. dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
  427. xfs_ilock(quotip, XFS_ILOCK_SHARED);
  428. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  429. /*
  430. * Return if this type of quotas is turned off while we
  431. * didn't have the quota inode lock.
  432. */
  433. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  434. return ESRCH;
  435. }
  436. /*
  437. * Find the block map; no allocations yet
  438. */
  439. error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
  440. XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
  441. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  442. if (error)
  443. return error;
  444. ASSERT(nmaps == 1);
  445. ASSERT(map.br_blockcount == 1);
  446. /*
  447. * Offset of dquot in the (fixed sized) dquot chunk.
  448. */
  449. dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
  450. sizeof(xfs_dqblk_t);
  451. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  452. if (map.br_startblock == HOLESTARTBLOCK) {
  453. /*
  454. * We don't allocate unless we're asked to
  455. */
  456. if (!(flags & XFS_QMOPT_DQALLOC))
  457. return ENOENT;
  458. ASSERT(tp);
  459. error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
  460. dqp->q_fileoffset, &bp);
  461. if (error)
  462. return error;
  463. tp = *tpp;
  464. } else {
  465. trace_xfs_dqtobp_read(dqp);
  466. /*
  467. * store the blkno etc so that we don't have to do the
  468. * mapping all the time
  469. */
  470. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  471. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  472. dqp->q_blkno,
  473. mp->m_quotainfo->qi_dqchunklen,
  474. 0, &bp);
  475. if (error || !bp)
  476. return XFS_ERROR(error);
  477. }
  478. ASSERT(xfs_buf_islocked(bp));
  479. /*
  480. * calculate the location of the dquot inside the buffer.
  481. */
  482. ddq = bp->b_addr + dqp->q_bufoffset;
  483. /*
  484. * A simple sanity check in case we got a corrupted dquot...
  485. */
  486. error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
  487. flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
  488. "dqtobp");
  489. if (error) {
  490. if (!(flags & XFS_QMOPT_DQREPAIR)) {
  491. xfs_trans_brelse(tp, bp);
  492. return XFS_ERROR(EIO);
  493. }
  494. }
  495. *O_bpp = bp;
  496. *O_ddpp = ddq;
  497. return (0);
  498. }
  499. /*
  500. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  501. * and release the buffer immediately.
  502. *
  503. */
  504. /* ARGSUSED */
  505. STATIC int
  506. xfs_qm_dqread(
  507. xfs_trans_t **tpp,
  508. xfs_dqid_t id,
  509. xfs_dquot_t *dqp, /* dquot to get filled in */
  510. uint flags)
  511. {
  512. xfs_disk_dquot_t *ddqp;
  513. xfs_buf_t *bp;
  514. int error;
  515. xfs_trans_t *tp;
  516. ASSERT(tpp);
  517. trace_xfs_dqread(dqp);
  518. /*
  519. * get a pointer to the on-disk dquot and the buffer containing it
  520. * dqp already knows its own type (GROUP/USER).
  521. */
  522. if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
  523. return (error);
  524. }
  525. tp = *tpp;
  526. /* copy everything from disk dquot to the incore dquot */
  527. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  528. ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
  529. xfs_qm_dquot_logitem_init(dqp);
  530. /*
  531. * Reservation counters are defined as reservation plus current usage
  532. * to avoid having to add every time.
  533. */
  534. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  535. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  536. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  537. /* Mark the buf so that this will stay incore a little longer */
  538. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  539. /*
  540. * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
  541. * So we need to release with xfs_trans_brelse().
  542. * The strategy here is identical to that of inodes; we lock
  543. * the dquot in xfs_qm_dqget() before making it accessible to
  544. * others. This is because dquots, like inodes, need a good level of
  545. * concurrency, and we don't want to take locks on the entire buffers
  546. * for dquot accesses.
  547. * Note also that the dquot buffer may even be dirty at this point, if
  548. * this particular dquot was repaired. We still aren't afraid to
  549. * brelse it because we have the changes incore.
  550. */
  551. ASSERT(xfs_buf_islocked(bp));
  552. xfs_trans_brelse(tp, bp);
  553. return (error);
  554. }
  555. /*
  556. * allocate an incore dquot from the kernel heap,
  557. * and fill its core with quota information kept on disk.
  558. * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
  559. * if it wasn't already allocated.
  560. */
  561. STATIC int
  562. xfs_qm_idtodq(
  563. xfs_mount_t *mp,
  564. xfs_dqid_t id, /* gid or uid, depending on type */
  565. uint type, /* UDQUOT or GDQUOT */
  566. uint flags, /* DQALLOC, DQREPAIR */
  567. xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
  568. {
  569. xfs_dquot_t *dqp;
  570. int error;
  571. xfs_trans_t *tp;
  572. int cancelflags=0;
  573. dqp = xfs_qm_dqinit(mp, id, type);
  574. tp = NULL;
  575. if (flags & XFS_QMOPT_DQALLOC) {
  576. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
  577. error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
  578. XFS_WRITE_LOG_RES(mp) +
  579. BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 +
  580. 128,
  581. 0,
  582. XFS_TRANS_PERM_LOG_RES,
  583. XFS_WRITE_LOG_COUNT);
  584. if (error) {
  585. cancelflags = 0;
  586. goto error0;
  587. }
  588. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  589. }
  590. /*
  591. * Read it from disk; xfs_dqread() takes care of
  592. * all the necessary initialization of dquot's fields (locks, etc)
  593. */
  594. if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
  595. /*
  596. * This can happen if quotas got turned off (ESRCH),
  597. * or if the dquot didn't exist on disk and we ask to
  598. * allocate (ENOENT).
  599. */
  600. trace_xfs_dqread_fail(dqp);
  601. cancelflags |= XFS_TRANS_ABORT;
  602. goto error0;
  603. }
  604. if (tp) {
  605. if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
  606. goto error1;
  607. }
  608. *O_dqpp = dqp;
  609. return (0);
  610. error0:
  611. ASSERT(error);
  612. if (tp)
  613. xfs_trans_cancel(tp, cancelflags);
  614. error1:
  615. xfs_qm_dqdestroy(dqp);
  616. *O_dqpp = NULL;
  617. return (error);
  618. }
  619. /*
  620. * Lookup a dquot in the incore dquot hashtable. We keep two separate
  621. * hashtables for user and group dquots; and, these are global tables
  622. * inside the XQM, not per-filesystem tables.
  623. * The hash chain must be locked by caller, and it is left locked
  624. * on return. Returning dquot is locked.
  625. */
  626. STATIC int
  627. xfs_qm_dqlookup(
  628. xfs_mount_t *mp,
  629. xfs_dqid_t id,
  630. xfs_dqhash_t *qh,
  631. xfs_dquot_t **O_dqpp)
  632. {
  633. xfs_dquot_t *dqp;
  634. ASSERT(mutex_is_locked(&qh->qh_lock));
  635. /*
  636. * Traverse the hashchain looking for a match
  637. */
  638. list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
  639. /*
  640. * We already have the hashlock. We don't need the
  641. * dqlock to look at the id field of the dquot, since the
  642. * id can't be modified without the hashlock anyway.
  643. */
  644. if (be32_to_cpu(dqp->q_core.d_id) != id || dqp->q_mount != mp)
  645. continue;
  646. trace_xfs_dqlookup_found(dqp);
  647. xfs_dqlock(dqp);
  648. if (dqp->dq_flags & XFS_DQ_FREEING) {
  649. *O_dqpp = NULL;
  650. xfs_dqunlock(dqp);
  651. return -1;
  652. }
  653. XFS_DQHOLD(dqp);
  654. /*
  655. * move the dquot to the front of the hashchain
  656. */
  657. list_move(&dqp->q_hashlist, &qh->qh_list);
  658. trace_xfs_dqlookup_done(dqp);
  659. *O_dqpp = dqp;
  660. return 0;
  661. }
  662. *O_dqpp = NULL;
  663. return 1;
  664. }
  665. /*
  666. * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
  667. * a locked dquot, doing an allocation (if requested) as needed.
  668. * When both an inode and an id are given, the inode's id takes precedence.
  669. * That is, if the id changes while we don't hold the ilock inside this
  670. * function, the new dquot is returned, not necessarily the one requested
  671. * in the id argument.
  672. */
  673. int
  674. xfs_qm_dqget(
  675. xfs_mount_t *mp,
  676. xfs_inode_t *ip, /* locked inode (optional) */
  677. xfs_dqid_t id, /* uid/projid/gid depending on type */
  678. uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
  679. uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
  680. xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
  681. {
  682. xfs_dquot_t *dqp;
  683. xfs_dqhash_t *h;
  684. uint version;
  685. int error;
  686. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  687. if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
  688. (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
  689. (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
  690. return (ESRCH);
  691. }
  692. h = XFS_DQ_HASH(mp, id, type);
  693. #ifdef DEBUG
  694. if (xfs_do_dqerror) {
  695. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  696. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  697. xfs_debug(mp, "Returning error in dqget");
  698. return (EIO);
  699. }
  700. }
  701. ASSERT(type == XFS_DQ_USER ||
  702. type == XFS_DQ_PROJ ||
  703. type == XFS_DQ_GROUP);
  704. if (ip) {
  705. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  706. if (type == XFS_DQ_USER)
  707. ASSERT(ip->i_udquot == NULL);
  708. else
  709. ASSERT(ip->i_gdquot == NULL);
  710. }
  711. #endif
  712. restart:
  713. mutex_lock(&h->qh_lock);
  714. /*
  715. * Look in the cache (hashtable).
  716. * The chain is kept locked during lookup.
  717. */
  718. switch (xfs_qm_dqlookup(mp, id, h, O_dqpp)) {
  719. case -1:
  720. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  721. mutex_unlock(&h->qh_lock);
  722. delay(1);
  723. goto restart;
  724. case 0:
  725. XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
  726. /*
  727. * The dquot was found, moved to the front of the chain,
  728. * taken off the freelist if it was on it, and locked
  729. * at this point. Just unlock the hashchain and return.
  730. */
  731. ASSERT(*O_dqpp);
  732. ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
  733. mutex_unlock(&h->qh_lock);
  734. trace_xfs_dqget_hit(*O_dqpp);
  735. return 0; /* success */
  736. default:
  737. XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
  738. break;
  739. }
  740. /*
  741. * Dquot cache miss. We don't want to keep the inode lock across
  742. * a (potential) disk read. Also we don't want to deal with the lock
  743. * ordering between quotainode and this inode. OTOH, dropping the inode
  744. * lock here means dealing with a chown that can happen before
  745. * we re-acquire the lock.
  746. */
  747. if (ip)
  748. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  749. /*
  750. * Save the hashchain version stamp, and unlock the chain, so that
  751. * we don't keep the lock across a disk read
  752. */
  753. version = h->qh_version;
  754. mutex_unlock(&h->qh_lock);
  755. /*
  756. * Allocate the dquot on the kernel heap, and read the ondisk
  757. * portion off the disk. Also, do all the necessary initialization
  758. * This can return ENOENT if dquot didn't exist on disk and we didn't
  759. * ask it to allocate; ESRCH if quotas got turned off suddenly.
  760. */
  761. if ((error = xfs_qm_idtodq(mp, id, type,
  762. flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
  763. XFS_QMOPT_DOWARN),
  764. &dqp))) {
  765. if (ip)
  766. xfs_ilock(ip, XFS_ILOCK_EXCL);
  767. return (error);
  768. }
  769. /*
  770. * See if this is mount code calling to look at the overall quota limits
  771. * which are stored in the id == 0 user or group's dquot.
  772. * Since we may not have done a quotacheck by this point, just return
  773. * the dquot without attaching it to any hashtables, lists, etc, or even
  774. * taking a reference.
  775. * The caller must dqdestroy this once done.
  776. */
  777. if (flags & XFS_QMOPT_DQSUSER) {
  778. ASSERT(id == 0);
  779. ASSERT(! ip);
  780. goto dqret;
  781. }
  782. /*
  783. * Dquot lock comes after hashlock in the lock ordering
  784. */
  785. if (ip) {
  786. xfs_ilock(ip, XFS_ILOCK_EXCL);
  787. /*
  788. * A dquot could be attached to this inode by now, since
  789. * we had dropped the ilock.
  790. */
  791. if (type == XFS_DQ_USER) {
  792. if (!XFS_IS_UQUOTA_ON(mp)) {
  793. /* inode stays locked on return */
  794. xfs_qm_dqdestroy(dqp);
  795. return XFS_ERROR(ESRCH);
  796. }
  797. if (ip->i_udquot) {
  798. xfs_qm_dqdestroy(dqp);
  799. dqp = ip->i_udquot;
  800. xfs_dqlock(dqp);
  801. goto dqret;
  802. }
  803. } else {
  804. if (!XFS_IS_OQUOTA_ON(mp)) {
  805. /* inode stays locked on return */
  806. xfs_qm_dqdestroy(dqp);
  807. return XFS_ERROR(ESRCH);
  808. }
  809. if (ip->i_gdquot) {
  810. xfs_qm_dqdestroy(dqp);
  811. dqp = ip->i_gdquot;
  812. xfs_dqlock(dqp);
  813. goto dqret;
  814. }
  815. }
  816. }
  817. /*
  818. * Hashlock comes after ilock in lock order
  819. */
  820. mutex_lock(&h->qh_lock);
  821. if (version != h->qh_version) {
  822. xfs_dquot_t *tmpdqp;
  823. /*
  824. * Now, see if somebody else put the dquot in the
  825. * hashtable before us. This can happen because we didn't
  826. * keep the hashchain lock. We don't have to worry about
  827. * lock order between the two dquots here since dqp isn't
  828. * on any findable lists yet.
  829. */
  830. switch (xfs_qm_dqlookup(mp, id, h, &tmpdqp)) {
  831. case 0:
  832. case -1:
  833. /*
  834. * Duplicate found, either in cache or on its way out.
  835. * Just throw away the new dquot and start over.
  836. */
  837. if (tmpdqp)
  838. xfs_qm_dqput(tmpdqp);
  839. mutex_unlock(&h->qh_lock);
  840. xfs_qm_dqdestroy(dqp);
  841. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  842. goto restart;
  843. default:
  844. break;
  845. }
  846. }
  847. /*
  848. * Put the dquot at the beginning of the hash-chain and mp's list
  849. * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
  850. */
  851. ASSERT(mutex_is_locked(&h->qh_lock));
  852. dqp->q_hash = h;
  853. list_add(&dqp->q_hashlist, &h->qh_list);
  854. h->qh_version++;
  855. /*
  856. * Attach this dquot to this filesystem's list of all dquots,
  857. * kept inside the mount structure in m_quotainfo field
  858. */
  859. mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
  860. /*
  861. * We return a locked dquot to the caller, with a reference taken
  862. */
  863. xfs_dqlock(dqp);
  864. dqp->q_nrefs = 1;
  865. list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
  866. mp->m_quotainfo->qi_dquots++;
  867. mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
  868. mutex_unlock(&h->qh_lock);
  869. dqret:
  870. ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
  871. trace_xfs_dqget_miss(dqp);
  872. *O_dqpp = dqp;
  873. return (0);
  874. }
  875. /*
  876. * Release a reference to the dquot (decrement ref-count)
  877. * and unlock it. If there is a group quota attached to this
  878. * dquot, carefully release that too without tripping over
  879. * deadlocks'n'stuff.
  880. */
  881. void
  882. xfs_qm_dqput(
  883. struct xfs_dquot *dqp)
  884. {
  885. struct xfs_dquot *gdqp;
  886. ASSERT(dqp->q_nrefs > 0);
  887. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  888. trace_xfs_dqput(dqp);
  889. recurse:
  890. if (--dqp->q_nrefs > 0) {
  891. xfs_dqunlock(dqp);
  892. return;
  893. }
  894. trace_xfs_dqput_free(dqp);
  895. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  896. if (list_empty(&dqp->q_freelist)) {
  897. list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
  898. xfs_Gqm->qm_dqfrlist_cnt++;
  899. }
  900. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  901. /*
  902. * If we just added a udquot to the freelist, then we want to release
  903. * the gdquot reference that it (probably) has. Otherwise it'll keep
  904. * the gdquot from getting reclaimed.
  905. */
  906. gdqp = dqp->q_gdquot;
  907. if (gdqp) {
  908. xfs_dqlock(gdqp);
  909. dqp->q_gdquot = NULL;
  910. }
  911. xfs_dqunlock(dqp);
  912. /*
  913. * If we had a group quota hint, release it now.
  914. */
  915. if (gdqp) {
  916. dqp = gdqp;
  917. goto recurse;
  918. }
  919. }
  920. /*
  921. * Release a dquot. Flush it if dirty, then dqput() it.
  922. * dquot must not be locked.
  923. */
  924. void
  925. xfs_qm_dqrele(
  926. xfs_dquot_t *dqp)
  927. {
  928. if (!dqp)
  929. return;
  930. trace_xfs_dqrele(dqp);
  931. xfs_dqlock(dqp);
  932. /*
  933. * We don't care to flush it if the dquot is dirty here.
  934. * That will create stutters that we want to avoid.
  935. * Instead we do a delayed write when we try to reclaim
  936. * a dirty dquot. Also xfs_sync will take part of the burden...
  937. */
  938. xfs_qm_dqput(dqp);
  939. }
  940. /*
  941. * This is the dquot flushing I/O completion routine. It is called
  942. * from interrupt level when the buffer containing the dquot is
  943. * flushed to disk. It is responsible for removing the dquot logitem
  944. * from the AIL if it has not been re-logged, and unlocking the dquot's
  945. * flush lock. This behavior is very similar to that of inodes..
  946. */
  947. STATIC void
  948. xfs_qm_dqflush_done(
  949. struct xfs_buf *bp,
  950. struct xfs_log_item *lip)
  951. {
  952. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  953. xfs_dquot_t *dqp = qip->qli_dquot;
  954. struct xfs_ail *ailp = lip->li_ailp;
  955. /*
  956. * We only want to pull the item from the AIL if its
  957. * location in the log has not changed since we started the flush.
  958. * Thus, we only bother if the dquot's lsn has
  959. * not changed. First we check the lsn outside the lock
  960. * since it's cheaper, and then we recheck while
  961. * holding the lock before removing the dquot from the AIL.
  962. */
  963. if ((lip->li_flags & XFS_LI_IN_AIL) &&
  964. lip->li_lsn == qip->qli_flush_lsn) {
  965. /* xfs_trans_ail_delete() drops the AIL lock. */
  966. spin_lock(&ailp->xa_lock);
  967. if (lip->li_lsn == qip->qli_flush_lsn)
  968. xfs_trans_ail_delete(ailp, lip);
  969. else
  970. spin_unlock(&ailp->xa_lock);
  971. }
  972. /*
  973. * Release the dq's flush lock since we're done with it.
  974. */
  975. xfs_dqfunlock(dqp);
  976. }
  977. /*
  978. * Write a modified dquot to disk.
  979. * The dquot must be locked and the flush lock too taken by caller.
  980. * The flush lock will not be unlocked until the dquot reaches the disk,
  981. * but the dquot is free to be unlocked and modified by the caller
  982. * in the interim. Dquot is still locked on return. This behavior is
  983. * identical to that of inodes.
  984. */
  985. int
  986. xfs_qm_dqflush(
  987. xfs_dquot_t *dqp,
  988. uint flags)
  989. {
  990. struct xfs_mount *mp = dqp->q_mount;
  991. struct xfs_buf *bp;
  992. struct xfs_disk_dquot *ddqp;
  993. int error;
  994. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  995. ASSERT(!completion_done(&dqp->q_flush));
  996. trace_xfs_dqflush(dqp);
  997. /*
  998. * If not dirty, or it's pinned and we are not supposed to block, nada.
  999. */
  1000. if (!XFS_DQ_IS_DIRTY(dqp) ||
  1001. ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
  1002. xfs_dqfunlock(dqp);
  1003. return 0;
  1004. }
  1005. xfs_qm_dqunpin_wait(dqp);
  1006. /*
  1007. * This may have been unpinned because the filesystem is shutting
  1008. * down forcibly. If that's the case we must not write this dquot
  1009. * to disk, because the log record didn't make it to disk!
  1010. */
  1011. if (XFS_FORCED_SHUTDOWN(mp)) {
  1012. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  1013. xfs_dqfunlock(dqp);
  1014. return XFS_ERROR(EIO);
  1015. }
  1016. /*
  1017. * Get the buffer containing the on-disk dquot
  1018. */
  1019. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  1020. mp->m_quotainfo->qi_dqchunklen, 0, &bp);
  1021. if (error) {
  1022. ASSERT(error != ENOENT);
  1023. xfs_dqfunlock(dqp);
  1024. return error;
  1025. }
  1026. /*
  1027. * Calculate the location of the dquot inside the buffer.
  1028. */
  1029. ddqp = bp->b_addr + dqp->q_bufoffset;
  1030. /*
  1031. * A simple sanity check in case we got a corrupted dquot..
  1032. */
  1033. error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
  1034. XFS_QMOPT_DOWARN, "dqflush (incore copy)");
  1035. if (error) {
  1036. xfs_buf_relse(bp);
  1037. xfs_dqfunlock(dqp);
  1038. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1039. return XFS_ERROR(EIO);
  1040. }
  1041. /* This is the only portion of data that needs to persist */
  1042. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  1043. /*
  1044. * Clear the dirty field and remember the flush lsn for later use.
  1045. */
  1046. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  1047. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  1048. &dqp->q_logitem.qli_item.li_lsn);
  1049. /*
  1050. * Attach an iodone routine so that we can remove this dquot from the
  1051. * AIL and release the flush lock once the dquot is synced to disk.
  1052. */
  1053. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  1054. &dqp->q_logitem.qli_item);
  1055. /*
  1056. * If the buffer is pinned then push on the log so we won't
  1057. * get stuck waiting in the write for too long.
  1058. */
  1059. if (xfs_buf_ispinned(bp)) {
  1060. trace_xfs_dqflush_force(dqp);
  1061. xfs_log_force(mp, 0);
  1062. }
  1063. if (flags & SYNC_WAIT)
  1064. error = xfs_bwrite(bp);
  1065. else
  1066. xfs_buf_delwri_queue(bp);
  1067. xfs_buf_relse(bp);
  1068. trace_xfs_dqflush_done(dqp);
  1069. /*
  1070. * dqp is still locked, but caller is free to unlock it now.
  1071. */
  1072. return error;
  1073. }
  1074. void
  1075. xfs_dqunlock(
  1076. xfs_dquot_t *dqp)
  1077. {
  1078. xfs_dqunlock_nonotify(dqp);
  1079. if (dqp->q_logitem.qli_dquot == dqp) {
  1080. xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
  1081. &dqp->q_logitem.qli_item);
  1082. }
  1083. }
  1084. /*
  1085. * Lock two xfs_dquot structures.
  1086. *
  1087. * To avoid deadlocks we always lock the quota structure with
  1088. * the lowerd id first.
  1089. */
  1090. void
  1091. xfs_dqlock2(
  1092. xfs_dquot_t *d1,
  1093. xfs_dquot_t *d2)
  1094. {
  1095. if (d1 && d2) {
  1096. ASSERT(d1 != d2);
  1097. if (be32_to_cpu(d1->q_core.d_id) >
  1098. be32_to_cpu(d2->q_core.d_id)) {
  1099. mutex_lock(&d2->q_qlock);
  1100. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  1101. } else {
  1102. mutex_lock(&d1->q_qlock);
  1103. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  1104. }
  1105. } else if (d1) {
  1106. mutex_lock(&d1->q_qlock);
  1107. } else if (d2) {
  1108. mutex_lock(&d2->q_qlock);
  1109. }
  1110. }
  1111. /*
  1112. * Take a dquot out of the mount's dqlist as well as the hashlist. This is
  1113. * called via unmount as well as quotaoff, and the purge will always succeed.
  1114. */
  1115. void
  1116. xfs_qm_dqpurge(
  1117. struct xfs_dquot *dqp)
  1118. {
  1119. struct xfs_mount *mp = dqp->q_mount;
  1120. struct xfs_dqhash *qh = dqp->q_hash;
  1121. xfs_dqlock(dqp);
  1122. /*
  1123. * If we're turning off quotas, we have to make sure that, for
  1124. * example, we don't delete quota disk blocks while dquots are
  1125. * in the process of getting written to those disk blocks.
  1126. * This dquot might well be on AIL, and we can't leave it there
  1127. * if we're turning off quotas. Basically, we need this flush
  1128. * lock, and are willing to block on it.
  1129. */
  1130. if (!xfs_dqflock_nowait(dqp)) {
  1131. /*
  1132. * Block on the flush lock after nudging dquot buffer,
  1133. * if it is incore.
  1134. */
  1135. xfs_dqflock_pushbuf_wait(dqp);
  1136. }
  1137. /*
  1138. * If we are turning this type of quotas off, we don't care
  1139. * about the dirty metadata sitting in this dquot. OTOH, if
  1140. * we're unmounting, we do care, so we flush it and wait.
  1141. */
  1142. if (XFS_DQ_IS_DIRTY(dqp)) {
  1143. int error;
  1144. /*
  1145. * We don't care about getting disk errors here. We need
  1146. * to purge this dquot anyway, so we go ahead regardless.
  1147. */
  1148. error = xfs_qm_dqflush(dqp, SYNC_WAIT);
  1149. if (error)
  1150. xfs_warn(mp, "%s: dquot %p flush failed",
  1151. __func__, dqp);
  1152. xfs_dqflock(dqp);
  1153. }
  1154. ASSERT(atomic_read(&dqp->q_pincount) == 0);
  1155. ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
  1156. !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
  1157. xfs_dqfunlock(dqp);
  1158. xfs_dqunlock(dqp);
  1159. mutex_lock(&qh->qh_lock);
  1160. list_del_init(&dqp->q_hashlist);
  1161. qh->qh_version++;
  1162. mutex_unlock(&qh->qh_lock);
  1163. mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
  1164. list_del_init(&dqp->q_mplist);
  1165. mp->m_quotainfo->qi_dqreclaims++;
  1166. mp->m_quotainfo->qi_dquots--;
  1167. mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
  1168. /*
  1169. * We move dquots to the freelist as soon as their reference count
  1170. * hits zero, so it really should be on the freelist here.
  1171. */
  1172. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  1173. ASSERT(!list_empty(&dqp->q_freelist));
  1174. list_del_init(&dqp->q_freelist);
  1175. xfs_Gqm->qm_dqfrlist_cnt--;
  1176. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  1177. xfs_qm_dqdestroy(dqp);
  1178. }
  1179. /*
  1180. * Give the buffer a little push if it is incore and
  1181. * wait on the flush lock.
  1182. */
  1183. void
  1184. xfs_dqflock_pushbuf_wait(
  1185. xfs_dquot_t *dqp)
  1186. {
  1187. xfs_mount_t *mp = dqp->q_mount;
  1188. xfs_buf_t *bp;
  1189. /*
  1190. * Check to see if the dquot has been flushed delayed
  1191. * write. If so, grab its buffer and send it
  1192. * out immediately. We'll be able to acquire
  1193. * the flush lock when the I/O completes.
  1194. */
  1195. bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
  1196. mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
  1197. if (!bp)
  1198. goto out_lock;
  1199. if (XFS_BUF_ISDELAYWRITE(bp)) {
  1200. if (xfs_buf_ispinned(bp))
  1201. xfs_log_force(mp, 0);
  1202. xfs_buf_delwri_promote(bp);
  1203. wake_up_process(bp->b_target->bt_task);
  1204. }
  1205. xfs_buf_relse(bp);
  1206. out_lock:
  1207. xfs_dqflock(dqp);
  1208. }