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