1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_inode.h"
17 #include "xfs_ialloc.h"
18 #include "xfs_alloc.h"
19 #include "xfs_trace.h"
20 #include "xfs_cksum.h"
21 #include "xfs_trans.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_alloc_btree.h"
24 #include "xfs_ialloc_btree.h"
25 #include "xfs_rmap_btree.h"
27 #include "xfs_refcount_btree.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
32 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
36 * Reference counting access wrappers to the perag structures.
37 * Because we never free per-ag structures, the only thing we
38 * have to protect against changes is the tree structure itself.
45 struct xfs_perag
*pag
;
49 pag
= radix_tree_lookup(&mp
->m_perag_tree
, agno
);
51 ASSERT(atomic_read(&pag
->pag_ref
) >= 0);
52 ref
= atomic_inc_return(&pag
->pag_ref
);
55 trace_xfs_perag_get(mp
, agno
, ref
, _RET_IP_
);
60 * search from @first to find the next perag with the given tag set.
68 struct xfs_perag
*pag
;
73 found
= radix_tree_gang_lookup_tag(&mp
->m_perag_tree
,
74 (void **)&pag
, first
, 1, tag
);
79 ref
= atomic_inc_return(&pag
->pag_ref
);
81 trace_xfs_perag_get_tag(mp
, pag
->pag_agno
, ref
, _RET_IP_
);
87 struct xfs_perag
*pag
)
91 ASSERT(atomic_read(&pag
->pag_ref
) > 0);
92 ref
= atomic_dec_return(&pag
->pag_ref
);
93 trace_xfs_perag_put(pag
->pag_mount
, pag
->pag_agno
, ref
, _RET_IP_
);
97 * Check the validity of the SB found.
100 xfs_mount_validate_sb(
103 bool check_inprogress
,
106 uint32_t agcount
= 0;
109 if (sbp
->sb_magicnum
!= XFS_SB_MAGIC
) {
110 xfs_warn(mp
, "bad magic number");
115 if (!xfs_sb_good_version(sbp
)) {
116 xfs_warn(mp
, "bad version");
121 * Version 5 superblock feature mask validation. Reject combinations the
122 * kernel cannot support up front before checking anything else. For
123 * write validation, we don't need to check feature masks.
125 if (check_version
&& XFS_SB_VERSION_NUM(sbp
) == XFS_SB_VERSION_5
) {
126 if (xfs_sb_has_compat_feature(sbp
,
127 XFS_SB_FEAT_COMPAT_UNKNOWN
)) {
129 "Superblock has unknown compatible features (0x%x) enabled.",
130 (sbp
->sb_features_compat
&
131 XFS_SB_FEAT_COMPAT_UNKNOWN
));
133 "Using a more recent kernel is recommended.");
136 if (xfs_sb_has_ro_compat_feature(sbp
,
137 XFS_SB_FEAT_RO_COMPAT_UNKNOWN
)) {
139 "Superblock has unknown read-only compatible features (0x%x) enabled.",
140 (sbp
->sb_features_ro_compat
&
141 XFS_SB_FEAT_RO_COMPAT_UNKNOWN
));
142 if (!(mp
->m_flags
& XFS_MOUNT_RDONLY
)) {
144 "Attempted to mount read-only compatible filesystem read-write.");
146 "Filesystem can only be safely mounted read only.");
151 if (xfs_sb_has_incompat_feature(sbp
,
152 XFS_SB_FEAT_INCOMPAT_UNKNOWN
)) {
154 "Superblock has unknown incompatible features (0x%x) enabled.",
155 (sbp
->sb_features_incompat
&
156 XFS_SB_FEAT_INCOMPAT_UNKNOWN
));
158 "Filesystem can not be safely mounted by this kernel.");
161 } else if (xfs_sb_version_hascrc(sbp
)) {
163 * We can't read verify the sb LSN because the read verifier is
164 * called before the log is allocated and processed. We know the
165 * log is set up before write verifier (!check_version) calls,
166 * so just check it here.
168 if (!xfs_log_check_lsn(mp
, sbp
->sb_lsn
))
169 return -EFSCORRUPTED
;
172 if (xfs_sb_version_has_pquotino(sbp
)) {
173 if (sbp
->sb_qflags
& (XFS_OQUOTA_ENFD
| XFS_OQUOTA_CHKD
)) {
175 "Version 5 of Super block has XFS_OQUOTA bits.");
176 return -EFSCORRUPTED
;
178 } else if (sbp
->sb_qflags
& (XFS_PQUOTA_ENFD
| XFS_GQUOTA_ENFD
|
179 XFS_PQUOTA_CHKD
| XFS_GQUOTA_CHKD
)) {
181 "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
182 return -EFSCORRUPTED
;
186 * Full inode chunks must be aligned to inode chunk size when
187 * sparse inodes are enabled to support the sparse chunk
188 * allocation algorithm and prevent overlapping inode records.
190 if (xfs_sb_version_hassparseinodes(sbp
)) {
193 align
= XFS_INODES_PER_CHUNK
* sbp
->sb_inodesize
195 if (sbp
->sb_inoalignmt
!= align
) {
197 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
198 sbp
->sb_inoalignmt
, align
);
204 sbp
->sb_logstart
== 0 && mp
->m_logdev_targp
== mp
->m_ddev_targp
)) {
206 "filesystem is marked as having an external log; "
207 "specify logdev on the mount command line.");
212 sbp
->sb_logstart
!= 0 && mp
->m_logdev_targp
!= mp
->m_ddev_targp
)) {
214 "filesystem is marked as having an internal log; "
215 "do not specify logdev on the mount command line.");
219 /* Compute agcount for this number of dblocks and agblocks */
220 if (sbp
->sb_agblocks
) {
221 agcount
= div_u64_rem(sbp
->sb_dblocks
, sbp
->sb_agblocks
, &rem
);
227 * More sanity checking. Most of these were stolen directly from
231 sbp
->sb_agcount
<= 0 ||
232 sbp
->sb_sectsize
< XFS_MIN_SECTORSIZE
||
233 sbp
->sb_sectsize
> XFS_MAX_SECTORSIZE
||
234 sbp
->sb_sectlog
< XFS_MIN_SECTORSIZE_LOG
||
235 sbp
->sb_sectlog
> XFS_MAX_SECTORSIZE_LOG
||
236 sbp
->sb_sectsize
!= (1 << sbp
->sb_sectlog
) ||
237 sbp
->sb_blocksize
< XFS_MIN_BLOCKSIZE
||
238 sbp
->sb_blocksize
> XFS_MAX_BLOCKSIZE
||
239 sbp
->sb_blocklog
< XFS_MIN_BLOCKSIZE_LOG
||
240 sbp
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
||
241 sbp
->sb_blocksize
!= (1 << sbp
->sb_blocklog
) ||
242 sbp
->sb_dirblklog
+ sbp
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
||
243 sbp
->sb_inodesize
< XFS_DINODE_MIN_SIZE
||
244 sbp
->sb_inodesize
> XFS_DINODE_MAX_SIZE
||
245 sbp
->sb_inodelog
< XFS_DINODE_MIN_LOG
||
246 sbp
->sb_inodelog
> XFS_DINODE_MAX_LOG
||
247 sbp
->sb_inodesize
!= (1 << sbp
->sb_inodelog
) ||
248 sbp
->sb_logsunit
> XLOG_MAX_RECORD_BSIZE
||
249 sbp
->sb_inopblock
!= howmany(sbp
->sb_blocksize
,sbp
->sb_inodesize
) ||
250 XFS_FSB_TO_B(mp
, sbp
->sb_agblocks
) < XFS_MIN_AG_BYTES
||
251 XFS_FSB_TO_B(mp
, sbp
->sb_agblocks
) > XFS_MAX_AG_BYTES
||
252 sbp
->sb_agblklog
!= xfs_highbit32(sbp
->sb_agblocks
- 1) + 1 ||
253 agcount
== 0 || agcount
!= sbp
->sb_agcount
||
254 (sbp
->sb_blocklog
- sbp
->sb_inodelog
!= sbp
->sb_inopblog
) ||
255 (sbp
->sb_rextsize
* sbp
->sb_blocksize
> XFS_MAX_RTEXTSIZE
) ||
256 (sbp
->sb_rextsize
* sbp
->sb_blocksize
< XFS_MIN_RTEXTSIZE
) ||
257 (sbp
->sb_imax_pct
> 100 /* zero sb_imax_pct is valid */) ||
258 sbp
->sb_dblocks
== 0 ||
259 sbp
->sb_dblocks
> XFS_MAX_DBLOCKS(sbp
) ||
260 sbp
->sb_dblocks
< XFS_MIN_DBLOCKS(sbp
) ||
261 sbp
->sb_shared_vn
!= 0)) {
262 xfs_notice(mp
, "SB sanity check failed");
263 return -EFSCORRUPTED
;
267 if (!xfs_sb_version_hasdalign(sbp
) ||
268 sbp
->sb_unit
> sbp
->sb_width
||
269 (sbp
->sb_width
% sbp
->sb_unit
) != 0) {
270 xfs_notice(mp
, "SB stripe unit sanity check failed");
271 return -EFSCORRUPTED
;
273 } else if (xfs_sb_version_hasdalign(sbp
)) {
274 xfs_notice(mp
, "SB stripe alignment sanity check failed");
275 return -EFSCORRUPTED
;
276 } else if (sbp
->sb_width
) {
277 xfs_notice(mp
, "SB stripe width sanity check failed");
278 return -EFSCORRUPTED
;
282 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
283 sbp
->sb_blocksize
< XFS_MIN_CRC_BLOCKSIZE
) {
284 xfs_notice(mp
, "v5 SB sanity check failed");
285 return -EFSCORRUPTED
;
289 * Currently only very few inode sizes are supported.
291 switch (sbp
->sb_inodesize
) {
298 xfs_warn(mp
, "inode size of %d bytes not supported",
303 if (xfs_sb_validate_fsb_count(sbp
, sbp
->sb_dblocks
) ||
304 xfs_sb_validate_fsb_count(sbp
, sbp
->sb_rblocks
)) {
306 "file system too large to be mounted on this system.");
314 xfs_sb_quota_from_disk(struct xfs_sb
*sbp
)
317 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
318 * leads to in-core values having two different values for a quota
319 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
322 * Note that this change affect only the in-core values. These
323 * values are not written back to disk unless any quota information
324 * is written to the disk. Even in that case, sb_pquotino field is
325 * not written to disk unless the superblock supports pquotino.
327 if (sbp
->sb_uquotino
== 0)
328 sbp
->sb_uquotino
= NULLFSINO
;
329 if (sbp
->sb_gquotino
== 0)
330 sbp
->sb_gquotino
= NULLFSINO
;
331 if (sbp
->sb_pquotino
== 0)
332 sbp
->sb_pquotino
= NULLFSINO
;
335 * We need to do these manipilations only if we are working
336 * with an older version of on-disk superblock.
338 if (xfs_sb_version_has_pquotino(sbp
))
341 if (sbp
->sb_qflags
& XFS_OQUOTA_ENFD
)
342 sbp
->sb_qflags
|= (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
) ?
343 XFS_PQUOTA_ENFD
: XFS_GQUOTA_ENFD
;
344 if (sbp
->sb_qflags
& XFS_OQUOTA_CHKD
)
345 sbp
->sb_qflags
|= (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
) ?
346 XFS_PQUOTA_CHKD
: XFS_GQUOTA_CHKD
;
347 sbp
->sb_qflags
&= ~(XFS_OQUOTA_ENFD
| XFS_OQUOTA_CHKD
);
349 if (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
&&
350 sbp
->sb_gquotino
!= NULLFSINO
) {
352 * In older version of superblock, on-disk superblock only
353 * has sb_gquotino, and in-core superblock has both sb_gquotino
354 * and sb_pquotino. But, only one of them is supported at any
355 * point of time. So, if PQUOTA is set in disk superblock,
356 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
357 * above is to make sure we don't do this twice and wipe them
360 sbp
->sb_pquotino
= sbp
->sb_gquotino
;
361 sbp
->sb_gquotino
= NULLFSINO
;
371 to
->sb_magicnum
= be32_to_cpu(from
->sb_magicnum
);
372 to
->sb_blocksize
= be32_to_cpu(from
->sb_blocksize
);
373 to
->sb_dblocks
= be64_to_cpu(from
->sb_dblocks
);
374 to
->sb_rblocks
= be64_to_cpu(from
->sb_rblocks
);
375 to
->sb_rextents
= be64_to_cpu(from
->sb_rextents
);
376 memcpy(&to
->sb_uuid
, &from
->sb_uuid
, sizeof(to
->sb_uuid
));
377 to
->sb_logstart
= be64_to_cpu(from
->sb_logstart
);
378 to
->sb_rootino
= be64_to_cpu(from
->sb_rootino
);
379 to
->sb_rbmino
= be64_to_cpu(from
->sb_rbmino
);
380 to
->sb_rsumino
= be64_to_cpu(from
->sb_rsumino
);
381 to
->sb_rextsize
= be32_to_cpu(from
->sb_rextsize
);
382 to
->sb_agblocks
= be32_to_cpu(from
->sb_agblocks
);
383 to
->sb_agcount
= be32_to_cpu(from
->sb_agcount
);
384 to
->sb_rbmblocks
= be32_to_cpu(from
->sb_rbmblocks
);
385 to
->sb_logblocks
= be32_to_cpu(from
->sb_logblocks
);
386 to
->sb_versionnum
= be16_to_cpu(from
->sb_versionnum
);
387 to
->sb_sectsize
= be16_to_cpu(from
->sb_sectsize
);
388 to
->sb_inodesize
= be16_to_cpu(from
->sb_inodesize
);
389 to
->sb_inopblock
= be16_to_cpu(from
->sb_inopblock
);
390 memcpy(&to
->sb_fname
, &from
->sb_fname
, sizeof(to
->sb_fname
));
391 to
->sb_blocklog
= from
->sb_blocklog
;
392 to
->sb_sectlog
= from
->sb_sectlog
;
393 to
->sb_inodelog
= from
->sb_inodelog
;
394 to
->sb_inopblog
= from
->sb_inopblog
;
395 to
->sb_agblklog
= from
->sb_agblklog
;
396 to
->sb_rextslog
= from
->sb_rextslog
;
397 to
->sb_inprogress
= from
->sb_inprogress
;
398 to
->sb_imax_pct
= from
->sb_imax_pct
;
399 to
->sb_icount
= be64_to_cpu(from
->sb_icount
);
400 to
->sb_ifree
= be64_to_cpu(from
->sb_ifree
);
401 to
->sb_fdblocks
= be64_to_cpu(from
->sb_fdblocks
);
402 to
->sb_frextents
= be64_to_cpu(from
->sb_frextents
);
403 to
->sb_uquotino
= be64_to_cpu(from
->sb_uquotino
);
404 to
->sb_gquotino
= be64_to_cpu(from
->sb_gquotino
);
405 to
->sb_qflags
= be16_to_cpu(from
->sb_qflags
);
406 to
->sb_flags
= from
->sb_flags
;
407 to
->sb_shared_vn
= from
->sb_shared_vn
;
408 to
->sb_inoalignmt
= be32_to_cpu(from
->sb_inoalignmt
);
409 to
->sb_unit
= be32_to_cpu(from
->sb_unit
);
410 to
->sb_width
= be32_to_cpu(from
->sb_width
);
411 to
->sb_dirblklog
= from
->sb_dirblklog
;
412 to
->sb_logsectlog
= from
->sb_logsectlog
;
413 to
->sb_logsectsize
= be16_to_cpu(from
->sb_logsectsize
);
414 to
->sb_logsunit
= be32_to_cpu(from
->sb_logsunit
);
415 to
->sb_features2
= be32_to_cpu(from
->sb_features2
);
416 to
->sb_bad_features2
= be32_to_cpu(from
->sb_bad_features2
);
417 to
->sb_features_compat
= be32_to_cpu(from
->sb_features_compat
);
418 to
->sb_features_ro_compat
= be32_to_cpu(from
->sb_features_ro_compat
);
419 to
->sb_features_incompat
= be32_to_cpu(from
->sb_features_incompat
);
420 to
->sb_features_log_incompat
=
421 be32_to_cpu(from
->sb_features_log_incompat
);
422 /* crc is only used on disk, not in memory; just init to 0 here. */
424 to
->sb_spino_align
= be32_to_cpu(from
->sb_spino_align
);
425 to
->sb_pquotino
= be64_to_cpu(from
->sb_pquotino
);
426 to
->sb_lsn
= be64_to_cpu(from
->sb_lsn
);
428 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
429 * feature flag is set; if not set we keep it only in memory.
431 if (xfs_sb_version_hasmetauuid(to
))
432 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_meta_uuid
);
434 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_uuid
);
435 /* Convert on-disk flags to in-memory flags? */
437 xfs_sb_quota_from_disk(to
);
445 __xfs_sb_from_disk(to
, from
, true);
449 xfs_sb_quota_to_disk(
453 uint16_t qflags
= from
->sb_qflags
;
455 to
->sb_uquotino
= cpu_to_be64(from
->sb_uquotino
);
456 if (xfs_sb_version_has_pquotino(from
)) {
457 to
->sb_qflags
= cpu_to_be16(from
->sb_qflags
);
458 to
->sb_gquotino
= cpu_to_be64(from
->sb_gquotino
);
459 to
->sb_pquotino
= cpu_to_be64(from
->sb_pquotino
);
464 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
465 * flags, whereas the on-disk version does. So, convert incore
466 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
468 qflags
&= ~(XFS_PQUOTA_ENFD
| XFS_PQUOTA_CHKD
|
469 XFS_GQUOTA_ENFD
| XFS_GQUOTA_CHKD
);
471 if (from
->sb_qflags
&
472 (XFS_PQUOTA_ENFD
| XFS_GQUOTA_ENFD
))
473 qflags
|= XFS_OQUOTA_ENFD
;
474 if (from
->sb_qflags
&
475 (XFS_PQUOTA_CHKD
| XFS_GQUOTA_CHKD
))
476 qflags
|= XFS_OQUOTA_CHKD
;
477 to
->sb_qflags
= cpu_to_be16(qflags
);
480 * GQUOTINO and PQUOTINO cannot be used together in versions
481 * of superblock that do not have pquotino. from->sb_flags
482 * tells us which quota is active and should be copied to
483 * disk. If neither are active, we should NULL the inode.
485 * In all cases, the separate pquotino must remain 0 because it
486 * it beyond the "end" of the valid non-pquotino superblock.
488 if (from
->sb_qflags
& XFS_GQUOTA_ACCT
)
489 to
->sb_gquotino
= cpu_to_be64(from
->sb_gquotino
);
490 else if (from
->sb_qflags
& XFS_PQUOTA_ACCT
)
491 to
->sb_gquotino
= cpu_to_be64(from
->sb_pquotino
);
494 * We can't rely on just the fields being logged to tell us
495 * that it is safe to write NULLFSINO - we should only do that
496 * if quotas are not actually enabled. Hence only write
497 * NULLFSINO if both in-core quota inodes are NULL.
499 if (from
->sb_gquotino
== NULLFSINO
&&
500 from
->sb_pquotino
== NULLFSINO
)
501 to
->sb_gquotino
= cpu_to_be64(NULLFSINO
);
512 xfs_sb_quota_to_disk(to
, from
);
514 to
->sb_magicnum
= cpu_to_be32(from
->sb_magicnum
);
515 to
->sb_blocksize
= cpu_to_be32(from
->sb_blocksize
);
516 to
->sb_dblocks
= cpu_to_be64(from
->sb_dblocks
);
517 to
->sb_rblocks
= cpu_to_be64(from
->sb_rblocks
);
518 to
->sb_rextents
= cpu_to_be64(from
->sb_rextents
);
519 memcpy(&to
->sb_uuid
, &from
->sb_uuid
, sizeof(to
->sb_uuid
));
520 to
->sb_logstart
= cpu_to_be64(from
->sb_logstart
);
521 to
->sb_rootino
= cpu_to_be64(from
->sb_rootino
);
522 to
->sb_rbmino
= cpu_to_be64(from
->sb_rbmino
);
523 to
->sb_rsumino
= cpu_to_be64(from
->sb_rsumino
);
524 to
->sb_rextsize
= cpu_to_be32(from
->sb_rextsize
);
525 to
->sb_agblocks
= cpu_to_be32(from
->sb_agblocks
);
526 to
->sb_agcount
= cpu_to_be32(from
->sb_agcount
);
527 to
->sb_rbmblocks
= cpu_to_be32(from
->sb_rbmblocks
);
528 to
->sb_logblocks
= cpu_to_be32(from
->sb_logblocks
);
529 to
->sb_versionnum
= cpu_to_be16(from
->sb_versionnum
);
530 to
->sb_sectsize
= cpu_to_be16(from
->sb_sectsize
);
531 to
->sb_inodesize
= cpu_to_be16(from
->sb_inodesize
);
532 to
->sb_inopblock
= cpu_to_be16(from
->sb_inopblock
);
533 memcpy(&to
->sb_fname
, &from
->sb_fname
, sizeof(to
->sb_fname
));
534 to
->sb_blocklog
= from
->sb_blocklog
;
535 to
->sb_sectlog
= from
->sb_sectlog
;
536 to
->sb_inodelog
= from
->sb_inodelog
;
537 to
->sb_inopblog
= from
->sb_inopblog
;
538 to
->sb_agblklog
= from
->sb_agblklog
;
539 to
->sb_rextslog
= from
->sb_rextslog
;
540 to
->sb_inprogress
= from
->sb_inprogress
;
541 to
->sb_imax_pct
= from
->sb_imax_pct
;
542 to
->sb_icount
= cpu_to_be64(from
->sb_icount
);
543 to
->sb_ifree
= cpu_to_be64(from
->sb_ifree
);
544 to
->sb_fdblocks
= cpu_to_be64(from
->sb_fdblocks
);
545 to
->sb_frextents
= cpu_to_be64(from
->sb_frextents
);
547 to
->sb_flags
= from
->sb_flags
;
548 to
->sb_shared_vn
= from
->sb_shared_vn
;
549 to
->sb_inoalignmt
= cpu_to_be32(from
->sb_inoalignmt
);
550 to
->sb_unit
= cpu_to_be32(from
->sb_unit
);
551 to
->sb_width
= cpu_to_be32(from
->sb_width
);
552 to
->sb_dirblklog
= from
->sb_dirblklog
;
553 to
->sb_logsectlog
= from
->sb_logsectlog
;
554 to
->sb_logsectsize
= cpu_to_be16(from
->sb_logsectsize
);
555 to
->sb_logsunit
= cpu_to_be32(from
->sb_logsunit
);
558 * We need to ensure that bad_features2 always matches features2.
559 * Hence we enforce that here rather than having to remember to do it
560 * everywhere else that updates features2.
562 from
->sb_bad_features2
= from
->sb_features2
;
563 to
->sb_features2
= cpu_to_be32(from
->sb_features2
);
564 to
->sb_bad_features2
= cpu_to_be32(from
->sb_bad_features2
);
566 if (xfs_sb_version_hascrc(from
)) {
567 to
->sb_features_compat
= cpu_to_be32(from
->sb_features_compat
);
568 to
->sb_features_ro_compat
=
569 cpu_to_be32(from
->sb_features_ro_compat
);
570 to
->sb_features_incompat
=
571 cpu_to_be32(from
->sb_features_incompat
);
572 to
->sb_features_log_incompat
=
573 cpu_to_be32(from
->sb_features_log_incompat
);
574 to
->sb_spino_align
= cpu_to_be32(from
->sb_spino_align
);
575 to
->sb_lsn
= cpu_to_be64(from
->sb_lsn
);
576 if (xfs_sb_version_hasmetauuid(from
))
577 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_meta_uuid
);
586 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
590 * Use call variant which doesn't convert quota flags from disk
591 * format, because xfs_mount_validate_sb checks the on-disk flags.
593 __xfs_sb_from_disk(&sb
, XFS_BUF_TO_SBP(bp
), false);
596 * Only check the in progress field for the primary superblock as
597 * mkfs.xfs doesn't clear it from secondary superblocks.
599 return xfs_mount_validate_sb(mp
, &sb
,
600 bp
->b_maps
[0].bm_bn
== XFS_SB_DADDR
,
605 * If the superblock has the CRC feature bit set or the CRC field is non-null,
606 * check that the CRC is valid. We check the CRC field is non-null because a
607 * single bit error could clear the feature bit and unused parts of the
608 * superblock are supposed to be zero. Hence a non-null crc field indicates that
609 * we've potentially lost a feature bit and we should check it anyway.
611 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
612 * last field in V4 secondary superblocks. So for secondary superblocks,
613 * we are more forgiving, and ignore CRC failures if the primary doesn't
614 * indicate that the fs version is V5.
620 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
621 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
625 * open code the version check to avoid needing to convert the entire
626 * superblock from disk order just to check the version number
628 if (dsb
->sb_magicnum
== cpu_to_be32(XFS_SB_MAGIC
) &&
629 (((be16_to_cpu(dsb
->sb_versionnum
) & XFS_SB_VERSION_NUMBITS
) ==
633 if (!xfs_buf_verify_cksum(bp
, XFS_SB_CRC_OFF
)) {
634 /* Only fail bad secondaries on a known V5 filesystem */
635 if (bp
->b_bn
== XFS_SB_DADDR
||
636 xfs_sb_version_hascrc(&mp
->m_sb
)) {
642 error
= xfs_sb_verify(bp
, true);
645 if (error
== -EFSCORRUPTED
|| error
== -EFSBADCRC
)
646 xfs_verifier_error(bp
, error
, __this_address
);
648 xfs_buf_ioerror(bp
, error
);
652 * We may be probed for a filesystem match, so we may not want to emit
653 * messages when the superblock buffer is not actually an XFS superblock.
654 * If we find an XFS superblock, then run a normal, noisy mount because we are
655 * really going to mount it and want to know about errors.
658 xfs_sb_quiet_read_verify(
661 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
663 if (dsb
->sb_magicnum
== cpu_to_be32(XFS_SB_MAGIC
)) {
664 /* XFS filesystem, verify noisily! */
665 xfs_sb_read_verify(bp
);
669 xfs_buf_ioerror(bp
, -EWRONGFS
);
676 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
677 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
680 error
= xfs_sb_verify(bp
, false);
682 xfs_verifier_error(bp
, error
, __this_address
);
686 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
690 XFS_BUF_TO_SBP(bp
)->sb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
692 xfs_buf_update_cksum(bp
, XFS_SB_CRC_OFF
);
695 const struct xfs_buf_ops xfs_sb_buf_ops
= {
697 .verify_read
= xfs_sb_read_verify
,
698 .verify_write
= xfs_sb_write_verify
,
701 const struct xfs_buf_ops xfs_sb_quiet_buf_ops
= {
702 .name
= "xfs_sb_quiet",
703 .verify_read
= xfs_sb_quiet_read_verify
,
704 .verify_write
= xfs_sb_write_verify
,
710 * Mount initialization code establishing various mount
711 * fields from the superblock associated with the given
716 struct xfs_mount
*mp
,
719 mp
->m_agfrotor
= mp
->m_agirotor
= 0;
720 mp
->m_maxagi
= mp
->m_sb
.sb_agcount
;
721 mp
->m_blkbit_log
= sbp
->sb_blocklog
+ XFS_NBBYLOG
;
722 mp
->m_blkbb_log
= sbp
->sb_blocklog
- BBSHIFT
;
723 mp
->m_sectbb_log
= sbp
->sb_sectlog
- BBSHIFT
;
724 mp
->m_agno_log
= xfs_highbit32(sbp
->sb_agcount
- 1) + 1;
725 mp
->m_agino_log
= sbp
->sb_inopblog
+ sbp
->sb_agblklog
;
726 mp
->m_blockmask
= sbp
->sb_blocksize
- 1;
727 mp
->m_blockwsize
= sbp
->sb_blocksize
>> XFS_WORDLOG
;
728 mp
->m_blockwmask
= mp
->m_blockwsize
- 1;
730 mp
->m_alloc_mxr
[0] = xfs_allocbt_maxrecs(mp
, sbp
->sb_blocksize
, 1);
731 mp
->m_alloc_mxr
[1] = xfs_allocbt_maxrecs(mp
, sbp
->sb_blocksize
, 0);
732 mp
->m_alloc_mnr
[0] = mp
->m_alloc_mxr
[0] / 2;
733 mp
->m_alloc_mnr
[1] = mp
->m_alloc_mxr
[1] / 2;
735 mp
->m_inobt_mxr
[0] = xfs_inobt_maxrecs(mp
, sbp
->sb_blocksize
, 1);
736 mp
->m_inobt_mxr
[1] = xfs_inobt_maxrecs(mp
, sbp
->sb_blocksize
, 0);
737 mp
->m_inobt_mnr
[0] = mp
->m_inobt_mxr
[0] / 2;
738 mp
->m_inobt_mnr
[1] = mp
->m_inobt_mxr
[1] / 2;
740 mp
->m_bmap_dmxr
[0] = xfs_bmbt_maxrecs(mp
, sbp
->sb_blocksize
, 1);
741 mp
->m_bmap_dmxr
[1] = xfs_bmbt_maxrecs(mp
, sbp
->sb_blocksize
, 0);
742 mp
->m_bmap_dmnr
[0] = mp
->m_bmap_dmxr
[0] / 2;
743 mp
->m_bmap_dmnr
[1] = mp
->m_bmap_dmxr
[1] / 2;
745 mp
->m_rmap_mxr
[0] = xfs_rmapbt_maxrecs(sbp
->sb_blocksize
, 1);
746 mp
->m_rmap_mxr
[1] = xfs_rmapbt_maxrecs(sbp
->sb_blocksize
, 0);
747 mp
->m_rmap_mnr
[0] = mp
->m_rmap_mxr
[0] / 2;
748 mp
->m_rmap_mnr
[1] = mp
->m_rmap_mxr
[1] / 2;
750 mp
->m_refc_mxr
[0] = xfs_refcountbt_maxrecs(sbp
->sb_blocksize
, true);
751 mp
->m_refc_mxr
[1] = xfs_refcountbt_maxrecs(sbp
->sb_blocksize
, false);
752 mp
->m_refc_mnr
[0] = mp
->m_refc_mxr
[0] / 2;
753 mp
->m_refc_mnr
[1] = mp
->m_refc_mxr
[1] / 2;
755 mp
->m_bsize
= XFS_FSB_TO_BB(mp
, 1);
756 mp
->m_ialloc_inos
= max_t(uint16_t, XFS_INODES_PER_CHUNK
,
758 mp
->m_ialloc_blks
= mp
->m_ialloc_inos
>> sbp
->sb_inopblog
;
760 if (sbp
->sb_spino_align
)
761 mp
->m_ialloc_min_blks
= sbp
->sb_spino_align
;
763 mp
->m_ialloc_min_blks
= mp
->m_ialloc_blks
;
764 mp
->m_alloc_set_aside
= xfs_alloc_set_aside(mp
);
765 mp
->m_ag_max_usable
= xfs_alloc_ag_max_usable(mp
);
769 * xfs_initialize_perag_data
771 * Read in each per-ag structure so we can count up the number of
772 * allocated inodes, free inodes and used filesystem blocks as this
773 * information is no longer persistent in the superblock. Once we have
774 * this information, write it into the in-core superblock structure.
777 xfs_initialize_perag_data(
778 struct xfs_mount
*mp
,
779 xfs_agnumber_t agcount
)
781 xfs_agnumber_t index
;
783 xfs_sb_t
*sbp
= &mp
->m_sb
;
787 uint64_t bfreelst
= 0;
792 for (index
= 0; index
< agcount
; index
++) {
794 * read the agf, then the agi. This gets us
795 * all the information we need and populates the
796 * per-ag structures for us.
798 error
= xfs_alloc_pagf_init(mp
, NULL
, index
, 0);
802 error
= xfs_ialloc_pagi_init(mp
, NULL
, index
);
805 pag
= xfs_perag_get(mp
, index
);
806 ifree
+= pag
->pagi_freecount
;
807 ialloc
+= pag
->pagi_count
;
808 bfree
+= pag
->pagf_freeblks
;
809 bfreelst
+= pag
->pagf_flcount
;
810 btree
+= pag
->pagf_btreeblks
;
813 fdblocks
= bfree
+ bfreelst
+ btree
;
816 * If the new summary counts are obviously incorrect, fail the
817 * mount operation because that implies the AGFs are also corrupt.
818 * Clear BAD_SUMMARY so that we don't unmount with a dirty log, which
819 * will prevent xfs_repair from fixing anything.
821 if (fdblocks
> sbp
->sb_dblocks
|| ifree
> ialloc
) {
822 xfs_alert(mp
, "AGF corruption. Please run xfs_repair.");
823 error
= -EFSCORRUPTED
;
827 /* Overwrite incore superblock counters with just-read data */
828 spin_lock(&mp
->m_sb_lock
);
829 sbp
->sb_ifree
= ifree
;
830 sbp
->sb_icount
= ialloc
;
831 sbp
->sb_fdblocks
= fdblocks
;
832 spin_unlock(&mp
->m_sb_lock
);
834 xfs_reinit_percpu_counters(mp
);
836 mp
->m_flags
&= ~XFS_MOUNT_BAD_SUMMARY
;
841 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
842 * into the superblock buffer to be logged. It does not provide the higher
843 * level of locking that is needed to protect the in-core superblock from
848 struct xfs_trans
*tp
)
850 struct xfs_mount
*mp
= tp
->t_mountp
;
851 struct xfs_buf
*bp
= xfs_trans_getsb(tp
, mp
, 0);
853 mp
->m_sb
.sb_icount
= percpu_counter_sum(&mp
->m_icount
);
854 mp
->m_sb
.sb_ifree
= percpu_counter_sum(&mp
->m_ifree
);
855 mp
->m_sb
.sb_fdblocks
= percpu_counter_sum(&mp
->m_fdblocks
);
857 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp
), &mp
->m_sb
);
858 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_SB_BUF
);
859 xfs_trans_log_buf(tp
, bp
, 0, sizeof(struct xfs_dsb
));
865 * Sync the superblock to disk.
867 * Note that the caller is responsible for checking the frozen state of the
868 * filesystem. This procedure uses the non-blocking transaction allocator and
869 * thus will allow modifications to a frozen fs. This is required because this
870 * code can be called during the process of freezing where use of the high-level
871 * allocator would deadlock.
875 struct xfs_mount
*mp
,
878 struct xfs_trans
*tp
;
881 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_sb
, 0, 0,
882 XFS_TRANS_NO_WRITECOUNT
, &tp
);
888 xfs_trans_set_sync(tp
);
889 return xfs_trans_commit(tp
);
893 * Update all the secondary superblocks to match the new state of the primary.
894 * Because we are completely overwriting all the existing fields in the
895 * secondary superblock buffers, there is no need to read them in from disk.
896 * Just get a new buffer, stamp it and write it.
898 * The sb buffers need to be cached here so that we serialise against other
899 * operations that access the secondary superblocks, but we don't want to keep
900 * them in memory once it is written so we mark it as a one-shot buffer.
903 xfs_update_secondary_sbs(
904 struct xfs_mount
*mp
)
909 LIST_HEAD (buffer_list
);
911 /* update secondary superblocks. */
912 for (agno
= 1; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
915 bp
= xfs_buf_get(mp
->m_ddev_targp
,
916 XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
),
917 XFS_FSS_TO_BB(mp
, 1), 0);
919 * If we get an error reading or writing alternate superblocks,
920 * continue. xfs_repair chooses the "best" superblock based
921 * on most matches; if we break early, we'll leave more
922 * superblocks un-updated than updated, and xfs_repair may
923 * pick them over the properly-updated primary.
927 "error allocating secondary superblock for ag %d",
930 saved_error
= -ENOMEM
;
934 bp
->b_ops
= &xfs_sb_buf_ops
;
936 xfs_buf_zero(bp
, 0, BBTOB(bp
->b_length
));
937 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp
), &mp
->m_sb
);
938 xfs_buf_delwri_queue(bp
, &buffer_list
);
941 /* don't hold too many buffers at once */
945 error
= xfs_buf_delwri_submit(&buffer_list
);
948 "write error %d updating a secondary superblock near ag %d",
955 error
= xfs_buf_delwri_submit(&buffer_list
);
958 "write error %d updating a secondary superblock near ag %d",
962 return saved_error
? saved_error
: error
;
966 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
967 * also writes the superblock buffer to disk sector 0 immediately.
971 struct xfs_mount
*mp
)
973 struct xfs_trans
*tp
;
977 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_sb
, 0, 0, 0, &tp
);
981 bp
= xfs_trans_getsb(tp
, mp
, 0);
983 xfs_trans_bhold(tp
, bp
);
984 xfs_trans_set_sync(tp
);
985 error
= xfs_trans_commit(tp
);
989 * write out the sb buffer to get the changes to disk
991 error
= xfs_bwrite(bp
);
1000 struct xfs_fsop_geom
*geo
,
1003 memset(geo
, 0, sizeof(struct xfs_fsop_geom
));
1005 geo
->blocksize
= sbp
->sb_blocksize
;
1006 geo
->rtextsize
= sbp
->sb_rextsize
;
1007 geo
->agblocks
= sbp
->sb_agblocks
;
1008 geo
->agcount
= sbp
->sb_agcount
;
1009 geo
->logblocks
= sbp
->sb_logblocks
;
1010 geo
->sectsize
= sbp
->sb_sectsize
;
1011 geo
->inodesize
= sbp
->sb_inodesize
;
1012 geo
->imaxpct
= sbp
->sb_imax_pct
;
1013 geo
->datablocks
= sbp
->sb_dblocks
;
1014 geo
->rtblocks
= sbp
->sb_rblocks
;
1015 geo
->rtextents
= sbp
->sb_rextents
;
1016 geo
->logstart
= sbp
->sb_logstart
;
1017 BUILD_BUG_ON(sizeof(geo
->uuid
) != sizeof(sbp
->sb_uuid
));
1018 memcpy(geo
->uuid
, &sbp
->sb_uuid
, sizeof(sbp
->sb_uuid
));
1020 if (struct_version
< 2)
1023 geo
->sunit
= sbp
->sb_unit
;
1024 geo
->swidth
= sbp
->sb_width
;
1026 if (struct_version
< 3)
1029 geo
->version
= XFS_FSOP_GEOM_VERSION
;
1030 geo
->flags
= XFS_FSOP_GEOM_FLAGS_NLINK
|
1031 XFS_FSOP_GEOM_FLAGS_DIRV2
;
1032 if (xfs_sb_version_hasattr(sbp
))
1033 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_ATTR
;
1034 if (xfs_sb_version_hasquota(sbp
))
1035 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_QUOTA
;
1036 if (xfs_sb_version_hasalign(sbp
))
1037 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_IALIGN
;
1038 if (xfs_sb_version_hasdalign(sbp
))
1039 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_DALIGN
;
1040 if (xfs_sb_version_hasextflgbit(sbp
))
1041 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_EXTFLG
;
1042 if (xfs_sb_version_hassector(sbp
))
1043 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_SECTOR
;
1044 if (xfs_sb_version_hasasciici(sbp
))
1045 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_DIRV2CI
;
1046 if (xfs_sb_version_haslazysbcount(sbp
))
1047 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_LAZYSB
;
1048 if (xfs_sb_version_hasattr2(sbp
))
1049 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_ATTR2
;
1050 if (xfs_sb_version_hasprojid32bit(sbp
))
1051 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_PROJID32
;
1052 if (xfs_sb_version_hascrc(sbp
))
1053 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_V5SB
;
1054 if (xfs_sb_version_hasftype(sbp
))
1055 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_FTYPE
;
1056 if (xfs_sb_version_hasfinobt(sbp
))
1057 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_FINOBT
;
1058 if (xfs_sb_version_hassparseinodes(sbp
))
1059 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_SPINODES
;
1060 if (xfs_sb_version_hasrmapbt(sbp
))
1061 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_RMAPBT
;
1062 if (xfs_sb_version_hasreflink(sbp
))
1063 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_REFLINK
;
1064 if (xfs_sb_version_hassector(sbp
))
1065 geo
->logsectsize
= sbp
->sb_logsectsize
;
1067 geo
->logsectsize
= BBSIZE
;
1068 geo
->rtsectsize
= sbp
->sb_blocksize
;
1069 geo
->dirblocksize
= xfs_dir2_dirblock_bytes(sbp
);
1071 if (struct_version
< 4)
1074 if (xfs_sb_version_haslogv2(sbp
))
1075 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_LOGV2
;
1077 geo
->logsunit
= sbp
->sb_logsunit
;
1082 /* Read a secondary superblock. */
1084 xfs_sb_read_secondary(
1085 struct xfs_mount
*mp
,
1086 struct xfs_trans
*tp
,
1087 xfs_agnumber_t agno
,
1088 struct xfs_buf
**bpp
)
1093 ASSERT(agno
!= 0 && agno
!= NULLAGNUMBER
);
1094 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
1095 XFS_AG_DADDR(mp
, agno
, XFS_SB_BLOCK(mp
)),
1096 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_sb_buf_ops
);
1099 xfs_buf_set_ref(bp
, XFS_SSB_REF
);
1104 /* Get an uninitialised secondary superblock buffer. */
1106 xfs_sb_get_secondary(
1107 struct xfs_mount
*mp
,
1108 struct xfs_trans
*tp
,
1109 xfs_agnumber_t agno
,
1110 struct xfs_buf
**bpp
)
1114 ASSERT(agno
!= 0 && agno
!= NULLAGNUMBER
);
1115 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
1116 XFS_AG_DADDR(mp
, agno
, XFS_SB_BLOCK(mp
)),
1117 XFS_FSS_TO_BB(mp
, 1), 0);
1120 bp
->b_ops
= &xfs_sb_buf_ops
;
1121 xfs_buf_oneshot(bp
);