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