xfs_dquot.c 38 KB

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