On Wed, Jul 19, 2023 at 11:06:57PM -0700, Eric Biggers wrote: > > I'm also having trouble understanding exactly when ->d_name is stable here. > AFAICS, unfortunately the VFS has an edge case where a dentry can be moved > without its parent's ->i_rwsem being held. It happens when a subdirectory is > "found" under multiple names. The VFS doesn't support directory hard links, so > if it finds a second link to a directory, it just moves the whole dentry tree to > the new location. This can happen if a filesystem image is corrupted and > contains directory hard links. Coincidentally, it can also happen in an > encrypted directory due to the no-key name => normal name transition... Sorry, I think I got this slightly wrong. The move does happen with the parent's ->i_rwsem held, but it's for read, not for write. First, before ->lookup is called, the ->i_rwsem of the parent directory is taken for read. ->lookup() calls d_splice_alias() which can call __d_unalias() which does the __d_move(). If the old alias is in a different directory (which cannot happen in that fscrypt case, but can happen in the general "directory hard links" case), __d_unalias() takes that directory's ->i_rwsem for read too. So it looks like the parent's ->i_rwsem does indeed exclude moves of child dentries, but only if it's taken for *write*. So I guess you can rely on that; it's just a bit more subtle than it first appears. Though, some of your explanation seems to assume that a read lock is sufficient ("In __lookup_slow, either the parent inode is locked by the caller (lookup_slow) ..."), so maybe there is still a problem. - Eric