xfs_dquot.c 38 KB

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