1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 Fujitsu. All Rights Reserved.
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_alloc.h"
14 #include "xfs_btree.h"
15 #include "xfs_inode.h"
16 #include "xfs_icache.h"
18 #include "xfs_rmap_btree.h"
19 #include "xfs_rtalloc.h"
20 #include "xfs_trans.h"
24 #include <linux/dax.h>
27 xfs_agblock_t startblock;
28 xfs_extlen_t blockcount;
35 const struct xfs_rmap_irec *rec,
36 const struct failure_info *notify)
38 loff_t pos = XFS_FSB_TO_B(mp, rec->rm_offset);
40 if (notify->startblock > rec->rm_startblock)
41 pos += XFS_FSB_TO_B(mp,
42 notify->startblock - rec->rm_startblock);
43 return pos >> PAGE_SHIFT;
49 const struct xfs_rmap_irec *rec,
50 const struct failure_info *notify)
52 xfs_agblock_t end_rec;
53 xfs_agblock_t end_notify;
54 xfs_agblock_t start_cross;
55 xfs_agblock_t end_cross;
57 start_cross = max(rec->rm_startblock, notify->startblock);
59 end_rec = rec->rm_startblock + rec->rm_blockcount;
60 end_notify = notify->startblock + notify->blockcount;
61 end_cross = min(end_rec, end_notify);
63 return XFS_FSB_TO_B(mp, end_cross - start_cross) >> PAGE_SHIFT;
68 struct xfs_btree_cur *cur,
69 const struct xfs_rmap_irec *rec,
72 struct xfs_mount *mp = cur->bc_mp;
74 struct failure_info *notify = data;
77 if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) ||
78 (rec->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))) {
79 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_ONDISK);
83 /* Get files that incore, filter out others that are not in use. */
84 error = xfs_iget(mp, cur->bc_tp, rec->rm_owner, XFS_IGET_INCORE,
86 /* Continue the rmap query if the inode isn't incore */
87 if (error == -ENODATA)
92 error = mf_dax_kill_procs(VFS_I(ip)->i_mapping,
93 xfs_failure_pgoff(mp, rec, notify),
94 xfs_failure_pgcnt(mp, rec, notify),
101 xfs_dax_notify_ddev_failure(
102 struct xfs_mount *mp,
107 struct xfs_trans *tp = NULL;
108 struct xfs_btree_cur *cur = NULL;
109 struct xfs_buf *agf_bp = NULL;
111 xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, daddr);
112 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, fsbno);
113 xfs_fsblock_t end_fsbno = XFS_DADDR_TO_FSB(mp, daddr + bblen);
114 xfs_agnumber_t end_agno = XFS_FSB_TO_AGNO(mp, end_fsbno);
116 error = xfs_trans_alloc_empty(mp, &tp);
120 for (; agno <= end_agno; agno++) {
121 struct xfs_rmap_irec ri_low = { };
122 struct xfs_rmap_irec ri_high;
123 struct failure_info notify;
126 struct xfs_perag *pag;
128 pag = xfs_perag_get(mp, agno);
129 error = xfs_alloc_read_agf(pag, tp, 0, &agf_bp);
135 cur = xfs_rmapbt_init_cursor(mp, tp, agf_bp, pag);
138 * Set the rmap range from ri_low to ri_high, which represents
139 * a [start, end] where we looking for the files or metadata.
141 memset(&ri_high, 0xFF, sizeof(ri_high));
142 ri_low.rm_startblock = XFS_FSB_TO_AGBNO(mp, fsbno);
143 if (agno == end_agno)
144 ri_high.rm_startblock = XFS_FSB_TO_AGBNO(mp, end_fsbno);
146 agf = agf_bp->b_addr;
147 agend = min(be32_to_cpu(agf->agf_length),
148 ri_high.rm_startblock);
149 notify.startblock = ri_low.rm_startblock;
150 notify.blockcount = agend - ri_low.rm_startblock;
152 error = xfs_rmap_query_range(cur, &ri_low, &ri_high,
153 xfs_dax_failure_fn, ¬ify);
154 xfs_btree_del_cursor(cur, error);
155 xfs_trans_brelse(tp, agf_bp);
160 fsbno = XFS_AGB_TO_FSB(mp, agno + 1, 0);
163 xfs_trans_cancel(tp);
168 xfs_dax_notify_failure(
169 struct dax_device *dax_dev,
174 struct xfs_mount *mp = dax_holder(dax_dev);
178 if (!(mp->m_super->s_flags & SB_BORN)) {
179 xfs_warn(mp, "filesystem is not ready for notify_failure()!");
183 if (mp->m_rtdev_targp && mp->m_rtdev_targp->bt_daxdev == dax_dev) {
185 "notify_failure() not supported on realtime device!");
189 if (mp->m_logdev_targp && mp->m_logdev_targp->bt_daxdev == dax_dev &&
190 mp->m_logdev_targp != mp->m_ddev_targp) {
191 xfs_err(mp, "ondisk log corrupt, shutting down fs!");
192 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_ONDISK);
193 return -EFSCORRUPTED;
196 if (!xfs_has_rmapbt(mp)) {
197 xfs_debug(mp, "notify_failure() needs rmapbt enabled!");
201 ddev_start = mp->m_ddev_targp->bt_dax_part_off;
202 ddev_end = ddev_start + bdev_nr_bytes(mp->m_ddev_targp->bt_bdev) - 1;
204 /* Ignore the range out of filesystem area */
205 if (offset + len < ddev_start)
207 if (offset > ddev_end)
210 /* Calculate the real range when it touches the boundary */
211 if (offset > ddev_start)
212 offset -= ddev_start;
214 len -= ddev_start - offset;
217 if (offset + len > ddev_end)
218 len -= ddev_end - offset;
220 return xfs_dax_notify_ddev_failure(mp, BTOBB(offset), BTOBB(len),
224 const struct dax_holder_operations xfs_dax_holder_operations = {
225 .notify_failure = xfs_dax_notify_failure,