xfs_dquot.c 27 KB

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