xfs_dquot.c 38 KB

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