xfs_dquot.c 26 KB

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