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