xfs_dquot.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_alloc.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_bmap.h"
  31. #include "xfs_rtalloc.h"
  32. #include "xfs_error.h"
  33. #include "xfs_itable.h"
  34. #include "xfs_attr.h"
  35. #include "xfs_buf_item.h"
  36. #include "xfs_trans_space.h"
  37. #include "xfs_trans_priv.h"
  38. #include "xfs_qm.h"
  39. #include "xfs_trace.h"
  40. /*
  41. * Lock order:
  42. *
  43. * ip->i_lock
  44. * qi->qi_tree_lock
  45. * dquot->q_qlock (xfs_dqlock() and friends)
  46. * dquot->q_flush (xfs_dqflock() and friends)
  47. * qi->qi_lru_lock
  48. *
  49. * If two dquots need to be locked the order is user before group/project,
  50. * otherwise by the lowest id first, see xfs_dqlock2.
  51. */
  52. #ifdef DEBUG
  53. xfs_buftarg_t *xfs_dqerror_target;
  54. int xfs_do_dqerror;
  55. int xfs_dqreq_num;
  56. int xfs_dqerror_mod = 33;
  57. #endif
  58. struct kmem_zone *xfs_qm_dqtrxzone;
  59. static struct kmem_zone *xfs_qm_dqzone;
  60. static struct lock_class_key xfs_dquot_other_class;
  61. /*
  62. * This is called to free all the memory associated with a dquot
  63. */
  64. void
  65. xfs_qm_dqdestroy(
  66. xfs_dquot_t *dqp)
  67. {
  68. ASSERT(list_empty(&dqp->q_lru));
  69. mutex_destroy(&dqp->q_qlock);
  70. kmem_zone_free(xfs_qm_dqzone, dqp);
  71. XFS_STATS_DEC(xs_qm_dquot);
  72. }
  73. /*
  74. * If default limits are in force, push them into the dquot now.
  75. * We overwrite the dquot limits only if they are zero and this
  76. * is not the root dquot.
  77. */
  78. void
  79. xfs_qm_adjust_dqlimits(
  80. xfs_mount_t *mp,
  81. xfs_disk_dquot_t *d)
  82. {
  83. xfs_quotainfo_t *q = mp->m_quotainfo;
  84. ASSERT(d->d_id);
  85. if (q->qi_bsoftlimit && !d->d_blk_softlimit)
  86. d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
  87. if (q->qi_bhardlimit && !d->d_blk_hardlimit)
  88. d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
  89. if (q->qi_isoftlimit && !d->d_ino_softlimit)
  90. d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
  91. if (q->qi_ihardlimit && !d->d_ino_hardlimit)
  92. d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
  93. if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
  94. d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
  95. if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
  96. d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
  97. }
  98. /*
  99. * Check the limits and timers of a dquot and start or reset timers
  100. * if necessary.
  101. * This gets called even when quota enforcement is OFF, which makes our
  102. * life a little less complicated. (We just don't reject any quota
  103. * reservations in that case, when enforcement is off).
  104. * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
  105. * enforcement's off.
  106. * In contrast, warnings are a little different in that they don't
  107. * 'automatically' get started when limits get exceeded. They do
  108. * get reset to zero, however, when we find the count to be under
  109. * the soft limit (they are only ever set non-zero via userspace).
  110. */
  111. void
  112. xfs_qm_adjust_dqtimers(
  113. xfs_mount_t *mp,
  114. xfs_disk_dquot_t *d)
  115. {
  116. ASSERT(d->d_id);
  117. #ifdef DEBUG
  118. if (d->d_blk_hardlimit)
  119. ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
  120. be64_to_cpu(d->d_blk_hardlimit));
  121. if (d->d_ino_hardlimit)
  122. ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
  123. be64_to_cpu(d->d_ino_hardlimit));
  124. if (d->d_rtb_hardlimit)
  125. ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
  126. be64_to_cpu(d->d_rtb_hardlimit));
  127. #endif
  128. if (!d->d_btimer) {
  129. if ((d->d_blk_softlimit &&
  130. (be64_to_cpu(d->d_bcount) >
  131. be64_to_cpu(d->d_blk_softlimit))) ||
  132. (d->d_blk_hardlimit &&
  133. (be64_to_cpu(d->d_bcount) >
  134. be64_to_cpu(d->d_blk_hardlimit)))) {
  135. d->d_btimer = cpu_to_be32(get_seconds() +
  136. mp->m_quotainfo->qi_btimelimit);
  137. } else {
  138. d->d_bwarns = 0;
  139. }
  140. } else {
  141. if ((!d->d_blk_softlimit ||
  142. (be64_to_cpu(d->d_bcount) <=
  143. be64_to_cpu(d->d_blk_softlimit))) &&
  144. (!d->d_blk_hardlimit ||
  145. (be64_to_cpu(d->d_bcount) <=
  146. be64_to_cpu(d->d_blk_hardlimit)))) {
  147. d->d_btimer = 0;
  148. }
  149. }
  150. if (!d->d_itimer) {
  151. if ((d->d_ino_softlimit &&
  152. (be64_to_cpu(d->d_icount) >
  153. be64_to_cpu(d->d_ino_softlimit))) ||
  154. (d->d_ino_hardlimit &&
  155. (be64_to_cpu(d->d_icount) >
  156. be64_to_cpu(d->d_ino_hardlimit)))) {
  157. d->d_itimer = cpu_to_be32(get_seconds() +
  158. mp->m_quotainfo->qi_itimelimit);
  159. } else {
  160. d->d_iwarns = 0;
  161. }
  162. } else {
  163. if ((!d->d_ino_softlimit ||
  164. (be64_to_cpu(d->d_icount) <=
  165. be64_to_cpu(d->d_ino_softlimit))) &&
  166. (!d->d_ino_hardlimit ||
  167. (be64_to_cpu(d->d_icount) <=
  168. be64_to_cpu(d->d_ino_hardlimit)))) {
  169. d->d_itimer = 0;
  170. }
  171. }
  172. if (!d->d_rtbtimer) {
  173. if ((d->d_rtb_softlimit &&
  174. (be64_to_cpu(d->d_rtbcount) >
  175. be64_to_cpu(d->d_rtb_softlimit))) ||
  176. (d->d_rtb_hardlimit &&
  177. (be64_to_cpu(d->d_rtbcount) >
  178. be64_to_cpu(d->d_rtb_hardlimit)))) {
  179. d->d_rtbtimer = cpu_to_be32(get_seconds() +
  180. mp->m_quotainfo->qi_rtbtimelimit);
  181. } else {
  182. d->d_rtbwarns = 0;
  183. }
  184. } else {
  185. if ((!d->d_rtb_softlimit ||
  186. (be64_to_cpu(d->d_rtbcount) <=
  187. be64_to_cpu(d->d_rtb_softlimit))) &&
  188. (!d->d_rtb_hardlimit ||
  189. (be64_to_cpu(d->d_rtbcount) <=
  190. be64_to_cpu(d->d_rtb_hardlimit)))) {
  191. d->d_rtbtimer = 0;
  192. }
  193. }
  194. }
  195. /*
  196. * initialize a buffer full of dquots and log the whole thing
  197. */
  198. STATIC void
  199. xfs_qm_init_dquot_blk(
  200. xfs_trans_t *tp,
  201. xfs_mount_t *mp,
  202. xfs_dqid_t id,
  203. uint type,
  204. xfs_buf_t *bp)
  205. {
  206. struct xfs_quotainfo *q = mp->m_quotainfo;
  207. xfs_dqblk_t *d;
  208. int curid, i;
  209. ASSERT(tp);
  210. ASSERT(xfs_buf_islocked(bp));
  211. d = bp->b_addr;
  212. /*
  213. * ID of the first dquot in the block - id's are zero based.
  214. */
  215. curid = id - (id % q->qi_dqperchunk);
  216. ASSERT(curid >= 0);
  217. memset(d, 0, BBTOB(q->qi_dqchunklen));
  218. for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
  219. d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  220. d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  221. d->dd_diskdq.d_id = cpu_to_be32(curid);
  222. d->dd_diskdq.d_flags = type;
  223. }
  224. xfs_trans_dquot_buf(tp, bp,
  225. (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
  226. ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
  227. XFS_BLF_GDQUOT_BUF)));
  228. xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
  229. }
  230. /*
  231. * Allocate a block and fill it with dquots.
  232. * This is called when the bmapi finds a hole.
  233. */
  234. STATIC int
  235. xfs_qm_dqalloc(
  236. xfs_trans_t **tpp,
  237. xfs_mount_t *mp,
  238. xfs_dquot_t *dqp,
  239. xfs_inode_t *quotip,
  240. xfs_fileoff_t offset_fsb,
  241. xfs_buf_t **O_bpp)
  242. {
  243. xfs_fsblock_t firstblock;
  244. xfs_bmap_free_t flist;
  245. xfs_bmbt_irec_t map;
  246. int nmaps, error, committed;
  247. xfs_buf_t *bp;
  248. xfs_trans_t *tp = *tpp;
  249. ASSERT(tp != NULL);
  250. trace_xfs_dqalloc(dqp);
  251. /*
  252. * Initialize the bmap freelist prior to calling bmapi code.
  253. */
  254. xfs_bmap_init(&flist, &firstblock);
  255. xfs_ilock(quotip, XFS_ILOCK_EXCL);
  256. /*
  257. * Return if this type of quotas is turned off while we didn't
  258. * have an inode lock
  259. */
  260. if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
  261. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  262. return (ESRCH);
  263. }
  264. xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
  265. nmaps = 1;
  266. error = xfs_bmapi_write(tp, quotip, offset_fsb,
  267. XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
  268. &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
  269. &map, &nmaps, &flist);
  270. if (error)
  271. goto error0;
  272. ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
  273. ASSERT(nmaps == 1);
  274. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  275. (map.br_startblock != HOLESTARTBLOCK));
  276. /*
  277. * Keep track of the blkno to save a lookup later
  278. */
  279. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  280. /* now we can just get the buffer (there's nothing to read yet) */
  281. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  282. dqp->q_blkno,
  283. mp->m_quotainfo->qi_dqchunklen,
  284. 0);
  285. error = xfs_buf_geterror(bp);
  286. if (error)
  287. goto error1;
  288. /*
  289. * Make a chunk of dquots out of this buffer and log
  290. * the entire thing.
  291. */
  292. xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
  293. dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
  294. /*
  295. * xfs_bmap_finish() may commit the current transaction and
  296. * start a second transaction if the freelist is not empty.
  297. *
  298. * Since we still want to modify this buffer, we need to
  299. * ensure that the buffer is not released on commit of
  300. * the first transaction and ensure the buffer is added to the
  301. * second transaction.
  302. *
  303. * If there is only one transaction then don't stop the buffer
  304. * from being released when it commits later on.
  305. */
  306. xfs_trans_bhold(tp, bp);
  307. if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
  308. goto error1;
  309. }
  310. if (committed) {
  311. tp = *tpp;
  312. xfs_trans_bjoin(tp, bp);
  313. } else {
  314. xfs_trans_bhold_release(tp, bp);
  315. }
  316. *O_bpp = bp;
  317. return 0;
  318. error1:
  319. xfs_bmap_cancel(&flist);
  320. error0:
  321. xfs_iunlock(quotip, XFS_ILOCK_EXCL);
  322. return (error);
  323. }
  324. /*
  325. * Maps a dquot to the buffer containing its on-disk version.
  326. * This returns a ptr to the buffer containing the on-disk dquot
  327. * in the bpp param, and a ptr to the on-disk dquot within that buffer
  328. */
  329. STATIC int
  330. xfs_qm_dqtobp(
  331. xfs_trans_t **tpp,
  332. xfs_dquot_t *dqp,
  333. xfs_disk_dquot_t **O_ddpp,
  334. xfs_buf_t **O_bpp,
  335. uint flags)
  336. {
  337. xfs_bmbt_irec_t map;
  338. int nmaps = 1, error;
  339. xfs_buf_t *bp;
  340. xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp);
  341. xfs_mount_t *mp = dqp->q_mount;
  342. xfs_disk_dquot_t *ddq;
  343. xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
  344. xfs_trans_t *tp = (tpp ? *tpp : NULL);
  345. dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
  346. xfs_ilock(quotip, XFS_ILOCK_SHARED);
  347. if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
  348. /*
  349. * Return if this type of quotas is turned off while we
  350. * didn't have the quota inode lock.
  351. */
  352. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  353. return ESRCH;
  354. }
  355. /*
  356. * Find the block map; no allocations yet
  357. */
  358. error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
  359. XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
  360. xfs_iunlock(quotip, XFS_ILOCK_SHARED);
  361. if (error)
  362. return error;
  363. ASSERT(nmaps == 1);
  364. ASSERT(map.br_blockcount == 1);
  365. /*
  366. * Offset of dquot in the (fixed sized) dquot chunk.
  367. */
  368. dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
  369. sizeof(xfs_dqblk_t);
  370. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  371. if (map.br_startblock == HOLESTARTBLOCK) {
  372. /*
  373. * We don't allocate unless we're asked to
  374. */
  375. if (!(flags & XFS_QMOPT_DQALLOC))
  376. return ENOENT;
  377. ASSERT(tp);
  378. error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
  379. dqp->q_fileoffset, &bp);
  380. if (error)
  381. return error;
  382. tp = *tpp;
  383. } else {
  384. trace_xfs_dqtobp_read(dqp);
  385. /*
  386. * store the blkno etc so that we don't have to do the
  387. * mapping all the time
  388. */
  389. dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
  390. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  391. dqp->q_blkno,
  392. mp->m_quotainfo->qi_dqchunklen,
  393. 0, &bp);
  394. if (error || !bp)
  395. return XFS_ERROR(error);
  396. }
  397. ASSERT(xfs_buf_islocked(bp));
  398. /*
  399. * calculate the location of the dquot inside the buffer.
  400. */
  401. ddq = bp->b_addr + dqp->q_bufoffset;
  402. /*
  403. * A simple sanity check in case we got a corrupted dquot...
  404. */
  405. error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
  406. flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
  407. "dqtobp");
  408. if (error) {
  409. if (!(flags & XFS_QMOPT_DQREPAIR)) {
  410. xfs_trans_brelse(tp, bp);
  411. return XFS_ERROR(EIO);
  412. }
  413. }
  414. *O_bpp = bp;
  415. *O_ddpp = ddq;
  416. return (0);
  417. }
  418. /*
  419. * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
  420. * and release the buffer immediately.
  421. *
  422. * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
  423. */
  424. int
  425. xfs_qm_dqread(
  426. struct xfs_mount *mp,
  427. xfs_dqid_t id,
  428. uint type,
  429. uint flags,
  430. struct xfs_dquot **O_dqpp)
  431. {
  432. struct xfs_dquot *dqp;
  433. struct xfs_disk_dquot *ddqp;
  434. struct xfs_buf *bp;
  435. struct xfs_trans *tp = NULL;
  436. int error;
  437. int cancelflags = 0;
  438. dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
  439. dqp->dq_flags = type;
  440. dqp->q_core.d_id = cpu_to_be32(id);
  441. dqp->q_mount = mp;
  442. INIT_LIST_HEAD(&dqp->q_lru);
  443. mutex_init(&dqp->q_qlock);
  444. init_waitqueue_head(&dqp->q_pinwait);
  445. /*
  446. * Because we want to use a counting completion, complete
  447. * the flush completion once to allow a single access to
  448. * the flush completion without blocking.
  449. */
  450. init_completion(&dqp->q_flush);
  451. complete(&dqp->q_flush);
  452. /*
  453. * Make sure group quotas have a different lock class than user
  454. * quotas.
  455. */
  456. if (!(type & XFS_DQ_USER))
  457. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
  458. XFS_STATS_INC(xs_qm_dquot);
  459. trace_xfs_dqread(dqp);
  460. if (flags & XFS_QMOPT_DQALLOC) {
  461. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
  462. error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
  463. XFS_WRITE_LOG_RES(mp) +
  464. /*
  465. * Round the chunklen up to the next multiple
  466. * of 128 (buf log item chunk size)).
  467. */
  468. BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 + 128,
  469. 0,
  470. XFS_TRANS_PERM_LOG_RES,
  471. XFS_WRITE_LOG_COUNT);
  472. if (error)
  473. goto error1;
  474. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  475. }
  476. /*
  477. * get a pointer to the on-disk dquot and the buffer containing it
  478. * dqp already knows its own type (GROUP/USER).
  479. */
  480. error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags);
  481. if (error) {
  482. /*
  483. * This can happen if quotas got turned off (ESRCH),
  484. * or if the dquot didn't exist on disk and we ask to
  485. * allocate (ENOENT).
  486. */
  487. trace_xfs_dqread_fail(dqp);
  488. cancelflags |= XFS_TRANS_ABORT;
  489. goto error1;
  490. }
  491. /* copy everything from disk dquot to the incore dquot */
  492. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  493. xfs_qm_dquot_logitem_init(dqp);
  494. /*
  495. * Reservation counters are defined as reservation plus current usage
  496. * to avoid having to add every time.
  497. */
  498. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  499. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  500. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  501. /* Mark the buf so that this will stay incore a little longer */
  502. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  503. /*
  504. * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
  505. * So we need to release with xfs_trans_brelse().
  506. * The strategy here is identical to that of inodes; we lock
  507. * the dquot in xfs_qm_dqget() before making it accessible to
  508. * others. This is because dquots, like inodes, need a good level of
  509. * concurrency, and we don't want to take locks on the entire buffers
  510. * for dquot accesses.
  511. * Note also that the dquot buffer may even be dirty at this point, if
  512. * this particular dquot was repaired. We still aren't afraid to
  513. * brelse it because we have the changes incore.
  514. */
  515. ASSERT(xfs_buf_islocked(bp));
  516. xfs_trans_brelse(tp, bp);
  517. if (tp) {
  518. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  519. if (error)
  520. goto error0;
  521. }
  522. *O_dqpp = dqp;
  523. return error;
  524. error1:
  525. if (tp)
  526. xfs_trans_cancel(tp, cancelflags);
  527. error0:
  528. xfs_qm_dqdestroy(dqp);
  529. *O_dqpp = NULL;
  530. return error;
  531. }
  532. /*
  533. * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
  534. * a locked dquot, doing an allocation (if requested) as needed.
  535. * When both an inode and an id are given, the inode's id takes precedence.
  536. * That is, if the id changes while we don't hold the ilock inside this
  537. * function, the new dquot is returned, not necessarily the one requested
  538. * in the id argument.
  539. */
  540. int
  541. xfs_qm_dqget(
  542. xfs_mount_t *mp,
  543. xfs_inode_t *ip, /* locked inode (optional) */
  544. xfs_dqid_t id, /* uid/projid/gid depending on type */
  545. uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
  546. uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
  547. xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
  548. {
  549. struct xfs_quotainfo *qi = mp->m_quotainfo;
  550. struct radix_tree_root *tree = XFS_DQUOT_TREE(qi, type);
  551. struct xfs_dquot *dqp;
  552. int error;
  553. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  554. if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
  555. (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
  556. (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
  557. return (ESRCH);
  558. }
  559. #ifdef DEBUG
  560. if (xfs_do_dqerror) {
  561. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  562. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  563. xfs_debug(mp, "Returning error in dqget");
  564. return (EIO);
  565. }
  566. }
  567. ASSERT(type == XFS_DQ_USER ||
  568. type == XFS_DQ_PROJ ||
  569. type == XFS_DQ_GROUP);
  570. if (ip) {
  571. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  572. ASSERT(xfs_inode_dquot(ip, type) == NULL);
  573. }
  574. #endif
  575. restart:
  576. mutex_lock(&qi->qi_tree_lock);
  577. dqp = radix_tree_lookup(tree, id);
  578. if (dqp) {
  579. xfs_dqlock(dqp);
  580. if (dqp->dq_flags & XFS_DQ_FREEING) {
  581. xfs_dqunlock(dqp);
  582. mutex_unlock(&qi->qi_tree_lock);
  583. trace_xfs_dqget_freeing(dqp);
  584. delay(1);
  585. goto restart;
  586. }
  587. dqp->q_nrefs++;
  588. mutex_unlock(&qi->qi_tree_lock);
  589. trace_xfs_dqget_hit(dqp);
  590. XFS_STATS_INC(xs_qm_dqcachehits);
  591. *O_dqpp = dqp;
  592. return 0;
  593. }
  594. mutex_unlock(&qi->qi_tree_lock);
  595. XFS_STATS_INC(xs_qm_dqcachemisses);
  596. /*
  597. * Dquot cache miss. We don't want to keep the inode lock across
  598. * a (potential) disk read. Also we don't want to deal with the lock
  599. * ordering between quotainode and this inode. OTOH, dropping the inode
  600. * lock here means dealing with a chown that can happen before
  601. * we re-acquire the lock.
  602. */
  603. if (ip)
  604. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  605. error = xfs_qm_dqread(mp, id, type, flags, &dqp);
  606. if (ip)
  607. xfs_ilock(ip, XFS_ILOCK_EXCL);
  608. if (error)
  609. return error;
  610. if (ip) {
  611. /*
  612. * A dquot could be attached to this inode by now, since
  613. * we had dropped the ilock.
  614. */
  615. if (xfs_this_quota_on(mp, type)) {
  616. struct xfs_dquot *dqp1;
  617. dqp1 = xfs_inode_dquot(ip, type);
  618. if (dqp1) {
  619. xfs_qm_dqdestroy(dqp);
  620. dqp = dqp1;
  621. xfs_dqlock(dqp);
  622. goto dqret;
  623. }
  624. } else {
  625. /* inode stays locked on return */
  626. xfs_qm_dqdestroy(dqp);
  627. return XFS_ERROR(ESRCH);
  628. }
  629. }
  630. mutex_lock(&qi->qi_tree_lock);
  631. error = -radix_tree_insert(tree, id, dqp);
  632. if (unlikely(error)) {
  633. WARN_ON(error != EEXIST);
  634. /*
  635. * Duplicate found. Just throw away the new dquot and start
  636. * over.
  637. */
  638. mutex_unlock(&qi->qi_tree_lock);
  639. trace_xfs_dqget_dup(dqp);
  640. xfs_qm_dqdestroy(dqp);
  641. XFS_STATS_INC(xs_qm_dquot_dups);
  642. goto restart;
  643. }
  644. /*
  645. * We return a locked dquot to the caller, with a reference taken
  646. */
  647. xfs_dqlock(dqp);
  648. dqp->q_nrefs = 1;
  649. qi->qi_dquots++;
  650. mutex_unlock(&qi->qi_tree_lock);
  651. dqret:
  652. ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
  653. trace_xfs_dqget_miss(dqp);
  654. *O_dqpp = dqp;
  655. return (0);
  656. }
  657. STATIC void
  658. xfs_qm_dqput_final(
  659. struct xfs_dquot *dqp)
  660. {
  661. struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
  662. struct xfs_dquot *gdqp;
  663. trace_xfs_dqput_free(dqp);
  664. mutex_lock(&qi->qi_lru_lock);
  665. if (list_empty(&dqp->q_lru)) {
  666. list_add_tail(&dqp->q_lru, &qi->qi_lru_list);
  667. qi->qi_lru_count++;
  668. XFS_STATS_INC(xs_qm_dquot_unused);
  669. }
  670. mutex_unlock(&qi->qi_lru_lock);
  671. /*
  672. * If we just added a udquot to the freelist, then we want to release
  673. * the gdquot reference that it (probably) has. Otherwise it'll keep
  674. * the gdquot from getting reclaimed.
  675. */
  676. gdqp = dqp->q_gdquot;
  677. if (gdqp) {
  678. xfs_dqlock(gdqp);
  679. dqp->q_gdquot = NULL;
  680. }
  681. xfs_dqunlock(dqp);
  682. /*
  683. * If we had a group quota hint, release it now.
  684. */
  685. if (gdqp)
  686. xfs_qm_dqput(gdqp);
  687. }
  688. /*
  689. * Release a reference to the dquot (decrement ref-count) and unlock it.
  690. *
  691. * If there is a group quota attached to this dquot, carefully release that
  692. * too without tripping over deadlocks'n'stuff.
  693. */
  694. void
  695. xfs_qm_dqput(
  696. struct xfs_dquot *dqp)
  697. {
  698. ASSERT(dqp->q_nrefs > 0);
  699. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  700. trace_xfs_dqput(dqp);
  701. if (--dqp->q_nrefs > 0)
  702. xfs_dqunlock(dqp);
  703. else
  704. xfs_qm_dqput_final(dqp);
  705. }
  706. /*
  707. * Release a dquot. Flush it if dirty, then dqput() it.
  708. * dquot must not be locked.
  709. */
  710. void
  711. xfs_qm_dqrele(
  712. xfs_dquot_t *dqp)
  713. {
  714. if (!dqp)
  715. return;
  716. trace_xfs_dqrele(dqp);
  717. xfs_dqlock(dqp);
  718. /*
  719. * We don't care to flush it if the dquot is dirty here.
  720. * That will create stutters that we want to avoid.
  721. * Instead we do a delayed write when we try to reclaim
  722. * a dirty dquot. Also xfs_sync will take part of the burden...
  723. */
  724. xfs_qm_dqput(dqp);
  725. }
  726. /*
  727. * This is the dquot flushing I/O completion routine. It is called
  728. * from interrupt level when the buffer containing the dquot is
  729. * flushed to disk. It is responsible for removing the dquot logitem
  730. * from the AIL if it has not been re-logged, and unlocking the dquot's
  731. * flush lock. This behavior is very similar to that of inodes..
  732. */
  733. STATIC void
  734. xfs_qm_dqflush_done(
  735. struct xfs_buf *bp,
  736. struct xfs_log_item *lip)
  737. {
  738. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  739. xfs_dquot_t *dqp = qip->qli_dquot;
  740. struct xfs_ail *ailp = lip->li_ailp;
  741. /*
  742. * We only want to pull the item from the AIL if its
  743. * location in the log has not changed since we started the flush.
  744. * Thus, we only bother if the dquot's lsn has
  745. * not changed. First we check the lsn outside the lock
  746. * since it's cheaper, and then we recheck while
  747. * holding the lock before removing the dquot from the AIL.
  748. */
  749. if ((lip->li_flags & XFS_LI_IN_AIL) &&
  750. lip->li_lsn == qip->qli_flush_lsn) {
  751. /* xfs_trans_ail_delete() drops the AIL lock. */
  752. spin_lock(&ailp->xa_lock);
  753. if (lip->li_lsn == qip->qli_flush_lsn)
  754. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  755. else
  756. spin_unlock(&ailp->xa_lock);
  757. }
  758. /*
  759. * Release the dq's flush lock since we're done with it.
  760. */
  761. xfs_dqfunlock(dqp);
  762. }
  763. /*
  764. * Write a modified dquot to disk.
  765. * The dquot must be locked and the flush lock too taken by caller.
  766. * The flush lock will not be unlocked until the dquot reaches the disk,
  767. * but the dquot is free to be unlocked and modified by the caller
  768. * in the interim. Dquot is still locked on return. This behavior is
  769. * identical to that of inodes.
  770. */
  771. int
  772. xfs_qm_dqflush(
  773. struct xfs_dquot *dqp,
  774. struct xfs_buf **bpp)
  775. {
  776. struct xfs_mount *mp = dqp->q_mount;
  777. struct xfs_buf *bp;
  778. struct xfs_disk_dquot *ddqp;
  779. int error;
  780. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  781. ASSERT(!completion_done(&dqp->q_flush));
  782. trace_xfs_dqflush(dqp);
  783. *bpp = NULL;
  784. xfs_qm_dqunpin_wait(dqp);
  785. /*
  786. * This may have been unpinned because the filesystem is shutting
  787. * down forcibly. If that's the case we must not write this dquot
  788. * to disk, because the log record didn't make it to disk.
  789. *
  790. * We also have to remove the log item from the AIL in this case,
  791. * as we wait for an emptry AIL as part of the unmount process.
  792. */
  793. if (XFS_FORCED_SHUTDOWN(mp)) {
  794. struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
  795. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  796. spin_lock(&mp->m_ail->xa_lock);
  797. if (lip->li_flags & XFS_LI_IN_AIL)
  798. xfs_trans_ail_delete(mp->m_ail, lip,
  799. SHUTDOWN_CORRUPT_INCORE);
  800. else
  801. spin_unlock(&mp->m_ail->xa_lock);
  802. error = XFS_ERROR(EIO);
  803. goto out_unlock;
  804. }
  805. /*
  806. * Get the buffer containing the on-disk dquot
  807. */
  808. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  809. mp->m_quotainfo->qi_dqchunklen, 0, &bp);
  810. if (error)
  811. goto out_unlock;
  812. /*
  813. * Calculate the location of the dquot inside the buffer.
  814. */
  815. ddqp = bp->b_addr + dqp->q_bufoffset;
  816. /*
  817. * A simple sanity check in case we got a corrupted dquot..
  818. */
  819. error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
  820. XFS_QMOPT_DOWARN, "dqflush (incore copy)");
  821. if (error) {
  822. xfs_buf_relse(bp);
  823. xfs_dqfunlock(dqp);
  824. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  825. return XFS_ERROR(EIO);
  826. }
  827. /* This is the only portion of data that needs to persist */
  828. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  829. /*
  830. * Clear the dirty field and remember the flush lsn for later use.
  831. */
  832. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  833. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  834. &dqp->q_logitem.qli_item.li_lsn);
  835. /*
  836. * Attach an iodone routine so that we can remove this dquot from the
  837. * AIL and release the flush lock once the dquot is synced to disk.
  838. */
  839. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  840. &dqp->q_logitem.qli_item);
  841. /*
  842. * If the buffer is pinned then push on the log so we won't
  843. * get stuck waiting in the write for too long.
  844. */
  845. if (xfs_buf_ispinned(bp)) {
  846. trace_xfs_dqflush_force(dqp);
  847. xfs_log_force(mp, 0);
  848. }
  849. trace_xfs_dqflush_done(dqp);
  850. *bpp = bp;
  851. return 0;
  852. out_unlock:
  853. xfs_dqfunlock(dqp);
  854. return XFS_ERROR(EIO);
  855. }
  856. /*
  857. * Lock two xfs_dquot structures.
  858. *
  859. * To avoid deadlocks we always lock the quota structure with
  860. * the lowerd id first.
  861. */
  862. void
  863. xfs_dqlock2(
  864. xfs_dquot_t *d1,
  865. xfs_dquot_t *d2)
  866. {
  867. if (d1 && d2) {
  868. ASSERT(d1 != d2);
  869. if (be32_to_cpu(d1->q_core.d_id) >
  870. be32_to_cpu(d2->q_core.d_id)) {
  871. mutex_lock(&d2->q_qlock);
  872. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  873. } else {
  874. mutex_lock(&d1->q_qlock);
  875. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  876. }
  877. } else if (d1) {
  878. mutex_lock(&d1->q_qlock);
  879. } else if (d2) {
  880. mutex_lock(&d2->q_qlock);
  881. }
  882. }
  883. int __init
  884. xfs_qm_init(void)
  885. {
  886. xfs_qm_dqzone =
  887. kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
  888. if (!xfs_qm_dqzone)
  889. goto out;
  890. xfs_qm_dqtrxzone =
  891. kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
  892. if (!xfs_qm_dqtrxzone)
  893. goto out_free_dqzone;
  894. return 0;
  895. out_free_dqzone:
  896. kmem_zone_destroy(xfs_qm_dqzone);
  897. out:
  898. return -ENOMEM;
  899. }
  900. void
  901. xfs_qm_exit(void)
  902. {
  903. kmem_zone_destroy(xfs_qm_dqtrxzone);
  904. kmem_zone_destroy(xfs_qm_dqzone);
  905. }