xfs_dquot.c 26 KB

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