Bug #1632

Seek to end of file returns 136 on CD/DVD drives

Added by steve almost 5 years ago. Updated almost 5 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:alexh% Done:

0%

Category:-
Target version:-

Description

Hi,

The following code always returns 136 (0x88) - no matter what is in
the drive (even no media at all). The same thing happens with cd0, acd0s1
and cd0s1 and acd1 etc. acd0 is a SATA DVD burner, acd1 is a PATA DVD
reader.

int main (int argc, char **argv)
{
int file = open ("/dev/acd0", O_RDONLY);
off_t end = lseek(file, 0, SEEK_END);
printf ("Size = %ld\n", end);
}

History

#1 Updated by alexh almost 5 years ago

I've identified the issue. In devfs getattr vnop I'm doing:
vap->va_bytes = vap->va_size = sizeof(struct devfs_node);
and kern_lseek then does:
new_offset = offset + vattr.va_size;

I'm not quite sure how to solve this. Should the size be set to the physical
size if the device is a disk type? This would also result in fstat to return
the physical size for a disk.

Unless anyone considers it to always set va_size and st_size to the disk size
for a disk-type device in the next few hours or so, I'll do it this way.

Cheers,
Alex Hornung

#2 Updated by alexh almost 5 years ago

oops; I meant "unless anyone considers it to be incorrect to always set
va_size..."

#3 Updated by alexh almost 5 years ago

For reference:
http://www.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html
So the standard does not specify what st_size has to be for devices.

Any opinion on this matter?

Cheers,
Alex Hornung

#4 Updated by steve almost 5 years ago

On Thu, 17 Dec 2009 14:08:49 +0000
"Alex Hornung \(via DragonFly issue tracker\)"
<> wrote:

>
> Alex Hornung <> added the comment:
>
> I've identified the issue. In devfs getattr vnop I'm doing:
> vap->va_bytes = vap->va_size = sizeof(struct devfs_node);
> and kern_lseek then does:
> new_offset = offset + vattr.va_size;
>
> I'm not quite sure how to solve this. Should the size be set to the
> physical size if the device is a disk type? This would also result in
> fstat to return the physical size for a disk.

The one place I know it is used is in ddrescue where it is
expected to return the amount of data that will be read by reading the
device until EOF, which for a DVD would be the amount of data written on
the media but for a hard disk would be the physical size of the disk.

#5 Updated by corecode almost 5 years ago

Alex Hornung (via DragonFly issue tracker) wrote:
> Alex Hornung <> added the comment:
>
> For reference:
> http://www.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html
> So the standard does not specify what st_size has to be for devices.
>
> Any opinion on this matter?

If we have the size of the device in question (i.e. disk), it won't hurt to set it. Else maybe set 0 to avoid confusion? Alternatively seek to EOF could fail.

cheers
simon

#6 Updated by alexh almost 5 years ago

Fixed in commit 2d0767552b6288b28a2ec45d633cfd93fa58ca64.
Steve, just fyi: you need to change your %ld formatter to %lld, as off_t is 64
bit wide.
I've tested this with several hard disks and partitions and a DVD. It seems to
work, but I'll wait for Steve's feedback before closing this issue.

Cheers,
Alex Hornung

#7 Updated by steve almost 5 years ago

On Thu, 17 Dec 2009 15:36:28 +0000
"Alex Hornung \(via DragonFly issue tracker\)"
<> wrote:

> Fixed in commit 2d0767552b6288b28a2ec45d633cfd93fa58ca64.

Works like a charm - Thank you very much.

> Steve, just fyi: you need to change your %ld formatter to %lld, as off_t
> is 64 bit wide.

Yeah I know - it was just a quick lash up to find out why ddrescue
was misbehaving, I'll be deleting it now.

> I've tested this with several hard disks and partitions and a DVD. It
> seems to work, but I'll wait for Steve's feedback before closing this
> issue.

Go ahead and close it.

#8 Updated by dillon almost 5 years ago

:I've identified the issue. In devfs getattr vnop I'm doing:
:vap->va_bytes =3D vap->va_size =3D sizeof(struct devfs_node);
:and kern_lseek then does:
:new_offset =3D offset + vattr.va_size;
:
:I'm not quite sure how to solve this. Should the size be set to the physica=
:l=20
:size if the device is a disk type? This would also result in fstat to retur=
:n=20
:the physical size for a disk.
:
:Unless anyone considers it to always set va_size and st_size to the disk si=
:ze=20
:for a disk-type device in the next few hours or so, I'll do it this way.
:
:Cheers,
:Alex Hornung

BSDs in the past have not supported st_size on disk devices, but
linux has.

I personally think that if we can easily support st_size for disk devices
that we should.

-Matt

#9 Updated by dillon almost 5 years ago

I don't think va_size should ever be set to the size of an internal
structure. That information is not meaningful to consumers / stat.
It should either be something meaningful, like the size of the
disk (partition/slice/etc) or 0.

For softlinks va_size is the length of the softlink string.

-Matt
Matthew Dillon
<>

#10 Updated by dillon almost 5 years ago

Note that %lld is 128 bits on 64-bit machines. The best thing to do is
to cast to size_t or ssize_t:

"... %ju ...", ... (size_t)blah
"... %jd ...", ... (ssize_t)blah

-Matt
Matthew Dillon
<>

#11 Updated by dillon almost 5 years ago

Ugh. never mind. long long is 64 bits. But use size_t anyway.

-Matt
Matthew Dillon
<>

Also available in: Atom PDF