xfs_dquot.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. kmem_zone_free(xfs_qm_dqzone, dqp);
  73. XFS_STATS_DEC(xs_qm_dquot);
  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_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(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(xs_qm_dqcachehits);
  665. *O_dqpp = dqp;
  666. return 0;
  667. }
  668. mutex_unlock(&qi->qi_tree_lock);
  669. XFS_STATS_INC(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(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(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. spin_lock(&mp->m_ail->xa_lock);
  843. if (lip->li_flags & XFS_LI_IN_AIL)
  844. xfs_trans_ail_delete(mp->m_ail, lip,
  845. SHUTDOWN_CORRUPT_INCORE);
  846. else
  847. spin_unlock(&mp->m_ail->xa_lock);
  848. error = -EIO;
  849. goto out_unlock;
  850. }
  851. /*
  852. * Get the buffer containing the on-disk dquot
  853. */
  854. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  855. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  856. &xfs_dquot_buf_ops);
  857. if (error)
  858. goto out_unlock;
  859. /*
  860. * Calculate the location of the dquot inside the buffer.
  861. */
  862. ddqp = bp->b_addr + dqp->q_bufoffset;
  863. /*
  864. * A simple sanity check in case we got a corrupted dquot..
  865. */
  866. error = xfs_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
  867. XFS_QMOPT_DOWARN, "dqflush (incore copy)");
  868. if (error) {
  869. xfs_buf_relse(bp);
  870. xfs_dqfunlock(dqp);
  871. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  872. return -EIO;
  873. }
  874. /* This is the only portion of data that needs to persist */
  875. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  876. /*
  877. * Clear the dirty field and remember the flush lsn for later use.
  878. */
  879. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  880. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  881. &dqp->q_logitem.qli_item.li_lsn);
  882. /*
  883. * copy the lsn into the on-disk dquot now while we have the in memory
  884. * dquot here. This can't be done later in the write verifier as we
  885. * can't get access to the log item at that point in time.
  886. *
  887. * We also calculate the CRC here so that the on-disk dquot in the
  888. * buffer always has a valid CRC. This ensures there is no possibility
  889. * of a dquot without an up-to-date CRC getting to disk.
  890. */
  891. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  892. struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddqp;
  893. dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
  894. xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
  895. XFS_DQUOT_CRC_OFF);
  896. }
  897. /*
  898. * Attach an iodone routine so that we can remove this dquot from the
  899. * AIL and release the flush lock once the dquot is synced to disk.
  900. */
  901. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  902. &dqp->q_logitem.qli_item);
  903. /*
  904. * If the buffer is pinned then push on the log so we won't
  905. * get stuck waiting in the write for too long.
  906. */
  907. if (xfs_buf_ispinned(bp)) {
  908. trace_xfs_dqflush_force(dqp);
  909. xfs_log_force(mp, 0);
  910. }
  911. trace_xfs_dqflush_done(dqp);
  912. *bpp = bp;
  913. return 0;
  914. out_unlock:
  915. xfs_dqfunlock(dqp);
  916. return -EIO;
  917. }
  918. /*
  919. * Lock two xfs_dquot structures.
  920. *
  921. * To avoid deadlocks we always lock the quota structure with
  922. * the lowerd id first.
  923. */
  924. void
  925. xfs_dqlock2(
  926. xfs_dquot_t *d1,
  927. xfs_dquot_t *d2)
  928. {
  929. if (d1 && d2) {
  930. ASSERT(d1 != d2);
  931. if (be32_to_cpu(d1->q_core.d_id) >
  932. be32_to_cpu(d2->q_core.d_id)) {
  933. mutex_lock(&d2->q_qlock);
  934. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  935. } else {
  936. mutex_lock(&d1->q_qlock);
  937. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  938. }
  939. } else if (d1) {
  940. mutex_lock(&d1->q_qlock);
  941. } else if (d2) {
  942. mutex_lock(&d2->q_qlock);
  943. }
  944. }
  945. int __init
  946. xfs_qm_init(void)
  947. {
  948. xfs_qm_dqzone =
  949. kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
  950. if (!xfs_qm_dqzone)
  951. goto out;
  952. xfs_qm_dqtrxzone =
  953. kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
  954. if (!xfs_qm_dqtrxzone)
  955. goto out_free_dqzone;
  956. return 0;
  957. out_free_dqzone:
  958. kmem_zone_destroy(xfs_qm_dqzone);
  959. out:
  960. return -ENOMEM;
  961. }
  962. void
  963. xfs_qm_exit(void)
  964. {
  965. kmem_zone_destroy(xfs_qm_dqtrxzone);
  966. kmem_zone_destroy(xfs_qm_dqzone);
  967. }