Home | History | Annotate | Line # | Download | only in installboot
ffs.c revision 1.1
      1 /*	$NetBSD: ffs.c,v 1.1 2002/04/19 07:08:51 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Fredette.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(__lint)
     41 __RCSID("$NetBSD: ffs.c,v 1.1 2002/04/19 07:08:51 lukem Exp $");
     42 #endif	/* !__lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/mount.h>
     46 
     47 #include <assert.h>
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <fcntl.h>
     51 #include <stdarg.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 #include "installboot.h"
     58 
     59 #undef DIRBLKSIZ
     60 
     61 #include <ufs/ufs/dinode.h>
     62 #include <ufs/ufs/dir.h>
     63 #include <ufs/ffs/fs.h>
     64 #include <ufs/ffs/ffs_extern.h>
     65 #include <ufs/ufs/ufs_bswap.h>
     66 
     67 /* This reads a disk block from the filesystem. */
     68 static int
     69 ffs_read_disk_block(ib_params *params, uint32_t blkno, int size, char *blk)
     70 {
     71 	int	rv;
     72 
     73 	assert(params->filesystem != NULL);
     74 	assert(params->fsfd != -1);
     75 	assert(blkno > 0);
     76 	assert(size > 0);
     77 	assert(blk != NULL);
     78 
     79 	rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
     80 	if (rv == -1) {
     81 		warn("Reading block %d in `%s'", blkno, params->filesystem);
     82 		return (0);
     83 	} else if (rv != size) {
     84 		warnx("Reading block %d in `%s': short read", blkno,
     85 		    params->filesystem);
     86 		return (0);
     87 	}
     88 
     89 	return (1);
     90 }
     91 
     92 /*
     93  * This iterates over the data blocks belonging to an inode,
     94  * making a callback each iteration with the disk block number
     95  * and the size.
     96  */
     97 static int
     98 ffs_find_disk_blocks(ib_params *params, uint32_t ino,
     99 	int (*callback)(ib_params *, void *, uint32_t, uint32_t, int),
    100 	void *state)
    101 {
    102 	char		sbbuf[SBSIZE];
    103 	struct fs	*fs;
    104 	int		needswap;
    105 	char		inodebuf[MAXBSIZE];
    106 	struct dinode	*inode;
    107 	int		level_i;
    108 	ufs_daddr_t	blk, lblk, nblk;
    109 	int		rv;
    110 #define LEVELS 4
    111 	struct {
    112 		ufs_daddr_t	*blknums;
    113 		unsigned long	blkcount;
    114 		char		diskbuf[MAXBSIZE];
    115 	} level[LEVELS];
    116 
    117 	/* Read the superblock. */
    118 	if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf))
    119 		return (0);
    120 	fs = (struct fs *)sbbuf;
    121 	needswap = 0;
    122 	if (fs->fs_magic == htole32(FS_MAGIC)) {
    123 #if BYTE_ORDER == BIG_ENDIAN
    124 		needswap = 1;
    125 #endif
    126 	} else if (fs->fs_magic == htobe32(FS_MAGIC)) {
    127 #if BYTE_ORDER == LITTLE_ENDIAN
    128 		needswap = 1;
    129 #endif
    130 	} else
    131 		return (0);
    132 	if (needswap)
    133 		ffs_sb_swap(fs, fs);
    134 
    135 	/* Sanity check the superblock. */
    136 	if (fs->fs_magic != FS_MAGIC) {
    137 		warnx("Bad superblock magic number in `%s'",
    138 		    params->filesystem);
    139 		return (0);
    140 	}
    141 	if (fs->fs_inopb <= 0) {
    142 		warnx("Bad inopb %d in superblock in `%s'",
    143 		    fs->fs_inopb, params->filesystem);
    144 		return (0);
    145 	}
    146 
    147 	/* Read the inode. */
    148 	if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)),
    149 		fs->fs_bsize, inodebuf))
    150 		return (0);
    151 	inode = (struct dinode *)inodebuf;
    152 	inode += ino_to_fsbo(fs, ino);
    153 	if (needswap)
    154 		ffs_dinode_swap(inode, inode);
    155 
    156 	/* Get the block count and initialize for our block walk. */
    157 	nblk = howmany(inode->di_size, fs->fs_bsize);
    158 	lblk = 0;
    159 	level_i = 0;
    160 	level[0].blknums = &inode->di_db[0];
    161 	level[0].blkcount = NDADDR;
    162 	level[1].blknums = &inode->di_ib[0];
    163 	level[1].blkcount = 1;
    164 	level[2].blknums = &inode->di_ib[1];
    165 	level[2].blkcount = 1;
    166 	level[3].blknums = &inode->di_ib[2];
    167 	level[3].blkcount = 1;
    168 
    169 	/* Walk the data blocks. */
    170 	while (nblk > 0) {
    171 
    172 		/*
    173 		 * If there are no more blocks at this indirection
    174 		 * level, move up one indirection level and loop.
    175 		 */
    176 		if (level[level_i].blkcount == 0) {
    177 			if (++level_i == LEVELS)
    178 				break;
    179 			continue;
    180 		}
    181 
    182 		/* Get the next block at this level. */
    183 		blk = *(level[level_i].blknums++);
    184 		level[level_i].blkcount--;
    185 		if (needswap)
    186 			blk = bswap32(blk);
    187 
    188 #if 0
    189 		fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
    190 		    level_i);
    191 #endif
    192 
    193 		/*
    194 		 * If we're not at the direct level, descend one
    195 		 * level, read in that level's new block list,
    196 		 * and loop.
    197 		 */
    198 		if (level_i > 0) {
    199 			level_i--;
    200 			if (blk == 0)
    201 				memset(level[level_i].diskbuf, 0, MAXBSIZE);
    202 			else if (! ffs_read_disk_block(params,
    203 				fsbtodb(fs, blk),
    204 				fs->fs_bsize, level[level_i].diskbuf))
    205 				return (0);
    206 			level[level_i].blknums =
    207 				(ufs_daddr_t *)level[level_i].diskbuf;
    208 			level[level_i].blkcount = NINDIR(fs);
    209 			continue;
    210 		}
    211 
    212 		/* blk is the next direct level block. */
    213 #if 0
    214 		fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
    215 		    fsbtodb(fs, blk), dblksize(fs, inode, lblk));
    216 #endif
    217 		rv = (*callback)(params, state,
    218 		    fsbtodb(fs, blk), dblksize(fs, inode, lblk), needswap);
    219 		lblk++;
    220 		nblk--;
    221 		if (rv != 1)
    222 			return (rv);
    223 	}
    224 
    225 	if (nblk != 0) {
    226 		warnx("Inode %d in `%s' ran out of blocks?", ino,
    227 		    params->filesystem);
    228 		return (0);
    229 	}
    230 
    231 	return (1);
    232 }
    233 
    234 /*
    235  * This callback reads a block of the root directory,
    236  * searches for an entry for the secondary bootstrap,
    237  * and saves the inode number if one is found.
    238  */
    239 static int
    240 ffs_findstage2_ino(ib_params *params, void *_ino,
    241 	uint32_t blk, uint32_t blksize, int needswap)
    242 {
    243 	char		dirbuf[MAXBSIZE];
    244 	struct direct	*de, *ede;
    245 	uint32_t	ino;
    246 
    247 	/* Skip directory holes. */
    248 	if (blk == 0)
    249 		return (1);
    250 
    251 	/* Read the directory block. */
    252 	if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
    253 		return (0);
    254 
    255 	/* Loop over the directory entries. */
    256 	de = (struct direct *)&dirbuf[0];
    257 	ede = (struct direct *)&dirbuf[blksize];
    258 	while (de < ede) {
    259 		ino = de->d_ino;
    260 		if (needswap) {
    261 			ino = bswap32(ino);
    262 			de->d_reclen = bswap16(de->d_reclen);
    263 		}
    264 		if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
    265 			*((uint32_t *)_ino) = ino;
    266 			return (2);
    267 		}
    268 		de = (struct direct *)((char *)de + de->d_reclen);
    269 	}
    270 
    271 	return (1);
    272 }
    273 
    274 struct findblks_state {
    275 	uint32_t	maxblk;
    276 	uint32_t	nblk;
    277 	ib_block	*blocks;
    278 };
    279 
    280 /* This callback records the blocks of the secondary bootstrap. */
    281 static int
    282 ffs_findstage2_blocks(ib_params *params, void *_state,
    283 	uint32_t blk, uint32_t blksize, int needswap)
    284 {
    285 	struct findblks_state *state = _state;
    286 
    287 	if (state->nblk == state->maxblk) {
    288 		warnx("Secondary bootstrap `%s' has too many blocks " \
    289 		    "(max %d)\n", params->stage2, state->maxblk);
    290 		return (0);
    291 	}
    292 	state->blocks[state->nblk].block = blk;
    293 	state->blocks[state->nblk].blocksize = blksize;
    294 	state->nblk++;
    295 	return (1);
    296 }
    297 
    298 	/* publically visible functions */
    299 
    300 int
    301 ffs_match(ib_params *params)
    302 {
    303 	char		sbbuf[SBSIZE];
    304 	struct fs	*fs;
    305 
    306 	/* Read and check the superblock. */
    307 	if (! ffs_read_disk_block(params, SBLOCK, SBSIZE, sbbuf))
    308 		return (0);
    309 	fs = (struct fs *)sbbuf;
    310 	if (fs->fs_magic == htole32(FS_MAGIC) ||
    311 	    fs->fs_magic == htobe32(FS_MAGIC))
    312 		return (1);
    313 
    314 	return (0);
    315 }
    316 
    317 int
    318 ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
    319 {
    320 	int			rv;
    321 	uint32_t		ino;
    322 	struct findblks_state	state;
    323 
    324 	assert (params->stage2 != NULL);
    325 
    326 	/* The secondary bootstrap must be clearly in /. */
    327 	if (params->stage2[0] == '/')
    328 		params->stage2++;
    329 	if (strchr(params->stage2, '/') != NULL) {
    330 		warnx("The secondary bootstrap `%s' must be in /",
    331 		    params->stage2);
    332 		return (0);
    333 	}
    334 
    335 	/* Get the inode number of the secondary bootstrap. */
    336 	rv = ffs_find_disk_blocks(params, ROOTINO, ffs_findstage2_ino, &ino);
    337 	if (rv != 2)
    338 		return (0);
    339 
    340 	/* Record the disk blocks of the secondary bootstrap. */
    341 	state.maxblk = *maxblk;
    342 	state.nblk = 0;
    343 	state.blocks = blocks;
    344 	rv = ffs_find_disk_blocks(params, ino, ffs_findstage2_blocks, &state);
    345 	if (! rv)
    346 		return (0);
    347 
    348 	*maxblk = state.nblk;
    349 	return (1);
    350 }
    351