⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 课程设计
💻 C
字号:
#include <linux/module.h>
#include <linux/buffer_head.h>
#include <linux/init.h>

#include "xbfs.h"

extern struct super_operations xbfs_sops;

static int xbfs_fill_super(struct super_block *s, void *data, int silent)
{
	struct inode *inode;
	struct xbfs_sb_mem *sm;
	int i;

	sm = kzalloc(sizeof(*sm), GFP_KERNEL);
	if (!sm) {
		DPRINT("malloc super block error\n");
		return -ENOMEM;
	}
	s->s_fs_info = sm;

	if (!sb_set_blocksize(s, BLK_SIZE)) {
		DPRINT("sb_set_blocksize to %d error\n", BLK_SIZE);
		goto out_free_sd;
	}
	
	sm->bh_sb = sb_bread(s, 1);
	if (!sm->bh_sb) {
		DPRINT("sb_bread get super block error\n");
		goto out_free_sd;
	}

	sm->sb = (typeof(sm->sb))sm->bh_sb->b_data;
	if (sm->sb->magic != XBFS_MAGIC) {
		DPRINT("block device doesn't contains the xbfs filesystem\n");
		goto out_free_sd;
	}

	sm->bh_bmp = kzalloc(sm->sb->bmp_count * 4, GFP_KERNEL);
	if (!sm->bh_bmp) {
		DPRINT("malloc bitmap error\n");
		goto out_free_sd;
	}
	
	memset(sm->bh_bmp, 0, sm->sb->bmp_count * 4);
	for (i = 0; i < sm->sb->bmp_count; i++) {
		sm->bh_bmp[i] = sb_bread(s, sm->sb->bmp_start + i);
		if (!sm->bh_bmp[i]) {
			DPRINT("read bitmap block %x error\n", i + 1);
			goto out_free_bh_bmp;
		}
	}

	printk(KERN_ERR COPYRIGHT);
	DPRINT("data start: %u\n", sm->sb->data_start);
	DPRINT("data blocks: %u\n", sm->sb->data_count);
	DPRINT("bitmap start: %u\n", sm->sb->bmp_start);
	DPRINT("bitmap blocks: %u\n", sm->sb->bmp_count);
	
	s->s_op = &xbfs_sops;
	
	inode = iget(s, sm->sb->root_inode);
	if (!inode || is_bad_inode(inode))
		goto out_free_bh_bmp;

	s->s_root = d_alloc_root(inode);
	if (!s->s_root)
		goto out_iput;

	DPRINT("exit\n");	
	return 0;

out_iput:
	iput(inode);

out_free_bh_bmp:
	for (i = 0; i < sm->sb->bmp_count; i++)
		if (sm->bh_bmp[i])
			brelse(sm->bh_bmp[i]);
	kfree(sm->bh_bmp);

out_free_sd:
	kfree(sm);
	s->s_fs_info = NULL;

	return -EINVAL;
}


static int xbfs_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
	return get_sb_bdev(fs_type, flags, dev_name, data, xbfs_fill_super,
			   mnt);
}


static struct file_system_type xbfs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "xbfs",
	.get_sb		= xbfs_get_sb,
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV
};

static int __init init_xbfs(void)
{
	return register_filesystem(&xbfs_fs_type);
}

static void __exit exit_xbfs(void)
{
	unregister_filesystem(&xbfs_fs_type);
}

module_init(init_xbfs)
module_exit(exit_xbfs)
MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -