xfs_dquot.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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 DEBUG
  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_islocked(bp));
  295. d = bp->b_addr;
  296. /*
  297. * ID of the first dquot in the block - id's are zero based.
  298. */
  299. curid = id - (id % q->qi_dqperchunk);
  300. ASSERT(curid >= 0);
  301. memset(d, 0, BBTOB(q->qi_dqchunklen));
  302. for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++)
  303. xfs_qm_dqinit_core(curid, type, d);
  304. xfs_trans_dquot_buf(tp, bp,
  305. (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
  306. ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
  307. XFS_BLF_GDQUOT_BUF)));
  308. xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
  309. }
  310. /*
  311. * Allocate a block and fill it with dquots.
  312. * This is called when the bmapi finds a hole.
  313. */
  314. STATIC int
  315. xfs_qm_dqalloc(
  316. xfs_trans_t **tpp,
  317. xfs_mount_t *mp,
  318. xfs_dquot_t *dqp,
  319. xfs_inode_t *quotip,
  320. xfs_fileoff_t offset_fsb,
  321. xfs_buf_t **O_bpp)
  322. {
  323. xfs_fsblock_t firstblock;
  324. xfs_bmap_free_t flist;
  325. xfs_bmbt_irec_t map;
  326. int nmaps, error, committed;
  327. xfs_buf_t *bp;
  328. xfs_trans_t *tp = *tpp;
  329. ASSERT(tp != NULL);
  330. trace_xfs_dqalloc(dqp);
  331. /*
  332. * Initialize the bmap freelist prior to calling bmapi code.
  333. */
  334. xfs_bmap_init(&flist, &firstblock);
  335. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  336. /*
  337. * Return if this type of quotas is turned off while we didn't
  338. * have an inode lock
  339. */
  340. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  341. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  342. return (ESRCH);
  343. }
  344. xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
  345. nmaps = 1;
  346. error = xfs_bmapi_write(tp, quotip, offset_fsb,
  347. XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
  348. &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
  349. &map, &nmaps, &flist);
  350. if (error)
  351. goto error0;
  352. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  353. ASSERT(nmaps == 1);
  354. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  355. (map.br_startblock != HOLESTARTBLOCK));
  356. /*
  357. * Keep track of the blkno to save a lookup later
  358. */
  359. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  360. /* now we can just get the buffer (there's nothing to read yet) */
  361. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  362. dqp->q_blkno,
  363. mp->m_quotainfo->qi_dqchunklen,
  364. 0);
  365. error = xfs_buf_geterror(bp);
  366. if (error)
  367. goto error1;
  368. /*
  369. * Make a chunk of dquots out of this buffer and log
  370. * the entire thing.
  371. */
  372. xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
  373. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  374. /*
  375. * xfs_bmap_finish() may commit the current transaction and
  376. * start a second transaction if the freelist is not empty.
  377. *
  378. * Since we still want to modify this buffer, we need to
  379. * ensure that the buffer is not released on commit of
  380. * the first transaction and ensure the buffer is added to the
  381. * second transaction.
  382. *
  383. * If there is only one transaction then don't stop the buffer
  384. * from being released when it commits later on.
  385. */
  386. xfs_trans_bhold(tp, bp);
  387. if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
  388. goto error1;
  389. }
  390. if (committed) {
  391. tp = *tpp;
  392. xfs_trans_bjoin(tp, bp);
  393. } else {
  394. xfs_trans_bhold_release(tp, bp);
  395. }
  396. *O_bpp = bp;
  397. return 0;
  398. error1:
  399. xfs_bmap_cancel(&flist);
  400. error0:
  401. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  402. return (error);
  403. }
  404. /*
  405. * Maps a dquot to the buffer containing its on-disk version.
  406. * This returns a ptr to the buffer containing the on-disk dquot
  407. * in the bpp param, and a ptr to the on-disk dquot within that buffer
  408. */
  409. STATIC int
  410. xfs_qm_dqtobp(
  411. xfs_trans_t **tpp,
  412. xfs_dquot_t *dqp,
  413. xfs_disk_dquot_t **O_ddpp,
  414. xfs_buf_t **O_bpp,
  415. uint flags)
  416. {
  417. xfs_bmbt_irec_t map;
  418. int nmaps = 1, error;
  419. xfs_buf_t *bp;
  420. xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp);
  421. xfs_mount_t *mp = dqp->q_mount;
  422. xfs_disk_dquot_t *ddq;
  423. xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
  424. xfs_trans_t *tp = (tpp ? *tpp : NULL);
  425. dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
  426. xfs_ilock(quotip, XFS_ILOCK_SHARED);
  427. if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
  428. /*
  429. * Return if this type of quotas is turned off while we
  430. * didn't have the quota inode lock.
  431. */
  432. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  433. return ESRCH;
  434. }
  435. /*
  436. * Find the block map; no allocations yet
  437. */
  438. error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
  439. XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
  440. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  441. if (error)
  442. return error;
  443. ASSERT(nmaps == 1);
  444. ASSERT(map.br_blockcount == 1);
  445. /*
  446. * Offset of dquot in the (fixed sized) dquot chunk.
  447. */
  448. dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
  449. sizeof(xfs_dqblk_t);
  450. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  451. if (map.br_startblock == HOLESTARTBLOCK) {
  452. /*
  453. * We don't allocate unless we're asked to
  454. */
  455. if (!(flags & XFS_QMOPT_DQALLOC))
  456. return ENOENT;
  457. ASSERT(tp);
  458. error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
  459. dqp->q_fileoffset, &bp);
  460. if (error)
  461. return error;
  462. tp = *tpp;
  463. } else {
  464. trace_xfs_dqtobp_read(dqp);
  465. /*
  466. * store the blkno etc so that we don't have to do the
  467. * mapping all the time
  468. */
  469. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  470. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  471. dqp->q_blkno,
  472. mp->m_quotainfo->qi_dqchunklen,
  473. 0, &bp);
  474. if (error || !bp)
  475. return XFS_ERROR(error);
  476. }
  477. ASSERT(xfs_buf_islocked(bp));
  478. /*
  479. * calculate the location of the dquot inside the buffer.
  480. */
  481. ddq = bp->b_addr + dqp->q_bufoffset;
  482. /*
  483. * A simple sanity check in case we got a corrupted dquot...
  484. */
  485. error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
  486. flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
  487. "dqtobp");
  488. if (error) {
  489. if (!(flags & XFS_QMOPT_DQREPAIR)) {
  490. xfs_trans_brelse(tp, bp);
  491. return XFS_ERROR(EIO);
  492. }
  493. }
  494. *O_bpp = bp;
  495. *O_ddpp = ddq;
  496. return (0);
  497. }
  498. /*
  499. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  500. * and release the buffer immediately.
  501. *
  502. */
  503. /* ARGSUSED */
  504. STATIC int
  505. xfs_qm_dqread(
  506. xfs_trans_t **tpp,
  507. xfs_dqid_t id,
  508. xfs_dquot_t *dqp, /* dquot to get filled in */
  509. uint flags)
  510. {
  511. xfs_disk_dquot_t *ddqp;
  512. xfs_buf_t *bp;
  513. int error;
  514. xfs_trans_t *tp;
  515. ASSERT(tpp);
  516. trace_xfs_dqread(dqp);
  517. /*
  518. * get a pointer to the on-disk dquot and the buffer containing it
  519. * dqp already knows its own type (GROUP/USER).
  520. */
  521. if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
  522. return (error);
  523. }
  524. tp = *tpp;
  525. /* copy everything from disk dquot to the incore dquot */
  526. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  527. ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
  528. xfs_qm_dquot_logitem_init(dqp);
  529. /*
  530. * Reservation counters are defined as reservation plus current usage
  531. * to avoid having to add every time.
  532. */
  533. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  534. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  535. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  536. /* Mark the buf so that this will stay incore a little longer */
  537. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  538. /*
  539. * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
  540. * So we need to release with xfs_trans_brelse().
  541. * The strategy here is identical to that of inodes; we lock
  542. * the dquot in xfs_qm_dqget() before making it accessible to
  543. * others. This is because dquots, like inodes, need a good level of
  544. * concurrency, and we don't want to take locks on the entire buffers
  545. * for dquot accesses.
  546. * Note also that the dquot buffer may even be dirty at this point, if
  547. * this particular dquot was repaired. We still aren't afraid to
  548. * brelse it because we have the changes incore.
  549. */
  550. ASSERT(xfs_buf_islocked(bp));
  551. xfs_trans_brelse(tp, bp);
  552. return (error);
  553. }
  554. /*
  555. * allocate an incore dquot from the kernel heap,
  556. * and fill its core with quota information kept on disk.
  557. * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
  558. * if it wasn't already allocated.
  559. */
  560. STATIC int
  561. xfs_qm_idtodq(
  562. xfs_mount_t *mp,
  563. xfs_dqid_t id, /* gid or uid, depending on type */
  564. uint type, /* UDQUOT or GDQUOT */
  565. uint flags, /* DQALLOC, DQREPAIR */
  566. xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
  567. {
  568. xfs_dquot_t *dqp;
  569. int error;
  570. xfs_trans_t *tp;
  571. int cancelflags=0;
  572. dqp = xfs_qm_dqinit(mp, id, type);
  573. tp = NULL;
  574. if (flags & XFS_QMOPT_DQALLOC) {
  575. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
  576. error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
  577. XFS_WRITE_LOG_RES(mp) +
  578. BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 +
  579. 128,
  580. 0,
  581. XFS_TRANS_PERM_LOG_RES,
  582. XFS_WRITE_LOG_COUNT);
  583. if (error) {
  584. cancelflags = 0;
  585. goto error0;
  586. }
  587. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  588. }
  589. /*
  590. * Read it from disk; xfs_dqread() takes care of
  591. * all the necessary initialization of dquot's fields (locks, etc)
  592. */
  593. if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
  594. /*
  595. * This can happen if quotas got turned off (ESRCH),
  596. * or if the dquot didn't exist on disk and we ask to
  597. * allocate (ENOENT).
  598. */
  599. trace_xfs_dqread_fail(dqp);
  600. cancelflags |= XFS_TRANS_ABORT;
  601. goto error0;
  602. }
  603. if (tp) {
  604. if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
  605. goto error1;
  606. }
  607. *O_dqpp = dqp;
  608. return (0);
  609. error0:
  610. ASSERT(error);
  611. if (tp)
  612. xfs_trans_cancel(tp, cancelflags);
  613. error1:
  614. xfs_qm_dqdestroy(dqp);
  615. *O_dqpp = NULL;
  616. return (error);
  617. }
  618. /*
  619. * Lookup a dquot in the incore dquot hashtable. We keep two separate
  620. * hashtables for user and group dquots; and, these are global tables
  621. * inside the XQM, not per-filesystem tables.
  622. * The hash chain must be locked by caller, and it is left locked
  623. * on return. Returning dquot is locked.
  624. */
  625. STATIC int
  626. xfs_qm_dqlookup(
  627. xfs_mount_t *mp,
  628. xfs_dqid_t id,
  629. xfs_dqhash_t *qh,
  630. xfs_dquot_t **O_dqpp)
  631. {
  632. xfs_dquot_t *dqp;
  633. uint flist_locked;
  634. ASSERT(mutex_is_locked(&qh->qh_lock));
  635. flist_locked = B_FALSE;
  636. /*
  637. * Traverse the hashchain looking for a match
  638. */
  639. list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
  640. /*
  641. * We already have the hashlock. We don't need the
  642. * dqlock to look at the id field of the dquot, since the
  643. * id can't be modified without the hashlock anyway.
  644. */
  645. if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
  646. trace_xfs_dqlookup_found(dqp);
  647. /*
  648. * All in core dquots must be on the dqlist of mp
  649. */
  650. ASSERT(!list_empty(&dqp->q_mplist));
  651. xfs_dqlock(dqp);
  652. if (dqp->q_nrefs == 0) {
  653. ASSERT(!list_empty(&dqp->q_freelist));
  654. if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
  655. trace_xfs_dqlookup_want(dqp);
  656. /*
  657. * We may have raced with dqreclaim_one()
  658. * (and lost). So, flag that we don't
  659. * want the dquot to be reclaimed.
  660. */
  661. dqp->dq_flags |= XFS_DQ_WANT;
  662. xfs_dqunlock(dqp);
  663. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  664. xfs_dqlock(dqp);
  665. dqp->dq_flags &= ~(XFS_DQ_WANT);
  666. }
  667. flist_locked = B_TRUE;
  668. }
  669. /*
  670. * id couldn't have changed; we had the hashlock all
  671. * along
  672. */
  673. ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
  674. if (flist_locked) {
  675. if (dqp->q_nrefs != 0) {
  676. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  677. flist_locked = B_FALSE;
  678. } else {
  679. /* take it off the freelist */
  680. trace_xfs_dqlookup_freelist(dqp);
  681. list_del_init(&dqp->q_freelist);
  682. xfs_Gqm->qm_dqfrlist_cnt--;
  683. }
  684. }
  685. XFS_DQHOLD(dqp);
  686. if (flist_locked)
  687. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  688. /*
  689. * move the dquot to the front of the hashchain
  690. */
  691. ASSERT(mutex_is_locked(&qh->qh_lock));
  692. list_move(&dqp->q_hashlist, &qh->qh_list);
  693. trace_xfs_dqlookup_done(dqp);
  694. *O_dqpp = dqp;
  695. return 0;
  696. }
  697. }
  698. *O_dqpp = NULL;
  699. ASSERT(mutex_is_locked(&qh->qh_lock));
  700. return (1);
  701. }
  702. /*
  703. * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
  704. * a locked dquot, doing an allocation (if requested) as needed.
  705. * When both an inode and an id are given, the inode's id takes precedence.
  706. * That is, if the id changes while we don't hold the ilock inside this
  707. * function, the new dquot is returned, not necessarily the one requested
  708. * in the id argument.
  709. */
  710. int
  711. xfs_qm_dqget(
  712. xfs_mount_t *mp,
  713. xfs_inode_t *ip, /* locked inode (optional) */
  714. xfs_dqid_t id, /* uid/projid/gid depending on type */
  715. uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
  716. uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
  717. xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
  718. {
  719. xfs_dquot_t *dqp;
  720. xfs_dqhash_t *h;
  721. uint version;
  722. int error;
  723. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  724. if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
  725. (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
  726. (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
  727. return (ESRCH);
  728. }
  729. h = XFS_DQ_HASH(mp, id, type);
  730. #ifdef DEBUG
  731. if (xfs_do_dqerror) {
  732. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  733. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  734. xfs_debug(mp, "Returning error in dqget");
  735. return (EIO);
  736. }
  737. }
  738. #endif
  739. again:
  740. #ifdef DEBUG
  741. ASSERT(type == XFS_DQ_USER ||
  742. type == XFS_DQ_PROJ ||
  743. type == XFS_DQ_GROUP);
  744. if (ip) {
  745. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  746. if (type == XFS_DQ_USER)
  747. ASSERT(ip->i_udquot == NULL);
  748. else
  749. ASSERT(ip->i_gdquot == NULL);
  750. }
  751. #endif
  752. mutex_lock(&h->qh_lock);
  753. /*
  754. * Look in the cache (hashtable).
  755. * The chain is kept locked during lookup.
  756. */
  757. if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
  758. XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
  759. /*
  760. * The dquot was found, moved to the front of the chain,
  761. * taken off the freelist if it was on it, and locked
  762. * at this point. Just unlock the hashchain and return.
  763. */
  764. ASSERT(*O_dqpp);
  765. ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
  766. mutex_unlock(&h->qh_lock);
  767. trace_xfs_dqget_hit(*O_dqpp);
  768. return (0); /* success */
  769. }
  770. XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
  771. /*
  772. * Dquot cache miss. We don't want to keep the inode lock across
  773. * a (potential) disk read. Also we don't want to deal with the lock
  774. * ordering between quotainode and this inode. OTOH, dropping the inode
  775. * lock here means dealing with a chown that can happen before
  776. * we re-acquire the lock.
  777. */
  778. if (ip)
  779. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  780. /*
  781. * Save the hashchain version stamp, and unlock the chain, so that
  782. * we don't keep the lock across a disk read
  783. */
  784. version = h->qh_version;
  785. mutex_unlock(&h->qh_lock);
  786. /*
  787. * Allocate the dquot on the kernel heap, and read the ondisk
  788. * portion off the disk. Also, do all the necessary initialization
  789. * This can return ENOENT if dquot didn't exist on disk and we didn't
  790. * ask it to allocate; ESRCH if quotas got turned off suddenly.
  791. */
  792. if ((error = xfs_qm_idtodq(mp, id, type,
  793. flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
  794. XFS_QMOPT_DOWARN),
  795. &dqp))) {
  796. if (ip)
  797. xfs_ilock(ip, XFS_ILOCK_EXCL);
  798. return (error);
  799. }
  800. /*
  801. * See if this is mount code calling to look at the overall quota limits
  802. * which are stored in the id == 0 user or group's dquot.
  803. * Since we may not have done a quotacheck by this point, just return
  804. * the dquot without attaching it to any hashtables, lists, etc, or even
  805. * taking a reference.
  806. * The caller must dqdestroy this once done.
  807. */
  808. if (flags & XFS_QMOPT_DQSUSER) {
  809. ASSERT(id == 0);
  810. ASSERT(! ip);
  811. goto dqret;
  812. }
  813. /*
  814. * Dquot lock comes after hashlock in the lock ordering
  815. */
  816. if (ip) {
  817. xfs_ilock(ip, XFS_ILOCK_EXCL);
  818. /*
  819. * A dquot could be attached to this inode by now, since
  820. * we had dropped the ilock.
  821. */
  822. if (type == XFS_DQ_USER) {
  823. if (!XFS_IS_UQUOTA_ON(mp)) {
  824. /* inode stays locked on return */
  825. xfs_qm_dqdestroy(dqp);
  826. return XFS_ERROR(ESRCH);
  827. }
  828. if (ip->i_udquot) {
  829. xfs_qm_dqdestroy(dqp);
  830. dqp = ip->i_udquot;
  831. xfs_dqlock(dqp);
  832. goto dqret;
  833. }
  834. } else {
  835. if (!XFS_IS_OQUOTA_ON(mp)) {
  836. /* inode stays locked on return */
  837. xfs_qm_dqdestroy(dqp);
  838. return XFS_ERROR(ESRCH);
  839. }
  840. if (ip->i_gdquot) {
  841. xfs_qm_dqdestroy(dqp);
  842. dqp = ip->i_gdquot;
  843. xfs_dqlock(dqp);
  844. goto dqret;
  845. }
  846. }
  847. }
  848. /*
  849. * Hashlock comes after ilock in lock order
  850. */
  851. mutex_lock(&h->qh_lock);
  852. if (version != h->qh_version) {
  853. xfs_dquot_t *tmpdqp;
  854. /*
  855. * Now, see if somebody else put the dquot in the
  856. * hashtable before us. This can happen because we didn't
  857. * keep the hashchain lock. We don't have to worry about
  858. * lock order between the two dquots here since dqp isn't
  859. * on any findable lists yet.
  860. */
  861. if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
  862. /*
  863. * Duplicate found. Just throw away the new dquot
  864. * and start over.
  865. */
  866. xfs_qm_dqput(tmpdqp);
  867. mutex_unlock(&h->qh_lock);
  868. xfs_qm_dqdestroy(dqp);
  869. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  870. goto again;
  871. }
  872. }
  873. /*
  874. * Put the dquot at the beginning of the hash-chain and mp's list
  875. * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
  876. */
  877. ASSERT(mutex_is_locked(&h->qh_lock));
  878. dqp->q_hash = h;
  879. list_add(&dqp->q_hashlist, &h->qh_list);
  880. h->qh_version++;
  881. /*
  882. * Attach this dquot to this filesystem's list of all dquots,
  883. * kept inside the mount structure in m_quotainfo field
  884. */
  885. mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
  886. /*
  887. * We return a locked dquot to the caller, with a reference taken
  888. */
  889. xfs_dqlock(dqp);
  890. dqp->q_nrefs = 1;
  891. list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
  892. mp->m_quotainfo->qi_dquots++;
  893. mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
  894. mutex_unlock(&h->qh_lock);
  895. dqret:
  896. ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
  897. trace_xfs_dqget_miss(dqp);
  898. *O_dqpp = dqp;
  899. return (0);
  900. }
  901. /*
  902. * Release a reference to the dquot (decrement ref-count)
  903. * and unlock it. If there is a group quota attached to this
  904. * dquot, carefully release that too without tripping over
  905. * deadlocks'n'stuff.
  906. */
  907. void
  908. xfs_qm_dqput(
  909. xfs_dquot_t *dqp)
  910. {
  911. xfs_dquot_t *gdqp;
  912. ASSERT(dqp->q_nrefs > 0);
  913. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  914. trace_xfs_dqput(dqp);
  915. if (dqp->q_nrefs != 1) {
  916. dqp->q_nrefs--;
  917. xfs_dqunlock(dqp);
  918. return;
  919. }
  920. /*
  921. * drop the dqlock and acquire the freelist and dqlock
  922. * in the right order; but try to get it out-of-order first
  923. */
  924. if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
  925. trace_xfs_dqput_wait(dqp);
  926. xfs_dqunlock(dqp);
  927. mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
  928. xfs_dqlock(dqp);
  929. }
  930. while (1) {
  931. gdqp = NULL;
  932. /* We can't depend on nrefs being == 1 here */
  933. if (--dqp->q_nrefs == 0) {
  934. trace_xfs_dqput_free(dqp);
  935. list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
  936. xfs_Gqm->qm_dqfrlist_cnt++;
  937. /*
  938. * If we just added a udquot to the freelist, then
  939. * we want to release the gdquot reference that
  940. * it (probably) has. Otherwise it'll keep the
  941. * gdquot from getting reclaimed.
  942. */
  943. if ((gdqp = dqp->q_gdquot)) {
  944. /*
  945. * Avoid a recursive dqput call
  946. */
  947. xfs_dqlock(gdqp);
  948. dqp->q_gdquot = NULL;
  949. }
  950. }
  951. xfs_dqunlock(dqp);
  952. /*
  953. * If we had a group quota inside the user quota as a hint,
  954. * release it now.
  955. */
  956. if (! gdqp)
  957. break;
  958. dqp = gdqp;
  959. }
  960. mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
  961. }
  962. /*
  963. * Release a dquot. Flush it if dirty, then dqput() it.
  964. * dquot must not be locked.
  965. */
  966. void
  967. xfs_qm_dqrele(
  968. xfs_dquot_t *dqp)
  969. {
  970. if (!dqp)
  971. return;
  972. trace_xfs_dqrele(dqp);
  973. xfs_dqlock(dqp);
  974. /*
  975. * We don't care to flush it if the dquot is dirty here.
  976. * That will create stutters that we want to avoid.
  977. * Instead we do a delayed write when we try to reclaim
  978. * a dirty dquot. Also xfs_sync will take part of the burden...
  979. */
  980. xfs_qm_dqput(dqp);
  981. }
  982. /*
  983. * This is the dquot flushing I/O completion routine. It is called
  984. * from interrupt level when the buffer containing the dquot is
  985. * flushed to disk. It is responsible for removing the dquot logitem
  986. * from the AIL if it has not been re-logged, and unlocking the dquot's
  987. * flush lock. This behavior is very similar to that of inodes..
  988. */
  989. STATIC void
  990. xfs_qm_dqflush_done(
  991. struct xfs_buf *bp,
  992. struct xfs_log_item *lip)
  993. {
  994. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  995. xfs_dquot_t *dqp = qip->qli_dquot;
  996. struct xfs_ail *ailp = lip->li_ailp;
  997. /*
  998. * We only want to pull the item from the AIL if its
  999. * location in the log has not changed since we started the flush.
  1000. * Thus, we only bother if the dquot's lsn has
  1001. * not changed. First we check the lsn outside the lock
  1002. * since it's cheaper, and then we recheck while
  1003. * holding the lock before removing the dquot from the AIL.
  1004. */
  1005. if ((lip->li_flags & XFS_LI_IN_AIL) &&
  1006. lip->li_lsn == qip->qli_flush_lsn) {
  1007. /* xfs_trans_ail_delete() drops the AIL lock. */
  1008. spin_lock(&ailp->xa_lock);
  1009. if (lip->li_lsn == qip->qli_flush_lsn)
  1010. xfs_trans_ail_delete(ailp, lip);
  1011. else
  1012. spin_unlock(&ailp->xa_lock);
  1013. }
  1014. /*
  1015. * Release the dq's flush lock since we're done with it.
  1016. */
  1017. xfs_dqfunlock(dqp);
  1018. }
  1019. /*
  1020. * Write a modified dquot to disk.
  1021. * The dquot must be locked and the flush lock too taken by caller.
  1022. * The flush lock will not be unlocked until the dquot reaches the disk,
  1023. * but the dquot is free to be unlocked and modified by the caller
  1024. * in the interim. Dquot is still locked on return. This behavior is
  1025. * identical to that of inodes.
  1026. */
  1027. int
  1028. xfs_qm_dqflush(
  1029. xfs_dquot_t *dqp,
  1030. uint flags)
  1031. {
  1032. struct xfs_mount *mp = dqp->q_mount;
  1033. struct xfs_buf *bp;
  1034. struct xfs_disk_dquot *ddqp;
  1035. int error;
  1036. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1037. ASSERT(!completion_done(&dqp->q_flush));
  1038. trace_xfs_dqflush(dqp);
  1039. /*
  1040. * If not dirty, or it's pinned and we are not supposed to block, nada.
  1041. */
  1042. if (!XFS_DQ_IS_DIRTY(dqp) ||
  1043. (!(flags & SYNC_WAIT) && atomic_read(&dqp->q_pincount) > 0)) {
  1044. xfs_dqfunlock(dqp);
  1045. return 0;
  1046. }
  1047. xfs_qm_dqunpin_wait(dqp);
  1048. /*
  1049. * This may have been unpinned because the filesystem is shutting
  1050. * down forcibly. If that's the case we must not write this dquot
  1051. * to disk, because the log record didn't make it to disk!
  1052. */
  1053. if (XFS_FORCED_SHUTDOWN(mp)) {
  1054. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  1055. xfs_dqfunlock(dqp);
  1056. return XFS_ERROR(EIO);
  1057. }
  1058. /*
  1059. * Get the buffer containing the on-disk dquot
  1060. */
  1061. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  1062. mp->m_quotainfo->qi_dqchunklen, 0, &bp);
  1063. if (error) {
  1064. ASSERT(error != ENOENT);
  1065. xfs_dqfunlock(dqp);
  1066. return error;
  1067. }
  1068. /*
  1069. * Calculate the location of the dquot inside the buffer.
  1070. */
  1071. ddqp = bp->b_addr + dqp->q_bufoffset;
  1072. /*
  1073. * A simple sanity check in case we got a corrupted dquot..
  1074. */
  1075. error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
  1076. XFS_QMOPT_DOWARN, "dqflush (incore copy)");
  1077. if (error) {
  1078. xfs_buf_relse(bp);
  1079. xfs_dqfunlock(dqp);
  1080. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  1081. return XFS_ERROR(EIO);
  1082. }
  1083. /* This is the only portion of data that needs to persist */
  1084. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  1085. /*
  1086. * Clear the dirty field and remember the flush lsn for later use.
  1087. */
  1088. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  1089. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  1090. &dqp->q_logitem.qli_item.li_lsn);
  1091. /*
  1092. * Attach an iodone routine so that we can remove this dquot from the
  1093. * AIL and release the flush lock once the dquot is synced to disk.
  1094. */
  1095. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  1096. &dqp->q_logitem.qli_item);
  1097. /*
  1098. * If the buffer is pinned then push on the log so we won't
  1099. * get stuck waiting in the write for too long.
  1100. */
  1101. if (xfs_buf_ispinned(bp)) {
  1102. trace_xfs_dqflush_force(dqp);
  1103. xfs_log_force(mp, 0);
  1104. }
  1105. if (flags & SYNC_WAIT)
  1106. error = xfs_bwrite(bp);
  1107. else
  1108. xfs_buf_delwri_queue(bp);
  1109. xfs_buf_relse(bp);
  1110. trace_xfs_dqflush_done(dqp);
  1111. /*
  1112. * dqp is still locked, but caller is free to unlock it now.
  1113. */
  1114. return error;
  1115. }
  1116. int
  1117. xfs_qm_dqlock_nowait(
  1118. xfs_dquot_t *dqp)
  1119. {
  1120. return mutex_trylock(&dqp->q_qlock);
  1121. }
  1122. void
  1123. xfs_dqlock(
  1124. xfs_dquot_t *dqp)
  1125. {
  1126. mutex_lock(&dqp->q_qlock);
  1127. }
  1128. void
  1129. xfs_dqunlock(
  1130. xfs_dquot_t *dqp)
  1131. {
  1132. mutex_unlock(&(dqp->q_qlock));
  1133. if (dqp->q_logitem.qli_dquot == dqp) {
  1134. /* Once was dqp->q_mount, but might just have been cleared */
  1135. xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
  1136. (xfs_log_item_t*)&(dqp->q_logitem));
  1137. }
  1138. }
  1139. void
  1140. xfs_dqunlock_nonotify(
  1141. xfs_dquot_t *dqp)
  1142. {
  1143. mutex_unlock(&(dqp->q_qlock));
  1144. }
  1145. /*
  1146. * Lock two xfs_dquot structures.
  1147. *
  1148. * To avoid deadlocks we always lock the quota structure with
  1149. * the lowerd id first.
  1150. */
  1151. void
  1152. xfs_dqlock2(
  1153. xfs_dquot_t *d1,
  1154. xfs_dquot_t *d2)
  1155. {
  1156. if (d1 && d2) {
  1157. ASSERT(d1 != d2);
  1158. if (be32_to_cpu(d1->q_core.d_id) >
  1159. be32_to_cpu(d2->q_core.d_id)) {
  1160. mutex_lock(&d2->q_qlock);
  1161. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  1162. } else {
  1163. mutex_lock(&d1->q_qlock);
  1164. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  1165. }
  1166. } else if (d1) {
  1167. mutex_lock(&d1->q_qlock);
  1168. } else if (d2) {
  1169. mutex_lock(&d2->q_qlock);
  1170. }
  1171. }
  1172. /*
  1173. * Take a dquot out of the mount's dqlist as well as the hashlist.
  1174. * This is called via unmount as well as quotaoff, and the purge
  1175. * will always succeed unless there are soft (temp) references
  1176. * outstanding.
  1177. *
  1178. * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
  1179. * that we're returning! XXXsup - not cool.
  1180. */
  1181. /* ARGSUSED */
  1182. int
  1183. xfs_qm_dqpurge(
  1184. xfs_dquot_t *dqp)
  1185. {
  1186. xfs_dqhash_t *qh = dqp->q_hash;
  1187. xfs_mount_t *mp = dqp->q_mount;
  1188. ASSERT(mutex_is_locked(&mp->m_quotainfo->qi_dqlist_lock));
  1189. ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
  1190. xfs_dqlock(dqp);
  1191. /*
  1192. * We really can't afford to purge a dquot that is
  1193. * referenced, because these are hard refs.
  1194. * It shouldn't happen in general because we went thru _all_ inodes in
  1195. * dqrele_all_inodes before calling this and didn't let the mountlock go.
  1196. * However it is possible that we have dquots with temporary
  1197. * references that are not attached to an inode. e.g. see xfs_setattr().
  1198. */
  1199. if (dqp->q_nrefs != 0) {
  1200. xfs_dqunlock(dqp);
  1201. mutex_unlock(&dqp->q_hash->qh_lock);
  1202. return (1);
  1203. }
  1204. ASSERT(!list_empty(&dqp->q_freelist));
  1205. /*
  1206. * If we're turning off quotas, we have to make sure that, for
  1207. * example, we don't delete quota disk blocks while dquots are
  1208. * in the process of getting written to those disk blocks.
  1209. * This dquot might well be on AIL, and we can't leave it there
  1210. * if we're turning off quotas. Basically, we need this flush
  1211. * lock, and are willing to block on it.
  1212. */
  1213. if (!xfs_dqflock_nowait(dqp)) {
  1214. /*
  1215. * Block on the flush lock after nudging dquot buffer,
  1216. * if it is incore.
  1217. */
  1218. xfs_qm_dqflock_pushbuf_wait(dqp);
  1219. }
  1220. /*
  1221. * XXXIf we're turning this type of quotas off, we don't care
  1222. * about the dirty metadata sitting in this dquot. OTOH, if
  1223. * we're unmounting, we do care, so we flush it and wait.
  1224. */
  1225. if (XFS_DQ_IS_DIRTY(dqp)) {
  1226. int error;
  1227. /* dqflush unlocks dqflock */
  1228. /*
  1229. * Given that dqpurge is a very rare occurrence, it is OK
  1230. * that we're holding the hashlist and mplist locks
  1231. * across the disk write. But, ... XXXsup
  1232. *
  1233. * We don't care about getting disk errors here. We need
  1234. * to purge this dquot anyway, so we go ahead regardless.
  1235. */
  1236. error = xfs_qm_dqflush(dqp, SYNC_WAIT);
  1237. if (error)
  1238. xfs_warn(mp, "%s: dquot %p flush failed",
  1239. __func__, dqp);
  1240. xfs_dqflock(dqp);
  1241. }
  1242. ASSERT(atomic_read(&dqp->q_pincount) == 0);
  1243. ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
  1244. !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
  1245. list_del_init(&dqp->q_hashlist);
  1246. qh->qh_version++;
  1247. list_del_init(&dqp->q_mplist);
  1248. mp->m_quotainfo->qi_dqreclaims++;
  1249. mp->m_quotainfo->qi_dquots--;
  1250. /*
  1251. * XXX Move this to the front of the freelist, if we can get the
  1252. * freelist lock.
  1253. */
  1254. ASSERT(!list_empty(&dqp->q_freelist));
  1255. dqp->q_mount = NULL;
  1256. dqp->q_hash = NULL;
  1257. dqp->dq_flags = XFS_DQ_INACTIVE;
  1258. memset(&dqp->q_core, 0, sizeof(dqp->q_core));
  1259. xfs_dqfunlock(dqp);
  1260. xfs_dqunlock(dqp);
  1261. mutex_unlock(&qh->qh_lock);
  1262. return (0);
  1263. }
  1264. /*
  1265. * Give the buffer a little push if it is incore and
  1266. * wait on the flush lock.
  1267. */
  1268. void
  1269. xfs_qm_dqflock_pushbuf_wait(
  1270. xfs_dquot_t *dqp)
  1271. {
  1272. xfs_mount_t *mp = dqp->q_mount;
  1273. xfs_buf_t *bp;
  1274. /*
  1275. * Check to see if the dquot has been flushed delayed
  1276. * write. If so, grab its buffer and send it
  1277. * out immediately. We'll be able to acquire
  1278. * the flush lock when the I/O completes.
  1279. */
  1280. bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
  1281. mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
  1282. if (!bp)
  1283. goto out_lock;
  1284. if (XFS_BUF_ISDELAYWRITE(bp)) {
  1285. if (xfs_buf_ispinned(bp))
  1286. xfs_log_force(mp, 0);
  1287. xfs_buf_delwri_promote(bp);
  1288. wake_up_process(bp->b_target->bt_task);
  1289. }
  1290. xfs_buf_relse(bp);
  1291. out_lock:
  1292. xfs_dqflock(dqp);
  1293. }