IOCTL to read a disk block

T

Tyrone Jackson

Hello,

I'm very new to using IOCTL's for programming and am having difficulty
getting the information I want by searching Google. Every time I enter
something like, "IOCTL read block" etc. I get a bunch of hits on blocking
reads, etc. I'm not having trouble with blocking I/O.

What I'm after is, I want to know what the IOCTL is for reading a block of
data from an arbitrary block in a filesystem. I've looked through the
include files in /usr/include/sys but not being certain of what I'm
looking for, it's very tough to know if I've looked through all the right
files. So basically, what IOCTL would I use to read block 5,520 from a
file system?

- TJ -

--


oooo$$$$$$$$$$$$oooo
oo$$$$$$$$$$$$$$$$$$$$$$$$o
oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o o$ $$ o$
o $ oo o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$ $$ $$o$
oo $ $ "$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$$o$$o$
"$$$$$$o$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$$$$$$$
$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ """$$$
"$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$
$$$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$o
o$$" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$o
$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o
o$$$oooo$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ o$$$$$$$$$$$$$$$$$
$$$$$$$$"$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$""""""""
"""" $$$$ "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" o$$$
"$$$o """$$$$$$$$$$$$$$$$$$"$$" $$$
$$$o "$$""$$$$$$"""" o$$$
$$$$o oo o$$$"
"$$$$o o$$$$$$o"$$$$o o$$$$
"$$$$$oo ""$$$$o$$$$$o o$$$$""
""$$$$$oooo "$$$o$$$$$$$$$"""
""$$$$$$$oo $$$$$$$$$$
""""$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$"
"$$$""""
 
A

Antoninus Twink

So basically, what IOCTL would I use to read block 5,520 from a file
system?

You don't need anything like an ioctl - at some level it will just come
down to opening the file system and doing an lseek64().

Of course, you would need to know something about how the filesystem is
laid out, what the block size is, etc. to make any sense of the raw
bytes in the filesystem, and there are almost certainly libraries that
do that for you - for example, if you have an ext2/ext3 filesystem then
the e2fslibs provide access functions that will let you iterate over the
blocks in an inode etc.
 
K

Keith Thompson

Eric Sosman said:
Tyrone said:
Hello,
I'm very new to using IOCTL's for programming and am having
difficulty
getting the information I want by searching Google. [...]

comp.unix.programmer is ------+
|
+------> over there.

And I doubt that they'll appreciate a 30-line sig block any more than
we do. Incidentally, the standard signature delimiter is "-- "; the
trailing space is required.
 
E

Erik Trulsson

Tyrone Jackson said:
Hello,

I'm very new to using IOCTL's for programming and am having difficulty
getting the information I want by searching Google. Every time I enter
something like, "IOCTL read block" etc. I get a bunch of hits on blocking
reads, etc. I'm not having trouble with blocking I/O.

What I'm after is, I want to know what the IOCTL is for reading a block of
data from an arbitrary block in a filesystem. I've looked through the
include files in /usr/include/sys but not being certain of what I'm
looking for, it's very tough to know if I've looked through all the right
files. So basically, what IOCTL would I use to read block 5,520 from a
file system?

ioctl() is a very system-specific call that does not even exist in many
systems. You will have to ask in a forum dedicated to your particular system
to find out how you do it there, but at least on Unix (and Unix-derived
systems like Linux) the answer is: None. ioctl() is not used for reading
from a file system. To read a specific block you would just open the device
file corresponding to the disk you are interested in, seek to the correct
position, and read from it, just like you would do with a normal file.




-
<Insert your favourite quote here.>
Erik Trulsson
(e-mail address removed)
 
T

Tyrone Jackson

OK thanks to you and Antonius.

Could you give some example code of reading a particular block? I'm
running FreeBSD if that makes a difference, standard filesystem (/dev/hda3).

Respect

- TJ -


ioctl() is a very system-specific call that does not even exist in many
systems. You will have to ask in a forum dedicated to your particular system
to find out how you do it there, but at least on Unix (and Unix-derived
systems like Linux) the answer is: None. ioctl() is not used for reading
from a file system. To read a specific block you would just open the device
file corresponding to the disk you are interested in, seek to the correct
position, and read from it, just like you would do with a normal file.




-
<Insert your favourite quote here.>
Erik Trulsson
(e-mail address removed)

--


oooo$$$$$$$$$$$$oooo
oo$$$$$$$$$$$$$$$$$$$$$$$$o
oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o o$ $$ o$
o $ oo o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$ $$ $$o$
oo $ $ "$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$$o$$o$
"$$$$$$o$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$$$$$$$
$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ """$$$
"$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$
$$$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$o
o$$" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$o
$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o
o$$$oooo$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ o$$$$$$$$$$$$$$$$$
$$$$$$$$"$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$""""""""
"""" $$$$ "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" o$$$
"$$$o """$$$$$$$$$$$$$$$$$$"$$" $$$
$$$o "$$""$$$$$$"""" o$$$
$$$$o oo o$$$"
"$$$$o o$$$$$$o"$$$$o o$$$$
"$$$$$oo ""$$$$o$$$$$o o$$$$""
""$$$$$oooo "$$$o$$$$$$$$$"""
""$$$$$$$oo $$$$$$$$$$
""""$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$"
"$$$""""
 
K

Keith Thompson

Tyrone Jackson said:
OK thanks to you and Antonius.

Could you give some example code of reading a particular block? I'm
running FreeBSD if that makes a difference, standard filesystem (/dev/hda3).

Antoninus is a troll; he deliberately tries to disrupt this newsgroup.
(If he were really trying to be helpful with your Unix questions, he'd
post to comp.unix.programmer where his answers could be checked.)

As Erik told you in the article to which you're responding, your
question is system-specific, and this is not the place to get good
answers. If you'll post to comp.unix.programmer, your question will
be answered by Unix experts who know about ioctl, rather than by C
experts who most likely don't.

Finally, a bit of advice about posting to technical Usenet groups, not
just this one.

Please don't top-post. This is explained, with reasons for not
doing it, at:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php
You should also trim quoted material when you post a followup.

And *please* lose the 30-line signature. Tradition says a signature
should be no longer than 4 lines, and it's introduced by "-- " (the
trailing space is required). If you want to show off your ASCII art,
put it on a web site somewhere; we don't need to see it on every
article you post.
 
K

Kenny McCormack

Keith Thompson is a troll; he deliberately tries to disrupt this newsgroup.
(If he were really trying to be helpful with your Unix questions, he'd
just answer them and be done with it.)

As another helpful poster, "Han from China" notes, you need to decide
who to listen to, and in general, the ones who are helping you with your
problems are more likely to be deserving of your attention than the ones
who don't.
 
D

dannyx.payton

Keith Thompson is a troll; he deliberately tries to disrupt this newsgroup.
(If he were really trying to be helpful with your Unix questions, he'd
just answer them and be done with it.)

Wrong for 2 reasons:

1) Twink suggested stupid Linux's stupid "lseek64" function
which doesn't exist on FreeBSD, NetBSD, OpenBSD, or Mac OS X
since those modern OS automatically support 64bit filesizes.
The OP later revealed he's using FreeBSD, so Twink's advice is wrong.

2) Twink did not suggest comp.unix.programmer.
Therefore, the OP would have tried lseek64, fail to compile it
on his system, then waited in vain for other comments from
comp.lang.c.
So if the honorable Keith hadn't mentioned comp.unix.programmer,
the OP would have wasted precious days before giving up...

Therefore, you stupid Kenny and you stupid Twink are both idiots
for trying to ruin people's days.

Morons!
 
K

Keith Thompson

Therefore, you stupid Kenny and you stupid Twink are both idiots
for trying to ruin people's days.

And by replying to them, you give them exactly the attention that they
seem to want.
 
W

Wolfgang Draxinger

You don't even need C for that:

dd if=/dev/hda3 count=1 seek=${block} bs=${blocksize}

Well the C version (I'm not going to include error handling, do
that yourself; on modern systems, lseek and off_t likely are
aliases for lseek64 and off64_t):

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/* put the blocksize here, usually 512k */
#define BLOCKSIZE 524288
void *readblock(char const * const pathname, blkcnt_t block)
{
void *block_buf;
int fd;
size_t read_count;
size_t const count = BLOCKSIZE;
off64_t const offset = BLOCKSIZE * block;

block_buf = malloc(BLOCKSIZE);
fd = open(pathname, O_RDONLY);
lseek64(fd, offset, SEEK_SET);
read(fd, block_buf, BLOCKSIZE);
close(fd);
return block_buf;
}

Antoninus is a troll; he deliberately tries to disrupt this
newsgroup. (If he were really trying to be helpful with your
Unix questions, he'd post to comp.unix.programmer where his
answers could be checked.)

Then it's good that a Unix-guy lurks here. And just for the
record: Antonius gave the correct answer.
As Erik told you in the article to which you're responding,
your question is system-specific, and this is not the place to
get good answers.
If you'll post to comp.unix.programmer, your question
will be answered by Unix experts who know about ioctl, rather
than by C experts who most likely don't.

You must admit, that C originates in the Unix environment and
thus a lot of people (wrongly) assume, that C wizards are Unix
wizards, too.
And *please* lose the 30-line signature. Tradition says a
signature should be no longer than 4 lines, and it's introduced
by "-- " (the
trailing space is required). If you want to show off your
ASCII art, put it on a web site somewhere; we don't need to see
it on every article you post.

I double that.

Wolfgang
 
W

Wolfgang Draxinger

Wolfgang said:
/* put the blocksize here, usually 512k */
#define BLOCKSIZE 524288

sorry, I had a momentary lapse here: Usually a block is 512 bytes
(not kibibytes). However that size varies depending on
application and data structure.

Wolfgang
 
C

CBFalconer

Tyrone said:
Could you give some example code of reading a particular block?
I'm running FreeBSD if that makes a difference, standard
filesystem (/dev/hda3).

If you want to use c.l.c you will need to learn not to top-post,
and to reduce your sig to roughly 4 lines or less, with those lines
72 or less chars. long.

Continuing to top-post, or to use such ugly sigs continuously, will
just get you plonked.
 
C

CBFalconer

Wolfgang said:
Keith Thompson wrote:
.... snip ...


Then it's good that a Unix-guy lurks here. And just for the
record: Antonius gave the correct answer.

The point is that Twink is a troll, and deliberately posts
off-topic answers, which do not get adequately criticized here.
For this particular suggestion the correct place is
comp.unix.programmer.

Please do not encourage off-topic posts.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
<http://www.dinkumware.com/c99.aspx> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
K

Keith Thompson

Wolfgang Draxinger said:
Keith Thompson wrote: [...]
As Erik told you in the article to which you're responding,
your question is system-specific, and this is not the place to
get good answers.
If you'll post to comp.unix.programmer, your question
will be answered by Unix experts who know about ioctl, rather
than by C experts who most likely don't.

You must admit, that C originates in the Unix environment and
thus a lot of people (wrongly) assume, that C wizards are Unix
wizards, too.
[...]

Certainly I admit that. It's not surprising that people occasionally
post Unix-specific questions in comp.lang.c. I'm not even
particularly upset by it. But that's why we need to tell them about
comp.unix.programmer.
 
A

Antoninus Twink

Now I can't particiapte in this thread. I don't have a clue how a
system like Unix lays out its files (other than that in the very
general sense I know how computer backing stores work). I don't even
know what the initials IOCTL stand for. So I am locked out, an
unintended consequence of widening topicality, I am sure, but a
consequence nevetheless.

A positive consequence in my opinion!

As no one is an expert on everything, it means that everyone can learn
something from the group - only on clc could broadening one's knowledge
and experience of how C is used on different systems be considered a
negative thing!

I'm similarly unable to provide any useful help in threads about the
Windows API or C++, for example, but I'm always glad to raise my
awareness of these topics by browsing through other people's replies.

On your specific questions, I guess ioctl stands for "i/o control
intstruction" in so far as it stands for anything - presumably like
creat it's a relic from the days when identifiers had to be kept short.
Basically, it's a single API, namely

int ioctl(int d, int request, ...);

that abstracts away differences in functionality provided by different
device drivers: the "request" code and any varargs passed to the
function will be passed on to the device driver that controls the device
associated to the file descriptor d, which will presumably then get the
hardware to implement the request. (Of course, where possible it's
better to use generic higher-level functions, rather than communicating
directly with the device driver.)

A simple example is the CDROMEJECT ioctl provided by the standard Linux
cdrom driver: the following snippet will open the CD drive door if a CD
is present:

int cdrom;
if((cdrom = open("/dev/cdrom")) != -1)
ioctl(cdrom, CDROMEJECT, 0);

So much for ioctls. On the subject of filesystems, the key point is that
*nix abstracts file i/o away into the device driver, and the result is
that the application programmer gets presented with something that as
far as he's concerned is an ordinary file, which can be read and written
to using standard i/o calls ((f)read, (f)write, etc). There's an
informative Wikipedia article:
<http://en.wikipedia.org/wiki/Device_file_system>.

What you read from one of these "device files" is a binary
representation of whatever data structure is used to form the filesystem
on that device, typically a B-tree. Again, for normal purposes this
level of detail is all abstracted away by device drivers, and then
further abstracted away into the "standard" file i/o operations. A
*nix kernel will typically seamlessly support several different types of
filesystem: ext3 and reiserfs are probably the most popular at the
moment, at least on desktop machines.
 
A

Antoninus Twink

The raison d'etre of the group is C, it cannot survive unless most
posts are technical in nature.

You're absolutely right. In the long run, I think things will settle
down with most people coming to accept the new broader "topicality", and
only a few of the usual suspects still insisting on generating heat by
shouting "troll! off topic! not portable!" all over the place.

Unfortunately, in the short term we are in a period of transition, where
the old guard are kicking and screaming hard. All we can do is try to
post as much useful technical information as possible, and avoid
becoming involved in polemic unless the provocation is so great that a
response is needed to clarify the new prevailing outlook on clc.
 
T

Tyrone Jackson

OK thanks to you and Antonius.

Could you give some example code of reading a particular block? I'm
running FreeBSD if that makes a difference, standard filesystem (/dev/hda3).

Respect

- TJ -


ioctl() is a very system-specific call that does not even exist in many
systems. You will have to ask in a forum dedicated to your particular system
to find out how you do it there, but at least on Unix (and Unix-derived
systems like Linux) the answer is: None. ioctl() is not used for reading
from a file system. To read a specific block you would just open the device
file corresponding to the disk you are interested in, seek to the correct
position, and read from it, just like you would do with a normal file.




-
<Insert your favourite quote here.>
Erik Trulsson
(e-mail address removed)

--


oooo$$$$$$$$$$$$oooo
oo$$$$$$$$$$$$$$$$$$$$$$$$o
oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o o$ $$ o$
o $ oo o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$ $$ $$o$
oo $ $ "$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$$o$$o$
"$$$$$$o$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$$$$$$$
$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ """$$$
"$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$
$$$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$o
o$$" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$o
$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o
o$$$oooo$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ o$$$$$$$$$$$$$$$$$
$$$$$$$$"$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$""""""""
"""" $$$$ "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" o$$$
"$$$o """$$$$$$$$$$$$$$$$$$"$$" $$$
$$$o "$$""$$$$$$"""" o$$$
$$$$o oo o$$$"
"$$$$o o$$$$$$o"$$$$o o$$$$
"$$$$$oo ""$$$$o$$$$$o o$$$$""
""$$$$$oooo "$$$o$$$$$$$$$"""
""$$$$$$$oo $$$$$$$$$$
""""$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$"
"$$$""""
 
K

Keith Thompson

Tyrone Jackson said:
OK thanks to you and Antonius.

Could you give some example code of reading a particular block? I'm
running FreeBSD if that makes a difference, standard filesystem (/dev/hda3).

I don't know what Antoninus told you, but what Erik told you was
correct: you need to ask in a system-specific newsgroup, not here.
comp.unix.programmer would probably be a good start.

I suggest that shortening your signature and not top-posting will make
it more likely that people will be willing to help you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top