xfs_dquot.c 40 KB

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