* Getting a block from a block device?
@ 2011-11-08 11:06 Dan Luedtke
[not found] ` <CACzurzsCpKN_9Ew+g-HLvH=GuPQU=Tzf8yrimf-zDOjPtFsF2w@mail.gmail.com>
2011-11-08 12:08 ` Javier Martinez Canillas
0 siblings, 2 replies; 11+ messages in thread
From: Dan Luedtke @ 2011-11-08 11:06 UTC (permalink / raw)
To: kernelnewbies
Hallo everyone,
could someone please give me a hint on how to get a block (e.g. 512
bytes) from a block device from a kernel module?
What .txt should I consult? Which headerfiles must I include?
Where can I find the required function's definitions?
In userspace I used fopen() with the device path as filename, I bet it
isn't that easy in kernel mode.
I am using kernel sources from gentoo: 3.0.6-gentoo, but I can use
vanilla if it really helps.
thanks,
Dan
--
Dan Luedtke
http://www.danrl.de
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
[not found] ` <CACzurzsCpKN_9Ew+g-HLvH=GuPQU=Tzf8yrimf-zDOjPtFsF2w@mail.gmail.com>
@ 2011-11-08 11:56 ` Dan Luedtke
2011-11-08 11:59 ` rohan puri
2011-11-08 12:12 ` Alexandru Juncu
0 siblings, 2 replies; 11+ messages in thread
From: Dan Luedtke @ 2011-11-08 11:56 UTC (permalink / raw)
To: kernelnewbies
Hallo,
On Tue, Nov 8, 2011 at 12:27 PM, Stephen Gream <poisonthemon@gmail.com> wrote:
> Once you have the device registered (on /sys or /dev), register a read
> callback on the file and use copy_to_user to output the data
I read that article you referred to, but I don't think it addresses my
problem, as I may have been more clear on what I want to achieve
before. Sorry for that!
I want to eventually implement a file system, and therefore I am
studying the kernel sources to get an idea about all that.
Here is my general understanding on how thinks may work :)
virtual file system (VFS) <-[1]-> my file system implementation
<-[2]-> a block device
As I understood, all these subsystems are running in kernel mode.
For [1] I read vfs.txt and learned about the VFS-API
For [2] I have no idea where I can find the API documentation, however
there were some hints on the book "Linux Driver Development" from
O'Reilly in chapter "block devices". Nothing really helpful, as they
were talking about registering new block devices. I want to use
already present devices where I expect my filesystem to be present on.
To check that, I have to read the first 512 bytes.
The userspace implementation I was talking about previously is
something like a mkfs.myfilesystem, that's why i referred to fopen()
there.
Thank you!
Greetings,
Dan
--
Dan Luedtke
http://www.danrl.de
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 11:56 ` Dan Luedtke
@ 2011-11-08 11:59 ` rohan puri
2011-11-08 12:28 ` mani
2011-11-08 12:12 ` Alexandru Juncu
1 sibling, 1 reply; 11+ messages in thread
From: rohan puri @ 2011-11-08 11:59 UTC (permalink / raw)
To: kernelnewbies
On Tue, Nov 8, 2011 at 5:26 PM, Dan Luedtke <maildanrl@googlemail.com>wrote:
> Hallo,
>
> On Tue, Nov 8, 2011 at 12:27 PM, Stephen Gream <poisonthemon@gmail.com>
> wrote:
> > Once you have the device registered (on /sys or /dev), register a read
> > callback on the file and use copy_to_user to output the data
>
> I read that article you referred to, but I don't think it addresses my
> problem, as I may have been more clear on what I want to achieve
> before. Sorry for that!
> I want to eventually implement a file system, and therefore I am
> studying the kernel sources to get an idea about all that.
>
> Here is my general understanding on how thinks may work :)
>
> virtual file system (VFS) <-[1]-> my file system implementation
> <-[2]-> a block device
>
> As I understood, all these subsystems are running in kernel mode.
>
> For [1] I read vfs.txt and learned about the VFS-API
> For [2] I have no idea where I can find the API documentation, however
> there were some hints on the book "Linux Driver Development" from
> O'Reilly in chapter "block devices". Nothing really helpful, as they
> were talking about registering new block devices. I want to use
> already present devices where I expect my filesystem to be present on.
> To check that, I have to read the first 512 bytes.
>
> The userspace implementation I was talking about previously is
> something like a mkfs.myfilesystem, that's why i referred to fopen()
> there.
>
> Thank you!
>
> Greetings,
>
> Dan
> --
> Dan Luedtke
> http://www.danrl.de
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
Dan,
You need to look at other block-based file systems in the kernel sources in
fs dir for examples and understand how they do it.
Regards,
Rohan Puri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20111108/935dbce9/attachment.html
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 11:06 Getting a block from a block device? Dan Luedtke
[not found] ` <CACzurzsCpKN_9Ew+g-HLvH=GuPQU=Tzf8yrimf-zDOjPtFsF2w@mail.gmail.com>
@ 2011-11-08 12:08 ` Javier Martinez Canillas
1 sibling, 0 replies; 11+ messages in thread
From: Javier Martinez Canillas @ 2011-11-08 12:08 UTC (permalink / raw)
To: kernelnewbies
On Tue, Nov 8, 2011 at 12:06 PM, Dan Luedtke <maildanrl@googlemail.com> wrote:
> Hallo everyone,
>
> could someone please give me a hint on how to get a block (e.g. 512
> bytes) from a block device from a kernel module?
>
> What .txt should I consult? Which headerfiles must I include?
> Where can I find the required function's definitions?
>
> In userspace I used fopen() with the device path as filename, I bet it
> isn't that easy in kernel mode.
>
Well even when is not considered a good practice, you can invoke the
file operations syscall handlers from your module.
Look at this LJ article: http://www.linuxjournal.com/node/8110/print
But I think that the right question is not _how_ to do it but _why_.
The kernel can't assume a filesystem layout and the location of a file.
Can you tell what are you trying to achieve? Maybe there are better
ways to do it.
Best regards,
--
Javier Mart?nez Canillas
(+34) 682 39 81 69
Barcelona, Spain
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 11:56 ` Dan Luedtke
2011-11-08 11:59 ` rohan puri
@ 2011-11-08 12:12 ` Alexandru Juncu
2011-11-08 12:21 ` Stephen Gream
1 sibling, 1 reply; 11+ messages in thread
From: Alexandru Juncu @ 2011-11-08 12:12 UTC (permalink / raw)
To: kernelnewbies
On Tue, Nov 8, 2011 at 1:56 PM, Dan Luedtke <maildanrl@googlemail.com> wrote:
> Hallo,
>
> On Tue, Nov 8, 2011 at 12:27 PM, Stephen Gream <poisonthemon@gmail.com> wrote:
>> Once you have the device registered (on /sys or /dev), register a read
>> callback on the file and use copy_to_user to output the data
>
> I read that article you referred to, but I don't think it addresses my
> problem, as I may have been more clear on what I want to achieve
> before. Sorry for that!
> I want to eventually implement a file system, and therefore I am
> studying the kernel sources to get an idea about all that.
>
> Here is my general understanding on how thinks may work :)
>
> virtual file system (VFS) <-[1]-> my file system implementation
> <-[2]-> a block device
>
> As I understood, all these subsystems are running in kernel mode.
>
> For [1] I read vfs.txt and learned about the VFS-API
> For [2] I have no idea where I can find the API documentation, however
> there were some hints on the book "Linux Driver Development" from
> O'Reilly in chapter "block devices". Nothing really helpful, as they
> were talking about registering new block devices. I want to use
> already present devices where I expect my filesystem to be present on.
> To check that, I have to read the first 512 bytes.
>
> The userspace implementation I was talking about previously is
> something like a mkfs.myfilesystem, that's why i referred to fopen()
> there.
>
> Thank you!
>
> Greetings,
While in the kernel you would be able to read the blocks from a device
without using a file path (you don't need to open a file and read from
it). You will need to interact with the IO System.
I would redirect you to learn about struct gendisk and struct bio.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 12:12 ` Alexandru Juncu
@ 2011-11-08 12:21 ` Stephen Gream
0 siblings, 0 replies; 11+ messages in thread
From: Stephen Gream @ 2011-11-08 12:21 UTC (permalink / raw)
To: kernelnewbies
http://web.archive.org/web/20100711055535/http://cs.anu.edu.au/students/comp3300/ass2.php
That's an old assignment from my uni. It should get you across the
basics of filesystem implementation if you work through it ;)
On Tue, Nov 8, 2011 at 11:12 PM, Alexandru Juncu <alex.juncu@rosedu.org> wrote:
> On Tue, Nov 8, 2011 at 1:56 PM, Dan Luedtke <maildanrl@googlemail.com> wrote:
>> Hallo,
>>
>> On Tue, Nov 8, 2011 at 12:27 PM, Stephen Gream <poisonthemon@gmail.com> wrote:
>>> Once you have the device registered (on /sys or /dev), register a read
>>> callback on the file and use copy_to_user to output the data
>>
>> I read that article you referred to, but I don't think it addresses my
>> problem, as I may have been more clear on what I want to achieve
>> before. Sorry for that!
>> I want to eventually implement a file system, and therefore I am
>> studying the kernel sources to get an idea about all that.
>>
>> Here is my general understanding on how thinks may work :)
>>
>> virtual file system (VFS) <-[1]-> my file system implementation
>> <-[2]-> a block device
>>
>> As I understood, all these subsystems are running in kernel mode.
>>
>> For [1] I read vfs.txt and learned about the VFS-API
>> For [2] I have no idea where I can find the API documentation, however
>> there were some hints on the book "Linux Driver Development" from
>> O'Reilly in chapter "block devices". Nothing really helpful, as they
>> were talking about registering new block devices. I want to use
>> already present devices where I expect my filesystem to be present on.
>> To check that, I have to read the first 512 bytes.
>>
>> The userspace implementation I was talking about previously is
>> something like a mkfs.myfilesystem, that's why i referred to fopen()
>> there.
>>
>> Thank you!
>>
>> Greetings,
>
>
> While in the kernel you would be able to read the blocks from a device
> without using a file path (you don't need to open a file and read from
> it). You will need to interact with the IO System.
>
> I would redirect you to learn about struct gendisk and struct bio.
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 11:59 ` rohan puri
@ 2011-11-08 12:28 ` mani
2011-11-08 14:02 ` Dan Luedtke
0 siblings, 1 reply; 11+ messages in thread
From: mani @ 2011-11-08 12:28 UTC (permalink / raw)
To: kernelnewbies
if you wanna open a block device specificallyy
you can look into fs/block_dev.c
struct block_device *open_bdev_exclusive(const char *path, fmode_t mode,
void *holder)
Also i think if you are interested in reading the first sector
then you can probably check the block/genhd.c file
get the disk (i.e struct genhd) then the partition (struct hdpart)
you will be able to get the first sector.
Thanks
On Tue, Nov 8, 2011 at 5:29 PM, rohan puri <rohan.puri15@gmail.com> wrote:
>
>
> On Tue, Nov 8, 2011 at 5:26 PM, Dan Luedtke <maildanrl@googlemail.com>wrote:
>
>> Hallo,
>>
>> On Tue, Nov 8, 2011 at 12:27 PM, Stephen Gream <poisonthemon@gmail.com>
>> wrote:
>> > Once you have the device registered (on /sys or /dev), register a read
>> > callback on the file and use copy_to_user to output the data
>>
>> I read that article you referred to, but I don't think it addresses my
>> problem, as I may have been more clear on what I want to achieve
>> before. Sorry for that!
>> I want to eventually implement a file system, and therefore I am
>> studying the kernel sources to get an idea about all that.
>>
>> Here is my general understanding on how thinks may work :)
>>
>> virtual file system (VFS) <-[1]-> my file system implementation
>> <-[2]-> a block device
>>
>> As I understood, all these subsystems are running in kernel mode.
>>
>> For [1] I read vfs.txt and learned about the VFS-API
>> For [2] I have no idea where I can find the API documentation, however
>> there were some hints on the book "Linux Driver Development" from
>> O'Reilly in chapter "block devices". Nothing really helpful, as they
>> were talking about registering new block devices. I want to use
>> already present devices where I expect my filesystem to be present on.
>> To check that, I have to read the first 512 bytes.
>>
>> The userspace implementation I was talking about previously is
>> something like a mkfs.myfilesystem, that's why i referred to fopen()
>> there.
>>
>> Thank you!
>>
>> Greetings,
>>
>> Dan
>> --
>> Dan Luedtke
>> http://www.danrl.de
>>
>> _______________________________________________
>> Kernelnewbies mailing list
>> Kernelnewbies at kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>
> Dan,
>
> You need to look at other block-based file systems in the kernel sources
> in fs dir for examples and understand how they do it.
>
> Regards,
> Rohan Puri
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20111108/beed6f3f/attachment.html
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 12:28 ` mani
@ 2011-11-08 14:02 ` Dan Luedtke
2011-11-08 15:18 ` Manish Katiyar
0 siblings, 1 reply; 11+ messages in thread
From: Dan Luedtke @ 2011-11-08 14:02 UTC (permalink / raw)
To: kernelnewbies
Thanks for all the answers!
> You need to look at other block-based file systems in the kernel sources in fs dir for examples and understand how they do it.
I looked at fs/minix/* fs/fat/* and peeked into fs/ext2/, but got more
confused so I decided to ask for a starting point.
> Also i think if you are interested in reading the first sector
> then you can probably check the block/genhd.c file
> get the disk (i.e struct genhd) then the partition (struct hdpart)
> you will be able to get the first sector.
Thanks, a well documented source file. However, shouldn't I get the
right partition (I called it block device in previously sent mails,
thought of e.g. /dev/sda2 as a device not a partition) from VFS when a
user requestes mounting it?
> That's an old assignment from my uni.
Wow, exactly what I needed! Thanks for this starting point.
> I would redirect you to learn about struct gendisk and struct bio.
Noted, I will take a look after studying the vvfs-files from Stephens link.
> Can you tell what are you trying to achieve?
I'd like to implement a filesystem as kernel modul (like fat, ext2,
...). The filesystem is a very simple one, based on chained blocks
(similar to linked lists in c). I "invented" it when I tried to write
a bootloader that does not require a second stage to read an kernel
image. (This is all more or less a "because we can"-project, no
commercial background, just learning).
Once again, thank you guys!
Greetings,
Dan
--
Dan Luedtke
http://www.danrl.de
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 14:02 ` Dan Luedtke
@ 2011-11-08 15:18 ` Manish Katiyar
2011-11-09 4:54 ` rohan puri
0 siblings, 1 reply; 11+ messages in thread
From: Manish Katiyar @ 2011-11-08 15:18 UTC (permalink / raw)
To: kernelnewbies
On Tue, Nov 8, 2011 at 6:02 AM, Dan Luedtke <maildanrl@googlemail.com> wrote:
> Thanks for all the answers!
>
>> You need to look at other block-based file systems in the kernel sources in fs dir for examples and understand how they do it.
> I looked at fs/minix/* fs/fat/* and peeked into fs/ext2/, but got more
> confused so I decided to ask for a starting point.
Hi Dan,
You may find https://github.com/mkatiyar/testfs helpful.
>
>> Also i think if you are interested in reading the first sector
>> then you can probably check the block/genhd.c file
>> get the disk (i.e struct genhd) then the partition (struct hdpart)
>> you will be able to get the first sector.
> Thanks, a well documented source file. However, shouldn't I get the
> right partition (I called it block device in previously sent mails,
> thought of e.g. /dev/sda2 as a device not a partition) from VFS when a
> user requestes mounting it?
>
>> That's an old assignment from my uni.
> Wow, exactly what I needed! Thanks for this starting point.
>
>> I would redirect you to learn about struct gendisk and struct bio.
> Noted, I will take a look after studying the vvfs-files from Stephens link.
>
>> Can you tell what are you trying to achieve?
> I'd like to implement a filesystem as kernel modul (like fat, ext2,
> ...). The filesystem is a very simple one, based on chained blocks
> (similar to linked lists in c). I "invented" it when I tried to write
> a bootloader that does not require a second stage to read an kernel
> image. (This is all more or less a "because we can"-project, no
> commercial background, just learning).
>
> Once again, thank you guys!
>
> Greetings,
>
> Dan
> --
> Dan Luedtke
> http://www.danrl.de
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
--
Thanks -
Manish
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-08 15:18 ` Manish Katiyar
@ 2011-11-09 4:54 ` rohan puri
2011-11-09 7:32 ` Dan Luedtke
0 siblings, 1 reply; 11+ messages in thread
From: rohan puri @ 2011-11-09 4:54 UTC (permalink / raw)
To: kernelnewbies
On Tue, Nov 8, 2011 at 8:48 PM, Manish Katiyar <mkatiyar@gmail.com> wrote:
> On Tue, Nov 8, 2011 at 6:02 AM, Dan Luedtke <maildanrl@googlemail.com>
> wrote:
> > Thanks for all the answers!
> >
> >> You need to look at other block-based file systems in the kernel
> sources in fs dir for examples and understand how they do it.
> > I looked at fs/minix/* fs/fat/* and peeked into fs/ext2/, but got more
> > confused so I decided to ask for a starting point.
>
> Hi Dan,
>
> You may find https://github.com/mkatiyar/testfs helpful.
>
> >
> >> Also i think if you are interested in reading the first sector
> >> then you can probably check the block/genhd.c file
> >> get the disk (i.e struct genhd) then the partition (struct hdpart)
> >> you will be able to get the first sector.
> > Thanks, a well documented source file. However, shouldn't I get the
> > right partition (I called it block device in previously sent mails,
> > thought of e.g. /dev/sda2 as a device not a partition) from VFS when a
> > user requestes mounting it?
> >
> >> That's an old assignment from my uni.
> > Wow, exactly what I needed! Thanks for this starting point.
> >
> >> I would redirect you to learn about struct gendisk and struct bio.
> > Noted, I will take a look after studying the vvfs-files from Stephens
> link.
> >
> >> Can you tell what are you trying to achieve?
> > I'd like to implement a filesystem as kernel modul (like fat, ext2,
> > ...). The filesystem is a very simple one, based on chained blocks
> > (similar to linked lists in c). I "invented" it when I tried to write
> > a bootloader that does not require a second stage to read an kernel
> > image. (This is all more or less a "because we can"-project, no
> > commercial background, just learning).
> >
> > Once again, thank you guys!
> >
> > Greetings,
> >
> > Dan
> > --
> > Dan Luedtke
> > http://www.danrl.de
> >
> > _______________________________________________
> > Kernelnewbies mailing list
> > Kernelnewbies at kernelnewbies.org
> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> >
>
>
>
> --
> Thanks -
> Manish
>
Nice reference manish :)
Regards,
Rohan Puri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20111109/73ec04e4/attachment.html
^ permalink raw reply [flat|nested] 11+ messages in thread
* Getting a block from a block device?
2011-11-09 4:54 ` rohan puri
@ 2011-11-09 7:32 ` Dan Luedtke
0 siblings, 0 replies; 11+ messages in thread
From: Dan Luedtke @ 2011-11-09 7:32 UTC (permalink / raw)
To: kernelnewbies
> You may find https://github.com/mkatiyar/testfs helpful.
Thanks! That helped me to get an idea of what actually happens.
--
Dan Luedtke
http://www.danrl.de
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2011-11-09 7:32 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-08 11:06 Getting a block from a block device? Dan Luedtke
[not found] ` <CACzurzsCpKN_9Ew+g-HLvH=GuPQU=Tzf8yrimf-zDOjPtFsF2w@mail.gmail.com>
2011-11-08 11:56 ` Dan Luedtke
2011-11-08 11:59 ` rohan puri
2011-11-08 12:28 ` mani
2011-11-08 14:02 ` Dan Luedtke
2011-11-08 15:18 ` Manish Katiyar
2011-11-09 4:54 ` rohan puri
2011-11-09 7:32 ` Dan Luedtke
2011-11-08 12:12 ` Alexandru Juncu
2011-11-08 12:21 ` Stephen Gream
2011-11-08 12:08 ` Javier Martinez Canillas
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).