xfs_dquot.c 28 KB

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