123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
#include "xfs_platform.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
#include "xfs_trans.h"
#include "xfs_mount.h"
#include "xfs_btree.h"
#include "xfs_alloc_btree.h"
#include "xfs_alloc.h"
#include "xfs_discard.h"
#include "xfs_error.h"
#include "xfs_extent_busy.h"
#include "xfs_trace.h"
#include "xfs_log.h"
#include "xfs_ag.h"
#include "xfs_health.h"
#include "xfs_rtbitmap.h"
#include "xfs_rtgroup.h"
#define XFS_DISCARD_MAX_EXAMINE (100)
struct workqueue_struct *xfs_discard_wq;
static void
xfs_discard_endio_work(
struct work_struct *work)
{
struct xfs_busy_extents *extents =
container_of(work, struct xfs_busy_extents, endio_work);
xfs_extent_busy_clear(&extents->extent_list, false);
kfree(extents->owner);
}
static void
xfs_discard_endio(
struct bio *bio)
{
struct xfs_busy_extents *extents = bio->bi_private;
INIT_WORK(&extents->endio_work, xfs_discard_endio_work);
queue_work(xfs_discard_wq, &extents->endio_work);
bio_put(bio);
}
void
xfs_discard_extents(
struct xfs_mount *mp,
struct xfs_busy_extents *extents)
{
struct xfs_extent_busy *busyp;
struct bio *bio = NULL;
struct blk_plug plug;
blk_start_plug(&plug);
list_for_each_entry(busyp, &extents->extent_list, list) {
struct xfs_group *xg = busyp->group;
struct xfs_buftarg *btp =
xfs_group_type_buftarg(xg->xg_mount, xg->xg_type);
trace_xfs_discard_extent(xg, busyp->bno, busyp->length);
__blkdev_issue_discard(btp->bt_bdev,
xfs_gbno_to_daddr(xg, busyp->bno),
XFS_FSB_TO_BB(mp, busyp->length),
GFP_KERNEL, &bio);
}
if (bio) {
bio->bi_private = extents;
bio->bi_end_io = xfs_discard_endio;
submit_bio(bio);
} else {
xfs_discard_endio_work(&extents->endio_work);
}
blk_finish_plug(&plug);
}
struct xfs_trim_cur {
xfs_agblock_t start;
xfs_extlen_t count;
xfs_agblock_t end;
xfs_extlen_t minlen;
bool by_bno;
xfs_agblock_t restart_bno;
};
static int
xfs_trim_gather_extents(
struct xfs_perag *pag,
struct xfs_trim_cur *tcur,
struct xfs_busy_extents *extents)
{
struct xfs_mount *mp = pag_mount(pag);
struct xfs_trans *tp;
struct xfs_btree_cur *cur;
struct xfs_buf *agbp;
int error;
int i;
int batch = XFS_DISCARD_MAX_EXAMINE;
xfs_log_force(mp, XFS_LOG_SYNC);
tp = xfs_trans_alloc_empty(mp);
error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
if (error)
goto out_trans_cancel;
if (!tcur->count)
tcur->count = pag->pagf_longest;
if (tcur->by_bno) {
cur = xfs_bnobt_init_cursor(mp, tp, agbp, pag);
error = xfs_alloc_lookup_le(cur, tcur->start, 0, &i);
if (!error && !i)
error = xfs_alloc_lookup_ge(cur, tcur->start, 0, &i);
} else {
cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag);
error = xfs_alloc_lookup_le(cur, tcur->restart_bno,
tcur->count, &i);
}
if (error)
goto out_del_cursor;
if (i == 0) {
tcur->count = 0;
goto out_del_cursor;
}
while (i) {
xfs_agblock_t fbno;
xfs_extlen_t flen;
error = xfs_alloc_get_rec(cur, &fbno, &flen, &i);
if (error)
break;
if (XFS_IS_CORRUPT(mp, i != 1)) {
xfs_btree_mark_sick(cur);
error = -EFSCORRUPTED;
break;
}
if (--batch <= 0) {
if (tcur->by_bno)
tcur->start = fbno;
else
tcur->restart_bno = fbno;
tcur->count = flen;
break;
}
if (fbno + flen < tcur->start) {
trace_xfs_discard_exclude(pag_group(pag), fbno, flen);
goto next_extent;
}
if (fbno > tcur->end) {
trace_xfs_discard_exclude(pag_group(pag), fbno, flen);
if (tcur->by_bno) {
tcur->count = 0;
break;
}
goto next_extent;
}
if (fbno < tcur->start) {
flen -= tcur->start - fbno;
fbno = tcur->start;
}
if (fbno + flen > tcur->end + 1)
flen = tcur->end - fbno + 1;
if (flen < tcur->minlen) {
trace_xfs_discard_toosmall(pag_group(pag), fbno, flen);
if (tcur->by_bno)
goto next_extent;
tcur->count = 0;
break;
}
if (xfs_extent_busy_search(pag_group(pag), fbno, flen)) {
trace_xfs_discard_busy(pag_group(pag), fbno, flen);
goto next_extent;
}
xfs_extent_busy_insert_discard(pag_group(pag), fbno, flen,
&extents->extent_list);
next_extent:
if (tcur->by_bno)
error = xfs_btree_increment(cur, 0, &i);
else
error = xfs_btree_decrement(cur, 0, &i);
if (error)
break;
if (i == 0)
tcur->count = 0;
}
if (error)
xfs_extent_busy_clear(&extents->extent_list, false);
out_del_cursor:
xfs_btree_del_cursor(cur, error);
out_trans_cancel:
xfs_trans_cancel(tp);
return error;
}
static bool
xfs_trim_should_stop(void)
{
return fatal_signal_pending(current) || freezing(current);
}
static int
xfs_trim_perag_extents(
struct xfs_perag *pag,
xfs_agblock_t start,
xfs_agblock_t end,
xfs_extlen_t minlen)
{
struct xfs_trim_cur tcur = {
.start = start,
.end = end,
.minlen = minlen,
.restart_bno = NULLAGBLOCK,
};
int error = 0;
if (start != 0 || end != pag_group(pag)->xg_block_count)
tcur.by_bno = true;
do {
struct xfs_busy_extents *extents;
extents = kzalloc_obj(*extents);
if (!extents) {
error = -ENOMEM;
break;
}
extents->owner = extents;
INIT_LIST_HEAD(&extents->extent_list);
error = xfs_trim_gather_extents(pag, &tcur, extents);
if (error) {
kfree(extents);
break;
}
xfs_discard_extents(pag_mount(pag), extents);
if (xfs_trim_should_stop())
break;
} while (tcur.count != 0);
return error;
}
static int
xfs_trim_datadev_extents(
struct xfs_mount *mp,
xfs_daddr_t start,
xfs_daddr_t end,
xfs_extlen_t minlen)
{
xfs_agnumber_t start_agno, end_agno;
xfs_agblock_t start_agbno, end_agbno;
struct xfs_perag *pag = NULL;
xfs_daddr_t ddev_end;
int last_error = 0, error;
ddev_end = min_t(xfs_daddr_t, end,
XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) - 1);
start_agno = xfs_daddr_to_agno(mp, start);
start_agbno = xfs_daddr_to_agbno(mp, start);
end_agno = xfs_daddr_to_agno(mp, ddev_end);
end_agbno = xfs_daddr_to_agbno(mp, ddev_end);
while ((pag = xfs_perag_next_range(mp, pag, start_agno, end_agno))) {
xfs_agblock_t agend = pag_group(pag)->xg_block_count;
if (pag_agno(pag) == end_agno)
agend = end_agbno;
error = xfs_trim_perag_extents(pag, start_agbno, agend, minlen);
if (error)
last_error = error;
if (xfs_trim_should_stop()) {
xfs_perag_rele(pag);
break;
}
start_agbno = 0;
}
return last_error;
}
#ifdef CONFIG_XFS_RT
struct xfs_trim_rtdev {
struct list_head extent_list;
xfs_rtblock_t minlen_fsb;
xfs_rtxnum_t restart_rtx;
xfs_rtxnum_t stop_rtx;
};
struct xfs_rtx_busy {
struct list_head list;
xfs_rtblock_t bno;
xfs_rtblock_t length;
};
static void
xfs_discard_free_rtdev_extents(
struct xfs_trim_rtdev *tr)
{
struct xfs_rtx_busy *busyp, *n;
list_for_each_entry_safe(busyp, n, &tr->extent_list, list) {
list_del_init(&busyp->list);
kfree(busyp);
}
}
static int
xfs_discard_rtdev_extents(
struct xfs_mount *mp,
struct xfs_trim_rtdev *tr)
{
struct block_device *bdev = mp->m_rtdev_targp->bt_bdev;
struct xfs_rtx_busy *busyp;
struct bio *bio = NULL;
struct blk_plug plug;
xfs_rtblock_t start = NULLRTBLOCK, length = 0;
int error = 0;
blk_start_plug(&plug);
list_for_each_entry(busyp, &tr->extent_list, list) {
if (start == NULLRTBLOCK)
start = busyp->bno;
length += busyp->length;
trace_xfs_discard_rtextent(mp, busyp->bno, busyp->length);
__blkdev_issue_discard(bdev,
xfs_rtb_to_daddr(mp, busyp->bno),
XFS_FSB_TO_BB(mp, busyp->length),
GFP_NOFS, &bio);
}
xfs_discard_free_rtdev_extents(tr);
if (bio) {
error = submit_bio_wait(bio);
if (error == -EOPNOTSUPP)
error = 0;
if (error)
xfs_info(mp,
"discard failed for rtextent [0x%llx,%llu], error %d",
(unsigned long long)start,
(unsigned long long)length,
error);
bio_put(bio);
}
blk_finish_plug(&plug);
return error;
}
static int
xfs_trim_gather_rtextent(
struct xfs_rtgroup *rtg,
struct xfs_trans *tp,
const struct xfs_rtalloc_rec *rec,
void *priv)
{
struct xfs_trim_rtdev *tr = priv;
struct xfs_rtx_busy *busyp;
xfs_rtblock_t rbno, rlen;
if (rec->ar_startext > tr->stop_rtx) {
tr->restart_rtx = rec->ar_startext;
return -ECANCELED;
}
rbno = xfs_rtx_to_rtb(rtg, rec->ar_startext);
rlen = xfs_rtbxlen_to_blen(rtg_mount(rtg), rec->ar_extcount);
if (rlen < tr->minlen_fsb) {
trace_xfs_discard_rttoosmall(rtg_mount(rtg), rbno, rlen);
return 0;
}
busyp = kzalloc_obj(struct xfs_rtx_busy);
if (!busyp)
return -ENOMEM;
busyp->bno = rbno;
busyp->length = rlen;
INIT_LIST_HEAD(&busyp->list);
list_add_tail(&busyp->list, &tr->extent_list);
tr->restart_rtx = rec->ar_startext + rec->ar_extcount;
return 0;
}
static int
xfs_trim_rtextents(
struct xfs_rtgroup *rtg,
xfs_rtxnum_t low,
xfs_rtxnum_t high,
xfs_daddr_t minlen)
{
struct xfs_mount *mp = rtg_mount(rtg);
struct xfs_trim_rtdev tr = {
.minlen_fsb = XFS_BB_TO_FSB(mp, minlen),
.extent_list = LIST_HEAD_INIT(tr.extent_list),
};
struct xfs_trans *tp;
int error;
tp = xfs_trans_alloc_empty(mp);
do {
tr.stop_rtx = low + xfs_rtbitmap_rtx_per_rbmblock(mp);
xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
error = xfs_rtalloc_query_range(rtg, tp, low, high,
xfs_trim_gather_rtextent, &tr);
if (error == -ECANCELED)
error = 0;
if (error) {
xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
xfs_discard_free_rtdev_extents(&tr);
break;
}
if (list_empty(&tr.extent_list)) {
xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
break;
}
error = xfs_discard_rtdev_extents(mp, &tr);
xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
if (error)
break;
low = tr.restart_rtx;
} while (!xfs_trim_should_stop() && low <= high);
xfs_trans_cancel(tp);
return error;
}
struct xfs_trim_rtgroup {
struct xfs_busy_extents *extents;
xfs_rtblock_t minlen_fsb;
xfs_rtxnum_t restart_rtx;
int batch;
int queued;
};
static int
xfs_trim_gather_rtgroup_extent(
struct xfs_rtgroup *rtg,
struct xfs_trans *tp,
const struct xfs_rtalloc_rec *rec,
void *priv)
{
struct xfs_trim_rtgroup *tr = priv;
xfs_rgblock_t rgbno;
xfs_extlen_t len;
if (--tr->batch <= 0) {
tr->restart_rtx = rec->ar_startext;
return -ECANCELED;
}
rgbno = xfs_rtx_to_rgbno(rtg, rec->ar_startext);
len = xfs_rtxlen_to_extlen(rtg_mount(rtg), rec->ar_extcount);
if (len < tr->minlen_fsb) {
trace_xfs_discard_toosmall(rtg_group(rtg), rgbno, len);
return 0;
}
if (xfs_extent_busy_search(rtg_group(rtg), rgbno, len)) {
trace_xfs_discard_busy(rtg_group(rtg), rgbno, len);
return 0;
}
xfs_extent_busy_insert_discard(rtg_group(rtg), rgbno, len,
&tr->extents->extent_list);
tr->queued++;
tr->restart_rtx = rec->ar_startext + rec->ar_extcount;
return 0;
}
static int
xfs_trim_rtgroup_extents(
struct xfs_rtgroup *rtg,
xfs_rtxnum_t low,
xfs_rtxnum_t high,
xfs_daddr_t minlen)
{
struct xfs_mount *mp = rtg_mount(rtg);
struct xfs_trim_rtgroup tr = {
.minlen_fsb = XFS_BB_TO_FSB(mp, minlen),
};
struct xfs_trans *tp;
int error;
tp = xfs_trans_alloc_empty(mp);
do {
tr.extents = kzalloc_obj(*tr.extents);
if (!tr.extents) {
error = -ENOMEM;
break;
}
tr.queued = 0;
tr.batch = XFS_DISCARD_MAX_EXAMINE;
tr.extents->owner = tr.extents;
INIT_LIST_HEAD(&tr.extents->extent_list);
xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
error = xfs_rtalloc_query_range(rtg, tp, low, high,
xfs_trim_gather_rtgroup_extent, &tr);
xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
if (error == -ECANCELED)
error = 0;
if (error) {
kfree(tr.extents);
break;
}
if (!tr.queued) {
kfree(tr.extents);
break;
}
xfs_discard_extents(rtg_mount(rtg), tr.extents);
low = tr.restart_rtx;
} while (!xfs_trim_should_stop() && low <= high);
xfs_trans_cancel(tp);
return error;
}
static int
xfs_trim_rtdev_extents(
struct xfs_mount *mp,
xfs_daddr_t start,
xfs_daddr_t end,
xfs_daddr_t minlen)
{
xfs_rtblock_t start_rtbno, end_rtbno;
xfs_rtxnum_t start_rtx, end_rtx;
xfs_rgnumber_t start_rgno, end_rgno;
xfs_daddr_t daddr_offset;
int last_error = 0, error;
struct xfs_rtgroup *rtg = NULL;
daddr_offset = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
if (start > daddr_offset)
start -= daddr_offset;
else
start = 0;
start_rtbno = xfs_daddr_to_rtb(mp, start);
start_rtx = xfs_rtb_to_rtx(mp, start_rtbno);
start_rgno = xfs_rtb_to_rgno(mp, start_rtbno);
if (end <= daddr_offset)
return 0;
else
end -= daddr_offset;
end_rtbno = xfs_daddr_to_rtb(mp, end);
end_rtx = xfs_rtb_to_rtx(mp, end_rtbno + mp->m_sb.sb_rextsize - 1);
end_rgno = xfs_rtb_to_rgno(mp, end_rtbno);
while ((rtg = xfs_rtgroup_next_range(mp, rtg, start_rgno, end_rgno))) {
xfs_rtxnum_t rtg_end = rtg->rtg_extents;
if (rtg_rgno(rtg) == end_rgno)
rtg_end = min(rtg_end, end_rtx);
if (xfs_has_rtgroups(mp))
error = xfs_trim_rtgroup_extents(rtg, start_rtx,
rtg_end, minlen);
else
error = xfs_trim_rtextents(rtg, start_rtx, rtg_end,
minlen);
if (error)
last_error = error;
if (xfs_trim_should_stop()) {
xfs_rtgroup_rele(rtg);
break;
}
start_rtx = 0;
}
return last_error;
}
#else
# define xfs_trim_rtdev_extents(...) (-EOPNOTSUPP)
#endif /* CONFIG_XFS_RT */
int
xfs_ioc_trim(
struct xfs_mount *mp,
struct fstrim_range __user *urange)
{
unsigned int granularity =
bdev_discard_granularity(mp->m_ddev_targp->bt_bdev);
struct block_device *rt_bdev = NULL;
struct fstrim_range range;
xfs_daddr_t start, end;
xfs_extlen_t minlen;
xfs_rfsblock_t max_blocks;
int error, last_error = 0;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (mp->m_rtdev_targp && !xfs_has_zoned(mp) &&
bdev_max_discard_sectors(mp->m_rtdev_targp->bt_bdev))
rt_bdev = mp->m_rtdev_targp->bt_bdev;
if (!bdev_max_discard_sectors(mp->m_ddev_targp->bt_bdev) && !rt_bdev)
return -EOPNOTSUPP;
if (rt_bdev)
granularity = max(granularity,
bdev_discard_granularity(rt_bdev));
if (xfs_has_norecovery(mp))
return -EROFS;
if (copy_from_user(&range, urange, sizeof(range)))
return -EFAULT;
range.minlen = max_t(u64, granularity, range.minlen);
minlen = XFS_B_TO_FSB(mp, range.minlen);
max_blocks = mp->m_sb.sb_dblocks + mp->m_sb.sb_rblocks;
if (range.start >= XFS_FSB_TO_B(mp, max_blocks) ||
range.minlen > XFS_FSB_TO_B(mp, mp->m_ag_max_usable) ||
range.len < mp->m_sb.sb_blocksize)
return -EINVAL;
start = BTOBB(range.start);
end = start + BTOBBT(range.len) - 1;
if (bdev_max_discard_sectors(mp->m_ddev_targp->bt_bdev)) {
error = xfs_trim_datadev_extents(mp, start, end, minlen);
if (error)
last_error = error;
}
if (rt_bdev && !xfs_trim_should_stop()) {
error = xfs_trim_rtdev_extents(mp, start, end, minlen);
if (error)
last_error = error;
}
if (last_error)
return last_error;
range.len = min_t(unsigned long long, range.len,
XFS_FSB_TO_B(mp, max_blocks) - range.start);
if (copy_to_user(urange, &range, sizeof(range)))
return -EFAULT;
return 0;
}