xfs_discard.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2010 Red Hat, 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_sb.h"
  20. #include "xfs_inum.h"
  21. #include "xfs_log.h"
  22. #include "xfs_ag.h"
  23. #include "xfs_mount.h"
  24. #include "xfs_quota.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_alloc_btree.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_ialloc_btree.h"
  29. #include "xfs_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_alloc.h"
  32. #include "xfs_error.h"
  33. #include "xfs_discard.h"
  34. #include "xfs_trace.h"
  35. STATIC int
  36. xfs_trim_extents(
  37. struct xfs_mount *mp,
  38. xfs_agnumber_t agno,
  39. xfs_fsblock_t start,
  40. xfs_fsblock_t len,
  41. xfs_fsblock_t minlen,
  42. __uint64_t *blocks_trimmed)
  43. {
  44. struct block_device *bdev = mp->m_ddev_targp->bt_bdev;
  45. struct xfs_btree_cur *cur;
  46. struct xfs_buf *agbp;
  47. struct xfs_perag *pag;
  48. int error;
  49. int i;
  50. pag = xfs_perag_get(mp, agno);
  51. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  52. if (error || !agbp)
  53. goto out_put_perag;
  54. cur = xfs_allocbt_init_cursor(mp, NULL, agbp, agno, XFS_BTNUM_CNT);
  55. /*
  56. * Force out the log. This means any transactions that might have freed
  57. * space before we took the AGF buffer lock are now on disk, and the
  58. * volatile disk cache is flushed.
  59. */
  60. xfs_log_force(mp, XFS_LOG_SYNC);
  61. /*
  62. * Look up the longest btree in the AGF and start with it.
  63. */
  64. error = xfs_alloc_lookup_le(cur, 0,
  65. XFS_BUF_TO_AGF(agbp)->agf_longest, &i);
  66. if (error)
  67. goto out_del_cursor;
  68. /*
  69. * Loop until we are done with all extents that are large
  70. * enough to be worth discarding.
  71. */
  72. while (i) {
  73. xfs_agblock_t fbno;
  74. xfs_extlen_t flen;
  75. error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
  76. if (error)
  77. goto out_del_cursor;
  78. XFS_WANT_CORRUPTED_GOTO(i == 1, out_del_cursor);
  79. ASSERT(flen <= XFS_BUF_TO_AGF(agbp)->agf_longest);
  80. /*
  81. * Too small? Give up.
  82. */
  83. if (flen < minlen) {
  84. trace_xfs_discard_toosmall(mp, agno, fbno, flen);
  85. goto out_del_cursor;
  86. }
  87. /*
  88. * If the extent is entirely outside of the range we are
  89. * supposed to discard skip it. Do not bother to trim
  90. * down partially overlapping ranges for now.
  91. */
  92. if (XFS_AGB_TO_FSB(mp, agno, fbno) + flen < start ||
  93. XFS_AGB_TO_FSB(mp, agno, fbno) >= start + len) {
  94. trace_xfs_discard_exclude(mp, agno, fbno, flen);
  95. goto next_extent;
  96. }
  97. /*
  98. * If any blocks in the range are still busy, skip the
  99. * discard and try again the next time.
  100. */
  101. if (xfs_alloc_busy_search(mp, agno, fbno, flen)) {
  102. trace_xfs_discard_busy(mp, agno, fbno, flen);
  103. goto next_extent;
  104. }
  105. trace_xfs_discard_extent(mp, agno, fbno, flen);
  106. error = -blkdev_issue_discard(bdev,
  107. XFS_AGB_TO_DADDR(mp, agno, fbno),
  108. XFS_FSB_TO_BB(mp, flen),
  109. GFP_NOFS, 0);
  110. if (error)
  111. goto out_del_cursor;
  112. *blocks_trimmed += flen;
  113. next_extent:
  114. error = xfs_btree_decrement(cur, 0, &i);
  115. if (error)
  116. goto out_del_cursor;
  117. }
  118. out_del_cursor:
  119. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  120. xfs_buf_relse(agbp);
  121. out_put_perag:
  122. xfs_perag_put(pag);
  123. return error;
  124. }
  125. int
  126. xfs_ioc_trim(
  127. struct xfs_mount *mp,
  128. struct fstrim_range __user *urange)
  129. {
  130. struct request_queue *q = mp->m_ddev_targp->bt_bdev->bd_disk->queue;
  131. unsigned int granularity = q->limits.discard_granularity;
  132. struct fstrim_range range;
  133. xfs_fsblock_t start, len, minlen;
  134. xfs_agnumber_t start_agno, end_agno, agno;
  135. __uint64_t blocks_trimmed = 0;
  136. int error, last_error = 0;
  137. if (!capable(CAP_SYS_ADMIN))
  138. return -XFS_ERROR(EPERM);
  139. if (!blk_queue_discard(q))
  140. return -XFS_ERROR(EOPNOTSUPP);
  141. if (copy_from_user(&range, urange, sizeof(range)))
  142. return -XFS_ERROR(EFAULT);
  143. /*
  144. * Truncating down the len isn't actually quite correct, but using
  145. * XFS_B_TO_FSB would mean we trivially get overflows for values
  146. * of ULLONG_MAX or slightly lower. And ULLONG_MAX is the default
  147. * used by the fstrim application. In the end it really doesn't
  148. * matter as trimming blocks is an advisory interface.
  149. */
  150. start = XFS_B_TO_FSBT(mp, range.start);
  151. len = XFS_B_TO_FSBT(mp, range.len);
  152. minlen = XFS_B_TO_FSB(mp, max_t(u64, granularity, range.minlen));
  153. start_agno = XFS_FSB_TO_AGNO(mp, start);
  154. if (start_agno >= mp->m_sb.sb_agcount)
  155. return -XFS_ERROR(EINVAL);
  156. end_agno = XFS_FSB_TO_AGNO(mp, start + len);
  157. if (end_agno >= mp->m_sb.sb_agcount)
  158. end_agno = mp->m_sb.sb_agcount - 1;
  159. for (agno = start_agno; agno <= end_agno; agno++) {
  160. error = -xfs_trim_extents(mp, agno, start, len, minlen,
  161. &blocks_trimmed);
  162. if (error)
  163. last_error = error;
  164. }
  165. if (last_error)
  166. return last_error;
  167. range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
  168. if (copy_to_user(urange, &range, sizeof(range)))
  169. return -XFS_ERROR(EFAULT);
  170. return 0;
  171. }