How to read a sector using a bio request in Linux kernel

前端 未结 1 1915
不知归路
不知归路 2020-12-28 22:39

How do I create my own bio request to read a sector from the disk drive ?

I am trying the following but it freezes the system.

static void read_bio()         


        
相关标签:
1条回答
  • 2020-12-28 23:03

    It is old question, but anyway here is the code for reading, I hope it will help someone:

    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;
    }
    

    And for writing:

    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 = device;
        bio->bi_sector = sector;
        bio_add_page(bio, page, size, 0);
        bio->bi_end_io = writeComplete;
        submit_bio(WRITE_FLUSH_FUA, bio);
    }
    

    page can be allocated with alloc_page(GFP_KERNEL). Also for changing data in page use page_address(page). It returns void* so you can interpret that pointer as whatever you want.

    0 讨论(0)
提交回复
热议问题