]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/sb.c
0e4827e046780ba696cb72d4bc6c8d7e5e90ffe5
[thirdparty/xfsprogs-dev.git] / repair / sb.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "libfrog/util.h"
7 #include "libxfs.h"
8 #include "libxcmd.h"
9 #include "libxlog.h"
10 #include "agheader.h"
11 #include "globals.h"
12 #include "protos.h"
13 #include "err_protos.h"
14 #include "xfs_multidisk.h"
15
16 #define BSIZE (1024 * 1024)
17
18 /*
19 * copy the fields of a superblock that are present in primary and
20 * secondaries -- preserve fields that are different in the primary.
21 */
22 static void
23 copy_sb(xfs_sb_t *source, xfs_sb_t *dest)
24 {
25 xfs_ino_t rootino;
26 xfs_ino_t rbmino;
27 xfs_ino_t rsumino;
28 xfs_ino_t uquotino;
29 xfs_ino_t gquotino;
30 xfs_ino_t pquotino;
31 xfs_ino_t metadirino;
32 uint16_t versionnum;
33
34 rootino = dest->sb_rootino;
35 rbmino = dest->sb_rbmino;
36 rsumino = dest->sb_rsumino;
37 uquotino = dest->sb_uquotino;
38 gquotino = dest->sb_gquotino;
39 pquotino = dest->sb_pquotino;
40 metadirino = dest->sb_metadirino;
41
42 versionnum = dest->sb_versionnum;
43
44 *dest = *source;
45
46 dest->sb_rootino = rootino;
47 dest->sb_rbmino = rbmino;
48 dest->sb_rsumino = rsumino;
49 dest->sb_uquotino = uquotino;
50 dest->sb_gquotino = gquotino;
51 dest->sb_pquotino = pquotino;
52 dest->sb_metadirino = metadirino;
53
54 dest->sb_versionnum = versionnum;
55
56 /*
57 * copy over version bits that are stamped into all
58 * secondaries and cannot be changed at run time in
59 * the primary superblock
60 */
61 if (xfs_sb_version_hasdalign(source))
62 dest->sb_versionnum |= XFS_SB_VERSION_DALIGNBIT;
63 dest->sb_versionnum |= XFS_SB_VERSION_EXTFLGBIT;
64
65 /*
66 * these are all supposed to be zero or will get reset anyway
67 */
68 dest->sb_icount = 0;
69 dest->sb_ifree = 0;
70 dest->sb_fdblocks = 0;
71 dest->sb_frextents = 0;
72
73 memset(source->sb_fname, 0, 12);
74 }
75
76 static int
77 verify_sb_blocksize(xfs_sb_t *sb)
78 {
79 /* check to make sure blocksize is legal 2^N, 9 <= N <= 16 */
80 if (sb->sb_blocksize == 0)
81 return XR_BAD_BLOCKSIZE;
82 if (sb->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
83 sb->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG)
84 return XR_BAD_BLOCKLOG;
85 if (sb->sb_blocksize != (1 << sb->sb_blocklog))
86 return XR_BAD_BLOCKLOG;
87
88 return 0;
89 }
90
91 /*
92 * find a secondary superblock, copy it into the sb buffer.
93 * start is the point to begin reading BSIZE bytes.
94 * skip contains a byte-count of how far to advance for next read.
95 */
96 static int
97 __find_secondary_sb(
98 xfs_sb_t *rsb,
99 uint64_t start,
100 uint64_t skip)
101 {
102 xfs_off_t off;
103 xfs_sb_t *sb;
104 xfs_sb_t bufsb;
105 char *c_bufsb;
106 int done;
107 int i;
108 int dirty;
109 int retval;
110 int bsize;
111
112 sb = (xfs_sb_t *)memalign(libxfs_device_alignment(), BSIZE);
113 if (!sb) {
114 do_error(
115 _("error finding secondary superblock -- failed to memalign buffer\n"));
116 exit(1);
117 }
118
119 memset(&bufsb, 0, sizeof(xfs_sb_t));
120 retval = 0;
121 dirty = 0;
122 bsize = 0;
123
124 /*
125 * skip first sector since we know that's bad
126 */
127 for (done = 0, off = start; !done ; off += skip) {
128 /*
129 * read disk 1 MByte at a time.
130 */
131 if (lseek(x.data.fd, off, SEEK_SET) != off)
132 done = 1;
133
134 if (!done && (bsize = read(x.data.fd, sb, BSIZE)) <= 0)
135 done = 1;
136
137 do_warn(".");
138
139 /*
140 * check the buffer 512 bytes at a time since
141 * we don't know how big the sectors really are.
142 */
143 for (i = 0; !done && i < bsize; i += BBSIZE) {
144 c_bufsb = (char *)sb + i;
145 libxfs_sb_from_disk(&bufsb, (struct xfs_dsb *)c_bufsb);
146
147 if (verify_sb(c_bufsb, &bufsb, 0) != XR_OK)
148 continue;
149
150 do_warn(_("found candidate secondary superblock...\n"));
151
152 /*
153 * found one. now verify it by looking
154 * for other secondaries.
155 */
156 memmove(rsb, &bufsb, sizeof(xfs_sb_t));
157 rsb->sb_inprogress = 0;
158 copied_sunit = 1;
159
160 if (verify_set_primary_sb(rsb, 0, &dirty) == XR_OK) {
161 do_warn(
162 _("verified secondary superblock...\n"));
163 done = 1;
164 retval = 1;
165 } else {
166 do_warn(
167 _("unable to verify superblock, continuing...\n"));
168 }
169 }
170 }
171
172 free(sb);
173 return retval;
174 }
175
176 static int
177 guess_default_geometry(
178 uint64_t *agsize,
179 uint64_t *agcount,
180 struct libxfs_init *x)
181 {
182 struct fs_topology ft;
183 int blocklog;
184 uint64_t dblocks;
185 int multidisk;
186
187 memset(&ft, 0, sizeof(ft));
188 get_topology(x, &ft, 1);
189
190 /*
191 * get geometry from get_topology result.
192 * Use default block size (2^12)
193 */
194 blocklog = 12;
195 multidisk = ft.data.swidth | ft.data.sunit;
196 dblocks = x->data.size >> (blocklog - BBSHIFT);
197 calc_default_ag_geometry(blocklog, dblocks, multidisk,
198 agsize, agcount);
199
200 return blocklog;
201 }
202
203 int
204 find_secondary_sb(xfs_sb_t *rsb)
205 {
206 int retval = 0;
207 uint64_t agcount;
208 uint64_t agsize;
209 uint64_t skip;
210 int blocklog;
211
212 /*
213 * Attempt to find secondary sb with a coarse approach,
214 * first trying agblocks and blocksize read from sb, providing
215 * they're sane.
216 */
217 do_warn(_("\nattempting to find secondary superblock...\n"));
218
219 if (verify_sb_blocksize(rsb) == 0) {
220 skip = (uint64_t)rsb->sb_agblocks * rsb->sb_blocksize;
221 if (skip >= XFS_AG_MIN_BYTES && skip <= XFS_AG_MAX_BYTES)
222 retval = __find_secondary_sb(rsb, skip, skip);
223 }
224
225 /* If that failed, retry coarse approach, using default geometry */
226 if (!retval) {
227 blocklog = guess_default_geometry(&agsize, &agcount, &x);
228 skip = agsize << blocklog;
229 retval = __find_secondary_sb(rsb, skip, skip);
230 }
231
232 /* If that failed, fall back to the brute force method */
233 if (!retval)
234 retval = __find_secondary_sb(rsb, XFS_AG_MIN_BYTES, BSIZE);
235
236 if (retval && xfs_sb_version_hasmetadir(rsb))
237 do_warn(_("quota accounting and enforcement flags lost\n"));
238
239 return retval;
240 }
241
242 /*
243 * Calculate what the inode alignment field ought to be based on internal
244 * superblock info and determine if it is valid.
245 *
246 * For standard v5 superblocks, the inode alignment must either match
247 * XFS_INODE_BIG_CLUSTER_SIZE or a multiple based on the inode size. For v5
248 * superblocks with sparse inode chunks enabled, inode alignment must match the
249 * inode chunk size.
250 *
251 * Return true if the alignment is valid, false otherwise.
252 */
253 static bool
254 sb_validate_ino_align(struct xfs_sb *sb)
255 {
256 xfs_extlen_t align;
257
258 if (!xfs_sb_version_hasalign(sb))
259 return true;
260
261 /* standard cluster size alignment is always valid */
262 align = XFS_INODE_BIG_CLUSTER_SIZE >> sb->sb_blocklog;
263 if (align == sb->sb_inoalignmt)
264 return true;
265
266 /* alignment scaled by inode size is v5 only for now */
267 if (!xfs_sb_version_hascrc(sb))
268 return false;
269
270 align = (XFS_INODE_BIG_CLUSTER_SIZE *
271 sb->sb_inodesize / XFS_DINODE_MIN_SIZE) >> sb->sb_blocklog;
272 if (align == sb->sb_inoalignmt)
273 return true;
274
275 /*
276 * Sparse inodes requires inoalignmt to match full inode chunk size and
277 * spino_align to match the scaled alignment (as calculated above).
278 */
279 if (xfs_sb_version_hassparseinodes(sb)) {
280 if (align != sb->sb_spino_align)
281 return false;
282
283 align = (sb->sb_inodesize * XFS_INODES_PER_CHUNK)
284 >> sb->sb_blocklog;
285 if (align == sb->sb_inoalignmt)
286 return true;
287 }
288
289 return false;
290 }
291
292 /*
293 * Validate the given log space. Derived from xfs_log_mount, though we
294 * can't validate the minimum log size until later. We only do this
295 * validation on V5 filesystems because the kernel doesn't reject malformed
296 * log geometry on older revision filesystems.
297 *
298 * Returns false if the log is garbage.
299 */
300 static bool
301 verify_sb_loginfo(
302 struct xfs_sb *sb)
303 {
304 if (xfs_sb_version_hascrc(sb) &&
305 (sb->sb_logblocks == 0 ||
306 sb->sb_logblocks > XFS_MAX_LOG_BLOCKS ||
307 ((unsigned long long)sb->sb_logblocks << sb->sb_blocklog) >
308 XFS_MAX_LOG_BYTES))
309 return false;
310
311 if (sb->sb_logsunit > 1 && sb->sb_logsunit % sb->sb_blocksize)
312 return false;
313
314 return true;
315 }
316
317 static int
318 verify_sb_rtgroups(
319 struct xfs_sb *sbp)
320 {
321 uint64_t groups;
322
323 if (sbp->sb_rextsize == 0)
324 return XR_BAD_RT_GEO_DATA;
325
326 if (sbp->sb_rgextents > XFS_MAX_RGBLOCKS / sbp->sb_rextsize)
327 return XR_BAD_RT_GEO_DATA;
328
329 if (sbp->sb_rgextents < XFS_MIN_RGEXTENTS)
330 return XR_BAD_RT_GEO_DATA;
331
332 if (sbp->sb_rgcount > XFS_MAX_RGNUMBER)
333 return XR_BAD_RT_GEO_DATA;
334
335 groups = howmany_64(sbp->sb_rextents, sbp->sb_rgextents);
336 if (groups != sbp->sb_rgcount)
337 return XR_BAD_RT_GEO_DATA;
338
339 if (!(sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_EXCHRANGE))
340 return XR_BAD_RT_GEO_DATA;
341
342 if (sbp->sb_rgblklog != libxfs_compute_rgblklog(sbp->sb_rgextents,
343 sbp->sb_rextsize))
344 return XR_BAD_RT_GEO_DATA;
345
346 return 0;
347 }
348
349 /*
350 * verify a superblock -- does not verify root inode #
351 * can only check that geometry info is internally
352 * consistent. because of growfs, that's no guarantee
353 * of correctness (e.g. geometry may have changed)
354 *
355 * fields verified or consistency checked:
356 *
357 * sb_magicnum
358 *
359 * sb_versionnum
360 *
361 * sb_inprogress
362 *
363 * sb_blocksize (as a group)
364 * sb_blocklog
365 *
366 * geometry info - sb_dblocks (as a group)
367 * sb_agcount
368 * sb_agblocks
369 * sb_agblklog
370 *
371 * inode info - sb_inodesize (x-checked with geo info)
372 * sb_inopblock
373 *
374 * sector size info -
375 * sb_sectsize
376 * sb_sectlog
377 * sb_logsectsize
378 * sb_logsectlog
379 *
380 * not checked here -
381 * sb_rootino
382 * sb_fname
383 * sb_fpack
384 * sb_logstart
385 * sb_uuid
386 *
387 * ALL real-time fields
388 * final 4 summary counters
389 */
390
391 int
392 verify_sb(char *sb_buf, xfs_sb_t *sb, int is_primary_sb)
393 {
394 uint32_t bsize;
395 int i;
396 int ret;
397
398 /* check magic number and version number */
399
400 if (sb->sb_magicnum != XFS_SB_MAGIC)
401 return(XR_BAD_MAGIC);
402
403 if (!xfs_sb_good_version(sb))
404 return(XR_BAD_VERSION);
405
406 /* does sb think mkfs really finished ? */
407 if (is_primary_sb && sb->sb_inprogress)
408 return(XR_BAD_INPROGRESS);
409
410 /*
411 * before going *any further*, validate the sector size and if the
412 * version says we should have CRCs enabled, validate that.
413 */
414
415 /* check to make sure sectorsize is legal 2^N, 9 <= N <= 15 */
416 if (sb->sb_sectsize == 0)
417 return(XR_BAD_SECT_SIZE_DATA);
418
419 bsize = 1;
420 for (i = 0; bsize < sb->sb_sectsize &&
421 i < sizeof(sb->sb_sectsize) * NBBY; i++) {
422 bsize <<= 1;
423 }
424
425 if (i < XFS_MIN_SECTORSIZE_LOG || i > XFS_MAX_SECTORSIZE_LOG)
426 return(XR_BAD_SECT_SIZE_DATA);
427
428 /* check sb sectorsize field against sb sectlog field */
429 if (i != sb->sb_sectlog)
430 return(XR_BAD_SECT_SIZE_DATA);
431
432 /* sector size in range - CRC check time */
433 if (xfs_sb_version_hascrc(sb) &&
434 !libxfs_verify_cksum(sb_buf, sb->sb_sectsize, XFS_SB_CRC_OFF))
435 return XR_BAD_CRC;
436
437 /* check to ensure blocksize and blocklog are legal */
438 ret = verify_sb_blocksize(sb);
439 if (ret != 0)
440 return ret;
441
442 /* sanity check ag count, size fields against data size field */
443
444 if (sb->sb_dblocks == 0 ||
445 sb->sb_dblocks > XFS_MAX_DBLOCKS(sb) ||
446 sb->sb_dblocks < XFS_MIN_DBLOCKS(sb))
447 return(XR_BAD_FS_SIZE_DATA);
448
449 if (sb->sb_agblklog != (uint8_t)log2_roundup(sb->sb_agblocks))
450 return(XR_BAD_FS_SIZE_DATA);
451
452 if (sb->sb_inodesize < XFS_DINODE_MIN_SIZE ||
453 sb->sb_inodesize > XFS_DINODE_MAX_SIZE ||
454 sb->sb_inodelog < XFS_DINODE_MIN_LOG ||
455 sb->sb_inodelog > XFS_DINODE_MAX_LOG ||
456 sb->sb_inodesize != (1 << sb->sb_inodelog) ||
457 sb->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
458 sb->sb_inopblock != howmany(sb->sb_blocksize, sb->sb_inodesize) ||
459 (sb->sb_blocklog - sb->sb_inodelog != sb->sb_inopblog))
460 return XR_BAD_INO_SIZE_DATA;
461
462 if (!verify_sb_loginfo(sb))
463 return XR_BAD_LOG_GEOMETRY;
464
465 if (xfs_sb_version_hassector(sb)) {
466
467 /* check to make sure log sector is legal 2^N, 9 <= N <= 15 */
468
469 if (sb->sb_logsectsize == 0)
470 return(XR_BAD_SECT_SIZE_DATA);
471
472 bsize = 1;
473
474 for (i = 0; bsize < sb->sb_logsectsize &&
475 i < sizeof(sb->sb_logsectsize) * NBBY; i++) {
476 bsize <<= 1;
477 }
478
479 if (i < XFS_MIN_SECTORSIZE_LOG || i > XFS_MAX_SECTORSIZE_LOG)
480 return(XR_BAD_SECT_SIZE_DATA);
481
482 /* check sb log sectorsize field against sb log sectlog field */
483
484 if (i != sb->sb_logsectlog)
485 return(XR_BAD_SECT_SIZE_DATA);
486 }
487
488 if (!libxfs_validate_rt_geometry(sb))
489 return XR_BAD_RT_GEO_DATA;
490
491 /*
492 * verify correctness of inode alignment if it's there
493 */
494 if (!sb_validate_ino_align(sb))
495 return(XR_BAD_INO_ALIGN);
496
497 /*
498 * verify max. % of inodes (sb_imax_pct)
499 */
500 if (sb->sb_imax_pct > 100)
501 return(XR_BAD_INO_MAX_PCT);
502
503 /*
504 * verify stripe alignment fields if present
505 */
506 if (xfs_sb_version_hasdalign(sb)) {
507 if ((!sb->sb_unit && sb->sb_width) ||
508 (sb->sb_unit && sb->sb_agblocks % sb->sb_unit))
509 return(XR_BAD_SB_UNIT);
510 if ((sb->sb_unit && !sb->sb_width) ||
511 (sb->sb_width && sb->sb_unit && sb->sb_width % sb->sb_unit))
512 return(XR_BAD_SB_WIDTH);
513 } else if (sb->sb_unit || sb->sb_width)
514 return XR_BAD_SB_WIDTH;
515
516 /* Directory block log */
517 if (sb->sb_blocklog + sb->sb_dirblklog > XFS_MAX_BLOCKSIZE_LOG)
518 return XR_BAD_DIR_SIZE_DATA;
519
520 if (xfs_sb_version_hasmetadir(sb)) {
521 if (memchr_inv(sb->sb_pad, 0, sizeof(sb->sb_pad)))
522 return XR_SB_GEO_MISMATCH;
523
524 ret = verify_sb_rtgroups(sb);
525 if (ret)
526 return ret;
527 }
528
529 return(XR_OK);
530 }
531
532 void
533 write_primary_sb(xfs_sb_t *sbp, int size)
534 {
535 struct xfs_dsb *buf;
536
537 if (no_modify)
538 return;
539
540 buf = memalign(libxfs_device_alignment(), size);
541 if (buf == NULL) {
542 do_error(_("failed to memalign superblock buffer\n"));
543 return;
544 }
545 memset(buf, 0, size);
546
547 if (lseek(x.data.fd, 0LL, SEEK_SET) != 0LL) {
548 free(buf);
549 do_error(_("couldn't seek to offset 0 in filesystem\n"));
550 }
551
552 libxfs_sb_to_disk(buf, sbp);
553
554 if (xfs_sb_version_hascrc(sbp))
555 xfs_update_cksum((char *)buf, size, XFS_SB_CRC_OFF);
556
557 if (write(x.data.fd, buf, size) != size) {
558 free(buf);
559 do_error(_("primary superblock write failed!\n"));
560 }
561
562 free(buf);
563 }
564
565 /*
566 * get a possible superblock -- checks for internal consistency
567 */
568 int
569 get_sb(xfs_sb_t *sbp, xfs_off_t off, int size, xfs_agnumber_t agno)
570 {
571 int error, rval;
572 struct xfs_dsb *buf;
573
574 buf = memalign(libxfs_device_alignment(), size);
575 if (buf == NULL) {
576 do_error(
577 _("error reading superblock %u -- failed to memalign buffer\n"),
578 agno);
579 exit(1);
580 }
581 memset(buf, 0, size);
582 memset(sbp, 0, sizeof(*sbp));
583
584 /* try and read it first */
585
586 if (lseek(x.data.fd, off, SEEK_SET) != off) {
587 do_warn(
588 _("error reading superblock %u -- seek to offset %" PRId64 " failed\n"),
589 agno, off);
590 free(buf);
591 return(XR_EOF);
592 }
593
594 if ((rval = read(x.data.fd, buf, size)) != size) {
595 error = errno;
596 do_warn(
597 _("superblock read failed, offset %" PRId64 ", size %d, ag %u, rval %d\n"),
598 off, size, agno, rval);
599 do_error("%s\n", strerror(error));
600 }
601 libxfs_sb_from_disk(sbp, buf);
602
603 rval = verify_sb((char *)buf, sbp, agno == 0);
604 free(buf);
605 return rval;
606 }
607
608 /* returns element on list with highest reference count */
609 static fs_geo_list_t *
610 get_best_geo(fs_geo_list_t *list)
611 {
612 int cnt = 0;
613 fs_geo_list_t *current, *rval = NULL;
614
615 current = list;
616
617 while (current != NULL) {
618 if (current->refs > cnt) {
619 rval = current;
620 cnt = current->refs;
621 }
622 current = current->next;
623 }
624
625 return(rval);
626 }
627
628 /* adds geometry info to linked list. returns (sometimes new) head of list */
629 static fs_geo_list_t *
630 add_geo(fs_geo_list_t *list, fs_geometry_t *geo_p, int index)
631 {
632 fs_geo_list_t *current = list;
633
634 while (current != NULL) {
635 if (memcmp(geo_p, &current->geo, sizeof(fs_geometry_t)) == 0) {
636 current->refs++;
637 return(list);
638 }
639
640 current = current->next;
641 }
642
643 if ((current = malloc(sizeof(fs_geo_list_t))) == NULL) {
644 do_error(_("couldn't malloc geometry structure\n"));
645 exit(1);
646 }
647
648 current->geo = *geo_p;
649 current->refs = 1;
650 current->next = list;
651 current->index = index;
652
653 return(current);
654 }
655
656 static void
657 free_geo(fs_geo_list_t *list)
658 {
659 fs_geo_list_t *next;
660 fs_geo_list_t *current;
661
662 for (current = list; current != NULL; current = next) {
663 next = current->next;
664 free(current);
665 }
666 }
667
668 void
669 get_sb_geometry(fs_geometry_t *geo, xfs_sb_t *sbp)
670 {
671 memset(geo, 0, sizeof(fs_geometry_t));
672
673 /*
674 * blindly set fields that we know are always good
675 */
676 geo->sb_blocksize = sbp->sb_blocksize;
677 geo->sb_dblocks = sbp->sb_dblocks;
678 geo->sb_rblocks = sbp->sb_rblocks;
679 geo->sb_rextents = sbp->sb_rextents;
680 geo->sb_logstart = sbp->sb_logstart;
681 geo->sb_rextsize = sbp->sb_rextsize;
682 geo->sb_agblocks = sbp->sb_agblocks;
683 geo->sb_agcount = sbp->sb_agcount;
684 geo->sb_rbmblocks = sbp->sb_rbmblocks;
685 geo->sb_logblocks = sbp->sb_logblocks;
686 geo->sb_sectsize = sbp->sb_sectsize;
687 geo->sb_inodesize = sbp->sb_inodesize;
688
689 if (xfs_sb_version_hasalign(sbp))
690 geo->sb_ialignbit = 1;
691
692 if (xfs_sb_version_hasdalign(sbp))
693 geo->sb_salignbit = 1;
694
695 geo->sb_extflgbit = 1;
696 geo->sb_fully_zeroed = 1;
697 }
698
699 /*
700 * the way to verify that a primary sb is consistent with the
701 * filesystem is find the secondaries given the info in the
702 * primary and compare the geometries in the secondaries against
703 * the geometry indicated by the primary.
704 *
705 * returns 0 if ok, else error code (XR_EOF, XR_INSUFF_SEC_SB, etc).
706 */
707 int
708 verify_set_primary_sb(xfs_sb_t *rsb,
709 int sb_index,
710 int *sb_modified)
711 {
712 xfs_off_t off;
713 fs_geometry_t geo;
714 xfs_sb_t *sb;
715 fs_geo_list_t *list;
716 fs_geo_list_t *current;
717 xfs_agnumber_t agno;
718 int num_sbs;
719 int size;
720 int num_ok;
721 int retval;
722
723 /*
724 * We haven't been able to validate the sector size yet properly
725 * (e.g. in the case of repairing an image in a file), so we need to
726 * take into account sector mismatches and so use the maximum possible
727 * sector size rather than the sector size in @rsb.
728 */
729 size = NUM_AGH_SECTS * (1 << (XFS_MAX_SECTORSIZE_LOG));
730 list = NULL;
731 num_ok = 0;
732 *sb_modified = 0;
733 num_sbs = rsb->sb_agcount;
734
735 sb = (xfs_sb_t *) alloc_ag_buf(size);
736
737 /*
738 * put the primary sb geometry info onto the geometry list
739 */
740 get_sb_geometry(&geo, rsb);
741 list = add_geo(list, &geo, sb_index);
742
743 /*
744 * scan the secondaries and check them off as we get them so we only
745 * process each one once
746 */
747 for (agno = 1; agno < rsb->sb_agcount; agno++) {
748 off = (xfs_off_t)agno * rsb->sb_agblocks << rsb->sb_blocklog;
749
750 retval = get_sb(sb, off, size, agno);
751 if (retval == XR_EOF)
752 goto out_free_list;
753
754 if (retval == XR_OK) {
755 /*
756 * save away geometry info. don't bother checking the
757 * sb against the agi/agf as the odds of the sb being
758 * corrupted in a way that it is internally consistent
759 * but not consistent with the rest of the filesystem is
760 * really really low.
761 */
762 get_sb_geometry(&geo, sb);
763 list = add_geo(list, &geo, agno);
764 num_ok++;
765 }
766 }
767
768 /*
769 * see if we have enough superblocks to bother with
770 */
771 retval = 0;
772 if (num_ok < num_sbs / 2) {
773 retval = XR_INSUFF_SEC_SB;
774 goto out_free_list;
775 }
776
777 current = get_best_geo(list);
778
779 /*
780 * check that enough sbs agree that we're willing to
781 * go with this geometry. if not, print out the
782 * geometry and a message about the force option.
783 */
784 switch (num_sbs) {
785 case 2:
786 /*
787 * If we only have two allocation groups, and the superblock
788 * in the second allocation group differs from the primary
789 * superblock we can't verify the geometry information.
790 * Warn the user about this situation and get out unless
791 * explicitly overridden.
792 */
793 if (current->refs != 2) {
794 if (!force_geo) {
795 do_warn(
796 _("Only two AGs detected and they do not match - "
797 "cannot validate filesystem geometry.\n"
798 "Use the -o force_geometry option to proceed.\n"));
799 exit(1);
800 }
801 }
802 goto out_free_list;
803 case 1:
804 /*
805 * If we only have a single allocation group there is no
806 * secondary superblock that we can use to verify the geometry
807 * information. Warn the user about this situation and get
808 * out unless explicitly overridden.
809 */
810 if (!force_geo) {
811 do_warn(
812 _("Only one AG detected - "
813 "cannot validate filesystem geometry.\n"
814 "Use the -o force_geometry option to proceed.\n"));
815 exit(1);
816 }
817 goto out_free_list;
818 default:
819 /*
820 * at least half of the probed superblocks have
821 * to agree. if they don't, this fs is probably
822 * too far gone anyway considering the fact that
823 * XFS normally doesn't alter the secondary superblocks.
824 */
825 if (current->refs < num_sbs / 2) {
826 do_warn(
827 _("Not enough matching superblocks - cannot proceed.\n"));
828 exit(1);
829 }
830 }
831
832 /*
833 * set the geometry into primary superblock if necessary.
834 */
835
836 if (current->index != sb_index) {
837 *sb_modified = 1;
838 off = (xfs_off_t)current->index * current->geo.sb_agblocks
839 * current->geo.sb_blocksize;
840 if (get_sb(sb, off, current->geo.sb_sectsize,
841 current->index) != XR_OK)
842 do_error(_("could not read superblock\n"));
843
844 copy_sb(sb, rsb);
845
846 /*
847 * turn off inprogress bit since this is the primary.
848 * also save away values that we need to ensure are
849 * consistent in the other secondaries.
850 */
851 rsb->sb_inprogress = 0;
852 sb_inoalignmt = sb->sb_inoalignmt;
853 sb_unit = sb->sb_unit;
854 sb_width = sb->sb_width;
855 }
856
857 out_free_list:
858 free_geo(list);
859 free(sb);
860 return retval;
861 }