/*-
* 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.
*
* 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.
*/
rv = pread(params->fsfd, blk, size, blkno * params->sectorsize);
if (rv == -1) {
warn("Reading block %llu in `%s'",
(unsigned long long)blkno, params->filesystem);
return (0);
} else if (rv != size) {
warnx("Reading block %llu in `%s': short read",
(unsigned long long)blkno, 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_ufs1(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 ufs1_dinode *inode;
int level_i;
int32_t blk, lblk, nblk;
int rv;
#define LEVELS 4
struct {
int32_t *blknums;
unsigned long blkcount;
char diskbuf[MAXBSIZE];
} level[LEVELS];
/*
* 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 = bswap32(blk);
/*
* 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,
FFS_FSBTODB(fs, blk) + params->fstype->offset,
fs->fs_bsize, level[level_i].diskbuf))
return (0);
/* XXX ondisk32 */
level[level_i].blknums =
(int32_t *)level[level_i].diskbuf;
level[level_i].blkcount = FFS_NINDIR(fs);
continue;
}
/* blk is the next direct level block. */
#if 0
fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
FFS_FSBTODB(fs, blk), ffs_sblksize(fs, inode->di_size, lblk));
#endif
rv = (*callback)(params, state,
FFS_FSBTODB(fs, blk) + params->fstype->offset,
ffs_sblksize(fs, (int64_t)inode->di_size, lblk));
lblk++;
nblk--;
if (rv != 1)
return (rv);
}
if (nblk != 0) {
warnx("Inode %llu in `%s' ran out of blocks?",
(unsigned long long)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];
/*
* 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 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,
FFS_FSBTODB(fs, blk) + params->fstype->offset,
fs->fs_bsize, level[level_i].diskbuf))
return (0);
level[level_i].blknums =
(int64_t *)level[level_i].diskbuf;
level[level_i].blkcount = FFS_NINDIR(fs);
continue;
}
/* blk is the next direct level block. */
#if 0
fprintf(stderr, "ino %lu db %llu blksize %lu\n", ino,
FFS_FSBTODB(fs, blk), ffs_sblksize(fs, inode->di_size, lblk));
#endif
rv = (*callback)(params, state,
FFS_FSBTODB(fs, blk) + params->fstype->offset,
ffs_sblksize(fs, (int64_t)inode->di_size, lblk));
lblk++;
nblk--;
if (rv != 1)
return (rv);
}
if (nblk != 0) {
warnx("Inode %llu in `%s' ran out of blocks?",
(unsigned long long)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,
uint64_t blk, uint32_t blksize)
{
char dirbuf[MAXBSIZE];
struct direct *de, *ede;
uint32_t ino;
/* This callback records the blocks of the secondary bootstrap. */
static int
ffs_findstage2_blocks(ib_params *params, void *_state,
uint64_t blk, uint32_t blksize)
{
struct findblks_state *state = _state;
if (params->flags & IB_STAGE2START)
return (hardcode_stage2(params, maxblk, blocks));
/* 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);
warnx("(Path must be relative to the file system in `%s')",
params->filesystem);
return (0);
}
/* Get the inode number of the secondary bootstrap. */
if (is_ufs2)
rv = ffs_find_disk_blocks_ufs2(params, UFS_ROOTINO,
ffs_findstage2_ino, &ino);
else
rv = ffs_find_disk_blocks_ufs1(params, UFS_ROOTINO,
ffs_findstage2_ino, &ino);
if (rv != 2) {
warnx("Could not find secondary bootstrap `%s' in `%s'",
params->stage2, params->filesystem);
warnx("(Path must be relative to the file system in `%s')",
params->filesystem);
return (0);
}
/* Record the disk blocks of the secondary bootstrap. */
state.maxblk = *maxblk;
state.nblk = 0;
state.blocks = blocks;
if (is_ufs2)
rv = ffs_find_disk_blocks_ufs2(params, ino,
ffs_findstage2_blocks, &state);
else
rv = ffs_find_disk_blocks_ufs1(params, ino,
ffs_findstage2_blocks, &state);
if (! rv) {
return (0);
}