#include <linux/module.h>
#include <linux/config.h>
#include <linux/fs.h>
#include <linux/ext2_fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
#include <asm/fcntl.h>
#include <sys/syscall.h>
#include <linux/sched.h>
#include<linux/string.h>
struct bitmap{
unsigned first : 1;
unsigned second : 1;
unsigned third : 1;
unsigned fourth : 1;
unsigned fifth : 1;
unsigned sixth : 1;
unsigned seventh : 1;
unsigned eighth : 1;
};
static int datablk_major = 211;
static int datablk_open(struct inode *inode, struct file *filp);
int init_module(void);
int datablk_init(void);
void cleanup_module(void);
int datablk_release(struct inode *inode, struct file *filp);
void read_datablk(void);
static struct file_operations datablk_fops ={
NULL, /*lseek*/
NULL, /* Read*/
NULL, /*write*/
NULL, /*readdir*/
NULL, /*select*/
NULL, /*ioctl*/
NULL, /*mmap*/
datablk_open, /*open*/
NULL, /*flush */
datablk_release, /*release*/
NULL,
NULL,
NULL,
NULL,
NULL
};
void read_datablk(void)
{
unsigned long block_count;
unsigned long group_count;
struct ext2_group_desc * gd;
unsigned long block_bitmp_block;
int blocksize = BLOCK_SIZE;
int group_flag,k,block_flag;
struct buffer_head *bh;
kdev_t dev = ROOT_DEV;
struct bitmap * bmp;
struct super_block * sa = get_super(dev);
block_count = sa->u.ext2_sb.s_blocks_per_group;
group_count=sa->u.ext2_sb.s_groups_count;
blocksize = get_hardblocksize(dev);
if(blocksize==0 || blocksize < BLOCK_SIZE)
{
blocksize = BLOCK_SIZE;
}
for (group_flag = 0; group_flag<group_count; group_flag++)
{
if((group_flag % EXT2_DESC_PER_BLOCK(sa)) == 0)
gd=(struct ext2_group_desc *) sa->u.ext2_sb.s_group_desc[group_flag]->b_data;
block_bitmp_block=le32_to_cpu(gd->bg_block_bitmap);
bh=bread(dev, block_bitmp_block, sa->s_blocksize);
bmp=(struct bitmap *)(bh->b_data);
if((block_count % 8 )==0)
k=(block_count/8);
if((block_count % 8 )!=0)
k=((block_count/8)+1);
printk("<1> Block Size = %ld\n",sa->s_blocksize);
for(block_flag=0;block_flag<k;block_flag++)
{
if(bmp->first ==1)
{
printk("<1> block allocated %d %d\n ",block_flag,group_flag);
}
else
{
printk("<1> block unallocated%d %d\n ",block_flag,group_flag);
}
if(bmp->second ==1)
{
printk("<1> block allocated ");
}
else
{
printk("<1> block unallocated ");
}
if(bmp->third ==1)
{
printk("<1> block allocated ");
}
else
{
printk("<1> block unallocated ");
}
if(bmp->fourth ==1)
{
printk("<1> block allocated ");
}
if(bmp->fifth ==1)
{
printk("<1> block allocated ");
}
else
{
printk("<1> block unallocated ");
}
if(bmp->sixth ==1)
{
printk("<1> block allocated ");
}
else
{
printk("<1> block unallocated ");
}
if(bmp->seventh ==1)
{
printk("<1> block allocated ");
}
else
{
printk("<1> block unallocated ");
}
if(bmp->eighth==1)
{
printk("<1> block allocated ");
}
else
{
printk("<1> block unallocated ");
}
}
}
}
static int datablk_open(struct inode *inode, struct file *filp)
{
MOD_INC_USE_COUNT;
read_datablk();
printk("<1>\n Device Opened");
return 0;
}
int init_module(void)
{
printk("<1>Module initialised\n");
datablk_init();
return 0;
}
int datablk_init(void)
{
int result_datablk;
result_datablk=register_chrdev(datablk_major, "testdata11", &datablk_fops);
printk("<1>Registered\n");
if(result_datablk <0)
{
printk("<1>SDRV : Can't get Major %d\n",datablk_major);
return -EIO;
}
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(datablk_major,"testdata11");
printk("<1>\nDevice Unregistered");
}
int datablk_release(struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
return 0;
}
Hi
i made this program to find which all data blocks are allocated or unallocated in the ext2 filesysem on my Linux 7.0 version. But i found that this program gives all the blocks as allocated.?? any comments . why is this happeneing whereas i have checked that there are data blocks that are free..