xfs_dquot.c 39 KB

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