Line data Source code
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 :
22 : STATIC int
23 19835 : xfs_trim_extents(
24 : struct xfs_perag *pag,
25 : xfs_daddr_t start,
26 : xfs_daddr_t end,
27 : xfs_daddr_t minlen,
28 : uint64_t *blocks_trimmed)
29 : {
30 19835 : struct xfs_mount *mp = pag->pag_mount;
31 19835 : struct block_device *bdev = mp->m_ddev_targp->bt_bdev;
32 19835 : struct xfs_btree_cur *cur;
33 19835 : struct xfs_buf *agbp;
34 19835 : struct xfs_agf *agf;
35 19835 : int error;
36 19835 : int i;
37 :
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 19835 : xfs_log_force(mp, XFS_LOG_SYNC);
44 :
45 19835 : error = xfs_alloc_read_agf(pag, NULL, 0, &agbp);
46 19835 : if (error)
47 : return error;
48 19835 : agf = agbp->b_addr;
49 :
50 19835 : cur = xfs_allocbt_init_cursor(mp, NULL, agbp, pag, XFS_BTNUM_CNT);
51 :
52 : /*
53 : * Look up the longest btree in the AGF and start with it.
54 : */
55 39670 : error = xfs_alloc_lookup_ge(cur, 0, be32_to_cpu(agf->agf_longest), &i);
56 19835 : if (error)
57 0 : goto out_del_cursor;
58 :
59 : /*
60 : * Loop until we are done with all extents that are large
61 : * enough to be worth discarding.
62 : */
63 385318 : while (i) {
64 384599 : xfs_agblock_t fbno;
65 384599 : xfs_extlen_t flen;
66 384599 : xfs_daddr_t dbno;
67 384599 : xfs_extlen_t dlen;
68 :
69 384599 : error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
70 384599 : if (error)
71 : break;
72 384599 : if (XFS_IS_CORRUPT(mp, i != 1)) {
73 : error = -EFSCORRUPTED;
74 : break;
75 : }
76 769198 : ASSERT(flen <= be32_to_cpu(agf->agf_longest));
77 :
78 : /*
79 : * use daddr format for all range/len calculations as that is
80 : * the format the range/len variables are supplied in by
81 : * userspace.
82 : */
83 384599 : dbno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, fbno);
84 384599 : dlen = XFS_FSB_TO_BB(mp, flen);
85 :
86 : /*
87 : * Too small? Give up.
88 : */
89 384599 : if (dlen < minlen) {
90 19114 : trace_xfs_discard_toosmall(mp, pag->pag_agno, fbno, flen);
91 19114 : break;
92 : }
93 :
94 : /*
95 : * If the extent is entirely outside of the range we are
96 : * supposed to discard skip it. Do not bother to trim
97 : * down partially overlapping ranges for now.
98 : */
99 365485 : if (dbno + dlen < start || dbno > end) {
100 6518 : trace_xfs_discard_exclude(mp, pag->pag_agno, fbno, flen);
101 6518 : goto next_extent;
102 : }
103 :
104 : /*
105 : * If any blocks in the range are still busy, skip the
106 : * discard and try again the next time.
107 : */
108 358967 : if (xfs_extent_busy_search(mp, pag, fbno, flen)) {
109 3620 : trace_xfs_discard_busy(mp, pag->pag_agno, fbno, flen);
110 3620 : goto next_extent;
111 : }
112 :
113 355347 : trace_xfs_discard_extent(mp, pag->pag_agno, fbno, flen);
114 355347 : error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS);
115 355347 : if (error)
116 : break;
117 355347 : *blocks_trimmed += flen;
118 :
119 365485 : next_extent:
120 365485 : error = xfs_btree_decrement(cur, 0, &i);
121 365485 : if (error)
122 : break;
123 :
124 365485 : if (fatal_signal_pending(current)) {
125 : error = -ERESTARTSYS;
126 : break;
127 : }
128 : }
129 :
130 719 : out_del_cursor:
131 19835 : xfs_btree_del_cursor(cur, error);
132 19835 : xfs_buf_relse(agbp);
133 19835 : return error;
134 : }
135 :
136 : /*
137 : * trim a range of the filesystem.
138 : *
139 : * Note: the parameters passed from userspace are byte ranges into the
140 : * filesystem which does not match to the format we use for filesystem block
141 : * addressing. FSB addressing is sparse (AGNO|AGBNO), while the incoming format
142 : * is a linear address range. Hence we need to use DADDR based conversions and
143 : * comparisons for determining the correct offset and regions to trim.
144 : */
145 : int
146 11521 : xfs_ioc_trim(
147 : struct xfs_mount *mp,
148 : struct fstrim_range __user *urange)
149 : {
150 11521 : struct xfs_perag *pag;
151 11521 : unsigned int granularity =
152 11521 : bdev_discard_granularity(mp->m_ddev_targp->bt_bdev);
153 11521 : struct fstrim_range range;
154 11521 : xfs_daddr_t start, end, minlen;
155 11521 : xfs_agnumber_t agno;
156 11521 : uint64_t blocks_trimmed = 0;
157 11521 : int error, last_error = 0;
158 :
159 11521 : if (!capable(CAP_SYS_ADMIN))
160 : return -EPERM;
161 11521 : if (!bdev_max_discard_sectors(mp->m_ddev_targp->bt_bdev))
162 : return -EOPNOTSUPP;
163 :
164 : /*
165 : * We haven't recovered the log, so we cannot use our bnobt-guided
166 : * storage zapping commands.
167 : */
168 11521 : if (xfs_has_norecovery(mp))
169 : return -EROFS;
170 :
171 11519 : if (copy_from_user(&range, urange, sizeof(range)))
172 : return -EFAULT;
173 :
174 11519 : range.minlen = max_t(u64, granularity, range.minlen);
175 11519 : minlen = BTOBB(range.minlen);
176 : /*
177 : * Truncating down the len isn't actually quite correct, but using
178 : * BBTOB would mean we trivially get overflows for values
179 : * of ULLONG_MAX or slightly lower. And ULLONG_MAX is the default
180 : * used by the fstrim application. In the end it really doesn't
181 : * matter as trimming blocks is an advisory interface.
182 : */
183 11519 : if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) ||
184 11509 : range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
185 11509 : range.len < mp->m_sb.sb_blocksize)
186 14 : return -EINVAL;
187 :
188 11505 : start = BTOBB(range.start);
189 11505 : end = start + BTOBBT(range.len) - 1;
190 :
191 11505 : if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
192 2689 : end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1;
193 :
194 11505 : agno = xfs_daddr_to_agno(mp, start);
195 31338 : for_each_perag_range(mp, agno, xfs_daddr_to_agno(mp, end), pag) {
196 19835 : error = xfs_trim_extents(pag, start, end, minlen,
197 : &blocks_trimmed);
198 19835 : if (error) {
199 2 : last_error = error;
200 2 : if (error == -ERESTARTSYS) {
201 2 : xfs_perag_rele(pag);
202 2 : break;
203 : }
204 : }
205 : }
206 :
207 11505 : if (last_error)
208 : return last_error;
209 :
210 11503 : range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
211 11503 : if (copy_to_user(urange, &range, sizeof(range)))
212 0 : return -EFAULT;
213 : return 0;
214 : }
|