0

I know the unit in the partition table is sector. But is it the size of physical sector size or logical sector size?

When the logical sector size is different with physical sector size, how does linux kernel and partition tools deal with the partition size since numbers in partition table is always recorded as number od sectors.

2

As you can see e.g. on Wikipedia, the entries in the GPT are LBA's (Logical Block Addresses). These are addresses based on the logical sector size.

When harddisks started to have a different (larger) physical sector size, the vendors kept the old physical sector size as logical sector size, to help old systems use the new harddisks. This means the harddisk firmware contains code that can deal with logical sector reads and writes: For a read, it will read the correct (larger) physical sector and return only part of it. For a write, it will first read the (larger) physical sector, then modify part of it, and the move it back.

This is inefficient unless reads and writes happen at LBAs that correspond to the start of a physical sector, and with lengths that are multiple of physical sectors.

So the way the kernel deals with large physical sector sizes is that it tries only to use these kind of reads and writes. For this to work, the partitions must be aligned correctly (on boundaries of physical sectors). Usually, partitioning programs will ensure that this is the case, and print a warning if you try to use them in a way where it isn't.

The kernel doesn't have to do that; it would work even without doing it, but it would be less efficient.

Edit

Can userland apps just read the logical sector size through /sys and read the partition table then multiply them together to tell the user how big a partition is?

Yes, userland apps like fdisk do exactly that (though I think the physical and logical sector size is in /proc, not in /sys, but I'd have to look this up).

When the kernel operates filesystem, it doesn't need to know the size of underlying storage, just the size of filesystem structure by reading the metadata of the filesystem, is that right?

It's a bit more difficult: The basic kernel unit is a "page" (the MMU unit), and the basic unit of a particular filesystem is a "block" (sometimes multiple pages). The kernel just needs to know how this relates to the logical block size of the underlying storage, because that's needed to calculate the LBAs.

But kernel needs to know the start and size of a partition when userland access raw partition device like /dev/sda2, isn't that true?

Yes. That's why it reads the partition table before it creates entries like /dev/sda2.

When kernel do this, it needs to know the unit of numbers recorded in partition table is logical sector size, not physical sector size, right?

Yes.

  • Can userland apps just read the logical sector size through /sys and read the partition table then multiply them together to tell the user how big a partition is? When the kernel operates filesystem, it doesn't need to know the size of underlying storage, just the size of filesystem structure by reading the metadata of the filesystem, is that right? – 炸鱼薯条德里克 Dec 18 '18 at 7:48
  • But kernel needs to know the start and size of a partition when userland access raw partition device like /dev/sda2, isn't that true? When kernel do this, it needs to know the unit of numbers recorded in partition table is logical sector size, not physical sector size, right? – 炸鱼薯条德里克 Dec 18 '18 at 7:54
  • Strictly speaking, the kernel only needs to know the logical sector size (= the size of the smallest individually addressable unit of disk space), but it is useful for the kernel to know the physical sector size too: some storages (multi-TB HDDs, enterprise SAN storage systems) use larger physical sector size internally, and arranging the disk I/O operations to conform to the physical sector size usually results in better performance than ignoring the difference. – telcoM Dec 18 '18 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.