xfs_dquot.c 28 KB

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