How to modify the number of XFS pre-allocated blocks?

1.7k views Asked by At

I wrote a simple program and ran the program on ext4 and xfs.

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

int
main(int argc, char *argv[])
{
        int fd;
        char *file_name = argv[1];
        struct stat buf;

        fd = open (file_name, O_RDWR|O_CREAT);
        if (fd == -1) {
                printf ("Error: %s\n", strerror(errno));
                return -1;
        }

        write (fd, "hello", sizeof ("hello"));

        fstat (fd, &buf);
        printf ("st_blocks: %lu\n", buf.st_blocks);

        stat (file_name, &buf);
        printf ("st_blocks: %lu\n", buf.st_blocks);

        close (fd);

        stat (file_name, &buf);
        printf ("st_blocks: %lu\n", buf.st_blocks);

        return 0;
}

output on ext4:

st_blocks: 8 st_blocks: 8 st_blocks: 8

output on xfs:

st_blocks: 128 st_blocks: 128 st_blocks: 8

Then I explored about xfs and found an option for changing the extent size while running mkfs.xfs.

example: mkfs.xfs -r extsize=4096 /dev/sda1

But still I get the same output on XFS. Can anyone provide more insight on how to change the st_blocks. Thanks in advance.

2

There are 2 answers

0
hue On

I found the answer, posting the answer here so that others facing the problem can refer it.

mount -t xfs -o allocsize=4096 device mount-point

The allocsize option is used to tune the buffer size.

0
Eric Sandeen On

What you are seeing is xfs speculative preallocation, which is a heuristic which is used to avoid fragmentation of files as they grow. For more info, see this FAQ entry.

You are correct that the "-o allocsize=XXX" option disables that heuristic. Your attempt at using "-r extsize=XXX" failed because that option is only for the realtime subvolume, which you are almost certainly not using.