Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | #include "xfs.h" |
| 19 | #include "xfs_fs.h" |
Dave Chinner | 632b89e | 2013-10-29 22:11:58 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 21 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 28 | #include "xfs_inode.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 29 | #include "xfs_ialloc.h" |
| 30 | #include "xfs_alloc.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 31 | #include "xfs_error.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 32 | #include "xfs_trace.h" |
| 33 | #include "xfs_cksum.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 34 | #include "xfs_trans.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 35 | #include "xfs_buf_item.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 36 | #include "xfs_bmap_btree.h" |
| 37 | #include "xfs_alloc_btree.h" |
| 38 | #include "xfs_ialloc_btree.h" |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 39 | #include "xfs_log.h" |
Darrick J. Wong | 035e00a | 2016-08-03 11:36:07 +1000 | [diff] [blame] | 40 | #include "xfs_rmap_btree.h" |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Physical superblock buffer manipulations. Shared with libxfs in userspace. |
| 44 | */ |
| 45 | |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 46 | /* |
| 47 | * Reference counting access wrappers to the perag structures. |
| 48 | * Because we never free per-ag structures, the only thing we |
| 49 | * have to protect against changes is the tree structure itself. |
| 50 | */ |
| 51 | struct xfs_perag * |
| 52 | xfs_perag_get( |
| 53 | struct xfs_mount *mp, |
| 54 | xfs_agnumber_t agno) |
| 55 | { |
| 56 | struct xfs_perag *pag; |
| 57 | int ref = 0; |
| 58 | |
| 59 | rcu_read_lock(); |
| 60 | pag = radix_tree_lookup(&mp->m_perag_tree, agno); |
| 61 | if (pag) { |
| 62 | ASSERT(atomic_read(&pag->pag_ref) >= 0); |
| 63 | ref = atomic_inc_return(&pag->pag_ref); |
| 64 | } |
| 65 | rcu_read_unlock(); |
| 66 | trace_xfs_perag_get(mp, agno, ref, _RET_IP_); |
| 67 | return pag; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * search from @first to find the next perag with the given tag set. |
| 72 | */ |
| 73 | struct xfs_perag * |
| 74 | xfs_perag_get_tag( |
| 75 | struct xfs_mount *mp, |
| 76 | xfs_agnumber_t first, |
| 77 | int tag) |
| 78 | { |
| 79 | struct xfs_perag *pag; |
| 80 | int found; |
| 81 | int ref; |
| 82 | |
| 83 | rcu_read_lock(); |
| 84 | found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, |
| 85 | (void **)&pag, first, 1, tag); |
| 86 | if (found <= 0) { |
| 87 | rcu_read_unlock(); |
| 88 | return NULL; |
| 89 | } |
| 90 | ref = atomic_inc_return(&pag->pag_ref); |
| 91 | rcu_read_unlock(); |
| 92 | trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_); |
| 93 | return pag; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | xfs_perag_put( |
| 98 | struct xfs_perag *pag) |
| 99 | { |
| 100 | int ref; |
| 101 | |
| 102 | ASSERT(atomic_read(&pag->pag_ref) > 0); |
| 103 | ref = atomic_dec_return(&pag->pag_ref); |
| 104 | trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Check the validity of the SB found. |
| 109 | */ |
| 110 | STATIC int |
| 111 | xfs_mount_validate_sb( |
| 112 | xfs_mount_t *mp, |
| 113 | xfs_sb_t *sbp, |
| 114 | bool check_inprogress, |
| 115 | bool check_version) |
| 116 | { |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 117 | if (sbp->sb_magicnum != XFS_SB_MAGIC) { |
| 118 | xfs_warn(mp, "bad magic number"); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 119 | return -EWRONGFS; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | if (!xfs_sb_good_version(sbp)) { |
| 124 | xfs_warn(mp, "bad version"); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 125 | return -EWRONGFS; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Version 5 superblock feature mask validation. Reject combinations the |
| 130 | * kernel cannot support up front before checking anything else. For |
| 131 | * write validation, we don't need to check feature masks. |
| 132 | */ |
| 133 | if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) { |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 134 | if (xfs_sb_has_compat_feature(sbp, |
| 135 | XFS_SB_FEAT_COMPAT_UNKNOWN)) { |
| 136 | xfs_warn(mp, |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 137 | "Superblock has unknown compatible features (0x%x) enabled.", |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 138 | (sbp->sb_features_compat & |
| 139 | XFS_SB_FEAT_COMPAT_UNKNOWN)); |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 140 | xfs_warn(mp, |
| 141 | "Using a more recent kernel is recommended."); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (xfs_sb_has_ro_compat_feature(sbp, |
| 145 | XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) { |
| 146 | xfs_alert(mp, |
| 147 | "Superblock has unknown read-only compatible features (0x%x) enabled.", |
| 148 | (sbp->sb_features_ro_compat & |
| 149 | XFS_SB_FEAT_RO_COMPAT_UNKNOWN)); |
| 150 | if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { |
| 151 | xfs_warn(mp, |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 152 | "Attempted to mount read-only compatible filesystem read-write."); |
| 153 | xfs_warn(mp, |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 154 | "Filesystem can only be safely mounted read only."); |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 155 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 156 | return -EINVAL; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | if (xfs_sb_has_incompat_feature(sbp, |
| 160 | XFS_SB_FEAT_INCOMPAT_UNKNOWN)) { |
| 161 | xfs_warn(mp, |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 162 | "Superblock has unknown incompatible features (0x%x) enabled.", |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 163 | (sbp->sb_features_incompat & |
| 164 | XFS_SB_FEAT_INCOMPAT_UNKNOWN)); |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 165 | xfs_warn(mp, |
| 166 | "Filesystem can not be safely mounted by this kernel."); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 167 | return -EINVAL; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 168 | } |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 169 | } else if (xfs_sb_version_hascrc(sbp)) { |
| 170 | /* |
| 171 | * We can't read verify the sb LSN because the read verifier is |
| 172 | * called before the log is allocated and processed. We know the |
| 173 | * log is set up before write verifier (!check_version) calls, |
| 174 | * so just check it here. |
| 175 | */ |
| 176 | if (!xfs_log_check_lsn(mp, sbp->sb_lsn)) |
| 177 | return -EFSCORRUPTED; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | if (xfs_sb_version_has_pquotino(sbp)) { |
| 181 | if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) { |
| 182 | xfs_notice(mp, |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 183 | "Version 5 of Super block has XFS_OQUOTA bits."); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 184 | return -EFSCORRUPTED; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 185 | } |
| 186 | } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD | |
| 187 | XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) { |
| 188 | xfs_notice(mp, |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 189 | "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits."); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 190 | return -EFSCORRUPTED; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 191 | } |
| 192 | |
Brian Foster | e5376fc | 2015-05-29 08:57:27 +1000 | [diff] [blame] | 193 | /* |
| 194 | * Full inode chunks must be aligned to inode chunk size when |
| 195 | * sparse inodes are enabled to support the sparse chunk |
| 196 | * allocation algorithm and prevent overlapping inode records. |
| 197 | */ |
| 198 | if (xfs_sb_version_hassparseinodes(sbp)) { |
| 199 | uint32_t align; |
| 200 | |
Brian Foster | e5376fc | 2015-05-29 08:57:27 +1000 | [diff] [blame] | 201 | align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize |
| 202 | >> sbp->sb_blocklog; |
| 203 | if (sbp->sb_inoalignmt != align) { |
| 204 | xfs_warn(mp, |
| 205 | "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.", |
| 206 | sbp->sb_inoalignmt, align); |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | } |
| 210 | |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 211 | if (unlikely( |
| 212 | sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { |
| 213 | xfs_warn(mp, |
| 214 | "filesystem is marked as having an external log; " |
| 215 | "specify logdev on the mount command line."); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 216 | return -EINVAL; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (unlikely( |
| 220 | sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { |
| 221 | xfs_warn(mp, |
| 222 | "filesystem is marked as having an internal log; " |
| 223 | "do not specify logdev on the mount command line."); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 224 | return -EINVAL; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * More sanity checking. Most of these were stolen directly from |
| 229 | * xfs_repair. |
| 230 | */ |
| 231 | if (unlikely( |
| 232 | sbp->sb_agcount <= 0 || |
| 233 | sbp->sb_sectsize < XFS_MIN_SECTORSIZE || |
| 234 | sbp->sb_sectsize > XFS_MAX_SECTORSIZE || |
| 235 | sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || |
| 236 | sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || |
| 237 | sbp->sb_sectsize != (1 << sbp->sb_sectlog) || |
| 238 | sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || |
| 239 | sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || |
| 240 | sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || |
| 241 | sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || |
| 242 | sbp->sb_blocksize != (1 << sbp->sb_blocklog) || |
Eric Sandeen | e1b0572 | 2014-09-09 11:47:24 +1000 | [diff] [blame] | 243 | sbp->sb_dirblklog > XFS_MAX_BLOCKSIZE_LOG || |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 244 | sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || |
| 245 | sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || |
| 246 | sbp->sb_inodelog < XFS_DINODE_MIN_LOG || |
| 247 | sbp->sb_inodelog > XFS_DINODE_MAX_LOG || |
| 248 | sbp->sb_inodesize != (1 << sbp->sb_inodelog) || |
Eric Sandeen | e1b0572 | 2014-09-09 11:47:24 +1000 | [diff] [blame] | 249 | sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE || |
Eric Sandeen | 392c6de | 2014-02-07 15:26:11 +1100 | [diff] [blame] | 250 | sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) || |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 251 | (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || |
| 252 | (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || |
| 253 | (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || |
| 254 | (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) || |
| 255 | sbp->sb_dblocks == 0 || |
| 256 | sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) || |
Dave Chinner | ab3e57b | 2014-05-20 07:47:05 +1000 | [diff] [blame] | 257 | sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) || |
| 258 | sbp->sb_shared_vn != 0)) { |
Eric Sandeen | 5ef11eb | 2014-02-19 15:39:35 +1100 | [diff] [blame] | 259 | xfs_notice(mp, "SB sanity check failed"); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 260 | return -EFSCORRUPTED; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Until this is fixed only page-sized or smaller data blocks work. |
| 265 | */ |
| 266 | if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) { |
| 267 | xfs_warn(mp, |
| 268 | "File system with blocksize %d bytes. " |
| 269 | "Only pagesize (%ld) or less will currently work.", |
| 270 | sbp->sb_blocksize, PAGE_SIZE); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 271 | return -ENOSYS; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Currently only very few inode sizes are supported. |
| 276 | */ |
| 277 | switch (sbp->sb_inodesize) { |
| 278 | case 256: |
| 279 | case 512: |
| 280 | case 1024: |
| 281 | case 2048: |
| 282 | break; |
| 283 | default: |
| 284 | xfs_warn(mp, "inode size of %d bytes not supported", |
| 285 | sbp->sb_inodesize); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 286 | return -ENOSYS; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) || |
| 290 | xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) { |
| 291 | xfs_warn(mp, |
| 292 | "file system too large to be mounted on this system."); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 293 | return -EFBIG; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if (check_inprogress && sbp->sb_inprogress) { |
| 297 | xfs_warn(mp, "Offline file system operation in progress!"); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 298 | return -EFSCORRUPTED; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 299 | } |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | xfs_sb_quota_from_disk(struct xfs_sb *sbp) |
| 305 | { |
| 306 | /* |
| 307 | * older mkfs doesn't initialize quota inodes to NULLFSINO. This |
| 308 | * leads to in-core values having two different values for a quota |
| 309 | * inode to be invalid: 0 and NULLFSINO. Change it to a single value |
| 310 | * NULLFSINO. |
| 311 | * |
| 312 | * Note that this change affect only the in-core values. These |
| 313 | * values are not written back to disk unless any quota information |
| 314 | * is written to the disk. Even in that case, sb_pquotino field is |
| 315 | * not written to disk unless the superblock supports pquotino. |
| 316 | */ |
| 317 | if (sbp->sb_uquotino == 0) |
| 318 | sbp->sb_uquotino = NULLFSINO; |
| 319 | if (sbp->sb_gquotino == 0) |
| 320 | sbp->sb_gquotino = NULLFSINO; |
| 321 | if (sbp->sb_pquotino == 0) |
| 322 | sbp->sb_pquotino = NULLFSINO; |
| 323 | |
| 324 | /* |
| 325 | * We need to do these manipilations only if we are working |
| 326 | * with an older version of on-disk superblock. |
| 327 | */ |
| 328 | if (xfs_sb_version_has_pquotino(sbp)) |
| 329 | return; |
| 330 | |
| 331 | if (sbp->sb_qflags & XFS_OQUOTA_ENFD) |
| 332 | sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ? |
| 333 | XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD; |
| 334 | if (sbp->sb_qflags & XFS_OQUOTA_CHKD) |
| 335 | sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ? |
| 336 | XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD; |
| 337 | sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD); |
| 338 | |
| 339 | if (sbp->sb_qflags & XFS_PQUOTA_ACCT) { |
| 340 | /* |
| 341 | * In older version of superblock, on-disk superblock only |
| 342 | * has sb_gquotino, and in-core superblock has both sb_gquotino |
| 343 | * and sb_pquotino. But, only one of them is supported at any |
| 344 | * point of time. So, if PQUOTA is set in disk superblock, |
| 345 | * copy over sb_gquotino to sb_pquotino. |
| 346 | */ |
| 347 | sbp->sb_pquotino = sbp->sb_gquotino; |
| 348 | sbp->sb_gquotino = NULLFSINO; |
| 349 | } |
| 350 | } |
| 351 | |
Eric Sandeen | 5ef828c | 2014-08-04 11:35:44 +1000 | [diff] [blame] | 352 | static void |
| 353 | __xfs_sb_from_disk( |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 354 | struct xfs_sb *to, |
Eric Sandeen | 5ef828c | 2014-08-04 11:35:44 +1000 | [diff] [blame] | 355 | xfs_dsb_t *from, |
| 356 | bool convert_xquota) |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 357 | { |
| 358 | to->sb_magicnum = be32_to_cpu(from->sb_magicnum); |
| 359 | to->sb_blocksize = be32_to_cpu(from->sb_blocksize); |
| 360 | to->sb_dblocks = be64_to_cpu(from->sb_dblocks); |
| 361 | to->sb_rblocks = be64_to_cpu(from->sb_rblocks); |
| 362 | to->sb_rextents = be64_to_cpu(from->sb_rextents); |
| 363 | memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); |
| 364 | to->sb_logstart = be64_to_cpu(from->sb_logstart); |
| 365 | to->sb_rootino = be64_to_cpu(from->sb_rootino); |
| 366 | to->sb_rbmino = be64_to_cpu(from->sb_rbmino); |
| 367 | to->sb_rsumino = be64_to_cpu(from->sb_rsumino); |
| 368 | to->sb_rextsize = be32_to_cpu(from->sb_rextsize); |
| 369 | to->sb_agblocks = be32_to_cpu(from->sb_agblocks); |
| 370 | to->sb_agcount = be32_to_cpu(from->sb_agcount); |
| 371 | to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks); |
| 372 | to->sb_logblocks = be32_to_cpu(from->sb_logblocks); |
| 373 | to->sb_versionnum = be16_to_cpu(from->sb_versionnum); |
| 374 | to->sb_sectsize = be16_to_cpu(from->sb_sectsize); |
| 375 | to->sb_inodesize = be16_to_cpu(from->sb_inodesize); |
| 376 | to->sb_inopblock = be16_to_cpu(from->sb_inopblock); |
| 377 | memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); |
| 378 | to->sb_blocklog = from->sb_blocklog; |
| 379 | to->sb_sectlog = from->sb_sectlog; |
| 380 | to->sb_inodelog = from->sb_inodelog; |
| 381 | to->sb_inopblog = from->sb_inopblog; |
| 382 | to->sb_agblklog = from->sb_agblklog; |
| 383 | to->sb_rextslog = from->sb_rextslog; |
| 384 | to->sb_inprogress = from->sb_inprogress; |
| 385 | to->sb_imax_pct = from->sb_imax_pct; |
| 386 | to->sb_icount = be64_to_cpu(from->sb_icount); |
| 387 | to->sb_ifree = be64_to_cpu(from->sb_ifree); |
| 388 | to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks); |
| 389 | to->sb_frextents = be64_to_cpu(from->sb_frextents); |
| 390 | to->sb_uquotino = be64_to_cpu(from->sb_uquotino); |
| 391 | to->sb_gquotino = be64_to_cpu(from->sb_gquotino); |
| 392 | to->sb_qflags = be16_to_cpu(from->sb_qflags); |
| 393 | to->sb_flags = from->sb_flags; |
| 394 | to->sb_shared_vn = from->sb_shared_vn; |
| 395 | to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt); |
| 396 | to->sb_unit = be32_to_cpu(from->sb_unit); |
| 397 | to->sb_width = be32_to_cpu(from->sb_width); |
| 398 | to->sb_dirblklog = from->sb_dirblklog; |
| 399 | to->sb_logsectlog = from->sb_logsectlog; |
| 400 | to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize); |
| 401 | to->sb_logsunit = be32_to_cpu(from->sb_logsunit); |
| 402 | to->sb_features2 = be32_to_cpu(from->sb_features2); |
| 403 | to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2); |
| 404 | to->sb_features_compat = be32_to_cpu(from->sb_features_compat); |
| 405 | to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat); |
| 406 | to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat); |
| 407 | to->sb_features_log_incompat = |
| 408 | be32_to_cpu(from->sb_features_log_incompat); |
Eric Sandeen | 04dd1a0 | 2014-10-02 09:24:11 +1000 | [diff] [blame] | 409 | /* crc is only used on disk, not in memory; just init to 0 here. */ |
| 410 | to->sb_crc = 0; |
Brian Foster | fb4f2b4 | 2015-05-29 08:54:03 +1000 | [diff] [blame] | 411 | to->sb_spino_align = be32_to_cpu(from->sb_spino_align); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 412 | to->sb_pquotino = be64_to_cpu(from->sb_pquotino); |
| 413 | to->sb_lsn = be64_to_cpu(from->sb_lsn); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 414 | /* |
| 415 | * sb_meta_uuid is only on disk if it differs from sb_uuid and the |
| 416 | * feature flag is set; if not set we keep it only in memory. |
| 417 | */ |
| 418 | if (xfs_sb_version_hasmetauuid(to)) |
| 419 | uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid); |
| 420 | else |
| 421 | uuid_copy(&to->sb_meta_uuid, &from->sb_uuid); |
Eric Sandeen | 5ef828c | 2014-08-04 11:35:44 +1000 | [diff] [blame] | 422 | /* Convert on-disk flags to in-memory flags? */ |
| 423 | if (convert_xquota) |
| 424 | xfs_sb_quota_from_disk(to); |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | xfs_sb_from_disk( |
| 429 | struct xfs_sb *to, |
| 430 | xfs_dsb_t *from) |
| 431 | { |
| 432 | __xfs_sb_from_disk(to, from, true); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 433 | } |
| 434 | |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 435 | static void |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 436 | xfs_sb_quota_to_disk( |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 437 | struct xfs_dsb *to, |
| 438 | struct xfs_sb *from) |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 439 | { |
| 440 | __uint16_t qflags = from->sb_qflags; |
| 441 | |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 442 | to->sb_uquotino = cpu_to_be64(from->sb_uquotino); |
| 443 | if (xfs_sb_version_has_pquotino(from)) { |
| 444 | to->sb_qflags = cpu_to_be16(from->sb_qflags); |
| 445 | to->sb_gquotino = cpu_to_be64(from->sb_gquotino); |
| 446 | to->sb_pquotino = cpu_to_be64(from->sb_pquotino); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 447 | return; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 451 | * The in-core version of sb_qflags do not have XFS_OQUOTA_* |
| 452 | * flags, whereas the on-disk version does. So, convert incore |
| 453 | * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags. |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 454 | */ |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 455 | qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD | |
| 456 | XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD); |
| 457 | |
| 458 | if (from->sb_qflags & |
| 459 | (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD)) |
| 460 | qflags |= XFS_OQUOTA_ENFD; |
| 461 | if (from->sb_qflags & |
| 462 | (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) |
| 463 | qflags |= XFS_OQUOTA_CHKD; |
| 464 | to->sb_qflags = cpu_to_be16(qflags); |
| 465 | |
| 466 | /* |
| 467 | * GQUOTINO and PQUOTINO cannot be used together in versions |
| 468 | * of superblock that do not have pquotino. from->sb_flags |
| 469 | * tells us which quota is active and should be copied to |
| 470 | * disk. If neither are active, we should NULL the inode. |
| 471 | * |
| 472 | * In all cases, the separate pquotino must remain 0 because it |
| 473 | * it beyond the "end" of the valid non-pquotino superblock. |
| 474 | */ |
| 475 | if (from->sb_qflags & XFS_GQUOTA_ACCT) |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 476 | to->sb_gquotino = cpu_to_be64(from->sb_gquotino); |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 477 | else if (from->sb_qflags & XFS_PQUOTA_ACCT) |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 478 | to->sb_gquotino = cpu_to_be64(from->sb_pquotino); |
Dave Chinner | 03e0134 | 2014-07-15 07:28:41 +1000 | [diff] [blame] | 479 | else { |
| 480 | /* |
| 481 | * We can't rely on just the fields being logged to tell us |
| 482 | * that it is safe to write NULLFSINO - we should only do that |
| 483 | * if quotas are not actually enabled. Hence only write |
| 484 | * NULLFSINO if both in-core quota inodes are NULL. |
| 485 | */ |
| 486 | if (from->sb_gquotino == NULLFSINO && |
| 487 | from->sb_pquotino == NULLFSINO) |
| 488 | to->sb_gquotino = cpu_to_be64(NULLFSINO); |
| 489 | } |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 490 | |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 491 | to->sb_pquotino = 0; |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 492 | } |
| 493 | |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 494 | void |
| 495 | xfs_sb_to_disk( |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 496 | struct xfs_dsb *to, |
| 497 | struct xfs_sb *from) |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 498 | { |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 499 | xfs_sb_quota_to_disk(to, from); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 500 | |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 501 | to->sb_magicnum = cpu_to_be32(from->sb_magicnum); |
| 502 | to->sb_blocksize = cpu_to_be32(from->sb_blocksize); |
| 503 | to->sb_dblocks = cpu_to_be64(from->sb_dblocks); |
| 504 | to->sb_rblocks = cpu_to_be64(from->sb_rblocks); |
| 505 | to->sb_rextents = cpu_to_be64(from->sb_rextents); |
| 506 | memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); |
| 507 | to->sb_logstart = cpu_to_be64(from->sb_logstart); |
| 508 | to->sb_rootino = cpu_to_be64(from->sb_rootino); |
| 509 | to->sb_rbmino = cpu_to_be64(from->sb_rbmino); |
| 510 | to->sb_rsumino = cpu_to_be64(from->sb_rsumino); |
| 511 | to->sb_rextsize = cpu_to_be32(from->sb_rextsize); |
| 512 | to->sb_agblocks = cpu_to_be32(from->sb_agblocks); |
| 513 | to->sb_agcount = cpu_to_be32(from->sb_agcount); |
| 514 | to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks); |
| 515 | to->sb_logblocks = cpu_to_be32(from->sb_logblocks); |
| 516 | to->sb_versionnum = cpu_to_be16(from->sb_versionnum); |
| 517 | to->sb_sectsize = cpu_to_be16(from->sb_sectsize); |
| 518 | to->sb_inodesize = cpu_to_be16(from->sb_inodesize); |
| 519 | to->sb_inopblock = cpu_to_be16(from->sb_inopblock); |
| 520 | memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); |
| 521 | to->sb_blocklog = from->sb_blocklog; |
| 522 | to->sb_sectlog = from->sb_sectlog; |
| 523 | to->sb_inodelog = from->sb_inodelog; |
| 524 | to->sb_inopblog = from->sb_inopblog; |
| 525 | to->sb_agblklog = from->sb_agblklog; |
| 526 | to->sb_rextslog = from->sb_rextslog; |
| 527 | to->sb_inprogress = from->sb_inprogress; |
| 528 | to->sb_imax_pct = from->sb_imax_pct; |
| 529 | to->sb_icount = cpu_to_be64(from->sb_icount); |
| 530 | to->sb_ifree = cpu_to_be64(from->sb_ifree); |
| 531 | to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks); |
| 532 | to->sb_frextents = cpu_to_be64(from->sb_frextents); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 533 | |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 534 | to->sb_flags = from->sb_flags; |
| 535 | to->sb_shared_vn = from->sb_shared_vn; |
| 536 | to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt); |
| 537 | to->sb_unit = cpu_to_be32(from->sb_unit); |
| 538 | to->sb_width = cpu_to_be32(from->sb_width); |
| 539 | to->sb_dirblklog = from->sb_dirblklog; |
| 540 | to->sb_logsectlog = from->sb_logsectlog; |
| 541 | to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize); |
| 542 | to->sb_logsunit = cpu_to_be32(from->sb_logsunit); |
Dave Chinner | 074e427 | 2015-01-22 09:10:33 +1100 | [diff] [blame] | 543 | |
| 544 | /* |
| 545 | * We need to ensure that bad_features2 always matches features2. |
| 546 | * Hence we enforce that here rather than having to remember to do it |
| 547 | * everywhere else that updates features2. |
| 548 | */ |
| 549 | from->sb_bad_features2 = from->sb_features2; |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 550 | to->sb_features2 = cpu_to_be32(from->sb_features2); |
| 551 | to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 552 | |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 553 | if (xfs_sb_version_hascrc(from)) { |
| 554 | to->sb_features_compat = cpu_to_be32(from->sb_features_compat); |
| 555 | to->sb_features_ro_compat = |
| 556 | cpu_to_be32(from->sb_features_ro_compat); |
| 557 | to->sb_features_incompat = |
| 558 | cpu_to_be32(from->sb_features_incompat); |
| 559 | to->sb_features_log_incompat = |
| 560 | cpu_to_be32(from->sb_features_log_incompat); |
Brian Foster | fb4f2b4 | 2015-05-29 08:54:03 +1000 | [diff] [blame] | 561 | to->sb_spino_align = cpu_to_be32(from->sb_spino_align); |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 562 | to->sb_lsn = cpu_to_be64(from->sb_lsn); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 563 | if (xfs_sb_version_hasmetauuid(from)) |
| 564 | uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | static int |
| 569 | xfs_sb_verify( |
| 570 | struct xfs_buf *bp, |
| 571 | bool check_version) |
| 572 | { |
| 573 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 574 | struct xfs_sb sb; |
| 575 | |
Eric Sandeen | 5ef828c | 2014-08-04 11:35:44 +1000 | [diff] [blame] | 576 | /* |
| 577 | * Use call variant which doesn't convert quota flags from disk |
| 578 | * format, because xfs_mount_validate_sb checks the on-disk flags. |
| 579 | */ |
| 580 | __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 581 | |
| 582 | /* |
| 583 | * Only check the in progress field for the primary superblock as |
| 584 | * mkfs.xfs doesn't clear it from secondary superblocks. |
| 585 | */ |
| 586 | return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR, |
| 587 | check_version); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * If the superblock has the CRC feature bit set or the CRC field is non-null, |
| 592 | * check that the CRC is valid. We check the CRC field is non-null because a |
| 593 | * single bit error could clear the feature bit and unused parts of the |
| 594 | * superblock are supposed to be zero. Hence a non-null crc field indicates that |
| 595 | * we've potentially lost a feature bit and we should check it anyway. |
Eric Sandeen | 10e6e65 | 2013-09-09 15:33:29 -0500 | [diff] [blame] | 596 | * |
| 597 | * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the |
| 598 | * last field in V4 secondary superblocks. So for secondary superblocks, |
| 599 | * we are more forgiving, and ignore CRC failures if the primary doesn't |
| 600 | * indicate that the fs version is V5. |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 601 | */ |
| 602 | static void |
| 603 | xfs_sb_read_verify( |
| 604 | struct xfs_buf *bp) |
| 605 | { |
| 606 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 607 | struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp); |
| 608 | int error; |
| 609 | |
| 610 | /* |
| 611 | * open code the version check to avoid needing to convert the entire |
| 612 | * superblock from disk order just to check the version number |
| 613 | */ |
| 614 | if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) && |
| 615 | (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) == |
| 616 | XFS_SB_VERSION_5) || |
| 617 | dsb->sb_crc != 0)) { |
| 618 | |
Eric Sandeen | 5158217 | 2014-02-27 15:17:27 +1100 | [diff] [blame] | 619 | if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) { |
Eric Sandeen | 10e6e65 | 2013-09-09 15:33:29 -0500 | [diff] [blame] | 620 | /* Only fail bad secondaries on a known V5 filesystem */ |
Eric Sandeen | 7a01e707 | 2014-02-19 15:33:05 +1100 | [diff] [blame] | 621 | if (bp->b_bn == XFS_SB_DADDR || |
Eric Sandeen | 10e6e65 | 2013-09-09 15:33:29 -0500 | [diff] [blame] | 622 | xfs_sb_version_hascrc(&mp->m_sb)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 623 | error = -EFSBADCRC; |
Eric Sandeen | 10e6e65 | 2013-09-09 15:33:29 -0500 | [diff] [blame] | 624 | goto out_error; |
| 625 | } |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | error = xfs_sb_verify(bp, true); |
| 629 | |
| 630 | out_error: |
| 631 | if (error) { |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 632 | xfs_buf_ioerror(bp, error); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 633 | if (error == -EFSCORRUPTED || error == -EFSBADCRC) |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 634 | xfs_verifier_error(bp); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * We may be probed for a filesystem match, so we may not want to emit |
| 640 | * messages when the superblock buffer is not actually an XFS superblock. |
Zhi Yong Wu | 2533787 | 2013-08-12 03:15:02 +0000 | [diff] [blame] | 641 | * If we find an XFS superblock, then run a normal, noisy mount because we are |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 642 | * really going to mount it and want to know about errors. |
| 643 | */ |
| 644 | static void |
| 645 | xfs_sb_quiet_read_verify( |
| 646 | struct xfs_buf *bp) |
| 647 | { |
| 648 | struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp); |
| 649 | |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 650 | if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) { |
| 651 | /* XFS filesystem, verify noisily! */ |
| 652 | xfs_sb_read_verify(bp); |
| 653 | return; |
| 654 | } |
| 655 | /* quietly fail */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 656 | xfs_buf_ioerror(bp, -EWRONGFS); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static void |
| 660 | xfs_sb_write_verify( |
| 661 | struct xfs_buf *bp) |
| 662 | { |
| 663 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 664 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 665 | int error; |
| 666 | |
| 667 | error = xfs_sb_verify(bp, false); |
| 668 | if (error) { |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 669 | xfs_buf_ioerror(bp, error); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 670 | xfs_verifier_error(bp); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 671 | return; |
| 672 | } |
| 673 | |
| 674 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 675 | return; |
| 676 | |
| 677 | if (bip) |
| 678 | XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 679 | |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 680 | xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | const struct xfs_buf_ops xfs_sb_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 684 | .name = "xfs_sb", |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 685 | .verify_read = xfs_sb_read_verify, |
| 686 | .verify_write = xfs_sb_write_verify, |
| 687 | }; |
| 688 | |
| 689 | const struct xfs_buf_ops xfs_sb_quiet_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 690 | .name = "xfs_sb_quiet", |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 691 | .verify_read = xfs_sb_quiet_read_verify, |
| 692 | .verify_write = xfs_sb_write_verify, |
| 693 | }; |
| 694 | |
| 695 | /* |
| 696 | * xfs_mount_common |
| 697 | * |
| 698 | * Mount initialization code establishing various mount |
| 699 | * fields from the superblock associated with the given |
| 700 | * mount structure |
| 701 | */ |
| 702 | void |
| 703 | xfs_sb_mount_common( |
| 704 | struct xfs_mount *mp, |
| 705 | struct xfs_sb *sbp) |
| 706 | { |
| 707 | mp->m_agfrotor = mp->m_agirotor = 0; |
| 708 | spin_lock_init(&mp->m_agirotor_lock); |
| 709 | mp->m_maxagi = mp->m_sb.sb_agcount; |
| 710 | mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; |
| 711 | mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; |
| 712 | mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; |
| 713 | mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; |
| 714 | mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog; |
| 715 | mp->m_blockmask = sbp->sb_blocksize - 1; |
| 716 | mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; |
| 717 | mp->m_blockwmask = mp->m_blockwsize - 1; |
| 718 | |
| 719 | mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 720 | mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 721 | mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2; |
| 722 | mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2; |
| 723 | |
| 724 | mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 725 | mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 726 | mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2; |
| 727 | mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2; |
| 728 | |
| 729 | mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 730 | mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 731 | mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2; |
| 732 | mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2; |
| 733 | |
Darrick J. Wong | 035e00a | 2016-08-03 11:36:07 +1000 | [diff] [blame] | 734 | mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 735 | mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 736 | mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2; |
| 737 | mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2; |
| 738 | |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 739 | mp->m_bsize = XFS_FSB_TO_BB(mp, 1); |
| 740 | mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK, |
| 741 | sbp->sb_inopblock); |
| 742 | mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog; |
Brian Foster | 066a188 | 2015-05-29 08:55:20 +1000 | [diff] [blame] | 743 | |
| 744 | if (sbp->sb_spino_align) |
| 745 | mp->m_ialloc_min_blks = sbp->sb_spino_align; |
| 746 | else |
| 747 | mp->m_ialloc_min_blks = mp->m_ialloc_blks; |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 748 | mp->m_alloc_set_aside = xfs_alloc_set_aside(mp); |
| 749 | mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* |
| 753 | * xfs_initialize_perag_data |
| 754 | * |
| 755 | * Read in each per-ag structure so we can count up the number of |
| 756 | * allocated inodes, free inodes and used filesystem blocks as this |
| 757 | * information is no longer persistent in the superblock. Once we have |
| 758 | * this information, write it into the in-core superblock structure. |
| 759 | */ |
| 760 | int |
| 761 | xfs_initialize_perag_data( |
| 762 | struct xfs_mount *mp, |
| 763 | xfs_agnumber_t agcount) |
| 764 | { |
| 765 | xfs_agnumber_t index; |
| 766 | xfs_perag_t *pag; |
| 767 | xfs_sb_t *sbp = &mp->m_sb; |
| 768 | uint64_t ifree = 0; |
| 769 | uint64_t ialloc = 0; |
| 770 | uint64_t bfree = 0; |
| 771 | uint64_t bfreelst = 0; |
| 772 | uint64_t btree = 0; |
| 773 | int error; |
| 774 | |
| 775 | for (index = 0; index < agcount; index++) { |
| 776 | /* |
| 777 | * read the agf, then the agi. This gets us |
| 778 | * all the information we need and populates the |
| 779 | * per-ag structures for us. |
| 780 | */ |
| 781 | error = xfs_alloc_pagf_init(mp, NULL, index, 0); |
| 782 | if (error) |
| 783 | return error; |
| 784 | |
| 785 | error = xfs_ialloc_pagi_init(mp, NULL, index); |
| 786 | if (error) |
| 787 | return error; |
| 788 | pag = xfs_perag_get(mp, index); |
| 789 | ifree += pag->pagi_freecount; |
| 790 | ialloc += pag->pagi_count; |
| 791 | bfree += pag->pagf_freeblks; |
| 792 | bfreelst += pag->pagf_flcount; |
| 793 | btree += pag->pagf_btreeblks; |
| 794 | xfs_perag_put(pag); |
| 795 | } |
Dave Chinner | 5681ca4 | 2015-02-23 21:22:31 +1100 | [diff] [blame] | 796 | |
| 797 | /* Overwrite incore superblock counters with just-read data */ |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 798 | spin_lock(&mp->m_sb_lock); |
| 799 | sbp->sb_ifree = ifree; |
| 800 | sbp->sb_icount = ialloc; |
| 801 | sbp->sb_fdblocks = bfree + bfreelst + btree; |
| 802 | spin_unlock(&mp->m_sb_lock); |
| 803 | |
Dave Chinner | 5681ca4 | 2015-02-23 21:22:31 +1100 | [diff] [blame] | 804 | xfs_reinit_percpu_counters(mp); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | /* |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 810 | * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock |
| 811 | * into the superblock buffer to be logged. It does not provide the higher |
| 812 | * level of locking that is needed to protect the in-core superblock from |
| 813 | * concurrent access. |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 814 | */ |
| 815 | void |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 816 | xfs_log_sb( |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 817 | struct xfs_trans *tp) |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 818 | { |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 819 | struct xfs_mount *mp = tp->t_mountp; |
| 820 | struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 821 | |
Dave Chinner | 501ab32 | 2015-02-23 21:19:28 +1100 | [diff] [blame] | 822 | mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount); |
Dave Chinner | e88b64e | 2015-02-23 21:19:53 +1100 | [diff] [blame] | 823 | mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 824 | mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks); |
Dave Chinner | 501ab32 | 2015-02-23 21:19:28 +1100 | [diff] [blame] | 825 | |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 826 | xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 827 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 828 | xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb)); |
Dave Chinner | ff55068 | 2013-08-12 20:49:41 +1000 | [diff] [blame] | 829 | } |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 830 | |
| 831 | /* |
| 832 | * xfs_sync_sb |
| 833 | * |
| 834 | * Sync the superblock to disk. |
| 835 | * |
| 836 | * Note that the caller is responsible for checking the frozen state of the |
| 837 | * filesystem. This procedure uses the non-blocking transaction allocator and |
| 838 | * thus will allow modifications to a frozen fs. This is required because this |
| 839 | * code can be called during the process of freezing where use of the high-level |
| 840 | * allocator would deadlock. |
| 841 | */ |
| 842 | int |
| 843 | xfs_sync_sb( |
| 844 | struct xfs_mount *mp, |
| 845 | bool wait) |
| 846 | { |
| 847 | struct xfs_trans *tp; |
| 848 | int error; |
| 849 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 850 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, |
| 851 | XFS_TRANS_NO_WRITECOUNT, &tp); |
| 852 | if (error) |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 853 | return error; |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 854 | |
| 855 | xfs_log_sb(tp); |
| 856 | if (wait) |
| 857 | xfs_trans_set_sync(tp); |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 858 | return xfs_trans_commit(tp); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 859 | } |