在 Linux 中删除超大目录时出现“遍历失败:u:错误消息”

在 Linux 中删除超大目录时出现“遍历失败:u:错误消息”

我试图删除一些非常大的目录,但没有成功。以下是一些观察结果:

# cwd contains the two larger directories
$ ls -lhF
drwxrwxr-x 2 hongxu hongxu 471M Oct 16 18:52 J/
drwxr-xr-x 2 hongxu hongxu 5.8M Oct 16 17:21 u/
# Note that this is the output of `ls` of the directory themselves so they should be *huge*
# J/ seems much larger than u/ (containing more files), so take u/ as an example

$ rm -rf u/
# hang for a very long time, and finally report
rm: traversal failed: u: Bad message

$ cd u/
# can cd into u/ without problems

$ ls -lhF
# hang for a long time; cancel succeeds when I press Ctrl-C

$ rm *
# hang for a long time; cancel fails when I press Ctrl-C
# however there are no process associated with `rm` as reported by `ps aux`

这两个目录大多包含很多小文件(我想每个文件不超过10k)。现在我必须删除这两个目录以释放更多磁盘空间。我应该怎么办?

更新1:
请查看输出,rm -rf u/其中显示rm: traversal failed: u: Bad message经过相当长的时间(> 2 小时)。因此,问题似乎不在于效率。

更新2:
申请时fsck,它报告如下(看起来不错):

$ sudo fsck -A -y /dev/sda2
fsck from util-linux 2.31.1
fsck.fat 4.1 (2017-01-24)
/dev/sda1: 13 files, 1884/130812 clusters

$ df /dev/sda2
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/sda2      244568380 189896000  43628648  82% /

更新3:
如果可能相关(但可能不相关),这两个目录(J/u/)包含terminfotic命令生成的;与常规编译的 terminfo 文件(例如,内部的那些)不同/lib/terminfo,这些文件是使用一些模糊测试技术生成的,因此可能不是“合法”的 terminfo 文件。无关紧要!

更新4:
更多观察结果:

$ find u/ -type f | while read f; do echo $f; rm -f $f; done
# hang for a long time, IUsed (`df -i /dev/sda2`) not decreased
$ mkdir emptyfolder && rsync -r --delete emptyfolder/ u/
# hang for a long time, IUsed (`df -i /dev/sda2`) not decreased
$ strace rm -rf u/
execve("/bin/rm", ["rm", "-rf", "u"], 0x7fffffffc550 /* 121 vars */) = 0                                                                                                                       
brk(NULL)                               = 0x555555764000                                                                                                                                       
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)                                                                                                                
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)                                                                                                                
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3                                                                                                                                   
fstat(3, {st_mode=S_IFREG|0644, st_size=125128, ...}) = 0                                                                                                                                      
mmap(NULL, 125128, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ffff7fd8000                                                                                                                              
close(3)                                = 0                                                                                                                                                    
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)                                                                                                                
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3                                                                                                                    
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832                                                                                                     
fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, ...}) = 0                                                                                                                                     
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffff7fd6000                                                                                                      
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ffff79e4000                                                                                                     
mprotect(0x7ffff7bcb000, 2097152, PROT_NONE) = 0                                                                                                                                               
mmap(0x7ffff7dcb000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7ffff7dcb000                                                                           
mmap(0x7ffff7dd1000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ffff7dd1000                                                                                 
close(3)                                = 0                                                                                                                                                    
arch_prctl(ARCH_SET_FS, 0x7ffff7fd7540) = 0                                                                                                                                                    
mprotect(0x7ffff7dcb000, 16384, PROT_READ) = 0                                                                                                                                                 
mprotect(0x555555762000, 4096, PROT_READ) = 0                                                                                                                                                  
mprotect(0x7ffff7ffc000, 4096, PROT_READ) = 0                                                                                                                                                  
munmap(0x7ffff7fd8000, 125128)          = 0                                                                                                                                                    
brk(NULL)                               = 0x555555764000                                                                                                                                       
brk(0x555555785000)                     = 0x555555785000                                                                                                                                       
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3                                                                                                                     
fstat(3, {st_mode=S_IFREG|0644, st_size=1683056, ...}) = 0                                                                                                                                     
mmap(NULL, 1683056, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ffff7e3b000                                                                                                                             
close(3)                                = 0                                                                                                                                                    
ioctl(0, TCGETS, {B38400 opost isig icanon echo ...}) = 0                                                                                                                                      
lstat("/", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0                                                                                                                                      
newfstatat(AT_FDCWD, "u", {st_mode=S_IFDIR|0755, st_size=6045696, ...}, AT_SYMLINK_NOFOLLOW) = 0                                                                                               
openat(AT_FDCWD, "u", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW|O_DIRECTORY) = 3                                                                                                                 
fstat(3, {st_mode=S_IFDIR|0755, st_size=6045696, ...}) = 0                                                                                                                                     
fcntl(3, F_GETFL)                       = 0x38800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_NOFOLLOW|O_DIRECTORY)                                                                               
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0                                                                                                                                                    
getdents(3, /* 2 entries */, 32768)     = 48                                                                                                                                                   
getdents(3, /* 1 entries */, 32768)     = 24                                                                                                                                                   
... (repeated lines)                                                                                                                                                                           
getdents(3, /* 1 entries */, 32768)     = 24                                                                                                                                                   
getdents(3strace: Process 5307 detached                                                                                                                                                        
 <detached ...>
# (manually killed)
$ ls -f1 u/
./ 
../
../
../
../
... (repeated lines)
../
$ sudo journalctl -ex
Oct 17 16:00:16 CSLRF03AU kernel: JBD2: Spotted dirty metadata buffer (dev = sda2, blocknr = 0). There's a risk of filesystem corruption in case of system crash.
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error: 6971 callbacks suppressed
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm find: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm zsh: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm rm: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm find: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm rsync: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm zsh: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm zsh: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm rm: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm find: Directory index failed checksum
Oct 17 16:00:20 CSLRF03AU kernel: EXT4-fs error (device sda2): ext4_htree_next_block:948: inode #9789534: block 1020: comm find: Directory index failed checksum
# #9789534 is the inode of `u/` as reported by `ls -i`

所以应该是文件系统损坏。但重启不起作用:(

答案1

好吧,我终于解决了问题。这是由于文件系统错误导致ls显示错误以及其他实用程序发生故障。
很抱歉,问题标题具有误导性(尽管里面确实有很多文件u/,但目录是不是很大)。

我通过使用实时 USB 解决了这个问题,因为损坏的文件系统是/.修复方法只是应用损坏的磁盘所在的sudo fsck -cfk /dev/sda2位置。dev/sda2

答案2

您无法使用 删除大量文件rm。你可以做

find u/ -type f -print0 | xargs -r -0 rm -f

这只会删除文件;要删除所有内容,请使用

find u/ -print0 | xargs -r -0 rm -rf

如果您的系统有该选项,您可能可以使用该--delete选项:find

find u/ -type f --delete

或时髦的方法rsync

mkdir emptyfolder
rsync -r --delete emptyfolder/ u/

rsync比删除内容要快得多rm,因为它会绕过一些检查。

答案3

您可以尝试find /u -type f | while read f; do rm -f $f; done 这将需要一段时间,但可能会有效。由于某种原因,当其他方法失败时,bash 中的循环可以很好地工作。

相关内容