blob: 50966a9912cd847060c1fca1435f22446b525c56 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwiga46db602011-01-07 13:02:04 +00002/*
3 * Copyright (C) 2010 Red Hat, Inc.
4 * All Rights Reserved.
Christoph Hellwiga46db602011-01-07 13:02:04 +00005 */
6#include "xfs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07007#include "xfs_shared.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +10008#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100011#include "xfs_sb.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000012#include "xfs_mount.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110013#include "xfs_btree.h"
14#include "xfs_alloc_btree.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000015#include "xfs_alloc.h"
Darrick J. Wong5f213dd2019-11-06 17:19:33 -080016#include "xfs_discard.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000017#include "xfs_error.h"
Dave Chinnerefc27b52012-04-29 10:39:43 +000018#include "xfs_extent_busy.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000019#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_log.h"
Christoph Hellwiga46db602011-01-07 13:02:04 +000021
22STATIC int
23xfs_trim_extents(
24 struct xfs_mount *mp,
25 xfs_agnumber_t agno,
Dave Chinnera66d6362012-03-22 05:15:12 +000026 xfs_daddr_t start,
27 xfs_daddr_t end,
28 xfs_daddr_t minlen,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070029 uint64_t *blocks_trimmed)
Christoph Hellwiga46db602011-01-07 13:02:04 +000030{
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_perag *pag;
35 int error;
36 int i;
37
38 pag = xfs_perag_get(mp, agno);
39
Carlos Maiolino8c81dd42018-04-10 22:39:04 -070040 /*
41 * Force out the log. This means any transactions that might have freed
42 * space before we take the AGF buffer lock are now on disk, and the
43 * volatile disk cache is flushed.
44 */
45 xfs_log_force(mp, XFS_LOG_SYNC);
46
Christoph Hellwiga46db602011-01-07 13:02:04 +000047 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
48 if (error || !agbp)
49 goto out_put_perag;
50
51 cur = xfs_allocbt_init_cursor(mp, NULL, agbp, agno, XFS_BTNUM_CNT);
52
53 /*
Christoph Hellwiga46db602011-01-07 13:02:04 +000054 * Look up the longest btree in the AGF and start with it.
55 */
Dave Chinnera66d6362012-03-22 05:15:12 +000056 error = xfs_alloc_lookup_ge(cur, 0,
Dave Chinnerb1c770c2011-12-21 00:07:42 +000057 be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_longest), &i);
Christoph Hellwiga46db602011-01-07 13:02:04 +000058 if (error)
59 goto out_del_cursor;
60
61 /*
62 * Loop until we are done with all extents that are large
63 * enough to be worth discarding.
64 */
65 while (i) {
Dave Chinnera66d6362012-03-22 05:15:12 +000066 xfs_agblock_t fbno;
67 xfs_extlen_t flen;
68 xfs_daddr_t dbno;
69 xfs_extlen_t dlen;
Christoph Hellwiga46db602011-01-07 13:02:04 +000070
71 error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
72 if (error)
73 goto out_del_cursor;
Eric Sandeenc29aad42015-02-23 22:39:08 +110074 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_del_cursor);
Dave Chinnerb1c770c2011-12-21 00:07:42 +000075 ASSERT(flen <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_longest));
Christoph Hellwiga46db602011-01-07 13:02:04 +000076
77 /*
Dave Chinnera66d6362012-03-22 05:15:12 +000078 * use daddr format for all range/len calculations as that is
79 * the format the range/len variables are supplied in by
80 * userspace.
81 */
82 dbno = XFS_AGB_TO_DADDR(mp, agno, fbno);
83 dlen = XFS_FSB_TO_BB(mp, flen);
84
85 /*
Christoph Hellwiga46db602011-01-07 13:02:04 +000086 * Too small? Give up.
87 */
Dave Chinnera66d6362012-03-22 05:15:12 +000088 if (dlen < minlen) {
Christoph Hellwiga46db602011-01-07 13:02:04 +000089 trace_xfs_discard_toosmall(mp, agno, fbno, flen);
90 goto out_del_cursor;
91 }
92
93 /*
94 * If the extent is entirely outside of the range we are
95 * supposed to discard skip it. Do not bother to trim
96 * down partially overlapping ranges for now.
97 */
Dave Chinnera66d6362012-03-22 05:15:12 +000098 if (dbno + dlen < start || dbno > end) {
Christoph Hellwiga46db602011-01-07 13:02:04 +000099 trace_xfs_discard_exclude(mp, agno, fbno, flen);
100 goto next_extent;
101 }
102
103 /*
104 * If any blocks in the range are still busy, skip the
105 * discard and try again the next time.
106 */
Dave Chinner4ecbfe62012-04-29 10:41:10 +0000107 if (xfs_extent_busy_search(mp, agno, fbno, flen)) {
Christoph Hellwiga46db602011-01-07 13:02:04 +0000108 trace_xfs_discard_busy(mp, agno, fbno, flen);
109 goto next_extent;
110 }
111
112 trace_xfs_discard_extent(mp, agno, fbno, flen);
Dave Chinner24513372014-06-25 14:58:08 +1000113 error = blkdev_issue_discard(bdev, dbno, dlen, GFP_NOFS, 0);
Christoph Hellwiga46db602011-01-07 13:02:04 +0000114 if (error)
115 goto out_del_cursor;
116 *blocks_trimmed += flen;
117
118next_extent:
119 error = xfs_btree_decrement(cur, 0, &i);
120 if (error)
121 goto out_del_cursor;
Lukas Czerner3c378192017-04-27 08:59:36 -0700122
123 if (fatal_signal_pending(current)) {
124 error = -ERESTARTSYS;
125 goto out_del_cursor;
126 }
Christoph Hellwiga46db602011-01-07 13:02:04 +0000127 }
128
129out_del_cursor:
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700130 xfs_btree_del_cursor(cur, error);
Christoph Hellwiga46db602011-01-07 13:02:04 +0000131 xfs_buf_relse(agbp);
132out_put_perag:
133 xfs_perag_put(pag);
134 return error;
135}
136
Dave Chinnera66d6362012-03-22 05:15:12 +0000137/*
138 * trim a range of the filesystem.
139 *
140 * Note: the parameters passed from userspace are byte ranges into the
141 * filesystem which does not match to the format we use for filesystem block
142 * addressing. FSB addressing is sparse (AGNO|AGBNO), while the incoming format
143 * is a linear address range. Hence we need to use DADDR based conversions and
144 * comparisons for determining the correct offset and regions to trim.
145 */
Christoph Hellwiga46db602011-01-07 13:02:04 +0000146int
147xfs_ioc_trim(
148 struct xfs_mount *mp,
149 struct fstrim_range __user *urange)
150{
Jie Liu2f42d612013-11-20 16:08:53 +0800151 struct request_queue *q = bdev_get_queue(mp->m_ddev_targp->bt_bdev);
Christoph Hellwiga46db602011-01-07 13:02:04 +0000152 unsigned int granularity = q->limits.discard_granularity;
153 struct fstrim_range range;
Dave Chinnera66d6362012-03-22 05:15:12 +0000154 xfs_daddr_t start, end, minlen;
Christoph Hellwiga46db602011-01-07 13:02:04 +0000155 xfs_agnumber_t start_agno, end_agno, agno;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700156 uint64_t blocks_trimmed = 0;
Christoph Hellwiga46db602011-01-07 13:02:04 +0000157 int error, last_error = 0;
158
159 if (!capable(CAP_SYS_ADMIN))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000160 return -EPERM;
Lukas Czernerbe715142011-02-15 17:07:36 +0000161 if (!blk_queue_discard(q))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000162 return -EOPNOTSUPP;
Darrick J. Wonged79dac2019-03-22 18:10:22 -0700163
164 /*
165 * We haven't recovered the log, so we cannot use our bnobt-guided
166 * storage zapping commands.
167 */
168 if (mp->m_flags & XFS_MOUNT_NORECOVERY)
169 return -EROFS;
170
Christoph Hellwiga46db602011-01-07 13:02:04 +0000171 if (copy_from_user(&range, urange, sizeof(range)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000172 return -EFAULT;
Christoph Hellwiga46db602011-01-07 13:02:04 +0000173
Wang Shilong2bf9d262019-04-12 07:39:21 -0700174 range.minlen = max_t(u64, granularity, range.minlen);
175 minlen = BTOBB(range.minlen);
Christoph Hellwiga46db602011-01-07 13:02:04 +0000176 /*
177 * Truncating down the len isn't actually quite correct, but using
Dave Chinnera66d6362012-03-22 05:15:12 +0000178 * BBTOB would mean we trivially get overflows for values
Christoph Hellwiga46db602011-01-07 13:02:04 +0000179 * 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 */
Tomas Raceka672e1b2012-08-14 10:35:04 +0200183 if (range.start >= XFS_FSB_TO_B(mp, mp->m_sb.sb_dblocks) ||
Darrick J. Wong52548852016-08-03 11:38:24 +1000184 range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
Jie Liu2f42d612013-11-20 16:08:53 +0800185 range.len < mp->m_sb.sb_blocksize)
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000186 return -EINVAL;
Tomas Raceka672e1b2012-08-14 10:35:04 +0200187
Dave Chinnera66d6362012-03-22 05:15:12 +0000188 start = BTOBB(range.start);
189 end = start + BTOBBT(range.len) - 1;
Christoph Hellwiga46db602011-01-07 13:02:04 +0000190
Dave Chinnera66d6362012-03-22 05:15:12 +0000191 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;
Christoph Hellwiga46db602011-01-07 13:02:04 +0000193
Dave Chinnera66d6362012-03-22 05:15:12 +0000194 start_agno = xfs_daddr_to_agno(mp, start);
195 end_agno = xfs_daddr_to_agno(mp, end);
Christoph Hellwiga46db602011-01-07 13:02:04 +0000196
197 for (agno = start_agno; agno <= end_agno; agno++) {
Dave Chinner24513372014-06-25 14:58:08 +1000198 error = xfs_trim_extents(mp, agno, start, end, minlen,
Christoph Hellwiga46db602011-01-07 13:02:04 +0000199 &blocks_trimmed);
Lukas Czerner3c378192017-04-27 08:59:36 -0700200 if (error) {
Christoph Hellwiga46db602011-01-07 13:02:04 +0000201 last_error = error;
Lukas Czerner3c378192017-04-27 08:59:36 -0700202 if (error == -ERESTARTSYS)
203 break;
204 }
Christoph Hellwiga46db602011-01-07 13:02:04 +0000205 }
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)))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000212 return -EFAULT;
Christoph Hellwiga46db602011-01-07 13:02:04 +0000213 return 0;
214}