2003-05-30 13:22:50 +04:00
|
|
|
/* $NetBSD: ffs.c,v 1.12 2003/05/30 09:22:50 dsl Exp $ */
|
2002-04-19 11:08:51 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Matt Fredette.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(__RCSID) && !defined(__lint)
|
2003-05-30 13:22:50 +04:00
|
|
|
__RCSID("$NetBSD: ffs.c,v 1.12 2003/05/30 09:22:50 dsl Exp $");
|
2002-04-19 11:08:51 +04:00
|
|
|
#endif /* !__lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "installboot.h"
|
|
|
|
|
|
|
|
#undef DIRBLKSIZ
|
|
|
|
|
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/dir.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
#include <ufs/ffs/ffs_extern.h>
|
|
|
|
#include <ufs/ufs/ufs_bswap.h>
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
static int ffs_read_disk_block(ib_params *, uint64_t, int, char *);
|
|
|
|
static int ffs_find_disk_blocks_ufs1(ib_params *, ino_t,
|
|
|
|
int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
|
|
|
|
static int ffs_find_disk_blocks_ufs2(ib_params *, ino_t,
|
|
|
|
int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
|
|
|
|
static int ffs_findstage2_ino(ib_params *, void *, uint64_t, uint32_t);
|
|
|
|
static int ffs_findstage2_blocks(ib_params *, void *, uint64_t, uint32_t);
|
|
|
|
|
|
|
|
static int is_ufs2;
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
|
|
|
|
|
2002-04-19 11:08:51 +04:00
|
|
|
/* This reads a disk block from the filesystem. */
|
|
|
|
static int
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_read_disk_block(ib_params *params, uint64_t blkno, int size, char *blk)
|
2002-04-19 11:08:51 +04:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(params != NULL);
|
|
|
|
assert(blk != NULL);
|
2002-04-19 11:08:51 +04:00
|
|
|
assert(params->filesystem != NULL);
|
|
|
|
assert(params->fsfd != -1);
|
2003-04-03 02:02:56 +04:00
|
|
|
assert(blkno >= 0);
|
2002-04-19 11:08:51 +04:00
|
|
|
assert(size > 0);
|
|
|
|
assert(blk != NULL);
|
|
|
|
|
|
|
|
rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
|
|
|
|
if (rv == -1) {
|
2003-04-03 03:02:29 +04:00
|
|
|
warn("Reading block %llu in `%s'",
|
|
|
|
(unsigned long long)blkno, params->filesystem);
|
2002-04-19 11:08:51 +04:00
|
|
|
return (0);
|
|
|
|
} else if (rv != size) {
|
2003-04-03 03:02:29 +04:00
|
|
|
warnx("Reading block %llu in `%s': short read",
|
|
|
|
(unsigned long long)blkno, params->filesystem);
|
2002-04-19 11:08:51 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This iterates over the data blocks belonging to an inode,
|
|
|
|
* making a callback each iteration with the disk block number
|
|
|
|
* and the size.
|
|
|
|
*/
|
|
|
|
static int
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_find_disk_blocks_ufs1(ib_params *params, ino_t ino,
|
|
|
|
int (*callback)(ib_params *, void *, uint64_t, uint32_t),
|
2002-04-19 11:08:51 +04:00
|
|
|
void *state)
|
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
char sbbuf[SBLOCKSIZE];
|
2002-04-19 11:08:51 +04:00
|
|
|
struct fs *fs;
|
|
|
|
char inodebuf[MAXBSIZE];
|
2003-04-02 14:39:19 +04:00
|
|
|
struct ufs1_dinode *inode;
|
2002-04-19 11:08:51 +04:00
|
|
|
int level_i;
|
2003-04-02 14:39:19 +04:00
|
|
|
int32_t blk, lblk, nblk;
|
2002-04-19 11:08:51 +04:00
|
|
|
int rv;
|
|
|
|
#define LEVELS 4
|
|
|
|
struct {
|
2003-01-25 00:55:02 +03:00
|
|
|
int32_t *blknums;
|
2002-04-19 11:08:51 +04:00
|
|
|
unsigned long blkcount;
|
|
|
|
char diskbuf[MAXBSIZE];
|
|
|
|
} level[LEVELS];
|
|
|
|
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(params != NULL);
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
assert(params->fstype != NULL);
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(callback != NULL);
|
|
|
|
assert(state != NULL);
|
|
|
|
|
2002-04-19 11:08:51 +04:00
|
|
|
/* Read the superblock. */
|
2003-04-05 15:30:53 +04:00
|
|
|
if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
|
2003-04-02 14:39:19 +04:00
|
|
|
sbbuf))
|
2002-04-19 11:08:51 +04:00
|
|
|
return (0);
|
|
|
|
fs = (struct fs *)sbbuf;
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
if (params->fstype->needswap)
|
2002-04-19 11:08:51 +04:00
|
|
|
ffs_sb_swap(fs, fs);
|
|
|
|
|
|
|
|
if (fs->fs_inopb <= 0) {
|
|
|
|
warnx("Bad inopb %d in superblock in `%s'",
|
|
|
|
fs->fs_inopb, params->filesystem);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the inode. */
|
|
|
|
if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)),
|
|
|
|
fs->fs_bsize, inodebuf))
|
|
|
|
return (0);
|
2003-04-02 14:39:19 +04:00
|
|
|
inode = (struct ufs1_dinode *)inodebuf;
|
2002-04-19 11:08:51 +04:00
|
|
|
inode += ino_to_fsbo(fs, ino);
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
if (params->fstype->needswap)
|
2003-04-02 14:39:19 +04:00
|
|
|
ffs_dinode1_swap(inode, inode);
|
2002-04-19 11:08:51 +04:00
|
|
|
|
|
|
|
/* Get the block count and initialize for our block walk. */
|
|
|
|
nblk = howmany(inode->di_size, fs->fs_bsize);
|
|
|
|
lblk = 0;
|
|
|
|
level_i = 0;
|
|
|
|
level[0].blknums = &inode->di_db[0];
|
|
|
|
level[0].blkcount = NDADDR;
|
|
|
|
level[1].blknums = &inode->di_ib[0];
|
|
|
|
level[1].blkcount = 1;
|
|
|
|
level[2].blknums = &inode->di_ib[1];
|
|
|
|
level[2].blkcount = 1;
|
|
|
|
level[3].blknums = &inode->di_ib[2];
|
|
|
|
level[3].blkcount = 1;
|
|
|
|
|
|
|
|
/* Walk the data blocks. */
|
|
|
|
while (nblk > 0) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are no more blocks at this indirection
|
|
|
|
* level, move up one indirection level and loop.
|
|
|
|
*/
|
|
|
|
if (level[level_i].blkcount == 0) {
|
|
|
|
if (++level_i == LEVELS)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next block at this level. */
|
|
|
|
blk = *(level[level_i].blknums++);
|
|
|
|
level[level_i].blkcount--;
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
if (params->fstype->needswap)
|
2002-04-19 11:08:51 +04:00
|
|
|
blk = bswap32(blk);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
|
|
|
|
level_i);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're not at the direct level, descend one
|
|
|
|
* level, read in that level's new block list,
|
|
|
|
* and loop.
|
|
|
|
*/
|
|
|
|
if (level_i > 0) {
|
|
|
|
level_i--;
|
|
|
|
if (blk == 0)
|
|
|
|
memset(level[level_i].diskbuf, 0, MAXBSIZE);
|
|
|
|
else if (! ffs_read_disk_block(params,
|
|
|
|
fsbtodb(fs, blk),
|
|
|
|
fs->fs_bsize, level[level_i].diskbuf))
|
|
|
|
return (0);
|
2003-01-25 00:55:02 +03:00
|
|
|
/* XXX ondisk32 */
|
2002-04-19 11:08:51 +04:00
|
|
|
level[level_i].blknums =
|
2003-01-25 00:55:02 +03:00
|
|
|
(int32_t *)level[level_i].diskbuf;
|
2002-04-19 11:08:51 +04:00
|
|
|
level[level_i].blkcount = NINDIR(fs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* blk is the next direct level block. */
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
|
2003-04-02 14:39:19 +04:00
|
|
|
fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
|
|
|
|
#endif
|
|
|
|
rv = (*callback)(params, state,
|
|
|
|
fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
|
|
|
|
lblk++;
|
|
|
|
nblk--;
|
|
|
|
if (rv != 1)
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nblk != 0) {
|
|
|
|
warnx("Inode %d in `%s' ran out of blocks?", ino,
|
|
|
|
params->filesystem);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This iterates over the data blocks belonging to an inode,
|
|
|
|
* making a callback each iteration with the disk block number
|
|
|
|
* and the size.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ffs_find_disk_blocks_ufs2(ib_params *params, ino_t ino,
|
|
|
|
int (*callback)(ib_params *, void *, uint64_t, uint32_t),
|
|
|
|
void *state)
|
|
|
|
{
|
|
|
|
char sbbuf[SBLOCKSIZE];
|
|
|
|
struct fs *fs;
|
|
|
|
char inodebuf[MAXBSIZE];
|
|
|
|
struct ufs2_dinode *inode;
|
|
|
|
int level_i;
|
|
|
|
int64_t blk, lblk, nblk;
|
|
|
|
int rv;
|
|
|
|
#define LEVELS 4
|
|
|
|
struct {
|
|
|
|
int64_t *blknums;
|
|
|
|
unsigned long blkcount;
|
|
|
|
char diskbuf[MAXBSIZE];
|
|
|
|
} level[LEVELS];
|
|
|
|
|
|
|
|
assert(params != NULL);
|
|
|
|
assert(params->fstype != NULL);
|
|
|
|
assert(callback != NULL);
|
|
|
|
assert(state != NULL);
|
|
|
|
|
|
|
|
/* Read the superblock. */
|
2003-04-05 15:30:53 +04:00
|
|
|
if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
|
2003-04-02 14:39:19 +04:00
|
|
|
sbbuf))
|
|
|
|
return (0);
|
|
|
|
fs = (struct fs *)sbbuf;
|
|
|
|
if (params->fstype->needswap)
|
|
|
|
ffs_sb_swap(fs, fs);
|
|
|
|
|
|
|
|
if (fs->fs_inopb <= 0) {
|
|
|
|
warnx("Bad inopb %d in superblock in `%s'",
|
|
|
|
fs->fs_inopb, params->filesystem);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the inode. */
|
|
|
|
if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)),
|
|
|
|
fs->fs_bsize, inodebuf))
|
|
|
|
return (0);
|
|
|
|
inode = (struct ufs2_dinode *)inodebuf;
|
|
|
|
inode += ino_to_fsbo(fs, ino);
|
|
|
|
if (params->fstype->needswap)
|
|
|
|
ffs_dinode2_swap(inode, inode);
|
|
|
|
|
|
|
|
/* Get the block count and initialize for our block walk. */
|
|
|
|
nblk = howmany(inode->di_size, fs->fs_bsize);
|
|
|
|
lblk = 0;
|
|
|
|
level_i = 0;
|
|
|
|
level[0].blknums = &inode->di_db[0];
|
|
|
|
level[0].blkcount = NDADDR;
|
|
|
|
level[1].blknums = &inode->di_ib[0];
|
|
|
|
level[1].blkcount = 1;
|
|
|
|
level[2].blknums = &inode->di_ib[1];
|
|
|
|
level[2].blkcount = 1;
|
|
|
|
level[3].blknums = &inode->di_ib[2];
|
|
|
|
level[3].blkcount = 1;
|
|
|
|
|
|
|
|
/* Walk the data blocks. */
|
|
|
|
while (nblk > 0) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are no more blocks at this indirection
|
|
|
|
* level, move up one indirection level and loop.
|
|
|
|
*/
|
|
|
|
if (level[level_i].blkcount == 0) {
|
|
|
|
if (++level_i == LEVELS)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next block at this level. */
|
|
|
|
blk = *(level[level_i].blknums++);
|
|
|
|
level[level_i].blkcount--;
|
|
|
|
if (params->fstype->needswap)
|
|
|
|
blk = bswap64(blk);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "ino %lu blk %llu level %d\n", ino,
|
|
|
|
(unsigned long long)blk, level_i);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're not at the direct level, descend one
|
|
|
|
* level, read in that level's new block list,
|
|
|
|
* and loop.
|
|
|
|
*/
|
|
|
|
if (level_i > 0) {
|
|
|
|
level_i--;
|
|
|
|
if (blk == 0)
|
|
|
|
memset(level[level_i].diskbuf, 0, MAXBSIZE);
|
|
|
|
else if (! ffs_read_disk_block(params,
|
|
|
|
fsbtodb(fs, blk),
|
|
|
|
fs->fs_bsize, level[level_i].diskbuf))
|
|
|
|
return (0);
|
|
|
|
level[level_i].blknums =
|
|
|
|
(int64_t *)level[level_i].diskbuf;
|
|
|
|
level[level_i].blkcount = NINDIR(fs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* blk is the next direct level block. */
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "ino %lu db %llu blksize %lu\n", ino,
|
|
|
|
fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
|
2002-04-19 11:08:51 +04:00
|
|
|
#endif
|
|
|
|
rv = (*callback)(params, state,
|
2003-04-02 14:39:19 +04:00
|
|
|
fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
|
2002-04-19 11:08:51 +04:00
|
|
|
lblk++;
|
|
|
|
nblk--;
|
|
|
|
if (rv != 1)
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nblk != 0) {
|
|
|
|
warnx("Inode %d in `%s' ran out of blocks?", ino,
|
|
|
|
params->filesystem);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback reads a block of the root directory,
|
|
|
|
* searches for an entry for the secondary bootstrap,
|
|
|
|
* and saves the inode number if one is found.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ffs_findstage2_ino(ib_params *params, void *_ino,
|
2003-04-02 14:39:19 +04:00
|
|
|
uint64_t blk, uint32_t blksize)
|
2002-04-19 11:08:51 +04:00
|
|
|
{
|
|
|
|
char dirbuf[MAXBSIZE];
|
|
|
|
struct direct *de, *ede;
|
|
|
|
uint32_t ino;
|
|
|
|
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(params != NULL);
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
assert(params->fstype != NULL);
|
2002-05-02 17:03:32 +04:00
|
|
|
assert(params->stage2 != NULL);
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(_ino != NULL);
|
|
|
|
|
2002-04-19 11:08:51 +04:00
|
|
|
/* Skip directory holes. */
|
|
|
|
if (blk == 0)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/* Read the directory block. */
|
|
|
|
if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* Loop over the directory entries. */
|
|
|
|
de = (struct direct *)&dirbuf[0];
|
|
|
|
ede = (struct direct *)&dirbuf[blksize];
|
|
|
|
while (de < ede) {
|
|
|
|
ino = de->d_ino;
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
if (params->fstype->needswap) {
|
2002-04-19 11:08:51 +04:00
|
|
|
ino = bswap32(ino);
|
|
|
|
de->d_reclen = bswap16(de->d_reclen);
|
|
|
|
}
|
|
|
|
if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
|
|
|
|
*((uint32_t *)_ino) = ino;
|
|
|
|
return (2);
|
|
|
|
}
|
2002-05-07 16:22:23 +04:00
|
|
|
if (de->d_reclen == 0)
|
|
|
|
break;
|
2002-04-19 11:08:51 +04:00
|
|
|
de = (struct direct *)((char *)de + de->d_reclen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct findblks_state {
|
|
|
|
uint32_t maxblk;
|
|
|
|
uint32_t nblk;
|
|
|
|
ib_block *blocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This callback records the blocks of the secondary bootstrap. */
|
|
|
|
static int
|
|
|
|
ffs_findstage2_blocks(ib_params *params, void *_state,
|
2003-04-02 14:39:19 +04:00
|
|
|
uint64_t blk, uint32_t blksize)
|
2002-04-19 11:08:51 +04:00
|
|
|
{
|
|
|
|
struct findblks_state *state = _state;
|
|
|
|
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(params != NULL);
|
2002-05-02 17:03:32 +04:00
|
|
|
assert(params->stage2 != NULL);
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(_state != NULL);
|
|
|
|
|
2002-04-19 11:08:51 +04:00
|
|
|
if (state->nblk == state->maxblk) {
|
2002-05-15 13:44:54 +04:00
|
|
|
warnx("Secondary bootstrap `%s' has too many blocks (max %d)",
|
|
|
|
params->stage2, state->maxblk);
|
2002-04-19 11:08:51 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
state->blocks[state->nblk].block = blk;
|
|
|
|
state->blocks[state->nblk].blocksize = blksize;
|
|
|
|
state->nblk++;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
/*
|
|
|
|
* publically visible functions
|
|
|
|
*/
|
2002-04-19 11:08:51 +04:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
static off_t sblock_try[] = SBLOCKSEARCH;
|
|
|
|
|
2002-04-19 11:08:51 +04:00
|
|
|
int
|
|
|
|
ffs_match(ib_params *params)
|
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
char sbbuf[SBLOCKSIZE];
|
2002-04-19 11:08:51 +04:00
|
|
|
struct fs *fs;
|
2003-04-02 14:39:19 +04:00
|
|
|
int i;
|
|
|
|
off_t loc;
|
2002-04-19 11:08:51 +04:00
|
|
|
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(params != NULL);
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
assert(params->fstype != NULL);
|
2002-04-30 18:21:17 +04:00
|
|
|
|
2002-04-19 11:08:51 +04:00
|
|
|
fs = (struct fs *)sbbuf;
|
2003-04-02 14:39:19 +04:00
|
|
|
for (i = 0; sblock_try[i] != -1; i++) {
|
2003-04-05 15:30:53 +04:00
|
|
|
loc = sblock_try[i] / DEV_BSIZE;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (!ffs_read_disk_block(params, loc, SBLOCKSIZE, sbbuf))
|
|
|
|
continue;
|
|
|
|
switch (fs->fs_magic) {
|
|
|
|
case FS_UFS2_MAGIC:
|
|
|
|
is_ufs2 = 1;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FS_UFS1_MAGIC:
|
|
|
|
params->fstype->needswap = 0;
|
|
|
|
params->fstype->blocksize = fs->fs_bsize;
|
|
|
|
params->fstype->sblockloc = loc;
|
|
|
|
return (1);
|
|
|
|
case FS_UFS2_MAGIC_SWAPPED:
|
|
|
|
is_ufs2 = 1;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FS_UFS1_MAGIC_SWAPPED:
|
|
|
|
params->fstype->needswap = 1;
|
|
|
|
params->fstype->blocksize = bswap32(fs->fs_bsize);
|
|
|
|
params->fstype->sblockloc = loc;
|
|
|
|
return (1);
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
}
|
2002-04-19 11:08:51 +04:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
return (0);
|
2002-04-19 11:08:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
uint32_t ino;
|
|
|
|
struct findblks_state state;
|
|
|
|
|
2002-04-30 18:21:17 +04:00
|
|
|
assert(params != NULL);
|
|
|
|
assert(params->stage2 != NULL);
|
|
|
|
assert(maxblk != NULL);
|
|
|
|
assert(blocks != NULL);
|
2002-04-19 11:08:51 +04:00
|
|
|
|
user visible stuff:
- add `-B s2bno', to provide the starting block for the secondary bootstrap.
intended for use on platforms where the blocks of the stage2 bootstrap
are hardcoded into the stage1 bootstrap (currently: sparc, sun2)
- don't support `-b s1bno' for sparc or sun2, since the primary is always
at a fixed location on the disk.
- if `filesystem' is a regular file, use fsync(2) instead of sync(2)
code changes:
- add hardcode_stage2(), which uses -B s2bno and the size of the
provided secondary bootstrap (as an actual file, not as part of the
`filesystem' argument) to provide a sequential list of blocks from s2bno,
each block being the appropriate file system size (from
params->fstype->blocksize)
- add blocksize and needswap run-time parameters to ib_fs
- in *_match(), set params->fstype->blocksize to the underlying block size
(8KB for raw), and params->fstype->needswap as appropriate
- rename IB_STARTBLOCK to IB_STAGE1START, and add IB_STAGE2START
- use hardcode_stage2() to implement raw_findstage2() and IB_STAGE2BLOCK
support for ffs_findstage2()
- improve some comments, add some prototypes, ...
2002-05-14 10:18:50 +04:00
|
|
|
if (params->flags & IB_STAGE2START)
|
|
|
|
return (hardcode_stage2(params, maxblk, blocks));
|
|
|
|
|
2002-04-19 11:08:51 +04:00
|
|
|
/* The secondary bootstrap must be clearly in /. */
|
|
|
|
if (params->stage2[0] == '/')
|
|
|
|
params->stage2++;
|
|
|
|
if (strchr(params->stage2, '/') != NULL) {
|
|
|
|
warnx("The secondary bootstrap `%s' must be in /",
|
|
|
|
params->stage2);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the inode number of the secondary bootstrap. */
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2)
|
|
|
|
rv = ffs_find_disk_blocks_ufs2(params, ROOTINO,
|
|
|
|
ffs_findstage2_ino, &ino);
|
|
|
|
else
|
|
|
|
rv = ffs_find_disk_blocks_ufs1(params, ROOTINO,
|
|
|
|
ffs_findstage2_ino, &ino);
|
2002-05-15 13:44:54 +04:00
|
|
|
if (rv != 2) {
|
|
|
|
warnx("Could not find secondary bootstrap `%s' in `%s'",
|
|
|
|
params->stage2, params->filesystem);
|
2002-04-19 11:08:51 +04:00
|
|
|
return (0);
|
2002-05-15 13:44:54 +04:00
|
|
|
}
|
2002-04-19 11:08:51 +04:00
|
|
|
|
|
|
|
/* Record the disk blocks of the secondary bootstrap. */
|
|
|
|
state.maxblk = *maxblk;
|
|
|
|
state.nblk = 0;
|
|
|
|
state.blocks = blocks;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2)
|
|
|
|
rv = ffs_find_disk_blocks_ufs2(params, ino,
|
|
|
|
ffs_findstage2_blocks, &state);
|
2003-05-30 13:22:50 +04:00
|
|
|
else
|
2003-04-02 14:39:19 +04:00
|
|
|
rv = ffs_find_disk_blocks_ufs1(params, ino,
|
|
|
|
ffs_findstage2_blocks, &state);
|
2002-05-15 13:44:54 +04:00
|
|
|
if (! rv) {
|
2002-04-19 11:08:51 +04:00
|
|
|
return (0);
|
2002-05-15 13:44:54 +04:00
|
|
|
}
|
2002-04-19 11:08:51 +04:00
|
|
|
|
|
|
|
*maxblk = state.nblk;
|
|
|
|
return (1);
|
|
|
|
}
|