Skip to contentSkip to conversation

An idea for supporting large directories and readdir+stat workloads

DISCUSSION Ted Ts'o 2 participants 4 messages Aug 11, 2012 – Aug 13, 2012

lore
Share & tools
lore

Ted Ts'o

author
#0 lore raw

I was thinking about how to respond to Andreas's offer:

Quoted 2 lines · Andreas Dilger

We have a patch to fix the 2-level htree and 2GB directory size limits already, in case that is of interest to anyone.

where in theory, I'd be interested, but there have been enough other performance problems with really large directories that it was never been clear to me it would be a huge win, and given the many other changes queued up, I was hesitant.

... and then I started thinking about this while I was in the shower, and I think I've come up with a really good solution to the problem. What if we also integrated the proposed extension that you guys have designed for storing additional metadata in the directory, and then for files that do not have hard links, we keep a copy of all of the fs metadata required for stat(2) in the directory?

This would speed up the readdir() + stat(2) workload, which has always been the major problem, in the common case where a file is linked to via only a single directory. If a file gets hard-linked, we will need to drop the copy in the directory and then make sure the fields in the inode table are up-to-date. But if we do this, I think it would be a huge improvement for ext4.

What do folks think?

- Ted

P.S. We might need to add a special case hack for the SELINUX sid, and put it in the extended directory entry as well, since otherwise every single stat operation will need to go to the inode table just to read the SELINUX extended attribute. But I don't really care about that, since my personal answer to SELINUX has been "just say no". But I do know there are enough people using it that we probably do need to accomodate those poor folks....

#1 lore raw
Quoted 18 lines · Ted Ts'o

I was thinking about how to respond to Andreas's offer:

Quoted 2 lines · Andreas Dilger

We have a patch to fix the 2-level htree and 2GB directory size limits already, in case that is of interest to anyone.

where in theory, I'd be interested, but there have been enough other performance problems with really large directories that it was never been clear to me it would be a huge win, and given the many other changes queued up, I was hesitant.

... and then I started thinking about this while I was in the shower, and I think I've come up with a really good solution to the problem. What if we also integrated the proposed extension that you guys have designed for storing additional metadata in the directory, and then for files that do not have hard links, we keep a copy of all of the fs metadata required for stat(2) in the directory?

Quoted 8 lines

This would speed up the readdir() + stat(2) workload, which has always been the major problem, in the common case where a file is linked to via only a single directory. If a file gets hard-linked, we will need to drop the copy in the directory and then make sure the fields in the inode table are up-to-date. But if we do this, I think it would be a huge improvement for ext4.

What do folks think?

Essentially, this is storing the inode in the directory. What might be possible is to have the directory leaf block point be the same thing as the inode table block, and then use an xattr in the inode to hold the parent directory number and filename as the "dirent".

We are actually already storing the Lustre parent FID and filename in an xattr in the inode as a mechanism for doing inode->path generation on the fly, but this could replace the directory entry entirely. It is possible to have multiple links to a file by having multiple xattr entries on the inode, and if some inode is renamed to another directory then the xattr is changed. If there are a lot of hard links to a single file, then it is possible that an external xattr block would be needed, but in the overwhelmingly common case of a single link it can be stored in the inode.

The htree index would be used to reference the various inode table blocks. Each inode in that block would check the xattr for entries for the parent directory that is doing readdir/lookup, and would otherwise be ignored if
the parent inode does not match.

Quoted 7 lines

P.S. We might need to add a special case hack for the SELINUX sid, and put it in the extended directory entry as well, since otherwise every single stat operation will need to go to the inode table just to read the SELINUX extended attribute. But I don't really care about that, since my personal answer to SELINUX has been "just say no". But I do know there are enough people using it that we probably do need to accomodate those poor folks....

I think that by the time we store all of the "stat" attributes into the directory it would essentially duplicate the inode, and increase overhead instead of reducing it. Every inode update would likely require also updating the directory (mtime, ctime, etc).

Cheers, Andreas

#2 lore raw
Quoted 1 line · Andreas Dilger

Essentially, this is storing the inode in the directory.

Well, except we don't need to store *all* of the inode in the directory. My proposal was to include just the bits which are necessary for stat(2) to function, which is about 64 bytes including the SELinux SID.

If we store the "compact inode" in the directory, for an average file name length of, say, 12 bytes, we can still store some 50-52 directory entries in each 4k block. If the htree code directs us to the correct leaf block, we can still do lookups and stats very quickly.

Quoted 4 lines

What might be possible is to have the directory leaf block point be the same thing as the inode table block, and then use an xattr in the inode to hold the parent directory number and filename as the "dirent".

There are a couple of problems I see with this approach; one i show do we handle hash collisions in this new scheme? You could put multiple entries in the index block, one for each inode table block, I suppose.

Another issue is that it will slow down a pure readdir() only workload significantly, since it will require a large number of random reads into the inode table. Even a readdir+stat workload will require many more random reads than the "compact inode" in the directory entry which I propose.

Quoted 4 lines

I think that by the time we store all of the "stat" attributes into the directory it would essentially duplicate the inode, and increase overhead instead of reducing it. Every inode update would likely require also updating the directory (mtime, ctime, etc).

What I was proposing was in the case where the inode had only a single hard link (the common case), the information in the "compat inode" (i.e., in the directory entry) would take precedence over what was stored in the inode table. This means that we would only need to update the directory block -- and these are the fields that we would need to store:

Field Size

ino 4
dev 4
mode 4
uid 4
gid 4
size 8
atime 8
mtime 8
ctime 8
blocks 10
selinux sid 4

               ====

TOTAL 62

So basically, instead of updating the inode table, we would have to update the directory block instead. So there wouldn't be any extra I/O overhead. In terms of increasing the size of the directory, it would certainly do that; but I think the speed improvements could very well be worth it.

Regards,

- Ted

#3 lore raw
Quoted 3 lines · Theodore Ts'o

Field Size

ino 4

Silly me, we don't need actually need to store the inode a second time, since that's already in the struct ext4_dir_entry.

Quoted 1 line

dev 4

... and we only need to store the device if this is a block or character device, so we could actually use the i_size field below to store the dev_t for devices inodes.

So this would shrink the size of the "compact inode" that would be stored in the directory entry down from 62 bytes to 58 bytes.

- Ted