Skip to contentSkip to conversation

block ioctl to read/write last sector

DISCUSSION [email protected] 4 participants 7 messages Feb 13, 2001 – Feb 14, 2001

Subsystems (4)
lore
Share & tools
lore
#0 lore raw
Quoted 7 lines

The block device uses 1K blocksize, and will prevent userspace from
seeing the odd-block at the end of the disk, if the disk is odd-size.

IA-64 architecture defines a new partitioning scheme where there is a
backup of the partition table header in the last sector of the disk. While we can read and write to this sector in the kernel partition code, we have no way for userspace to update this partition block.

Are you sure?

There may be no easy, convenient way right now, but (without having checked anything) it seems to me that you can, also today.
Look at the addpart utility in the util-linux package. It will allow you to add a partition disjoint from previously existing partitions.
And since a partition can start on an odd sector, this should allow you to also read the last sector.

Do I overlook something?

Anyway, an ioctl just to read the last sector is too silly. An ioctl to change the blocksize is more reasonable. And I expect that this fixed blocksize will go soon.

Andries

[Sorry if precisely the same discussion has happened earlier - I have no memory.]

#1 lore raw

Hi Andries!

Quoted 9 lines · On Tue, 13 Feb 2001 [email protected] wrote:
Quoted 7 lines

The block device uses 1K blocksize, and will prevent userspace from
seeing the odd-block at the end of the disk, if the disk is odd-size.

IA-64 architecture defines a new partitioning scheme where there is a
backup of the partition table header in the last sector of the disk. While we can read and write to this sector in the kernel partition code, we have no way for userspace to update this partition block.

Are you sure?

Yes.

The only alternative at this time is to use the scsi-generic tools to read directly from the scsi-layer. But, of course, this only works with scsi devices.

Quoted 4 lines

There may be no easy, convenient way right now, but (without having checked anything) it seems to me that you can, also today.

Please go check :-) I believe my statement stands: You cannot read or write to odd-sectors at the end of the disk from userspace. (see below for definition of odd sector...)

Quoted 7 lines

Look at the addpart utility in the util-linux package. It will allow you to add a partition disjoint from previously existing partitions.
And since a partition can start on an odd sector, this should allow you to also read the last sector.

Do I overlook something?

Yes. The addpart utility just uses the block-layer ioctls to dynamically add and/or remove partitions. What this is doing is just adjusting the kernel's idea of what the current partition scheme is. This has _nothing_ to do with actually reading or writing data from the disk.

The ia64 gpt partitioning code defines a partition header at the front of the disk and at the end of the disk. I definetly have a need to read and write to these headers.

What this proposed patch does has _nothing_ to do with partitioning :-) It is _only_ to read and write the last sector of the disk. It just so happens that the reason that I have to read that last sector is to read a partition header.

Quoted 3 lines

Anyway, an ioctl just to read the last sector is too silly. An ioctl to change the blocksize is more reasonable.

That may be better, I don't know. That's why this is an RFC. Are there any possible races with that method? It seems to me that you might adversely affect io in progress by changing the blocksize. The method demonstrated in this patch shouldn't do that.

Quoted 1 line

And I expect that this fixed blocksize will go soon.

That may be, I don't know that much about the block layer. All I know is that, with the current structure, I cannot read or write to sectors where (sector #) > total-disk-blocks - (total-disk-blocks / (softblocksize/hardblocksize))

This ioctl can be deprecated when that is no longer the case.

Quoted 3 lines

Andries

Thanks for the comments.

Quoted 3 lines

[Sorry if precisely the same discussion has happened earlier - I have no memory.]

Not really. I have discussed this with some folks with Red Hat, but this is the first discussion on L-K.
--
Michael Brown

#2 lore raw
Quoted 10 lines · Michael E Brown
Quoted 4 lines

Anyway, an ioctl just to read the last sector is too silly. An ioctl to change the blocksize is more reasonable.

That may be better, I don't know. That's why this is an RFC. Are there any possible races with that method? It seems to me that you might adversely affect io in progress by changing the blocksize. The method demonstrated in this patch shouldn't do that.

block size changing is dangerous:
if you change the blocksize of a mounted partition you'll disrupt the filesystem.
some kernels crash hard if you execute

swapon /dev/<insert your root device>

swapon won't overwrite your root fs: it changes the blocksize to 4kB and then notices that there is no swap signature.
But the blocksize change is fatal.

Quoted 2 lines
Quoted 1 line

And I expect that this fixed blocksize will go soon.

that's 2.5.

Quoted 5 lines

That may be, I don't know that much about the block layer. All I know is that, with the current structure, I cannot read or write to sectors where (sector #) > total-disk-blocks - (total-disk-blocks / (softblocksize/hardblocksize))

I have one additional user space only idea:
have you tried raw-io? bind a raw device to the partition, IIRC raw-io is always in 512 byte units.

Probably an ioctl is the better idea, but I'd use absolute sector numbers (not relative to the end), and obviously 64-bit sector numbers - 2 TB isn't that far away.

--
Manfred

#3 lore raw
Quoted 3 lines · Manfred Spraul

I have one additional user space only idea:
have you tried raw-io? bind a raw device to the partition, IIRC raw-io is always in 512 byte units.

That has been tried. No, it does not work. :-) Using Scsi-Generic is the only way so far found, but of course, it only works on SCSI drives.

Quoted 5 lines

Probably an ioctl is the better idea, but I'd use absolute sector numbers (not relative to the end), and obviously 64-bit sector numbers - 2 TB isn't that far away.

I was deliberately trying to limit the scope to avoid misuse. This is to work around a flaw in the current API, not to create a new API. Limiting access to only those blocks that would normally be inaccessible through the normal API seemed like the best bet to me.

--
Michael Brown

#4 lore raw
In reply to [email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Andries" == Andries Brouwer <[email protected]> writes:

Andries> Anyway, an ioctl just to read the last sector is too silly. Andries> An ioctl to change the blocksize is more reasonable.

I actually sent you a patch implementing this some time ago, remember? We need it for XFS...

Patch against 2.4.2-pre3 follows.

Signature · 3 lines
Martin K. Petersen, Principal Linux Consultant, Linuxcare, Inc.
[email protected], http://www.linuxcare.com/
SGI XFS for Linux Developer, http://oss.sgi.com/projects/xfs/
arch/mips64/kernel/ioctl32.c +1 -0
diff -urN linux/arch/mips64/kernel/ioctl32.c linux-blksetsize/arch/mips64/kernel/ioctl32.c
@@ -712,6 +712,7 @@
 	IOCTL32_HANDLER(BLKPG, blkpg_ioctl_trans),
 	IOCTL32_DEFAULT(BLKELVGET),
 	IOCTL32_DEFAULT(BLKELVSET),
+	IOCTL32_DEFAULT(BLKSETSIZE),
 
 	IOCTL32_DEFAULT(MTIOCTOP),			/* mtio.h ioctls  */
 	IOCTL32_HANDLER(MTIOCGET32, mt_ioctl_trans),
arch/sparc64/kernel/ioctl32.c +1 -0
diff -urN linux/arch/sparc64/kernel/ioctl32.c linux-blksetsize/arch/sparc64/kernel/ioctl32.c
@@ -3107,6 +3107,7 @@
 COMPATIBLE_IOCTL(BLKFRASET)
 COMPATIBLE_IOCTL(BLKSECTSET)
 COMPATIBLE_IOCTL(BLKSSZGET)
+COMPATIBLE_IOCTL(BLKSETSIZE)
 
 /* RAID */
 COMPATIBLE_IOCTL(RAID_VERSION)
drivers/block/blkpg.c +17 -0
diff -urN linux/drivers/block/blkpg.c linux-blksetsize/drivers/block/blkpg.c
@@ -208,6 +208,7 @@
 int blk_ioctl(kdev_t dev, unsigned int cmd, unsigned long arg)
 {
 	int intval;
+	long longval;
 
 	switch (cmd) {
 		case BLKROSET:
@@ -258,6 +259,22 @@
 				longval = g->part[MINOR(dev)].nr_sects;
 			return put_user(longval, (long *) arg);
 #endif
+		case BLKSETSIZE:
+			/* Can be used to set block size from userland. */
+			if(!capable(CAP_SYS_ADMIN))
+				return -EACCES;
+
+			if(!dev || !arg)
+				return -EINVAL;
+
+			if (get_user(longval, (int *)(arg)))
+				return -EFAULT;
+
+			if (longval > PAGE_SIZE || longval < 512 || (longval & (longval-1)))
+				return -EINVAL;
+
+			set_blocksize(dev, longval);
+			return 0;
 #if 0
 		case BLKRRPART: /* Re-read partition tables */
 			if (!capable(CAP_SYS_ADMIN)) 
drivers/ide/ide.c +1 -0
diff -urN linux/drivers/ide/ide.c linux-blksetsize/drivers/ide/ide.c
@@ -2672,6 +2672,7 @@
 		case BLKPG:
 		case BLKELVGET:
 		case BLKELVSET:
+		case BLKSETSIZE:
 			return blk_ioctl(inode->i_rdev, cmd, arg);
 
 		default:
drivers/md/lvm.c +2 -0
diff -urN linux/drivers/md/lvm.c linux-blksetsize/drivers/md/lvm.c
@@ -910,6 +910,8 @@
 			return -EFAULT;
 		break;
 
+	case BLKSETSIZE:
+		return blk_ioctl(inode->i_rdev, command, a);
 
 	case BLKFLSBUF:
 		/* flush buffer cache */
drivers/md/md.c +3 -0
diff -urN linux/drivers/md/md.c linux-blksetsize/drivers/md/md.c
@@ -2511,6 +2511,9 @@
 						(long *) arg);
 			goto done;
 
+		case BLKSETSIZE:
+			return blk_ioctl(dev, cmd, (long *) arg);
+
 		case BLKFLSBUF:
 			fsync_dev(dev);
 			invalidate_buffers(dev);
drivers/scsi/sd.c +1 -0
diff -urN linux/drivers/scsi/sd.c linux-blksetsize/drivers/scsi/sd.c
@@ -234,6 +234,7 @@
 		case BLKPG:
                 case BLKELVGET:
                 case BLKELVSET:
+                case BLKSETSIZE:
 			return blk_ioctl(inode->i_rdev, cmd, arg);
 
 		case BLKRRPART: /* Re-read partition tables */
include/linux/fs.h +1 -0
diff -urN linux/include/linux/fs.h linux-blksetsize/include/linux/fs.h
@@ -181,6 +181,7 @@
 #define BLKSECTSET _IO(0x12,102)/* set max sectors per request (ll_rw_blk.c) */
 #define BLKSECTGET _IO(0x12,103)/* get max sectors per request (ll_rw_blk.c) */
 #define BLKSSZGET  _IO(0x12,104)/* get block device sector size */
+#define BLKSETSIZE _IO(0x12,108)/* set device block size */
 #if 0
 #define BLKPG      _IO(0x12,105)/* See blkpg.h */
 #define BLKELVGET  _IOR(0x12,106,sizeof(blkelv_ioctl_arg_t))/* elevator get */
#5 lore raw

Martin,

It looks like the numbers we picked for our respective IOCTLs conflict.
I think I can change mine to the next higher since your patch seems to have been around longer. What is the general way to deal with these conflicts?

--
Michael

Quoted 11 lines · Martin K. Petersen
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Andries" == Andries Brouwer <[email protected]> writes:

Andries> Anyway, an ioctl just to read the last sector is too silly. Andries> An ioctl to change the blocksize is more reasonable.

I actually sent you a patch implementing this some time ago, remember? We need it for XFS...

Patch against 2.4.2-pre3 follows.

#6 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Michael" == Michael E Brown <[email protected]> writes:

Michael,

Michael> It looks like the numbers we picked for our respective IOCTLs Michael> conflict. I think I can change mine to the next higher since Michael> your patch seems to have been around longer.

If you could pick another number that would be great. All the people out there using XFS rely on the BLKSETSIZE ioctl, and mkfs.xfs would break horribly with your patch.

Michael> What is the general way to deal with these conflicts?

Whoever applies the patch to the official tree deals with them :)

Signature · 3 lines
Martin K. Petersen, Principal Linux Consultant, Linuxcare, Inc.
[email protected], http://www.linuxcare.com/
SGI XFS for Linux Developer, http://oss.sgi.com/projects/xfs/