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