xfs_dquot.c 27 KB

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