xfs_dquot.c 41 KB

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