xfs_dquot.c 41 KB

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