public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Compatible fstat()
@ 2005-11-08 15:48 linux-os (Dick Johnson)
  2005-11-08 17:10 ` Parag Warudkar
  0 siblings, 1 reply; 19+ messages in thread
From: linux-os (Dick Johnson) @ 2005-11-08 15:48 UTC (permalink / raw)
  To: Linux kernel



Hello,

The Linux fstat() doesn't return any information number of blocks,
or the byte-length of a physical hard disk.

       Device name : /dev/hda
     Device st_dev : 00000301
    Device st_rdev : 00000300
    Device st_size : 0
  Device st_blocks : 0
Device st_blksize : 4096
       Device name : /dev/sda
     Device st_dev : 00000301
    Device st_rdev : 00000800
    Device st_size : 0
  Device st_blocks : 0
Device st_blksize : 4096
       Device name : /dev/sdb
     Device st_dev : 00000301
    Device st_rdev : 00000810
    Device st_size : 0
  Device st_blocks : 0
Device st_blksize : 4096
       Device name : /dev/sdc
     Device st_dev : 00000301
    Device st_rdev : 00000820
    Device st_size : 0
  Device st_blocks : 0
Device st_blksize : 4096

So, in a PORTABLE way, how do I find the block device with a
certain size, perhaps even the largest or smallest? I need to
have a customer plug in an external drive (USB) and be able
to determine if it's big enough for a raw-data write (no
file-system).

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 15:48 Compatible fstat() linux-os (Dick Johnson)
@ 2005-11-08 17:10 ` Parag Warudkar
  2005-11-08 17:22   ` Al Viro
  2005-11-08 17:53   ` linux-os (Dick Johnson)
  0 siblings, 2 replies; 19+ messages in thread
From: Parag Warudkar @ 2005-11-08 17:10 UTC (permalink / raw)
  To: linux-os (Dick Johnson); +Cc: Linux kernel


On Nov 8, 2005, at 10:48 AM, linux-os ((Dick Johnson)) wrote:

> The Linux fstat() doesn't return any information number of blocks,
> or the byte-length of a physical hard disk.

I don't think (f)stat returns size and blocks information about a  
block device on any UNIX platform.

But I don't know for sure how to get it - perhaps ioctl on the  
device? BLKGETSIZE?

Parag

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 17:10 ` Parag Warudkar
@ 2005-11-08 17:22   ` Al Viro
  2005-11-08 17:56     ` linux-os (Dick Johnson)
                       ` (2 more replies)
  2005-11-08 17:53   ` linux-os (Dick Johnson)
  1 sibling, 3 replies; 19+ messages in thread
From: Al Viro @ 2005-11-08 17:22 UTC (permalink / raw)
  To: Parag Warudkar; +Cc: linux-os (Dick Johnson), Linux kernel

On Tue, Nov 08, 2005 at 12:10:25PM -0500, Parag Warudkar wrote:
> 
> On Nov 8, 2005, at 10:48 AM, linux-os ((Dick Johnson)) wrote:
> 
> >The Linux fstat() doesn't return any information number of blocks,
> >or the byte-length of a physical hard disk.
> 
> I don't think (f)stat returns size and blocks information about a  
> block device on any UNIX platform.
> 
> But I don't know for sure how to get it - perhaps ioctl on the  
> device? BLKGETSIZE?

	fd = open(bdev, O_RDONLY);
	lseek(fd, SEEK_END, 0);
	size = lseek(fd, SEEK_SET, 0);
	close(fd);

i.e. same as for regular files.  Won't be portable, though...

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 17:10 ` Parag Warudkar
  2005-11-08 17:22   ` Al Viro
@ 2005-11-08 17:53   ` linux-os (Dick Johnson)
  2005-11-08 18:04     ` Parag Warudkar
  1 sibling, 1 reply; 19+ messages in thread
From: linux-os (Dick Johnson) @ 2005-11-08 17:53 UTC (permalink / raw)
  To: Parag Warudkar; +Cc: Linux kernel


On Tue, 8 Nov 2005, Parag Warudkar wrote:

>
> On Nov 8, 2005, at 10:48 AM, linux-os ((Dick Johnson)) wrote:
>
>> The Linux fstat() doesn't return any information number of blocks,
>> or the byte-length of a physical hard disk.
>
> I don't think (f)stat returns size and blocks information about a
> block device on any UNIX platform.
>
> But I don't know for sure how to get it - perhaps ioctl on the
> device? BLKGETSIZE?
>
> Parag
>

I'll see if it works on my Sun. Thanks.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 17:22   ` Al Viro
@ 2005-11-08 17:56     ` linux-os (Dick Johnson)
  2005-11-08 17:58     ` Parag Warudkar
  2005-11-08 18:49     ` Theodore Ts'o
  2 siblings, 0 replies; 19+ messages in thread
From: linux-os (Dick Johnson) @ 2005-11-08 17:56 UTC (permalink / raw)
  To: Al Viro; +Cc: Parag Warudkar, Linux kernel


On Tue, 8 Nov 2005, Al Viro wrote:

> On Tue, Nov 08, 2005 at 12:10:25PM -0500, Parag Warudkar wrote:
>>
>> On Nov 8, 2005, at 10:48 AM, linux-os ((Dick Johnson)) wrote:
>>
>>> The Linux fstat() doesn't return any information number of blocks,
>>> or the byte-length of a physical hard disk.
>>
>> I don't think (f)stat returns size and blocks information about a
>> block device on any UNIX platform.
>>
>> But I don't know for sure how to get it - perhaps ioctl on the
>> device? BLKGETSIZE?
>
> 	fd = open(bdev, O_RDONLY);
> 	lseek(fd, SEEK_END, 0);
> 	size = lseek(fd, SEEK_SET, 0);
> 	close(fd);
>
> i.e. same as for regular files.  Won't be portable, though...
>
I think lseek has more of a chance than ioctl(), though.
Thanks.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 17:22   ` Al Viro
  2005-11-08 17:56     ` linux-os (Dick Johnson)
@ 2005-11-08 17:58     ` Parag Warudkar
  2005-11-08 18:10       ` linux-os (Dick Johnson)
  2005-11-08 18:49     ` Theodore Ts'o
  2 siblings, 1 reply; 19+ messages in thread
From: Parag Warudkar @ 2005-11-08 17:58 UTC (permalink / raw)
  To: Al Viro; +Cc: linux-os (Dick Johnson), Linux kernel


On Nov 8, 2005, at 12:22 PM, Al Viro wrote:
>
> 	fd = open(bdev, O_RDONLY);
> 	lseek(fd, SEEK_END, 0);
> 	size = lseek(fd, SEEK_SET, 0);
> 	close(fd);
>
> i.e. same as for regular files.  Won't be portable, though...

For some reason this didn't work for bdev == "/dev/hda" - Size is  
returned as 0..

Parag

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 17:53   ` linux-os (Dick Johnson)
@ 2005-11-08 18:04     ` Parag Warudkar
  0 siblings, 0 replies; 19+ messages in thread
From: Parag Warudkar @ 2005-11-08 18:04 UTC (permalink / raw)
  To: linux-os (Dick Johnson); +Cc: Linux kernel


On Nov 8, 2005, at 12:53 PM, linux-os ((Dick Johnson)) wrote:
>>
>
> I'll see if it works on my Sun. Thanks.
>

Sun? ioctls being what they are it most likely won't work. Since  
there doesn't seem to be a portable way of doing this my response was  
linux-centric. (I know at least fstat doesn't give what you are  
looking for on Linux and *BSD)

Parag

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 17:58     ` Parag Warudkar
@ 2005-11-08 18:10       ` linux-os (Dick Johnson)
  2005-11-08 18:15         ` Parag Warudkar
  0 siblings, 1 reply; 19+ messages in thread
From: linux-os (Dick Johnson) @ 2005-11-08 18:10 UTC (permalink / raw)
  To: Parag Warudkar; +Cc: Al Viro, Linux kernel


On Tue, 8 Nov 2005, Parag Warudkar wrote:

>
> On Nov 8, 2005, at 12:22 PM, Al Viro wrote:
>>
>> 	fd = open(bdev, O_RDONLY);
>> 	lseek(fd, SEEK_END, 0);
>> 	size = lseek(fd, SEEK_SET, 0);
>> 	close(fd);
>>
>> i.e. same as for regular files.  Won't be portable, though...
>
> For some reason this didn't work for bdev == "/dev/hda" - Size is
> returned as 0..
>

I think it's just a syntax error...

>> 	size = lseek(fd, 0, SEEK_SET);
                             |___________  Whence at the end

... will do fine on three different systems so far.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 18:10       ` linux-os (Dick Johnson)
@ 2005-11-08 18:15         ` Parag Warudkar
  2005-11-08 18:20           ` linux-os (Dick Johnson)
  0 siblings, 1 reply; 19+ messages in thread
From: Parag Warudkar @ 2005-11-08 18:15 UTC (permalink / raw)
  To: linux-os (Dick Johnson); +Cc: Al Viro, Linux kernel


>
> I think it's just a syntax error...
>
>>> 	size = lseek(fd, 0, SEEK_SET);
>                              |___________  Whence at the end
>
> ... will do fine on three different systems so far.
>

Yeah I corrected that before trying but still didn't work on Debian  
(2.6.8 kernel)...
Running root, open successful but size is always zero - Strange..

Parag

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 18:15         ` Parag Warudkar
@ 2005-11-08 18:20           ` linux-os (Dick Johnson)
  2005-11-08 19:39             ` Bob Copeland
  0 siblings, 1 reply; 19+ messages in thread
From: linux-os (Dick Johnson) @ 2005-11-08 18:20 UTC (permalink / raw)
  To: Parag Warudkar; +Cc: Al Viro, Linux kernel


On Tue, 8 Nov 2005, Parag Warudkar wrote:

>
>>
>> I think it's just a syntax error...
>>
>>>> 	size = lseek(fd, 0, SEEK_SET);
>>                              |___________  Whence at the end
>>
>> ... will do fine on three different systems so far.
>>
>
> Yeah I corrected that before trying but still didn't work on Debian
> (2.6.8 kernel)...
> Running root, open successful but size is always zero - Strange..
>
> Parag

Also found that the returned value was -1 and errno was EOVERFLOW.
So, that doesn't work either!

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 17:22   ` Al Viro
  2005-11-08 17:56     ` linux-os (Dick Johnson)
  2005-11-08 17:58     ` Parag Warudkar
@ 2005-11-08 18:49     ` Theodore Ts'o
  2005-11-08 19:12       ` Parag Warudkar
  2 siblings, 1 reply; 19+ messages in thread
From: Theodore Ts'o @ 2005-11-08 18:49 UTC (permalink / raw)
  To: Al Viro; +Cc: Parag Warudkar, linux-os (Dick Johnson), Linux kernel

On Tue, Nov 08, 2005 at 05:22:44PM +0000, Al Viro wrote:
> On Tue, Nov 08, 2005 at 12:10:25PM -0500, Parag Warudkar wrote:
> > 
> > On Nov 8, 2005, at 10:48 AM, linux-os ((Dick Johnson)) wrote:
> > 
> > >The Linux fstat() doesn't return any information number of blocks,
> > >or the byte-length of a physical hard disk.
> > 
> > I don't think (f)stat returns size and blocks information about a  
> > block device on any UNIX platform.
> > 
> > But I don't know for sure how to get it - perhaps ioctl on the  
> > device? BLKGETSIZE?
> 
> 	fd = open(bdev, O_RDONLY);
> 	lseek(fd, SEEK_END, 0);
> 	size = lseek(fd, SEEK_SET, 0);
> 	close(fd);
> 
> i.e. same as for regular files.  Won't be portable, though...

As I recall, this didn't always work; e2fsprogs falls back to using a
binary search using SEEK_SET to find the device size.  Folks who need
to do this should look at lib/ext2fs/getsize.c in the e2fsprogs
sources; it has ioctl's and other procedures for determining block
device size for Linux, *BSD, MacOSX, as well a generalized binary
search algorithm.

						- Ted


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 18:49     ` Theodore Ts'o
@ 2005-11-08 19:12       ` Parag Warudkar
  2005-11-09  3:23         ` Theodore Ts'o
  0 siblings, 1 reply; 19+ messages in thread
From: Parag Warudkar @ 2005-11-08 19:12 UTC (permalink / raw)
  To: Theodore Ts'o; +Cc: Al Viro, linux-os (Dick Johnson), Linux kernel


On Nov 8, 2005, at 1:49 PM, Theodore Ts'o wrote:

> e2fsprogs falls back to using a
> binary search using SEEK_SET to find the device size.

Binary search of what? I tried to read the relevant code in getsize.c  
but apart from suspecting that the binary search thing might be  
specific to ext2fs I didn't quite understand what's going on in the  
code.  (Will it work irrespective of the file system presence on the  
device?)

Thanks
Parag


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 18:20           ` linux-os (Dick Johnson)
@ 2005-11-08 19:39             ` Bob Copeland
  2005-11-08 20:03               ` linux-os (Dick Johnson)
  2005-11-08 21:06               ` Parag Warudkar
  0 siblings, 2 replies; 19+ messages in thread
From: Bob Copeland @ 2005-11-08 19:39 UTC (permalink / raw)
  To: linux-os (Dick Johnson); +Cc: Parag Warudkar, Al Viro, Linux kernel

> > Yeah I corrected that before trying but still didn't work on Debian
> > (2.6.8 kernel)...
> > Running root, open successful but size is always zero - Strange..
> >
> > Parag
>
> Also found that the returned value was -1 and errno was EOVERFLOW.
> So, that doesn't work either!

Isn't this just because the device size is > 2**32?  What if you use fseeko(3)
and #define _FILE_OFFSET_BITS 64?

Okay, still not portable and there is probably a better way that doesn't rely
on such nonsense.  For example, since you have a minimum size in mind,
just seek that much and see if it works - you don't really need to know the
whole disk size for that.  Or figure out the best way on all of your platforms
and abstract it.

-Bob

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 19:39             ` Bob Copeland
@ 2005-11-08 20:03               ` linux-os (Dick Johnson)
  2005-11-08 21:06               ` Parag Warudkar
  1 sibling, 0 replies; 19+ messages in thread
From: linux-os (Dick Johnson) @ 2005-11-08 20:03 UTC (permalink / raw)
  To: Bob Copeland; +Cc: Parag Warudkar, Al Viro, Linux kernel


On Tue, 8 Nov 2005, Bob Copeland wrote:

>>> Yeah I corrected that before trying but still didn't work on Debian
>>> (2.6.8 kernel)...
>>> Running root, open successful but size is always zero - Strange..
>>>
>>> Parag
>>
>> Also found that the returned value was -1 and errno was EOVERFLOW.
>> So, that doesn't work either!
>
> Isn't this just because the device size is > 2**32?  What if you use fseeko(3)
> and #define _FILE_OFFSET_BITS 64?
>
> Okay, still not portable and there is probably a better way that doesn't rely
> on such nonsense.  For example, since you have a minimum size in mind,
> just seek that much and see if it works - you don't really need to know the
> whole disk size for that.  Or figure out the best way on all of your platforms
> and abstract it.
>
> -Bob

Well if I could __count__ on EOVERFLOW meaning there was plenty of
room, it would be okay. Also, if I could count on walking-up-the
ladder of some SEEK_SET code that would be good also.

Anyway. I'm still checking. It sure would have been helpful
if fstat returned the number of blocks and the block-size for
the device (as it implied by the existance of those members).
Unfortunately the only POSIX requirement is for the st_*time,
st_mode, st_ini, st_dev, and st_*id fields to contain meaningful
values, damn.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 19:39             ` Bob Copeland
  2005-11-08 20:03               ` linux-os (Dick Johnson)
@ 2005-11-08 21:06               ` Parag Warudkar
  2005-11-08 22:57                 ` Bill Davidsen
  1 sibling, 1 reply; 19+ messages in thread
From: Parag Warudkar @ 2005-11-08 21:06 UTC (permalink / raw)
  To: Bob Copeland; +Cc: linux-os (Dick Johnson), Al Viro, Linux kernel


On Nov 8, 2005, at 2:39 PM, Bob Copeland wrote:

> Isn't this just because the device size is > 2**32?  What if you  
> use fseeko(3)
> and #define _FILE_OFFSET_BITS 64?
>

Yep. I got it to return the correct hard disk size (17Gb) using  
lseek64 and
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

Here is what I did
-------------------------------------------------
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
         int f;
         off64_t off=0;
         f = open("/dev/hda", O_RDONLY );
         if(f <= 0){
                 perror("open");
                 exit(0);
         }
         off = lseek64(f, 0, SEEK_SET);
         off = lseek64(f, 0, SEEK_END);
         perror("llseek");
         printf ("Size %lld\n", off);
         close(f);
         return 0;
}



> Okay, still not portable and there is probably a better way that  
> doesn't rely
> on such nonsense.  For example, since you have a minimum size in mind,
> just seek that much and see if it works - you don't really need to  
> know the
> whole disk size for that.  Or figure out the best way on all of  
> your platforms
> and abstract it.

Now this gives me a clue what Ted was referring to as binary search  
in e2fsprogs.
If nothing works this should work portably I guess.

Thanks
Parag 

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 21:06               ` Parag Warudkar
@ 2005-11-08 22:57                 ` Bill Davidsen
  2005-11-09  0:14                   ` Parag Warudkar
  0 siblings, 1 reply; 19+ messages in thread
From: Bill Davidsen @ 2005-11-08 22:57 UTC (permalink / raw)
  To: Parag Warudkar; +Cc: linux-os (Dick Johnson), Al Viro, Linux kernel

Parag Warudkar wrote:
> 
> On Nov 8, 2005, at 2:39 PM, Bob Copeland wrote:
> 
>> Isn't this just because the device size is > 2**32?  What if you  use 
>> fseeko(3)
>> and #define _FILE_OFFSET_BITS 64?
>>
> 
> Yep. I got it to return the correct hard disk size (17Gb) using  lseek64 
> and
> #define _LARGEFILE64_SOURCE
> #define _FILE_OFFSET_BITS 64
> 
> Here is what I did
> -------------------------------------------------
> #define _LARGEFILE64_SOURCE
> #define _FILE_OFFSET_BITS 64
> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> 
> int main()
> {
>         int f;
>         off64_t off=0;
Why is this initialized?
>         f = open("/dev/hda", O_RDONLY );
>         if(f <= 0){
>                 perror("open");
>                 exit(0);
>         }
>         off = lseek64(f, 0, SEEK_SET);
Why do this? it always returns zero.
>         off = lseek64(f, 0, SEEK_END);
>         perror("llseek");
>         printf ("Size %lld\n", off);
>         close(f);
>         return 0;
> }
> 
RETURN VALUE
   Upon successful completion, lseek returns the resulting offset
   location as measured in bytes from the beginning of the  file.
   Otherwise,  a  value  of  (off_t)-1 is returned and errno is
   set to indicate the error.


-- 
    -bill davidsen (davidsen@tmr.com)
"The secret to procrastination is to put things off until the
  last possible moment - but no longer"  -me

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 22:57                 ` Bill Davidsen
@ 2005-11-09  0:14                   ` Parag Warudkar
  2005-11-12 13:41                     ` Bill Davidsen
  0 siblings, 1 reply; 19+ messages in thread
From: Parag Warudkar @ 2005-11-09  0:14 UTC (permalink / raw)
  To: Bill Davidsen; +Cc: Linux Kernel


On Nov 8, 2005, at 5:57 PM, Bill Davidsen wrote:

> Parag Warudkar wrote:
>> On Nov 8, 2005, at 2:39 PM, Bob Copeland wrote:
>>> Isn't this just because the device size is > 2**32?  What if you   
>>> use fseeko(3)
>>> and #define _FILE_OFFSET_BITS 64?
>>>
>> Yep. I got it to return the correct hard disk size (17Gb) using   
>> lseek64 and
>> #define _LARGEFILE64_SOURCE
>> #define _FILE_OFFSET_BITS 64
>> Here is what I did
>> -------------------------------------------------
>> #define _LARGEFILE64_SOURCE
>> #define _FILE_OFFSET_BITS 64
>> #include <stdio.h>
>> #include <unistd.h>
>> #include <fcntl.h>
>> int main()
>> {
>>         int f;
>>         off64_t off=0;
> Why is this initialized?
>>         f = open("/dev/hda", O_RDONLY );
>>         if(f <= 0){
>>                 perror("open");
>>                 exit(0);
>>         }
>>         off = lseek64(f, 0, SEEK_SET);
> Why do this? it always returns zero.
>>         off = lseek64(f, 0, SEEK_END);
>>         perror("llseek");
>>         printf ("Size %lld\n", off);
>>         close(f);
>>         return 0;
>> }
> RETURN VALUE
>   Upon successful completion, lseek returns the resulting offset
>   location as measured in bytes from the beginning of the  file.
>   Otherwise,  a  value  of  (off_t)-1 is returned and errno is
>   set to indicate the error.
>

You took it a little too seriously! It was from the scribble-and- 
shuffle-till-it-works department ;) Sole purpose was to figure out a  
way to print the disk size from the device - some how that is!

But thanks for pointing out anyway - it doesn't hurt to be correct no  
matter what the purpose is.

Parag

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-08 19:12       ` Parag Warudkar
@ 2005-11-09  3:23         ` Theodore Ts'o
  0 siblings, 0 replies; 19+ messages in thread
From: Theodore Ts'o @ 2005-11-09  3:23 UTC (permalink / raw)
  To: Parag Warudkar; +Cc: Al Viro, linux-os (Dick Johnson), Linux kernel

On Tue, Nov 08, 2005 at 02:12:24PM -0500, Parag Warudkar wrote:
> 
> On Nov 8, 2005, at 1:49 PM, Theodore Ts'o wrote:
> 
> >e2fsprogs falls back to using a
> >binary search using SEEK_SET to find the device size.
> 
> Binary search of what? 

Of the device size; it doubles the guessed size of the disk until
lseek+read returns an error, and then uses binary search to figure out
the size of the disk.  I did this because it works on pretty much any
OS.

>  I tried to read the relevant code in getsize.c  
> but apart from suspecting that the binary search thing might be  
> specific to ext2fs I didn't quite understand what's going on in the  
> code.  (Will it work irrespective of the file system presence on the  
> device?)

Yes, it works irrespective of what's on the disk.  In fact, if the
Linux-specific ioctl's are not available, it's what will be used by
mke2fs to figure out the size of the device.

					- Ted

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Compatible fstat()
  2005-11-09  0:14                   ` Parag Warudkar
@ 2005-11-12 13:41                     ` Bill Davidsen
  0 siblings, 0 replies; 19+ messages in thread
From: Bill Davidsen @ 2005-11-12 13:41 UTC (permalink / raw)
  To: Parag Warudkar; +Cc: Linux Kernel

Parag Warudkar wrote:

>
> On Nov 8, 2005, at 5:57 PM, Bill Davidsen wrote:
>
>> Parag Warudkar wrote:
>>
>>> On Nov 8, 2005, at 2:39 PM, Bob Copeland wrote:
>>>
>>>> Isn't this just because the device size is > 2**32?  What if you   
>>>> use fseeko(3)
>>>> and #define _FILE_OFFSET_BITS 64?
>>>>
>>> Yep. I got it to return the correct hard disk size (17Gb) using   
>>> lseek64 and
>>> #define _LARGEFILE64_SOURCE
>>> #define _FILE_OFFSET_BITS 64
>>> Here is what I did
>>> -------------------------------------------------
>>> #define _LARGEFILE64_SOURCE
>>> #define _FILE_OFFSET_BITS 64
>>> #include <stdio.h>
>>> #include <unistd.h>
>>> #include <fcntl.h>
>>> int main()
>>> {
>>>         int f;
>>>         off64_t off=0;
>>
>> Why is this initialized?
>>
>>>         f = open("/dev/hda", O_RDONLY );
>>>         if(f <= 0){
>>>                 perror("open");
>>>                 exit(0);
>>>         }
>>>         off = lseek64(f, 0, SEEK_SET);
>>
>> Why do this? it always returns zero.
>>
>>>         off = lseek64(f, 0, SEEK_END);
>>>         perror("llseek");
>>>         printf ("Size %lld\n", off);
>>>         close(f);
>>>         return 0;
>>> }
>>
>> RETURN VALUE
>>   Upon successful completion, lseek returns the resulting offset
>>   location as measured in bytes from the beginning of the  file.
>>   Otherwise,  a  value  of  (off_t)-1 is returned and errno is
>>   set to indicate the error.
>>
>
> You took it a little too seriously! It was from the scribble-and- 
> shuffle-till-it-works department ;) Sole purpose was to figure out a  
> way to print the disk size from the device - some how that is!
>
> But thanks for pointing out anyway - it doesn't hurt to be correct no  
> matter what the purpose is. 

You could look at how blockdev does it, the --getss (sector size) and 
--getsize (size in sectors) seem to work, based on trying it on a total 
of two machines ;-) Even gives correct size for software RAID arrays!

-- 
bill davidsen <davidsen@tmr.com>
  CTO TMR Associates, Inc
  Doing interesting things with small computers since 1979


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2005-11-12 13:32 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-08 15:48 Compatible fstat() linux-os (Dick Johnson)
2005-11-08 17:10 ` Parag Warudkar
2005-11-08 17:22   ` Al Viro
2005-11-08 17:56     ` linux-os (Dick Johnson)
2005-11-08 17:58     ` Parag Warudkar
2005-11-08 18:10       ` linux-os (Dick Johnson)
2005-11-08 18:15         ` Parag Warudkar
2005-11-08 18:20           ` linux-os (Dick Johnson)
2005-11-08 19:39             ` Bob Copeland
2005-11-08 20:03               ` linux-os (Dick Johnson)
2005-11-08 21:06               ` Parag Warudkar
2005-11-08 22:57                 ` Bill Davidsen
2005-11-09  0:14                   ` Parag Warudkar
2005-11-12 13:41                     ` Bill Davidsen
2005-11-08 18:49     ` Theodore Ts'o
2005-11-08 19:12       ` Parag Warudkar
2005-11-09  3:23         ` Theodore Ts'o
2005-11-08 17:53   ` linux-os (Dick Johnson)
2005-11-08 18:04     ` Parag Warudkar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox