xfs_dquot.c 41 KB

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