xfs_dquot.c 41 KB

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