xfs_dquot.c 27 KB

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