xfs_dquot.c 40 KB

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