xfs_dquot.c 36 KB

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