Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
| 5 | */ |
| 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
| 8 | #include "xfs_shared.h" |
| 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
| 12 | #include "xfs_bit.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 13 | #include "xfs_mount.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 14 | #include "xfs_ialloc.h" |
| 15 | #include "xfs_alloc.h" |
| 16 | #include "xfs_error.h" |
| 17 | #include "xfs_trace.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 18 | #include "xfs_trans.h" |
| 19 | #include "xfs_buf_item.h" |
| 20 | #include "xfs_bmap_btree.h" |
| 21 | #include "xfs_alloc_btree.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | #include "xfs_log.h" |
| 23 | #include "xfs_rmap_btree.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 24 | #include "xfs_refcount_btree.h" |
| 25 | #include "xfs_da_format.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 26 | #include "xfs_health.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Physical superblock buffer manipulations. Shared with libxfs in userspace. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Reference counting access wrappers to the perag structures. |
| 34 | * Because we never free per-ag structures, the only thing we |
| 35 | * have to protect against changes is the tree structure itself. |
| 36 | */ |
| 37 | struct xfs_perag * |
| 38 | xfs_perag_get( |
| 39 | struct xfs_mount *mp, |
| 40 | xfs_agnumber_t agno) |
| 41 | { |
| 42 | struct xfs_perag *pag; |
| 43 | int ref = 0; |
| 44 | |
| 45 | rcu_read_lock(); |
| 46 | pag = radix_tree_lookup(&mp->m_perag_tree, agno); |
| 47 | if (pag) { |
| 48 | ASSERT(atomic_read(&pag->pag_ref) >= 0); |
| 49 | ref = atomic_inc_return(&pag->pag_ref); |
| 50 | } |
| 51 | rcu_read_unlock(); |
| 52 | trace_xfs_perag_get(mp, agno, ref, _RET_IP_); |
| 53 | return pag; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * search from @first to find the next perag with the given tag set. |
| 58 | */ |
| 59 | struct xfs_perag * |
| 60 | xfs_perag_get_tag( |
| 61 | struct xfs_mount *mp, |
| 62 | xfs_agnumber_t first, |
| 63 | int tag) |
| 64 | { |
| 65 | struct xfs_perag *pag; |
| 66 | int found; |
| 67 | int ref; |
| 68 | |
| 69 | rcu_read_lock(); |
| 70 | found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, |
| 71 | (void **)&pag, first, 1, tag); |
| 72 | if (found <= 0) { |
| 73 | rcu_read_unlock(); |
| 74 | return NULL; |
| 75 | } |
| 76 | ref = atomic_inc_return(&pag->pag_ref); |
| 77 | rcu_read_unlock(); |
| 78 | trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_); |
| 79 | return pag; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | xfs_perag_put( |
| 84 | struct xfs_perag *pag) |
| 85 | { |
| 86 | int ref; |
| 87 | |
| 88 | ASSERT(atomic_read(&pag->pag_ref) > 0); |
| 89 | ref = atomic_dec_return(&pag->pag_ref); |
| 90 | trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_); |
| 91 | } |
| 92 | |
| 93 | /* Check all the superblock fields we care about when reading one in. */ |
| 94 | STATIC int |
| 95 | xfs_validate_sb_read( |
| 96 | struct xfs_mount *mp, |
| 97 | struct xfs_sb *sbp) |
| 98 | { |
| 99 | if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5) |
| 100 | return 0; |
| 101 | |
| 102 | /* |
| 103 | * Version 5 superblock feature mask validation. Reject combinations |
| 104 | * the kernel cannot support up front before checking anything else. |
| 105 | */ |
| 106 | if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) { |
| 107 | xfs_warn(mp, |
| 108 | "Superblock has unknown compatible features (0x%x) enabled.", |
| 109 | (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN)); |
| 110 | xfs_warn(mp, |
| 111 | "Using a more recent kernel is recommended."); |
| 112 | } |
| 113 | |
| 114 | if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) { |
| 115 | xfs_alert(mp, |
| 116 | "Superblock has unknown read-only compatible features (0x%x) enabled.", |
| 117 | (sbp->sb_features_ro_compat & |
| 118 | XFS_SB_FEAT_RO_COMPAT_UNKNOWN)); |
| 119 | if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { |
| 120 | xfs_warn(mp, |
| 121 | "Attempted to mount read-only compatible filesystem read-write."); |
| 122 | xfs_warn(mp, |
| 123 | "Filesystem can only be safely mounted read only."); |
| 124 | |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | } |
| 128 | if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) { |
| 129 | xfs_warn(mp, |
| 130 | "Superblock has unknown incompatible features (0x%x) enabled.", |
| 131 | (sbp->sb_features_incompat & |
| 132 | XFS_SB_FEAT_INCOMPAT_UNKNOWN)); |
| 133 | xfs_warn(mp, |
| 134 | "Filesystem cannot be safely mounted by this kernel."); |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* Check all the superblock fields we care about when writing one out. */ |
| 142 | STATIC int |
| 143 | xfs_validate_sb_write( |
| 144 | struct xfs_mount *mp, |
| 145 | struct xfs_buf *bp, |
| 146 | struct xfs_sb *sbp) |
| 147 | { |
| 148 | /* |
| 149 | * Carry out additional sb summary counter sanity checks when we write |
| 150 | * the superblock. We skip this in the read validator because there |
| 151 | * could be newer superblocks in the log and if the values are garbage |
| 152 | * even after replay we'll recalculate them at the end of log mount. |
| 153 | * |
| 154 | * mkfs has traditionally written zeroed counters to inprogress and |
| 155 | * secondary superblocks, so allow this usage to continue because |
| 156 | * we never read counters from such superblocks. |
| 157 | */ |
| 158 | if (XFS_BUF_ADDR(bp) == XFS_SB_DADDR && !sbp->sb_inprogress && |
| 159 | (sbp->sb_fdblocks > sbp->sb_dblocks || |
| 160 | !xfs_verify_icount(mp, sbp->sb_icount) || |
| 161 | sbp->sb_ifree > sbp->sb_icount)) { |
| 162 | xfs_warn(mp, "SB summary counter sanity check failed"); |
| 163 | return -EFSCORRUPTED; |
| 164 | } |
| 165 | |
| 166 | if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5) |
| 167 | return 0; |
| 168 | |
| 169 | /* |
| 170 | * Version 5 superblock feature mask validation. Reject combinations |
| 171 | * the kernel cannot support since we checked for unsupported bits in |
| 172 | * the read verifier, which means that memory is corrupt. |
| 173 | */ |
| 174 | if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) { |
| 175 | xfs_warn(mp, |
| 176 | "Corruption detected in superblock compatible features (0x%x)!", |
| 177 | (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN)); |
| 178 | return -EFSCORRUPTED; |
| 179 | } |
| 180 | |
| 181 | if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) { |
| 182 | xfs_alert(mp, |
| 183 | "Corruption detected in superblock read-only compatible features (0x%x)!", |
| 184 | (sbp->sb_features_ro_compat & |
| 185 | XFS_SB_FEAT_RO_COMPAT_UNKNOWN)); |
| 186 | return -EFSCORRUPTED; |
| 187 | } |
| 188 | if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) { |
| 189 | xfs_warn(mp, |
| 190 | "Corruption detected in superblock incompatible features (0x%x)!", |
| 191 | (sbp->sb_features_incompat & |
| 192 | XFS_SB_FEAT_INCOMPAT_UNKNOWN)); |
| 193 | return -EFSCORRUPTED; |
| 194 | } |
| 195 | if (xfs_sb_has_incompat_log_feature(sbp, |
| 196 | XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) { |
| 197 | xfs_warn(mp, |
| 198 | "Corruption detected in superblock incompatible log features (0x%x)!", |
| 199 | (sbp->sb_features_log_incompat & |
| 200 | XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)); |
| 201 | return -EFSCORRUPTED; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * We can't read verify the sb LSN because the read verifier is called |
| 206 | * before the log is allocated and processed. We know the log is set up |
| 207 | * before write verifier calls, so check it here. |
| 208 | */ |
| 209 | if (!xfs_log_check_lsn(mp, sbp->sb_lsn)) |
| 210 | return -EFSCORRUPTED; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /* Check the validity of the SB. */ |
| 216 | STATIC int |
| 217 | xfs_validate_sb_common( |
| 218 | struct xfs_mount *mp, |
| 219 | struct xfs_buf *bp, |
| 220 | struct xfs_sb *sbp) |
| 221 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 222 | struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 223 | uint32_t agcount = 0; |
| 224 | uint32_t rem; |
| 225 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 226 | if (!xfs_verify_magic(bp, dsb->sb_magicnum)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 227 | xfs_warn(mp, "bad magic number"); |
| 228 | return -EWRONGFS; |
| 229 | } |
| 230 | |
| 231 | if (!xfs_sb_good_version(sbp)) { |
| 232 | xfs_warn(mp, "bad version"); |
| 233 | return -EWRONGFS; |
| 234 | } |
| 235 | |
| 236 | if (xfs_sb_version_has_pquotino(sbp)) { |
| 237 | if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) { |
| 238 | xfs_notice(mp, |
| 239 | "Version 5 of Super block has XFS_OQUOTA bits."); |
| 240 | return -EFSCORRUPTED; |
| 241 | } |
| 242 | } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD | |
| 243 | XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) { |
| 244 | xfs_notice(mp, |
| 245 | "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits."); |
| 246 | return -EFSCORRUPTED; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Full inode chunks must be aligned to inode chunk size when |
| 251 | * sparse inodes are enabled to support the sparse chunk |
| 252 | * allocation algorithm and prevent overlapping inode records. |
| 253 | */ |
| 254 | if (xfs_sb_version_hassparseinodes(sbp)) { |
| 255 | uint32_t align; |
| 256 | |
| 257 | align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize |
| 258 | >> sbp->sb_blocklog; |
| 259 | if (sbp->sb_inoalignmt != align) { |
| 260 | xfs_warn(mp, |
| 261 | "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.", |
| 262 | sbp->sb_inoalignmt, align); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (unlikely( |
| 268 | sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { |
| 269 | xfs_warn(mp, |
| 270 | "filesystem is marked as having an external log; " |
| 271 | "specify logdev on the mount command line."); |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | if (unlikely( |
| 276 | sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { |
| 277 | xfs_warn(mp, |
| 278 | "filesystem is marked as having an internal log; " |
| 279 | "do not specify logdev on the mount command line."); |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
| 283 | /* Compute agcount for this number of dblocks and agblocks */ |
| 284 | if (sbp->sb_agblocks) { |
| 285 | agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem); |
| 286 | if (rem) |
| 287 | agcount++; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * More sanity checking. Most of these were stolen directly from |
| 292 | * xfs_repair. |
| 293 | */ |
| 294 | if (unlikely( |
| 295 | sbp->sb_agcount <= 0 || |
| 296 | sbp->sb_sectsize < XFS_MIN_SECTORSIZE || |
| 297 | sbp->sb_sectsize > XFS_MAX_SECTORSIZE || |
| 298 | sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || |
| 299 | sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || |
| 300 | sbp->sb_sectsize != (1 << sbp->sb_sectlog) || |
| 301 | sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || |
| 302 | sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || |
| 303 | sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || |
| 304 | sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || |
| 305 | sbp->sb_blocksize != (1 << sbp->sb_blocklog) || |
| 306 | sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || |
| 307 | sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || |
| 308 | sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || |
| 309 | sbp->sb_inodelog < XFS_DINODE_MIN_LOG || |
| 310 | sbp->sb_inodelog > XFS_DINODE_MAX_LOG || |
| 311 | sbp->sb_inodesize != (1 << sbp->sb_inodelog) || |
| 312 | sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE || |
| 313 | sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) || |
| 314 | XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES || |
| 315 | XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES || |
| 316 | sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 || |
| 317 | agcount == 0 || agcount != sbp->sb_agcount || |
| 318 | (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || |
| 319 | (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || |
| 320 | (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || |
| 321 | (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) || |
| 322 | sbp->sb_dblocks == 0 || |
| 323 | sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) || |
| 324 | sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) || |
| 325 | sbp->sb_shared_vn != 0)) { |
| 326 | xfs_notice(mp, "SB sanity check failed"); |
| 327 | return -EFSCORRUPTED; |
| 328 | } |
| 329 | |
| 330 | if (sbp->sb_unit) { |
| 331 | if (!xfs_sb_version_hasdalign(sbp) || |
| 332 | sbp->sb_unit > sbp->sb_width || |
| 333 | (sbp->sb_width % sbp->sb_unit) != 0) { |
| 334 | xfs_notice(mp, "SB stripe unit sanity check failed"); |
| 335 | return -EFSCORRUPTED; |
| 336 | } |
| 337 | } else if (xfs_sb_version_hasdalign(sbp)) { |
| 338 | xfs_notice(mp, "SB stripe alignment sanity check failed"); |
| 339 | return -EFSCORRUPTED; |
| 340 | } else if (sbp->sb_width) { |
| 341 | xfs_notice(mp, "SB stripe width sanity check failed"); |
| 342 | return -EFSCORRUPTED; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 347 | sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) { |
| 348 | xfs_notice(mp, "v5 SB sanity check failed"); |
| 349 | return -EFSCORRUPTED; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Until this is fixed only page-sized or smaller data blocks work. |
| 354 | */ |
| 355 | if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) { |
| 356 | xfs_warn(mp, |
| 357 | "File system with blocksize %d bytes. " |
| 358 | "Only pagesize (%ld) or less will currently work.", |
| 359 | sbp->sb_blocksize, PAGE_SIZE); |
| 360 | return -ENOSYS; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Currently only very few inode sizes are supported. |
| 365 | */ |
| 366 | switch (sbp->sb_inodesize) { |
| 367 | case 256: |
| 368 | case 512: |
| 369 | case 1024: |
| 370 | case 2048: |
| 371 | break; |
| 372 | default: |
| 373 | xfs_warn(mp, "inode size of %d bytes not supported", |
| 374 | sbp->sb_inodesize); |
| 375 | return -ENOSYS; |
| 376 | } |
| 377 | |
| 378 | if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) || |
| 379 | xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) { |
| 380 | xfs_warn(mp, |
| 381 | "file system too large to be mounted on this system."); |
| 382 | return -EFBIG; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Don't touch the filesystem if a user tool thinks it owns the primary |
| 387 | * superblock. mkfs doesn't clear the flag from secondary supers, so |
| 388 | * we don't check them at all. |
| 389 | */ |
| 390 | if (XFS_BUF_ADDR(bp) == XFS_SB_DADDR && sbp->sb_inprogress) { |
| 391 | xfs_warn(mp, "Offline file system operation in progress!"); |
| 392 | return -EFSCORRUPTED; |
| 393 | } |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | xfs_sb_quota_from_disk(struct xfs_sb *sbp) |
| 399 | { |
| 400 | /* |
| 401 | * older mkfs doesn't initialize quota inodes to NULLFSINO. This |
| 402 | * leads to in-core values having two different values for a quota |
| 403 | * inode to be invalid: 0 and NULLFSINO. Change it to a single value |
| 404 | * NULLFSINO. |
| 405 | * |
| 406 | * Note that this change affect only the in-core values. These |
| 407 | * values are not written back to disk unless any quota information |
| 408 | * is written to the disk. Even in that case, sb_pquotino field is |
| 409 | * not written to disk unless the superblock supports pquotino. |
| 410 | */ |
| 411 | if (sbp->sb_uquotino == 0) |
| 412 | sbp->sb_uquotino = NULLFSINO; |
| 413 | if (sbp->sb_gquotino == 0) |
| 414 | sbp->sb_gquotino = NULLFSINO; |
| 415 | if (sbp->sb_pquotino == 0) |
| 416 | sbp->sb_pquotino = NULLFSINO; |
| 417 | |
| 418 | /* |
| 419 | * We need to do these manipilations only if we are working |
| 420 | * with an older version of on-disk superblock. |
| 421 | */ |
| 422 | if (xfs_sb_version_has_pquotino(sbp)) |
| 423 | return; |
| 424 | |
| 425 | if (sbp->sb_qflags & XFS_OQUOTA_ENFD) |
| 426 | sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ? |
| 427 | XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD; |
| 428 | if (sbp->sb_qflags & XFS_OQUOTA_CHKD) |
| 429 | sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ? |
| 430 | XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD; |
| 431 | sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD); |
| 432 | |
| 433 | if (sbp->sb_qflags & XFS_PQUOTA_ACCT && |
| 434 | sbp->sb_gquotino != NULLFSINO) { |
| 435 | /* |
| 436 | * In older version of superblock, on-disk superblock only |
| 437 | * has sb_gquotino, and in-core superblock has both sb_gquotino |
| 438 | * and sb_pquotino. But, only one of them is supported at any |
| 439 | * point of time. So, if PQUOTA is set in disk superblock, |
| 440 | * copy over sb_gquotino to sb_pquotino. The NULLFSINO test |
| 441 | * above is to make sure we don't do this twice and wipe them |
| 442 | * both out! |
| 443 | */ |
| 444 | sbp->sb_pquotino = sbp->sb_gquotino; |
| 445 | sbp->sb_gquotino = NULLFSINO; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | static void |
| 450 | __xfs_sb_from_disk( |
| 451 | struct xfs_sb *to, |
| 452 | xfs_dsb_t *from, |
| 453 | bool convert_xquota) |
| 454 | { |
| 455 | to->sb_magicnum = be32_to_cpu(from->sb_magicnum); |
| 456 | to->sb_blocksize = be32_to_cpu(from->sb_blocksize); |
| 457 | to->sb_dblocks = be64_to_cpu(from->sb_dblocks); |
| 458 | to->sb_rblocks = be64_to_cpu(from->sb_rblocks); |
| 459 | to->sb_rextents = be64_to_cpu(from->sb_rextents); |
| 460 | memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); |
| 461 | to->sb_logstart = be64_to_cpu(from->sb_logstart); |
| 462 | to->sb_rootino = be64_to_cpu(from->sb_rootino); |
| 463 | to->sb_rbmino = be64_to_cpu(from->sb_rbmino); |
| 464 | to->sb_rsumino = be64_to_cpu(from->sb_rsumino); |
| 465 | to->sb_rextsize = be32_to_cpu(from->sb_rextsize); |
| 466 | to->sb_agblocks = be32_to_cpu(from->sb_agblocks); |
| 467 | to->sb_agcount = be32_to_cpu(from->sb_agcount); |
| 468 | to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks); |
| 469 | to->sb_logblocks = be32_to_cpu(from->sb_logblocks); |
| 470 | to->sb_versionnum = be16_to_cpu(from->sb_versionnum); |
| 471 | to->sb_sectsize = be16_to_cpu(from->sb_sectsize); |
| 472 | to->sb_inodesize = be16_to_cpu(from->sb_inodesize); |
| 473 | to->sb_inopblock = be16_to_cpu(from->sb_inopblock); |
| 474 | memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); |
| 475 | to->sb_blocklog = from->sb_blocklog; |
| 476 | to->sb_sectlog = from->sb_sectlog; |
| 477 | to->sb_inodelog = from->sb_inodelog; |
| 478 | to->sb_inopblog = from->sb_inopblog; |
| 479 | to->sb_agblklog = from->sb_agblklog; |
| 480 | to->sb_rextslog = from->sb_rextslog; |
| 481 | to->sb_inprogress = from->sb_inprogress; |
| 482 | to->sb_imax_pct = from->sb_imax_pct; |
| 483 | to->sb_icount = be64_to_cpu(from->sb_icount); |
| 484 | to->sb_ifree = be64_to_cpu(from->sb_ifree); |
| 485 | to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks); |
| 486 | to->sb_frextents = be64_to_cpu(from->sb_frextents); |
| 487 | to->sb_uquotino = be64_to_cpu(from->sb_uquotino); |
| 488 | to->sb_gquotino = be64_to_cpu(from->sb_gquotino); |
| 489 | to->sb_qflags = be16_to_cpu(from->sb_qflags); |
| 490 | to->sb_flags = from->sb_flags; |
| 491 | to->sb_shared_vn = from->sb_shared_vn; |
| 492 | to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt); |
| 493 | to->sb_unit = be32_to_cpu(from->sb_unit); |
| 494 | to->sb_width = be32_to_cpu(from->sb_width); |
| 495 | to->sb_dirblklog = from->sb_dirblklog; |
| 496 | to->sb_logsectlog = from->sb_logsectlog; |
| 497 | to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize); |
| 498 | to->sb_logsunit = be32_to_cpu(from->sb_logsunit); |
| 499 | to->sb_features2 = be32_to_cpu(from->sb_features2); |
| 500 | to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2); |
| 501 | to->sb_features_compat = be32_to_cpu(from->sb_features_compat); |
| 502 | to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat); |
| 503 | to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat); |
| 504 | to->sb_features_log_incompat = |
| 505 | be32_to_cpu(from->sb_features_log_incompat); |
| 506 | /* crc is only used on disk, not in memory; just init to 0 here. */ |
| 507 | to->sb_crc = 0; |
| 508 | to->sb_spino_align = be32_to_cpu(from->sb_spino_align); |
| 509 | to->sb_pquotino = be64_to_cpu(from->sb_pquotino); |
| 510 | to->sb_lsn = be64_to_cpu(from->sb_lsn); |
| 511 | /* |
| 512 | * sb_meta_uuid is only on disk if it differs from sb_uuid and the |
| 513 | * feature flag is set; if not set we keep it only in memory. |
| 514 | */ |
| 515 | if (xfs_sb_version_hasmetauuid(to)) |
| 516 | uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid); |
| 517 | else |
| 518 | uuid_copy(&to->sb_meta_uuid, &from->sb_uuid); |
| 519 | /* Convert on-disk flags to in-memory flags? */ |
| 520 | if (convert_xquota) |
| 521 | xfs_sb_quota_from_disk(to); |
| 522 | } |
| 523 | |
| 524 | void |
| 525 | xfs_sb_from_disk( |
| 526 | struct xfs_sb *to, |
| 527 | xfs_dsb_t *from) |
| 528 | { |
| 529 | __xfs_sb_from_disk(to, from, true); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | xfs_sb_quota_to_disk( |
| 534 | struct xfs_dsb *to, |
| 535 | struct xfs_sb *from) |
| 536 | { |
| 537 | uint16_t qflags = from->sb_qflags; |
| 538 | |
| 539 | to->sb_uquotino = cpu_to_be64(from->sb_uquotino); |
| 540 | if (xfs_sb_version_has_pquotino(from)) { |
| 541 | to->sb_qflags = cpu_to_be16(from->sb_qflags); |
| 542 | to->sb_gquotino = cpu_to_be64(from->sb_gquotino); |
| 543 | to->sb_pquotino = cpu_to_be64(from->sb_pquotino); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * The in-core version of sb_qflags do not have XFS_OQUOTA_* |
| 549 | * flags, whereas the on-disk version does. So, convert incore |
| 550 | * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags. |
| 551 | */ |
| 552 | qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD | |
| 553 | XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD); |
| 554 | |
| 555 | if (from->sb_qflags & |
| 556 | (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD)) |
| 557 | qflags |= XFS_OQUOTA_ENFD; |
| 558 | if (from->sb_qflags & |
| 559 | (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) |
| 560 | qflags |= XFS_OQUOTA_CHKD; |
| 561 | to->sb_qflags = cpu_to_be16(qflags); |
| 562 | |
| 563 | /* |
| 564 | * GQUOTINO and PQUOTINO cannot be used together in versions |
| 565 | * of superblock that do not have pquotino. from->sb_flags |
| 566 | * tells us which quota is active and should be copied to |
| 567 | * disk. If neither are active, we should NULL the inode. |
| 568 | * |
| 569 | * In all cases, the separate pquotino must remain 0 because it |
| 570 | * it beyond the "end" of the valid non-pquotino superblock. |
| 571 | */ |
| 572 | if (from->sb_qflags & XFS_GQUOTA_ACCT) |
| 573 | to->sb_gquotino = cpu_to_be64(from->sb_gquotino); |
| 574 | else if (from->sb_qflags & XFS_PQUOTA_ACCT) |
| 575 | to->sb_gquotino = cpu_to_be64(from->sb_pquotino); |
| 576 | else { |
| 577 | /* |
| 578 | * We can't rely on just the fields being logged to tell us |
| 579 | * that it is safe to write NULLFSINO - we should only do that |
| 580 | * if quotas are not actually enabled. Hence only write |
| 581 | * NULLFSINO if both in-core quota inodes are NULL. |
| 582 | */ |
| 583 | if (from->sb_gquotino == NULLFSINO && |
| 584 | from->sb_pquotino == NULLFSINO) |
| 585 | to->sb_gquotino = cpu_to_be64(NULLFSINO); |
| 586 | } |
| 587 | |
| 588 | to->sb_pquotino = 0; |
| 589 | } |
| 590 | |
| 591 | void |
| 592 | xfs_sb_to_disk( |
| 593 | struct xfs_dsb *to, |
| 594 | struct xfs_sb *from) |
| 595 | { |
| 596 | xfs_sb_quota_to_disk(to, from); |
| 597 | |
| 598 | to->sb_magicnum = cpu_to_be32(from->sb_magicnum); |
| 599 | to->sb_blocksize = cpu_to_be32(from->sb_blocksize); |
| 600 | to->sb_dblocks = cpu_to_be64(from->sb_dblocks); |
| 601 | to->sb_rblocks = cpu_to_be64(from->sb_rblocks); |
| 602 | to->sb_rextents = cpu_to_be64(from->sb_rextents); |
| 603 | memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); |
| 604 | to->sb_logstart = cpu_to_be64(from->sb_logstart); |
| 605 | to->sb_rootino = cpu_to_be64(from->sb_rootino); |
| 606 | to->sb_rbmino = cpu_to_be64(from->sb_rbmino); |
| 607 | to->sb_rsumino = cpu_to_be64(from->sb_rsumino); |
| 608 | to->sb_rextsize = cpu_to_be32(from->sb_rextsize); |
| 609 | to->sb_agblocks = cpu_to_be32(from->sb_agblocks); |
| 610 | to->sb_agcount = cpu_to_be32(from->sb_agcount); |
| 611 | to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks); |
| 612 | to->sb_logblocks = cpu_to_be32(from->sb_logblocks); |
| 613 | to->sb_versionnum = cpu_to_be16(from->sb_versionnum); |
| 614 | to->sb_sectsize = cpu_to_be16(from->sb_sectsize); |
| 615 | to->sb_inodesize = cpu_to_be16(from->sb_inodesize); |
| 616 | to->sb_inopblock = cpu_to_be16(from->sb_inopblock); |
| 617 | memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); |
| 618 | to->sb_blocklog = from->sb_blocklog; |
| 619 | to->sb_sectlog = from->sb_sectlog; |
| 620 | to->sb_inodelog = from->sb_inodelog; |
| 621 | to->sb_inopblog = from->sb_inopblog; |
| 622 | to->sb_agblklog = from->sb_agblklog; |
| 623 | to->sb_rextslog = from->sb_rextslog; |
| 624 | to->sb_inprogress = from->sb_inprogress; |
| 625 | to->sb_imax_pct = from->sb_imax_pct; |
| 626 | to->sb_icount = cpu_to_be64(from->sb_icount); |
| 627 | to->sb_ifree = cpu_to_be64(from->sb_ifree); |
| 628 | to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks); |
| 629 | to->sb_frextents = cpu_to_be64(from->sb_frextents); |
| 630 | |
| 631 | to->sb_flags = from->sb_flags; |
| 632 | to->sb_shared_vn = from->sb_shared_vn; |
| 633 | to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt); |
| 634 | to->sb_unit = cpu_to_be32(from->sb_unit); |
| 635 | to->sb_width = cpu_to_be32(from->sb_width); |
| 636 | to->sb_dirblklog = from->sb_dirblklog; |
| 637 | to->sb_logsectlog = from->sb_logsectlog; |
| 638 | to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize); |
| 639 | to->sb_logsunit = cpu_to_be32(from->sb_logsunit); |
| 640 | |
| 641 | /* |
| 642 | * We need to ensure that bad_features2 always matches features2. |
| 643 | * Hence we enforce that here rather than having to remember to do it |
| 644 | * everywhere else that updates features2. |
| 645 | */ |
| 646 | from->sb_bad_features2 = from->sb_features2; |
| 647 | to->sb_features2 = cpu_to_be32(from->sb_features2); |
| 648 | to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2); |
| 649 | |
| 650 | if (xfs_sb_version_hascrc(from)) { |
| 651 | to->sb_features_compat = cpu_to_be32(from->sb_features_compat); |
| 652 | to->sb_features_ro_compat = |
| 653 | cpu_to_be32(from->sb_features_ro_compat); |
| 654 | to->sb_features_incompat = |
| 655 | cpu_to_be32(from->sb_features_incompat); |
| 656 | to->sb_features_log_incompat = |
| 657 | cpu_to_be32(from->sb_features_log_incompat); |
| 658 | to->sb_spino_align = cpu_to_be32(from->sb_spino_align); |
| 659 | to->sb_lsn = cpu_to_be64(from->sb_lsn); |
| 660 | if (xfs_sb_version_hasmetauuid(from)) |
| 661 | uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * If the superblock has the CRC feature bit set or the CRC field is non-null, |
| 667 | * check that the CRC is valid. We check the CRC field is non-null because a |
| 668 | * single bit error could clear the feature bit and unused parts of the |
| 669 | * superblock are supposed to be zero. Hence a non-null crc field indicates that |
| 670 | * we've potentially lost a feature bit and we should check it anyway. |
| 671 | * |
| 672 | * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the |
| 673 | * last field in V4 secondary superblocks. So for secondary superblocks, |
| 674 | * we are more forgiving, and ignore CRC failures if the primary doesn't |
| 675 | * indicate that the fs version is V5. |
| 676 | */ |
| 677 | static void |
| 678 | xfs_sb_read_verify( |
| 679 | struct xfs_buf *bp) |
| 680 | { |
| 681 | struct xfs_sb sb; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 682 | struct xfs_mount *mp = bp->b_mount; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 683 | struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp); |
| 684 | int error; |
| 685 | |
| 686 | /* |
| 687 | * open code the version check to avoid needing to convert the entire |
| 688 | * superblock from disk order just to check the version number |
| 689 | */ |
| 690 | if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) && |
| 691 | (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) == |
| 692 | XFS_SB_VERSION_5) || |
| 693 | dsb->sb_crc != 0)) { |
| 694 | |
| 695 | if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) { |
| 696 | /* Only fail bad secondaries on a known V5 filesystem */ |
| 697 | if (bp->b_bn == XFS_SB_DADDR || |
| 698 | xfs_sb_version_hascrc(&mp->m_sb)) { |
| 699 | error = -EFSBADCRC; |
| 700 | goto out_error; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Check all the superblock fields. Don't byteswap the xquota flags |
| 707 | * because _verify_common checks the on-disk values. |
| 708 | */ |
| 709 | __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false); |
| 710 | error = xfs_validate_sb_common(mp, bp, &sb); |
| 711 | if (error) |
| 712 | goto out_error; |
| 713 | error = xfs_validate_sb_read(mp, &sb); |
| 714 | |
| 715 | out_error: |
| 716 | if (error == -EFSCORRUPTED || error == -EFSBADCRC) |
| 717 | xfs_verifier_error(bp, error, __this_address); |
| 718 | else if (error) |
| 719 | xfs_buf_ioerror(bp, error); |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * We may be probed for a filesystem match, so we may not want to emit |
| 724 | * messages when the superblock buffer is not actually an XFS superblock. |
| 725 | * If we find an XFS superblock, then run a normal, noisy mount because we are |
| 726 | * really going to mount it and want to know about errors. |
| 727 | */ |
| 728 | static void |
| 729 | xfs_sb_quiet_read_verify( |
| 730 | struct xfs_buf *bp) |
| 731 | { |
| 732 | struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp); |
| 733 | |
| 734 | if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) { |
| 735 | /* XFS filesystem, verify noisily! */ |
| 736 | xfs_sb_read_verify(bp); |
| 737 | return; |
| 738 | } |
| 739 | /* quietly fail */ |
| 740 | xfs_buf_ioerror(bp, -EWRONGFS); |
| 741 | } |
| 742 | |
| 743 | static void |
| 744 | xfs_sb_write_verify( |
| 745 | struct xfs_buf *bp) |
| 746 | { |
| 747 | struct xfs_sb sb; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 748 | struct xfs_mount *mp = bp->b_mount; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 749 | struct xfs_buf_log_item *bip = bp->b_log_item; |
| 750 | int error; |
| 751 | |
| 752 | /* |
| 753 | * Check all the superblock fields. Don't byteswap the xquota flags |
| 754 | * because _verify_common checks the on-disk values. |
| 755 | */ |
| 756 | __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false); |
| 757 | error = xfs_validate_sb_common(mp, bp, &sb); |
| 758 | if (error) |
| 759 | goto out_error; |
| 760 | error = xfs_validate_sb_write(mp, bp, &sb); |
| 761 | if (error) |
| 762 | goto out_error; |
| 763 | |
| 764 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 765 | return; |
| 766 | |
| 767 | if (bip) |
| 768 | XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 769 | |
| 770 | xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF); |
| 771 | return; |
| 772 | |
| 773 | out_error: |
| 774 | xfs_verifier_error(bp, error, __this_address); |
| 775 | } |
| 776 | |
| 777 | const struct xfs_buf_ops xfs_sb_buf_ops = { |
| 778 | .name = "xfs_sb", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 779 | .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 780 | .verify_read = xfs_sb_read_verify, |
| 781 | .verify_write = xfs_sb_write_verify, |
| 782 | }; |
| 783 | |
| 784 | const struct xfs_buf_ops xfs_sb_quiet_buf_ops = { |
| 785 | .name = "xfs_sb_quiet", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 786 | .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 787 | .verify_read = xfs_sb_quiet_read_verify, |
| 788 | .verify_write = xfs_sb_write_verify, |
| 789 | }; |
| 790 | |
| 791 | /* |
| 792 | * xfs_mount_common |
| 793 | * |
| 794 | * Mount initialization code establishing various mount |
| 795 | * fields from the superblock associated with the given |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 796 | * mount structure. |
| 797 | * |
| 798 | * Inode geometry are calculated in xfs_ialloc_setup_geometry. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 799 | */ |
| 800 | void |
| 801 | xfs_sb_mount_common( |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 802 | struct xfs_mount *mp, |
| 803 | struct xfs_sb *sbp) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 804 | { |
| 805 | mp->m_agfrotor = mp->m_agirotor = 0; |
| 806 | mp->m_maxagi = mp->m_sb.sb_agcount; |
| 807 | mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; |
| 808 | mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; |
| 809 | mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; |
| 810 | mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 811 | mp->m_blockmask = sbp->sb_blocksize - 1; |
| 812 | mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; |
| 813 | mp->m_blockwmask = mp->m_blockwsize - 1; |
| 814 | |
| 815 | mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 816 | mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 817 | mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2; |
| 818 | mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2; |
| 819 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 820 | mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 821 | mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 822 | mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2; |
| 823 | mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2; |
| 824 | |
| 825 | mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1); |
| 826 | mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0); |
| 827 | mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2; |
| 828 | mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2; |
| 829 | |
| 830 | mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true); |
| 831 | mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false); |
| 832 | mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2; |
| 833 | mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2; |
| 834 | |
| 835 | mp->m_bsize = XFS_FSB_TO_BB(mp, 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 836 | mp->m_alloc_set_aside = xfs_alloc_set_aside(mp); |
| 837 | mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp); |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * xfs_initialize_perag_data |
| 842 | * |
| 843 | * Read in each per-ag structure so we can count up the number of |
| 844 | * allocated inodes, free inodes and used filesystem blocks as this |
| 845 | * information is no longer persistent in the superblock. Once we have |
| 846 | * this information, write it into the in-core superblock structure. |
| 847 | */ |
| 848 | int |
| 849 | xfs_initialize_perag_data( |
| 850 | struct xfs_mount *mp, |
| 851 | xfs_agnumber_t agcount) |
| 852 | { |
| 853 | xfs_agnumber_t index; |
| 854 | xfs_perag_t *pag; |
| 855 | xfs_sb_t *sbp = &mp->m_sb; |
| 856 | uint64_t ifree = 0; |
| 857 | uint64_t ialloc = 0; |
| 858 | uint64_t bfree = 0; |
| 859 | uint64_t bfreelst = 0; |
| 860 | uint64_t btree = 0; |
| 861 | uint64_t fdblocks; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 862 | int error = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 863 | |
| 864 | for (index = 0; index < agcount; index++) { |
| 865 | /* |
| 866 | * read the agf, then the agi. This gets us |
| 867 | * all the information we need and populates the |
| 868 | * per-ag structures for us. |
| 869 | */ |
| 870 | error = xfs_alloc_pagf_init(mp, NULL, index, 0); |
| 871 | if (error) |
| 872 | return error; |
| 873 | |
| 874 | error = xfs_ialloc_pagi_init(mp, NULL, index); |
| 875 | if (error) |
| 876 | return error; |
| 877 | pag = xfs_perag_get(mp, index); |
| 878 | ifree += pag->pagi_freecount; |
| 879 | ialloc += pag->pagi_count; |
| 880 | bfree += pag->pagf_freeblks; |
| 881 | bfreelst += pag->pagf_flcount; |
| 882 | btree += pag->pagf_btreeblks; |
| 883 | xfs_perag_put(pag); |
| 884 | } |
| 885 | fdblocks = bfree + bfreelst + btree; |
| 886 | |
| 887 | /* |
| 888 | * If the new summary counts are obviously incorrect, fail the |
| 889 | * mount operation because that implies the AGFs are also corrupt. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 890 | * Clear FS_COUNTERS so that we don't unmount with a dirty log, which |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 891 | * will prevent xfs_repair from fixing anything. |
| 892 | */ |
| 893 | if (fdblocks > sbp->sb_dblocks || ifree > ialloc) { |
| 894 | xfs_alert(mp, "AGF corruption. Please run xfs_repair."); |
| 895 | error = -EFSCORRUPTED; |
| 896 | goto out; |
| 897 | } |
| 898 | |
| 899 | /* Overwrite incore superblock counters with just-read data */ |
| 900 | spin_lock(&mp->m_sb_lock); |
| 901 | sbp->sb_ifree = ifree; |
| 902 | sbp->sb_icount = ialloc; |
| 903 | sbp->sb_fdblocks = fdblocks; |
| 904 | spin_unlock(&mp->m_sb_lock); |
| 905 | |
| 906 | xfs_reinit_percpu_counters(mp); |
| 907 | out: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 908 | xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 909 | return error; |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock |
| 914 | * into the superblock buffer to be logged. It does not provide the higher |
| 915 | * level of locking that is needed to protect the in-core superblock from |
| 916 | * concurrent access. |
| 917 | */ |
| 918 | void |
| 919 | xfs_log_sb( |
| 920 | struct xfs_trans *tp) |
| 921 | { |
| 922 | struct xfs_mount *mp = tp->t_mountp; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 923 | struct xfs_buf *bp = xfs_trans_getsb(tp, mp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 924 | |
| 925 | mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount); |
| 926 | mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree); |
| 927 | mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks); |
| 928 | |
| 929 | xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb); |
| 930 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 931 | xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* |
| 935 | * xfs_sync_sb |
| 936 | * |
| 937 | * Sync the superblock to disk. |
| 938 | * |
| 939 | * Note that the caller is responsible for checking the frozen state of the |
| 940 | * filesystem. This procedure uses the non-blocking transaction allocator and |
| 941 | * thus will allow modifications to a frozen fs. This is required because this |
| 942 | * code can be called during the process of freezing where use of the high-level |
| 943 | * allocator would deadlock. |
| 944 | */ |
| 945 | int |
| 946 | xfs_sync_sb( |
| 947 | struct xfs_mount *mp, |
| 948 | bool wait) |
| 949 | { |
| 950 | struct xfs_trans *tp; |
| 951 | int error; |
| 952 | |
| 953 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, |
| 954 | XFS_TRANS_NO_WRITECOUNT, &tp); |
| 955 | if (error) |
| 956 | return error; |
| 957 | |
| 958 | xfs_log_sb(tp); |
| 959 | if (wait) |
| 960 | xfs_trans_set_sync(tp); |
| 961 | return xfs_trans_commit(tp); |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * Update all the secondary superblocks to match the new state of the primary. |
| 966 | * Because we are completely overwriting all the existing fields in the |
| 967 | * secondary superblock buffers, there is no need to read them in from disk. |
| 968 | * Just get a new buffer, stamp it and write it. |
| 969 | * |
| 970 | * The sb buffers need to be cached here so that we serialise against other |
| 971 | * operations that access the secondary superblocks, but we don't want to keep |
| 972 | * them in memory once it is written so we mark it as a one-shot buffer. |
| 973 | */ |
| 974 | int |
| 975 | xfs_update_secondary_sbs( |
| 976 | struct xfs_mount *mp) |
| 977 | { |
| 978 | xfs_agnumber_t agno; |
| 979 | int saved_error = 0; |
| 980 | int error = 0; |
| 981 | LIST_HEAD (buffer_list); |
| 982 | |
| 983 | /* update secondary superblocks. */ |
| 984 | for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) { |
| 985 | struct xfs_buf *bp; |
| 986 | |
| 987 | bp = xfs_buf_get(mp->m_ddev_targp, |
| 988 | XFS_AG_DADDR(mp, agno, XFS_SB_DADDR), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 989 | XFS_FSS_TO_BB(mp, 1)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 990 | /* |
| 991 | * If we get an error reading or writing alternate superblocks, |
| 992 | * continue. xfs_repair chooses the "best" superblock based |
| 993 | * on most matches; if we break early, we'll leave more |
| 994 | * superblocks un-updated than updated, and xfs_repair may |
| 995 | * pick them over the properly-updated primary. |
| 996 | */ |
| 997 | if (!bp) { |
| 998 | xfs_warn(mp, |
| 999 | "error allocating secondary superblock for ag %d", |
| 1000 | agno); |
| 1001 | if (!saved_error) |
| 1002 | saved_error = -ENOMEM; |
| 1003 | continue; |
| 1004 | } |
| 1005 | |
| 1006 | bp->b_ops = &xfs_sb_buf_ops; |
| 1007 | xfs_buf_oneshot(bp); |
| 1008 | xfs_buf_zero(bp, 0, BBTOB(bp->b_length)); |
| 1009 | xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb); |
| 1010 | xfs_buf_delwri_queue(bp, &buffer_list); |
| 1011 | xfs_buf_relse(bp); |
| 1012 | |
| 1013 | /* don't hold too many buffers at once */ |
| 1014 | if (agno % 16) |
| 1015 | continue; |
| 1016 | |
| 1017 | error = xfs_buf_delwri_submit(&buffer_list); |
| 1018 | if (error) { |
| 1019 | xfs_warn(mp, |
| 1020 | "write error %d updating a secondary superblock near ag %d", |
| 1021 | error, agno); |
| 1022 | if (!saved_error) |
| 1023 | saved_error = error; |
| 1024 | continue; |
| 1025 | } |
| 1026 | } |
| 1027 | error = xfs_buf_delwri_submit(&buffer_list); |
| 1028 | if (error) { |
| 1029 | xfs_warn(mp, |
| 1030 | "write error %d updating a secondary superblock near ag %d", |
| 1031 | error, agno); |
| 1032 | } |
| 1033 | |
| 1034 | return saved_error ? saved_error : error; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Same behavior as xfs_sync_sb, except that it is always synchronous and it |
| 1039 | * also writes the superblock buffer to disk sector 0 immediately. |
| 1040 | */ |
| 1041 | int |
| 1042 | xfs_sync_sb_buf( |
| 1043 | struct xfs_mount *mp) |
| 1044 | { |
| 1045 | struct xfs_trans *tp; |
| 1046 | struct xfs_buf *bp; |
| 1047 | int error; |
| 1048 | |
| 1049 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp); |
| 1050 | if (error) |
| 1051 | return error; |
| 1052 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1053 | bp = xfs_trans_getsb(tp, mp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1054 | xfs_log_sb(tp); |
| 1055 | xfs_trans_bhold(tp, bp); |
| 1056 | xfs_trans_set_sync(tp); |
| 1057 | error = xfs_trans_commit(tp); |
| 1058 | if (error) |
| 1059 | goto out; |
| 1060 | /* |
| 1061 | * write out the sb buffer to get the changes to disk |
| 1062 | */ |
| 1063 | error = xfs_bwrite(bp); |
| 1064 | out: |
| 1065 | xfs_buf_relse(bp); |
| 1066 | return error; |
| 1067 | } |
| 1068 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1069 | void |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1070 | xfs_fs_geometry( |
| 1071 | struct xfs_sb *sbp, |
| 1072 | struct xfs_fsop_geom *geo, |
| 1073 | int struct_version) |
| 1074 | { |
| 1075 | memset(geo, 0, sizeof(struct xfs_fsop_geom)); |
| 1076 | |
| 1077 | geo->blocksize = sbp->sb_blocksize; |
| 1078 | geo->rtextsize = sbp->sb_rextsize; |
| 1079 | geo->agblocks = sbp->sb_agblocks; |
| 1080 | geo->agcount = sbp->sb_agcount; |
| 1081 | geo->logblocks = sbp->sb_logblocks; |
| 1082 | geo->sectsize = sbp->sb_sectsize; |
| 1083 | geo->inodesize = sbp->sb_inodesize; |
| 1084 | geo->imaxpct = sbp->sb_imax_pct; |
| 1085 | geo->datablocks = sbp->sb_dblocks; |
| 1086 | geo->rtblocks = sbp->sb_rblocks; |
| 1087 | geo->rtextents = sbp->sb_rextents; |
| 1088 | geo->logstart = sbp->sb_logstart; |
| 1089 | BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid)); |
| 1090 | memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid)); |
| 1091 | |
| 1092 | if (struct_version < 2) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1093 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1094 | |
| 1095 | geo->sunit = sbp->sb_unit; |
| 1096 | geo->swidth = sbp->sb_width; |
| 1097 | |
| 1098 | if (struct_version < 3) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1099 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1100 | |
| 1101 | geo->version = XFS_FSOP_GEOM_VERSION; |
| 1102 | geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1103 | XFS_FSOP_GEOM_FLAGS_DIRV2 | |
| 1104 | XFS_FSOP_GEOM_FLAGS_EXTFLG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1105 | if (xfs_sb_version_hasattr(sbp)) |
| 1106 | geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR; |
| 1107 | if (xfs_sb_version_hasquota(sbp)) |
| 1108 | geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA; |
| 1109 | if (xfs_sb_version_hasalign(sbp)) |
| 1110 | geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN; |
| 1111 | if (xfs_sb_version_hasdalign(sbp)) |
| 1112 | geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1113 | if (xfs_sb_version_hassector(sbp)) |
| 1114 | geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR; |
| 1115 | if (xfs_sb_version_hasasciici(sbp)) |
| 1116 | geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI; |
| 1117 | if (xfs_sb_version_haslazysbcount(sbp)) |
| 1118 | geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB; |
| 1119 | if (xfs_sb_version_hasattr2(sbp)) |
| 1120 | geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2; |
| 1121 | if (xfs_sb_version_hasprojid32bit(sbp)) |
| 1122 | geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32; |
| 1123 | if (xfs_sb_version_hascrc(sbp)) |
| 1124 | geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB; |
| 1125 | if (xfs_sb_version_hasftype(sbp)) |
| 1126 | geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE; |
| 1127 | if (xfs_sb_version_hasfinobt(sbp)) |
| 1128 | geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT; |
| 1129 | if (xfs_sb_version_hassparseinodes(sbp)) |
| 1130 | geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES; |
| 1131 | if (xfs_sb_version_hasrmapbt(sbp)) |
| 1132 | geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT; |
| 1133 | if (xfs_sb_version_hasreflink(sbp)) |
| 1134 | geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK; |
| 1135 | if (xfs_sb_version_hassector(sbp)) |
| 1136 | geo->logsectsize = sbp->sb_logsectsize; |
| 1137 | else |
| 1138 | geo->logsectsize = BBSIZE; |
| 1139 | geo->rtsectsize = sbp->sb_blocksize; |
| 1140 | geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp); |
| 1141 | |
| 1142 | if (struct_version < 4) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1143 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1144 | |
| 1145 | if (xfs_sb_version_haslogv2(sbp)) |
| 1146 | geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2; |
| 1147 | |
| 1148 | geo->logsunit = sbp->sb_logsunit; |
| 1149 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1150 | if (struct_version < 5) |
| 1151 | return; |
| 1152 | |
| 1153 | geo->version = XFS_FSOP_GEOM_VERSION_V5; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /* Read a secondary superblock. */ |
| 1157 | int |
| 1158 | xfs_sb_read_secondary( |
| 1159 | struct xfs_mount *mp, |
| 1160 | struct xfs_trans *tp, |
| 1161 | xfs_agnumber_t agno, |
| 1162 | struct xfs_buf **bpp) |
| 1163 | { |
| 1164 | struct xfs_buf *bp; |
| 1165 | int error; |
| 1166 | |
| 1167 | ASSERT(agno != 0 && agno != NULLAGNUMBER); |
| 1168 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
| 1169 | XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)), |
| 1170 | XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops); |
| 1171 | if (error) |
| 1172 | return error; |
| 1173 | xfs_buf_set_ref(bp, XFS_SSB_REF); |
| 1174 | *bpp = bp; |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | /* Get an uninitialised secondary superblock buffer. */ |
| 1179 | int |
| 1180 | xfs_sb_get_secondary( |
| 1181 | struct xfs_mount *mp, |
| 1182 | struct xfs_trans *tp, |
| 1183 | xfs_agnumber_t agno, |
| 1184 | struct xfs_buf **bpp) |
| 1185 | { |
| 1186 | struct xfs_buf *bp; |
| 1187 | |
| 1188 | ASSERT(agno != 0 && agno != NULLAGNUMBER); |
| 1189 | bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, |
| 1190 | XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)), |
| 1191 | XFS_FSS_TO_BB(mp, 1), 0); |
| 1192 | if (!bp) |
| 1193 | return -ENOMEM; |
| 1194 | bp->b_ops = &xfs_sb_buf_ops; |
| 1195 | xfs_buf_oneshot(bp); |
| 1196 | *bpp = bp; |
| 1197 | return 0; |
| 1198 | } |