xfs_dquot.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. int cancelflags = 0;
  509. dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
  510. dqp->dq_flags = type;
  511. dqp->q_core.d_id = cpu_to_be32(id);
  512. dqp->q_mount = mp;
  513. INIT_LIST_HEAD(&dqp->q_lru);
  514. mutex_init(&dqp->q_qlock);
  515. init_waitqueue_head(&dqp->q_pinwait);
  516. /*
  517. * Because we want to use a counting completion, complete
  518. * the flush completion once to allow a single access to
  519. * the flush completion without blocking.
  520. */
  521. init_completion(&dqp->q_flush);
  522. complete(&dqp->q_flush);
  523. /*
  524. * Make sure group quotas have a different lock class than user
  525. * quotas.
  526. */
  527. switch (type) {
  528. case XFS_DQ_USER:
  529. /* uses the default lock class */
  530. break;
  531. case XFS_DQ_GROUP:
  532. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
  533. break;
  534. case XFS_DQ_PROJ:
  535. lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
  536. break;
  537. default:
  538. ASSERT(0);
  539. break;
  540. }
  541. XFS_STATS_INC(xs_qm_dquot);
  542. trace_xfs_dqread(dqp);
  543. if (flags & XFS_QMOPT_DQALLOC) {
  544. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
  545. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_dqalloc,
  546. XFS_QM_DQALLOC_SPACE_RES(mp), 0);
  547. if (error)
  548. goto error1;
  549. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  550. }
  551. /*
  552. * get a pointer to the on-disk dquot and the buffer containing it
  553. * dqp already knows its own type (GROUP/USER).
  554. */
  555. error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags);
  556. if (error) {
  557. /*
  558. * This can happen if quotas got turned off (ESRCH),
  559. * or if the dquot didn't exist on disk and we ask to
  560. * allocate (ENOENT).
  561. */
  562. trace_xfs_dqread_fail(dqp);
  563. cancelflags |= XFS_TRANS_ABORT;
  564. goto error1;
  565. }
  566. /* copy everything from disk dquot to the incore dquot */
  567. memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
  568. xfs_qm_dquot_logitem_init(dqp);
  569. /*
  570. * Reservation counters are defined as reservation plus current usage
  571. * to avoid having to add every time.
  572. */
  573. dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
  574. dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
  575. dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
  576. /* initialize the dquot speculative prealloc thresholds */
  577. xfs_dquot_set_prealloc_limits(dqp);
  578. /* Mark the buf so that this will stay incore a little longer */
  579. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  580. /*
  581. * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
  582. * So we need to release with xfs_trans_brelse().
  583. * The strategy here is identical to that of inodes; we lock
  584. * the dquot in xfs_qm_dqget() before making it accessible to
  585. * others. This is because dquots, like inodes, need a good level of
  586. * concurrency, and we don't want to take locks on the entire buffers
  587. * for dquot accesses.
  588. * Note also that the dquot buffer may even be dirty at this point, if
  589. * this particular dquot was repaired. We still aren't afraid to
  590. * brelse it because we have the changes incore.
  591. */
  592. ASSERT(xfs_buf_islocked(bp));
  593. xfs_trans_brelse(tp, bp);
  594. if (tp) {
  595. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  596. if (error)
  597. goto error0;
  598. }
  599. *O_dqpp = dqp;
  600. return error;
  601. error1:
  602. if (tp)
  603. xfs_trans_cancel(tp, cancelflags);
  604. error0:
  605. xfs_qm_dqdestroy(dqp);
  606. *O_dqpp = NULL;
  607. return error;
  608. }
  609. /*
  610. * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
  611. * a locked dquot, doing an allocation (if requested) as needed.
  612. * When both an inode and an id are given, the inode's id takes precedence.
  613. * That is, if the id changes while we don't hold the ilock inside this
  614. * function, the new dquot is returned, not necessarily the one requested
  615. * in the id argument.
  616. */
  617. int
  618. xfs_qm_dqget(
  619. xfs_mount_t *mp,
  620. xfs_inode_t *ip, /* locked inode (optional) */
  621. xfs_dqid_t id, /* uid/projid/gid depending on type */
  622. uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
  623. uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
  624. xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
  625. {
  626. struct xfs_quotainfo *qi = mp->m_quotainfo;
  627. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  628. struct xfs_dquot *dqp;
  629. int error;
  630. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  631. if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
  632. (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
  633. (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
  634. return -ESRCH;
  635. }
  636. #ifdef DEBUG
  637. if (xfs_do_dqerror) {
  638. if ((xfs_dqerror_target == mp->m_ddev_targp) &&
  639. (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
  640. xfs_debug(mp, "Returning error in dqget");
  641. return -EIO;
  642. }
  643. }
  644. ASSERT(type == XFS_DQ_USER ||
  645. type == XFS_DQ_PROJ ||
  646. type == XFS_DQ_GROUP);
  647. if (ip) {
  648. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  649. ASSERT(xfs_inode_dquot(ip, type) == NULL);
  650. }
  651. #endif
  652. restart:
  653. mutex_lock(&qi->qi_tree_lock);
  654. dqp = radix_tree_lookup(tree, id);
  655. if (dqp) {
  656. xfs_dqlock(dqp);
  657. if (dqp->dq_flags & XFS_DQ_FREEING) {
  658. xfs_dqunlock(dqp);
  659. mutex_unlock(&qi->qi_tree_lock);
  660. trace_xfs_dqget_freeing(dqp);
  661. delay(1);
  662. goto restart;
  663. }
  664. dqp->q_nrefs++;
  665. mutex_unlock(&qi->qi_tree_lock);
  666. trace_xfs_dqget_hit(dqp);
  667. XFS_STATS_INC(xs_qm_dqcachehits);
  668. *O_dqpp = dqp;
  669. return 0;
  670. }
  671. mutex_unlock(&qi->qi_tree_lock);
  672. XFS_STATS_INC(xs_qm_dqcachemisses);
  673. /*
  674. * Dquot cache miss. We don't want to keep the inode lock across
  675. * a (potential) disk read. Also we don't want to deal with the lock
  676. * ordering between quotainode and this inode. OTOH, dropping the inode
  677. * lock here means dealing with a chown that can happen before
  678. * we re-acquire the lock.
  679. */
  680. if (ip)
  681. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  682. error = xfs_qm_dqread(mp, id, type, flags, &dqp);
  683. if (ip)
  684. xfs_ilock(ip, XFS_ILOCK_EXCL);
  685. if (error)
  686. return error;
  687. if (ip) {
  688. /*
  689. * A dquot could be attached to this inode by now, since
  690. * we had dropped the ilock.
  691. */
  692. if (xfs_this_quota_on(mp, type)) {
  693. struct xfs_dquot *dqp1;
  694. dqp1 = xfs_inode_dquot(ip, type);
  695. if (dqp1) {
  696. xfs_qm_dqdestroy(dqp);
  697. dqp = dqp1;
  698. xfs_dqlock(dqp);
  699. goto dqret;
  700. }
  701. } else {
  702. /* inode stays locked on return */
  703. xfs_qm_dqdestroy(dqp);
  704. return -ESRCH;
  705. }
  706. }
  707. mutex_lock(&qi->qi_tree_lock);
  708. error = radix_tree_insert(tree, id, dqp);
  709. if (unlikely(error)) {
  710. WARN_ON(error != -EEXIST);
  711. /*
  712. * Duplicate found. Just throw away the new dquot and start
  713. * over.
  714. */
  715. mutex_unlock(&qi->qi_tree_lock);
  716. trace_xfs_dqget_dup(dqp);
  717. xfs_qm_dqdestroy(dqp);
  718. XFS_STATS_INC(xs_qm_dquot_dups);
  719. goto restart;
  720. }
  721. /*
  722. * We return a locked dquot to the caller, with a reference taken
  723. */
  724. xfs_dqlock(dqp);
  725. dqp->q_nrefs = 1;
  726. qi->qi_dquots++;
  727. mutex_unlock(&qi->qi_tree_lock);
  728. dqret:
  729. ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
  730. trace_xfs_dqget_miss(dqp);
  731. *O_dqpp = dqp;
  732. return 0;
  733. }
  734. /*
  735. * Release a reference to the dquot (decrement ref-count) and unlock it.
  736. *
  737. * If there is a group quota attached to this dquot, carefully release that
  738. * too without tripping over deadlocks'n'stuff.
  739. */
  740. void
  741. xfs_qm_dqput(
  742. struct xfs_dquot *dqp)
  743. {
  744. ASSERT(dqp->q_nrefs > 0);
  745. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  746. trace_xfs_dqput(dqp);
  747. if (--dqp->q_nrefs == 0) {
  748. struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;
  749. trace_xfs_dqput_free(dqp);
  750. if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
  751. XFS_STATS_INC(xs_qm_dquot_unused);
  752. }
  753. xfs_dqunlock(dqp);
  754. }
  755. /*
  756. * Release a dquot. Flush it if dirty, then dqput() it.
  757. * dquot must not be locked.
  758. */
  759. void
  760. xfs_qm_dqrele(
  761. xfs_dquot_t *dqp)
  762. {
  763. if (!dqp)
  764. return;
  765. trace_xfs_dqrele(dqp);
  766. xfs_dqlock(dqp);
  767. /*
  768. * We don't care to flush it if the dquot is dirty here.
  769. * That will create stutters that we want to avoid.
  770. * Instead we do a delayed write when we try to reclaim
  771. * a dirty dquot. Also xfs_sync will take part of the burden...
  772. */
  773. xfs_qm_dqput(dqp);
  774. }
  775. /*
  776. * This is the dquot flushing I/O completion routine. It is called
  777. * from interrupt level when the buffer containing the dquot is
  778. * flushed to disk. It is responsible for removing the dquot logitem
  779. * from the AIL if it has not been re-logged, and unlocking the dquot's
  780. * flush lock. This behavior is very similar to that of inodes..
  781. */
  782. STATIC void
  783. xfs_qm_dqflush_done(
  784. struct xfs_buf *bp,
  785. struct xfs_log_item *lip)
  786. {
  787. xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
  788. xfs_dquot_t *dqp = qip->qli_dquot;
  789. struct xfs_ail *ailp = lip->li_ailp;
  790. /*
  791. * We only want to pull the item from the AIL if its
  792. * location in the log has not changed since we started the flush.
  793. * Thus, we only bother if the dquot's lsn has
  794. * not changed. First we check the lsn outside the lock
  795. * since it's cheaper, and then we recheck while
  796. * holding the lock before removing the dquot from the AIL.
  797. */
  798. if ((lip->li_flags & XFS_LI_IN_AIL) &&
  799. lip->li_lsn == qip->qli_flush_lsn) {
  800. /* xfs_trans_ail_delete() drops the AIL lock. */
  801. spin_lock(&ailp->xa_lock);
  802. if (lip->li_lsn == qip->qli_flush_lsn)
  803. xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
  804. else
  805. spin_unlock(&ailp->xa_lock);
  806. }
  807. /*
  808. * Release the dq's flush lock since we're done with it.
  809. */
  810. xfs_dqfunlock(dqp);
  811. }
  812. /*
  813. * Write a modified dquot to disk.
  814. * The dquot must be locked and the flush lock too taken by caller.
  815. * The flush lock will not be unlocked until the dquot reaches the disk,
  816. * but the dquot is free to be unlocked and modified by the caller
  817. * in the interim. Dquot is still locked on return. This behavior is
  818. * identical to that of inodes.
  819. */
  820. int
  821. xfs_qm_dqflush(
  822. struct xfs_dquot *dqp,
  823. struct xfs_buf **bpp)
  824. {
  825. struct xfs_mount *mp = dqp->q_mount;
  826. struct xfs_buf *bp;
  827. struct xfs_disk_dquot *ddqp;
  828. int error;
  829. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  830. ASSERT(!completion_done(&dqp->q_flush));
  831. trace_xfs_dqflush(dqp);
  832. *bpp = NULL;
  833. xfs_qm_dqunpin_wait(dqp);
  834. /*
  835. * This may have been unpinned because the filesystem is shutting
  836. * down forcibly. If that's the case we must not write this dquot
  837. * to disk, because the log record didn't make it to disk.
  838. *
  839. * We also have to remove the log item from the AIL in this case,
  840. * as we wait for an emptry AIL as part of the unmount process.
  841. */
  842. if (XFS_FORCED_SHUTDOWN(mp)) {
  843. struct xfs_log_item *lip = &dqp->q_logitem.qli_item;
  844. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  845. spin_lock(&mp->m_ail->xa_lock);
  846. if (lip->li_flags & XFS_LI_IN_AIL)
  847. xfs_trans_ail_delete(mp->m_ail, lip,
  848. SHUTDOWN_CORRUPT_INCORE);
  849. else
  850. spin_unlock(&mp->m_ail->xa_lock);
  851. error = -EIO;
  852. goto out_unlock;
  853. }
  854. /*
  855. * Get the buffer containing the on-disk dquot
  856. */
  857. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
  858. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  859. &xfs_dquot_buf_ops);
  860. if (error)
  861. goto out_unlock;
  862. /*
  863. * Calculate the location of the dquot inside the buffer.
  864. */
  865. ddqp = bp->b_addr + dqp->q_bufoffset;
  866. /*
  867. * A simple sanity check in case we got a corrupted dquot..
  868. */
  869. error = xfs_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
  870. XFS_QMOPT_DOWARN, "dqflush (incore copy)");
  871. if (error) {
  872. xfs_buf_relse(bp);
  873. xfs_dqfunlock(dqp);
  874. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  875. return -EIO;
  876. }
  877. /* This is the only portion of data that needs to persist */
  878. memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
  879. /*
  880. * Clear the dirty field and remember the flush lsn for later use.
  881. */
  882. dqp->dq_flags &= ~XFS_DQ_DIRTY;
  883. xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
  884. &dqp->q_logitem.qli_item.li_lsn);
  885. /*
  886. * copy the lsn into the on-disk dquot now while we have the in memory
  887. * dquot here. This can't be done later in the write verifier as we
  888. * can't get access to the log item at that point in time.
  889. *
  890. * We also calculate the CRC here so that the on-disk dquot in the
  891. * buffer always has a valid CRC. This ensures there is no possibility
  892. * of a dquot without an up-to-date CRC getting to disk.
  893. */
  894. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  895. struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddqp;
  896. dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
  897. xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
  898. XFS_DQUOT_CRC_OFF);
  899. }
  900. /*
  901. * Attach an iodone routine so that we can remove this dquot from the
  902. * AIL and release the flush lock once the dquot is synced to disk.
  903. */
  904. xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
  905. &dqp->q_logitem.qli_item);
  906. /*
  907. * If the buffer is pinned then push on the log so we won't
  908. * get stuck waiting in the write for too long.
  909. */
  910. if (xfs_buf_ispinned(bp)) {
  911. trace_xfs_dqflush_force(dqp);
  912. xfs_log_force(mp, 0);
  913. }
  914. trace_xfs_dqflush_done(dqp);
  915. *bpp = bp;
  916. return 0;
  917. out_unlock:
  918. xfs_dqfunlock(dqp);
  919. return -EIO;
  920. }
  921. /*
  922. * Lock two xfs_dquot structures.
  923. *
  924. * To avoid deadlocks we always lock the quota structure with
  925. * the lowerd id first.
  926. */
  927. void
  928. xfs_dqlock2(
  929. xfs_dquot_t *d1,
  930. xfs_dquot_t *d2)
  931. {
  932. if (d1 && d2) {
  933. ASSERT(d1 != d2);
  934. if (be32_to_cpu(d1->q_core.d_id) >
  935. be32_to_cpu(d2->q_core.d_id)) {
  936. mutex_lock(&d2->q_qlock);
  937. mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
  938. } else {
  939. mutex_lock(&d1->q_qlock);
  940. mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
  941. }
  942. } else if (d1) {
  943. mutex_lock(&d1->q_qlock);
  944. } else if (d2) {
  945. mutex_lock(&d2->q_qlock);
  946. }
  947. }
  948. int __init
  949. xfs_qm_init(void)
  950. {
  951. xfs_qm_dqzone =
  952. kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
  953. if (!xfs_qm_dqzone)
  954. goto out;
  955. xfs_qm_dqtrxzone =
  956. kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
  957. if (!xfs_qm_dqtrxzone)
  958. goto out_free_dqzone;
  959. return 0;
  960. out_free_dqzone:
  961. kmem_zone_destroy(xfs_qm_dqzone);
  962. out:
  963. return -ENOMEM;
  964. }
  965. void
  966. xfs_qm_exit(void)
  967. {
  968. kmem_zone_destroy(xfs_qm_dqtrxzone);
  969. kmem_zone_destroy(xfs_qm_dqzone);
  970. }