CharybdeBE
CharybdeBE

Reputation: 1843

Read chunks in a disk (kernel programming)

I want to read the first(s) chunks of my disk. I'm currently developing a driver and i want to stock (and retrieve) some meta data (the number of time each chunks is consulted) in the firsts chunk of my disk

I've see How to read a sector using a bio request in Linux kernel and then i start writing the code for the read part :

struct bio *bio = bio_alloc(GFP_NOIO, 1);
struct page *page =  alloc_page(GFP_KERNEL)
struct completion event;
bio->bi_bdev = conf->disks[0].rdev;
bio->bi_sector = (sector_t) 0; 
bio_add_page(bio, page, (sizeof(struct nuda_table)) * conf->nbr_chunk, 0);
init_completion(&event);            
bio->bi_private = &event;
bio->bi_end_io = readComplete;

submit_bio(READ | REQ_SYNC, bio);
wait_for_completion(&event);
bio_put(bio);

But then i don't know where the data that i have read are stored. In the struct page ? Little other question : there is a parameter length in bio_add_page() is this suppose to be bytes ? or chunks ? or other things ?

Thank you in advance

Upvotes: 3

Views: 386

Answers (1)

MappaM
MappaM

Reputation: 921

page_address will return a pointer (void*) that you can use to read or write the content of the page. However, if it is a page in high memory it will only work if the page is mapped.

Using kmap may be preferable, as it will do that check for you :

void *kmap(struct page *page)
{
        might_sleep();
        if (!PageHighMem(page))
               return page_address(page);
        return kmap_high(page);
}

Upvotes: 2

Related Questions