如何在Linux内核中使用生物请求读取扇区

时间:2012-10-04 04:48:54

标签: linux kernel driver block

如何创建自己的生物请求以从磁盘驱动器读取扇区?

我正在尝试以下操作,但它冻结了系统。

static void read_bio()
{
    struct bio *b;
    struct page *p;

    b = bio_alloc(GFP_KERNEL, 1);
    if (!b) {
        printk(KERN_INFO "bio allocation failed\n");
    }
    bio_init(b);

    b->bi_sector = 10000;
    b->bi_bdev = bd;    /* "/dev/sda1" */
    b->bi_end_io = bio_end_clone;

    p = alloc_page(GFP_KERNEL);
    if (!p) {
        printk(KERN_INFO "page allocation failed\n");
    }
    bio_add_page(b, p, PAGE_SIZE, 0);
    b->bi_private = p;

    bio_get(b);
    submit_bio(READ, b);
    printk(KERN_DEBUG "submit read request\n");
}

1 个答案:

答案 0 :(得分:7)

这是一个古老的问题,但无论如何这里是阅读的代码,我希望它能帮助某人:

int readPage(struct block_device *device, sector_t sector, int size,
     struct page *page)
{
    int ret;
    struct completion event;
    struct bio *bio = bio_alloc(GFP_NOIO, 1);
    bio->bi_bdev = device;
    bio->bi_sector = sector;
    bio_add_page(bio, page, size, 0);
    init_completion(&event);
    bio->bi_private = &event;
    bio->bi_end_io = readComplete;
    submit_bio(READ | REQ_SYNC, bio);
    wait_for_completion(&event);
    ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
    bio_put(bio);
    return ret;
}

写作:

void writePage(struct block_device *device,
           sector_t sector, int size, struct page *page)
{
    struct bio *bio = bio_alloc(GFP_NOIO, 1);
    bio->bi_bdev = vnode->blkDevice;
    bio->bi_sector = sector;
    bio_add_page(bio, page, size, 0);
    bio->bi_end_io = writeComplete;
    submit_bio(WRITE_FLUSH_FUA, bio);
}
可以使用alloc_page(GFP_KERNEL)分配

page。同样用于更改page使用page_address(page)中的数据。它返回void*,因此您可以将该指针解释为您想要的任何内容。