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