xfs_dquot.c 39 KB

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