xfs_dquot.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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. return (ESRCH);
  832. }
  833. h = XFS_DQ_HASH(mp, id, type);
  834. #ifdef DEBUG
  835. if (xfs_do_dqerror) {
  836. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  837. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  838. cmn_err(CE_DEBUG, "Returning error in dqget");
  839. return (EIO);
  840. }
  841. }
  842. #endif
  843. again:
  844. #ifdef DEBUG
  845. ASSERT(type == XFS_DQ_USER ||
  846. type == XFS_DQ_PROJ ||
  847. type == XFS_DQ_GROUP);
  848. if (ip) {
  849. ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
  850. if (type == XFS_DQ_USER)
  851. ASSERT(ip->i_udquot == NULL);
  852. else
  853. ASSERT(ip->i_gdquot == NULL);
  854. }
  855. #endif
  856. XFS_DQ_HASH_LOCK(h);
  857. /*
  858. * Look in the cache (hashtable).
  859. * The chain is kept locked during lookup.
  860. */
  861. if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
  862. XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
  863. /*
  864. * The dquot was found, moved to the front of the chain,
  865. * taken off the freelist if it was on it, and locked
  866. * at this point. Just unlock the hashchain and return.
  867. */
  868. ASSERT(*O_dqpp);
  869. ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
  870. XFS_DQ_HASH_UNLOCK(h);
  871. xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
  872. return (0); /* success */
  873. }
  874. XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
  875. /*
  876. * Dquot cache miss. We don't want to keep the inode lock across
  877. * a (potential) disk read. Also we don't want to deal with the lock
  878. * ordering between quotainode and this inode. OTOH, dropping the inode
  879. * lock here means dealing with a chown that can happen before
  880. * we re-acquire the lock.
  881. */
  882. if (ip)
  883. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  884. /*
  885. * Save the hashchain version stamp, and unlock the chain, so that
  886. * we don't keep the lock across a disk read
  887. */
  888. version = h->qh_version;
  889. XFS_DQ_HASH_UNLOCK(h);
  890. /*
  891. * Allocate the dquot on the kernel heap, and read the ondisk
  892. * portion off the disk. Also, do all the necessary initialization
  893. * This can return ENOENT if dquot didn't exist on disk and we didn't
  894. * ask it to allocate; ESRCH if quotas got turned off suddenly.
  895. */
  896. if ((error = xfs_qm_idtodq(mp, id, type,
  897. flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
  898. XFS_QMOPT_DOWARN),
  899. &dqp))) {
  900. if (ip)
  901. xfs_ilock(ip, XFS_ILOCK_EXCL);
  902. return (error);
  903. }
  904. /*
  905. * See if this is mount code calling to look at the overall quota limits
  906. * which are stored in the id == 0 user or group's dquot.
  907. * Since we may not have done a quotacheck by this point, just return
  908. * the dquot without attaching it to any hashtables, lists, etc, or even
  909. * taking a reference.
  910. * The caller must dqdestroy this once done.
  911. */
  912. if (flags & XFS_QMOPT_DQSUSER) {
  913. ASSERT(id == 0);
  914. ASSERT(! ip);
  915. goto dqret;
  916. }
  917. /*
  918. * Dquot lock comes after hashlock in the lock ordering
  919. */
  920. if (ip) {
  921. xfs_ilock(ip, XFS_ILOCK_EXCL);
  922. if (! XFS_IS_DQTYPE_ON(mp, type)) {
  923. /* inode stays locked on return */
  924. xfs_qm_dqdestroy(dqp);
  925. return XFS_ERROR(ESRCH);
  926. }
  927. /*
  928. * A dquot could be attached to this inode by now, since
  929. * we had dropped the ilock.
  930. */
  931. if (type == XFS_DQ_USER) {
  932. if (ip->i_udquot) {
  933. xfs_qm_dqdestroy(dqp);
  934. dqp = ip->i_udquot;
  935. xfs_dqlock(dqp);
  936. goto dqret;
  937. }
  938. } else {
  939. if (ip->i_gdquot) {
  940. xfs_qm_dqdestroy(dqp);
  941. dqp = ip->i_gdquot;
  942. xfs_dqlock(dqp);
  943. goto dqret;
  944. }
  945. }
  946. }
  947. /*
  948. * Hashlock comes after ilock in lock order
  949. */
  950. XFS_DQ_HASH_LOCK(h);
  951. if (version != h->qh_version) {
  952. xfs_dquot_t *tmpdqp;
  953. /*
  954. * Now, see if somebody else put the dquot in the
  955. * hashtable before us. This can happen because we didn't
  956. * keep the hashchain lock. We don't have to worry about
  957. * lock order between the two dquots here since dqp isn't
  958. * on any findable lists yet.
  959. */
  960. if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
  961. /*
  962. * Duplicate found. Just throw away the new dquot
  963. * and start over.
  964. */
  965. xfs_qm_dqput(tmpdqp);
  966. XFS_DQ_HASH_UNLOCK(h);
  967. xfs_qm_dqdestroy(dqp);
  968. XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
  969. goto again;
  970. }
  971. }
  972. /*
  973. * Put the dquot at the beginning of the hash-chain and mp's list
  974. * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
  975. */
  976. ASSERT(XFS_DQ_IS_HASH_LOCKED(h));
  977. dqp->q_hash = h;
  978. XQM_HASHLIST_INSERT(h, dqp);
  979. /*
  980. * Attach this dquot to this filesystem's list of all dquots,
  981. * kept inside the mount structure in m_quotainfo field
  982. */
  983. xfs_qm_mplist_lock(mp);
  984. /*
  985. * We return a locked dquot to the caller, with a reference taken
  986. */
  987. xfs_dqlock(dqp);
  988. dqp->q_nrefs = 1;
  989. XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
  990. xfs_qm_mplist_unlock(mp);
  991. XFS_DQ_HASH_UNLOCK(h);
  992. dqret:
  993. ASSERT((ip == NULL) || XFS_ISLOCKED_INODE_EXCL(ip));
  994. xfs_dqtrace_entry(dqp, "DQGET DONE");
  995. *O_dqpp = dqp;
  996. return (0);
  997. }
  998. /*
  999. * Release a reference to the dquot (decrement ref-count)
  1000. * and unlock it. If there is a group quota attached to this
  1001. * dquot, carefully release that too without tripping over
  1002. * deadlocks'n'stuff.
  1003. */
  1004. void
  1005. xfs_qm_dqput(
  1006. xfs_dquot_t *dqp)
  1007. {
  1008. xfs_dquot_t *gdqp;
  1009. ASSERT(dqp->q_nrefs > 0);
  1010. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1011. xfs_dqtrace_entry(dqp, "DQPUT");
  1012. if (dqp->q_nrefs != 1) {
  1013. dqp->q_nrefs--;
  1014. xfs_dqunlock(dqp);
  1015. return;
  1016. }
  1017. /*
  1018. * drop the dqlock and acquire the freelist and dqlock
  1019. * in the right order; but try to get it out-of-order first
  1020. */
  1021. if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
  1022. xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
  1023. xfs_dqunlock(dqp);
  1024. xfs_qm_freelist_lock(xfs_Gqm);
  1025. xfs_dqlock(dqp);
  1026. }
  1027. while (1) {
  1028. gdqp = NULL;
  1029. /* We can't depend on nrefs being == 1 here */
  1030. if (--dqp->q_nrefs == 0) {
  1031. xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
  1032. /*
  1033. * insert at end of the freelist.
  1034. */
  1035. XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
  1036. /*
  1037. * If we just added a udquot to the freelist, then
  1038. * we want to release the gdquot reference that
  1039. * it (probably) has. Otherwise it'll keep the
  1040. * gdquot from getting reclaimed.
  1041. */
  1042. if ((gdqp = dqp->q_gdquot)) {
  1043. /*
  1044. * Avoid a recursive dqput call
  1045. */
  1046. xfs_dqlock(gdqp);
  1047. dqp->q_gdquot = NULL;
  1048. }
  1049. /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
  1050. "@@@@@++ Free list (after append) @@@@@+");
  1051. */
  1052. }
  1053. xfs_dqunlock(dqp);
  1054. /*
  1055. * If we had a group quota inside the user quota as a hint,
  1056. * release it now.
  1057. */
  1058. if (! gdqp)
  1059. break;
  1060. dqp = gdqp;
  1061. }
  1062. xfs_qm_freelist_unlock(xfs_Gqm);
  1063. }
  1064. /*
  1065. * Release a dquot. Flush it if dirty, then dqput() it.
  1066. * dquot must not be locked.
  1067. */
  1068. void
  1069. xfs_qm_dqrele(
  1070. xfs_dquot_t *dqp)
  1071. {
  1072. ASSERT(dqp);
  1073. xfs_dqtrace_entry(dqp, "DQRELE");
  1074. xfs_dqlock(dqp);
  1075. /*
  1076. * We don't care to flush it if the dquot is dirty here.
  1077. * That will create stutters that we want to avoid.
  1078. * Instead we do a delayed write when we try to reclaim
  1079. * a dirty dquot. Also xfs_sync will take part of the burden...
  1080. */
  1081. xfs_qm_dqput(dqp);
  1082. }
  1083. /*
  1084. * Write a modified dquot to disk.
  1085. * The dquot must be locked and the flush lock too taken by caller.
  1086. * The flush lock will not be unlocked until the dquot reaches the disk,
  1087. * but the dquot is free to be unlocked and modified by the caller
  1088. * in the interim. Dquot is still locked on return. This behavior is
  1089. * identical to that of inodes.
  1090. */
  1091. int
  1092. xfs_qm_dqflush(
  1093. xfs_dquot_t *dqp,
  1094. uint flags)
  1095. {
  1096. xfs_mount_t *mp;
  1097. xfs_buf_t *bp;
  1098. xfs_disk_dquot_t *ddqp;
  1099. int error;
  1100. SPLDECL(s);
  1101. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1102. ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp));
  1103. xfs_dqtrace_entry(dqp, "DQFLUSH");
  1104. /*
  1105. * If not dirty, nada.
  1106. */
  1107. if (!XFS_DQ_IS_DIRTY(dqp)) {
  1108. xfs_dqfunlock(dqp);
  1109. return (0);
  1110. }
  1111. /*
  1112. * Cant flush a pinned dquot. Wait for it.
  1113. */
  1114. xfs_qm_dqunpin_wait(dqp);
  1115. /*
  1116. * This may have been unpinned because the filesystem is shutting
  1117. * down forcibly. If that's the case we must not write this dquot
  1118. * to disk, because the log record didn't make it to disk!
  1119. */
  1120. if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
  1121. dqp->dq_flags &= ~(XFS_DQ_DIRTY);
  1122. xfs_dqfunlock(dqp);
  1123. return XFS_ERROR(EIO);
  1124. }
  1125. /*
  1126. * Get the buffer containing the on-disk dquot
  1127. * We don't need a transaction envelope because we know that the
  1128. * the ondisk-dquot has already been allocated for.
  1129. */
  1130. if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
  1131. xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
  1132. ASSERT(error != ENOENT);
  1133. /*
  1134. * Quotas could have gotten turned off (ESRCH)
  1135. */
  1136. xfs_dqfunlock(dqp);
  1137. return (error);
  1138. }
  1139. if (xfs_qm_dqcheck(&dqp->q_core, INT_GET(ddqp->d_id, ARCH_CONVERT),
  1140. 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
  1141. xfs_force_shutdown(dqp->q_mount, XFS_CORRUPT_INCORE);
  1142. return XFS_ERROR(EIO);
  1143. }
  1144. /* This is the only portion of data that needs to persist */
  1145. memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
  1146. /*
  1147. * Clear the dirty field and remember the flush lsn for later use.
  1148. */
  1149. dqp->dq_flags &= ~(XFS_DQ_DIRTY);
  1150. mp = dqp->q_mount;
  1151. /* lsn is 64 bits */
  1152. AIL_LOCK(mp, s);
  1153. dqp->q_logitem.qli_flush_lsn = dqp->q_logitem.qli_item.li_lsn;
  1154. AIL_UNLOCK(mp, s);
  1155. /*
  1156. * Attach an iodone routine so that we can remove this dquot from the
  1157. * AIL and release the flush lock once the dquot is synced to disk.
  1158. */
  1159. xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
  1160. xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
  1161. /*
  1162. * If the buffer is pinned then push on the log so we won't
  1163. * get stuck waiting in the write for too long.
  1164. */
  1165. if (XFS_BUF_ISPINNED(bp)) {
  1166. xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
  1167. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  1168. }
  1169. if (flags & XFS_QMOPT_DELWRI) {
  1170. xfs_bdwrite(mp, bp);
  1171. } else if (flags & XFS_QMOPT_ASYNC) {
  1172. xfs_bawrite(mp, bp);
  1173. } else {
  1174. error = xfs_bwrite(mp, bp);
  1175. }
  1176. xfs_dqtrace_entry(dqp, "DQFLUSH END");
  1177. /*
  1178. * dqp is still locked, but caller is free to unlock it now.
  1179. */
  1180. return (error);
  1181. }
  1182. /*
  1183. * This is the dquot flushing I/O completion routine. It is called
  1184. * from interrupt level when the buffer containing the dquot is
  1185. * flushed to disk. It is responsible for removing the dquot logitem
  1186. * from the AIL if it has not been re-logged, and unlocking the dquot's
  1187. * flush lock. This behavior is very similar to that of inodes..
  1188. */
  1189. /*ARGSUSED*/
  1190. STATIC void
  1191. xfs_qm_dqflush_done(
  1192. xfs_buf_t *bp,
  1193. xfs_dq_logitem_t *qip)
  1194. {
  1195. xfs_dquot_t *dqp;
  1196. SPLDECL(s);
  1197. dqp = qip->qli_dquot;
  1198. /*
  1199. * We only want to pull the item from the AIL if its
  1200. * location in the log has not changed since we started the flush.
  1201. * Thus, we only bother if the dquot's lsn has
  1202. * not changed. First we check the lsn outside the lock
  1203. * since it's cheaper, and then we recheck while
  1204. * holding the lock before removing the dquot from the AIL.
  1205. */
  1206. if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
  1207. qip->qli_item.li_lsn == qip->qli_flush_lsn) {
  1208. AIL_LOCK(dqp->q_mount, s);
  1209. /*
  1210. * xfs_trans_delete_ail() drops the AIL lock.
  1211. */
  1212. if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
  1213. xfs_trans_delete_ail(dqp->q_mount,
  1214. (xfs_log_item_t*)qip, s);
  1215. else
  1216. AIL_UNLOCK(dqp->q_mount, s);
  1217. }
  1218. /*
  1219. * Release the dq's flush lock since we're done with it.
  1220. */
  1221. xfs_dqfunlock(dqp);
  1222. }
  1223. int
  1224. xfs_qm_dqflock_nowait(
  1225. xfs_dquot_t *dqp)
  1226. {
  1227. int locked;
  1228. locked = cpsema(&((dqp)->q_flock));
  1229. /* XXX ifdef these out */
  1230. if (locked)
  1231. (dqp)->dq_flags |= XFS_DQ_FLOCKED;
  1232. return (locked);
  1233. }
  1234. int
  1235. xfs_qm_dqlock_nowait(
  1236. xfs_dquot_t *dqp)
  1237. {
  1238. return (mutex_trylock(&((dqp)->q_qlock)));
  1239. }
  1240. void
  1241. xfs_dqlock(
  1242. xfs_dquot_t *dqp)
  1243. {
  1244. mutex_lock(&(dqp->q_qlock), PINOD);
  1245. }
  1246. void
  1247. xfs_dqunlock(
  1248. xfs_dquot_t *dqp)
  1249. {
  1250. mutex_unlock(&(dqp->q_qlock));
  1251. if (dqp->q_logitem.qli_dquot == dqp) {
  1252. /* Once was dqp->q_mount, but might just have been cleared */
  1253. xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_mountp,
  1254. (xfs_log_item_t*)&(dqp->q_logitem));
  1255. }
  1256. }
  1257. void
  1258. xfs_dqunlock_nonotify(
  1259. xfs_dquot_t *dqp)
  1260. {
  1261. mutex_unlock(&(dqp->q_qlock));
  1262. }
  1263. void
  1264. xfs_dqlock2(
  1265. xfs_dquot_t *d1,
  1266. xfs_dquot_t *d2)
  1267. {
  1268. if (d1 && d2) {
  1269. ASSERT(d1 != d2);
  1270. if (INT_GET(d1->q_core.d_id, ARCH_CONVERT) >
  1271. INT_GET(d2->q_core.d_id, ARCH_CONVERT)) {
  1272. xfs_dqlock(d2);
  1273. xfs_dqlock(d1);
  1274. } else {
  1275. xfs_dqlock(d1);
  1276. xfs_dqlock(d2);
  1277. }
  1278. } else {
  1279. if (d1) {
  1280. xfs_dqlock(d1);
  1281. } else if (d2) {
  1282. xfs_dqlock(d2);
  1283. }
  1284. }
  1285. }
  1286. /*
  1287. * Take a dquot out of the mount's dqlist as well as the hashlist.
  1288. * This is called via unmount as well as quotaoff, and the purge
  1289. * will always succeed unless there are soft (temp) references
  1290. * outstanding.
  1291. *
  1292. * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
  1293. * that we're returning! XXXsup - not cool.
  1294. */
  1295. /* ARGSUSED */
  1296. int
  1297. xfs_qm_dqpurge(
  1298. xfs_dquot_t *dqp,
  1299. uint flags)
  1300. {
  1301. xfs_dqhash_t *thishash;
  1302. xfs_mount_t *mp;
  1303. mp = dqp->q_mount;
  1304. ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
  1305. ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp->q_hash));
  1306. xfs_dqlock(dqp);
  1307. /*
  1308. * We really can't afford to purge a dquot that is
  1309. * referenced, because these are hard refs.
  1310. * It shouldn't happen in general because we went thru _all_ inodes in
  1311. * dqrele_all_inodes before calling this and didn't let the mountlock go.
  1312. * However it is possible that we have dquots with temporary
  1313. * references that are not attached to an inode. e.g. see xfs_setattr().
  1314. */
  1315. if (dqp->q_nrefs != 0) {
  1316. xfs_dqunlock(dqp);
  1317. XFS_DQ_HASH_UNLOCK(dqp->q_hash);
  1318. return (1);
  1319. }
  1320. ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
  1321. /*
  1322. * If we're turning off quotas, we have to make sure that, for
  1323. * example, we don't delete quota disk blocks while dquots are
  1324. * in the process of getting written to those disk blocks.
  1325. * This dquot might well be on AIL, and we can't leave it there
  1326. * if we're turning off quotas. Basically, we need this flush
  1327. * lock, and are willing to block on it.
  1328. */
  1329. if (! xfs_qm_dqflock_nowait(dqp)) {
  1330. /*
  1331. * Block on the flush lock after nudging dquot buffer,
  1332. * if it is incore.
  1333. */
  1334. xfs_qm_dqflock_pushbuf_wait(dqp);
  1335. }
  1336. /*
  1337. * XXXIf we're turning this type of quotas off, we don't care
  1338. * about the dirty metadata sitting in this dquot. OTOH, if
  1339. * we're unmounting, we do care, so we flush it and wait.
  1340. */
  1341. if (XFS_DQ_IS_DIRTY(dqp)) {
  1342. xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
  1343. /* dqflush unlocks dqflock */
  1344. /*
  1345. * Given that dqpurge is a very rare occurrence, it is OK
  1346. * that we're holding the hashlist and mplist locks
  1347. * across the disk write. But, ... XXXsup
  1348. *
  1349. * We don't care about getting disk errors here. We need
  1350. * to purge this dquot anyway, so we go ahead regardless.
  1351. */
  1352. (void) xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
  1353. xfs_dqflock(dqp);
  1354. }
  1355. ASSERT(dqp->q_pincount == 0);
  1356. ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
  1357. !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
  1358. thishash = dqp->q_hash;
  1359. XQM_HASHLIST_REMOVE(thishash, dqp);
  1360. XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
  1361. /*
  1362. * XXX Move this to the front of the freelist, if we can get the
  1363. * freelist lock.
  1364. */
  1365. ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
  1366. dqp->q_mount = NULL;
  1367. dqp->q_hash = NULL;
  1368. dqp->dq_flags = XFS_DQ_INACTIVE;
  1369. memset(&dqp->q_core, 0, sizeof(dqp->q_core));
  1370. xfs_dqfunlock(dqp);
  1371. xfs_dqunlock(dqp);
  1372. XFS_DQ_HASH_UNLOCK(thishash);
  1373. return (0);
  1374. }
  1375. #ifdef QUOTADEBUG
  1376. void
  1377. xfs_qm_dqprint(xfs_dquot_t *dqp)
  1378. {
  1379. cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
  1380. cmn_err(CE_DEBUG, "---- dquotID = %d",
  1381. (int)INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
  1382. cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
  1383. cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
  1384. cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
  1385. cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
  1386. cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
  1387. INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT),
  1388. (int) INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT));
  1389. cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
  1390. INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT),
  1391. (int)INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT));
  1392. cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
  1393. INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT),
  1394. (int)INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT));
  1395. cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
  1396. INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT),
  1397. (int)INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT));
  1398. cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
  1399. INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
  1400. (int)INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT));
  1401. cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
  1402. INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
  1403. (int)INT_GET(dqp->q_core.d_icount, ARCH_CONVERT));
  1404. cmn_err(CE_DEBUG, "---- btimer = %d",
  1405. (int)INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT));
  1406. cmn_err(CE_DEBUG, "---- itimer = %d",
  1407. (int)INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT));
  1408. cmn_err(CE_DEBUG, "---------------------------");
  1409. }
  1410. #endif
  1411. /*
  1412. * Give the buffer a little push if it is incore and
  1413. * wait on the flush lock.
  1414. */
  1415. void
  1416. xfs_qm_dqflock_pushbuf_wait(
  1417. xfs_dquot_t *dqp)
  1418. {
  1419. xfs_buf_t *bp;
  1420. /*
  1421. * Check to see if the dquot has been flushed delayed
  1422. * write. If so, grab its buffer and send it
  1423. * out immediately. We'll be able to acquire
  1424. * the flush lock when the I/O completes.
  1425. */
  1426. bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
  1427. XFS_QI_DQCHUNKLEN(dqp->q_mount),
  1428. XFS_INCORE_TRYLOCK);
  1429. if (bp != NULL) {
  1430. if (XFS_BUF_ISDELAYWRITE(bp)) {
  1431. if (XFS_BUF_ISPINNED(bp)) {
  1432. xfs_log_force(dqp->q_mount,
  1433. (xfs_lsn_t)0,
  1434. XFS_LOG_FORCE);
  1435. }
  1436. xfs_bawrite(dqp->q_mount, bp);
  1437. } else {
  1438. xfs_buf_relse(bp);
  1439. }
  1440. }
  1441. xfs_dqflock(dqp);
  1442. }