0

I'm currently learning kernel driver programmation using Linux Device Drivers - 3rd.

To define a block device, I have to declare a gendisk structure and to initialize it with relevant informations.

In chapter 16 its written:

sector_t capacity;

The capacity of this drive, in 512-byte sectors. The sector_t type can be 64 bits wide. Drivers should not set this field directly; instead, pass the number of sectors to set_capacity.

Usualy I set the capacity using a call like:

set_capacity(gendisk, sector_number*(hard_sector_size/KERNEL_SECTOR_SIZE));

Imagine that I have a device with a memory zone that is not a multiple of 512 bytes.

Let's take 2000 bytes so there is three 512 bytes zones plus a 464 bytes zone

[-512b-]      [-512b-]      [-512b-]      [-464b-]
0     511    512    1023   1024   1535   1536   1999
  • What should I pass to set_capacity ?

Furthermore on some devices, memory is splitted into several areas. Imagine that I have a device containing multiple memory areas, each with different size and that I want to abstract this splitted memory into a single memory zone.

area1 => 32000 bytes
area2 => 512 bytes
area3 => 50 bytes
area4 => 45 bytes
area5 => 1024 bytes

In this case the hard sector size is not a constant within the device.

  • How should I fill the gendisk structure ?

UPDATE after @KamilCuk answer:

To continue with this example let's take the full device memory (33631 bytes) which is 65 sectors of 512 bytes + 1 sector of 351 bytes.

So the solutions are to present it either as:

  • 33270 bytes memory zone ==> 65 sectors

or

  • 33631 bytes memory zone ==> 66 sectors with 161 bytes discarded at the end of last sector

Are those the best solutions or there is another way to handle this specific case ?


  • Basically the question could be resumed to how to initialize a block device when it's not a multiple of KERNEL_SECTOR_SIZE?
4

1 回答 1

2
  • What should I pass to set_capacity ?
    来自lwm.net 文章

    大小值应以 512 字节扇区为单位,即使您的设备使用的硬件扇区大小不同。

通过你有 3 个扇区,因此与内核兼容并忽略最后一个扇区。
或者传递你有 4 个 512 扇区,在写入时丢弃最后一个扇区上从 464 到 512 的字节,并在读取时填充零。请确保,所有使用您的驱动程序的驱动程序都知道,最后一个扇区较小并且可以对其进行操作。

  • How should I fill the gendisk structure ?
    确实,使用您的块驱动程序实现。您正在实现块驱动程序而不是文件系统驱动程序,文件位于上层。一个文件系统可以实现多个文件保存在块之间或底层块设备的同一块中。(就像 btrfs 可以将小文件压缩成一个块)
于 2018-05-17T17:32:59.720 回答