xfs_discard.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_daddr_t start,
  40. xfs_daddr_t end,
  41. xfs_daddr_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_ge(cur, 0,
  65. be32_to_cpu(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. xfs_daddr_t dbno;
  76. xfs_extlen_t dlen;
  77. error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
  78. if (error)
  79. goto out_del_cursor;
  80. XFS_WANT_CORRUPTED_GOTO(i == 1, out_del_cursor);
  81. ASSERT(flen <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_longest));
  82. /*
  83. * use daddr format for all range/len calculations as that is
  84. * the format the range/len variables are supplied in by
  85. * userspace.
  86. */
  87. dbno = XFS_AGB_TO_DADDR(mp, agno, fbno);
  88. dlen = XFS_FSB_TO_BB(mp, flen);
  89. /*
  90. * Too small? Give up.
  91. */
  92. if (dlen < minlen) {
  93. trace_xfs_discard_toosmall(mp, agno, fbno, flen);
  94. goto out_del_cursor;
  95. }
  96. /*
  97. * If the extent is entirely outside of the range we are
  98. * supposed to discard skip it. Do not bother to trim
  99. * down partially overlapping ranges for now.
  100. */
  101. if (dbno + dlen < start || dbno > end) {
  102. trace_xfs_discard_exclude(mp, agno, fbno, flen);
  103. goto next_extent;
  104. }
  105. /*
  106. * If any blocks in the range are still busy, skip the
  107. * discard and try again the next time.
  108. */
  109. if (xfs_alloc_busy_search(mp, agno, fbno, flen)) {
  110. trace_xfs_discard_busy(mp, agno, fbno, flen);
  111. goto next_extent;
  112. }
  113. trace_xfs_discard_extent(mp, agno, fbno, flen);
  114. error = -blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS, 0);
  115. if (error)
  116. goto out_del_cursor;
  117. *blocks_trimmed += flen;
  118. next_extent:
  119. error = xfs_btree_decrement(cur, 0, &i);
  120. if (error)
  121. goto out_del_cursor;
  122. }
  123. out_del_cursor:
  124. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  125. xfs_buf_relse(agbp);
  126. out_put_perag:
  127. xfs_perag_put(pag);
  128. return error;
  129. }
  130. /*
  131. * trim a range of the filesystem.
  132. *
  133. * Note: the parameters passed from userspace are byte ranges into the
  134. * filesystem which does not match to the format we use for filesystem block
  135. * addressing. FSB addressing is sparse (AGNO|AGBNO), while the incoming format
  136. * is a linear address range. Hence we need to use DADDR based conversions and
  137. * comparisons for determining the correct offset and regions to trim.
  138. */
  139. int
  140. xfs_ioc_trim(
  141. struct xfs_mount *mp,
  142. struct fstrim_range __user *urange)
  143. {
  144. struct request_queue *q = mp->m_ddev_targp->bt_bdev->bd_disk->queue;
  145. unsigned int granularity = q->limits.discard_granularity;
  146. struct fstrim_range range;
  147. xfs_daddr_t start, end, minlen;
  148. xfs_agnumber_t start_agno, end_agno, agno;
  149. __uint64_t blocks_trimmed = 0;
  150. int error, last_error = 0;
  151. if (!capable(CAP_SYS_ADMIN))
  152. return -XFS_ERROR(EPERM);
  153. if (!blk_queue_discard(q))
  154. return -XFS_ERROR(EOPNOTSUPP);
  155. if (copy_from_user(&range, urange, sizeof(range)))
  156. return -XFS_ERROR(EFAULT);
  157. /*
  158. * Truncating down the len isn't actually quite correct, but using
  159. * BBTOB would mean we trivially get overflows for values
  160. * of ULLONG_MAX or slightly lower. And ULLONG_MAX is the default
  161. * used by the fstrim application. In the end it really doesn't
  162. * matter as trimming blocks is an advisory interface.
  163. */
  164. start = BTOBB(range.start);
  165. end = start + BTOBBT(range.len) - 1;
  166. minlen = BTOBB(max_t(u64, granularity, range.minlen));
  167. if (XFS_BB_TO_FSB(mp, start) >= mp->m_sb.sb_dblocks)
  168. return -XFS_ERROR(EINVAL);
  169. if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
  170. end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)- 1;
  171. start_agno = xfs_daddr_to_agno(mp, start);
  172. end_agno = xfs_daddr_to_agno(mp, end);
  173. for (agno = start_agno; agno <= end_agno; agno++) {
  174. error = -xfs_trim_extents(mp, agno, start, end, minlen,
  175. &blocks_trimmed);
  176. if (error)
  177. last_error = error;
  178. }
  179. if (last_error)
  180. return last_error;
  181. range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
  182. if (copy_to_user(urange, &range, sizeof(range)))
  183. return -XFS_ERROR(EFAULT);
  184. return 0;
  185. }
  186. int
  187. xfs_discard_extents(
  188. struct xfs_mount *mp,
  189. struct list_head *list)
  190. {
  191. struct xfs_busy_extent *busyp;
  192. int error = 0;
  193. list_for_each_entry(busyp, list, list) {
  194. trace_xfs_discard_extent(mp, busyp->agno, busyp->bno,
  195. busyp->length);
  196. error = -blkdev_issue_discard(mp->m_ddev_targp->bt_bdev,
  197. XFS_AGB_TO_DADDR(mp, busyp->agno, busyp->bno),
  198. XFS_FSB_TO_BB(mp, busyp->length),
  199. GFP_NOFS, 0);
  200. if (error && error != EOPNOTSUPP) {
  201. xfs_info(mp,
  202. "discard failed for extent [0x%llu,%u], error %d",
  203. (unsigned long long)busyp->bno,
  204. busyp->length,
  205. error);
  206. return error;
  207. }
  208. }
  209. return 0;
  210. }