Readdir function on XFS filesystem not working properly

I have a code something like this:

opendir(...);
while ( (entry = readdir()) {
    if (entry->d_type == DT_DIR) {
        ...
    }
}

It is working for many years. But today we switched to a new server which uses XFS filesystem and it doesn’t work anymore. Is there any problem related with to filesystem?

This behaviour related with the XFS filesystem design.

Normally you have to make a stat() call to get type of the entry which returns from readdir() function.

Stat system call gets information from inodes so an inode lookup (and another system call overhead) required here.

Most of the filesystems also duplicates type information in directory entry structure to speed up this process without need to inode lookup.

In default form, XFS filesystem doesn’t store inode information in directory structure. You can change this behaviour while formatting the filesystem with mkfs.xfs using -ftype=1 option like below:

mkfs.xfs -n -ftype=1

But it is not possible to change after format. So, you need to catch the problem and create a fallback function to handle it.

If underlying filesystem doesn’t store inode information, entry->d_type will be equal to DT_UNKNOWN and you will have to make a stat call for this case.