xref: /openbmc/linux/fs/xfs/xfs_discard.c (revision 47ebd031)
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 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 	struct xfs_mount	*mp = pag->pag_mount;
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_agf		*agf;
35 	int			error;
36 	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 	xfs_log_force(mp, XFS_LOG_SYNC);
44 
45 	error = xfs_alloc_read_agf(pag, NULL, 0, &agbp);
46 	if (error)
47 		return error;
48 	agf = agbp->b_addr;
49 
50 	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 	error = xfs_alloc_lookup_ge(cur, 0, be32_to_cpu(agf->agf_longest), &i);
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) {
64 		xfs_agblock_t	fbno;
65 		xfs_extlen_t	flen;
66 		xfs_daddr_t	dbno;
67 		xfs_extlen_t	dlen;
68 
69 		error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
70 		if (error)
71 			break;
72 		if (XFS_IS_CORRUPT(mp, i != 1)) {
73 			error = -EFSCORRUPTED;
74 			break;
75 		}
76 		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 		dbno = XFS_AGB_TO_DADDR(mp, pag->pag_agno, fbno);
84 		dlen = XFS_FSB_TO_BB(mp, flen);
85 
86 		/*
87 		 * Too small?  Give up.
88 		 */
89 		if (dlen < minlen) {
90 			trace_xfs_discard_toosmall(mp, pag->pag_agno, fbno, flen);
91 			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 		if (dbno + dlen < start || dbno > end) {
100 			trace_xfs_discard_exclude(mp, pag->pag_agno, fbno, flen);
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 		 */
108 		if (xfs_extent_busy_search(mp, pag, fbno, flen)) {
109 			trace_xfs_discard_busy(mp, pag->pag_agno, fbno, flen);
110 			goto next_extent;
111 		}
112 
113 		trace_xfs_discard_extent(mp, pag->pag_agno, fbno, flen);
114 		error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS);
115 		if (error)
116 			break;
117 		*blocks_trimmed += flen;
118 
119 next_extent:
120 		error = xfs_btree_decrement(cur, 0, &i);
121 		if (error)
122 			break;
123 
124 		if (fatal_signal_pending(current)) {
125 			error = -ERESTARTSYS;
126 			break;
127 		}
128 	}
129 
130 out_del_cursor:
131 	xfs_btree_del_cursor(cur, error);
132 	xfs_buf_relse(agbp);
133 	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 xfs_ioc_trim(
147 	struct xfs_mount		*mp,
148 	struct fstrim_range __user	*urange)
149 {
150 	struct xfs_perag	*pag;
151 	unsigned int		granularity =
152 		bdev_discard_granularity(mp->m_ddev_targp->bt_bdev);
153 	struct fstrim_range	range;
154 	xfs_daddr_t		start, end, minlen;
155 	xfs_agnumber_t		agno;
156 	uint64_t		blocks_trimmed = 0;
157 	int			error, last_error = 0;
158 
159 	if (!capable(CAP_SYS_ADMIN))
160 		return -EPERM;
161 	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 	if (xfs_has_norecovery(mp))
169 		return -EROFS;
170 
171 	if (copy_from_user(&range, urange, sizeof(range)))
172 		return -EFAULT;
173 
174 	range.minlen = max_t(u64, granularity, range.minlen);
175 	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 	if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) ||
184 	    range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
185 	    range.len < mp->m_sb.sb_blocksize)
186 		return -EINVAL;
187 
188 	start = BTOBB(range.start);
189 	end = start + BTOBBT(range.len) - 1;
190 
191 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
192 		end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1;
193 
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,
197 					  &blocks_trimmed);
198 		if (error) {
199 			last_error = error;
200 			if (error == -ERESTARTSYS) {
201 				xfs_perag_rele(pag);
202 				break;
203 			}
204 		}
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)))
212 		return -EFAULT;
213 	return 0;
214 }
215