xfs_dquot.c 39 KB

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