Skip to content

Commit f1a1e2c

Browse files
perseantperseant
authored andcommitted
Look at the superblock magic number before attempting to read the address
of the first alternate out of it. If the magic number doesn't match one of our known numbers, the primary is invalid and we have no way of locating an alternate. Bail out in that case. Makes fsck_lfs work for LFS64, at least in the minimal sense of being able to report that a newly created LFS64 has no errors.
1 parent 5812a10 commit f1a1e2c

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

sbin/fsck_lfs/lfs.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: lfs.c,v 1.75 2020/04/03 19:36:33 joerg Exp $ */
1+
/* $NetBSD: lfs.c,v 1.76 2025/10/12 01:44:26 perseant Exp $ */
22
/*-
33
* Copyright (c) 2003 The NetBSD Foundation, Inc.
44
* All rights reserved.
@@ -449,8 +449,12 @@ check_sb(struct lfs *fs)
449449
break;
450450
default:
451451
printf("Superblock magic number (0x%lx) does not match "
452-
"expected 0x%lx\n", (unsigned long) magic,
453-
(unsigned long) LFS_MAGIC);
452+
"any of the expected 0x%lx, 0x%lx, 0x%lx or 0x%lx\n",
453+
(unsigned long)magic,
454+
(unsigned long)LFS_MAGIC,
455+
(unsigned long)LFS_MAGIC_SWAPPED,
456+
(unsigned long)LFS64_MAGIC,
457+
(unsigned long)LFS64_MAGIC_SWAPPED);
454458
return 1;
455459
}
456460

@@ -508,14 +512,35 @@ lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
508512
bp->b_flags |= B_INVAL;
509513
brelse(bp, 0);
510514

515+
/*
516+
* Look at the magic number before validating the rest
517+
* of the superblock. If the magic number is bad, too,
518+
* we don't know where to look for an alternate superblock
519+
* either; so bail out now.
520+
*/
521+
switch (fs->lfs_dlfs_u.u_32.dlfs_magic) {
522+
case LFS_MAGIC:
523+
case LFS_MAGIC_SWAPPED:
524+
break;
525+
526+
case LFS64_MAGIC:
527+
case LFS64_MAGIC_SWAPPED:
528+
fs->lfs_is64 = true;
529+
break;
530+
531+
default:
532+
return NULL;
533+
}
534+
535+
511536
dev_bsize = lfs_sb_getfsize(fs) >> lfs_sb_getfsbtodb(fs);
512537

513538
if (tryalt) {
514539
(void)bread(devvp, LFS_FSBTODB(fs, lfs_sb_getsboff(fs, 1)),
515540
LFS_SBPAD, 0, &bp);
516541
altfs = ecalloc(1, sizeof(*altfs));
517542
memcpy(&altfs->lfs_dlfs_u, bp->b_data,
518-
sizeof(struct dlfs));
543+
sizeof(altfs->lfs_dlfs_u));
519544
altfs->lfs_devvp = devvp;
520545
bp->b_flags |= B_INVAL;
521546
brelse(bp, 0);
@@ -667,7 +692,7 @@ try_verify(struct lfs *osb, struct uvnode *devvp, daddr_t goal, int debug)
667692
break;
668693
}
669694
if (debug)
670-
pwarn("summary good: 0x%x/%d\n", (uintmax_t)daddr,
695+
pwarn("summary good: 0x%jx/%d\n", (uintmax_t)daddr,
671696
(int)lfs_ss_getserial(osb, sp));
672697
assert (bc > 0);
673698
odaddr = daddr;

0 commit comments

Comments
 (0)