示例#1
0
/*
 * Get file system statistics.
 */
int
ext2_statfs(struct mount *mp, struct statfs *sbp)
{
	struct ext2mount *ump;
	struct m_ext2fs *fs;
	uint32_t overhead, overhead_per_group, ngdb;
	int i, ngroups;

	ump = VFSTOEXT2(mp);
	fs = ump->um_e2fs;
	if (fs->e2fs->e2fs_magic != E2FS_MAGIC)
		panic("ext2fs_statfs");

	/*
	 * Compute the overhead (FS structures)
	 */
	overhead_per_group =
	    1 /* block bitmap */ +
	    1 /* inode bitmap */ +
	    fs->e2fs_itpg;
	overhead = fs->e2fs->e2fs_first_dblock +
	    fs->e2fs_gcount * overhead_per_group;
	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
	    fs->e2fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
		for (i = 0, ngroups = 0; i < fs->e2fs_gcount; i++) {
			if (cg_has_sb(i))
				ngroups++;
		}
	} else {
		ngroups = fs->e2fs_gcount;
	}
	ngdb = fs->e2fs_gdbcount;
	if (fs->e2fs->e2fs_rev > E2FS_REV0 &&
	    fs->e2fs->e2fs_features_compat & EXT2F_COMPAT_RESIZE)
		ngdb += fs->e2fs->e2fs_reserved_ngdb;
	overhead += ngroups * (1 /* superblock */ + ngdb);

	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
	sbp->f_blocks = fs->e2fs->e2fs_bcount - overhead;
	sbp->f_bfree = fs->e2fs->e2fs_fbcount;
	sbp->f_bavail = sbp->f_bfree - fs->e2fs->e2fs_rbcount;
	sbp->f_files = fs->e2fs->e2fs_icount;
	sbp->f_ffree = fs->e2fs->e2fs_ficount;
	return (0);
}
示例#2
0
/*
 * Get file system statistics.
 */
int
ext2_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
{
        unsigned long overhead;
	struct ext2_mount *ump;
	struct ext2_sb_info *fs;
	struct ext2_super_block *es;
	int i, nsb;

	ump = VFSTOEXT2(mp);
	fs = ump->um_e2fs;
	es = fs->s_es;

	if (es->s_magic != EXT2_SUPER_MAGIC)
		panic("ext2_statfs - magic number spoiled");

	/*
	 * Compute the overhead (FS structures)
	 */
	if (es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
		nsb = 0;
		for (i = 0 ; i < fs->s_groups_count; i++)
			if (ext2_group_sparse(i))
				nsb++;
	} else
		nsb = fs->s_groups_count;
	overhead = es->s_first_data_block +
	    /* Superblocks and block group descriptors: */
	    nsb * (1 + fs->s_db_per_group) +
	    /* Inode bitmap, block bitmap, and inode table: */
	    fs->s_groups_count * (1 + 1 + fs->s_itb_per_group);

	sbp->f_bsize = EXT2_FRAG_SIZE(fs);
	sbp->f_iosize = EXT2_BLOCK_SIZE(fs);
	sbp->f_blocks = es->s_blocks_count - overhead;
	sbp->f_bfree = es->s_free_blocks_count;
	sbp->f_bavail = sbp->f_bfree - es->s_r_blocks_count;
	sbp->f_files = es->s_inodes_count;
	sbp->f_ffree = es->s_free_inodes_count;
	if (sbp != &mp->mnt_stat) {
		sbp->f_type = mp->mnt_vfc->vfc_typenum;
		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
	}
	return (0);
}
示例#3
0
void list_super2(struct ext2_super_block * sb, FILE *f)
{
	int inode_blocks_per_group;
	char buf[80], *str;
	time_t	tm;

	inode_blocks_per_group = (((sb->s_inodes_per_group *
				    EXT2_INODE_SIZE(sb)) +
				   EXT2_BLOCK_SIZE(sb) - 1) /
				  EXT2_BLOCK_SIZE(sb));
	if (sb->s_volume_name[0]) {
		memset(buf, 0, sizeof(buf));
		strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
	} else
		strcpy(buf, "<none>");
	fprintf(f, "Filesystem volume name:   %s\n", buf);
	if (sb->s_last_mounted[0]) {
		memset(buf, 0, sizeof(buf));
		strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
	} else
		strcpy(buf, "<not available>");
	fprintf(f, "Last mounted on:          %s\n", buf);
	fprintf(f, "Filesystem UUID:          %s\n", e2p_uuid2str(sb->s_uuid));
	fprintf(f, "Filesystem magic number:  0x%04X\n", sb->s_magic);
	fprintf(f, "Filesystem revision #:    %d", sb->s_rev_level);
	if (sb->s_rev_level == EXT2_GOOD_OLD_REV) {
		fprintf(f, " (original)\n");
#ifdef EXT2_DYNAMIC_REV
	} else if (sb->s_rev_level == EXT2_DYNAMIC_REV) {
		fprintf(f, " (dynamic)\n");
#endif
	} else
		fprintf(f, " (unknown)\n");
	print_features(sb, f);
	print_super_flags(sb, f);
	print_mntopts(sb, f);
	fprintf(f, "Filesystem state:        ");
	print_fs_state (f, sb->s_state);
	fprintf(f, "\n");
	fprintf(f, "Errors behavior:          ");
	print_fs_errors(f, sb->s_errors);
	fprintf(f, "\n");
	str = e2p_os2string(sb->s_creator_os);
	fprintf(f, "Filesystem OS type:       %s\n", str);
	free(str);
	fprintf(f, "Inode count:              %u\n", sb->s_inodes_count);
	fprintf(f, "Block count:              %u\n", sb->s_blocks_count);
	fprintf(f, "Reserved block count:     %u\n", sb->s_r_blocks_count);
	fprintf(f, "Free blocks:              %u\n", sb->s_free_blocks_count);
	fprintf(f, "Free inodes:              %u\n", sb->s_free_inodes_count);
	fprintf(f, "First block:              %u\n", sb->s_first_data_block);
	fprintf(f, "Block size:               %u\n", EXT2_BLOCK_SIZE(sb));
	fprintf(f, "Fragment size:            %u\n", EXT2_FRAG_SIZE(sb));
	if (sb->s_reserved_gdt_blocks)
		fprintf(f, "Reserved GDT blocks:      %u\n", 
			sb->s_reserved_gdt_blocks);
	fprintf(f, "Blocks per group:         %u\n", sb->s_blocks_per_group);
	fprintf(f, "Fragments per group:      %u\n", sb->s_frags_per_group);
	fprintf(f, "Inodes per group:         %u\n", sb->s_inodes_per_group);
	fprintf(f, "Inode blocks per group:   %u\n", inode_blocks_per_group);
	if (sb->s_raid_stride)
		fprintf(f, "RAID stride:              %u\n",
			sb->s_raid_stride);
	if (sb->s_raid_stripe_width)
		fprintf(f, "RAID stripe width:        %u\n",
			sb->s_raid_stripe_width);
	if (sb->s_first_meta_bg)
		fprintf(f, "First meta block group:   %u\n",
			sb->s_first_meta_bg);
	if (sb->s_log_groups_per_flex)
		fprintf(f, "Flex block group size:    %u\n",
			1 << sb->s_log_groups_per_flex);
	if (sb->s_mkfs_time) {
		tm = sb->s_mkfs_time;
		fprintf(f, "Filesystem created:       %s", ctime(&tm));
	}
	tm = sb->s_mtime;
	fprintf(f, "Last mount time:          %s",
		sb->s_mtime ? ctime(&tm) : "n/a\n");
	tm = sb->s_wtime;
	fprintf(f, "Last write time:          %s", ctime(&tm));
	fprintf(f, "Mount count:              %u\n", sb->s_mnt_count);
	fprintf(f, "Maximum mount count:      %d\n", sb->s_max_mnt_count);
	tm = sb->s_lastcheck;
	fprintf(f, "Last checked:             %s", ctime(&tm));
	fprintf(f, "Check interval:           %u (%s)\n", sb->s_checkinterval,
	       interval_string(sb->s_checkinterval));
	if (sb->s_checkinterval)
	{
		time_t next;

		next = sb->s_lastcheck + sb->s_checkinterval;
		fprintf(f, "Next check after:         %s", ctime(&next));
	}
	fprintf(f, "Reserved blocks uid:      ");
	print_user(sb->s_def_resuid, f);
	fprintf(f, "Reserved blocks gid:      ");
	print_group(sb->s_def_resgid, f);
	if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
		fprintf(f, "First inode:              %d\n", sb->s_first_ino);
		fprintf(f, "Inode size:	          %d\n", sb->s_inode_size);
		if (sb->s_min_extra_isize)
			fprintf(f, "Required extra isize:     %d\n", 
				sb->s_min_extra_isize);
		if (sb->s_want_extra_isize)
			fprintf(f, "Desired extra isize:      %d\n", 
				sb->s_want_extra_isize);
	}
	if (!e2p_is_null_uuid(sb->s_journal_uuid))
		fprintf(f, "Journal UUID:             %s\n",
			e2p_uuid2str(sb->s_journal_uuid));
	if (sb->s_journal_inum)
		fprintf(f, "Journal inode:            %u\n",
			sb->s_journal_inum);
	if (sb->s_journal_dev)
		fprintf(f, "Journal device:	          0x%04x\n",
			sb->s_journal_dev);
	if (sb->s_last_orphan)
		fprintf(f, "First orphan inode:       %u\n",
			sb->s_last_orphan);
	if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
	    sb->s_def_hash_version)
		fprintf(f, "Default directory hash:   %s\n",
			e2p_hash2string(sb->s_def_hash_version));
	if (!e2p_is_null_uuid(sb->s_hash_seed))
		fprintf(f, "Directory Hash Seed:      %s\n",
			e2p_uuid2str(sb->s_hash_seed));
	if (sb->s_jnl_backup_type) {
		fprintf(f, "Journal backup:           ");
		switch (sb->s_jnl_backup_type) {
		case 1:
			fprintf(f, "inode blocks\n");
			break;
		default:
			fprintf(f, "type %u\n", sb->s_jnl_backup_type);
		}
	}
}
示例#4
0
/*
 *  Note: if superblock is non-zero, block-size must also be non-zero.
 * 	Superblock and block_size can be zero to use the default size.
 *
 * Valid flags for ext2fs_open()
 *
 * 	EXT2_FLAG_RW	- Open the filesystem for read/write.
 * 	EXT2_FLAG_FORCE - Open the filesystem even if some of the
 *				features aren't supported.
 *	EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
 */
errcode_t ext2fs_open2(const char *name, const char *io_options,
		       int flags, int superblock,
		       unsigned int block_size, io_manager manager,
		       ext2_filsys *ret_fs)
{
	ext2_filsys	fs;
	errcode_t	retval;
	unsigned long	i, first_meta_bg;
	__u32		features;
	int		groups_per_block, blocks_per_group, io_flags;
	blk_t		group_block, blk;
	char		*dest, *cp;
#ifdef WORDS_BIGENDIAN
	struct ext2_group_desc *gdp;
	int		j;
#endif

	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);

	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
	if (retval)
		return retval;

	memset(fs, 0, sizeof(struct struct_ext2_filsys));
	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
	fs->flags = flags;
	/* don't overwrite sb backups unless flag is explicitly cleared */
	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
	fs->umask = 022;
	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
	if (retval)
		goto cleanup;
	strcpy(fs->device_name, name);
	cp = strchr(fs->device_name, '?');
	if (!io_options && cp) {
		*cp++ = 0;
		io_options = cp;
	}

	io_flags = 0;
	if (flags & EXT2_FLAG_RW)
		io_flags |= IO_FLAG_RW;
	if (flags & EXT2_FLAG_EXCLUSIVE)
		io_flags |= IO_FLAG_EXCLUSIVE;
	if (flags & EXT2_FLAG_DIRECT_IO)
		io_flags |= IO_FLAG_DIRECT_IO;
	retval = manager->open(fs->device_name, io_flags, &fs->io);
	if (retval)
		goto cleanup;
	if (io_options &&
	    (retval = io_channel_set_options(fs->io, io_options)))
		goto cleanup;
	fs->image_io = fs->io;
	fs->io->app_data = fs;
	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
	if (retval)
		goto cleanup;
	if (flags & EXT2_FLAG_IMAGE_FILE) {
		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
					&fs->image_header);
		if (retval)
			goto cleanup;
		retval = io_channel_read_blk(fs->io, 0,
					     -(int)sizeof(struct ext2_image_hdr),
					     fs->image_header);
		if (retval)
			goto cleanup;
		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
			return EXT2_ET_MAGIC_E2IMAGE;
		superblock = 1;
		block_size = fs->image_header->fs_blocksize;
	}

	/*
	 * If the user specifies a specific block # for the
	 * superblock, then he/she must also specify the block size!
	 * Otherwise, read the master superblock located at offset
	 * SUPERBLOCK_OFFSET from the start of the partition.
	 *
	 * Note: we only save a backup copy of the superblock if we
	 * are reading the superblock from the primary superblock location.
	 */
	if (superblock) {
		if (!block_size) {
			retval = EXT2_ET_INVALID_ARGUMENT;
			goto cleanup;
		}
		io_channel_set_blksize(fs->io, block_size);
		group_block = superblock;
		fs->orig_super = 0;
	} else {
		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
		superblock = 1;
		group_block = 0;
		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
		if (retval)
			goto cleanup;
	}
	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
				     fs->super);
	if (retval)
		goto cleanup;
	if (fs->orig_super)
		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);

#ifdef WORDS_BIGENDIAN
	fs->flags |= EXT2_FLAG_SWAP_BYTES;
	ext2fs_swap_super(fs->super);
#else
	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
		retval = EXT2_ET_UNIMPLEMENTED;
		goto cleanup;
	}
#endif

	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
		retval = EXT2_ET_BAD_MAGIC;
		goto cleanup;
	}
	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
		retval = EXT2_ET_REV_TOO_HIGH;
		goto cleanup;
	}

	/*
	 * Check for feature set incompatibility
	 */
	if (!(flags & EXT2_FLAG_FORCE)) {
		features = fs->super->s_feature_incompat;
#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
			features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
#endif
		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
			retval = EXT2_ET_UNSUPP_FEATURE;
			goto cleanup;
		}

		features = fs->super->s_feature_ro_compat;
#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
			features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
#endif
		if ((flags & EXT2_FLAG_RW) &&
		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
			retval = EXT2_ET_RO_UNSUPP_FEATURE;
			goto cleanup;
		}

		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
		    (fs->super->s_feature_incompat &
		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
			retval = EXT2_ET_UNSUPP_FEATURE;
			goto cleanup;
		}
	}

	if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
	    EXT2_MAX_BLOCK_LOG_SIZE) {
		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
		goto cleanup;
	}
	fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
	if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
		goto cleanup;
	}
	fs->fragsize = EXT2_FRAG_SIZE(fs->super);
	fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
				       EXT2_INODE_SIZE(fs->super) +
				       EXT2_BLOCK_SIZE(fs->super) - 1) /
				      EXT2_BLOCK_SIZE(fs->super));
	if (block_size) {
		if (block_size != fs->blocksize) {
			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
			goto cleanup;
		}
	}
	/*
	 * Set the blocksize to the filesystem's blocksize.
	 */
	io_channel_set_blksize(fs->io, fs->blocksize);

	/*
	 * If this is an external journal device, don't try to read
	 * the group descriptors, because they're not there.
	 */
	if (fs->super->s_feature_incompat &
	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
		fs->group_desc_count = 0;
		*ret_fs = fs;
		return 0;
	}

	if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
		goto cleanup;
	}

	/*
	 * Read group descriptors
	 */
	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
	if (blocks_per_group == 0 ||
	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
           EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
           fs->super->s_first_data_block >= fs->super->s_blocks_count) {
		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
		goto cleanup;
	}
	fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
					       fs->super->s_first_data_block,
					       blocks_per_group);
       if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
           fs->super->s_inodes_count) {
               retval = EXT2_ET_CORRUPT_SUPERBLOCK;
		goto cleanup;
       }
	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
					  EXT2_DESC_PER_BLOCK(fs->super));
	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
				&fs->group_desc);
	if (retval)
		goto cleanup;
	if (!group_block)
		group_block = fs->super->s_first_data_block;
	dest = (char *) fs->group_desc;
	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
		first_meta_bg = fs->super->s_first_meta_bg;
	else
		first_meta_bg = fs->desc_blocks;
	if (first_meta_bg) {
		retval = io_channel_read_blk(fs->io, group_block+1,
					     first_meta_bg, dest);
		if (retval)
			goto cleanup;
#ifdef WORDS_BIGENDIAN
		gdp = (struct ext2_group_desc *) dest;
		for (j=0; j < groups_per_block*first_meta_bg; j++)
			ext2fs_swap_group_desc(gdp++);
#endif
		dest += fs->blocksize*first_meta_bg;
	}
	for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
		blk = ext2fs_descriptor_block_loc(fs, group_block, i);
		retval = io_channel_read_blk(fs->io, blk, 1, dest);
		if (retval)
			goto cleanup;
#ifdef WORDS_BIGENDIAN
		gdp = (struct ext2_group_desc *) dest;
		for (j=0; j < groups_per_block; j++)
			ext2fs_swap_group_desc(gdp++);
#endif
		dest += fs->blocksize;
	}

	fs->stride = fs->super->s_raid_stride;

	/*
	 * If recovery is from backup superblock, Clear _UNININT flags &
	 * reset bg_itable_unused to zero
	 */
	if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
					EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
		struct ext2_group_desc *gd;
		for (i = 0, gd = fs->group_desc; i < fs->group_desc_count;
		     i++, gd++) {
			gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
			gd->bg_flags &= ~EXT2_BG_INODE_UNINIT;
			gd->bg_itable_unused = 0;
		}
		ext2fs_mark_super_dirty(fs);
	}

	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
	*ret_fs = fs;
	return 0;
cleanup:
	if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
		*ret_fs = fs;
	else
		ext2fs_free(fs);
	return retval;
}
示例#5
0
errcode_t ext2fs_initialize(const char *name, int flags,
                            struct ext2_super_block *param,
                            io_manager manager, ext2_filsys *ret_fs)
{
    ext2_filsys	fs;
    errcode_t	retval;
    struct ext2_super_block *super;
    int		frags_per_block;
    unsigned int	rem;
    unsigned int	overhead = 0;
    unsigned int	ipg;
    dgrp_t		i;
    blk_t		numblocks;
    int		rsv_gdt;
    int		io_flags;
    char		*buf;
    char		c;

    if (!param || !param->s_blocks_count)
        return EXT2_ET_INVALID_ARGUMENT;

    retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
    if (retval)
        return retval;

    memset(fs, 0, sizeof(struct struct_ext2_filsys));
    fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
    fs->flags = flags | EXT2_FLAG_RW;
    fs->umask = 022;
#ifdef WORDS_BIGENDIAN
    fs->flags |= EXT2_FLAG_SWAP_BYTES;
#endif
    io_flags = IO_FLAG_RW;
    if (flags & EXT2_FLAG_EXCLUSIVE)
        io_flags |= IO_FLAG_EXCLUSIVE;
    retval = manager->open(name, io_flags, &fs->io);
    if (retval)
        goto cleanup;
    fs->image_io = fs->io;
    fs->io->app_data = fs;
    retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
    if (retval)
        goto cleanup;

    strcpy(fs->device_name, name);
    retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super);
    if (retval)
        goto cleanup;
    fs->super = super;

    memset(super, 0, SUPERBLOCK_SIZE);

#define set_field(field, default) (super->field = param->field ? \
				   param->field : (default))

    super->s_magic = EXT2_SUPER_MAGIC;
    super->s_state = EXT2_VALID_FS;

    set_field(s_log_block_size, 0);	/* default blocksize: 1024 bytes */
    set_field(s_log_frag_size, 0); /* default fragsize: 1024 bytes */
    set_field(s_first_data_block, super->s_log_block_size ? 0 : 1);
    set_field(s_max_mnt_count, EXT2_DFL_MAX_MNT_COUNT);
    set_field(s_errors, EXT2_ERRORS_DEFAULT);
    set_field(s_feature_compat, 0);
    set_field(s_feature_incompat, 0);
    set_field(s_feature_ro_compat, 0);
    set_field(s_first_meta_bg, 0);
    if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
        retval = EXT2_ET_UNSUPP_FEATURE;
        goto cleanup;
    }
    if (super->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
        retval = EXT2_ET_RO_UNSUPP_FEATURE;
        goto cleanup;
    }

    set_field(s_rev_level, EXT2_GOOD_OLD_REV);
    if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
        set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
        set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
    }

    set_field(s_checkinterval, EXT2_DFL_CHECKINTERVAL);
    super->s_mkfs_time = super->s_lastcheck = fs->now ? fs->now : time(NULL);

    super->s_creator_os = CREATOR_OS;

    fs->blocksize = EXT2_BLOCK_SIZE(super);
    fs->fragsize = EXT2_FRAG_SIZE(super);
    frags_per_block = fs->blocksize / fs->fragsize;

    /* default: (fs->blocksize*8) blocks/group, up to 2^16 (GDT limit) */
    set_field(s_blocks_per_group, fs->blocksize * 8);
    if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super))
        super->s_blocks_per_group = EXT2_MAX_BLOCKS_PER_GROUP(super);
    super->s_frags_per_group = super->s_blocks_per_group * frags_per_block;

    super->s_blocks_count = param->s_blocks_count;
    super->s_r_blocks_count = param->s_r_blocks_count;
    if (super->s_r_blocks_count >= param->s_blocks_count) {
        retval = EXT2_ET_INVALID_ARGUMENT;
        goto cleanup;
    }

    /*
     * If we're creating an external journal device, we don't need
     * to bother with the rest.
     */
    if (super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
        fs->group_desc_count = 0;
        ext2fs_mark_super_dirty(fs);
        *ret_fs = fs;
        return 0;
    }

retry:
    fs->group_desc_count = ext2fs_div_ceil(super->s_blocks_count -
                                           super->s_first_data_block,
                                           EXT2_BLOCKS_PER_GROUP(super));
    if (fs->group_desc_count == 0) {
        retval = EXT2_ET_TOOSMALL;
        goto cleanup;
    }
    fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
                                      EXT2_DESC_PER_BLOCK(super));

    i = fs->blocksize >= 4096 ? 1 : 4096 / fs->blocksize;
    set_field(s_inodes_count, super->s_blocks_count / i);

    /*
     * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
     * that we have enough inodes for the filesystem(!)
     */
    if (super->s_inodes_count < EXT2_FIRST_INODE(super)+1)
        super->s_inodes_count = EXT2_FIRST_INODE(super)+1;

    /*
     * There should be at least as many inodes as the user
     * requested.  Figure out how many inodes per group that
     * should be.  But make sure that we don't allocate more than
     * one bitmap's worth of inodes each group.
     */
    ipg = ext2fs_div_ceil(super->s_inodes_count, fs->group_desc_count);
    if (ipg > fs->blocksize * 8) {
        if (super->s_blocks_per_group >= 256) {
            /* Try again with slightly different parameters */
            super->s_blocks_per_group -= 8;
            super->s_blocks_count = param->s_blocks_count;
            super->s_frags_per_group = super->s_blocks_per_group *
                                       frags_per_block;
            goto retry;
        } else
            return EXT2_ET_TOO_MANY_INODES;
    }

    if (ipg > (unsigned) EXT2_MAX_INODES_PER_GROUP(super))
        ipg = EXT2_MAX_INODES_PER_GROUP(super);

ipg_retry:
    super->s_inodes_per_group = ipg;

    /*
     * Make sure the number of inodes per group completely fills
     * the inode table blocks in the descriptor.  If not, add some
     * additional inodes/group.  Waste not, want not...
     */
    fs->inode_blocks_per_group = (((super->s_inodes_per_group *
                                    EXT2_INODE_SIZE(super)) +
                                   EXT2_BLOCK_SIZE(super) - 1) /
                                  EXT2_BLOCK_SIZE(super));
    super->s_inodes_per_group = ((fs->inode_blocks_per_group *
                                  EXT2_BLOCK_SIZE(super)) /
                                 EXT2_INODE_SIZE(super));
    /*
     * Finally, make sure the number of inodes per group is a
     * multiple of 8.  This is needed to simplify the bitmap
     * splicing code.
     */
    super->s_inodes_per_group &= ~7;
    fs->inode_blocks_per_group = (((super->s_inodes_per_group *
                                    EXT2_INODE_SIZE(super)) +
                                   EXT2_BLOCK_SIZE(super) - 1) /
                                  EXT2_BLOCK_SIZE(super));

    /*
     * adjust inode count to reflect the adjusted inodes_per_group
     */
    if ((__u64)super->s_inodes_per_group * fs->group_desc_count > ~0U) {
        ipg--;
        goto ipg_retry;
    }
    super->s_inodes_count = super->s_inodes_per_group *
                            fs->group_desc_count;
    super->s_free_inodes_count = super->s_inodes_count;

    /*
     * check the number of reserved group descriptor table blocks
     */
    if (super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)
        rsv_gdt = calc_reserved_gdt_blocks(fs);
    else
        rsv_gdt = 0;
    set_field(s_reserved_gdt_blocks, rsv_gdt);
    if (super->s_reserved_gdt_blocks > EXT2_ADDR_PER_BLOCK(super)) {
        retval = EXT2_ET_RES_GDT_BLOCKS;
        goto cleanup;
    }

    /*
     * Overhead is the number of bookkeeping blocks per group.  It
     * includes the superblock backup, the group descriptor
     * backups, the inode bitmap, the block bitmap, and the inode
     * table.
     */

    overhead = (int) (2 + fs->inode_blocks_per_group);

    if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
        overhead += 1 + fs->desc_blocks + super->s_reserved_gdt_blocks;

    /* This can only happen if the user requested too many inodes */
    if (overhead > super->s_blocks_per_group)
        return EXT2_ET_TOO_MANY_INODES;

    /*
     * See if the last group is big enough to support the
     * necessary data structures.  If not, we need to get rid of
     * it.
     */
    rem = ((super->s_blocks_count - super->s_first_data_block) %
           super->s_blocks_per_group);
    if ((fs->group_desc_count == 1) && rem && (rem < overhead))
        return EXT2_ET_TOOSMALL;
    if (rem && (rem < overhead+50)) {
        super->s_blocks_count -= rem;
        goto retry;
    }

    /*
     * At this point we know how big the filesystem will be.  So
     * we can do any and all allocations that depend on the block
     * count.
     */

    retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
    if (retval)
        goto cleanup;

    sprintf(buf, "block bitmap for %s", fs->device_name);
    retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
    if (retval)
        goto cleanup;

    sprintf(buf, "inode bitmap for %s", fs->device_name);
    retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
    if (retval)
        goto cleanup;

    ext2fs_free_mem(&buf);

    retval = ext2fs_get_mem((size_t) fs->desc_blocks * fs->blocksize,
                            &fs->group_desc);
    if (retval)
        goto cleanup;

    memset(fs->group_desc, 0, (size_t) fs->desc_blocks * fs->blocksize);

    /*
     * Reserve the superblock and group descriptors for each
     * group, and fill in the correct group statistics for group.
     * Note that although the block bitmap, inode bitmap, and
     * inode table have not been allocated (and in fact won't be
     * by this routine), they are accounted for nevertheless.
     */
    super->s_free_blocks_count = 0;
    for (i = 0; i < fs->group_desc_count; i++) {
        numblocks = ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);

        super->s_free_blocks_count += numblocks;
        fs->group_desc[i].bg_free_blocks_count = numblocks;
        fs->group_desc[i].bg_free_inodes_count =
            fs->super->s_inodes_per_group;
        fs->group_desc[i].bg_used_dirs_count = 0;
    }

    c = (char) 255;
    if (((int) c) == -1) {
        super->s_flags |= EXT2_FLAGS_SIGNED_HASH;
    } else {
        super->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
    }

    ext2fs_mark_super_dirty(fs);
    ext2fs_mark_bb_dirty(fs);
    ext2fs_mark_ib_dirty(fs);

    io_channel_set_blksize(fs->io, fs->blocksize);

    *ret_fs = fs;
    return 0;
cleanup:
    ext2fs_free(fs);
    return retval;
}
示例#6
0
文件: openfs.c 项目: K0T0LI/busybox
/*
 *  Note: if superblock is non-zero, block-size must also be non-zero.
 * 	Superblock and block_size can be zero to use the default size.
 *
 * Valid flags for ext2fs_open()
 * 
 * 	EXT2_FLAG_RW	- Open the filesystem for read/write.
 * 	EXT2_FLAG_FORCE - Open the filesystem even if some of the
 *				features aren't supported.
 *	EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
 */
errcode_t ext2fs_open2(const char *name, const char *io_options,
		       int flags, int superblock,
		       unsigned int block_size, io_manager manager, 
		       ext2_filsys *ret_fs)
{
	ext2_filsys	fs;
	errcode_t	retval;
	unsigned long	i;
	int		groups_per_block, blocks_per_group;
	blk_t		group_block, blk;
	char		*dest, *cp;
#ifdef EXT2FS_ENABLE_SWAPFS
	int j;
	struct ext2_group_desc *gdp;
#endif
	
	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);

	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
	if (retval)
		return retval;
	
	memset(fs, 0, sizeof(struct struct_ext2_filsys));
	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
	fs->flags = flags;
	fs->umask = 022;
	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
	if (retval)
		goto cleanup;
	strcpy(fs->device_name, name);
	cp = strchr(fs->device_name, '?');
	if (!io_options && cp) {
		*cp++ = 0;
		io_options = cp;
	}
		
	retval = manager->open(fs->device_name, 
			       (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
			       &fs->io);
	if (retval)
		goto cleanup;
	if (io_options && 
	    (retval = io_channel_set_options(fs->io, io_options)))
		goto cleanup;
	fs->image_io = fs->io;
	fs->io->app_data = fs;
	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
	if (retval)
		goto cleanup;
	if (flags & EXT2_FLAG_IMAGE_FILE) {
		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
					&fs->image_header);
		if (retval)
			goto cleanup;
		retval = io_channel_read_blk(fs->io, 0,
					     -(int)sizeof(struct ext2_image_hdr),
					     fs->image_header);
		if (retval)
			goto cleanup;
		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
			return EXT2_ET_MAGIC_E2IMAGE;
		superblock = 1;
		block_size = fs->image_header->fs_blocksize;
	}

	/*
	 * If the user specifies a specific block # for the
	 * superblock, then he/she must also specify the block size!
	 * Otherwise, read the master superblock located at offset
	 * SUPERBLOCK_OFFSET from the start of the partition.
	 *
	 * Note: we only save a backup copy of the superblock if we
	 * are reading the superblock from the primary superblock location.
	 */
	if (superblock) {
		if (!block_size) {
			retval = EXT2_ET_INVALID_ARGUMENT;
			goto cleanup;
		}
		io_channel_set_blksize(fs->io, block_size);
		group_block = superblock;
		fs->orig_super = 0;
	} else {
		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
		superblock = 1;
		group_block = 0;
		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
		if (retval)
			goto cleanup;
	}
	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
				     fs->super);
	if (retval)
		goto cleanup;
	if (fs->orig_super)
		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);

#ifdef EXT2FS_ENABLE_SWAPFS
	if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
	    (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
		fs->flags |= EXT2_FLAG_SWAP_BYTES;

		ext2fs_swap_super(fs->super);
	}
#endif
	
	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
		retval = EXT2_ET_BAD_MAGIC;
		goto cleanup;
	}
	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
		retval = EXT2_ET_REV_TOO_HIGH;
		goto cleanup;
	}

	/*
	 * Check for feature set incompatibility
	 */
	if (!(flags & EXT2_FLAG_FORCE)) {
		if (fs->super->s_feature_incompat &
		    ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
			retval = EXT2_ET_UNSUPP_FEATURE;
			goto cleanup;
		}
		if ((flags & EXT2_FLAG_RW) &&
		    (fs->super->s_feature_ro_compat &
		     ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
			retval = EXT2_ET_RO_UNSUPP_FEATURE;
			goto cleanup;
		}
		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
		    (fs->super->s_feature_incompat &
		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
			retval = EXT2_ET_UNSUPP_FEATURE;
			goto cleanup;
		}
	}
	
	fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
	if (fs->blocksize == 0) {
		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
		goto cleanup;
	}
	fs->fragsize = EXT2_FRAG_SIZE(fs->super);
	fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
				       EXT2_INODE_SIZE(fs->super) +
				       EXT2_BLOCK_SIZE(fs->super) - 1) /
				      EXT2_BLOCK_SIZE(fs->super));
	if (block_size) {
		if (block_size != fs->blocksize) {
			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
			goto cleanup;
		}
	}
	/*
	 * Set the blocksize to the filesystem's blocksize.
	 */
	io_channel_set_blksize(fs->io, fs->blocksize);

	/*
	 * If this is an external journal device, don't try to read
	 * the group descriptors, because they're not there.
	 */
	if (fs->super->s_feature_incompat &
	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
		fs->group_desc_count = 0;
		*ret_fs = fs;
		return 0;
	}
	
	/*
	 * Read group descriptors
	 */
	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
	if (blocks_per_group == 0 ||
	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
		goto cleanup;
	}
	fs->group_desc_count = (fs->super->s_blocks_count -
				fs->super->s_first_data_block +
				blocks_per_group - 1) / blocks_per_group;
	fs->desc_blocks = (fs->group_desc_count +
			   EXT2_DESC_PER_BLOCK(fs->super) - 1)
		/ EXT2_DESC_PER_BLOCK(fs->super);
	retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
				&fs->group_desc);
	if (retval)
		goto cleanup;
	if (!group_block)
		group_block = fs->super->s_first_data_block;
	dest = (char *) fs->group_desc;
	groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc);
	for (i=0 ; i < fs->desc_blocks; i++) {
		blk = ext2fs_descriptor_block_loc(fs, group_block, i);
		retval = io_channel_read_blk(fs->io, blk, 1, dest);
		if (retval)
			goto cleanup;
#ifdef EXT2FS_ENABLE_SWAPFS
		if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
			gdp = (struct ext2_group_desc *) dest;
			for (j=0; j < groups_per_block; j++)
				ext2fs_swap_group_desc(gdp++);
		}
#endif
		dest += fs->blocksize;
	}

	*ret_fs = fs;
	return 0;
cleanup:
	ext2fs_free(fs);
	return retval;
}