xfs_discard.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_format.h"
  8. #include "xfs_log_format.h"
  9. #include "xfs_trans_resv.h"
  10. #include "xfs_sb.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_quota.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_btree.h"
  15. #include "xfs_alloc_btree.h"
  16. #include "xfs_alloc.h"
  17. #include "xfs_error.h"
  18. #include "xfs_extent_busy.h"
  19. #include "xfs_discard.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_log.h"
  22. STATIC int
  23. xfs_trim_extents(
  24. struct xfs_mount *mp,
  25. xfs_agnumber_t agno,
  26. xfs_daddr_t start,
  27. xfs_daddr_t end,
  28. xfs_daddr_t minlen,
  29. uint64_t *blocks_trimmed)
  30. {
  31. struct block_device *bdev = mp->m_ddev_targp->bt_bdev;
  32. struct xfs_btree_cur *cur;
  33. struct xfs_buf *agbp;
  34. struct xfs_perag *pag;
  35. int error;
  36. int i;
  37. pag = xfs_perag_get(mp, agno);
  38. /*
  39. * Force out the log. This means any transactions that might have freed
  40. * space before we take the AGF buffer lock are now on disk, and the
  41. * volatile disk cache is flushed.
  42. */
  43. xfs_log_force(mp, XFS_LOG_SYNC);
  44. error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
  45. if (error || !agbp)
  46. goto out_put_perag;
  47. cur = xfs_allocbt_init_cursor(mp, NULL, agbp, agno, XFS_BTNUM_CNT);
  48. /*
  49. * Look up the longest btree in the AGF and start with it.
  50. */
  51. error = xfs_alloc_lookup_ge(cur, 0,
  52. be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_longest), &i);
  53. if (error)
  54. goto out_del_cursor;
  55. /*
  56. * Loop until we are done with all extents that are large
  57. * enough to be worth discarding.
  58. */
  59. while (i) {
  60. xfs_agblock_t fbno;
  61. xfs_extlen_t flen;
  62. xfs_daddr_t dbno;
  63. xfs_extlen_t dlen;
  64. error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
  65. if (error)
  66. goto out_del_cursor;
  67. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_del_cursor);
  68. ASSERT(flen <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_longest));
  69. /*
  70. * use daddr format for all range/len calculations as that is
  71. * the format the range/len variables are supplied in by
  72. * userspace.
  73. */
  74. dbno = XFS_AGB_TO_DADDR(mp, agno, fbno);
  75. dlen = XFS_FSB_TO_BB(mp, flen);
  76. /*
  77. * Too small? Give up.
  78. */
  79. if (dlen < minlen) {
  80. trace_xfs_discard_toosmall(mp, agno, fbno, flen);
  81. goto out_del_cursor;
  82. }
  83. /*
  84. * If the extent is entirely outside of the range we are
  85. * supposed to discard skip it. Do not bother to trim
  86. * down partially overlapping ranges for now.
  87. */
  88. if (dbno + dlen < start || dbno > end) {
  89. trace_xfs_discard_exclude(mp, agno, fbno, flen);
  90. goto next_extent;
  91. }
  92. /*
  93. * If any blocks in the range are still busy, skip the
  94. * discard and try again the next time.
  95. */
  96. if (xfs_extent_busy_search(mp, agno, fbno, flen)) {
  97. trace_xfs_discard_busy(mp, agno, fbno, flen);
  98. goto next_extent;
  99. }
  100. trace_xfs_discard_extent(mp, agno, fbno, flen);
  101. error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS, 0);
  102. if (error)
  103. goto out_del_cursor;
  104. *blocks_trimmed += flen;
  105. next_extent:
  106. error = xfs_btree_decrement(cur, 0, &i);
  107. if (error)
  108. goto out_del_cursor;
  109. if (fatal_signal_pending(current)) {
  110. error = -ERESTARTSYS;
  111. goto out_del_cursor;
  112. }
  113. }
  114. out_del_cursor:
  115. xfs_btree_del_cursor(cur, error);
  116. xfs_buf_relse(agbp);
  117. out_put_perag:
  118. xfs_perag_put(pag);
  119. return error;
  120. }
  121. /*
  122. * trim a range of the filesystem.
  123. *
  124. * Note: the parameters passed from userspace are byte ranges into the
  125. * filesystem which does not match to the format we use for filesystem block
  126. * addressing. FSB addressing is sparse (AGNO|AGBNO), while the incoming format
  127. * is a linear address range. Hence we need to use DADDR based conversions and
  128. * comparisons for determining the correct offset and regions to trim.
  129. */
  130. int
  131. xfs_ioc_trim(
  132. struct xfs_mount *mp,
  133. struct fstrim_range __user *urange)
  134. {
  135. struct request_queue *q = bdev_get_queue(mp->m_ddev_targp->bt_bdev);
  136. unsigned int granularity = q->limits.discard_granularity;
  137. struct fstrim_range range;
  138. xfs_daddr_t start, end, minlen;
  139. xfs_agnumber_t start_agno, end_agno, agno;
  140. uint64_t blocks_trimmed = 0;
  141. int error, last_error = 0;
  142. if (!capable(CAP_SYS_ADMIN))
  143. return -EPERM;
  144. if (!blk_queue_discard(q))
  145. return -EOPNOTSUPP;
  146. if (copy_from_user(&range, urange, sizeof(range)))
  147. return -EFAULT;
  148. /*
  149. * Truncating down the len isn't actually quite correct, but using
  150. * BBTOB would mean we trivially get overflows for values
  151. * of ULLONG_MAX or slightly lower. And ULLONG_MAX is the default
  152. * used by the fstrim application. In the end it really doesn't
  153. * matter as trimming blocks is an advisory interface.
  154. */
  155. if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) ||
  156. range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
  157. range.len < mp->m_sb.sb_blocksize)
  158. return -EINVAL;
  159. start = BTOBB(range.start);
  160. end = start + BTOBBT(range.len) - 1;
  161. minlen = BTOBB(max_t(u64, granularity, range.minlen));
  162. if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
  163. end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)- 1;
  164. start_agno = xfs_daddr_to_agno(mp, start);
  165. end_agno = xfs_daddr_to_agno(mp, end);
  166. for (agno = start_agno; agno <= end_agno; agno++) {
  167. error = xfs_trim_extents(mp, agno, start, end, minlen,
  168. &blocks_trimmed);
  169. if (error) {
  170. last_error = error;
  171. if (error == -ERESTARTSYS)
  172. break;
  173. }
  174. }
  175. if (last_error)
  176. return last_error;
  177. range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
  178. if (copy_to_user(urange, &range, sizeof(range)))
  179. return -EFAULT;
  180. return 0;
  181. }