xfs_discard.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_shared.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_btree.h"
  13. #include "xfs_alloc_btree.h"
  14. #include "xfs_alloc.h"
  15. #include "xfs_discard.h"
  16. #include "xfs_error.h"
  17. #include "xfs_extent_busy.h"
  18. #include "xfs_trace.h"
  19. #include "xfs_log.h"
  20. #include "xfs_ag.h"
  21. STATIC int
  22. xfs_trim_extents(
  23. struct xfs_mount *mp,
  24. xfs_agnumber_t agno,
  25. xfs_daddr_t start,
  26. xfs_daddr_t end,
  27. xfs_daddr_t minlen,
  28. uint64_t *blocks_trimmed)
  29. {
  30. struct block_device *bdev = mp->m_ddev_targp->bt_bdev;
  31. struct xfs_btree_cur *cur;
  32. struct xfs_buf *agbp;
  33. struct xfs_agf *agf;
  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(pag, NULL, 0, &agbp);
  45. if (error)
  46. goto out_put_perag;
  47. agf = agbp->b_addr;
  48. cur = xfs_allocbt_init_cursor(mp, NULL, agbp, pag, XFS_BTNUM_CNT);
  49. /*
  50. * Look up the longest btree in the AGF and start with it.
  51. */
  52. error = xfs_alloc_lookup_ge(cur, 0, be32_to_cpu(agf->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. if (XFS_IS_CORRUPT(mp, i != 1)) {
  68. error = -EFSCORRUPTED;
  69. goto out_del_cursor;
  70. }
  71. ASSERT(flen <= be32_to_cpu(agf->agf_longest));
  72. /*
  73. * use daddr format for all range/len calculations as that is
  74. * the format the range/len variables are supplied in by
  75. * userspace.
  76. */
  77. dbno = XFS_AGB_TO_DADDR(mp, agno, fbno);
  78. dlen = XFS_FSB_TO_BB(mp, flen);
  79. /*
  80. * Too small? Give up.
  81. */
  82. if (dlen < minlen) {
  83. trace_xfs_discard_toosmall(mp, agno, fbno, flen);
  84. goto out_del_cursor;
  85. }
  86. /*
  87. * If the extent is entirely outside of the range we are
  88. * supposed to discard skip it. Do not bother to trim
  89. * down partially overlapping ranges for now.
  90. */
  91. if (dbno + dlen < start || dbno > end) {
  92. trace_xfs_discard_exclude(mp, agno, fbno, flen);
  93. goto next_extent;
  94. }
  95. /*
  96. * If any blocks in the range are still busy, skip the
  97. * discard and try again the next time.
  98. */
  99. if (xfs_extent_busy_search(mp, pag, fbno, flen)) {
  100. trace_xfs_discard_busy(mp, agno, fbno, flen);
  101. goto next_extent;
  102. }
  103. trace_xfs_discard_extent(mp, agno, fbno, flen);
  104. error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS);
  105. if (error)
  106. goto out_del_cursor;
  107. *blocks_trimmed += flen;
  108. next_extent:
  109. error = xfs_btree_decrement(cur, 0, &i);
  110. if (error)
  111. goto out_del_cursor;
  112. if (fatal_signal_pending(current)) {
  113. error = -ERESTARTSYS;
  114. goto out_del_cursor;
  115. }
  116. }
  117. out_del_cursor:
  118. xfs_btree_del_cursor(cur, error);
  119. xfs_buf_relse(agbp);
  120. out_put_perag:
  121. xfs_perag_put(pag);
  122. return error;
  123. }
  124. /*
  125. * trim a range of the filesystem.
  126. *
  127. * Note: the parameters passed from userspace are byte ranges into the
  128. * filesystem which does not match to the format we use for filesystem block
  129. * addressing. FSB addressing is sparse (AGNO|AGBNO), while the incoming format
  130. * is a linear address range. Hence we need to use DADDR based conversions and
  131. * comparisons for determining the correct offset and regions to trim.
  132. */
  133. int
  134. xfs_ioc_trim(
  135. struct xfs_mount *mp,
  136. struct fstrim_range __user *urange)
  137. {
  138. unsigned int granularity =
  139. bdev_discard_granularity(mp->m_ddev_targp->bt_bdev);
  140. struct fstrim_range range;
  141. xfs_daddr_t start, end, minlen;
  142. xfs_agnumber_t start_agno, end_agno, agno;
  143. uint64_t blocks_trimmed = 0;
  144. int error, last_error = 0;
  145. if (!capable(CAP_SYS_ADMIN))
  146. return -EPERM;
  147. if (!bdev_max_discard_sectors(mp->m_ddev_targp->bt_bdev))
  148. return -EOPNOTSUPP;
  149. /*
  150. * We haven't recovered the log, so we cannot use our bnobt-guided
  151. * storage zapping commands.
  152. */
  153. if (xfs_has_norecovery(mp))
  154. return -EROFS;
  155. if (copy_from_user(&range, urange, sizeof(range)))
  156. return -EFAULT;
  157. range.minlen = max_t(u64, granularity, range.minlen);
  158. minlen = BTOBB(range.minlen);
  159. /*
  160. * Truncating down the len isn't actually quite correct, but using
  161. * BBTOB would mean we trivially get overflows for values
  162. * of ULLONG_MAX or slightly lower. And ULLONG_MAX is the default
  163. * used by the fstrim application. In the end it really doesn't
  164. * matter as trimming blocks is an advisory interface.
  165. */
  166. if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) ||
  167. range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
  168. range.len < mp->m_sb.sb_blocksize)
  169. return -EINVAL;
  170. start = BTOBB(range.start);
  171. end = start + BTOBBT(range.len) - 1;
  172. if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
  173. end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)- 1;
  174. start_agno = xfs_daddr_to_agno(mp, start);
  175. end_agno = xfs_daddr_to_agno(mp, end);
  176. for (agno = start_agno; agno <= end_agno; agno++) {
  177. error = xfs_trim_extents(mp, agno, start, end, minlen,
  178. &blocks_trimmed);
  179. if (error) {
  180. last_error = error;
  181. if (error == -ERESTARTSYS)
  182. break;
  183. }
  184. }
  185. if (last_error)
  186. return last_error;
  187. range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
  188. if (copy_to_user(urange, &range, sizeof(range)))
  189. return -EFAULT;
  190. return 0;
  191. }