xref: /openbmc/linux/fs/xfs/scrub/rtbitmap.c (revision fb8d6c8d)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans.h"
14 #include "xfs_rtalloc.h"
15 #include "xfs_inode.h"
16 #include "scrub/scrub.h"
17 #include "scrub/common.h"
18 
19 /* Set us up with the realtime metadata locked. */
20 int
21 xchk_setup_rt(
22 	struct xfs_scrub	*sc,
23 	struct xfs_inode	*ip)
24 {
25 	int			error;
26 
27 	error = xchk_setup_fs(sc, ip);
28 	if (error)
29 		return error;
30 
31 	sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
32 	sc->ip = sc->mp->m_rbmip;
33 	xfs_ilock(sc->ip, sc->ilock_flags);
34 
35 	return 0;
36 }
37 
38 /* Realtime bitmap. */
39 
40 /* Scrub a free extent record from the realtime bitmap. */
41 STATIC int
42 xchk_rtbitmap_rec(
43 	struct xfs_trans	*tp,
44 	struct xfs_rtalloc_rec	*rec,
45 	void			*priv)
46 {
47 	struct xfs_scrub	*sc = priv;
48 	xfs_rtblock_t		startblock;
49 	xfs_rtblock_t		blockcount;
50 
51 	startblock = rec->ar_startext * tp->t_mountp->m_sb.sb_rextsize;
52 	blockcount = rec->ar_extcount * tp->t_mountp->m_sb.sb_rextsize;
53 
54 	if (startblock + blockcount <= startblock ||
55 	    !xfs_verify_rtbno(sc->mp, startblock) ||
56 	    !xfs_verify_rtbno(sc->mp, startblock + blockcount - 1))
57 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
58 	return 0;
59 }
60 
61 /* Scrub the realtime bitmap. */
62 int
63 xchk_rtbitmap(
64 	struct xfs_scrub	*sc)
65 {
66 	int			error;
67 
68 	/* Invoke the fork scrubber. */
69 	error = xchk_metadata_inode_forks(sc);
70 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
71 		return error;
72 
73 	error = xfs_rtalloc_query_all(sc->tp, xchk_rtbitmap_rec, sc);
74 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
75 		goto out;
76 
77 out:
78 	return error;
79 }
80 
81 /* Scrub the realtime summary. */
82 int
83 xchk_rtsummary(
84 	struct xfs_scrub	*sc)
85 {
86 	struct xfs_inode	*rsumip = sc->mp->m_rsumip;
87 	struct xfs_inode	*old_ip = sc->ip;
88 	uint			old_ilock_flags = sc->ilock_flags;
89 	int			error = 0;
90 
91 	/*
92 	 * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
93 	 * rt summary ip in compliance with the rt inode locking rules.
94 	 *
95 	 * Since we switch sc->ip to rsumip we have to save the old ilock
96 	 * flags so that we don't mix up the inode state that @sc tracks.
97 	 */
98 	sc->ip = rsumip;
99 	sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
100 	xfs_ilock(sc->ip, sc->ilock_flags);
101 
102 	/* Invoke the fork scrubber. */
103 	error = xchk_metadata_inode_forks(sc);
104 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
105 		goto out;
106 
107 	/* XXX: implement this some day */
108 	xchk_set_incomplete(sc);
109 out:
110 	/* Switch back to the rtbitmap inode and lock flags. */
111 	xfs_iunlock(sc->ip, sc->ilock_flags);
112 	sc->ilock_flags = old_ilock_flags;
113 	sc->ip = old_ip;
114 	return error;
115 }
116 
117 
118 /* xref check that the extent is not free in the rtbitmap */
119 void
120 xchk_xref_is_used_rt_space(
121 	struct xfs_scrub	*sc,
122 	xfs_rtblock_t		fsbno,
123 	xfs_extlen_t		len)
124 {
125 	xfs_rtblock_t		startext;
126 	xfs_rtblock_t		endext;
127 	xfs_rtblock_t		extcount;
128 	bool			is_free;
129 	int			error;
130 
131 	if (xchk_skip_xref(sc->sm))
132 		return;
133 
134 	startext = fsbno;
135 	endext = fsbno + len - 1;
136 	do_div(startext, sc->mp->m_sb.sb_rextsize);
137 	do_div(endext, sc->mp->m_sb.sb_rextsize);
138 	extcount = endext - startext + 1;
139 	xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
140 	error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
141 			&is_free);
142 	if (!xchk_should_check_xref(sc, &error, NULL))
143 		goto out_unlock;
144 	if (is_free)
145 		xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
146 out_unlock:
147 	xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
148 }
149