hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/fs/xfs/libxfs/xfs_sb.c
....@@ -12,24 +12,19 @@
1212 #include "xfs_bit.h"
1313 #include "xfs_sb.h"
1414 #include "xfs_mount.h"
15
-#include "xfs_defer.h"
16
-#include "xfs_inode.h"
1715 #include "xfs_ialloc.h"
1816 #include "xfs_alloc.h"
1917 #include "xfs_error.h"
2018 #include "xfs_trace.h"
21
-#include "xfs_cksum.h"
2219 #include "xfs_trans.h"
2320 #include "xfs_buf_item.h"
2421 #include "xfs_bmap_btree.h"
2522 #include "xfs_alloc_btree.h"
26
-#include "xfs_ialloc_btree.h"
2723 #include "xfs_log.h"
2824 #include "xfs_rmap_btree.h"
29
-#include "xfs_bmap.h"
3025 #include "xfs_refcount_btree.h"
3126 #include "xfs_da_format.h"
32
-#include "xfs_da_btree.h"
27
+#include "xfs_health.h"
3328
3429 /*
3530 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
....@@ -225,10 +220,11 @@
225220 struct xfs_buf *bp,
226221 struct xfs_sb *sbp)
227222 {
223
+ struct xfs_dsb *dsb = bp->b_addr;
228224 uint32_t agcount = 0;
229225 uint32_t rem;
230226
231
- if (sbp->sb_magicnum != XFS_SB_MAGIC) {
227
+ if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
232228 xfs_warn(mp, "bad magic number");
233229 return -EWRONGFS;
234230 }
....@@ -247,7 +243,7 @@
247243 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
248244 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
249245 xfs_notice(mp,
250
-"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
246
+"Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
251247 return -EFSCORRUPTED;
252248 }
253249
....@@ -330,6 +326,38 @@
330326 sbp->sb_shared_vn != 0)) {
331327 xfs_notice(mp, "SB sanity check failed");
332328 return -EFSCORRUPTED;
329
+ }
330
+
331
+ /* Validate the realtime geometry; stolen from xfs_repair */
332
+ if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
333
+ sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
334
+ xfs_notice(mp,
335
+ "realtime extent sanity check failed");
336
+ return -EFSCORRUPTED;
337
+ }
338
+
339
+ if (sbp->sb_rblocks == 0) {
340
+ if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
341
+ sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
342
+ xfs_notice(mp,
343
+ "realtime zeroed geometry check failed");
344
+ return -EFSCORRUPTED;
345
+ }
346
+ } else {
347
+ uint64_t rexts;
348
+ uint64_t rbmblocks;
349
+
350
+ rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
351
+ rbmblocks = howmany_64(sbp->sb_rextents,
352
+ NBBY * sbp->sb_blocksize);
353
+
354
+ if (sbp->sb_rextents != rexts ||
355
+ sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
356
+ sbp->sb_rbmblocks != rbmblocks) {
357
+ xfs_notice(mp,
358
+ "realtime geometry sanity check failed");
359
+ return -EFSCORRUPTED;
360
+ }
333361 }
334362
335363 if (sbp->sb_unit) {
....@@ -572,7 +600,7 @@
572600 * disk. If neither are active, we should NULL the inode.
573601 *
574602 * In all cases, the separate pquotino must remain 0 because it
575
- * it beyond the "end" of the valid non-pquotino superblock.
603
+ * is beyond the "end" of the valid non-pquotino superblock.
576604 */
577605 if (from->sb_qflags & XFS_GQUOTA_ACCT)
578606 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
....@@ -684,8 +712,8 @@
684712 struct xfs_buf *bp)
685713 {
686714 struct xfs_sb sb;
687
- struct xfs_mount *mp = bp->b_target->bt_mount;
688
- struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
715
+ struct xfs_mount *mp = bp->b_mount;
716
+ struct xfs_dsb *dsb = bp->b_addr;
689717 int error;
690718
691719 /*
....@@ -711,7 +739,7 @@
711739 * Check all the superblock fields. Don't byteswap the xquota flags
712740 * because _verify_common checks the on-disk values.
713741 */
714
- __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
742
+ __xfs_sb_from_disk(&sb, dsb, false);
715743 error = xfs_validate_sb_common(mp, bp, &sb);
716744 if (error)
717745 goto out_error;
....@@ -734,7 +762,7 @@
734762 xfs_sb_quiet_read_verify(
735763 struct xfs_buf *bp)
736764 {
737
- struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
765
+ struct xfs_dsb *dsb = bp->b_addr;
738766
739767 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
740768 /* XFS filesystem, verify noisily! */
....@@ -750,15 +778,16 @@
750778 struct xfs_buf *bp)
751779 {
752780 struct xfs_sb sb;
753
- struct xfs_mount *mp = bp->b_target->bt_mount;
781
+ struct xfs_mount *mp = bp->b_mount;
754782 struct xfs_buf_log_item *bip = bp->b_log_item;
783
+ struct xfs_dsb *dsb = bp->b_addr;
755784 int error;
756785
757786 /*
758787 * Check all the superblock fields. Don't byteswap the xquota flags
759788 * because _verify_common checks the on-disk values.
760789 */
761
- __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
790
+ __xfs_sb_from_disk(&sb, dsb, false);
762791 error = xfs_validate_sb_common(mp, bp, &sb);
763792 if (error)
764793 goto out_error;
....@@ -770,7 +799,7 @@
770799 return;
771800
772801 if (bip)
773
- XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
802
+ dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
774803
775804 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
776805 return;
....@@ -781,12 +810,14 @@
781810
782811 const struct xfs_buf_ops xfs_sb_buf_ops = {
783812 .name = "xfs_sb",
813
+ .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
784814 .verify_read = xfs_sb_read_verify,
785815 .verify_write = xfs_sb_write_verify,
786816 };
787817
788818 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
789819 .name = "xfs_sb_quiet",
820
+ .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
790821 .verify_read = xfs_sb_quiet_read_verify,
791822 .verify_write = xfs_sb_write_verify,
792823 };
....@@ -796,12 +827,14 @@
796827 *
797828 * Mount initialization code establishing various mount
798829 * fields from the superblock associated with the given
799
- * mount structure
830
+ * mount structure.
831
+ *
832
+ * Inode geometry are calculated in xfs_ialloc_setup_geometry.
800833 */
801834 void
802835 xfs_sb_mount_common(
803
- struct xfs_mount *mp,
804
- struct xfs_sb *sbp)
836
+ struct xfs_mount *mp,
837
+ struct xfs_sb *sbp)
805838 {
806839 mp->m_agfrotor = mp->m_agirotor = 0;
807840 mp->m_maxagi = mp->m_sb.sb_agcount;
....@@ -809,7 +842,6 @@
809842 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
810843 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
811844 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
812
- mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
813845 mp->m_blockmask = sbp->sb_blocksize - 1;
814846 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
815847 mp->m_blockwmask = mp->m_blockwsize - 1;
....@@ -818,11 +850,6 @@
818850 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
819851 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
820852 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
821
-
822
- mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
823
- mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
824
- mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
825
- mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
826853
827854 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
828855 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
....@@ -840,14 +867,6 @@
840867 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
841868
842869 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
843
- mp->m_ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
844
- sbp->sb_inopblock);
845
- mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
846
-
847
- if (sbp->sb_spino_align)
848
- mp->m_ialloc_min_blks = sbp->sb_spino_align;
849
- else
850
- mp->m_ialloc_min_blks = mp->m_ialloc_blks;
851870 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
852871 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
853872 }
....@@ -874,7 +893,7 @@
874893 uint64_t bfreelst = 0;
875894 uint64_t btree = 0;
876895 uint64_t fdblocks;
877
- int error;
896
+ int error = 0;
878897
879898 for (index = 0; index < agcount; index++) {
880899 /*
....@@ -902,7 +921,7 @@
902921 /*
903922 * If the new summary counts are obviously incorrect, fail the
904923 * mount operation because that implies the AGFs are also corrupt.
905
- * Clear BAD_SUMMARY so that we don't unmount with a dirty log, which
924
+ * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
906925 * will prevent xfs_repair from fixing anything.
907926 */
908927 if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
....@@ -920,7 +939,7 @@
920939
921940 xfs_reinit_percpu_counters(mp);
922941 out:
923
- mp->m_flags &= ~XFS_MOUNT_BAD_SUMMARY;
942
+ xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
924943 return error;
925944 }
926945
....@@ -935,15 +954,25 @@
935954 struct xfs_trans *tp)
936955 {
937956 struct xfs_mount *mp = tp->t_mountp;
938
- struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
957
+ struct xfs_buf *bp = xfs_trans_getsb(tp);
939958
940
- mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
941
- mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
942
- mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
959
+ /*
960
+ * Lazy sb counters don't update the in-core superblock so do that now.
961
+ * If this is at unmount, the counters will be exactly correct, but at
962
+ * any other time they will only be ballpark correct because of
963
+ * reservations that have been taken out percpu counters. If we have an
964
+ * unclean shutdown, this will be corrected by log recovery rebuilding
965
+ * the counters from the AGF block counts.
966
+ */
967
+ if (xfs_sb_version_haslazysbcount(&mp->m_sb)) {
968
+ mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
969
+ mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
970
+ mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
971
+ }
943972
944
- xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
973
+ xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
945974 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
946
- xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
975
+ xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
947976 }
948977
949978 /*
....@@ -999,9 +1028,9 @@
9991028 for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) {
10001029 struct xfs_buf *bp;
10011030
1002
- bp = xfs_buf_get(mp->m_ddev_targp,
1031
+ error = xfs_buf_get(mp->m_ddev_targp,
10031032 XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
1004
- XFS_FSS_TO_BB(mp, 1), 0);
1033
+ XFS_FSS_TO_BB(mp, 1), &bp);
10051034 /*
10061035 * If we get an error reading or writing alternate superblocks,
10071036 * continue. xfs_repair chooses the "best" superblock based
....@@ -1009,19 +1038,19 @@
10091038 * superblocks un-updated than updated, and xfs_repair may
10101039 * pick them over the properly-updated primary.
10111040 */
1012
- if (!bp) {
1041
+ if (error) {
10131042 xfs_warn(mp,
10141043 "error allocating secondary superblock for ag %d",
10151044 agno);
10161045 if (!saved_error)
1017
- saved_error = -ENOMEM;
1046
+ saved_error = error;
10181047 continue;
10191048 }
10201049
10211050 bp->b_ops = &xfs_sb_buf_ops;
10221051 xfs_buf_oneshot(bp);
10231052 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
1024
- xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
1053
+ xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
10251054 xfs_buf_delwri_queue(bp, &buffer_list);
10261055 xfs_buf_relse(bp);
10271056
....@@ -1065,7 +1094,7 @@
10651094 if (error)
10661095 return error;
10671096
1068
- bp = xfs_trans_getsb(tp, mp, 0);
1097
+ bp = xfs_trans_getsb(tp);
10691098 xfs_log_sb(tp);
10701099 xfs_trans_bhold(tp, bp);
10711100 xfs_trans_set_sync(tp);
....@@ -1081,7 +1110,7 @@
10811110 return error;
10821111 }
10831112
1084
-int
1113
+void
10851114 xfs_fs_geometry(
10861115 struct xfs_sb *sbp,
10871116 struct xfs_fsop_geom *geo,
....@@ -1105,17 +1134,18 @@
11051134 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
11061135
11071136 if (struct_version < 2)
1108
- return 0;
1137
+ return;
11091138
11101139 geo->sunit = sbp->sb_unit;
11111140 geo->swidth = sbp->sb_width;
11121141
11131142 if (struct_version < 3)
1114
- return 0;
1143
+ return;
11151144
11161145 geo->version = XFS_FSOP_GEOM_VERSION;
11171146 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1118
- XFS_FSOP_GEOM_FLAGS_DIRV2;
1147
+ XFS_FSOP_GEOM_FLAGS_DIRV2 |
1148
+ XFS_FSOP_GEOM_FLAGS_EXTFLG;
11191149 if (xfs_sb_version_hasattr(sbp))
11201150 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
11211151 if (xfs_sb_version_hasquota(sbp))
....@@ -1124,8 +1154,6 @@
11241154 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
11251155 if (xfs_sb_version_hasdalign(sbp))
11261156 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1127
- if (xfs_sb_version_hasextflgbit(sbp))
1128
- geo->flags |= XFS_FSOP_GEOM_FLAGS_EXTFLG;
11291157 if (xfs_sb_version_hassector(sbp))
11301158 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
11311159 if (xfs_sb_version_hasasciici(sbp))
....@@ -1148,6 +1176,8 @@
11481176 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
11491177 if (xfs_sb_version_hasreflink(sbp))
11501178 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1179
+ if (xfs_sb_version_hasbigtime(sbp))
1180
+ geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
11511181 if (xfs_sb_version_hassector(sbp))
11521182 geo->logsectsize = sbp->sb_logsectsize;
11531183 else
....@@ -1156,14 +1186,17 @@
11561186 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
11571187
11581188 if (struct_version < 4)
1159
- return 0;
1189
+ return;
11601190
11611191 if (xfs_sb_version_haslogv2(sbp))
11621192 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
11631193
11641194 geo->logsunit = sbp->sb_logsunit;
11651195
1166
- return 0;
1196
+ if (struct_version < 5)
1197
+ return;
1198
+
1199
+ geo->version = XFS_FSOP_GEOM_VERSION_V5;
11671200 }
11681201
11691202 /* Read a secondary superblock. */
....@@ -1197,13 +1230,14 @@
11971230 struct xfs_buf **bpp)
11981231 {
11991232 struct xfs_buf *bp;
1233
+ int error;
12001234
12011235 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1202
- bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1236
+ error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
12031237 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1204
- XFS_FSS_TO_BB(mp, 1), 0);
1205
- if (!bp)
1206
- return -ENOMEM;
1238
+ XFS_FSS_TO_BB(mp, 1), 0, &bp);
1239
+ if (error)
1240
+ return error;
12071241 bp->b_ops = &xfs_sb_buf_ops;
12081242 xfs_buf_oneshot(bp);
12091243 *bpp = bp;