开发者

How to "defragment" a directory on ext3?

开发者 https://www.devze.com 2023-01-27 04:47 出处:网络
I am running a daemon which analyses files in a directory and then deletes them. In case the daemon is not running for whatever rea开发者_运维技巧son, files get stacked there. Today I had 90k files in

I am running a daemon which analyses files in a directory and then deletes them. In case the daemon is not running for whatever rea开发者_运维技巧son, files get stacked there. Today I had 90k files in that directory. After starting the daemon again, it processed all the files.

However, the directory remains large; "ls -dh ." returns a size of 5.6M. How can I "defragment" that directory? I already figured out that renaming that directory, and creaing a new one with the same name and permissions solves the problem. However, as files get written in there at any time, there doesn't seem to be a safe way to rename the directory and create a new one as for a moment, the target directory does not exist.

So a) is there a way/a (shell) program which can defragment directories on an ext3 filesystem? or b) is there a way to create a lock on a directory so that trying to write files blocks until the rename/create has finished?


"Optimize directories in filesystem. This option causes e2fsck to try to optimize all directories, either by reindexing them if the filesystem supports directory indexing, or by sorting and compressing directories for smaller directories, or for filesystems using traditional linear directories." -- fsck.ext3 -D

Of course this should not be done on a mounted filesystem.


Not really applicable for Ext3, but maybe useful for users of other filesystems:

  • according to https://wiki.archlinux.org/index.php/Btrfs#Defragmentation, with Btrfs it is apparently possible to defragment the metadata of a directory: btrfs filesystem defragment / will defragment the metadata of the root folder. This uses the online defragmentation support of Btrfs.

  • while Ext4 does support online defragmentation (with e4defrag), this doesn't seem to apply to directory metadata (according to http://sourceforge.net/p/e2fsprogs/bugs/326/).

I haven't tried either of these solutions, though.


I'm not aware of a way to reclaim free space from within a directory.

5MB isn't very much space, so it may be easiest to just ignore it. If this problem (files stacking up in the directory) occurs on a regular basis, then that space will be reused anytime the directory fills up again.

If you desperately need the ability to shrink the directory here's a (ugly) hack that might work.

Replace the directory with a symbolic link to an empty directory. If this problem reoccurs, you can create a new empty directory, and then change the symlink to point to the new directory. Changing the symlink should be atomic, so you won't loose any incoming files. Then you can safely empty and delete the old directory.

[Edited to add: It turns out that this does not work. As Bada points out in the comments you can't atomically change a symlink in the way I suggested. This leaves me with my original point. File systems I'm familiar with don't provide a mechanism to reclaim free space within directory blocks.]

0

精彩评论

暂无评论...
验证码 换一张
取 消