Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
| 3 | * Copyright (c) 2013 Red Hat, Inc. |
| 4 | * All Rights Reserved. |
| 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 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it would be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #include "xfs.h" |
| 20 | #include "xfs_fs.h" |
Dave Chinner | 632b89e | 2013-10-29 22:11:58 +1100 | [diff] [blame] | 21 | #include "xfs_shared.h" |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 22 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 23 | #include "xfs_log_format.h" |
| 24 | #include "xfs_trans_resv.h" |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 25 | #include "xfs_mount.h" |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 26 | #include "xfs_inode.h" |
| 27 | #include "xfs_quota.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 28 | #include "xfs_trans.h" |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 29 | #include "xfs_qm.h" |
| 30 | #include "xfs_error.h" |
| 31 | #include "xfs_cksum.h" |
| 32 | #include "xfs_trace.h" |
| 33 | |
| 34 | int |
| 35 | xfs_calc_dquots_per_chunk( |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 36 | unsigned int nbblks) /* basic block units */ |
| 37 | { |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 38 | ASSERT(nbblks > 0); |
Eric Sandeen | d956f81 | 2017-04-06 16:01:47 -0700 | [diff] [blame] | 39 | return BBTOB(nbblks) / sizeof(xfs_dqblk_t); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Do some primitive error checking on ondisk dquot data structures. |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 44 | * |
| 45 | * The xfs_dqblk structure /contains/ the xfs_disk_dquot structure; |
| 46 | * we verify them separately because at some points we have only the |
| 47 | * smaller xfs_disk_dquot structure available. |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 48 | */ |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 49 | |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 50 | xfs_failaddr_t |
| 51 | xfs_dquot_verify( |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 52 | struct xfs_mount *mp, |
| 53 | xfs_disk_dquot_t *ddq, |
| 54 | xfs_dqid_t id, |
Eric Sandeen | 57ab324 | 2018-05-07 09:20:17 -0700 | [diff] [blame] | 55 | uint type) /* used only during quotacheck */ |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 56 | { |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 57 | /* |
| 58 | * We can encounter an uninitialized dquot buffer for 2 reasons: |
| 59 | * 1. If we crash while deleting the quotainode(s), and those blks got |
| 60 | * used for user data. This is because we take the path of regular |
| 61 | * file deletion; however, the size field of quotainodes is never |
| 62 | * updated, so all the tricks that we play in itruncate_finish |
| 63 | * don't quite matter. |
| 64 | * |
| 65 | * 2. We don't play the quota buffers when there's a quotaoff logitem. |
| 66 | * But the allocation will be replayed so we'll end up with an |
| 67 | * uninitialized quota block. |
| 68 | * |
| 69 | * This is all fine; things are still consistent, and we haven't lost |
| 70 | * any quota information. Just don't complain about bad dquot blks. |
| 71 | */ |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 72 | if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC)) |
| 73 | return __this_address; |
| 74 | if (ddq->d_version != XFS_DQUOT_VERSION) |
| 75 | return __this_address; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 76 | |
Eric Sandeen | 57ab324 | 2018-05-07 09:20:17 -0700 | [diff] [blame] | 77 | if (type && ddq->d_flags != type) |
| 78 | return __this_address; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 79 | if (ddq->d_flags != XFS_DQ_USER && |
| 80 | ddq->d_flags != XFS_DQ_PROJ && |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 81 | ddq->d_flags != XFS_DQ_GROUP) |
| 82 | return __this_address; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 83 | |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 84 | if (id != -1 && id != be32_to_cpu(ddq->d_id)) |
| 85 | return __this_address; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 86 | |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 87 | if (!ddq->d_id) |
| 88 | return NULL; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 89 | |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 90 | if (ddq->d_blk_softlimit && |
| 91 | be64_to_cpu(ddq->d_bcount) > be64_to_cpu(ddq->d_blk_softlimit) && |
| 92 | !ddq->d_btimer) |
| 93 | return __this_address; |
| 94 | |
| 95 | if (ddq->d_ino_softlimit && |
| 96 | be64_to_cpu(ddq->d_icount) > be64_to_cpu(ddq->d_ino_softlimit) && |
| 97 | !ddq->d_itimer) |
| 98 | return __this_address; |
| 99 | |
| 100 | if (ddq->d_rtb_softlimit && |
| 101 | be64_to_cpu(ddq->d_rtbcount) > be64_to_cpu(ddq->d_rtb_softlimit) && |
| 102 | !ddq->d_rtbtimer) |
| 103 | return __this_address; |
| 104 | |
| 105 | return NULL; |
Darrick J. Wong | eeea798 | 2018-01-08 10:51:24 -0800 | [diff] [blame] | 106 | } |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 107 | |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 108 | xfs_failaddr_t |
| 109 | xfs_dqblk_verify( |
| 110 | struct xfs_mount *mp, |
| 111 | struct xfs_dqblk *dqb, |
| 112 | xfs_dqid_t id, |
| 113 | uint type) /* used only during quotacheck */ |
| 114 | { |
| 115 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 116 | !uuid_equal(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid)) |
| 117 | return __this_address; |
| 118 | |
| 119 | return xfs_dquot_verify(mp, &dqb->dd_diskdq, id, type); |
| 120 | } |
| 121 | |
Darrick J. Wong | eeea798 | 2018-01-08 10:51:24 -0800 | [diff] [blame] | 122 | /* |
| 123 | * Do some primitive error checking on ondisk dquot data structures. |
| 124 | */ |
| 125 | int |
Eric Sandeen | 48fa1db | 2018-05-07 09:20:17 -0700 | [diff] [blame] | 126 | xfs_dqblk_repair( |
Darrick J. Wong | eeea798 | 2018-01-08 10:51:24 -0800 | [diff] [blame] | 127 | struct xfs_mount *mp, |
Eric Sandeen | 48fa1db | 2018-05-07 09:20:17 -0700 | [diff] [blame] | 128 | struct xfs_dqblk *dqb, |
Darrick J. Wong | eeea798 | 2018-01-08 10:51:24 -0800 | [diff] [blame] | 129 | xfs_dqid_t id, |
| 130 | uint type) |
| 131 | { |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 132 | /* |
| 133 | * Typically, a repair is only requested by quotacheck. |
| 134 | */ |
| 135 | ASSERT(id != -1); |
Eric Sandeen | 48fa1db | 2018-05-07 09:20:17 -0700 | [diff] [blame] | 136 | memset(dqb, 0, sizeof(xfs_dqblk_t)); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 137 | |
Eric Sandeen | 48fa1db | 2018-05-07 09:20:17 -0700 | [diff] [blame] | 138 | dqb->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC); |
| 139 | dqb->dd_diskdq.d_version = XFS_DQUOT_VERSION; |
| 140 | dqb->dd_diskdq.d_flags = type; |
| 141 | dqb->dd_diskdq.d_id = cpu_to_be32(id); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 142 | |
| 143 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
Eric Sandeen | 48fa1db | 2018-05-07 09:20:17 -0700 | [diff] [blame] | 144 | uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid); |
| 145 | xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk), |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 146 | XFS_DQUOT_CRC_OFF); |
| 147 | } |
| 148 | |
Darrick J. Wong | eeea798 | 2018-01-08 10:51:24 -0800 | [diff] [blame] | 149 | return 0; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | STATIC bool |
| 153 | xfs_dquot_buf_verify_crc( |
| 154 | struct xfs_mount *mp, |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 155 | struct xfs_buf *bp, |
| 156 | bool readahead) |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 157 | { |
| 158 | struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr; |
| 159 | int ndquots; |
| 160 | int i; |
| 161 | |
| 162 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 163 | return true; |
| 164 | |
| 165 | /* |
| 166 | * if we are in log recovery, the quota subsystem has not been |
| 167 | * initialised so we have no quotainfo structure. In that case, we need |
| 168 | * to manually calculate the number of dquots in the buffer. |
| 169 | */ |
| 170 | if (mp->m_quotainfo) |
| 171 | ndquots = mp->m_quotainfo->qi_dqperchunk; |
| 172 | else |
Darrick J. Wong | 58d7896 | 2016-10-20 15:46:18 +1100 | [diff] [blame] | 173 | ndquots = xfs_calc_dquots_per_chunk(bp->b_length); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 174 | |
| 175 | for (i = 0; i < ndquots; i++, d++) { |
| 176 | if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk), |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 177 | XFS_DQUOT_CRC_OFF)) { |
| 178 | if (!readahead) |
| 179 | xfs_buf_verifier_error(bp, -EFSBADCRC, __func__, |
| 180 | d, sizeof(*d), __this_address); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 181 | return false; |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 182 | } |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 187 | STATIC xfs_failaddr_t |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 188 | xfs_dquot_buf_verify( |
| 189 | struct xfs_mount *mp, |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 190 | struct xfs_buf *bp, |
| 191 | bool readahead) |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 192 | { |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 193 | struct xfs_dqblk *dqb = bp->b_addr; |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 194 | xfs_failaddr_t fa; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 195 | xfs_dqid_t id = 0; |
| 196 | int ndquots; |
| 197 | int i; |
| 198 | |
| 199 | /* |
| 200 | * if we are in log recovery, the quota subsystem has not been |
| 201 | * initialised so we have no quotainfo structure. In that case, we need |
| 202 | * to manually calculate the number of dquots in the buffer. |
| 203 | */ |
| 204 | if (mp->m_quotainfo) |
| 205 | ndquots = mp->m_quotainfo->qi_dqperchunk; |
| 206 | else |
Eric Sandeen | 6ea94bb | 2014-04-14 19:03:34 +1000 | [diff] [blame] | 207 | ndquots = xfs_calc_dquots_per_chunk(bp->b_length); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * On the first read of the buffer, verify that each dquot is valid. |
| 211 | * We don't know what the id of the dquot is supposed to be, just that |
| 212 | * they should be increasing monotonically within the buffer. If the |
| 213 | * first id is corrupt, then it will fail on the second dquot in the |
| 214 | * buffer so corruptions could point to the wrong dquot in this case. |
| 215 | */ |
| 216 | for (i = 0; i < ndquots; i++) { |
| 217 | struct xfs_disk_dquot *ddq; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 218 | |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 219 | ddq = &dqb[i].dd_diskdq; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 220 | |
| 221 | if (i == 0) |
| 222 | id = be32_to_cpu(ddq->d_id); |
| 223 | |
Eric Sandeen | 7224fa4 | 2018-05-07 09:20:18 -0700 | [diff] [blame] | 224 | fa = xfs_dqblk_verify(mp, &dqb[i], id + i, 0); |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 225 | if (fa) { |
| 226 | if (!readahead) |
| 227 | xfs_buf_verifier_error(bp, -EFSCORRUPTED, |
| 228 | __func__, &dqb[i], |
| 229 | sizeof(struct xfs_dqblk), fa); |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 230 | return fa; |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 231 | } |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 232 | } |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 233 | |
| 234 | return NULL; |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 235 | } |
| 236 | |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 237 | static xfs_failaddr_t |
| 238 | xfs_dquot_buf_verify_struct( |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 239 | struct xfs_buf *bp) |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 240 | { |
| 241 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 242 | |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 243 | return xfs_dquot_buf_verify(mp, bp, false); |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 246 | static void |
| 247 | xfs_dquot_buf_read_verify( |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 248 | struct xfs_buf *bp) |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 249 | { |
| 250 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 251 | |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 252 | if (!xfs_dquot_buf_verify_crc(mp, bp, false)) |
| 253 | return; |
| 254 | xfs_dquot_buf_verify(mp, bp, false); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
Dave Chinner | 7d6a13f | 2016-01-12 07:04:01 +1100 | [diff] [blame] | 258 | * readahead errors are silent and simply leave the buffer as !done so a real |
| 259 | * read will then be run with the xfs_dquot_buf_ops verifier. See |
| 260 | * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than |
| 261 | * reporting the failure. |
| 262 | */ |
| 263 | static void |
| 264 | xfs_dquot_buf_readahead_verify( |
| 265 | struct xfs_buf *bp) |
| 266 | { |
| 267 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 268 | |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 269 | if (!xfs_dquot_buf_verify_crc(mp, bp, true) || |
| 270 | xfs_dquot_buf_verify(mp, bp, true) != NULL) { |
Dave Chinner | 7d6a13f | 2016-01-12 07:04:01 +1100 | [diff] [blame] | 271 | xfs_buf_ioerror(bp, -EIO); |
| 272 | bp->b_flags &= ~XBF_DONE; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 277 | * we don't calculate the CRC here as that is done when the dquot is flushed to |
| 278 | * the buffer after the update is done. This ensures that the dquot in the |
| 279 | * buffer always has an up-to-date CRC value. |
| 280 | */ |
Dave Chinner | 632b89e | 2013-10-29 22:11:58 +1100 | [diff] [blame] | 281 | static void |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 282 | xfs_dquot_buf_write_verify( |
Darrick J. Wong | eebf3ca | 2018-01-08 10:51:25 -0800 | [diff] [blame] | 283 | struct xfs_buf *bp) |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 284 | { |
| 285 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 286 | |
Eric Sandeen | 72c5c5f | 2018-05-07 09:20:47 -0700 | [diff] [blame] | 287 | xfs_dquot_buf_verify(mp, bp, false); |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | const struct xfs_buf_ops xfs_dquot_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 291 | .name = "xfs_dquot", |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 292 | .verify_read = xfs_dquot_buf_read_verify, |
| 293 | .verify_write = xfs_dquot_buf_write_verify, |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 294 | .verify_struct = xfs_dquot_buf_verify_struct, |
Dave Chinner | 9aede1d | 2013-10-15 09:17:52 +1100 | [diff] [blame] | 295 | }; |
| 296 | |
Dave Chinner | 7d6a13f | 2016-01-12 07:04:01 +1100 | [diff] [blame] | 297 | const struct xfs_buf_ops xfs_dquot_buf_ra_ops = { |
| 298 | .name = "xfs_dquot_ra", |
| 299 | .verify_read = xfs_dquot_buf_readahead_verify, |
| 300 | .verify_write = xfs_dquot_buf_write_verify, |
| 301 | }; |