From: zzCherring <zzjolin2005@hotmail.com>
To: linux-kernel@vger.kernel.org
Subject: how to setup struct bio
Date: Thu, 21 Feb 2008 20:41:39 -0800 (PST) [thread overview]
Message-ID: <15627654.post@talk.nabble.com> (raw)
Hi,
I am having problem reading first several blocks from disk(/dev/sda1, for
example). Not sure if it is because i am not doing correctly as to creating
struct bio, filling its fields and sending it down to block device, or it is
because there are certain rools which prevent the first couple of blocks of
disk from being read out, which sounds ridiculous.
Following is how i am doing it now,
Thanks.
// used in completion routine, trying to sync back to the calling //thread.
struct __io_complete_d
{
wait_queue_head_t whead;
unsigned int err;
unsigned int len;
unsigned int flags;
};
// completion routine
int io_complete_r( struct bio *b, unsigned int n, int err )
{
struct __io_complete_d *icd = 0;
if( b != 0 )
{
if( b->bi_private )
{
icd = ( struct __io_complete_d *)b->bi_private;
icd->err = err;
icd->len = n;
icd->flags = 1;
wake_up(&icd->whead);
}
if( b->bi_destructor ) b->bi_destructor(b);
}
return err;
}
// alloc page, send bio down to block device
struct page * read_from_disk(
struct block_device *bdev,
void *d,
bio_end_io_t *io_complete,
sector_t sect_off,
unsigned int od
)
{
struct bio *b = 0;
struct page *pg = 0;
if( bdev == 0 ) return 0;
// having tried alloc_pages(as it is a macro), but failed somehow
pg = __alloc_pages(GFP_KERNEL, od, contig_page_data.node_zonelists +
(GFP_KERNEL & GFP_ZONEMASK));
if( pg )
{
b = bio_alloc(GFP_KERNEL, 1);
if( b == 0)
{
__free_pages( pg, od);
pg = 0;
return 0;
}
b->bi_sector = sect_off;
b->bi_next = 0;
b->bi_bdev = bdev;
b->bi_end_io = io_complete;
b->bi_private = d;
bio_add_page( b, pg, PAGE_SIZE << od, 0 );
submit_bio(0, b);
}
return pg;
}
// example using read_from_disk
void example(
struct block_device *bdev,
unsigned int nblocks
)
{
struct __io_complete_d *icd = 0;
struct page *pg = 0;
if( bdev == 0 )
{
return 0;
}
icd = kmalloc( GFP_KERNEL, sizeof(struct __io_complete_d));
if(icd == 0)
{
goto __end;
}
memset( icd, 0, sizeof(struct __io_complete_d));
icd->flags = 0;
init_waitqueue_head( &icd->whead);
pg = read_from_disk( bdev, icd, io_complete_r, nblock * BLOCK_SIZE
/SECT_SIZE, 0 );
if( pg == 0 ) goto __end;
wait_event( icd->whead, icd->flags != 0 );
if(icd->err == 0)
{
// After successfully reading from disk
}
//.....
_end:
if(icd)
{
kfree(icd);
}
if(pg)
{
__free_pages(pg, 0);
}
}
--
View this message in context: http://www.nabble.com/how-to-setup-struct-bio-tp15627654p15627654.html
Sent from the linux-kernel mailing list archive at Nabble.com.
reply other threads:[~2008-02-22 4:41 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=15627654.post@talk.nabble.com \
--to=zzjolin2005@hotmail.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox