An example of Block device driver

Block Drivers

Global architecture

Block I/O on Linux
Block devices are storage media capable of random access. Unlike character devices, block devices can hold file system data.

The storage media contains files residing in a filesystem, such as EXT3 or Reiserfs. User applications invoke I/O system calls to access these files. The resulting filesystem operations pass through the generic Virtual File System (VFS) layer before entering the individual filesystem driver. The buffer cache speeds up filesystem access to block devices by caching disk blocks. If a block is found in the buffer cache, the time required to access the disk to read the block is saved. Data destined for each block device is lined up in a request queue. The filesystem driver populates the request queue belonging to the desired block device, whereas the block driver receives and consumes requests from the corresponding queue. In between, I/O schedulers manipulate the request queue so as to minimize disk access latencies and maximize throughput.

Linux I/O schedulers

  • Linus elevator
  • Deadline: tries to guarantee that an I/O will be served within a deadline
  • Anticipatory: tries to anticipate what could be the next accesses
  • Complete Fair Queuing: the default scheduler, tries to guarantee fairness between users of a block device
  • Noop: for non­disk based block devices

The current scheduler for a device can be get and set in

/sys/block/<dev>/queue/scheduler

Ramdisk Example

This example is based on kernel version 4.15.0-46-generic.

Register a block I/O device

#define MY_BLOCK_MAJOR           240
#define MY_BLKDEV_NAME          "sun_block"

static int my_block_init(void)
{
    int status;
    status = register_blkdev(MY_BLOCK_MAJOR, MY_BLKDEV_NAME);
    if (status < 0) {
             printk(KERN_ERR "unable to register mybdev block device\n");
             return -EBUSY;
    }
    printk("reigster a test block driver...\n");
    return 0;
}

static void my_block_exit(void)
{
    printk("exit my block driver..\n");    
    unregister_blkdev(MY_BLOCK_MAJOR, MY_BLKDEV_NAME);
}

module_init(my_block_init);
module_exit(my_block_exit);

Register a disk

struct gendisk is an abstraction of real disk, there are differences between gendisk and block_device. gendisk is defined in “include/linux/genhd.h”, which indicate this structure is mainly used by block device driver; Meanwhile, block_device is defined in “include/linux/fs.h”, which indicate block_device has a close relationship with filesystem.
在这里插入图片描述
For each partition of a block device that has already been opened, there is an instance of struct block_device . The objects for partitions are connected with the object for the complete device via bd_contains . All block_device instances contain a link to their generic disk data structure gen_disk via bd_disk . Note that while there are multiple block_device instances for a partitioned disk, one gendisk instance is sufficient.

The gendisk instance points to an array with pointers to hd_structs. Each represents one partition. If a block_device represents a partition, then it contains a pointer to the hd_struct in question — the hd_struct instances are shared between struct gendisk and struct block_device .

Additionally, generic hard disks are integrated into the kobject framework as shown in Figure 6-11. The block subsystem is represented by the kset instance block_subsystem . The kset contains a linked list on which the embedded kobject s of each gendisk instance are collected.
在这里插入图片描述
Partitions represented by struct hd_struct also contain an embedded kobject . Conceptually, partitions are subelements of a hard disk, and this is also captured in the data structures: The parent pointer of the kobject embedded in every hd_struct points to the kobject of the generic hard disk.

#define SECTOR_SIZE     512
#define MY_SECTORS      16
#define MY_HEADS        4
#define MY_CYLINDERS        1024    

#define MY_SECTOR_TOTAL (MY_SECTORS*MY_HEADS*MY_CYLINDERS)
#define MY_SIZE         (MY_SECTOR_TOTAL*SECTOR_SIZE)

static struct my_block_dev {
    spinlock_t lock;                /* For mutual exclusion */
    struct request_queue *queue;    /* The device request queue */
    struct gendisk *gd;             /* The gendisk structure */
    unsigned char * data;
} dev;

struct block_device_operations my_block_ops = {
    .owner = THIS_MODULE,
    .open = my_block_open,
    .release = my_block_release,
    .ioctl = my_block_ioctl,
};

static int create_block_device(struct my_block_dev *dev)
{
    dev->gd = alloc_disk(MY_BLOCK_MINORS);
    if (!dev->gd) {
        printk (KERN_NOTICE "alloc_disk failure\n");
        return -ENOMEM;
    }
    
    dev->gd->major = MY_BLOCK_MAJOR;
    dev->gd->first_minor = 0;
    dev->gd->fops = &my_block_ops;
    dev->gd->queue = dev->queue;
    dev->gd->private_data = dev;
    snprintf (dev->gd->disk_name, 32, "sun_block");
    set_capacity(dev->gd, MY_SECTOR_TOTAL);
    
    add_disk(dev->gd);
    return 0;
}

static int my_block_init(void)
{
	...
    dev.data = vmalloc(MY_SIZE); //molloc 8M RAM memory for this ramdisk
    memset(dev.data, 0, MY_SIZE);
    printk("data range 0x%x---0x%x, 0x%x", dev.data, dev.data+MY_SIZE, MY_SIZE);
    if(dev.data == NULL) {
             printk(KERN_ERR "unable to vmalloc\n");
             return -ENOMEM;
    }
    
    create_block_device(&dev);
    ...
}

static void delete_block_device(struct my_block_dev *dev)
{
    if (dev->gd)
        del_gendisk(dev->gd);
    if(dev->data)
        vfree(dev->data);
}

static void my_block_exit(void)
{
	...
    delete_block_device(&dev);
    ...
}

Request queues

Drivers for block devices use queues to store the block requests I/O that will be processed. A request queue is represented by the struct request_queue structure. request_queue contains a double linked list of request and their associated control information. There are 2 different ways to manipulate the queues: request version and make_request version.

The relationship between request and bio is showed below.
在这里插入图片描述
在这里插入图片描述

1. request version.
static void my_block_request(struct request_queue *q) 
{
    struct request *rq;
 
    while ((rq = blk_fetch_request(q)) != NULL) {
        __process_request(rq);
        __blk_end_request_all(rq, 0);
    }
}

static int create_block_device(struct my_block_dev *dev)
{
	/* Initialize the I/O queue */
	spin_lock_init(&dev->lock);
	dev->queue = blk_init_queue(my_block_request, &dev->lock);
	if (dev->queue == NULL)
		return -ENOMEM;
		...
}
2. make_request version

blk_queue_make_request does not alloc queue in its logic, so must use blk_alloc_queue to alloc a queue first.

static blk_qc_t my_make_request(struct request_queue *q, struct bio *bio)
{
    struct my_block_dev *pdev = bio->bi_disk->private_data;
    struct bio_vec bvec;
    sector_t sector;
    struct bvec_iter iter;
    char *pData, *pBuffer;

    sector = bio->bi_iter.bi_sector;
    if (bio_end_sector(bio) > get_capacity(bio->bi_disk))
        goto io_error;
        
    pData = pdev->data + (sector * SECTOR_SIZE);
    bio_for_each_segment(bvec, bio, iter) {
        __process_bio_vector();
        pData += bvec.bv_len;
    }

    bio_endio(bio);
    return BLK_QC_T_NONE;
io_error:
    bio_io_error(bio);
    return BLK_QC_T_NONE;
}

static int create_block_device(struct my_block_dev *dev)
{
    /* Initialize the I/O queue */
    spin_lock_init(&dev->lock);
    dev->queue = blk_alloc_queue(GFP_KERNEL);
    if (dev->queue == NULL)
        return -ENOMEM;
        
    blk_queue_make_request(dev->queue, my_make_request);
    blk_queue_logical_block_size(dev->queue, SECTOR_SIZE);
    ...
}

Once upper layer create a bio structure and use submit_bio(rw, bio) to process these bio, submit_bio would do some initialization and statistics work, then calling generic_make_request(bio) , this function would check if make_request_fn is active on this task right now(there might be recursive call of generic_make_request, thus, stacked bio would be considered), if yes, append the bio at the end of bio_list and return, if no, it will pop bio from current->bio_list, and call q->make_request_fn, if using request verison, the function is hooked with blk_queue_bio(q, bio), or, it’s user defined. blk_queue_bio would rearrange the bio in the queue, merge bios to optimize the operation, and copy data from new bio into queue via init_request_from_bio(req, bio). Then calling __blk_run_queue to process the queue, which would eventually call my_block_request.

Process the request

1. request version.
static void my_block_request(struct request_queue *q)
{
	...
	while ((rq = blk_fetch_request(q)) != NULL) {
        pdev = rq->rq_disk->private_data;

        rq_for_each_segment(bvec, rq, iter) {
            start = iter.iter.bi_sector;
            pData = pdev->data + start * SECTOR_SIZE;
            pBuffer = kmap(bvec.bv_page) + bvec.bv_offset;
            switch(rq_data_dir(rq))
            {
                case READ:
                    memcpy(pBuffer, pData, bvec.bv_len);
                    flush_dcache_page(bvec.bv_page);
                    break;
                case WRITE:
                    flush_dcache_page(bvec.bv_page);
                    memcpy(pData, pBuffer, bvec.bv_len);
                    break;
                default:
                    kunmap(bvec.bv_page);
                    goto io_error;
            }
            kunmap(bvec.bv_page);
            pData += bvec.bv_len;
        }
        __blk_end_request_all(rq, 0);
    }
io_error:
    return;
}
2. make_request version
static blk_qc_t my_make_request(struct request_queue *q, struct bio *bio)
{
	...
 	bio_for_each_segment(bvec, bio, iter) {
        pBuffer = kmap(bvec.bv_page) + bvec.bv_offset;
        switch(bio_data_dir(bio))
        {
            case READ:
                memcpy(pBuffer, pData, bvec.bv_len);
                flush_dcache_page(bvec.bv_page);
                break;
            case WRITE:
                flush_dcache_page(bvec.bv_page);
                memcpy(pData, pBuffer, bvec.bv_len);
                break;
            default:
                kunmap(bvec.bv_page);
                goto io_error;
        }
        kunmap(bvec.bv_page);
        pData += bvec.bv_len;
    }
	...
}
3. Makefile
obj-m += block_driver_make_request.o
obj-m += block_driver_request.o

all:
	make -C  /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Demo

After compile the modules, there will be block_driver_make_request.ko and block_driver_request.ko, these two kernel module would have same behavior.

#you would see /dev/sun_block is created after insmod
my-machine$ sudo insmod block_driver_make_request.ko

#you can use mkfs to make a filesystem
my-machine$ sudo mkfs.ext4 /dev/sun_block
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 32768 1k blocks and 8192 inodes
Filesystem UUID: 5f64e6ee-3ed1-4807-a81f-d00063f0466b
Superblock backups stored on blocks: 
	8193, 24577

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

#can mount the ramdisk
my-machine$  sudo mount /dev/sun_block /mnt
my-machine$  ls /mnt
lost+found

References

Revision

  1. Make a draft version of block device driver - 2019.4.3
### 回答1: CentOS 7启动httpd服务失败可能有多种原因,以下是一些常见的解决方法: 1. 检查httpd配置文件是否正确:可以使用命令`httpd -t`检查httpd配置文件是否正确,如果有错误,需要修改配置文件。 2. 检查端口是否被占用:可以使用命令`netstat -tlnp`查看端口是否被占用,如果被占用需要释放端口或修改httpd配置文件中的端口号。 3. 检查httpd服务是否安装:可以使用命令`rpm -qa | grep httpd`查看httpd服务是否安装,如果没有安装需要先安装httpd服务。 4. 检查httpd服务是否启动:可以使用命令`systemctl status httpd`查看httpd服务是否启动,如果没有启动需要使用命令`systemctl start httpd`启动httpd服务。 5. 检查SELinux是否开启:如果SELinux开启,可能会导致httpd服务启动失败,需要使用命令`setenforce 0`关闭SELinux,或者修改SELinux策略。 以上是一些常见的解决方法,如果以上方法都无法解决问题,可以查看httpd服务日志文件,找到具体的错误信息,然后根据错误信息进行解决。 ### 回答2: CentOS 7上的httpd服务启动失败可能有多种原因。以下列出了一些常见问题和解决方法: 1. 端口被占用 当httpd试图占用已被其他程序占用的端口时会启动失败。此时可以通过使用`netstat -tunlp`命令检查端口占用情况,然后杀死占用该端口的进程及时释放端口。或者修改httpd的配置文件,将端口修改为未被占用的端口。 2. 配置文件错误 有时httpd服务的配置文件中可能出现错误,例如语法错误或路径错误等等。在启动httpd服务之前,可以使用`apachectl configtest`命令进行检查,如果输出“Syntax OK”,则表示配置文件没有错误。如果出现错误,则需要根据错误提示进行相应修改。 3. 依赖关系问题 如果httpd依赖的其他程序或库缺失,也会导致启动失败。可以通过使用`systemctl status httpd.service`命令来查看httpd服务状态,如果输出“Failed to start”或“Loaded: failed”,则需要检查依赖关系是否完整。 4. SELinux问题 当SELinux启用时,有时会导致httpd服务启动失败。在这种情况下,可以在SELinux上禁用httpd服务,或者修改httpd配置文件解决SELinux相关的问题。 5. 用户权限问题 httpd服务启动可能需要特定的用户权限。如果使用的用户权限不够,则无法启动。可以尝试使用root用户启动httpd服务,或者根据需要修改相应的用户权限。 ### 回答3: CentOS 7中的Apache HTTP服务器(httpd)是一个常见的Web服务器,如果遇到httpd服务启动失败的情况,可能会影响服务器正常的工作和对外服务的稳定性。本文将提供一些可能会导致httpd服务启动失败的原因,并给出相应的解决方法。 1. 端口被占用 如果端口被其他进程占用,httpd服务就无法启动。可以通过 netstat -tulpn 命令查看端口占用情况,并杀死占用该端口的进程。如果端口被 httpd 服务自身占用,可以通过 systemctl restart httpd 命令重启 httpd 服务;如果是其他进程占用了端口,可以通过 kill 命令杀死该进程或更改 httpd.conf 文件配置,将 httpd 服务的端口改为其他空闲端口,重新启动。 2. 配置文件错误 httpd 服务的配置文件通常是 /etc/httpd/conf/httpd.conf,如果其中存在语法错误、权限问题或者其它配置错误,可能会导致 httpd 服务启动出错。可以通过将 httpd.conf 文件备份后删掉,重新执行 yum install httpd 命令安装 httpd 服务,然后手动修改 httpd.conf 文件,逐个检查每个配置项是否正确,确认无误后重启 httpd 服务。 3. SELinux 问题 SELinux 是 CentOS 7中提供的一种安全模块,它可以对系统文件和应用程序进行安全管控。如果 SELinux 配置不正确,可能会阻止 httpd 服务正常启动。可以通过修改 /etc/selinux/config 文件中 SELINUX=disabled 来暂时关闭 SELinux,然后重新启动 httpd 服务;或者一个更优的方式是,根据日志确定问题原因,使用命令 semanage 或者 setsebool 等工具将相关目录或者配置加入到 SELinux 许可列表中,重新启动 httpd 服务,以恢复服务正常工作。 4. 防火墙问题 如果你的 CentOs 7 服务器启用了防火墙,有可能会导致 httpd 服务启动失败。可以通过检查防火墙相关配置来确定问题原因,解决方案是修改防火墙规则,将端口 80 或者 443 等 httpd 服务需要的端口放行,重新启动 httpd 服务。 总之,当遇到 httpd 服务启动失败时,不要慌张,可以先通过日志或者执行命令查看错误信息,找到错误原因,然后根据错误原因一步一步解决问题。在解决问题过程中注意备份原始配置文件,以免造成不必要的损失。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值