- How many bytes in 1 block or 1 inode?
- 1 Answer 1
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
- Blocks, block devices and block sizes
- Contents
- Block sizes
- Disk block sizes
- File system block sizes
- Kernel block size
- Applications
- vmstat
- iostat
- Benchmarking I/O
- How to.
- Find out the size of a block device
- Find out the file system stored on a block device
- Find out the elevator algorithm
- Associated commands
- See also
- What is file block size in Linux?
- What is the meaning of IO Block and how is it calculated?
- 2 Answers 2
- Short explanation
- More detailed explanation
- «IO Block» vs. «Blocks»
- IO Block
- Blocks
- How do we know what «IO Block» and «Blocks» really refer to?
- stat the system call
- More examples with stat
- Changing value of «IO Block»
How many bytes in 1 block or 1 inode?
I’m trying to specify the amount of disk space for specific user in my Ubuntu Desktop 12.04 LTS System. I manually configure disk quota by editing /etc/fstab and add 2 more option next to the defaults option at the fourth field (e.g. usrquota and grpquota). Then I use edquota command to specify the amount of disk space for my user $ edquota -u user After that, new screen has appeared and there are 2 kinds of size for specifying, e.g. blocks and inodes. I’m unaware of either. I only know the human readable size (e.g. KB, MB, GB and so on). I will be happy if anyone can tell me what does inode means and how much size does it cost? and how much bytes in 1 block? Thanks.
1 Answer 1
“Block” is used with several different meanings: sometimes it’s a number of bytes that depends on the filesystem, sometimes it’s a number of bytes that depends on the application. The Linux quota tool uses a block size of 1024 bytes or 1 kilobyte. So to limit a user to 50MB you would set their limit to 50000 blocks.
As for inodes, one inode means one file (including directories), regarding of its size.
You can see the current disk usage with df (in kilobytes by default, you can switch to k/M/G/… prefixes with -h or -B 4096 to show the number of 4096-byte blocks). To see the current inode usage, use df -i .
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Blocks, block devices and block sizes
A block device is a device you can read blocks from. For example hard disks, cdrom drives and floppies are block devices, but not the keyboard. You can receive data from the keyboard and regard them as blocks, but you cannot seek on the keyboard. You can tell a hard disk «give me block 5433», then block 7707, then block 1807 and you cannot do this with a keyboard, so, a keyboard is no block device.
Contents
Block sizes
It is important to understand the ideas behind the block sizes.
Disk block sizes
Typically, a hard disk cannot read less than 512 bytes, if you want to read less, read 512 bytes and discard the rest. This is why dd reads one block à 512 bytes in the following example:
tweedleburg:~ # dd if=/dev/sda1 of=/dev/null count=1 1+0 records in 1+0 records out 512 bytes (512 B) copied, 1.8977e-05 s, 27.0 MB/s
File system block sizes
On the other hand, every file system needs to split up a partition into blocks to store files and file parts. This is why there is a different block size for a file system as you can see here:
$ stat -f . File: "." ID: 84bc409db75bdd32 Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 2531650 Free: 1704179 Available: 1570650 Inodes: Total: 647168 Free: 567361
So, if you store a file in this file system, it will be stored in a 4096-byte-block, that means, even if your file only contains 6 bytes, it will take away 4096 bytes from your disk’s capacity:
$ df . Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 10126600 3309884 6282600 35% / $ echo hello>world $ ls -l world -rw-r--r-- 1 thorsten_staerk_de thorsten_staerk_de 6 Apr 1 06:17 world $ df . Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 10126600 3309888 6282596 35% / $ du -sch world 4.0K world 4.0K total
Kernel block size
Also the kernel has its own block size. This is relevant e.g. for vmstat. In the vmstat man page you find the statement
All linux blocks are currently 1024 bytes.
So, again another block size when you work with vmstat. This is the block size the Linux kernel uses internally for caching and buffering. It is the most prominent of all block sizes.
Applications
vmstat
iostat
The difference between buffered I/O and direct I/O is that direct I/O does not use the operating system cache. During the open syscall, you can say that you want direct I/O. More info.
The difference between async I/O and sync I/O is that async I/O reads/writes from/to a file without waiting for the data to actually arrive, but rather having the data sent to a network socket. You must use libaio for this. More info.
Benchmarking I/O
How to.
Find out the size of a block device
To find out the size of the block device /dev/sda:
Find out the file system stored on a block device
A file system will typically be stored in a partition, not directly in a block device. To find all partitions on /dev/sda use
To find out what file system is stored on /dev/sda1 use
Find out the elevator algorithm
The elevator algorithm can be found like this:
cat /sys/devices/platform/host7/session1/target7\:0\:0/7\:0\:0\:0/block/sdi/queue/scheduler noop anticipatory [deadline] cfq
In this case, the deadline elevator algorithm is active.
Or it may be found like this:
cat /sys/class/block/sdb/queue/scheduler noop deadline [cfq]
You can switch the scheduler like this:
# cat /sys/class/block/sdb/queue/scheduler noop deadline [cfq] # echo "noop">/sys/class/block/sdb/queue/scheduler # cat /sys/class/block/sdb/queue/scheduler [noop] deadline cfq
Associated commands
- dd
- vmstat
- iostat
- stat
- fdisk
- hwinfo —block
- hwinfo —partition
- file -s /dev/sda : finds out the filesystem type of /dev/sda
See also
What is file block size in Linux?
I want to get file block size of two files, using stat struct. In internet, they say file block size is affected by which device is using. If that is right, does block size of all files in same partition equal? I am using blockcmp function, it always return same size. I wonder it is right or i miss something. Here is my code.
#include #include #include #include #include struct stat stat1, stat2; struct tm *time1, *time2; void filestat1(void); void filestat2(void); void filetime1(void); void filetime2(void); void sizecmp(void); void blockcmp(void); void datecmp(void); void timecmp(void); int main(void) < filestat1(); filestat2(); filetime1(); filetime2(); sizecmp(); blockcmp(); datecmp(); timecmp(); >void filestat1(void) < // check if there is no text1 int check = 0; check = stat("text1", &stat1); if(check != 0) < printf("Error : there is no text1\n"); >return; > void filestat2(void) < // check if there is no text2 int check = 0; check = stat("text2", &stat2); if(check != 0) < printf("Error : there is no text2\n"); >return; > void filetime1(void) < time1 = localtime(&stat1.st_mtime); return; >void filetime2(void) < time2 = localtime(&stat2.st_mtime); return; >void sizecmp(void) < printf("size compare\n"); //variable declare long long int text1_size; long long int text2_size; //variable initialize text1_size = stat1.st_size; text2_size = stat2.st_size; if(text1_size >text2_size) printf("text1 is bigger\n"); else if(text1_size < text2_size) printf("text2 is bigger\n"); else printf("same size\n"); printf("\n"); return; >void blockcmp(void) < printf("block compare\n"); //variable declare long long int text1_block_size; long long int text2_block_size; //variable initialize text1_block_size = stat1.st_blocks; text2_block_size = stat2.st_blocks; if(text1_block_size >text2_block_size) printf("text1 is bigger\n"); else if(text1_block_size < text2_block_size) printf("text2 is bigger\n"); else printf("same size\n"); printf("\n"); return; >void datecmp(void) < printf("date compare\n"); // compare tm_mon if(time1->tm_mon > time2->tm_mon) printf("time1 is early \n"); else if(time1->tm_mon < time2->tm_mon) printf("time2 is early \n"); else< // compare tm_mday if(time1->tm_mday > time2->tm_mday) printf("time1 is early \n"); else if(time1->tm_mday < time2->tm_mday) printf("time2 is early \n"); // same date else printf("same time \n"); > printf("\n"); > void timecmp(void) < printf("time compare\n"); // compare hour if(time1->tm_hour > time2->tm_hour) printf("time1 is early \n"); else if(time1->tm_hour < time2->tm_hour) printf("time2 is early \n"); else< // compare minutes if(time1->tm_min > time2->tm_min) printf("time1 is early \n"); else if(time1->tm_min < time2->tm_min) printf("time2 is early \n"); // same time ;else printf("same time \n") > >
What is the meaning of IO Block and how is it calculated?
I’d like to know what the meaning of «IO Block» is in the output of stat and how that number is calculated. I know that the physical size of a folder/file is measured in 4096 byte blocks. However in this example below, the size of the file is more than 4096 bytes and the value of «IO Block» has not changed. What is the meaning of IO blocks and how to change it?
stat yeni File: 'yeni' Size: 12890 Blocks: 32 IO Block: 4096 normal dosya Device: 805h/2053d Inode: 2255976 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ ihsan) Gid: ( 1000/ ihsan) Access: 2017-08-15 20:07:26.324017949 +0300 Modify: 2017-08-15 20:06:30.703053636 +0300 Change: 2017-08-15 20:07:26.324017949 +0300 Birth: -
2 Answers 2
Short explanation
- «IO Block» in stat ‘s output is the preferred number of bytes that the file system uses for reading and writing files. An IO Block is commonly 4096 bytes.
- «Blocks», on the other hand, counts how many 512-bytes blocks are allocated on disk for the file.
More detailed explanation
«IO Block» vs. «Blocks»
IO Block
«IO Block» is the preferred number of bytes that the file system uses for reading and writing files.
This number is fixed to 4096 bytes on my ext4 partition.
A block size of 4096 bytes is also the default for file systems between 512 MB and 4 TB. See man mke2fs.conf , your /etc/mke2fs.conf , and the documentation of the -T option in man mkfs.ext4 .
Blocks
On the other hand, «Blocks» gives the number of 512-byte blocks allocated for the file on disk. Note that the blocks of stat ‘s «Blocks» have a different size than the blocks from «IO Block»:
- number of bytes in a block of «IO Block»: Defined by the file system. 4096 bytes in your and my case.
- number of bytes in a block of «Blocks»: Always 512 bytes.
How do we know what «IO Block» and «Blocks» really refer to?
Digging through stat ‘s source code we find this line that creates the default output of stat :
Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n\
Size: %-10s : «Size:» prints the size of the file in bytes, with some padding using -10 .
Blocks: %-10b : «Blocks» prints the value of placeholder %b (with some padding). What’s %b ? According to man 1 stat :
%b number of blocks allocated (see %B)
%B the size in bytes of each block reported by %b
How many bytes are in a block denoted by «Blocks»?
This prints «512». Alright, the block size of «Blocks» is 512 bytes.
On to the code that prints «IO Block»:
IO Block: %-6o : What’s the placeholder %o ? Straight from man 1 stat :
%o optimal I/O transfer size hint
Interesting! «IO Block» is short for «optimal I/O transfer size hint».
Running this on an ext4 file system:
we learn the «IO Block» is 4096 bytes. That’s quite a size difference:
stat --format="File is %s bytes but needs %B*%b bytes on disk. Optimal I/O transfer size hint (alias IO Block): %o bytes" ~/small_file
Which prints the following in my case:
File is 116 bytes but needs 512*8 bytes on disk. Optimal I/O transfer size hint (alias IO Block): 4096 bytes
It’s easy to make the mistake of thinking that the Blocks value counts the number of IO Blocks allocated on disk.
stat the system call
From man 2 stat we can learn more about the IO transfer size hint, also known as «IO Block»:
The st_blksize field gives the «preferred» blocksize for efficient file system I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)
Also citing from man 2 stat that describes the field st_blocks which is useful to understand stat ‘s «Blocks»:
This field indicates the number of blocks allocated to the file, in 512-byte units.
More examples with stat
Here is shortened example output of stat ~/small_file :
File: /home/mb/small_file Size: 116 Blocks: 8 IO Block: 4096 regular file // Rest omitted
- Size: 116 means there are 116 bytes of data in this file. But it doesn’t tell us how many bytes are used to store this file on disk. That’s what «Blocks» is for.
- Blocks: 8 means this file takes up 8 * 512 = 4096 bytes on disk. Given that the file only has 116 bytes of data, this is a little wasteful. But we can’t use fewer bytes since the block size of ext4 on my system is 4096 bytes. How wasteful? Let’s calculate: 116 / 4096 * 100 ≈ 2.8 meaning only about 2.8% of the allocated bytes are actually used for storing the file’s data.
- IO Block: 4096 means the preferred number of bytes that are read or written from the disk at once.
Another example with a slightly larger file, stat ~/larger_file :
File: /home/mb/larger_file Size: 91246 Blocks: 184 IO Block: 4096 regular file // Rest omitted
- Size: 91246 tells us the number of bytes in this file, independent of our file system and the number of bytes it physically occupies on disk.
- Blocks: 184 lets us calculate the number of bytes this file physically occupies on disk: 184 * 512 = 94208 bytes. How many of those allocated bytes are used for actually storing larger_file ‘s data? 91246 / 94208 * 100 ≈ 96.9 . About 96.6% is the file’s data, that’s much more efficient compared to our previous example with ~/small_file .
Changing value of «IO Block»
I doubt you can change the value of «IO Block» of an existing file system. Here’s a discussion on that.
In case you are creating a new file system, there’s the -b option that specifies the block size in bytes.
Valid block-size values are 1024, 2048 and 4096 bytes per block.