linuxhavoc 发表于 2012-05-11 22:45

求救..一个块设备的问题...

很悲催, 我自己根据sbull简化的块设备出问题了...搞了几天都没解决.!
在mkfs.ext3 /dev/my_block 的时候...卡在Writing superblocks and filesystem accounting information:/*这里*/

-,- 实在想不通哪出问题了.!.. 以下是代码,哪个好心的帮我解决一下....
还有就是- - 有没有关于驱动的群... .我自己QQ上加的群都没几个人说话- - 一问问题群就冷清了.!

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/kdev_t.h>
#include <linux/blkdev.h>
#include <linux/genhd.h>
#include <linux/vmalloc.h>
#include <linux/types.h>
#include <linux/buffer_head.h>
#include <linux/fcntl.h>
#include <linux/sched.h>

MODULE_LICENSE("GPL");
#define MY_BLOCK_SIZE (16*1024*1024)

static int block_major = 0;
module_param(block_major, int , 0);

struct block_dev{
      int size;      
      u8 *data;
      spinlock_t lock;      
      struct request_queue *queue;
      struct gendisk *gd;
};

struct block_dev *Devices = NULL;

static voidblock_transfer(struct block_dev *dev, unsigned longsector,
                                 unsigned longnsect, char *buffer, int write)
{
      unsigned long offset = sector << 9;
      unsigned long nbytes = nsect << 9;

      if ((offset + nbytes) > dev->size)
      {
                printk(KERN_NOTICE "Beyond-end write (%lu %lu)\n", offset, nbytes);
                return;
      }

      if (write == WRITE)
      memcpy(dev->data + offset, buffer , nbytes);

      else
      memcpy(buffer, dev->data + offset, nbytes);
}

static void block_request(struct request_queue *q)
{
      struct request *req;

      while((req = blk_fetch_request(q)) != NULL)
      {
                struct block_dev *dev = req->rq_disk->private_data;

                if (!blk_fs_request(req))
                {
                        printk(KERN_ALERT "Blk_fs_request error");
                        __blk_end_request_all(req, -1);
                        continue;
                }


                block_transfer(dev, blk_rq_pos(req), blk_rq_cur_sectors(req),
                                                req->buffer, rq_data_dir(req));
                        __blk_end_request_cur(req, 0);
      }
}

static struct block_device_operations block_fops ={
      .owner                = THIS_MODULE,
};

static int __init block_init(void)
{
      block_major = register_blkdev(block_major, "My_block");
      if (block_major <= 0){
                printk(KERN_ALERT "Register blkdev error:%d", block_major);
                return 0;
      }

      Devices = kmalloc(sizeof(struct block_dev), GFP_KERNEL);
      if (Devices == NULL){
                printk(KERN_ALERT "Kmalloc error");      
                unregister_blkdev(block_major, "My_block");
                return -ENOMEM;
      }

      memset(Devices, 0, sizeof(struct block_dev));
      Devices -> size = MY_BLOCK_SIZE;
      Devices -> data = vmalloc(Devices->size);
      if (Devices->data == NULL){
                printk(KERN_ALERT"Vmalloc error");
                return ENOMEM;;
      }
      spin_lock_init(&Devices->lock);

      Devices->queue = blk_init_queue(block_request, &Devices->lock);
      if (Devices->queue == NULL)
      {
                if (Devices->data)
                        vfree(Devices->data);
                return -ENOMEM;
      }
      Devices->queue->queuedata = Devices;
      blk_queue_logical_block_size(Devices->queue, 512);
               

      Devices->gd = alloc_disk(1);
      if (! Devices->gd)
      {
                if (Devices->data)
                        vfree(Devices->data);
                return -ENOMEM;
      }

      strcpy(Devices->gd->disk_name, "My_block");
      Devices->gd->major = block_major;
      Devices->gd->first_minor = 0;
      Devices->gd->fops = &block_fops;
      Devices->gd->queue = Devices->queue;
      Devices->gd->private_data= Devices;
      set_capacity(Devices->gd, MY_BLOCK_SIZE>>9);
      add_disk(Devices->gd);

      return 0;

}

static void __exit block_exit(void)
{
      if (Devices->gd)
                del_gendisk(Devices->gd);

      if (Devices->queue)
                blk_cleanup_queue(Devices->queue);

      if (Devices -> data)
                vfree(Devices->data);

      unregister_blkdev(block_major, "My_block");
      kfree(Devices);
}

module_init(block_init);
module_exit(block_exit);

sting99999 发表于 2013-08-22 16:35

我可以给你说怎么解决,但是为什么这样解决我也不清楚。。可以探讨qq:1193533825
页: [1]
查看完整版本: 求救..一个块设备的问题...