移植sbull至kernel version > 2.6.31

【Linux】LDD3中sbull源码编译问题梳理 LDD3,linux设备驱动第三版用的内核是2.6.10,在编译的时候我用的是2.6.32 2.6.32相比2.6.10在块设备驱动上,简直是动了大手术,编译基本满满错误,这里梳理一下我遇到全部的错误 很多都是网上找的资料,只有小部分是我写的: 1、包含文件 错误:没有#include 改法:删掉 2、整一个块设备驱动的struct block_device_operation 阅读详情

移植sbull至kernel version > 2.6.31

     最近在看LDD3和ULK,学习kernel中块设备的驱动机制,kernel 2.6中block layer API已经变更了许多,主要有以下几点:
1. 对于request的请求处理机制,去除掉了原先的elv_next_request和blk_dequeue_request结构,取而代之的是blk_peek_request和blk_start_request,详见如下链接(http://lwn.net/Articles/333620/)。
2.对于block_device_operations中相关成员的修改,详见如下链接(http://lwn.net/Articles/246919/)。
3.对于与bio相关的操作宏的修改,去掉了bio_cur_sectors.
由于以上的修改,LDD3中示例的sbull驱动无法使用,下面是我针对2.6.31的内核对sbull驱动进行修改后的源码。
/*
* Sample disk driver, from the beginning.
* Ported to kernel v2.6.31 by casualfish. 2010.7.20
*/

#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h>   /* kmalloc() */
#include <linux/fs.h>   /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/timer.h>
#include <linux/types.h> /* size_t */
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/hdreg.h> /* HDIO_GETGEO */
#include <linux/kdev_t.h>
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/buffer_head.h> /* invalidate_bdev */
#include <linux/bio.h>

MODULE_LICENSE("Dual BSD/GPL");

static int sbull_major = 0;
module_param(sbull_major, int, 0);
static int hardsect_size = 512;
module_param(hardsect_size, int, 0);
static int nsectors = 1024; /* How big the drive is */
module_param(nsectors, int, 0);
static int ndevices = 4;
module_param(ndevices, int, 0);

/*
* The different "request modes" we can use.
*/
enum {
RM_SIMPLE = 0, /* The extra-simple request function */
RM_FULL    = 1, /* The full-blown version */
RM_NOQUEUE = 2, /* Use make_request */
};
static int request_mode = RM_SIMPLE;
module_param(request_mode, int, 0);

/*
* Minor number and partition management.
*/
#define SBULL_MINORS 16
#define MINOR_SHIFT 4
#define DEVNUM(kdevnum) (MINOR(kdev_t_to_nr(kdevnum)) >> MINOR_SHIFT

/*
* We can tweak our hardware sector size, but the kernel talks to us
* in terms of small sectors, always.
*/
#define KERNEL_SECTOR_SIZE 512

/*
* After this much idle time, the driver will simulate a media change.
*/
#define INVALIDATE_DELAY 30*HZ

/*
* The internal representation of our device.
*/
struct sbull_dev {
        int size;                       /* Device size in sectors */
        u8 *data;                       /* The data array */
        short users;                    /* How many users */
        short media_change;             /* Flag a media change? */
        spinlock_t lock;                /* For mutual exclusion */
        struct request_queue *queue;    /* The device request queue */
        struct gendisk *gd;             /* The gendisk structure */
        struct timer_list timer;        /* For simulated media changes */
};

static struct sbull_dev *Devices = NULL;

/*
* Handle an I/O request, in sectors.
*/
static void sbull_transfer(struct sbull_dev *dev, unsigned long sector,
   unsigned long nsect, char *buffer, int write)
{
unsigned long offset = sector*KERNEL_SECTOR_SIZE;
unsigned long nbytes = nsect*KERNEL_SECTOR_SIZE;

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

/*
* The simple form of the request function.
*/
static void sbull_request(struct request_queue *q)
{
struct request *req;

req = blk_fetch_request(q);
while (req != NULL) {
   struct sbull_dev *dev = req->rq_disk->private_data;
   if (! blk_fs_request(req)) {
    printk (KERN_NOTICE "Skip non-fs request\n");
    __blk_end_request_all(req, -EIO);
    continue;
   }
    //    printk (KERN_NOTICE "Req dev %d dir %ld sec %ld, nr %d f %lx\n",
    //        dev - Devices, rq_data_dir(req),
    //        req->sector, req->current_nr_sectors,
    //        req->flags);
   sbull_transfer(dev, blk_rq_pos(req), blk_rq_cur_sectors(req), req->buffer, rq_data_dir(req));
   /* end_request(req, 1); */
   if(!__blk_end_request_cur(req, 0)) {
    req = blk_fetch_request(q);
   }
}
}


/*
* Transfer a single BIO.
*/
static int sbull_xfer_bio(struct sbull_dev *dev, struct bio *bio)
{
int i;
struct bio_vec *bvec;
sector_t sector = bio->bi_sector;

/* Do each segment independently. */
bio_for_each_segment(bvec, bio, i) {
   char *buffer = __bio_kmap_atomic(bio, i, KM_USER0);
   sbull_transfer(dev, sector, bio_cur_bytes(bio)>>9 /* in sectors */, 
    buffer, bio_data_dir(bio) == WRITE);
   sector += bio_cur_bytes(bio)>>9; /* in sectors */
   __bio_kunmap_atomic(bio, KM_USER0);
}
return 0; /* Always "succeed" */
}

/*
* Transfer a full request.
*/
static int sbull_xfer_request(struct sbull_dev *dev, struct request *req)
{
struct bio *bio;
int nsect = 0;
    
__rq_for_each_bio(bio, req) {
   sbull_xfer_bio(dev, bio);
   nsect += bio->bi_size/KERNEL_SECTOR_SIZE;
}
return nsect;
}

/*
* Smarter request function that "handles clustering".
*/
static void sbull_full_request(struct request_queue *q)
{
struct request *req;
int sectors_xferred;
struct sbull_dev *dev = q->queuedata;

req = blk_fetch_request(q);
while (req != NULL) {
   if (! blk_fs_request(req)) {
    printk (KERN_NOTICE "Skip non-fs request\n");
    __blk_end_request_all(req, -EIO);
    continue;
   }
   sectors_xferred = sbull_xfer_request(dev, req);
   if (!__blk_end_request_cur(req, 0)) {
    blk_fetch_request(q);
   }
}
}

/*
* The direct make request version.
*/
static int sbull_make_request(struct request_queue *q, struct bio *bio)
{
struct sbull_dev *dev = q->queuedata;
int status;

status = sbull_xfer_bio(dev, bio);
bio_endio(bio, status);
return 0;
}


/*
* Open and close.
*/

static int sbull_open(struct block_device *device, fmode_t mode)
{
struct sbull_dev *dev = device->bd_disk->private_data;

del_timer_sync(&dev->timer);
/* filp->private_data = dev;*/
spin_lock(&dev->lock);
if (! dev->users) 
   check_disk_change(device);
dev->users++;
spin_unlock(&dev->lock);
return 0;
}

static int sbull_release(struct gendisk *disk, fmode_t mode)
{
struct sbull_dev *dev = disk->private_data;

spin_lock(&dev->lock);
dev->users--;

if (!dev->users) {
   dev->timer.expires = jiffies + INVALIDATE_DELAY;
   add_timer(&dev->timer);
}
spin_unlock(&dev->lock);

return 0;
}

/*
* Look for a (simulated) media change.
*/
int sbull_media_changed(struct gendisk *gd)
{
struct sbull_dev *dev = gd->private_data;

return dev->media_change;
}

/*
* Revalidate. WE DO NOT TAKE THE LOCK HERE, for fear of deadlocking
* with open. That needs to be reevaluated.
*/
int sbull_revalidate(struct gendisk *gd)
{
struct sbull_dev *dev = gd->private_data;

if (dev->media_change) {
   dev->media_change = 0;
   memset (dev->data, 0, dev->size);
}
return 0;
}

/*
* The "invalidate" function runs out of the device timer; it sets
* a flag to simulate the removal of the media.
*/
void sbull_invalidate(unsigned long ldev)
{
struct sbull_dev *dev = (struct sbull_dev *) ldev;

spin_lock(&dev->lock);
if (dev->users || !dev->data) 
   printk (KERN_WARNING "sbull: timer sanity check failed\n");
else
   dev->media_change = 1;
spin_unlock(&dev->lock);
}

/*
* The ioctl() implementation
*/

int sbull_ioctl (struct block_device *device, fmode_t mode,
                 unsigned int cmd, unsigned long arg)
{
long size;
struct hd_geometry geo;
struct sbull_dev *dev = device->bd_disk->private_data;

switch(cmd) {
     case HDIO_GETGEO:
        /*
   * Get geometry: since we are a virtual device, we have to make
   * up something plausible. So we claim 16 sectors, four heads,
   * and calculate the corresponding number of cylinders. We set the
   * start of data at sector four.
   */
   size = dev->size*(hardsect_size/KERNEL_SECTOR_SIZE);
   geo.cylinders = (size & ~0x3f) >> 6;
   geo.heads = 4;
   geo.sectors = 16;
   geo.start = 4;
   if (copy_to_user((void __user *) arg, &geo, sizeof(geo)))
    return -EFAULT;
   return 0;
}

return -ENOTTY; /* unknown command */
}

/*
* The device operations structure.
*/
static struct block_device_operations sbull_ops = {
.owner           = THIS_MODULE,
.open           = sbull_open,
.release = sbull_release,
.media_changed   = sbull_media_changed,
.revalidate_disk = sbull_revalidate,
.ioctl          = sbull_ioctl
};


/*
* Set up our internal device.
*/
static void setup_device(struct sbull_dev *dev, int which)
{
/*
* Get some memory.
*/
memset (dev, 0, sizeof (struct sbull_dev));
dev->size = nsectors*hardsect_size;
dev->data = vmalloc(dev->size);
if (dev->data == NULL) {
   printk (KERN_NOTICE "vmalloc failure.\n");
   return;
}
spin_lock_init(&dev->lock);

/*
* The timer which "invalidates" the device.
*/
init_timer(&dev->timer);
dev->timer.data = (unsigned long) dev;
dev->timer.function = sbull_invalidate;

/*
* The I/O queue, depending on whether we are using our own
* make_request function or not.
*/
switch (request_mode) {
     case RM_NOQUEUE:
   dev->queue = blk_alloc_queue(GFP_KERNEL);
   if (dev->queue == NULL)
    goto out_vfree;
   blk_queue_make_request(dev->queue, sbull_make_request);
   break;

     case RM_FULL:
   dev->queue = blk_init_queue(sbull_full_request, &dev->lock);
   if (dev->queue == NULL)
    goto out_vfree;
   break;

     default:
   printk(KERN_NOTICE "Bad request mode %d, using simple\n", request_mode);
        /* fall into.. */

     case RM_SIMPLE:
   dev->queue = blk_init_queue(sbull_request, &dev->lock);
   if (dev->queue == NULL)
    goto out_vfree;
   break;
}
blk_queue_logical_block_size(dev->queue, hardsect_size);
dev->queue->queuedata = dev;
/*
* And the gendisk structure.
*/
dev->gd = alloc_disk(SBULL_MINORS);
if (! dev->gd) {
   printk (KERN_NOTICE "alloc_disk failure\n");
   goto out_vfree;
}
dev->gd->major = sbull_major;
dev->gd->first_minor = which*SBULL_MINORS;
dev->gd->fops = &sbull_ops;
dev->gd->queue = dev->queue;
dev->gd->private_data = dev;
snprintf (dev->gd->disk_name, 32, "sbull%c", which + 'a');
set_capacity(dev->gd, nsectors*(hardsect_size/KERNEL_SECTOR_SIZE));
add_disk(dev->gd);
return;

out_vfree:
if (dev->data)
   vfree(dev->data);
}

static int __init sbull_init(void)
{
int i;
/*
* Get registered.
*/
sbull_major = register_blkdev(sbull_major, "sbull");
if (sbull_major <= 0) {
   printk(KERN_WARNING "sbull: unable to get major number\n");
   return -EBUSY;
}
/*
* Allocate the device array, and initialize each one.
*/
Devices = kmalloc(ndevices*sizeof (struct sbull_dev), GFP_KERNEL);
if (Devices == NULL)
   goto out_unregister;
for (i = 0; i < ndevices; i++) 
   setup_device(Devices + i, i);
    
return 0;

out_unregister:
unregister_blkdev(sbull_major, "sbd");
return -ENOMEM;
}

static void sbull_exit(void)
{
int i;

for (i = 0; i < ndevices; i++) {
   struct sbull_dev *dev = Devices + i;

   del_timer_sync(&dev->timer);
   if (dev->gd) {
    del_gendisk(dev->gd);
    put_disk(dev->gd);
   }
   if (dev->queue)
    blk_cleanup_queue(dev->queue);
   if (dev->data)
    vfree(dev->data);
}
unregister_blkdev(sbull_major, "sbull");
kfree(Devices);
}

module_init(sbull_init);
module_exit(sbull_exit);

以下是测试结果:
ls -lh
total 80K
-rw-rw-r-- 1 casualfish casualfish 12K 2010-07-21 12:10 alice

[root@hitwh ~]# uname -a
Linux hitwh.edu.cn 2.6.31.1 #1 SMP Thu Oct 29 12:52:36 CST 2009 i686 i686 i386 GNU/Linux
[root@hitwh ~]# dd if=/home/casualfish/alice of=/dev/sbulla bs=512 count=2
2+0 records in
2+0 records out
1024 bytes (1.0 kB) copied, 4.854e-05 s, 21.1 MB/s
[root@hitwh ~]# dd if=/dev/sbulla of=/home/casualfish/file bs=512 count=2
2+0 records in
2+0 records out
1024 bytes (1.0 kB) copied, 0.000196812 s, 5.2 MB/s

LDD3示例代码sbull编译问题 编译块设备驱动程序实例代码sbull遇到的问题 示例代码版本为 2.6.10,编译时的内核版本为2.6.39。内核中许多函数和结构发生变化,造成了编译问题,记录如下: error: unknown type name ‘request_queue_t’ 原因:新版内核中已经没有request_queue_t的定义 解决方法:可在代码所在文件添加如下定义: typedef struct requ... 阅读详情

相关推荐

块设备驱动sbull.c

ramdisk设备的驱动程序sbull,虚拟出几个磁盘,并可以对这个虚拟磁盘进行格式化、文件复制和查看等操作。

sbull虚拟的磁盘驱动的编写

原理指导:我们通过vmalloc在内存中开辟一部分空间,作为一个虚拟的磁盘,然后我们以块设备的方式来访问这片内存,例如这个sbull模型。 sbull(Simple Block Utility for Loading Localities),该驱动程序实现了一个使用系统内存的块设备,从本质上讲,属于一种 RAM 磁盘驱动程序。字符设备的IO操作则是直接不绕弯的,块设备的IO操作会配对和整合。驱动

zplove003的专栏 2597

A Survey of Generative Image and Video Compression Methods_生成式图像与视频压缩方法综述.md

ArxivReview学术论文研究综述,详细内容可访问:https://www.arxivreview.com/surveys

嵌入式驱动开发之--- 虚拟磁盘SBULL块设备驱动程序分析

 #define SBULL_MINORS  16         /* 每个sbull设备所支持的次设备号的数量 */  #define KERNEL_SECTOR_SIZE 512  // 本地定义的常量,使用该常量进行内核512字节到实际                                      // 扇区大小的转换  #define INVALIDATE_DELAY ...

weixin_34184158的博客 213

A Survey on Muon Optimizer Variants and Analysis_矩阵几何重塑优化:Muon 优化器变体与理论分析综述.md

ArxivReview学术论文研究综述,详细内容可访问:https://www.arxivreview.com/surveys

A Survey of Instruction-Based Image Editing Methods_指令式图像编辑方法综述.md

ArxivReview学术论文研究综述,详细内容可访问:https://www.arxivreview.com/surveys

linux 下块设备驱动开发学习笔记 2(sbull驱动分析)

//这个驱动是针对gendisk下的buff来实现的,没有用到bio #include #include #include #include  //包含头文件 #include #include /* printk() */ #include /* kmalloc() */ #include /* everything... */ #include /*

Bean Huo 1699

S型、T型、多项式、三次样条多段轨迹规划及圆弧过渡算法Matlab仿真

内容概要:本文系统介绍了S型、T型、多项式及三次样条多段轨迹规划方法,并结合圆弧过渡算法在Matlab环境下的仿真研究,重点应用于无刷直流电机与多轴同步系统的运动控制。文章深入剖析了各类轨迹规划算法的数学模型与实现机制,涵盖速度与加速度连续性控制、平滑过渡设计以及多段轨迹拼接时的优化策略,通过Matlab仿真对比分析了不同算法在动态响应性能、轨迹平滑度及系统稳定性方面的表现差异。此外,还探讨了这些算法在机械臂、数控机床、无人机等高精度运动控制系统中的实际应用价值和技术优势。; 适合人群:具备自动控制理论基础和Matlab编程能力的研究生、科研人员及从事运动控制、机器人、自动化等相关领域的工程师。; 使用场景及目标:①用于高精度运动控制系统中实现平滑、低冲击的轨迹生成;②为机械臂、数控机床、无人机等设备的路径优化提供算法支持与仿真验证;③辅助高校教学与科研工作中对轨迹规划算法的理解、改进与创新设计。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注各类轨迹规划算法的参数配置、边界条件处理及过渡段设计,并通过仿真结果的对比分析深入理解其适用场景与性能差异。

sbull在新内核编译

《linux设备驱动程序》(ldd3)中的实例程序是在linux-2.6.10版本内核中开发的,我的linux内核版本是linux-2.6.32.16,许多函数和结构都相对2.6.10发生了变化,所以必须自己对源码进行修改。从oreilly官网download示例代码,进入sbul文件夹,开始第一次make:结果如下: root@ubuntu:/home/shevarey/ssdd/

yyp872的专栏 2048

A Survey of Federated Learning Systems and Algorithms_联邦学习系统与算法综述.md

ArxivReview学术论文研究综述,详细内容可访问:https://www.arxivreview.com/surveys

国内网络环境下Calico v3.26.4安装避坑指南(附华为云镜像加速脚本)

本文为国内开发者提供Calico v3.26.4的高效部署指南,重点介绍如何通过华为云镜像加速服务解决Kubernetes集群网络插件镜像拉取问题。文章包含详细的镜像加速脚本、部署清单修改方法及深度排错技巧,帮助开发者快速完成Calico网络插件的安装与配置。

brown的博客 294

2010-3-7 sbull代码阅读

昨晚,我把sbull的代码,大体上看了一下,因为ldd这一章已经看过,此外对Linux驱动程序的结构有一定了解,所以基本上看得懂,但是也遇到了一些疑惑,今天上午,我再次阅读了ldd中的相关内容,主要如下:1、弄清楚了以下几个概念:请求函数(请求处理函数):负责执行块设备的读写请求,blk_init_queue函数负责分配请求队列,并绑定自旋锁和请求函数

yangp01的专栏 1229

告别手动改Hex!用Vector HexView命令行5分钟搞定固件数据“乾坤大挪移”

本文详细介绍了如何利用Vector HexView命令行工具快速实现嵌入式固件数据的自动化迁移,告别手动修改Hex文件的繁琐流程。通过实战案例和脚本示例,展示了如何5分钟内完成数据地址范围的重新映射,提升嵌入式开发效率,特别适用于烧录文件的内存布局调整。

weixin_30635053的博客 713

块设备驱动实例

摘自:http://my.oschina.net/heiden/blog/194505 将ldd3(linxu device driver 3)中块设备驱动部分的代码在redhat6.3下编译时,会出现很多问题,主要是由于ldd3示例代码使用的内核版本较低(2.6.10版本),对于块设备子系统,很多接口都已经发生了改变,主要有如下变化:1,elv_next_request已删除,使用如下方式从请求

chinaclock的专栏 3618

最新,弱小目标检测数据集,持续更新中。

梳理了最新的下弱小目标检测数据库,方便后续开发使用

weixin_43331421的博客 4113

SpringAi 通过大模型来实现调用自己的Api

摘要 本文介绍了如何利用Spring AI的Tools机制扩展大模型能力,实现业务系统集成。通过@Tool和@ToolParam注解定义工具方法,将订单查询和关闭功能暴露给大模型调用。关键实现包括:1)使用详细描述标注工具方法和参数;2)在ChatClient中注册工具服务;3)集成对话记忆保持上下文。该方法使AI具备操作业务系统的能力,形成"认知-决策-执行"闭环,大幅提升了模型在实际业务场景中的应用价值。

又要搬砖了 839

ldd3 sbull编译遇到的问题 解决(部分)方法

源自于:http://www.vsyo.com/a/t/79a054b715b8bb10 开始第一次make:结果如下: root@ubuntu:/home/shevarey/ssdd/sbull# make make -C /lib/modules/2.6.32.16/build M=/home/shevarey/ssdd/sbull modules make[1]: Ente

xiaowen_10的专栏 2330

【大模型+知识图谱】哈工大版chinese-roberta-wwm-ext模型介绍、下载部署和GPU显卡配置

RoBERTa模型的一个重要改进版本,由阿里云的研究团队提出。而Chinese RoBERTa-wwm-ext则是针对中文文本的预训练模型,它在哈工大的研究团队手中进一步优化,特别适用于中文自然语言处理任务。

知识靠谱的博客 2778

修改sbull的一点体会

今天在修改sbull的 open函数时出现了一些问题,本来的想法是拿到设备文件系统的 inode 结点后,把其中的 i_data 中的 a_ops 里的prepare_write 替换成我们自己的。就是三两句代码的事,可是调了一个下午才通过了。开始是编译不过去,好不容易编译过了,insmod 和 mkfs 时都没有问题,最后在mount 时发生了几次 kernel panic. 下面把发生的原因和

Cole的专栏 744

华为MetaERP国资委语境下的 DRP(全域数字化资源管理平台/Digitalized Resource-management Platform)与传统 ERP(以 SAP、Oracle 为代表)的

【摘要】国资委推动的DRP(全域数字化资源管理平台)正在重构央国企数智化转型路径,其本质是通过"ERP管交易+DRP管管控"的双层架构实现升维替代。与传统ERP相比,DRP具有三大突破:采用国产分布式数据库实现全级次穿透监管、构建数据中台打破信息孤岛、以实时分析替代事后报表。当前政策明确2026-2030年分阶段建设目标,这将重塑产业格局——倒逼传统ERP厂商转型,为云原生/AI/信创企业创造新机遇。需注意DRP并非简单替代ERP,而是通过治理模式变革实现"交易系统做薄、管控中

金牌架构师 294

【QGIS】软件下载安装及GIS4WRF插件使用

【QGIS】软件下载安装及GIS4WRF插件使用

WW、forever的博客 1803
下一篇: ldd3中scullv模块vm_operations_struct -> nopage到vm_operations_struct ->fault的变换
nevil
博客等级 码龄24年 118粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值