Over the time, all file systems will become at certain level fragmented. When a file system is fragmented, blocks of free space are small and files have many extents. Same for XFS file system.
How to reorganizing XFS file system?
xfs package has a utility called xfs_fsr that reorganizes file systems so that the layout of the extents is improved. This improves overall performance. This can be done while a XFS file system is mounted.
xfs_fsr improves the organization of mounted filesystems. The reorganization algorithm operates on one file at a time, compacting or otherwise improving the layout of the file extents (contiguous blocks of file data).
Check if an XFS file system fragmented
For sure you want to check an XFS file system fragmentation level before you move on, here is the command.
# xfs_db -r /dev/mapper/LUN10
actual 10129, ideal 8062, fragmentation factor 20.41%
Note, this number is largely meaningless.
Files on this filesystem average 1.26 extents per file
Well, it does say the number 20.41% is meaningless due to 1.26 extents per file. So, this file system doesn't need a defragmentation.
Defragment an XFS filesystem
Without any options, xfs_fsr reorganizes all regular files in all mounted filesystems, '-v' option provides verbose output
# xfs_fsr -v /lun10
/lun10 start inode=0
No improvement will be made (skipping): ino=2147484872
No improvement will be made (skipping): ino=2147485226
No improvement will be made (skipping): ino=2147485440
extents before:2 after:1 DONE ino=2147485470
extents before:2 after:1 DONE ino=2147485501
No improvement will be made (skipping): ino=2147485573
extents before:3 after:1 DONE ino=2147485640
By default, it runs 2 hours reorganization. You can specify how long it runs by option '-t'
xfs_fsr -t seconds
Also, '-m' specify a list of filesystem to be reorganized. default is /etc/mtab
xfs_fsr -m <mtab>
Also, xfs_fsr has a feature records the file system where it left off from last run, so it can start there the next time. This information is stored in the file /var/tmp/.fsrlast_xfs. If the information found here is somehow inconsistent or out of date it is ignored and reorganization starts at the beginning or the first filesystem found in /etc/mtab.
So, this brings a good benefit, if you interrupt xfs_fsr cleanly (with any of the usual signals SIGINT, SIGHUP, SIGTERM or SIGQUIT), it takes care to write where it left off in /var/tmp/.fsrlast (or the file indicated with the -f option). So you can safely interrupt it with Ctrl+C, and restart it again with the same options later to complete the job.