What is the difference between 'physical' and 'logical' size?

7

1

I just found out about the mdls command on OS X which prints out the metadata associated with a given file. This command tells me both a 'physical size' ("kMDItemPhysicalSize") and 'logical size' ("kMDItemLogicalSize"). For example, the current text file I'm looking at has:

  • physical size: 1152
  • logical size: 4096

If I do a ls -l, it's size is given as 1152. If I open up the 'get info' panel, it says "1,152 bytes (4 KB on disk)". So whatever 'logical size is', it apparently equates to size "on disk".

DilithiumMatrix

Posted 2014-05-15T16:20:17.983

Reputation: 477

Answers

9

Disks are divided into sectors, which are 512 bytes* on your typical hard drive or SSD.

Filesystems are responsible for taking requests for files based on name (something you understand) and translating into block read/write requests (something the disk understands). For this to work, a map of which sectors belong to which file is needed. There's many ways to do this and thus, there are many filesystems in existence. Windows uses NTFS and FAT32, for example, and Linux has numerous ones including ext2, ext3, etc. Not sure about Mac filesystems but it's the same principle.

Some filesystems will have a level above sectors this called clusters which are sets of sectors - because a map of every single sector on large disks might take up too much space. On NTFS, for example, you can specify the cluster size ("allocation unit" is another name for it), and 4096 bytes (8 sectors) is one of them you can pick.

So, given all that, the "minimum" you can really read or write to a disk will be the sector size or cluster size. So while you may write 4097 bytes to a disk, the filesystem has to give that file two clusters, so it takes 8192 bytes away from your free space. Thus, you have the logical size (size it consumes on the disk) and the physical size (the real size of the file).


*On some newer "Advanced Format" disks it's internally 4Kbytes, but still looks like 512 byte sectors to the OS for compatibility. SSDs are wildly different on the inside but again still look like 512 byte sectors to the OS.

LawrenceC

Posted 2014-05-15T16:20:17.983

Reputation: 63 487

1@user55325 NTFS also does that (known as a resident record/file in their docs), so it's hardly new. Can't find much info on HFS+ though. – Bob – 2015-03-28T11:40:20.270

Fascinating! Thanks. So a hard drive (with this style, for example) would only be able to hold (approximately) just as many 4097b files as 8192b files? – DilithiumMatrix – 2014-05-15T17:03:32.890

Yes. Although next-generation filesystems like btrfs (maybe ext4 too, I don't know if it's in the kernel yet) support inlining small files along with the file metadata, so if it's below a certain size it won't take up an extra cluster. This means improved access time as well as potentially reduced space. – user55325 – 2014-05-15T17:36:31.220

0

You guys have it backwards.

On MacOS the 'logical' size is the size of the file. We can define the size of a file foo as the output of:

$ cat foo | wc -c

The 'physical' size is the number of bytes the file is occupying on disk.

Proof:

$ cat /dev/urandom | head -c 1 > foo; mdls foo | grep 'Logical\|Physical'

outputs:

kMDItemLogicalSize                 = 1
kMDItemPhysicalSize                = 4096

You can confirm the physical size with du -h foo which outputs 4.0k

Jay

Posted 2014-05-15T16:20:17.983

Reputation: 614