Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the filesystem block size?

Tags:

c

filesystems

gcc

I wanted to know a way to find out what the disk's block size is through a function or a compiler constant in C.

like image 976
maty_nz Avatar asked Jun 20 '10 20:06

maty_nz


People also ask

What is filesystem block size?

In a file system, a block is the largest contiguous amount of disk space that can be allocated to a file and also the largest amount of data that can be transferred in a single I/O operation. The block size determines the maximum size of a read request or write request that a file system sends to the I/O device driver.

What is my block size Linux?

Use the lsmem command to find out the size of your memory blocks. In the example, the block size is 256 MB. Alternatively, you can read /sys/devices/system/memory/block_size_bytes. This sysfs attribute contains the block size in byte in hexadecimal notation.

What is block size in dd command?

The dd command reports on the number of blocks it reads and writes. The number after the + is a count of the partial blocks that were copied. The default block size is 512 bytes.


1 Answers

The info about you using gcc compiler is not interesting, since compilers are not interested in the block size of the filesystem, they are not even aware of the fact that a filesystem can exist... the answer is system specific (MS Windows? GNU/Linux or other *nix/*nix like OS?); on POSIX you have the stat function, you can use it to have the stat struct, which contains the field st_blksize (blocksize for filesystem I/O) which could be what you're interested in.

ADD

Example

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>


int main()
{
  struct stat fi;
  stat("/", &fi);
  printf("%d\n", fi.st_blksize);
  return 0;
}

Tells you about the filesystem used on / (root); e.g. for me, it outputs 4096.

like image 173
ShinTakezou Avatar answered Sep 27 '22 16:09

ShinTakezou