All pastes #2092347 Raw Edit

Someone

public text v1 · immutable
#2092347 ·published 2011-10-21 22:38 UTC
rendered paste body
/* **************** LF339:1.0 s_03/trivial.c **************** */
/*
 * The code herein is: Copyright the Linux Foundation, 2011
 *
 * This Copyright is retained for the purpose of protecting free
 * redistribution of source.
 *
 *     URL:    http://training.linuxfoundation.org
 *     email:  trainingquestions@linuxfoundation.org
 *
 * The primary maintainer for this code is Jerry Cooperstein
 * The CONTRIBUTORS file (distributed with this
 * file) lists those known to have contributed to the source.
 *
 * This code is distributed under Version 2 of the GNU General Public
 * License, which you should have received with the source.
 *
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/moduleparam.h>
#include <linux/vmalloc.h>
#include <linux/blkdev.h>
#include <linux/genhd.h>
#include <linux/errno.h>
#include <linux/hdreg.h>

static int myblock_maj = 0 , diskmb = 64 , disksize = 0;
static char *my_blockdevice;
static struct gendisk *my_gendisk;
static spinlock_t my_block_lock ;
static unsigned short sector_size = 512 ;
static struct request_queue *my_block_request_queue;
#define blk_fs_request(rq)      ((rq)->cmd_type == REQ_TYPE_FS)

module_param(diskmb, int, 0);

static void my_request_func(struct request_queue *q){}

static int __init my_init(void)
{       
        disk_size = diskmb * 1024 *1024 ;                                               
	spin_lock_init(&my_block_lock);
	if (!my_blockdevice = vmalloc(disk_size))
	    return -ENOMEM;
	    
	// initialize the queue  or bail out
        if( !(my_block_request_queue = blk_init_queue(my_request_func, &my_block_lock)))
         {
            vfree(my_blockdevice);
            return -ENOMEM;
         }
        blk_queue_logical_block_size(my_block_request_queue, sector_size);	
        
        myblock_maj = register_blkdev(myblock_maj, MY_DEVICE_NAME); 
                                // major = dynamic

        // on error ????	
        
        // allocate gendisk structure (use 16 partitions)
        
        // set major in gendisk struct
        // set first_minor in gendisk struct   (0)
        // set blockdevice_ops
        // strcpy MY_DEVICE_NAME to disk_name
        // call set_capacity on gendisk structure (disk_size / sector_size)
        // add_disk
        // phew!! - done
        
	printk(KERN_INFO "Hello: module loaded at 0x%p\n", my_init);
	return 0;
}

static void __exit my_exit(void)
{
	printk(KERN_INFO "Bye: module unloaded from 0x%p\n", my_exit);
}

module_init(my_init);
module_exit(my_exit);

MODULE_AUTHOR("A GENIUS");
MODULE_LICENSE("GPL v2");