xfs_dquot.c 41 KB

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