1 /* 2 * Copyright (C) 2017 Oracle. All Rights Reserved. 3 * 4 * Author: Darrick J. Wong <darrick.wong@oracle.com> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it would be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 #include "xfs.h" 21 #include "xfs_fs.h" 22 #include "xfs_shared.h" 23 #include "xfs_format.h" 24 #include "xfs_trans_resv.h" 25 #include "xfs_mount.h" 26 #include "xfs_defer.h" 27 #include "xfs_btree.h" 28 #include "xfs_bit.h" 29 #include "xfs_log_format.h" 30 #include "xfs_trans.h" 31 #include "xfs_sb.h" 32 #include "xfs_alloc.h" 33 #include "xfs_rtalloc.h" 34 #include "xfs_inode.h" 35 #include "scrub/xfs_scrub.h" 36 #include "scrub/scrub.h" 37 #include "scrub/common.h" 38 #include "scrub/trace.h" 39 40 /* Set us up with the realtime metadata locked. */ 41 int 42 xfs_scrub_setup_rt( 43 struct xfs_scrub_context *sc, 44 struct xfs_inode *ip) 45 { 46 int error; 47 48 error = xfs_scrub_setup_fs(sc, ip); 49 if (error) 50 return error; 51 52 sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP; 53 sc->ip = sc->mp->m_rbmip; 54 xfs_ilock(sc->ip, sc->ilock_flags); 55 56 return 0; 57 } 58 59 /* Realtime bitmap. */ 60 61 /* Scrub a free extent record from the realtime bitmap. */ 62 STATIC int 63 xfs_scrub_rtbitmap_rec( 64 struct xfs_trans *tp, 65 struct xfs_rtalloc_rec *rec, 66 void *priv) 67 { 68 struct xfs_scrub_context *sc = priv; 69 xfs_rtblock_t startblock; 70 xfs_rtblock_t blockcount; 71 72 startblock = rec->ar_startext * tp->t_mountp->m_sb.sb_rextsize; 73 blockcount = rec->ar_extcount * tp->t_mountp->m_sb.sb_rextsize; 74 75 if (startblock + blockcount <= startblock || 76 !xfs_verify_rtbno(sc->mp, startblock) || 77 !xfs_verify_rtbno(sc->mp, startblock + blockcount - 1)) 78 xfs_scrub_fblock_set_corrupt(sc, XFS_DATA_FORK, 0); 79 return 0; 80 } 81 82 /* Scrub the realtime bitmap. */ 83 int 84 xfs_scrub_rtbitmap( 85 struct xfs_scrub_context *sc) 86 { 87 int error; 88 89 /* Invoke the fork scrubber. */ 90 error = xfs_scrub_metadata_inode_forks(sc); 91 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) 92 return error; 93 94 error = xfs_rtalloc_query_all(sc->tp, xfs_scrub_rtbitmap_rec, sc); 95 if (!xfs_scrub_fblock_process_error(sc, XFS_DATA_FORK, 0, &error)) 96 goto out; 97 98 out: 99 return error; 100 } 101 102 /* Scrub the realtime summary. */ 103 int 104 xfs_scrub_rtsummary( 105 struct xfs_scrub_context *sc) 106 { 107 struct xfs_inode *rsumip = sc->mp->m_rsumip; 108 struct xfs_inode *old_ip = sc->ip; 109 uint old_ilock_flags = sc->ilock_flags; 110 int error = 0; 111 112 /* 113 * We ILOCK'd the rt bitmap ip in the setup routine, now lock the 114 * rt summary ip in compliance with the rt inode locking rules. 115 * 116 * Since we switch sc->ip to rsumip we have to save the old ilock 117 * flags so that we don't mix up the inode state that @sc tracks. 118 */ 119 sc->ip = rsumip; 120 sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM; 121 xfs_ilock(sc->ip, sc->ilock_flags); 122 123 /* Invoke the fork scrubber. */ 124 error = xfs_scrub_metadata_inode_forks(sc); 125 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) 126 goto out; 127 128 /* XXX: implement this some day */ 129 xfs_scrub_set_incomplete(sc); 130 out: 131 /* Switch back to the rtbitmap inode and lock flags. */ 132 xfs_iunlock(sc->ip, sc->ilock_flags); 133 sc->ilock_flags = old_ilock_flags; 134 sc->ip = old_ip; 135 return error; 136 } 137 138 139 /* xref check that the extent is not free in the rtbitmap */ 140 void 141 xfs_scrub_xref_is_used_rt_space( 142 struct xfs_scrub_context *sc, 143 xfs_rtblock_t fsbno, 144 xfs_extlen_t len) 145 { 146 xfs_rtblock_t startext; 147 xfs_rtblock_t endext; 148 xfs_rtblock_t extcount; 149 bool is_free; 150 int error; 151 152 if (xfs_scrub_skip_xref(sc->sm)) 153 return; 154 155 startext = fsbno; 156 endext = fsbno + len - 1; 157 do_div(startext, sc->mp->m_sb.sb_rextsize); 158 if (do_div(endext, sc->mp->m_sb.sb_rextsize)) 159 endext++; 160 extcount = endext - startext; 161 xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP); 162 error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount, 163 &is_free); 164 if (!xfs_scrub_should_check_xref(sc, &error, NULL)) 165 goto out_unlock; 166 if (is_free) 167 xfs_scrub_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino); 168 out_unlock: 169 xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP); 170 } 171