xfs_dquot.c 39 KB

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