xfs_dquot.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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. sv_destroy(&dqp->q_pinwait);
  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 QUOTADEBUG
  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_ISBUSY(bp));
  296. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  297. d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
  298. /*
  299. * ID of the first dquot in the block - id's are zero based.
  300. */
  301. curid = id - (id % q->qi_dqperchunk);
  302. ASSERT(curid >= 0);
  303. memset(d, 0, BBTOB(q->qi_dqchunklen));
  304. for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++)
  305. xfs_qm_dqinit_core(curid, type, d);
  306. xfs_trans_dquot_buf(tp, bp,
  307. (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
  308. ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
  309. XFS_BLF_GDQUOT_BUF)));
  310. xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
  311. }
  312. /*
  313. * Allocate a block and fill it with dquots.
  314. * This is called when the bmapi finds a hole.
  315. */
  316. STATIC int
  317. xfs_qm_dqalloc(
  318. xfs_trans_t **tpp,
  319. xfs_mount_t *mp,
  320. xfs_dquot_t *dqp,
  321. xfs_inode_t *quotip,
  322. xfs_fileoff_t offset_fsb,
  323. xfs_buf_t **O_bpp)
  324. {
  325. xfs_fsblock_t firstblock;
  326. xfs_bmap_free_t flist;
  327. xfs_bmbt_irec_t map;
  328. int nmaps, error, committed;
  329. xfs_buf_t *bp;
  330. xfs_trans_t *tp = *tpp;
  331. ASSERT(tp != NULL);
  332. trace_xfs_dqalloc(dqp);
  333. /*
  334. * Initialize the bmap freelist prior to calling bmapi code.
  335. */
  336. xfs_bmap_init(&flist, &firstblock);
  337. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  338. /*
  339. * Return if this type of quotas is turned off while we didn't
  340. * have an inode lock
  341. */
  342. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  343. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  344. return (ESRCH);
  345. }
  346. /*
  347. * xfs_trans_commit normally decrements the vnode ref count
  348. * when it unlocks the inode. Since we want to keep the quota
  349. * inode around, we bump the vnode ref count now.
  350. */
  351. IHOLD(quotip);
  352. xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
  353. nmaps = 1;
  354. if ((error = xfs_bmapi(tp, quotip,
  355. offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
  356. XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
  357. &firstblock,
  358. XFS_QM_DQALLOC_SPACE_RES(mp),
  359. &map, &nmaps, &flist, NULL))) {
  360. goto error0;
  361. }
  362. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  363. ASSERT(nmaps == 1);
  364. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  365. (map.br_startblock != HOLESTARTBLOCK));
  366. /*
  367. * Keep track of the blkno to save a lookup later
  368. */
  369. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  370. /* now we can just get the buffer (there's nothing to read yet) */
  371. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  372. dqp->q_blkno,
  373. mp->m_quotainfo->qi_dqchunklen,
  374. 0);
  375. if (!bp || (error = XFS_BUF_GETERROR(bp)))
  376. goto error1;
  377. /*
  378. * Make a chunk of dquots out of this buffer and log
  379. * the entire thing.
  380. */
  381. xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
  382. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  383. /*
  384. * xfs_bmap_finish() may commit the current transaction and
  385. * start a second transaction if the freelist is not empty.
  386. *
  387. * Since we still want to modify this buffer, we need to
  388. * ensure that the buffer is not released on commit of
  389. * the first transaction and ensure the buffer is added to the
  390. * second transaction.
  391. *
  392. * If there is only one transaction then don't stop the buffer
  393. * from being released when it commits later on.
  394. */
  395. xfs_trans_bhold(tp, bp);
  396. if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
  397. goto error1;
  398. }
  399. if (committed) {
  400. tp = *tpp;
  401. xfs_trans_bjoin(tp, bp);
  402. } else {
  403. xfs_trans_bhold_release(tp, bp);
  404. }
  405. *O_bpp = bp;
  406. return 0;
  407. error1:
  408. xfs_bmap_cancel(&flist);
  409. error0:
  410. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  411. return (error);
  412. }
  413. /*
  414. * Maps a dquot to the buffer containing its on-disk version.
  415. * This returns a ptr to the buffer containing the on-disk dquot
  416. * in the bpp param, and a ptr to the on-disk dquot within that buffer
  417. */
  418. STATIC int
  419. xfs_qm_dqtobp(
  420. xfs_trans_t **tpp,
  421. xfs_dquot_t *dqp,
  422. xfs_disk_dquot_t **O_ddpp,
  423. xfs_buf_t **O_bpp,
  424. uint flags)
  425. {
  426. xfs_bmbt_irec_t map;
  427. int nmaps, error;
  428. xfs_buf_t *bp;
  429. xfs_inode_t *quotip;
  430. xfs_mount_t *mp;
  431. xfs_disk_dquot_t *ddq;
  432. xfs_dqid_t id;
  433. boolean_t newdquot;
  434. xfs_trans_t *tp = (tpp ? *tpp : NULL);
  435. mp = dqp->q_mount;
  436. id = be32_to_cpu(dqp->q_core.d_id);
  437. nmaps = 1;
  438. newdquot = B_FALSE;
  439. /*
  440. * If we don't know where the dquot lives, find out.
  441. */
  442. if (dqp->q_blkno == (xfs_daddr_t) 0) {
  443. /* We use the id as an index */
  444. dqp->q_fileoffset = (xfs_fileoff_t)id /
  445. mp->m_quotainfo->qi_dqperchunk;
  446. nmaps = 1;
  447. quotip = XFS_DQ_TO_QIP(dqp);
  448. xfs_ilock(quotip, XFS_ILOCK_SHARED);
  449. /*
  450. * Return if this type of quotas is turned off while we didn't
  451. * have an inode lock
  452. */
  453. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  454. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  455. return (ESRCH);
  456. }
  457. /*
  458. * Find the block map; no allocations yet
  459. */
  460. error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
  461. XFS_DQUOT_CLUSTER_SIZE_FSB,
  462. XFS_BMAPI_METADATA,
  463. NULL, 0, &map, &nmaps, NULL, NULL);
  464. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  465. if (error)
  466. return (error);
  467. ASSERT(nmaps == 1);
  468. ASSERT(map.br_blockcount == 1);
  469. /*
  470. * offset of dquot in the (fixed sized) dquot chunk.
  471. */
  472. dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
  473. sizeof(xfs_dqblk_t);
  474. if (map.br_startblock == HOLESTARTBLOCK) {
  475. /*
  476. * We don't allocate unless we're asked to
  477. */
  478. if (!(flags & XFS_QMOPT_DQALLOC))
  479. return (ENOENT);
  480. ASSERT(tp);
  481. if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
  482. dqp->q_fileoffset, &bp)))
  483. return (error);
  484. tp = *tpp;
  485. newdquot = B_TRUE;
  486. } else {
  487. /*
  488. * store the blkno etc so that we don't have to do the
  489. * mapping all the time
  490. */
  491. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  492. }
  493. }
  494. ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
  495. ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
  496. /*
  497. * Read in the buffer, unless we've just done the allocation
  498. * (in which case we already have the buf).
  499. */
  500. if (!newdquot) {
  501. trace_xfs_dqtobp_read(dqp);
  502. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  503. dqp->q_blkno,
  504. mp->m_quotainfo->qi_dqchunklen,
  505. 0, &bp);
  506. if (error || !bp)
  507. return XFS_ERROR(error);
  508. }
  509. ASSERT(XFS_BUF_ISBUSY(bp));
  510. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  511. /*
  512. * calculate the location of the dquot inside the buffer.
  513. */
  514. ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
  515. /*
  516. * A simple sanity check in case we got a corrupted dquot...
  517. */
  518. if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
  519. flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
  520. "dqtobp")) {
  521. if (!(flags & XFS_QMOPT_DQREPAIR)) {
  522. xfs_trans_brelse(tp, bp);
  523. return XFS_ERROR(EIO);
  524. }
  525. XFS_BUF_BUSY(bp); /* We dirtied this */
  526. }
  527. *O_bpp = bp;
  528. *O_ddpp = ddq;
  529. return (0);
  530. }
  531. /*
  532. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  533. * and release the buffer immediately.
  534. *
  535. */
  536. /* ARGSUSED */
  537. STATIC int
  538. xfs_qm_dqread(
  539. xfs_trans_t **tpp,
  540. xfs_dqid_t id,
  541. xfs_dquot_t *dqp, /* dquot to get filled in */
  542. uint flags)
  543. {
  544. xfs_disk_dquot_t *ddqp;
  545. xfs_buf_t *bp;
  546. int error;
  547. xfs_trans_t *tp;
  548. ASSERT(tpp);
  549. trace_xfs_dqread(dqp);
  550. /*
  551. * get a pointer to the on-disk dquot and the buffer containing it
  552. * dqp already knows its own type (GROUP/USER).
  553. */
  554. if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
  555. return (error);
  556. }
  557. tp = *tpp;
  558. /* copy everything from disk dquot to the incore dquot */
  559. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  560. ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
  561. xfs_qm_dquot_logitem_init(dqp);
  562. /*
  563. * Reservation counters are defined as reservation plus current usage
  564. * to avoid having to add everytime.
  565. */
  566. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  567. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  568. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  569. /* Mark the buf so that this will stay incore a little longer */
  570. XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
  571. /*
  572. * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
  573. * So we need to release with xfs_trans_brelse().
  574. * The strategy here is identical to that of inodes; we lock
  575. * the dquot in xfs_qm_dqget() before making it accessible to
  576. * others. This is because dquots, like inodes, need a good level of
  577. * concurrency, and we don't want to take locks on the entire buffers
  578. * for dquot accesses.
  579. * Note also that the dquot buffer may even be dirty at this point, if
  580. * this particular dquot was repaired. We still aren't afraid to
  581. * brelse it because we have the changes incore.
  582. */
  583. ASSERT(XFS_BUF_ISBUSY(bp));
  584. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  585. xfs_trans_brelse(tp, bp);
  586. return (error);
  587. }
  588. /*
  589. * allocate an incore dquot from the kernel heap,
  590. * and fill its core with quota information kept on disk.
  591. * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
  592. * if it wasn't already allocated.
  593. */
  594. STATIC int
  595. xfs_qm_idtodq(
  596. xfs_mount_t *mp,
  597. xfs_dqid_t id, /* gid or uid, depending on type */
  598. uint type, /* UDQUOT or GDQUOT */
  599. uint flags, /* DQALLOC, DQREPAIR */
  600. xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
  601. {
  602. xfs_dquot_t *dqp;
  603. int error;
  604. xfs_trans_t *tp;
  605. int cancelflags=0;
  606. dqp = xfs_qm_dqinit(mp, id, type);
  607. tp = NULL;
  608. if (flags & XFS_QMOPT_DQALLOC) {
  609. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
  610. error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
  611. XFS_WRITE_LOG_RES(mp) +
  612. BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 +
  613. 128,
  614. 0,
  615. XFS_TRANS_PERM_LOG_RES,
  616. XFS_WRITE_LOG_COUNT);
  617. if (error) {
  618. cancelflags = 0;
  619. goto error0;
  620. }
  621. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  622. }
  623. /*
  624. * Read it from disk; xfs_dqread() takes care of
  625. * all the necessary initialization of dquot's fields (locks, etc)
  626. */
  627. if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
  628. /*
  629. * This can happen if quotas got turned off (ESRCH),
  630. * or if the dquot didn't exist on disk and we ask to
  631. * allocate (ENOENT).
  632. */
  633. trace_xfs_dqread_fail(dqp);
  634. cancelflags |= XFS_TRANS_ABORT;
  635. goto error0;
  636. }
  637. if (tp) {
  638. if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
  639. goto error1;
  640. }
  641. *O_dqpp = dqp;
  642. return (0);
  643. error0:
  644. ASSERT(error);
  645. if (tp)
  646. xfs_trans_cancel(tp, cancelflags);
  647. error1:
  648. xfs_qm_dqdestroy(dqp);
  649. *O_dqpp = NULL;
  650. return (error);
  651. }
  652. /*
  653. * Lookup a dquot in the incore dquot hashtable. We keep two separate
  654. * hashtables for user and group dquots; and, these are global tables
  655. * inside the XQM, not per-filesystem tables.
  656. * The hash chain must be locked by caller, and it is left locked
  657. * on return. Returning dquot is locked.
  658. */
  659. STATIC int
  660. xfs_qm_dqlookup(
  661. xfs_mount_t *mp,
  662. xfs_dqid_t id,
  663. xfs_dqhash_t *qh,
  664. xfs_dquot_t **O_dqpp)
  665. {
  666. xfs_dquot_t *dqp;
  667. uint flist_locked;
  668. ASSERT(mutex_is_locked(&qh->qh_lock));
  669. flist_locked = B_FALSE;
  670. /*
  671. * Traverse the hashchain looking for a match
  672. */
  673. list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
  674. /*
  675. * We already have the hashlock. We don't need the
  676. * dqlock to look at the id field of the dquot, since the
  677. * id can't be modified without the hashlock anyway.
  678. */
  679. if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
  680. trace_xfs_dqlookup_found(dqp);
  681. /*
  682. * All in core dquots must be on the dqlist of mp
  683. */
  684. ASSERT(!list_empty(&dqp->q_mplist));
  685. xfs_dqlock(dqp);
  686. if (dqp->q_nrefs == 0) {
  687. ASSERT(!list_empty(&dqp->q_freelist));
  688. if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
  689. trace_xfs_dqlookup_want(dqp);
  690. /*
  691. * We may have raced with dqreclaim_one()
  692. * (and lost). So, flag that we don't
  693. * want the dquot to be reclaimed.
  694. */
  695. dqp->dq_flags |= XFS_DQ_WANT;
  696. xfs_dqunlock(dqp);
  697. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  698. xfs_dqlock(dqp);
  699. dqp->dq_flags &= ~(XFS_DQ_WANT);
  700. }
  701. flist_locked = B_TRUE;
  702. }
  703. /*
  704. * id couldn't have changed; we had the hashlock all
  705. * along
  706. */
  707. ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
  708. if (flist_locked) {
  709. if (dqp->q_nrefs != 0) {
  710. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  711. flist_locked = B_FALSE;
  712. } else {
  713. /* take it off the freelist */
  714. trace_xfs_dqlookup_freelist(dqp);
  715. list_del_init(&dqp->q_freelist);
  716. xfs_Gqm->qm_dqfrlist_cnt--;
  717. }
  718. }
  719. XFS_DQHOLD(dqp);
  720. if (flist_locked)
  721. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  722. /*
  723. * move the dquot to the front of the hashchain
  724. */
  725. ASSERT(mutex_is_locked(&qh->qh_lock));
  726. list_move(&dqp->q_hashlist, &qh->qh_list);
  727. trace_xfs_dqlookup_done(dqp);
  728. *O_dqpp = dqp;
  729. return 0;
  730. }
  731. }
  732. *O_dqpp = NULL;
  733. ASSERT(mutex_is_locked(&qh->qh_lock));
  734. return (1);
  735. }
  736. /*
  737. * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
  738. * a locked dquot, doing an allocation (if requested) as needed.
  739. * When both an inode and an id are given, the inode's id takes precedence.
  740. * That is, if the id changes while we don't hold the ilock inside this
  741. * function, the new dquot is returned, not necessarily the one requested
  742. * in the id argument.
  743. */
  744. int
  745. xfs_qm_dqget(
  746. xfs_mount_t *mp,
  747. xfs_inode_t *ip, /* locked inode (optional) */
  748. xfs_dqid_t id, /* uid/projid/gid depending on type */
  749. uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
  750. uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
  751. xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
  752. {
  753. xfs_dquot_t *dqp;
  754. xfs_dqhash_t *h;
  755. uint version;
  756. int error;
  757. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  758. if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
  759. (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
  760. (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
  761. return (ESRCH);
  762. }
  763. h = XFS_DQ_HASH(mp, id, type);
  764. #ifdef DEBUG
  765. if (xfs_do_dqerror) {
  766. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  767. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  768. cmn_err(CE_DEBUG, "Returning error in dqget");
  769. return (EIO);
  770. }
  771. }
  772. #endif
  773. again:
  774. #ifdef DEBUG
  775. ASSERT(type == XFS_DQ_USER ||
  776. type == XFS_DQ_PROJ ||
  777. type == XFS_DQ_GROUP);
  778. if (ip) {
  779. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  780. if (type == XFS_DQ_USER)
  781. ASSERT(ip->i_udquot == NULL);
  782. else
  783. ASSERT(ip->i_gdquot == NULL);
  784. }
  785. #endif
  786. mutex_lock(&h->qh_lock);
  787. /*
  788. * Look in the cache (hashtable).
  789. * The chain is kept locked during lookup.
  790. */
  791. if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
  792. XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
  793. /*
  794. * The dquot was found, moved to the front of the chain,
  795. * taken off the freelist if it was on it, and locked
  796. * at this point. Just unlock the hashchain and return.
  797. */
  798. ASSERT(*O_dqpp);
  799. ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
  800. mutex_unlock(&h->qh_lock);
  801. trace_xfs_dqget_hit(*O_dqpp);
  802. return (0); /* success */
  803. }
  804. XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
  805. /*
  806. * Dquot cache miss. We don't want to keep the inode lock across
  807. * a (potential) disk read. Also we don't want to deal with the lock
  808. * ordering between quotainode and this inode. OTOH, dropping the inode
  809. * lock here means dealing with a chown that can happen before
  810. * we re-acquire the lock.
  811. */
  812. if (ip)
  813. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  814. /*
  815. * Save the hashchain version stamp, and unlock the chain, so that
  816. * we don't keep the lock across a disk read
  817. */
  818. version = h->qh_version;
  819. mutex_unlock(&h->qh_lock);
  820. /*
  821. * Allocate the dquot on the kernel heap, and read the ondisk
  822. * portion off the disk. Also, do all the necessary initialization
  823. * This can return ENOENT if dquot didn't exist on disk and we didn't
  824. * ask it to allocate; ESRCH if quotas got turned off suddenly.
  825. */
  826. if ((error = xfs_qm_idtodq(mp, id, type,
  827. flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
  828. XFS_QMOPT_DOWARN),
  829. &dqp))) {
  830. if (ip)
  831. xfs_ilock(ip, XFS_ILOCK_EXCL);
  832. return (error);
  833. }
  834. /*
  835. * See if this is mount code calling to look at the overall quota limits
  836. * which are stored in the id == 0 user or group's dquot.
  837. * Since we may not have done a quotacheck by this point, just return
  838. * the dquot without attaching it to any hashtables, lists, etc, or even
  839. * taking a reference.
  840. * The caller must dqdestroy this once done.
  841. */
  842. if (flags & XFS_QMOPT_DQSUSER) {
  843. ASSERT(id == 0);
  844. ASSERT(! ip);
  845. goto dqret;
  846. }
  847. /*
  848. * Dquot lock comes after hashlock in the lock ordering
  849. */
  850. if (ip) {
  851. xfs_ilock(ip, XFS_ILOCK_EXCL);
  852. /*
  853. * A dquot could be attached to this inode by now, since
  854. * we had dropped the ilock.
  855. */
  856. if (type == XFS_DQ_USER) {
  857. if (!XFS_IS_UQUOTA_ON(mp)) {
  858. /* inode stays locked on return */
  859. xfs_qm_dqdestroy(dqp);
  860. return XFS_ERROR(ESRCH);
  861. }
  862. if (ip->i_udquot) {
  863. xfs_qm_dqdestroy(dqp);
  864. dqp = ip->i_udquot;
  865. xfs_dqlock(dqp);
  866. goto dqret;
  867. }
  868. } else {
  869. if (!XFS_IS_OQUOTA_ON(mp)) {
  870. /* inode stays locked on return */
  871. xfs_qm_dqdestroy(dqp);
  872. return XFS_ERROR(ESRCH);
  873. }
  874. if (ip->i_gdquot) {
  875. xfs_qm_dqdestroy(dqp);
  876. dqp = ip->i_gdquot;
  877. xfs_dqlock(dqp);
  878. goto dqret;
  879. }
  880. }
  881. }
  882. /*
  883. * Hashlock comes after ilock in lock order
  884. */
  885. mutex_lock(&h->qh_lock);
  886. if (version != h->qh_version) {
  887. xfs_dquot_t *tmpdqp;
  888. /*
  889. * Now, see if somebody else put the dquot in the
  890. * hashtable before us. This can happen because we didn't
  891. * keep the hashchain lock. We don't have to worry about
  892. * lock order between the two dquots here since dqp isn't
  893. * on any findable lists yet.
  894. */
  895. if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
  896. /*
  897. * Duplicate found. Just throw away the new dquot
  898. * and start over.
  899. */
  900. xfs_qm_dqput(tmpdqp);
  901. mutex_unlock(&h->qh_lock);
  902. xfs_qm_dqdestroy(dqp);
  903. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  904. goto again;
  905. }
  906. }
  907. /*
  908. * Put the dquot at the beginning of the hash-chain and mp's list
  909. * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
  910. */
  911. ASSERT(mutex_is_locked(&h->qh_lock));
  912. dqp->q_hash = h;
  913. list_add(&dqp->q_hashlist, &h->qh_list);
  914. h->qh_version++;
  915. /*
  916. * Attach this dquot to this filesystem's list of all dquots,
  917. * kept inside the mount structure in m_quotainfo field
  918. */
  919. mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
  920. /*
  921. * We return a locked dquot to the caller, with a reference taken
  922. */
  923. xfs_dqlock(dqp);
  924. dqp->q_nrefs = 1;
  925. list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
  926. mp->m_quotainfo->qi_dquots++;
  927. mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
  928. mutex_unlock(&h->qh_lock);
  929. dqret:
  930. ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
  931. trace_xfs_dqget_miss(dqp);
  932. *O_dqpp = dqp;
  933. return (0);
  934. }
  935. /*
  936. * Release a reference to the dquot (decrement ref-count)
  937. * and unlock it. If there is a group quota attached to this
  938. * dquot, carefully release that too without tripping over
  939. * deadlocks'n'stuff.
  940. */
  941. void
  942. xfs_qm_dqput(
  943. xfs_dquot_t *dqp)
  944. {
  945. xfs_dquot_t *gdqp;
  946. ASSERT(dqp->q_nrefs > 0);
  947. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  948. trace_xfs_dqput(dqp);
  949. if (dqp->q_nrefs != 1) {
  950. dqp->q_nrefs--;
  951. xfs_dqunlock(dqp);
  952. return;
  953. }
  954. /*
  955. * drop the dqlock and acquire the freelist and dqlock
  956. * in the right order; but try to get it out-of-order first
  957. */
  958. if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
  959. trace_xfs_dqput_wait(dqp);
  960. xfs_dqunlock(dqp);
  961. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  962. xfs_dqlock(dqp);
  963. }
  964. while (1) {
  965. gdqp = NULL;
  966. /* We can't depend on nrefs being == 1 here */
  967. if (--dqp->q_nrefs == 0) {
  968. trace_xfs_dqput_free(dqp);
  969. list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
  970. xfs_Gqm->qm_dqfrlist_cnt++;
  971. /*
  972. * If we just added a udquot to the freelist, then
  973. * we want to release the gdquot reference that
  974. * it (probably) has. Otherwise it'll keep the
  975. * gdquot from getting reclaimed.
  976. */
  977. if ((gdqp = dqp->q_gdquot)) {
  978. /*
  979. * Avoid a recursive dqput call
  980. */
  981. xfs_dqlock(gdqp);
  982. dqp->q_gdquot = NULL;
  983. }
  984. }
  985. xfs_dqunlock(dqp);
  986. /*
  987. * If we had a group quota inside the user quota as a hint,
  988. * release it now.
  989. */
  990. if (! gdqp)
  991. break;
  992. dqp = gdqp;
  993. }
  994. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  995. }
  996. /*
  997. * Release a dquot. Flush it if dirty, then dqput() it.
  998. * dquot must not be locked.
  999. */
  1000. void
  1001. xfs_qm_dqrele(
  1002. xfs_dquot_t *dqp)
  1003. {
  1004. if (!dqp)
  1005. return;
  1006. trace_xfs_dqrele(dqp);
  1007. xfs_dqlock(dqp);
  1008. /*
  1009. * We don't care to flush it if the dquot is dirty here.
  1010. * That will create stutters that we want to avoid.
  1011. * Instead we do a delayed write when we try to reclaim
  1012. * a dirty dquot. Also xfs_sync will take part of the burden...
  1013. */
  1014. xfs_qm_dqput(dqp);
  1015. }
  1016. /*
  1017. * This is the dquot flushing I/O completion routine. It is called
  1018. * from interrupt level when the buffer containing the dquot is
  1019. * flushed to disk. It is responsible for removing the dquot logitem
  1020. * from the AIL if it has not been re-logged, and unlocking the dquot's
  1021. * flush lock. This behavior is very similar to that of inodes..
  1022. */
  1023. STATIC void
  1024. xfs_qm_dqflush_done(
  1025. struct xfs_buf *bp,
  1026. struct xfs_log_item *lip)
  1027. {
  1028. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  1029. xfs_dquot_t *dqp = qip->qli_dquot;
  1030. struct xfs_ail *ailp = lip->li_ailp;
  1031. /*
  1032. * We only want to pull the item from the AIL if its
  1033. * location in the log has not changed since we started the flush.
  1034. * Thus, we only bother if the dquot's lsn has
  1035. * not changed. First we check the lsn outside the lock
  1036. * since it's cheaper, and then we recheck while
  1037. * holding the lock before removing the dquot from the AIL.
  1038. */
  1039. if ((lip->li_flags & XFS_LI_IN_AIL) &&
  1040. lip->li_lsn == qip->qli_flush_lsn) {
  1041. /* xfs_trans_ail_delete() drops the AIL lock. */
  1042. spin_lock(&ailp->xa_lock);
  1043. if (lip->li_lsn == qip->qli_flush_lsn)
  1044. xfs_trans_ail_delete(ailp, lip);
  1045. else
  1046. spin_unlock(&ailp->xa_lock);
  1047. }
  1048. /*
  1049. * Release the dq's flush lock since we're done with it.
  1050. */
  1051. xfs_dqfunlock(dqp);
  1052. }
  1053. /*
  1054. * Write a modified dquot to disk.
  1055. * The dquot must be locked and the flush lock too taken by caller.
  1056. * The flush lock will not be unlocked until the dquot reaches the disk,
  1057. * but the dquot is free to be unlocked and modified by the caller
  1058. * in the interim. Dquot is still locked on return. This behavior is
  1059. * identical to that of inodes.
  1060. */
  1061. int
  1062. xfs_qm_dqflush(
  1063. xfs_dquot_t *dqp,
  1064. uint flags)
  1065. {
  1066. xfs_mount_t *mp;
  1067. xfs_buf_t *bp;
  1068. xfs_disk_dquot_t *ddqp;
  1069. int error;
  1070. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1071. ASSERT(!completion_done(&dqp->q_flush));
  1072. trace_xfs_dqflush(dqp);
  1073. /*
  1074. * If not dirty, or it's pinned and we are not supposed to
  1075. * block, nada.
  1076. */
  1077. if (!XFS_DQ_IS_DIRTY(dqp) ||
  1078. (!(flags & SYNC_WAIT) && atomic_read(&dqp->q_pincount) > 0)) {
  1079. xfs_dqfunlock(dqp);
  1080. return 0;
  1081. }
  1082. xfs_qm_dqunpin_wait(dqp);
  1083. /*
  1084. * This may have been unpinned because the filesystem is shutting
  1085. * down forcibly. If that's the case we must not write this dquot
  1086. * to disk, because the log record didn't make it to disk!
  1087. */
  1088. if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
  1089. dqp->dq_flags &= ~(XFS_DQ_DIRTY);
  1090. xfs_dqfunlock(dqp);
  1091. return XFS_ERROR(EIO);
  1092. }
  1093. /*
  1094. * Get the buffer containing the on-disk dquot
  1095. * We don't need a transaction envelope because we know that the
  1096. * the ondisk-dquot has already been allocated for.
  1097. */
  1098. if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
  1099. ASSERT(error != ENOENT);
  1100. /*
  1101. * Quotas could have gotten turned off (ESRCH)
  1102. */
  1103. xfs_dqfunlock(dqp);
  1104. return (error);
  1105. }
  1106. if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
  1107. 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
  1108. xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
  1109. return XFS_ERROR(EIO);
  1110. }
  1111. /* This is the only portion of data that needs to persist */
  1112. memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
  1113. /*
  1114. * Clear the dirty field and remember the flush lsn for later use.
  1115. */
  1116. dqp->dq_flags &= ~(XFS_DQ_DIRTY);
  1117. mp = dqp->q_mount;
  1118. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  1119. &dqp->q_logitem.qli_item.li_lsn);
  1120. /*
  1121. * Attach an iodone routine so that we can remove this dquot from the
  1122. * AIL and release the flush lock once the dquot is synced to disk.
  1123. */
  1124. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  1125. &dqp->q_logitem.qli_item);
  1126. /*
  1127. * If the buffer is pinned then push on the log so we won't
  1128. * get stuck waiting in the write for too long.
  1129. */
  1130. if (XFS_BUF_ISPINNED(bp)) {
  1131. trace_xfs_dqflush_force(dqp);
  1132. xfs_log_force(mp, 0);
  1133. }
  1134. if (flags & SYNC_WAIT)
  1135. error = xfs_bwrite(mp, bp);
  1136. else
  1137. xfs_bdwrite(mp, bp);
  1138. trace_xfs_dqflush_done(dqp);
  1139. /*
  1140. * dqp is still locked, but caller is free to unlock it now.
  1141. */
  1142. return error;
  1143. }
  1144. int
  1145. xfs_qm_dqlock_nowait(
  1146. xfs_dquot_t *dqp)
  1147. {
  1148. return mutex_trylock(&dqp->q_qlock);
  1149. }
  1150. void
  1151. xfs_dqlock(
  1152. xfs_dquot_t *dqp)
  1153. {
  1154. mutex_lock(&dqp->q_qlock);
  1155. }
  1156. void
  1157. xfs_dqunlock(
  1158. xfs_dquot_t *dqp)
  1159. {
  1160. mutex_unlock(&(dqp->q_qlock));
  1161. if (dqp->q_logitem.qli_dquot == dqp) {
  1162. /* Once was dqp->q_mount, but might just have been cleared */
  1163. xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
  1164. (xfs_log_item_t*)&(dqp->q_logitem));
  1165. }
  1166. }
  1167. void
  1168. xfs_dqunlock_nonotify(
  1169. xfs_dquot_t *dqp)
  1170. {
  1171. mutex_unlock(&(dqp->q_qlock));
  1172. }
  1173. /*
  1174. * Lock two xfs_dquot structures.
  1175. *
  1176. * To avoid deadlocks we always lock the quota structure with
  1177. * the lowerd id first.
  1178. */
  1179. void
  1180. xfs_dqlock2(
  1181. xfs_dquot_t *d1,
  1182. xfs_dquot_t *d2)
  1183. {
  1184. if (d1 && d2) {
  1185. ASSERT(d1 != d2);
  1186. if (be32_to_cpu(d1->q_core.d_id) >
  1187. be32_to_cpu(d2->q_core.d_id)) {
  1188. mutex_lock(&d2->q_qlock);
  1189. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  1190. } else {
  1191. mutex_lock(&d1->q_qlock);
  1192. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  1193. }
  1194. } else if (d1) {
  1195. mutex_lock(&d1->q_qlock);
  1196. } else if (d2) {
  1197. mutex_lock(&d2->q_qlock);
  1198. }
  1199. }
  1200. /*
  1201. * Take a dquot out of the mount's dqlist as well as the hashlist.
  1202. * This is called via unmount as well as quotaoff, and the purge
  1203. * will always succeed unless there are soft (temp) references
  1204. * outstanding.
  1205. *
  1206. * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
  1207. * that we're returning! XXXsup - not cool.
  1208. */
  1209. /* ARGSUSED */
  1210. int
  1211. xfs_qm_dqpurge(
  1212. xfs_dquot_t *dqp)
  1213. {
  1214. xfs_dqhash_t *qh = dqp->q_hash;
  1215. xfs_mount_t *mp = dqp->q_mount;
  1216. ASSERT(mutex_is_locked(&mp->m_quotainfo->qi_dqlist_lock));
  1217. ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
  1218. xfs_dqlock(dqp);
  1219. /*
  1220. * We really can't afford to purge a dquot that is
  1221. * referenced, because these are hard refs.
  1222. * It shouldn't happen in general because we went thru _all_ inodes in
  1223. * dqrele_all_inodes before calling this and didn't let the mountlock go.
  1224. * However it is possible that we have dquots with temporary
  1225. * references that are not attached to an inode. e.g. see xfs_setattr().
  1226. */
  1227. if (dqp->q_nrefs != 0) {
  1228. xfs_dqunlock(dqp);
  1229. mutex_unlock(&dqp->q_hash->qh_lock);
  1230. return (1);
  1231. }
  1232. ASSERT(!list_empty(&dqp->q_freelist));
  1233. /*
  1234. * If we're turning off quotas, we have to make sure that, for
  1235. * example, we don't delete quota disk blocks while dquots are
  1236. * in the process of getting written to those disk blocks.
  1237. * This dquot might well be on AIL, and we can't leave it there
  1238. * if we're turning off quotas. Basically, we need this flush
  1239. * lock, and are willing to block on it.
  1240. */
  1241. if (!xfs_dqflock_nowait(dqp)) {
  1242. /*
  1243. * Block on the flush lock after nudging dquot buffer,
  1244. * if it is incore.
  1245. */
  1246. xfs_qm_dqflock_pushbuf_wait(dqp);
  1247. }
  1248. /*
  1249. * XXXIf we're turning this type of quotas off, we don't care
  1250. * about the dirty metadata sitting in this dquot. OTOH, if
  1251. * we're unmounting, we do care, so we flush it and wait.
  1252. */
  1253. if (XFS_DQ_IS_DIRTY(dqp)) {
  1254. int error;
  1255. /* dqflush unlocks dqflock */
  1256. /*
  1257. * Given that dqpurge is a very rare occurrence, it is OK
  1258. * that we're holding the hashlist and mplist locks
  1259. * across the disk write. But, ... XXXsup
  1260. *
  1261. * We don't care about getting disk errors here. We need
  1262. * to purge this dquot anyway, so we go ahead regardless.
  1263. */
  1264. error = xfs_qm_dqflush(dqp, SYNC_WAIT);
  1265. if (error)
  1266. xfs_fs_cmn_err(CE_WARN, mp,
  1267. "xfs_qm_dqpurge: dquot %p flush failed", dqp);
  1268. xfs_dqflock(dqp);
  1269. }
  1270. ASSERT(atomic_read(&dqp->q_pincount) == 0);
  1271. ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
  1272. !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
  1273. list_del_init(&dqp->q_hashlist);
  1274. qh->qh_version++;
  1275. list_del_init(&dqp->q_mplist);
  1276. mp->m_quotainfo->qi_dqreclaims++;
  1277. mp->m_quotainfo->qi_dquots--;
  1278. /*
  1279. * XXX Move this to the front of the freelist, if we can get the
  1280. * freelist lock.
  1281. */
  1282. ASSERT(!list_empty(&dqp->q_freelist));
  1283. dqp->q_mount = NULL;
  1284. dqp->q_hash = NULL;
  1285. dqp->dq_flags = XFS_DQ_INACTIVE;
  1286. memset(&dqp->q_core, 0, sizeof(dqp->q_core));
  1287. xfs_dqfunlock(dqp);
  1288. xfs_dqunlock(dqp);
  1289. mutex_unlock(&qh->qh_lock);
  1290. return (0);
  1291. }
  1292. #ifdef QUOTADEBUG
  1293. void
  1294. xfs_qm_dqprint(xfs_dquot_t *dqp)
  1295. {
  1296. cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
  1297. cmn_err(CE_DEBUG, "---- dquotID = %d",
  1298. (int)be32_to_cpu(dqp->q_core.d_id));
  1299. cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
  1300. cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
  1301. cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
  1302. cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
  1303. cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
  1304. be64_to_cpu(dqp->q_core.d_blk_hardlimit),
  1305. (int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  1306. cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
  1307. be64_to_cpu(dqp->q_core.d_blk_softlimit),
  1308. (int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
  1309. cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
  1310. be64_to_cpu(dqp->q_core.d_ino_hardlimit),
  1311. (int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
  1312. cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
  1313. be64_to_cpu(dqp->q_core.d_ino_softlimit),
  1314. (int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
  1315. cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
  1316. be64_to_cpu(dqp->q_core.d_bcount),
  1317. (int)be64_to_cpu(dqp->q_core.d_bcount));
  1318. cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
  1319. be64_to_cpu(dqp->q_core.d_icount),
  1320. (int)be64_to_cpu(dqp->q_core.d_icount));
  1321. cmn_err(CE_DEBUG, "---- btimer = %d",
  1322. (int)be32_to_cpu(dqp->q_core.d_btimer));
  1323. cmn_err(CE_DEBUG, "---- itimer = %d",
  1324. (int)be32_to_cpu(dqp->q_core.d_itimer));
  1325. cmn_err(CE_DEBUG, "---------------------------");
  1326. }
  1327. #endif
  1328. /*
  1329. * Give the buffer a little push if it is incore and
  1330. * wait on the flush lock.
  1331. */
  1332. void
  1333. xfs_qm_dqflock_pushbuf_wait(
  1334. xfs_dquot_t *dqp)
  1335. {
  1336. xfs_mount_t *mp = dqp->q_mount;
  1337. xfs_buf_t *bp;
  1338. /*
  1339. * Check to see if the dquot has been flushed delayed
  1340. * write. If so, grab its buffer and send it
  1341. * out immediately. We'll be able to acquire
  1342. * the flush lock when the I/O completes.
  1343. */
  1344. bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
  1345. mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
  1346. if (!bp)
  1347. goto out_lock;
  1348. if (XFS_BUF_ISDELAYWRITE(bp)) {
  1349. if (XFS_BUF_ISPINNED(bp))
  1350. xfs_log_force(mp, 0);
  1351. xfs_buf_delwri_promote(bp);
  1352. wake_up_process(bp->b_target->bt_task);
  1353. }
  1354. xfs_buf_relse(bp);
  1355. out_lock:
  1356. xfs_dqflock(dqp);
  1357. }