Saturday, September 11, 2010

block size

多处都存在block size, 概念同名但是意义不同, 相当迷惑
这哥们就是一个被迷惑的人: http://www.linuxforums.org/forum/misc/5654-linux-disk-block-size-help-please.html

上面的URL列出了如下几种block:
1. Hardware block size, "sector size"
2. Filesystem block size, "block size"
3. Kernel buffer cache block size, "block size"
4. Partition table block size, "cylinder size"

我对fdisk打印的blocks一栏很不解. 需要dig一下.
先来看看fdisk打印出来的block:

jessinio@jessinio-laptop:/ $ sudo fdisk -l
Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00038329

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         100      803218+  83  Linux
/dev/sda2             101       30401   243392782+  8e  Linux LVM

上面的内容和下面的是一致的:
jessinio@jessinio-laptop:/media/82d236f2-3592-4040-801c-3c2049ddfb95$ sudo fdisk -b 512 -l
Warning: the -b (set sector size) option should be used with one specified device
Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00038329

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         100      803218+  83  Linux
/dev/sda2             101       30401   243392782+  8e  Linux LVM

但是下面的就比较奇怪了:
jessinio@jessinio-laptop:/media/82d236f2-3592-4040-801c-3c2049ddfb95$ sudo fdisk -b 1024 -l
Warning: the -b (set sector size) option should be used with one specified device
Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 15200 cylinders
Units = cylinders of 16065 * 1024 = 16450560 bytes
Disk identifier: 0x00038329

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         100     1606437   83  Linux
/dev/sda2             101       30401   486785565   8e  Linux LVM

指定更大的硬件sector size反而block增加, 这是为什么呢?。下面是fdisk的相关代码:
sector_size变量的来源:
 759 static void
 760 get_sectorsize(int fd) {
 761 #if defined(BLKSSZGET)
 762     if (!user_set_sector_size && 
 763         linux_version_code() >= MAKE_VERSION(2,3,3)) {
 764         int arg;
 765         if (ioctl(fd, BLKSSZGET, &arg) == 0)
 766             sector_size = arg;
 767         if (sector_size != DEFAULT_SECTOR_SIZE)
 768             printf(_("Note: sector size is %d (not %d)\n"),
 769                    sector_size, DEFAULT_SECTOR_SIZE);
 770     }
 771 #else
 772     /* maybe the user specified it; and otherwise we still
 773        have the DEFAULT_SECTOR_SIZE default */
 774 #endif
 775 }
DEFAULT_SECTOR_SIZE 在fdisk.h中定义是
#define DEFAULT_SECTOR_SIZE     512

或者是用户指定的user_set_sector_sizesector_size = atoi(optarg);

打印时使用的代码是:
1731             unsigned int psects = get_nr_sects(p);
1732             unsigned int pblocks = psects;
1733             unsigned int podd = 0;
1734
1735             if (sector_size < 1024) {
1736                 pblocks /= (1024 / sector_size);
1737                 podd = psects % (1024 / sector_size);
1738             }
1739             if (sector_size > 1024)
1740                 pblocks *= (sector_size / 1024);
1741                         printf(
1742                 "%s  %c %11lu %11lu %11lu%c  %2x  %s\n",
1743             partname(disk_device, i+1, w+2),
1744 /* boot flag */     !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG
1745             ? '*' : '?',
1746 /* start */     (unsigned long) cround(get_partition_start(pe)),
1747 /* end */       (unsigned long) cround(get_partition_start(pe) + psects
1748                 - (psects ? 1 : 0)),
1749 /* odd flag on end */   (unsigned long) pblocks, podd ? '+' : ' ',
1750 /* type id */       p->sys_ind,
1751 /* type name */     (type = partition_type(p->sys_ind)) ?
1752             type : _("Unknown"));
1753             check_consistency(p, i);

1. 当sector_size刚好等于1024时, 打印出的正好是sector的数目。也是partition的大小(同size概念)
2. 当sector_size不等于1024时,把sector数目和sector_size结合起来換算成大小(同size概念)

可见, fdisk打印的Blocks一栏其实是partition的大小。下面测试一下:

$ sudo mount /dev/sda1 /media/disk/
$ df /media/disk/
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1               790556     48176    702220   7% /media/disk
790556 是文件系统总大小。
上面的fdisk -b 1024 -l 命令得到的1606437是sector的数目。这样计算:

1606437 * 512 / 1024 =803218 是约等于790556 的。partition的大小是比file system大是因为file system需要存放一些信息.

1. 要想得到一个partition占有用多少个sector的话, 可以使用fdisk -b 1024这种方式得到
2. 不加-b参数的fdisk命令打印的Blocks一栏其实是表示partition的大小(以K为单位)
3. 还没有能力找出Kernel buffer cache block size, "block size" 这一条的实际代码

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.