12.3. Administering XFS
XFS organizes a filesystem into allocation groups. Each allocation group
has its own free-space and inode B-trees and can allocate independently,
which is why XFS scales well with concurrent writers. Metadata carries
CRC32c checksums, and the inode and free-space structures are designed
for online growth. The internal log is the recovery mechanism; unlike
ext4, XFS has no supported shrink and relies on xfs_repair for offline
structural repair.
12.3.1. Creating and Inspecting an XFS Filesystem
Create XFS on the fourth practice partition. This overwrites the
partition, and -f is required because the partition may still carry a
stale signature from an earlier experiment. -L sets the label:
# mkfs.xfs -f -L handbook-xfs /dev/vdb4
meta-data=/dev/vdb4 isize=512 agcount=4, agsize=65280 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=1
= reflink=1 bigtime=1 inobtcount=1 nrext64=1
= exchange=0 metadir=0
data = bsize=4096 blocks=261120, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1, parent=0
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
= rgcount=0 rgsize=0 extents
Discarding blocks...Done.
The output describes the geometry: four allocation groups, 4096-byte
blocks, a 512-byte inode size, an internal log of 16384 blocks, and
crc=1, finobt=1, rmapbt=1, and reflink=1. Discarding blocks
means mkfs issued discard requests for the new filesystem; on a device
that does not support discard, the line is absent and the filesystem is
still valid.
mkfs.xfs option |
Description |
|---|---|
-f |
Force creation over an existing filesystem signature. |
-L LABEL |
Set the filesystem label (at most 12 characters for XFS). |
-b size=BYTES |
Set the filesystem block size; 4096 is the default. |
-m crc=1 |
Enable metadata CRCs (default). |
-m bigtime=1 |
Enable timestamps beyond 2038 (default on CRC filesystems). |
-d agcount=N |
Set the number of allocation groups. |
-l size=SIZE |
Set the internal log size, or logdev= for an external log. |
-n ftype=1 |
Store file type in directory entries (required for modern features). |
-q |
Suppress the geometry report. |
Mount and inspect the live filesystem with xfs_info. It reads the
mounted filesystem or a raw device and does not modify either:
# mkdir -p /mnt/xfs
# mount /dev/vdb4 /mnt/xfs
Both commands are silent. Then read the live geometry:
# xfs_info /mnt/xfs
meta-data=/dev/vdb4 isize=512 agcount=4, agsize=65280 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=1
= reflink=1 bigtime=1 inobtcount=1 nrext64=1
= exchange=0 metadir=0
data = bsize=4096 blocks=261120, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1, parent=0
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
= rgcount=0 rgsize=0 extents
The data section gives the data block size and block count; agcount
gives the number of allocation groups. sectsz=512 is the sector size
the filesystem was told about, not necessarily the physical sector size.
reflink=1 allows cp --reflink on XFS; rmapbt=1 maintains a reverse
mapping B-tree; bigtime=1 permits timestamps beyond 2038. log is the
internal log, 16384 blocks (64 MiB at 4 KiB per block).
12.3.2. Online Maintenance and Free-Space Management
XFS keeps free space in per-allocation-group B-trees. The blocks are
grouped into extents; a fragmented free-space tree has many small extents
and can make large allocations slower. xfs_spaceman queries the mounted
filesystem. Its freesp -s command prints a summary histogram:
# xfs_spaceman -c "freesp -s" /mnt/xfs
from to extents blocks pct blkcdf extcdf
2 3 1 3 0.00 100.00 100.00
32768 65280 4 244673 100.00 100.00 80.00
total free extents 5
total free blocks 244676
average free extent size 48935.2
Most free space is in four extents just under one allocation group; the
blkcdf and extcdf columns are cumulative percentages. A volume with
many tiny extents is a candidate for free-space maintenance, but
defragmenting a live XFS filesystem is not a routine operation. The
useful first step is to identify whether the free space is actually
fragmented and whether the workload allocates large contiguous extents.
Create a data directory owned by john, then write a small file from john's account:
# mkdir -p /mnt/xfs/projects
# chown john:john /mnt/xfs/projects
Both commands are silent. As john:
$ dd if=/dev/zero of=/mnt/xfs/projects/fill.bin bs=1M count=8 status=none
dd is silent because status=none suppresses its transfer summary. The
file is 8 MiB; XFS may delay the allocation and speculative preallocation
may reserve more space than the file currently uses. xfs_spaceman and
df may therefore disagree immediately after a write. sync or a
subsequent close/fsync makes the accounting settle.
XFS grows online. The underlying block device must first become larger. On a partition, that means resizing the partition while the filesystem is unmounted, rereading the table, mounting again, and then growing the filesystem. On LVM or a resizable virtual disk, the outer layer can be grown differently (Chapter 13 and Chapter 14). For this practice partition, grow the partition from 1020 MiB to 2031 MiB:
# umount /mnt/xfs
# parted -s /dev/vdb unit MiB resizepart 4 6500
# partprobe /dev/vdb
# mount /dev/vdb4 /mnt/xfs
All four commands are silent. The partition now extends into the
unallocated tail that was left at creation. xfs_growfs takes the mount
point and expands the filesystem to the new device size:
# xfs_growfs /mnt/xfs
meta-data=/dev/vdb4 isize=512 agcount=4, agsize=65280 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=1
= reflink=1 bigtime=1 inobtcount=1 nrext64=1
= exchange=0 metadir=0
data = bsize=4096 blocks=261120, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1, parent=0
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
= rgcount=0 rgsize=0 extents
data blocks changed from 261120 to 519936
The final line is the result that matters: the data section grew from
261120 to 519936 blocks. The geometry above it is the pre-growth state.
df -h confirms the new capacity:
# df -h /mnt/xfs
Filesystem Size Used Avail Use% Mounted on
/dev/vdb4 2.0G 79M 1.9G 4% /mnt/xfs
xfs_growfs option |
Description |
|---|---|
-d |
Grow the data section to the largest possible size. |
-D SIZE |
Set the data section to SIZE blocks. Values larger than the current size grow it; smaller values use an experimental shrink path that may refuse the request. |
-n |
Print the geometry and check arguments without changing anything. |
-m PERCENT |
Set the maximum percentage of the filesystem usable for inodes. |
-l, -r |
Grow the log or realtime section where configured. |
There is no ordinary supported XFS shrink. A smaller xfs_growfs -D
value exercises an experimental data-section shrink path; a target that
leaves too little allocation-group space is rejected with
XFS_IOC_FSGROWFSDATA failed: Invalid argument. Plan a migration for a
reliable reduction: copy the data to a new filesystem and switch the
mount. Chapter 14 gives that procedure.
xfs_spaceman command |
Description |
|---|---|
freesp -s |
Summarize free-space extents. |
freesp -g |
Show free block and extent counts per allocation group. |
info |
Print the same geometry as xfs_info. |
health -f |
Report collected health information; xfs_scrub must first gather it where the kernel supports online scrub. |
xfs_spaceman opens a mounted filesystem. It is a query tool; it does
not move extents or repair structures.
12.3.3. Offline Repair and the Consequences of Log Clearing
xfs_repair must see an unmounted filesystem. The -n option performs a
read-only scan and prints what would be repaired. The phase progress goes
to standard error, while standard output receives only a one-line notice;
the useful result is the summary and the exit status.
The filesystem is still mounted from the growth exercise, and
/mnt/xfs/projects is owned by john. As john, write a small file that the
checks below will inspect:
$ printf "%s\n" "xfs notes" > /mnt/xfs/projects/notes.txt
The write is silent. Back in the root shell, read the file's inode number:
# ls -i /mnt/xfs/projects/notes.txt
133 /mnt/xfs/projects/notes.txt
The first column is the inode number; it is 133 here, and that value is
used again in the xfs_db command below. Substitute whatever your own
ls -i prints. Unmount so the checker sees a quiet filesystem:
# umount /mnt/xfs
The unmount is silent. Then scan the unmounted filesystem. Selected output; the per-allocation-group detail under phases 3 and 4 is omitted:
# xfs_repair -n /dev/vdb4
Phase 1 - find and verify superblock...
Phase 2 - using internal log
- zero log...
- scan filesystem freespace and inode maps...
- found root inode chunk
Phase 3 - for each AG...
...
Phase 7 - verify link counts...
No modify flag set, skipping filesystem flush and exiting.
The command exits 0 when no repair is needed. The phases omitted above
scan allocation groups, inode maps, duplicate blocks, and inode
connectivity. -n is not a guarantee: the manual warns that it does not
fully validate free-space and inode maps, so a filesystem that checks
clean can still have subtle inconsistencies. It is the right first pass,
not a substitute for a repair when there is a known problem.
To create a controlled inconsistency, set that inode's link count to a
wrong value with xfs_db. It edits the metadata directly, bypassing the
filesystem driver:
# xfs_db -x -c "inode 133" -c "write core.nlinkv2 99" /dev/vdb4
core.nlinkv2 = 99
A read-only repair now finds the mismatch and exits 1; selected output, with the phase detail omitted:
# xfs_repair -n /dev/vdb4
Phase 1 - find and verify superblock...
...
Phase 7 - verify link counts...
would have reset inode 133 nlinks from 99 to 1
No modify flag set, skipping filesystem flush and exiting.
The actual repair resets the count and exits 0; selected output:
# xfs_repair /dev/vdb4
Phase 1 - find and verify superblock...
...
Phase 7 - verify and correct link counts...
resetting inode 133 nlinks from 99 to 1
done
A second read-only scan confirms the result; selected output, again with the phase detail omitted:
# xfs_repair -n /dev/vdb4
Phase 1 - find and verify superblock...
...
Phase 7 - verify link counts...
No modify flag set, skipping filesystem flush and exiting.
It exits 0. The repair preserved the file and its data; it corrected only the metadata count.
xfs_repair option |
Description |
|---|---|
-n |
Read-only scan; report what would be repaired. Exit 1 if repairs are needed. |
-L |
Force log zeroing, even if the log is dirty. Last resort only. |
-f |
Treat the argument as a filesystem image file rather than a block device. |
-m MB |
Limit the memory used by the repair. |
-P |
Disable inode and directory prefetching if a repair stalls. |
-v |
Verbose output. |
A dirty log and the meaning of -L. When a filesystem is mounted
and writing, XFS records metadata changes in
its log. A crash can leave a log with committed transactions that have
not yet been written back to their home locations. The kernel can replay
that log; xfs_repair cannot. The following exercise creates a
crash-consistent image without crashing the machine. It is a
controlled way to see the refusal; on a real system the same state comes
from a power loss. The image is a file under john's workspace, attached
to /dev/loop0:
# truncate -s 400M /home/john/debian-handbook/filesystems/xfs-crash.img
# losetup /dev/loop0 /home/john/debian-handbook/filesystems/xfs-crash.img
# mkfs.xfs -f -q -L crashlab /dev/loop0
# mkdir -p /mnt/xfs-crash
# mount /dev/loop0 /mnt/xfs-crash
All five commands are silent. Create one checkpointed file, sync it, then create a second file that represents a recent transaction:
# mkdir -p /mnt/xfs-crash/before
# printf "%s\n" "checkpointed" > /mnt/xfs-crash/before/kept.txt
# sync
# printf "%s\n" "logged" > /mnt/xfs-crash/late.txt
These commands are silent. Freeze the filesystem, copy the block device to a snapshot file, then thaw and unmount the original:
# fsfreeze -f /mnt/xfs-crash
# dd if=/dev/loop0 of=/home/john/debian-handbook/filesystems/xfs-crash-snapshot.img bs=1M status=none
# fsfreeze -u /mnt/xfs-crash
# umount /mnt/xfs-crash
# losetup -d /dev/loop0
All five commands are silent. The copy is crash-consistent and has a log
that was not closed with an unmount record. Preserve a second copy of the
dirty image now: the -L exercise later in this section destroys the log
in the copy it works on, so keep one image for the normal replay path and
one for the forced path. A plain file copy is sufficient because the image
is not mounted:
# cp /home/john/debian-handbook/filesystems/xfs-crash-snapshot.img /home/john/debian-handbook/filesystems/xfs-crash-forced.img
The command is silent. xfs_repair detects the dirty log and refuses to
continue, exiting 2:
# xfs_repair -f /home/john/debian-handbook/filesystems/xfs-crash-snapshot.img
Cannot get host filesystem geometry.
Repair may fail if there is a sector size mismatch between
the image and the host filesystem.
Phase 1 - find and verify superblock...
Cannot get host filesystem geometry.
Repair may fail if there is a sector size mismatch between
the image and the host filesystem.
Phase 2 - using internal log
- zero log...
ERROR: The filesystem has valuable metadata changes in a log which needs to
be replayed. Mount the filesystem to replay the log, and unmount it before
re-running xfs_repair. If you are unable to mount the filesystem, then use
the -L option to destroy the log and attempt a repair.
Note that destroying the log may cause corruption -- please attempt a mount
of the filesystem before doing this.
The Cannot get host filesystem geometry warnings appear because the
argument is an image file, not a block device, so xfs_repair cannot
confirm the sector geometry the host would use; they are advisory and
repeat before the superblock and log phases. The ERROR is the refusal
itself: the log holds metadata transactions that were never written to
their home locations, and xfs_repair will not zero them behind their
backs.
Now replay the log by mounting the original image and unmounting it cleanly:
# mount -o loop /home/john/debian-handbook/filesystems/xfs-crash-snapshot.img /mnt/xfs-crash
The mount is silent. Read the replayed tree:
# ls -l /mnt/xfs-crash /mnt/xfs-crash/before
/mnt/xfs-crash:
total 4
drwxr-xr-x 2 root root 22 Nov 20 07:32 before
-rw-r--r-- 1 root root 7 Nov 20 07:32 late.txt
/mnt/xfs-crash/before:
total 4
-rw-r--r-- 1 root root 13 Nov 20 07:32 kept.txt
Both files are present after replay. Unmount and confirm the log is now clean:
# umount /mnt/xfs-crash
The unmount is silent. Confirm the replay left a clean log. Selected output; the geometry warnings repeat, the phase detail is omitted, and the final line matches the clean scan from Section 12.3.3:
# xfs_repair -n -f /home/john/debian-handbook/filesystems/xfs-crash-snapshot.img
Phase 1 - find and verify superblock...
Phase 2 - using internal log
- zero log...
...
Phase 7 - verify link counts...
No modify flag set, skipping filesystem flush and exiting.
The exit status is 0. This is the normal path: replay the log by mounting, unmount cleanly, then repair.
-L is the last resort for a log that cannot be replayed. It discards the
log rather than replaying it. Use the copy preserved above, because the
operation destroys the only record of the recent metadata transactions.
Selected output; the geometry warnings and phase detail are omitted:
# xfs_repair -f -L /home/john/debian-handbook/filesystems/xfs-crash-forced.img
Phase 1 - find and verify superblock...
Phase 2 - using internal log
- zero log...
ALERT: The filesystem has valuable metadata changes in a log which is being
destroyed because the -L option was used.
- scan filesystem freespace and inode maps...
clearing needsrepair flag and regenerating metadata
sb_fdblocks 84323, counted 85978
...
Phase 7 - verify and correct link counts...
Maximum metadata LSN (1:17) is ahead of log (1:2).
Format log to cycle 4.
done
The repair formats the log and rebuilds the free-space and inode
structures from the on-disk metadata. The sb_fdblocks line shows the
cost: the superblock's free-block count did not match what the repair
counted on disk, because the discarded log still held block allocations
that the checkpointed metadata had never recorded. The filesystem mounts
afterward, but any metadata update that existed only in the discarded
log is gone. In this controlled image the files had already been
checkpointed, so they remain visible:
# mount -o loop /home/john/debian-handbook/filesystems/xfs-crash-forced.img /mnt/xfs-crash
The mount is silent. List the result:
# ls -l /mnt/xfs-crash /mnt/xfs-crash/before
/mnt/xfs-crash:
total 4
drwxr-xr-x 2 root root 22 Nov 20 07:32 before
-rw-r--r-- 1 root root 7 Nov 20 07:32 late.txt
/mnt/xfs-crash/before:
total 4
-rw-r--r-- 1 root root 13 Nov 20 07:32 kept.txt
That result is specific to this image. On a real crash, late.txt and the
directory update might exist only in the log; -L would discard them,
and the repair might place disconnected inodes in lost+found or fail to
recover them at all. Use -L only after a mount-and-unmount replay has
failed, and only after preserving an image for later recovery.
Clean up the practice images when finished:
# umount /mnt/xfs-crash
# rm -f /home/john/debian-handbook/filesystems/xfs-crash.img /home/john/debian-handbook/filesystems/xfs-crash-snapshot.img /home/john/debian-handbook/filesystems/xfs-crash-forced.img
Both commands are silent. The second removes only the three files created by this exercise.