On Thu, Jun 01, 2023 at 12:58:24PM +0200, Jan Kara wrote: > Currently the locking order of inode locks for directories that are not > in ancestor relationship is not defined because all operations that > needed to lock two directories like this were serialized by > sb->s_vfs_rename_mutex. However some filesystems need to lock two > subdirectories for RENAME_EXCHANGE operations and for this we need the > locking order established even for two tree-unrelated directories. > Provide a helper function lock_two_inodes() that establishes lock > ordering for any two inodes and use it in lock_two_directories(). > > CC: stable@xxxxxxxxxxxxxxx > Signed-off-by: Jan Kara <jack@xxxxxxx> > --- > fs/inode.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > fs/internal.h | 2 ++ > fs/namei.c | 4 ++-- > 3 files changed, 46 insertions(+), 2 deletions(-) > > diff --git a/fs/inode.c b/fs/inode.c > index 577799b7855f..4000ab08bbc0 100644 > --- a/fs/inode.c > +++ b/fs/inode.c > @@ -1103,6 +1103,48 @@ void discard_new_inode(struct inode *inode) > } > EXPORT_SYMBOL(discard_new_inode); > > +/** > + * lock_two_inodes - lock two inodes (may be regular files but also dirs) > + * > + * Lock any non-NULL argument. The caller must make sure that if he is passing > + * in two directories, one is not ancestor of the other. Zero, one or two > + * objects may be locked by this function. > + * > + * @inode1: first inode to lock > + * @inode2: second inode to lock > + * @subclass1: inode lock subclass for the first lock obtained > + * @subclass2: inode lock subclass for the second lock obtained > + */ > +void lock_two_inodes(struct inode *inode1, struct inode *inode2, > + unsigned subclass1, unsigned subclass2) > +{ > + if (!inode1 || !inode2) I think you forgot the opening bracket... I can just fix this up for you though. > + /* > + * Make sure @subclass1 will be used for the acquired lock. > + * This is not strictly necessary (no current caller cares) but > + * let's keep things consistent. > + */ > + if (!inode1) > + swap(inode1, inode2); > + goto lock; > + }