xfs_dquot.c 40 KB

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