give your comments

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hi, i have attached the module. Can any one review it and tell me what was the problem in this. Basically, i am trying to write in a raw hard disk, i doubt in the blocknr and brsector value.

 

#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/blkdev.h>
#define MINORBITS       8 //these two macros are used to find b_rdev value
#define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))
#define SIZE 512
#define BLK_SIZE 512
#define SECTOR_SIZE 512


int dataWrite(void);
void ioCompleteHandler(struct buffer_head *bh, int uptodate);
int major_no;
struct block_device_operations block_bdops;

void ioCompleteHandler(struct buffer_head *bh, int uptodate)
{
        printk(KERN_INFO"Freeing the memory in completeHandler function\n");
        mark_buffer_uptodate(bh, uptodate);

        kfree(bh->b_data);
        kfree(bh);
        printk(KERN_INFO"Memory freed in iocompleHandler function\n");
}

int dataWrite()
{
        struct buffer_head *bh;
        char *datpot;
        printk(KERN_INFO" For testing\n");
        printk(KERN_INFO"size of buffer is %d\n",sizeof(struct buffer_head));
        bh=kmalloc(sizeof(struct buffer_head),GFP_KERNEL);
                if( NULL == bh)
                {
                        printk(KERN_INFO" kmalloc failed\n");
                        return -1 ;
                }

        bh->b_next = NULL;
        bh->b_next_free = NULL;
        bh->b_prev_free = NULL;
        bh->b_this_page = NULL;
        bh->b_reqnext = NULL;
        bh->b_pprev = NULL;

        set_bit (BH_Uptodate, &bh->b_state);
        set_bit (BH_Mapped, &bh->b_state); //set if the device is mapped
        set_bit (BH_Req, &bh->b_state);
        set_bit (BH_Dirty, &bh->b_state); //added

        bh->b_size=SIZE;
        bh->b_dev = MKDEV(8,16);
        bh->b_blocknr= 1;       //not sure
        bh->b_rsector = bh->b_blocknr * (BLK_SIZE/SECTOR_SIZE);  // not sure
        printk(KERN_INFO"allocating buffer the data\n");
        datpot = kmalloc(SIZE,GFP_KERNEL);

                if (NULL == datpot)
                {
                        printk(KERN_INFO"data memory allocation failed\n");
                        return -1;
                }

        bh->b_data=datpot;
        bh->b_page = virt_to_page(bh->b_data);
        printk(KERN_INFO"virtual to page done\n");
        bh->b_rdev= bh->b_dev;
        bh->b_end_io=ioCompleteHandler;
//      bh->b_private = bh;
        printk(KERN_INFO"calling generic make request\n");
        generic_make_request(WRITE,bh);
        printk(KERN_INFO" write done\n");
        return 0;
}

static int block_init(void)
{
        int ret;
        major_no = register_blkdev(0,"block_data",&block_bdops);
        if ( major_no <0)
        {
                printk(KERN_INFO"Failed to register the module\n");
                return -1;
        }
        printk(KERN_INFO"Block_data Module inserted\n");
        ret = dataWrite();
return 0;
}

static void block_exit()
{
        int ret;
        ret = unregister_blkdev(major_no,"block_date");
        if( ret < 0 )
        {
                printk(KERN_INFO"failed to unregister the module\n");
                //return -1;
        }

        printk(KERN_INFO"Block_data Module removed\n");
}

module_init(block_init);
module_exit(block_exit);
MODULE_LICENSE("GPL");


--

___________________________________________________________
Sign-up for Ads Free at Mail.com
http://www.mail.com/?sr=signup

-- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux