Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a46db608 CH |
2 | /* |
3 | * Copyright (C) 2010 Red Hat, Inc. | |
4 | * All Rights Reserved. | |
a46db608 CH |
5 | */ |
6 | #include "xfs.h" | |
5467b34b | 7 | #include "xfs_shared.h" |
6ca1c906 | 8 | #include "xfs_format.h" |
239880ef DC |
9 | #include "xfs_log_format.h" |
10 | #include "xfs_trans_resv.h" | |
a46db608 | 11 | #include "xfs_mount.h" |
a4fbe6ab DC |
12 | #include "xfs_btree.h" |
13 | #include "xfs_alloc_btree.h" | |
a46db608 | 14 | #include "xfs_alloc.h" |
5f213ddb | 15 | #include "xfs_discard.h" |
a46db608 | 16 | #include "xfs_error.h" |
efc27b52 | 17 | #include "xfs_extent_busy.h" |
a46db608 | 18 | #include "xfs_trace.h" |
239880ef | 19 | #include "xfs_log.h" |
9bbafc71 | 20 | #include "xfs_ag.h" |
a46db608 CH |
21 | |
22 | STATIC int | |
23 | xfs_trim_extents( | |
35bf2b1a | 24 | struct xfs_perag *pag, |
a66d6363 DC |
25 | xfs_daddr_t start, |
26 | xfs_daddr_t end, | |
27 | xfs_daddr_t minlen, | |
c8ce540d | 28 | uint64_t *blocks_trimmed) |
a46db608 | 29 | { |
35bf2b1a | 30 | struct xfs_mount *mp = pag->pag_mount; |
a46db608 CH |
31 | struct block_device *bdev = mp->m_ddev_targp->bt_bdev; |
32 | struct xfs_btree_cur *cur; | |
33 | struct xfs_buf *agbp; | |
9798f615 | 34 | struct xfs_agf *agf; |
a46db608 CH |
35 | int error; |
36 | int i; | |
37 | ||
a46db608 CH |
38 | /* |
39 | * Force out the log. This means any transactions that might have freed | |
8c81dd46 | 40 | * space before we take the AGF buffer lock are now on disk, and the |
a46db608 CH |
41 | * volatile disk cache is flushed. |
42 | */ | |
43 | xfs_log_force(mp, XFS_LOG_SYNC); | |
44 | ||
08d3e84f | 45 | error = xfs_alloc_read_agf(pag, NULL, 0, &agbp); |
706b8c5b | 46 | if (error) |
35bf2b1a | 47 | return error; |
9798f615 | 48 | agf = agbp->b_addr; |
8c81dd46 | 49 | |
289d38d2 | 50 | cur = xfs_allocbt_init_cursor(mp, NULL, agbp, pag, XFS_BTNUM_CNT); |
8c81dd46 | 51 | |
a46db608 CH |
52 | /* |
53 | * Look up the longest btree in the AGF and start with it. | |
54 | */ | |
9798f615 | 55 | error = xfs_alloc_lookup_ge(cur, 0, be32_to_cpu(agf->agf_longest), &i); |
a46db608 CH |
56 | if (error) |
57 | 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 | while (i) { | |
a66d6363 DC |
64 | xfs_agblock_t fbno; |
65 | xfs_extlen_t flen; | |
66 | xfs_daddr_t dbno; | |
67 | xfs_extlen_t dlen; | |
a46db608 CH |
68 | |
69 | error = xfs_alloc_get_rec(cur, &fbno, &flen, &i); | |
70 | if (error) | |
35bf2b1a | 71 | break; |
f9e03706 DW |
72 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
73 | error = -EFSCORRUPTED; | |
35bf2b1a | 74 | break; |
f9e03706 | 75 | } |
9798f615 | 76 | ASSERT(flen <= be32_to_cpu(agf->agf_longest)); |
a46db608 | 77 | |
a66d6363 DC |
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 | */ | |
35bf2b1a | 83 | dbno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, fbno); |
a66d6363 DC |
84 | dlen = XFS_FSB_TO_BB(mp, flen); |
85 | ||
a46db608 CH |
86 | /* |
87 | * Too small? Give up. | |
88 | */ | |
a66d6363 | 89 | if (dlen < minlen) { |
35bf2b1a DC |
90 | trace_xfs_discard_toosmall(mp, pag->pag_agno, fbno, flen); |
91 | break; | |
a46db608 CH |
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 | */ | |
a66d6363 | 99 | if (dbno + dlen < start || dbno > end) { |
35bf2b1a | 100 | trace_xfs_discard_exclude(mp, pag->pag_agno, fbno, flen); |
a46db608 CH |
101 | 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 | */ | |
45d06621 | 108 | if (xfs_extent_busy_search(mp, pag, fbno, flen)) { |
35bf2b1a | 109 | trace_xfs_discard_busy(mp, pag->pag_agno, fbno, flen); |
a46db608 CH |
110 | goto next_extent; |
111 | } | |
112 | ||
35bf2b1a | 113 | trace_xfs_discard_extent(mp, pag->pag_agno, fbno, flen); |
44abff2c | 114 | error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS); |
a46db608 | 115 | if (error) |
35bf2b1a | 116 | break; |
a46db608 CH |
117 | *blocks_trimmed += flen; |
118 | ||
119 | next_extent: | |
120 | error = xfs_btree_decrement(cur, 0, &i); | |
121 | if (error) | |
35bf2b1a | 122 | break; |
3c378195 LC |
123 | |
124 | if (fatal_signal_pending(current)) { | |
125 | error = -ERESTARTSYS; | |
35bf2b1a | 126 | break; |
3c378195 | 127 | } |
a46db608 CH |
128 | } |
129 | ||
130 | out_del_cursor: | |
0b04b6b8 | 131 | xfs_btree_del_cursor(cur, error); |
a46db608 | 132 | xfs_buf_relse(agbp); |
a46db608 CH |
133 | return error; |
134 | } | |
135 | ||
a66d6363 DC |
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 | */ | |
a46db608 CH |
145 | int |
146 | xfs_ioc_trim( | |
147 | struct xfs_mount *mp, | |
148 | struct fstrim_range __user *urange) | |
149 | { | |
35bf2b1a | 150 | struct xfs_perag *pag; |
7b47ef52 CH |
151 | unsigned int granularity = |
152 | bdev_discard_granularity(mp->m_ddev_targp->bt_bdev); | |
a46db608 | 153 | struct fstrim_range range; |
a66d6363 | 154 | xfs_daddr_t start, end, minlen; |
35bf2b1a | 155 | xfs_agnumber_t agno; |
c8ce540d | 156 | uint64_t blocks_trimmed = 0; |
a46db608 CH |
157 | int error, last_error = 0; |
158 | ||
159 | if (!capable(CAP_SYS_ADMIN)) | |
b474c7ae | 160 | return -EPERM; |
70200574 | 161 | if (!bdev_max_discard_sectors(mp->m_ddev_targp->bt_bdev)) |
b474c7ae | 162 | return -EOPNOTSUPP; |
ed79dac9 DW |
163 | |
164 | /* | |
165 | * We haven't recovered the log, so we cannot use our bnobt-guided | |
166 | * storage zapping commands. | |
167 | */ | |
0560f31a | 168 | if (xfs_has_norecovery(mp)) |
ed79dac9 DW |
169 | return -EROFS; |
170 | ||
a46db608 | 171 | if (copy_from_user(&range, urange, sizeof(range))) |
b474c7ae | 172 | return -EFAULT; |
a46db608 | 173 | |
2bf9d264 WS |
174 | range.minlen = max_t(u64, granularity, range.minlen); |
175 | minlen = BTOBB(range.minlen); | |
a46db608 CH |
176 | /* |
177 | * Truncating down the len isn't actually quite correct, but using | |
a66d6363 | 178 | * BBTOB would mean we trivially get overflows for values |
a46db608 CH |
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 | */ | |
a672e1be | 183 | if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) || |
52548852 | 184 | range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) || |
2f42d612 | 185 | range.len < mp->m_sb.sb_blocksize) |
b474c7ae | 186 | return -EINVAL; |
a672e1be | 187 | |
a66d6363 DC |
188 | start = BTOBB(range.start); |
189 | end = start + BTOBBT(range.len) - 1; | |
a46db608 | 190 | |
a66d6363 | 191 | if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1) |
35bf2b1a | 192 | end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1; |
a46db608 | 193 | |
35bf2b1a DC |
194 | agno = xfs_daddr_to_agno(mp, start); |
195 | for_each_perag_range(mp, agno, xfs_daddr_to_agno(mp, end), pag) { | |
196 | error = xfs_trim_extents(pag, start, end, minlen, | |
a46db608 | 197 | &blocks_trimmed); |
3c378195 | 198 | if (error) { |
a46db608 | 199 | last_error = error; |
35bf2b1a DC |
200 | if (error == -ERESTARTSYS) { |
201 | xfs_perag_rele(pag); | |
3c378195 | 202 | break; |
35bf2b1a | 203 | } |
3c378195 | 204 | } |
a46db608 CH |
205 | } |
206 | ||
207 | if (last_error) | |
208 | return last_error; | |
209 | ||
210 | range.len = XFS_FSB_TO_B(mp, blocks_trimmed); | |
211 | if (copy_to_user(urange, &range, sizeof(range))) | |
b474c7ae | 212 | return -EFAULT; |
a46db608 CH |
213 | return 0; |
214 | } |