xref: /openbmc/linux/fs/xfs/xfs_discard.c (revision e52a6321)
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_sb.h"
12 #include "xfs_mount.h"
13 #include "xfs_btree.h"
14 #include "xfs_alloc_btree.h"
15 #include "xfs_alloc.h"
16 #include "xfs_error.h"
17 #include "xfs_extent_busy.h"
18 #include "xfs_trace.h"
19 #include "xfs_log.h"
20 
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_perag	*pag;
34 	int			error;
35 	int			i;
36 
37 	pag = xfs_perag_get(mp, agno);
38 
39 	/*
40 	 * Force out the log.  This means any transactions that might have freed
41 	 * space before we take the AGF buffer lock are now on disk, and the
42 	 * volatile disk cache is flushed.
43 	 */
44 	xfs_log_force(mp, XFS_LOG_SYNC);
45 
46 	error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
47 	if (error || !agbp)
48 		goto out_put_perag;
49 
50 	cur = xfs_allocbt_init_cursor(mp, NULL, agbp, agno, 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,
56 			    be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_longest), &i);
57 	if (error)
58 		goto out_del_cursor;
59 
60 	/*
61 	 * Loop until we are done with all extents that are large
62 	 * enough to be worth discarding.
63 	 */
64 	while (i) {
65 		xfs_agblock_t	fbno;
66 		xfs_extlen_t	flen;
67 		xfs_daddr_t	dbno;
68 		xfs_extlen_t	dlen;
69 
70 		error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
71 		if (error)
72 			goto out_del_cursor;
73 		XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_del_cursor);
74 		ASSERT(flen <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_longest));
75 
76 		/*
77 		 * use daddr format for all range/len calculations as that is
78 		 * the format the range/len variables are supplied in by
79 		 * userspace.
80 		 */
81 		dbno = XFS_AGB_TO_DADDR(mp, agno, fbno);
82 		dlen = XFS_FSB_TO_BB(mp, flen);
83 
84 		/*
85 		 * Too small?  Give up.
86 		 */
87 		if (dlen < minlen) {
88 			trace_xfs_discard_toosmall(mp, agno, fbno, flen);
89 			goto out_del_cursor;
90 		}
91 
92 		/*
93 		 * If the extent is entirely outside of the range we are
94 		 * supposed to discard skip it.  Do not bother to trim
95 		 * down partially overlapping ranges for now.
96 		 */
97 		if (dbno + dlen < start || dbno > end) {
98 			trace_xfs_discard_exclude(mp, agno, fbno, flen);
99 			goto next_extent;
100 		}
101 
102 		/*
103 		 * If any blocks in the range are still busy, skip the
104 		 * discard and try again the next time.
105 		 */
106 		if (xfs_extent_busy_search(mp, agno, fbno, flen)) {
107 			trace_xfs_discard_busy(mp, agno, fbno, flen);
108 			goto next_extent;
109 		}
110 
111 		trace_xfs_discard_extent(mp, agno, fbno, flen);
112 		error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS, 0);
113 		if (error)
114 			goto out_del_cursor;
115 		*blocks_trimmed += flen;
116 
117 next_extent:
118 		error = xfs_btree_decrement(cur, 0, &i);
119 		if (error)
120 			goto out_del_cursor;
121 
122 		if (fatal_signal_pending(current)) {
123 			error = -ERESTARTSYS;
124 			goto out_del_cursor;
125 		}
126 	}
127 
128 out_del_cursor:
129 	xfs_btree_del_cursor(cur, error);
130 	xfs_buf_relse(agbp);
131 out_put_perag:
132 	xfs_perag_put(pag);
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 request_queue	*q = bdev_get_queue(mp->m_ddev_targp->bt_bdev);
151 	unsigned int		granularity = q->limits.discard_granularity;
152 	struct fstrim_range	range;
153 	xfs_daddr_t		start, end, minlen;
154 	xfs_agnumber_t		start_agno, end_agno, agno;
155 	uint64_t		blocks_trimmed = 0;
156 	int			error, last_error = 0;
157 
158 	if (!capable(CAP_SYS_ADMIN))
159 		return -EPERM;
160 	if (!blk_queue_discard(q))
161 		return -EOPNOTSUPP;
162 
163 	/*
164 	 * We haven't recovered the log, so we cannot use our bnobt-guided
165 	 * storage zapping commands.
166 	 */
167 	if (mp->m_flags & XFS_MOUNT_NORECOVERY)
168 		return -EROFS;
169 
170 	if (copy_from_user(&range, urange, sizeof(range)))
171 		return -EFAULT;
172 
173 	range.minlen = max_t(u64, granularity, range.minlen);
174 	minlen = BTOBB(range.minlen);
175 	/*
176 	 * Truncating down the len isn't actually quite correct, but using
177 	 * BBTOB would mean we trivially get overflows for values
178 	 * of ULLONG_MAX or slightly lower.  And ULLONG_MAX is the default
179 	 * used by the fstrim application.  In the end it really doesn't
180 	 * matter as trimming blocks is an advisory interface.
181 	 */
182 	if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) ||
183 	    range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
184 	    range.len < mp->m_sb.sb_blocksize)
185 		return -EINVAL;
186 
187 	start = BTOBB(range.start);
188 	end = start + BTOBBT(range.len) - 1;
189 
190 	if (end > XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1)
191 		end = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)- 1;
192 
193 	start_agno = xfs_daddr_to_agno(mp, start);
194 	end_agno = xfs_daddr_to_agno(mp, end);
195 
196 	for (agno = start_agno; agno <= end_agno; agno++) {
197 		error = xfs_trim_extents(mp, agno, start, end, minlen,
198 					  &blocks_trimmed);
199 		if (error) {
200 			last_error = error;
201 			if (error == -ERESTARTSYS)
202 				break;
203 		}
204 	}
205 
206 	if (last_error)
207 		return last_error;
208 
209 	range.len = XFS_FSB_TO_B(mp, blocks_trimmed);
210 	if (copy_to_user(urange, &range, sizeof(range)))
211 		return -EFAULT;
212 	return 0;
213 }
214