tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Issues with lseek(2) on a block device



cryintothebluesky%gmail.com@localhost (Sad Clouds) writes:

>Hello, for most operating systems determining the size of a block
>device can be done with:

>lseek(fd, 0, SEEK_END);

>However, on NetBSD this does not seem to work.


The disk size is only retrieved at open time and stored in the
cached vnode. stat() therefore only sees the information as
long as the vnode is in cache. Before open, the vnode stores
the bits from e.g. the UFS inode, for UFS that would be zero.

It also does not work for block devices as these aren't really
opened when you call open(). Character devices ("raw disk")
are better, the lseek() or fstat() methods do work for raw
disk devices.

But it also does not work for wedges or device mapper volumes
(zfs vol probably fail too) as these don't implement the
used internal ioctl for disk devices. At least that part
would be easy to fix, but of questionable value.


Currently the only reliable way is to use ioctls. You can use
DIOCGDISKINFO (with proplib) or you can use the two FreeBSD ioctls
DIOCGMEDIASIZE and DIOCGSECTORSIZE to retrieve block count and
size individually as numbers.

For compatibility with netbsd < 8 you may need to fall back to
various older ioctls. That's what is still being implemented
for tools like fsck.




Home | Main Index | Thread Index | Old Index