Skip to contentSkip to conversation

how do i read a block

DISCUSSION shampavman 6 participants 8 messages May 13, 2013 – May 15, 2013

lore
Share & tools
lore

shampavman

author
#0 lore raw

Hi all,

Supposing i create a file of size 10K, it will occupy 2 blocks (4K each). Now if i want to read only 1 block from it how can i do it?

read(fd, buf, 4096) ;
would this mean i would read the first block and all its contents?

Thanks

#1 lore raw
Quoted 7 lines · shampavman

Hi all,

Supposing i create a file of size 10K, it will occupy 2 blocks (4K each). Now if i want to read only 1 block from it how can i do it?

read(fd, buf, 4096) ;
would this mean i would read the first block and all its contents?

This isn't a kernel question, it's a C 101 question.

But yes, if you only want to read the first 4K, you just read the first 4K. -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130513/49a9d745/attachment.bin

#2 lore raw
In reply to shampavman

El 13/05/2013 10:07, "shampavman" <[email protected]> va escriure:

Quoted 8 lines

Hi all,

Supposing i create a file of size 10K, it will occupy 2 blocks (4K each). Now if i want to read only 1 block from it how can i do it?

read(fd, buf, 4096) ;
would this mean i would read the first block and all its contents?

You actually might even read more, depending on your file system. The dentrys and inodes are stored in blocks on the disk as well. You can try to access your disk through /dev/sdX directly, but beware that when writing you will most probably destroy your data on that disk.

Regards,
Matthias

Quoted 8 lines

Thanks

_______________________________________________
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/20130513/2c2251a1/attachment.html

#3 lore raw

Hi,

Quoted 13 lines · Matthias Brugger

El 13/05/2013 10:07, "shampavman" <[email protected]> va escriure:

Quoted 8 lines

Hi all,

Supposing i create a file of size 10K, it will occupy 2 blocks (4K each). Now if i want to read only 1 block from it how can i do it?

read(fd, buf, 4096) ;
would this mean i would read the first block and all its contents?

One way to do this is use the FIBMAP ioctl to the get the block numbers of a file and then seek the disk /dev/sdX directly and use the read() to get data block.

Regards.

author
#4 lore raw
Quoted 9 lines · Prashant Shah

Hi,

Quoted 5 lines · Matthias Brugger

El 13/05/2013 10:07, "shampavman" <[email protected]> va escriure:

Quoted 2 lines

Hi all,

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Supposing i create a file of size 10K, it will occupy 2 blocks (4K each). Now if i want to read only 1 block from it how can i do it?

read(fd, buf, 4096) ;
would this mean i would read the first block and all its contents?

One way to do this is use the FIBMAP ioctl to the get the block numbers of a file and then seek the disk /dev/sdX directly and use the read() to get data block.

But why does a simple read not turn out 1 block for me?
Eg, i know my block size if 4k and my file (according to du) reports that it's using 8k of space.
So when i read the file via the read() and ask it to give me the 'first 4k' bytes, would that not return 1 block ?

thanks

Quoted 2 lines

Regards.

#5 lore raw
Quoted 1 line · shampavman

But why does a simple read not turn out 1 block for me?

So explain to us why you think a read of 4096 bytes is reading something other than 1 4K block?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130514/3a107ee9/attachment.bin

#6 lore raw
In reply to shampavman
Quoted 4 lines · shampavman

Hi all,

Supposing i create a file of size 10K, it will occupy 2 blocks (4K each).

I must be missing something obvious. Why does a file of 10K only needs 2 blocks of 4K ? You haven't mentioned that it has any holes in it.

Quoted 13 lines

Now if i want to read only 1 block from it how can i do it?

read(fd, buf, 4096) ;
would this mean i would read the first block and all its contents?

Thanks

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Signature · 5 lines
Thanks -
Manish
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130514/19439946/attachment.html 
#7 lore raw
In reply to shampavman
Quoted 5 lines · shampavman

Hi all,

Supposing i create a file of size 10K, it will occupy 2 blocks (4K each). Now if i want to read only 1 block from it how can i do it?

It will require 3 blocks.

Quoted 3 lines

read(fd, buf, 4096) ;
would this mean i would read the first block and all its contents?

It may be read even further (readahead)

If you are in kernel mode, and if you want to implement your own filesystem and want to read a single block, you can use the sb_bread function.

--
Sankar P
http://psankar.blogspot.com