0

There are two separate fields in inodes in Linux namely size and blocks. Why do we have to have two fields. If we just have the block count, wouldn't it be enough?

I tried creating a text file, and adding characters to it. The size was changing, but the block count stayed nearly the same. (used "stat filename" command)

Therefore can I assume that "size" field is used to make sure that the OS doesn't read unrelated data stored in the block which does not own to the file?

DesirePRG
  • 6,122
  • 15
  • 69
  • 114

2 Answers2

1

Yes, the size field is used to read only the bytes that belong to file.

Actually, a file may occupy more blocks on disks than size / block_size + 1.

nullptr
  • 11,008
  • 1
  • 23
  • 18
  • Why would a file occupy more blocks on disk than that? because of indirect blocks? – DesirePRG Jun 16 '13 at 19:08
  • 3
    No, because of disk space allocation algorithms and because of possible preallocations or holes in a file (in case of holes there can be less blocks than expected). Consider the following example: a block size (reported by `stat`) is 512 bytes, but an I/O block size is 4 KB. When you create a 1-byte file, it occupies the whole I/O block (as it would be inefficient to allocate less as the whole 4 KB are transferred to/from disk) which means `stat` will report 4096/512 = 8 blocks for the file. – nullptr Jun 16 '13 at 19:37
0

In most Linux file systems, files can have holes in them - areas filled with zeroes that aren't mapped to any disk block. Hence, the file size and the block size can be different.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40