How to fix the "Input/Output error" error in Linux?

cannot open directory '.': Input/Output error Metadata corruption detected at xfs dir2 leaf xfs_repair, disk repair, lvscan, pvs

How to fix the "Input/Output error" error in Linux?
Photo by Umberto / Unsplash

Are you seeing errors “Input/Output error” while accessing a filesystem or trying to delete the file?

ls: cannot open directory '.': Input/Output error

or kernel messages like when running dmesg command

XFS (dm-2): Metadata corruption detected at xfs dir2 leaf removename+0x407/0x470 [xfs]

XFS (dm-2): Unmount and run xfs_repair

XFS (dm-2): Internal error xfs trans cancel at line 954 of file fs/xfs/xfs trans.c. Calle

CPU: 1 PID: 3380 Comm: rm Kdump: Loaded Tainted: G 85.928444] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS

This usually happens in the case of a sudden power off while the system is running some I/O operations or someone forcing the shutdown of the system while the system is still running some critical operation. It usually corrupts the metadata of the file system which could be repaired by running a few commands in partitions.

In order to repair the partitions, the first step is to identify which partition is corrupted. Usually, you won’t be able to repair the disk after starting the system as you can’t run the filesystem repair command when the partition is mounted with read-write permissions.

If you are not doing any file I/O operation you should be able to run the ls -la command to check the file permissions. It may show file meta information with a “?” mark.

image.webp

NOTE: Here, we have taken Rocky Minimal ISO to troubleshoot the system as the disk had Rocky based system. Always prefer the same distro linux to run this operation.

In order to solve this issue, the first thing you can try to boot the system with ISO or CD. Select the option Troubleshooting from the menu screen.
Screenshot-2022-12-14-020210.webp

Select the option Rescue a Rocky Linux System.Screenshot-2022-12-14-020253-1.webp

These are critical operations and they may result in data loss so always double-check before running any commands. Read the full description and use the help command to understand the operation completely. This issue started occuring on Virtual Machine when forced power button issued when Host machine was running some update and one the of guest Virtual Machine was running I/O operation. Sudden power off corrupted the metadata of in the Virtual Machine. After restarting the system while running any bash script or removing the files throwing the error "Input/Output error".

Once it finishes the startup process then it should stop at the command shell with waiting for user input. Select 2 among them as we want to mount the local file system with Read Only. Post that it should load the rescue shell where we gonna run a few commands.
Screenshot_20221214_020557.webp

You can try running df -h command to check the file system mounted currently. In this case, disk file systems are not mounted so we gonna try to scan the disk and activate it.
Screenshot_20221214_021645.webp

df -h

Run the command lvscan to know the other inactive partitions. Here, you can notice the /dev/rl/root and currently inactive.

Now, we gonna try to activate the inactive dev mapper path using the command vgchange -a y

Post running the command you can verify using lvscan command whether the inactive status changed or not.

Now is the time to run the xfs_repair command. Run the xfs_repair command with the dev mapper path.

lvscan
vgchange -a y
lvscan
sbin/xfs_repair /dev/rl/root

It may throw some error and provide you some suggestions like running the command with a particular command option here it shows run with -L to destroy the logs. It may suggest with -d option which means Repair Dangerously. In one of the cases, we found that it worked as it was able to delete all the unmapped entries of the files.
Screenshot-2022-12-14-022016-1.webp

Once it finishes the repair successfully. Boot the system with the local disk. It should be working now.

reboot

This might have solved the issue. In case, it is not helping feel free to update in the comment sections. You can also refer the discussion on input/output error when accessing a directory