size-in-bytes
PATCH 1/1 size-in-bytes
I just rediffed the size-in-bytes patch against 2.5.5pre1. The result is found at ftp.kernel.org
(under 02-2.5.5pre1-sizeinbytes-bsd), and below.
Comment:
Disk and partition size is kept several places, sometimes in sectors (of 512 bytes), sometimes in blocks (of 1024 bytes). This is ugly, one finds a lot of shifting left and right, as in
limit = (size << BLOCK_SIZE_BITS) >> sector_bits;
and inconvenient (some places where one really needs info in sectors only info in blocks is available; strange kludges are needed to access the last sector of the disk, as needed for EFI), and insufficient (disk manufacturers say we'll have 2+TB disks three years from now, and 2+TB RAIDs exist today).
The patch below introduces the routine blkdev_size_in_bytes() that provides device size in a loff_t. Today that is done via the array blk_size_in_bytes[] that is to replace the array blk_size[]. Of course the array is to disappear again. Whatever one might have planned for blk_size[] also applies to blk_size_in_bytes[]. (For me it is a field in the structure pointed at by a kdev_t.)
Thrown in is a BSD partition handling fix.
Andries
linux/drivers/block/blkpg.c renamed from linux-2.5.5pre1/linux/drivers/block/blkpg.c +7 -10
@@ -203,7 +203,6 @@int blk_ioctl (kdev_t dev,unsigned int cmd,unsigned long arg) {request_queue_t *q; -struct gendisk *g;u64 ullval =0 ;int intval, *iptr;unsigned short usval;
@@ -268,20 +267,18 @@return 0 ;case BLKSSZGET : -/* get block device sector size as needed e.g. by fdisk */ +/* get block device hardware sector size */ intval =get_hardsect_size (dev);return put_user (intval, (int *) arg);case BLKGETSIZE : +/* size in sectors, works up to 2 TB */ + ullval =blkdev_size_in_bytes (dev); +return put_user ((unsigned long )(ullval >>9 ), (unsigned long *) arg);case BLKGETSIZE64 : - g =get_gendisk (dev); -if (g) - ullval = g->part[minor (dev)].nr_sects; - -if (cmd ==BLKGETSIZE ) -return put_user ((unsigned long )ullval, (unsigned long *)arg); -else -return put_user ((u64 )ullval <<9 , (u64 *)arg); +/* size in bytes */ + ullval =blkdev_size_in_bytes (dev); +return put_user (ullval, (u64 *) arg);#if 0 case BLKRRPART :/* Re-read partition tables */ if (!capable (CAP_SYS_ADMIN ))
linux/drivers/block/ll_rw_blk.c renamed from linux-2.5.5pre1/linux/drivers/block/ll_rw_blk.c +29 -23
74 lines not on this page · raw message
linux/drivers/block/loop.c renamed from linux-2.5.5pre1/linux/drivers/block/loop.c +13 -7
@@ -151,14 +151,20 @@#define MAX_DISK_SIZE 1024*1024*1024 -static unsigned long compute_loop_size (struct loop_device *lo,struct dentry * lo_dentry,kdev_t lodev) +static unsigned long +compute_loop_size (struct loop_device *lo, +struct dentry * lo_dentry,kdev_t lodev) { -if (S_ISREG (lo_dentry->d_inode->i_mode)) -return (lo_dentry->d_inode->i_size - lo->lo_offset) >>BLOCK_SIZE_BITS ; -if (blk_size[major (lodev)]) -return blk_size[major (lodev)][minor (lodev)] - - (lo->lo_offset >>BLOCK_SIZE_BITS ); -return MAX_DISK_SIZE ; +loff_t size =0 ; + +if (S_ISREG (lo_dentry->d_inode->i_mode)) { + size = lo_dentry->d_inode->i_size; + }else { + size =blkdev_size_in_bytes (lodev); +if (size ==0 ) +return MAX_DISK_SIZE ; + } +return (size - lo->lo_offset) >>BLOCK_SIZE_BITS ; }static void figure_loop_size (struct loop_device *lo)
linux/drivers/block/rd.c renamed from linux-2.5.5pre1/linux/drivers/block/rd.c +9 -15
@@ -300,19 +300,13 @@ }up (&inode->i_bdev->bd_sem);break ; -case BLKGETSIZE :/* Return device size */ -if (!arg) -break ; - error =put_user (rd_kbsize[minor] <<1 , (unsigned long *) arg); -break ; +case BLKGETSIZE :case BLKGETSIZE64 : - error =put_user ((u64 )rd_kbsize[minor]<<10 , (u64 *)arg); -break ;case BLKROSET :case BLKROGET :case BLKSSZGET : error =blk_ioctl (inode->i_rdev, cmd, arg); - }; + } out:return error; }
@@ -418,11 +412,10 @@/* This is the registration and initialization section of the RAM disk driver */ static int __init rd_init (void ) { -int i; +int i;if (rd_blocksize >PAGE_SIZE || rd_blocksize <512 || - (rd_blocksize & (rd_blocksize-1 ))) - { + (rd_blocksize & (rd_blocksize-1 ))) {printk ("RAMDISK: wrong blocksize %d, reverting to defaults\n" , rd_blocksize); rd_blocksize =BLOCK_SIZE ;
@@ -449,7 +442,8 @@ &rd_bd_op, NULL);for (i =0 ; i <NUM_RAMDISKS ; i++) -register_disk (NULL ,mk_kdev (MAJOR_NR ,i),1 , &rd_bd_op, rd_size<<1 ); +register_disk (NULL ,mk_kdev (MAJOR_NR ,i),1 , &rd_bd_op, + rd_size<<1 );#ifdef CONFIG_BLK_DEV_INITRD /* We ought to separate initrd operations here */
@@ -458,10 +452,10 @@ INITRD_MINOR, S_IFBLK | S_IRUSR, &rd_bd_op, NULL);#endif - blksize_size[MAJOR_NR ] = rd_blocksizes;/* Avoid set_blocksize() check */ - blk_size[MAJOR_NR ] = rd_kbsize;/* Size of the RAM disk in kB */ + blksize_size[MAJOR_NR ] = rd_blocksizes;/* Avoid set_blocksize() check */ + blk_size[MAJOR_NR ] = rd_kbsize;/* Size of the RAM disk in kB */ -/* rd_size is given in kB */ +/* rd_size is given in kB */ printk ("RAMDISK driver initialized: " "%d RAM disks of %dK size %d blocksize\n" ,NUM_RAMDISKS , rd_size, rd_blocksize);
linux/drivers/char/raw.c renamed from linux-2.5.5pre1/linux/drivers/char/raw.c +4 -5
@@ -289,11 +289,10 @@ dev =to_kdev_t (raw_devices[minor].binding->bd_dev); sector_size = raw_devices[minor].sector_size; sector_bits = raw_devices[minor].sector_bits; - sector_mask = sector_size-1 ; - -if (blk_size[major (dev)]) - limit = (((loff_t ) blk_size[major (dev)][minor (dev)]) <<BLOCK_SIZE_BITS ) >> sector_bits; -else + sector_mask = sector_size -1 ; + + limit =blkdev_size_in_bytes (dev) >> sector_bits; +if (!limit) limit =INT_MAX ;dprintk ("rw_raw_dev: dev %d:%d (+%d)\n" ,major (dev),minor (dev), limit);
linux/drivers/ide/ide-probe.c renamed from linux-2.5.5pre1/linux/drivers/ide/ide-probe.c +7 -0
@@ -790,6 +790,7 @@struct gendisk *gd;unsigned int unit, units, minors;int *bs, *max_ra; +loff_t *byteszs;extern devfs_handle_t ide_devfs_handle;#if 1
@@ -818,10 +819,14 @@ max_ra =kmalloc (minors*sizeof (int ),GFP_KERNEL );if (!max_ra) goto err_kmalloc_max_ra; + byteszs =kmalloc (minors *sizeof (loff_t),GFP_KERNEL ); +if (!byteszs) + goto err_kmalloc_byteszs;memset (gd->part,0 , minors *sizeof (struct hd_struct ));/* cdroms and msdos f/s are examples of non-1024 blocksizes */ + blk_size_in_bytes[hwif->major] = byteszs; blksize_size[hwif->major] = bs; max_readahead[hwif->major] = max_ra;for (unit =0 ; unit < minors; ++unit) {
@@ -875,6 +880,8 @@ }return ; +err_kmalloc_byteszs: +kfree (max_ra); err_kmalloc_max_ra:kfree (bs); err_kmalloc_bs:
linux/drivers/ide/ide.c renamed from linux-2.5.5pre1/linux/drivers/ide/ide.c +1 -0
@@ -2125,6 +2125,7 @@* Remove us from the kernel's knowledge */ unregister_blkdev (hwif->major, hwif->name); +kfree (blk_size_in_bytes[hwif->major]);kfree (blksize_size[hwif->major]);kfree (max_readahead[hwif->major]); blk_dev[hwif->major].data =NULL ;
linux/drivers/md/md.c renamed from linux-2.5.5pre1/linux/drivers/md/md.c +5 -9
@@ -283,10 +283,7 @@static unsigned int calc_dev_sboffset (kdev_t dev,mddev_t *mddev,int persistent) { -unsigned int size =0 ; - -if (blk_size[major (dev)]) - size = blk_size[major (dev)][minor (dev)]; +unsigned int size = (blkdev_size_in_bytes (dev) >>BLOCK_SIZE_BITS );if (persistent) size =MD_NEW_SIZE_BLOCKS (size);return size;
@@ -1105,12 +1102,11 @@ rdev->desc_nr =-1 ; rdev->faulty =0 ; - size =0 ; -if (blk_size[major (newdev)]) - size = blk_size[major (newdev)][minor (newdev)]; + size = (blkdev_size_in_bytes (newdev) >>BLOCK_SIZE_BITS );if (!size) { -printk (KERN_WARNING "md: %s has zero size, marking faulty!\n" , -partition_name (newdev)); +printk (KERN_WARNING +"md: %s has zero or unknown size, marking faulty!\n" , +partition_name (newdev)); err = -EINVAL ; goto abort_free; }
linux/drivers/mtd/devices/blkmtd.c renamed from linux-2.5.5pre1/linux/drivers/mtd/devices/blkmtd.c +3 -9
@@ -62,7 +62,6 @@/* Default erase size in K, always make it a multiple of PAGE_SIZE */ #define CONFIG_MTD_BLKDEV_ERASESIZE 128#define VERSION "1.1" -extern int *blk_size[];/* Info for the block device */ typedef struct mtd_raw_dev_data_s {
@@ -848,7 +847,7 @@mtd_raw_dev_data_t *rawdevice =NULL ;int maj, min;int i, blocksize, blocksize_bits; -loff_t size =0 ; +loff_t size;int readonly =0 ;int erase_size =CONFIG_MTD_BLKDEV_ERASESIZE ;kdev_t rdev;
@@ -911,13 +910,8 @@ i >>=1 ; } -if (count) { - size = count; - }else { -if (blk_size[maj]) { - size = ((loff_t ) blk_size[maj][min] <<BLOCK_SIZE_BITS ) >> blocksize_bits; - } - } + size = (count ? count : (blkdev_size_in_bytes (rdev) >> blocksize_bits)); + total_sectors = size; size *= blocksize; totalsize = size;
linux/drivers/s390/block/xpram.c renamed from linux-2.5.5pre1/linux/drivers/s390/block/xpram.c +1 -1
@@ -1065,7 +1065,7 @@for (i=0 ; i < xpram_devs; i++)if (xpram_sizes[i] ==0 ) xpram_sizes[i] = mem_auto; } - blk_size[major]=xpram_sizes; + blk_size[major] = xpram_sizes; xpram_offsets =kmalloc (xpram_devs *sizeof (int ),GFP_KERNEL );if (!xpram_offsets) {
linux/drivers/usb/storage/dpcm.c renamed from linux-2.5.5pre1/linux/drivers/usb/storage/dpcm.c +3 -2
@@ -43,8 +43,6 @@*/ int dpcm_transport (Scsi_Cmnd *srb,struct us_data *us) { -int ret; -if (srb ==NULL )return USB_STOR_TRANSPORT_ERROR ;
@@ -60,6 +58,8 @@#ifdef CONFIG_USB_STORAGE_SDDR09 case 1 : + { +int ret;/* * LUN 1 corresponds to the SmartMedia card reader.
@@ -73,6 +73,7 @@ srb->lun =1 ; us->srb->lun =1 ; return ret; + }#endif default :
linux/fs/affs/super.c renamed from linux-2.5.5pre1/linux/fs/affs/super.c +1 -2
@@ -29,7 +29,6 @@#include <asm/system.h> #include <asm/uaccess.h> -extern int *blk_size[];extern struct timezone sys_tz;static int affs_statfs (struct super_block *sb,struct statfs *buf);
@@ -308,7 +307,7 @@*/ size = sb->s_bdev->bd_inode->i_size >>9 ; -pr_debug ("AFFS: initial blksize=%d, blocks=%d\n" ,512 , blocks); +pr_debug ("AFFS: initial blocksize=%d, #blocks=%d\n" ,512 , size);affs_set_blocksize (sb,PAGE_SIZE );/* Try to find root block. Its location depends on the block size. */
linux/fs/block_dev.c renamed from linux-2.5.5pre1/linux/fs/block_dev.c +9 -20
@@ -27,33 +27,22 @@static unsigned long max_block (kdev_t dev) {unsigned int retval = ~0U ; -int major =major (dev); +loff_t sz =blkdev_size_in_bytes (dev); -if (blk_size[major]) { -int minor =minor (dev); -unsigned int blocks = blk_size[major][minor]; -if (blocks) { -unsigned int size =block_size (dev); -unsigned int sizebits =blksize_bits (size); - blocks += (size-1 ) >>BLOCK_SIZE_BITS ; - retval = blocks << (BLOCK_SIZE_BITS - sizebits); -if (sizebits >BLOCK_SIZE_BITS ) - retval = blocks >> (sizebits -BLOCK_SIZE_BITS ); - } +if (sz) { +unsigned int size =block_size (dev); +unsigned int sizebits =blksize_bits (size); + retval = (sz >> sizebits); }return retval; }static loff_t blkdev_size (kdev_t dev) { -unsigned int blocks = ~0U ; -int major =major (dev); - -if (blk_size[major]) { -int minor =minor (dev); - blocks = blk_size[major][minor]; - } -return (loff_t ) blocks <<BLOCK_SIZE_BITS ; +loff_t sz =blkdev_size_in_bytes (dev); +if (sz) +return sz; +return ~0ULL ; }/* Kill _all_ buffers, dirty or not.. */
linux/fs/cramfs/inode.c renamed from linux-2.5.5pre1/linux/fs/cramfs/inode.c +3 -8
@@ -116,8 +116,6 @@struct buffer_head * read_array[BLKS_PER_BUF ];unsigned i, blocknr, buffer, unread;unsigned long devsize; -unsigned int major, minor; -char *data;if (!len)
@@ -140,12 +138,9 @@return read_buffers[i] + blk_offset; } - devsize = ~0UL ; - major =major (sb->s_dev); - minor =minor (sb->s_dev); - -if (blk_size[major]) - devsize = blk_size[major][minor] >>2 ; + devsize =blkdev_size_in_bytes (sb->s_dev) >>12 ; +if (!devsize) + devsize = ~0UL ;/* Ok, read in BLKS_PER_BUF pages completely first. */ unread =0 ;
linux/fs/partitions/check.c renamed from linux-2.5.5pre1/linux/fs/partitions/check.c +36 -1
@@ -416,9 +416,19 @@for (i = first_minor; i < end_minor; i++) g->sizes[i] = g->part[i].nr_sects >> (BLOCK_SIZE_BITS -9 ); } + +/* +* In case the device provided an array blk_size_in_bytes[], fill it. +*/ +if (g->sizes !=NULL && blk_size_in_bytes[g->major] !=NULL ) { +for (i = first_minor; i < end_minor; i++) + blk_size_in_bytes[g->major][i] = + ((loff_t ) g->part[i].nr_sects <<9 ); + } } -unsigned char *read_dev_sector (struct block_device *bdev,unsigned long n,Sector *p) +unsigned char * +read_dev_sector (struct block_device *bdev,unsigned long n,Sector *p) {struct address_space *mapping = bdev->bd_inode->i_mapping;int sect =PAGE_CACHE_SIZE /512 ;
@@ -480,3 +490,28 @@return 0 ; } + +/* +* Make sure that a proposed subpartition is strictly contained inside +* the parent partition. If all is well, call add_gd_partition(). +*/ +int +check_and_add_subpartition (struct gendisk *hd,int super_minor,int minor, +int sub_start,int sub_size) +{ +int start = hd->part[super_minor].start_sect; +int size = hd->part[super_minor].nr_sects; + +if (start == sub_start && size == sub_size) { +/* full parent partition, we have it already */ +return 0 ; + } + +if (start <= sub_start && start+size >= sub_start+sub_size) { +add_gd_partition (hd, minor, sub_start, sub_size); +return 1 ; + } + +printk ("bad subpartition - ignored\n" ); +return 0 ; +}
linux/fs/partitions/check.h renamed from linux-2.5.5pre1/linux/fs/partitions/check.h +6 -0
@@ -7,4 +7,10 @@*/ void add_gd_partition (struct gendisk *hd,int minor,int start,int size); +/* +* check_and_add_subpartition does the same for subpartitions +*/ +int check_and_add_subpartition (struct gendisk *hd,int super_minor, +int minor,int sub_start,int sub_size); +extern int warn_no_part;
linux/fs/partitions/ldm.c renamed from linux-2.5.5pre1/linux/fs/partitions/ldm.c +4 -3
@@ -941,9 +941,10 @@* validate_patition_table - check whether @dev is a dynamic disk * @dev: device to test * -* Check whether @dev is a dynamic disk by looking for an MS-DOS-style partition -* table with one or more entries of type 0x42 (the former Secure File System -* (Landis) partition type, now recycled by Microsoft for dynamic disks) in it. +* Check whether @dev is a dynamic disk by looking for an MS-DOS-style +* partition table with one or more entries of type 0x42 (the former +* Secure File System partition type, now recycled by Microsoft for +* dynamic disks) in it. * If this succeeds we assume we have a dynamic disk, and not otherwise. * * Return 1 if @dev is a dynamic disk, 0 if not and -1 on error.
linux/fs/partitions/msdos.c renamed from linux-2.5.5pre1/linux/fs/partitions/msdos.c +9 -47
75 lines not on this page · raw message
linux/include/linux/blkdev.h renamed from linux-2.5.5pre1/linux/include/linux/blkdev.h +14 -3
@@ -313,7 +313,8 @@extern void blk_dump_rq_flags (struct request *,char *);extern void generic_unplug_device (void *); -extern int * blk_size[MAX_BLKDEV ]; +extern int * blk_size[MAX_BLKDEV ];/* in units of 1024 bytes */ +extern loff_t * blk_size_in_bytes[MAX_BLKDEV ];extern int * blksize_size[MAX_BLKDEV ];
@@ -336,9 +337,7 @@extern inline void blk_clear (int major) { blk_size[major] =NULL ; -#if 0 blk_size_in_bytes[major] =NULL ; -#endif blksize_size[major] =NULL ; max_readahead[major] =NULL ; read_ahead[major] =0 ;
@@ -358,6 +357,7 @@#define blk_finished_io (nsects) do { } while (0)#define blk_started_io (nsects) do { } while (0) +/* assumes size > 256 */ extern inline unsigned int blksize_bits (unsigned int size) {unsigned int bits =8 ;
@@ -381,6 +381,17 @@return retval; } +extern inline loff_t blkdev_size_in_bytes (kdev_t dev) +{ +if (blk_size_in_bytes[major (dev)]) +return blk_size_in_bytes[major (dev)][minor (dev)]; +else if (blk_size[major (dev)]) +return (loff_t ) blk_size[major (dev)][minor (dev)] + <<BLOCK_SIZE_BITS ; +else +return 0 ; +} +typedef struct {struct page *v;}Sector ;unsigned char *read_dev_sector (struct block_device *,unsigned long ,Sector *);
linux/kernel/ksyms.c renamed from linux-2.5.5pre1/linux/kernel/ksyms.c +1 -0
@@ -307,6 +307,7 @@/* block device driver support */ EXPORT_SYMBOL (blksize_size);EXPORT_SYMBOL (blk_size); +EXPORT_SYMBOL (blk_size_in_bytes);EXPORT_SYMBOL (blk_dev);EXPORT_SYMBOL (is_read_only);EXPORT_SYMBOL (set_device_ro);
Quoted 11 lines · [email protected] wrote:
/*
+ *blk_size_in_bytescontains the size of all block-devices in bytes
+ * (blk_sizehas too low a resolution, since we really need the size
+ * in 512 byte sectors, and fails on devices > 2 TB)
+ *
+ * blk_size_in_bytes[MAJOR][MINOR]
+ *
+ * if (!blk_size_in_bytes[MAJOR]) then no minor size checking is done.
+ */
linux/drivers/block/ll_rw_blk.c line 88
Quoted 3 lines
linux/drivers/block/ll_rw_blk.c renamed from linux-2.5.5pre1/linux/drivers/block/ll_rw_blk.c +3
-0+loff_t * blk_size_in_bytes[MAX_BLKDEV ]; + +/*
Please pin it up the block device structure not to just another arbitrary global array.
Quoted 9 lines · On February 16, 2002 05:09 pm, [email protected] wrote:
I just rediffed the size-in-bytes patch against 2.5.5pre1. The result is found at ftp.kernel.org
(under 02-2.5.5pre1-sizeinbytes-bsd), and below.Comment:
Disk and partition size is kept several places, sometimes in sectors (of 512 bytes), sometimes in blocks (of 1024 bytes). This is ugly, one finds a lot of shifting left and right, as inlimit = (size << BLOCK_SIZE_BITS) >> sector_bits;
We want to stay with the shift counts. They should be the primary currency of size measurement. You can add shift counts together and get nice, compact code, whereas with absolute size you often have to ugly things - e.g., it's a pain to divide by blocksize when you have it as an absolute number, it's easy when you have it as a shift.
If you are going to the trouble of fixing this, please don't use absolute size as the primary measure, use a shift count.
--
Daniel
Quoted 8 lines · Daniel Phillips
We want to stay with the shift counts. They should be the primary currency of size measurement. You can add shift counts together and get nice, compact code, whereas with absolute size you often have to ugly things - e.g., it's a pain to divide by blocksize when you have it as an absolute number, it's easy when you have it as a shift.
If you are going to the trouble of fixing this, please don't use absolute size as the primary measure, use a shift count.
Most of this is targetted at userland which needs byte counts (size in sectors was a bug introduced after the original BLKGETSIZE64 went in). Using the number of sectors in kernel is perhaps more efficient, but it is a microoptimization that won't show up on any benchmarks.
-ben