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