Whamcloud - gitweb
LU-15801 ldiskfs: Server support for RHEL9 69/47169/10
authorShaun Tancheff <shaun.tancheff@hpe.com>
Tue, 15 Nov 2022 12:10:42 +0000 (06:10 -0600)
committerOleg Drokin <green@whamcloud.com>
Tue, 13 Dec 2022 16:06:54 +0000 (16:06 +0000)
RHEL9 server patches update from SUSE 15 SP 4 series

Test-Parameters: trivial
HPE-bug-id: LUS-10920
Signed-off-by: Shaun Tancheff <shaun.tancheff@hpe.com>
Change-Id: I221f946d09892bf90406da70aa16432e5753d18a
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/47169
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Jian Yu <yujian@whamcloud.com>
config/lustre-build-ldiskfs.m4
ldiskfs/kernel_patches/patches/linux-5.16/ext4-corrupted-inode-block-bitmaps-handling-patches.patch [new file with mode: 0644]
ldiskfs/kernel_patches/patches/linux-5.16/ext4-inode-version.patch [new file with mode: 0644]
ldiskfs/kernel_patches/patches/linux-5.16/ext4-misc.patch [new file with mode: 0644]
ldiskfs/kernel_patches/patches/linux-5.16/ext4-osd-iop-common.patch [new file with mode: 0644]
ldiskfs/kernel_patches/patches/rhel9/ext4-data-in-dirent.patch [new file with mode: 0644]
ldiskfs/kernel_patches/patches/rhel9/ext4-give-warning-with-dir-htree-growing.patch [new file with mode: 0644]
ldiskfs/kernel_patches/patches/rhel9/ext4-max-dir-size.patch [new file with mode: 0644]
ldiskfs/kernel_patches/patches/rhel9/ext4-pdirop.patch [new file with mode: 0644]
ldiskfs/kernel_patches/series/ldiskfs-5.14-rhel9.series [new file with mode: 0644]

index ad611ca..f8d0b72 100644 (file)
@@ -13,6 +13,7 @@ esac
 AS_IF([test -z "$LDISKFS_SERIES"], [
 AS_IF([test x$RHEL_KERNEL = xyes], [
        case $RHEL_RELEASE_NO in
+       90)     LDISKFS_SERIES="5.14-rhel9.series"      ;;
        87)     LDISKFS_SERIES="4.18-rhel8.7.series"    ;;
        86)     LDISKFS_SERIES="4.18-rhel8.6.series"    ;;
        85)     LDISKFS_SERIES="4.18-rhel8.5.series"    ;;
diff --git a/ldiskfs/kernel_patches/patches/linux-5.16/ext4-corrupted-inode-block-bitmaps-handling-patches.patch b/ldiskfs/kernel_patches/patches/linux-5.16/ext4-corrupted-inode-block-bitmaps-handling-patches.patch
new file mode 100644 (file)
index 0000000..d64a450
--- /dev/null
@@ -0,0 +1,267 @@
+Since we could skip corrupt block groups, this patch
+use ext4_warning() intead of ext4_error() to make FS not
+remount RO in default
+
+---
+ fs/ext4/balloc.c  | 10 ++++----
+ fs/ext4/ialloc.c  |  6 ++---
+ fs/ext4/mballoc.c | 63 +++++++++++++++++++----------------------------
+ 3 files changed, 33 insertions(+), 46 deletions(-)
+
+diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
+index a0fb0c4..84c5c78 100644
+--- a/fs/ext4/balloc.c
++++ b/fs/ext4/balloc.c
+@@ -387,7 +387,7 @@ static int ext4_validate_block_bitmap(struct super_block *sb,
+                                                   desc, bh) ||
+                    ext4_simulate_fail(sb, EXT4_SIM_BBITMAP_CRC))) {
+               ext4_unlock_group(sb, block_group);
+-              ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
++              ext4_warning(sb, "bg %u: bad block bitmap checksum", block_group);
+               ext4_mark_group_bitmap_corrupted(sb, block_group,
+                                       EXT4_GROUP_INFO_BBITMAP_CORRUPT);
+               return -EFSBADCRC;
+@@ -395,8 +395,8 @@ static int ext4_validate_block_bitmap(struct super_block *sb,
+       blk = ext4_valid_block_bitmap(sb, desc, block_group, bh);
+       if (unlikely(blk != 0)) {
+               ext4_unlock_group(sb, block_group);
+-              ext4_error(sb, "bg %u: block %llu: invalid block bitmap",
+-                         block_group, blk);
++              ext4_warning(sb, "bg %u: block %llu: invalid block bitmap",
++                           block_group, blk);
+               ext4_mark_group_bitmap_corrupted(sb, block_group,
+                                       EXT4_GROUP_INFO_BBITMAP_CORRUPT);
+               return -EFSCORRUPTED;
+@@ -479,8 +479,8 @@ ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group,
+               ext4_unlock_group(sb, block_group);
+               unlock_buffer(bh);
+               if (err) {
+-                      ext4_error(sb, "Failed to init block bitmap for group "
+-                                 "%u: %d", block_group, err);
++                      ext4_warning(sb, "Failed to init block bitmap for group "
++                                   "%u: %d", block_group, err);
+                       goto out;
+               }
+               goto verify;
+diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
+index 4840190..f73d3f8 100644
+--- a/fs/ext4/ialloc.c
++++ b/fs/ext4/ialloc.c
+@@ -102,8 +102,8 @@ static int ext4_validate_inode_bitmap(struct super_block *sb,
+                                          EXT4_INODES_PER_GROUP(sb) / 8) ||
+           ext4_simulate_fail(sb, EXT4_SIM_IBITMAP_CRC)) {
+               ext4_unlock_group(sb, block_group);
+-              ext4_error(sb, "Corrupt inode bitmap - block_group = %u, "
+-                         "inode_bitmap = %llu", block_group, blk);
++              ext4_warning(sb, "Corrupt inode bitmap - block_group = %u, "
++                           "inode_bitmap = %llu", block_group, blk);
+               ext4_mark_group_bitmap_corrupted(sb, block_group,
+                                       EXT4_GROUP_INFO_IBITMAP_CORRUPT);
+               return -EFSBADCRC;
+@@ -353,7 +353,7 @@ out:
+               if (!fatal)
+                       fatal = err;
+       } else {
+-              ext4_error(sb, "bit already cleared for inode %lu", ino);
++              ext4_warning(sb, "bit already cleared for inode %lu", ino);
+               ext4_mark_group_bitmap_corrupted(sb, block_group,
+                                       EXT4_GROUP_INFO_IBITMAP_CORRUPT);
+       }
+diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
+index cff1df6..0ee37f0 100644
+--- a/fs/ext4/mballoc.c
++++ b/fs/ext4/mballoc.c
+@@ -1138,10 +1138,14 @@ int ext4_mb_generate_buddy(struct super_block *sb,
+       grp->bb_fragments = fragments;
+       if (free != grp->bb_free) {
+-              ext4_grp_locked_error(sb, group, 0, 0,
+-                                    "block bitmap and bg descriptor "
+-                                    "inconsistent: %u vs %u free clusters",
+-                                    free, grp->bb_free);
++              struct ext4_group_desc *gdp;
++              gdp = ext4_get_group_desc(sb, group, NULL);
++              ext4_warning(sb, "group %lu: block bitmap and bg descriptor "
++                           "inconsistent: %u vs %u free clusters "
++                           "%u in gd, %lu pa's",
++                           (long unsigned int)group, free, grp->bb_free,
++                           ext4_free_group_clusters(sb, gdp),
++                           grp->bb_prealloc_nr);
+               /*
+                * If we intend to continue, we consider group descriptor
+                * corrupt and update bb_free using bitmap value
+@@ -1485,7 +1489,7 @@ ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group,
+       int block;
+       int pnum;
+       int poff;
+-      struct page *page;
++      struct page *page = NULL;
+       int ret;
+       struct ext4_group_info *grp;
+       struct ext4_sb_info *sbi = EXT4_SB(sb);
+@@ -1511,7 +1515,7 @@ ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group,
+                */
+               ret = ext4_mb_init_group(sb, group, gfp);
+               if (ret)
+-                      return ret;
++                      goto err;
+       }
+       /*
+@@ -1611,6 +1615,7 @@ err:
+               put_page(e4b->bd_buddy_page);
+       e4b->bd_buddy = NULL;
+       e4b->bd_bitmap = NULL;
++      ext4_warning(sb, "Error loading buddy information for %u", group);
+       return ret;
+ }
+@@ -4587,9 +4592,11 @@ int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
+       }
+       if (free != free_in_gdp) {
+-              ext4_error(sb, "on-disk bitmap for group %d"
++              ext4_warning(sb, "on-disk bitmap for group %d"
+                       "corrupted: %u blocks free in bitmap, %u - in gd\n",
+                       group, free, free_in_gdp);
++              ext4_mark_group_bitmap_corrupted(sb, group,
++                                      EXT4_GROUP_INFO_BBITMAP_CORRUPT);
+               return -EIO;
+       }
+       return 0;
+@@ -4955,16 +4962,8 @@ ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
+       /* "free < pa->pa_free" means we maybe double alloc the same blocks,
+        * otherwise maybe leave some free blocks unavailable, no need to BUG.*/
+       if ((free > pa->pa_free && !pa->pa_error) || (free < pa->pa_free)) {
+-              ext4_error(sb, "pa free mismatch: [pa %p] "
+-                              "[phy %lu] [logic %lu] [len %u] [free %u] "
+-                              "[error %u] [inode %d] [freed %u]", pa,
+-                              (unsigned long)pa->pa_pstart,
+-                              (unsigned long)pa->pa_lstart,
+-                              pa->pa_len, (unsigned)pa->pa_free,
+-                              (unsigned)pa->pa_error, pa->pa_inode->i_ino,
+-                              free);
+               ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
+-                                      free, pa->pa_free);
++                                    free, pa->pa_free);
+               /*
+                * pa is already deleted so we use the value obtained
+                * from the bitmap and continue.
+@@ -5024,16 +5023,11 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
+       bitmap_bh = ext4_read_block_bitmap(sb, group);
+       if (IS_ERR(bitmap_bh)) {
+               err = PTR_ERR(bitmap_bh);
+-              ext4_error_err(sb, -err,
+-                             "Error %d reading block bitmap for %u",
+-                             err, group);
+               goto out_dbg;
+       }
+       err = ext4_mb_load_buddy(sb, group, &e4b);
+       if (err) {
+-              ext4_warning(sb, "Error %d loading buddy information for %u",
+-                           err, group);
+               put_bh(bitmap_bh);
+               goto out_dbg;
+       }
+@@ -5190,17 +5184,12 @@ repeat:
+               err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
+                                            GFP_NOFS|__GFP_NOFAIL);
+-              if (err) {
+-                      ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
+-                                     err, group);
++              if (err)
+                       return;
+-              }
+               bitmap_bh = ext4_read_block_bitmap(sb, group);
+               if (IS_ERR(bitmap_bh)) {
+                       err = PTR_ERR(bitmap_bh);
+-                      ext4_error_err(sb, -err, "Error %d reading block bitmap for %u",
+-                                     err, group);
+                       ext4_mb_unload_buddy(&e4b);
+                       continue;
+               }
+@@ -5495,11 +5484,8 @@ ext4_mb_discard_lg_preallocations(struct super_block *sb,
+               group = ext4_get_group_number(sb, pa->pa_pstart);
+               err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
+                                            GFP_NOFS|__GFP_NOFAIL);
+-              if (err) {
+-                      ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
+-                                     err, group);
++              if (err)
+                       continue;
+-              }
+               ext4_lock_group(sb, group);
+               list_del(&pa->pa_group_list);
+               ext4_get_group_info(sb, group)->bb_prealloc_nr--;
+@@ -5834,7 +5820,7 @@ errout:
+                        * been updated or not when fail case. So can
+                        * not revert pa_free back, just mark pa_error*/
+                       pa->pa_error++;
+-                      ext4_error(sb,
++                      ext4_warning(sb,
+                               "Updating bitmap error: [err %d] "
+                               "[pa %p] [phy %lu] [logic %lu] "
+                               "[len %u] [free %u] [error %u] "
+@@ -5845,6 +5831,7 @@ errout:
+                               (unsigned)pa->pa_free,
+                               (unsigned)pa->pa_error,
+                               pa->pa_inode ? pa->pa_inode->i_ino : 0);
++                      ext4_mark_group_bitmap_corrupted(sb, 0, 0);
+               }
+       }
+       ext4_mb_release_context(ac);
+@@ -6243,7 +6230,7 @@ do_more:
+       err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
+                                    GFP_NOFS|__GFP_NOFAIL);
+       if (err)
+-              goto error_return;
++              goto error_brelse;
+       /*
+        * We need to make sure we don't reuse the freed block until after the
+@@ -6334,8 +6321,9 @@ do_more:
+               goto do_more;
+       }
+ error_return:
+-      brelse(bitmap_bh);
+       ext4_std_error(sb, err);
++error_brelse:
++      brelse(bitmap_bh);
+       return;
+ }
+@@ -6436,7 +6424,7 @@ int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
+       err = ext4_mb_load_buddy(sb, block_group, &e4b);
+       if (err)
+-              goto error_return;
++              goto error_brelse;
+       /*
+        * need to update group_info->bb_free and bitmap
+@@ -6475,8 +6463,9 @@ int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
+               err = ret;
+ error_return:
+-      brelse(bitmap_bh);
+       ext4_std_error(sb, err);
++error_brelse:
++      brelse(bitmap_bh);
+       return err;
+ }
+@@ -6594,8 +6583,6 @@ ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
+       ret = ext4_mb_load_buddy(sb, group, &e4b);
+       if (ret) {
+-              ext4_warning(sb, "Error %d loading buddy information for %u",
+-                           ret, group);
+               return ret;
+       }
+-- 
+2.27.0
+
diff --git a/ldiskfs/kernel_patches/patches/linux-5.16/ext4-inode-version.patch b/ldiskfs/kernel_patches/patches/linux-5.16/ext4-inode-version.patch
new file mode 100644 (file)
index 0000000..e487696
--- /dev/null
@@ -0,0 +1,50 @@
+diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
+index d248a01..a154919 100644
+--- a/fs/ext4/ext4.h
++++ b/fs/ext4/ext4.h
+@@ -1165,6 +1165,8 @@ struct ext4_inode_info {
+       struct dquot *i_dquot[MAXQUOTAS];
+ #endif
++      __u64 i_fs_version;
++
+       /* Precomputed uuid+inum+igen checksum for seeding inode checksums */
+       __u32 i_csum_seed;
+diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
+index f73e5eb..5d0a11d 100644
+--- a/fs/ext4/ialloc.c
++++ b/fs/ext4/ialloc.c
+@@ -1264,6 +1264,7 @@ got:
+       ei->i_dtime = 0;
+       ei->i_block_group = group;
+       ei->i_last_alloc_group = ~0;
++      ei->i_fs_version = 0;
+       ext4_set_inode_flags(inode, true);
+       if (IS_DIRSYNC(inode))
+diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
+index 3bdfe01..b22628a 100644
+--- a/fs/ext4/inode.c
++++ b/fs/ext4/inode.c
+@@ -4220,7 +4220,7 @@ static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
+       if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
+               return inode_peek_iversion_raw(inode);
+       else
+-              return inode_peek_iversion(inode);
++              return EXT4_I(inode)->i_fs_version;
+ }
+ static int ext4_inode_blocks_set(struct ext4_inode *raw_inode,
+@@ -4658,7 +4658,7 @@ static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
+       if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
+               inode_set_iversion_raw(inode, val);
+       else
+-              inode_set_iversion_queried(inode, val);
++              EXT4_I(inode)->i_fs_version = val;
+ }
+ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
+-- 
+2.27.0
+
diff --git a/ldiskfs/kernel_patches/patches/linux-5.16/ext4-misc.patch b/ldiskfs/kernel_patches/patches/linux-5.16/ext4-misc.patch
new file mode 100644 (file)
index 0000000..dfb353b
--- /dev/null
@@ -0,0 +1,215 @@
+diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
+index d7332df..d04d87c 100644
+--- a/fs/ext4/ext4.h
++++ b/fs/ext4/ext4.h
+@@ -1956,6 +1956,8 @@ static inline bool ext4_verity_in_progress(struct inode *inode)
+ #define NEXT_ORPHAN(inode) EXT4_I(inode)->i_dtime
++#define JOURNAL_START_HAS_3ARGS       1
++
+ /*
+  * Codes for operating systems
+  */
+@@ -2202,7 +2204,21 @@ static inline bool ext4_has_unknown_ext##ver##_incompat_features(struct super_bl
+ EXTN_FEATURE_FUNCS(2)
+ EXTN_FEATURE_FUNCS(3)
+-EXTN_FEATURE_FUNCS(4)
++static inline bool ext4_has_unknown_ext4_compat_features(struct super_block *sb)
++{
++      return ((EXT4_SB(sb)->s_es->s_feature_compat &
++              cpu_to_le32(~EXT4_FEATURE_COMPAT_SUPP)) != 0);
++}
++static inline bool ext4_has_unknown_ext4_ro_compat_features(struct super_block *sb)
++{
++      return ((EXT4_SB(sb)->s_es->s_feature_ro_compat &
++              cpu_to_le32(~EXT4_FEATURE_RO_COMPAT_SUPP)) != 0);
++}
++static inline bool ext4_has_unknown_ext4_incompat_features(struct super_block *sb)
++{
++      return ((EXT4_SB(sb)->s_es->s_feature_incompat &
++              cpu_to_le32(~EXT4_FEATURE_INCOMPAT_SUPP)) != 0);
++}
+ static inline bool ext4_has_compat_features(struct super_block *sb)
+ {
+@@ -3724,6 +3740,13 @@ struct ext4_extent;
+ #define EXT_MAX_BLOCKS        0xffffffff
+ extern void ext4_ext_tree_init(handle_t *handle, struct inode *inode);
++extern struct buffer_head *ext4_read_inode_bitmap(struct super_block *sb,
++                                                ext4_group_t block_group);
++extern void ext4_inc_count(struct inode *inode);
++extern void ext4_dec_count(struct inode *inode);
++extern struct buffer_head *ext4_append(handle_t *handle,
++                                     struct inode *inode,
++                                     ext4_lblk_t *block);
+ extern int ext4_ext_index_trans_blocks(struct inode *inode, int extents);
+ extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
+                              struct ext4_map_blocks *map, int flags);
+diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
+index 5d0a11d..4840190 100644
+--- a/fs/ext4/ialloc.c
++++ b/fs/ext4/ialloc.c
+@@ -120,7 +120,7 @@ verified:
+  *
+  * Return buffer_head of bitmap on success, or an ERR_PTR on error.
+  */
+-static struct buffer_head *
++struct buffer_head *
+ ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
+ {
+       struct ext4_group_desc *desc;
+@@ -215,6 +215,7 @@ out:
+       put_bh(bh);
+       return ERR_PTR(err);
+ }
++EXPORT_SYMBOL(ext4_read_inode_bitmap);
+ /*
+  * NOTE! When we get the inode, we're the only people
+diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
+index f0206e2..88e82d6 100644
+--- a/fs/ext4/inode.c
++++ b/fs/ext4/inode.c
+@@ -6145,3 +6145,19 @@ out_error:
+       ext4_journal_stop(handle);
+       goto out;
+ }
++EXPORT_SYMBOL(ext4_map_blocks);
++EXPORT_SYMBOL(ext4_truncate);
++EXPORT_SYMBOL(ext4_iget);
++EXPORT_SYMBOL(ext4_bread);
++EXPORT_SYMBOL(ext4_itable_unused_count);
++EXPORT_SYMBOL(ext4_force_commit);
++EXPORT_SYMBOL(__ext4_mark_inode_dirty);
++EXPORT_SYMBOL(ext4_get_group_desc);
++EXPORT_SYMBOL(__ext4_journal_get_write_access);
++EXPORT_SYMBOL(__ext4_journal_start_sb);
++EXPORT_SYMBOL(__ext4_journal_stop);
++EXPORT_SYMBOL(__ext4_handle_dirty_metadata);
++EXPORT_SYMBOL(__ext4_std_error);
++EXPORT_SYMBOL(ext4fs_dirhash);
++EXPORT_SYMBOL(ext4_get_inode_loc);
++EXPORT_SYMBOL(__ext4_journal_ensure_credits);
+diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
+index 249836e..627b9eb 100644
+--- a/fs/ext4/namei.c
++++ b/fs/ext4/namei.c
+@@ -50,7 +50,7 @@
+ #define NAMEI_RA_BLOCKS  4
+ #define NAMEI_RA_SIZE      (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
+-static struct buffer_head *ext4_append(handle_t *handle,
++struct buffer_head *ext4_append(handle_t *handle,
+                                       struct inode *inode,
+                                       ext4_lblk_t *block)
+ {
+@@ -182,6 +182,7 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
+       }
+       return bh;
+ }
++EXPORT_SYMBOL(ext4_append);
+ #ifdef DX_DEBUG
+ #define dxtrace(command) command
+@@ -2703,23 +2704,25 @@ out:
+  * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set
+  * on regular files) and to avoid creating huge/slow non-HTREE directories.
+  */
+-static void ext4_inc_count(struct inode *inode)
++void ext4_inc_count(struct inode *inode)
+ {
+       inc_nlink(inode);
+       if (is_dx(inode) &&
+           (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2))
+               set_nlink(inode, 1);
+ }
++EXPORT_SYMBOL(ext4_inc_count);
+ /*
+  * If a directory had nlink == 1, then we should let it be 1. This indicates
+  * directory has >EXT4_LINK_MAX subdirs.
+  */
+-static void ext4_dec_count(struct inode *inode)
++void ext4_dec_count(struct inode *inode)
+ {
+       if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
+               drop_nlink(inode);
+ }
++EXPORT_SYMBOL(ext4_dec_count);
+ /*
+diff --git a/fs/ext4/super.c b/fs/ext4/super.c
+index 24a7ad8..b8a9fce 100644
+--- a/fs/ext4/super.c
++++ b/fs/ext4/super.c
+@@ -5429,7 +5429,7 @@ static void ext4_update_super(struct super_block *sb)
+                       __ext4_update_tstamp(&es->s_first_error_time,
+                                            &es->s_first_error_time_hi,
+                                            sbi->s_first_error_time);
+-                      strncpy(es->s_first_error_func, sbi->s_first_error_func,
++                      strlcpy(es->s_first_error_func, sbi->s_first_error_func,
+                               sizeof(es->s_first_error_func));
+                       es->s_first_error_line =
+                               cpu_to_le32(sbi->s_first_error_line);
+@@ -5443,7 +5443,7 @@ static void ext4_update_super(struct super_block *sb)
+               __ext4_update_tstamp(&es->s_last_error_time,
+                                    &es->s_last_error_time_hi,
+                                    sbi->s_last_error_time);
+-              strncpy(es->s_last_error_func, sbi->s_last_error_func,
++              strlcpy(es->s_last_error_func, sbi->s_last_error_func,
+                       sizeof(es->s_last_error_func));
+               es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
+               es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
+@@ -6652,16 +6652,12 @@ static int __init ext4_init_fs(void)
+       if (err)
+               goto out05;
+-      register_as_ext3();
+-      register_as_ext2();
+       err = register_filesystem(&ext4_fs_type);
+       if (err)
+               goto out;
+       return 0;
+ out:
+-      unregister_as_ext2();
+-      unregister_as_ext3();
+       ext4_fc_destroy_dentry_cache();
+ out05:
+       destroy_inodecache();
+@@ -6686,8 +6682,6 @@ out7:
+ static void __exit ext4_exit_fs(void)
+ {
+       ext4_destroy_lazyinit_thread();
+-      unregister_as_ext2();
+-      unregister_as_ext3();
+       unregister_filesystem(&ext4_fs_type);
+       ext4_fc_destroy_dentry_cache();
+       destroy_inodecache();
+diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
+index 1e0fc1e..379c59c 100644
+--- a/fs/ext4/xattr.c
++++ b/fs/ext4/xattr.c
+@@ -656,6 +656,7 @@ ext4_xattr_get(struct inode *inode, int name_index, const char *name,
+       up_read(&EXT4_I(inode)->xattr_sem);
+       return error;
+ }
++EXPORT_SYMBOL(ext4_xattr_get);
+ static int
+ ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
+@@ -2416,6 +2417,7 @@ cleanup:
+       ext4_write_unlock_xattr(inode, &no_expand);
+       return error;
+ }
++EXPORT_SYMBOL(ext4_xattr_set_handle);
+ int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
+                          bool is_create, int *credits)
+-- 
+2.27.0
+
diff --git a/ldiskfs/kernel_patches/patches/linux-5.16/ext4-osd-iop-common.patch b/ldiskfs/kernel_patches/patches/linux-5.16/ext4-osd-iop-common.patch
new file mode 100644 (file)
index 0000000..5ef0889
--- /dev/null
@@ -0,0 +1,72 @@
+diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
+index d7332df..72c4d1a 100644
+--- a/fs/ext4/ext4.h
++++ b/fs/ext4/ext4.h
+@@ -3084,6 +3084,12 @@ extern int ext4_dirblock_csum_verify(struct inode *inode,
+                                    struct buffer_head *bh);
+ extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
+                               __u32 start_minor_hash, __u32 *next_hash);
++extern struct inode *ext4_create_inode(handle_t *handle,
++                                     struct inode *dir, int mode,
++                                     uid_t *owner);
++extern int ext4_delete_entry(handle_t *handle, struct inode * dir,
++                           struct ext4_dir_entry_2 *de_del,
++                           struct buffer_head *bh);
+ extern int ext4_search_dir(struct buffer_head *bh,
+                          char *search_buf,
+                          int buf_size,
+diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
+index 249836e..91467a1 100644
+--- a/fs/ext4/namei.c
++++ b/fs/ext4/namei.c
+@@ -2651,7 +2651,7 @@ int ext4_generic_delete_entry(struct inode *dir,
+       return -ENOENT;
+ }
+-static int ext4_delete_entry(handle_t *handle,
++int ext4_delete_entry(handle_t *handle,
+                            struct inode *dir,
+                            struct ext4_dir_entry_2 *de_del,
+                            struct buffer_head *bh)
+@@ -2691,7 +2691,7 @@ out:
+               ext4_std_error(dir->i_sb, err);
+       return err;
+ }
+-
++EXPORT_SYMBOL(ext4_delete_entry);
+ /*
+  * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2
+  * since this indicates that nlinks count was previously 1 to avoid overflowing
+@@ -2748,6 +2748,29 @@ static int ext4_add_nondir(handle_t *handle,
+       return err;
+ }
++ /* Return locked inode, then the caller can modify the inode's states/flags
++  * before others finding it. The caller should unlock the inode by itself. */
++struct inode *ext4_create_inode(handle_t *handle, struct inode *dir, int mode,
++                              uid_t *owner)
++{
++      struct inode *inode;
++
++      inode = ext4_new_inode(handle, dir, mode, NULL, 0, owner, 0);
++      if (!IS_ERR(inode)) {
++              if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode)) {
++#ifdef CONFIG_LDISKFS_FS_XATTR
++                      inode->i_op = &ext4_special_inode_operations;
++#endif
++              } else {
++                      inode->i_op = &ext4_file_inode_operations;
++                      inode->i_fop = &ext4_file_operations;
++                      ext4_set_aops(inode);
++              }
++      }
++      return inode;
++}
++EXPORT_SYMBOL(ext4_create_inode);
++
+ /*
+  * By the time this is called, we already have created
+  * the directory cache entry for the new file, but it
+-- 
+2.27.0
+
diff --git a/ldiskfs/kernel_patches/patches/rhel9/ext4-data-in-dirent.patch b/ldiskfs/kernel_patches/patches/rhel9/ext4-data-in-dirent.patch
new file mode 100644 (file)
index 0000000..11236a5
--- /dev/null
@@ -0,0 +1,881 @@
+this patch implements feature which allows ext4 fs users (e.g. Lustre)
+to store data in ext4 dirent.
+data is stored in ext4 dirent after file-name, this space is accounted
+in de->rec_len. flag EXT4_DIRENT_LUFID added to d_type if extra data
+is present.
+
+make use of dentry->d_fsdata to pass fid to ext4. so no
+changes in ext4_add_entry() interface required.
+
+---
+ fs/ext4/dir.c         |   9 +-
+ fs/ext4/ext4.h        | 107 ++++++++++++++++--
+ fs/ext4/fast_commit.c |   2 +-
+ fs/ext4/inline.c      |   8 +-
+ fs/ext4/namei.c       | 250 ++++++++++++++++++++++++++++++++----------
+ fs/ext4/super.c       |   4 +-
+ 6 files changed, 305 insertions(+), 75 deletions(-)
+
+diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
+index a6bb86f..edd0540 100644
+--- a/fs/ext4/dir.c
++++ b/fs/ext4/dir.c
+@@ -465,12 +465,17 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
+       struct fname *fname, *new_fn;
+       struct dir_private_info *info;
+       int len;
++      int extra_data = 0;
+       info = dir_file->private_data;
+       p = &info->root.rb_node;
+       /* Create and allocate the fname structure */
+-      len = sizeof(struct fname) + ent_name->len + 1;
++      if (dirent->file_type & EXT4_DIRENT_LUFID)
++              extra_data = ext4_get_dirent_data_len(dirent);
++
++      len = sizeof(struct fname) + ent_name->len + extra_data + 1;
++
+       new_fn = kzalloc(len, GFP_KERNEL);
+       if (!new_fn)
+               return -ENOMEM;
+@@ -479,7 +484,7 @@ int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
+       new_fn->inode = le32_to_cpu(dirent->inode);
+       new_fn->name_len = ent_name->len;
+       new_fn->file_type = dirent->file_type;
+-      memcpy(new_fn->name, ent_name->name, ent_name->len);
++      memcpy(new_fn->name, ent_name->name, ent_name->len + extra_data);
+       while (*p) {
+               parent = *p;
+diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
+index 58645be..8c6864c 100644
+--- a/fs/ext4/ext4.h
++++ b/fs/ext4/ext4.h
+@@ -1171,6 +1171,7 @@ struct ext4_inode_info {
+       __u32 i_csum_seed;
+       kprojid_t i_projid;
++      void *i_dirdata;
+ };
+ /*
+@@ -1192,6 +1193,7 @@ struct ext4_inode_info {
+  * Mount flags set via mount options or defaults
+  */
+ #define EXT4_MOUNT_NO_MBCACHE         0x00001 /* Do not use mbcache */
++#define EXT4_MOUNT_DIRDATA            0x00002 /* Data in directory entries */
+ #define EXT4_MOUNT_GRPID              0x00004 /* Create files with directory's group */
+ #define EXT4_MOUNT_DEBUG              0x00008 /* Some debugging messages */
+ #define EXT4_MOUNT_ERRORS_CONT                0x00010 /* Continue on errors */
+@@ -2164,6 +2166,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(casefold,            CASEFOLD)
+                                        EXT4_FEATURE_INCOMPAT_FLEX_BG| \
+                                        EXT4_FEATURE_INCOMPAT_EA_INODE| \
+                                        EXT4_FEATURE_INCOMPAT_MMP | \
++                                       EXT4_FEATURE_INCOMPAT_DIRDATA| \
+                                        EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
+                                        EXT4_FEATURE_INCOMPAT_ENCRYPT | \
+                                        EXT4_FEATURE_INCOMPAT_CASEFOLD | \
+@@ -2371,6 +2374,42 @@ struct ext4_dir_entry_tail {
+ #define EXT4_FT_SYMLINK               7
+ #define EXT4_FT_MAX           8
++#define EXT4_FT_MASK          0xf
++
++#if EXT4_FT_MAX > EXT4_FT_MASK
++#error "conflicting EXT4_FT_MAX and EXT4_FT_MASK"
++#endif
++
++/*
++ * d_type has 4 unused bits, so it can hold four types data. these different
++ * type of data (e.g. lustre data, high 32 bits of 64-bit inode number) can be
++ * stored, in flag order, after file-name in ext4 dirent.
++*/
++/*
++ * this flag is added to d_type if ext4 dirent has extra data after
++ * filename. this data length is variable and length is stored in first byte
++ * of data. data start after filename NUL byte.
++ * This is used by Lustre FS.
++  */
++#define EXT4_DIRENT_LUFID             0x10
++
++#define EXT4_LUFID_MAGIC    0xAD200907UL
++struct ext4_dentry_param {
++      __u32  edp_magic;       /* EXT4_LUFID_MAGIC */
++      char   edp_len;         /* size of edp_data in bytes */
++      char   edp_data[0];     /* packed array of data */
++} __packed;
++
++static inline unsigned char *ext4_dentry_get_data(struct super_block *sb,
++                                                struct ext4_dentry_param *p)
++{
++      if (!ext4_has_feature_dirdata(sb))
++              return NULL;
++      if (p && p->edp_magic == EXT4_LUFID_MAGIC)
++              return &p->edp_len;
++      else
++              return NULL;
++}
+ #define EXT4_FT_DIR_CSUM      0xDE
+@@ -2382,6 +2421,17 @@ struct ext4_dir_entry_tail {
+ #define EXT4_DIR_PAD                  4
+ #define EXT4_DIR_ROUND                        (EXT4_DIR_PAD - 1)
+ #define EXT4_MAX_REC_LEN              ((1<<16)-1)
++#define EXT4_DIR_REC_LEN_(name_len, i_dir) \
++      ext4_dir_rec_len((name_len), (i_dir))
++#define EXT4_DIR_ENTRY_LEN_(de, i_dir) \
++      (EXT4_DIR_REC_LEN_((de)->name_len + ext4_get_dirent_data_len(de), \
++              (i_dir)))
++/* ldiskfs */
++#define EXT4_DIR_REC_LEN(name_len, i_dir)     EXT4_DIR_REC_LEN_((name_len), (i_dir))
++#define EXT4_DIR_ENTRY_LEN(de, i_dir)         EXT4_DIR_ENTRY_LEN_((de), (i_dir))
++/* lustre osd_handler compat -- ifdef LDISKFS_DIR_REC_LEN_WITH_DIR */
++#define EXT4_DIR_REC_LEN_WITH_DIR             1
++#define __EXT4_DIR_REC_LEN(name_len)          EXT4_DIR_REC_LEN_((name_len), NULL)
+ /*
+  * The rec_len is dependent on the type of directory. Directories that are
+@@ -2389,10 +2439,10 @@ struct ext4_dir_entry_tail {
+  * ext4_extended_dir_entry_2. For all entries related to '.' or '..' you should
+  * pass NULL for dir, as those entries do not use the extra fields.
+  */
+-static inline unsigned int ext4_dir_rec_len(__u8 name_len,
++static inline unsigned int ext4_dir_rec_len(__u32 name_len,
+                                               const struct inode *dir)
+ {
+-      int rec_len = (name_len + 8 + EXT4_DIR_ROUND);
++      __u32 rec_len = (name_len + 8 + EXT4_DIR_ROUND);
+       if (dir && ext4_hash_in_dirent(dir))
+               rec_len += sizeof(struct ext4_dir_entry_hash);
+@@ -2865,11 +2915,13 @@ extern int ext4_find_dest_de(struct inode *dir, struct inode *inode,
+                            struct buffer_head *bh,
+                            void *buf, int buf_size,
+                            struct ext4_filename *fname,
+-                           struct ext4_dir_entry_2 **dest_de);
++                           struct ext4_dir_entry_2 **dest_de,
++                           int *dlen);
+ void ext4_insert_dentry(struct inode *dir, struct inode *inode,
+                       struct ext4_dir_entry_2 *de,
+                       int buf_size,
+-                      struct ext4_filename *fname);
++                      struct ext4_filename *fname,
++                      void *data);
+ static inline void ext4_update_dx_flag(struct inode *inode)
+ {
+       if (!ext4_has_feature_dir_index(inode->i_sb) &&
+@@ -2885,10 +2937,17 @@ static const unsigned char ext4_filetype_table[] = {
+ static inline  unsigned char get_dtype(struct super_block *sb, int filetype)
+ {
+-      if (!ext4_has_feature_filetype(sb) || filetype >= EXT4_FT_MAX)
++      int fl_index = filetype & EXT4_FT_MASK;
++
++      if (!ext4_has_feature_filetype(sb) || fl_index >= EXT4_FT_MAX)
+               return DT_UNKNOWN;
+-      return ext4_filetype_table[filetype];
++      if (!test_opt(sb, DIRDATA))
++              return ext4_filetype_table[fl_index];
++
++      return (ext4_filetype_table[fl_index]) |
++              (filetype & EXT4_DIRENT_LUFID);
++
+ }
+ extern int ext4_check_all_de(struct inode *dir, struct buffer_head *bh,
+                            void *buf, int buf_size);
+@@ -3087,9 +3146,13 @@ extern int ext4_ind_migrate(struct inode *inode);
+ /* namei.c */
+ extern int ext4_init_new_dir(handle_t *handle, struct inode *dir,
+-                           struct inode *inode);
++                           struct inode *inode,
++                           const void *data1, const void *data2);
+ extern int ext4_dirblock_csum_verify(struct inode *inode,
+                                    struct buffer_head *bh);
++extern int ext4_add_dot_dotdot(handle_t *handle, struct inode *dir,
++                             struct inode *inode,
++                             const void *data1, const void *data2);
+ extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
+                               __u32 start_minor_hash, __u32 *next_hash);
+ extern struct inode *ext4_create_inode(handle_t *handle,
+@@ -3915,6 +3978,36 @@ static inline int ext4_buffer_uptodate(struct buffer_head *bh)
+       return buffer_uptodate(bh);
+ }
++/*
++ * Compute the total directory entry data length.
++ * This includes the filename and an implicit NUL terminator (always present),
++ * and optional extensions.  Each extension has a bit set in the high 4 bits of
++ * de->file_type, and the extension length is the first byte in each entry.
++ */
++static inline int ext4_get_dirent_data_len(struct ext4_dir_entry_2 *de)
++{
++      char *len = de->name + de->name_len + 1 /* NUL terminator */;
++      int dlen = 0;
++      __u8 extra_data_flags = (de->file_type & ~EXT4_FT_MASK) >> 4;
++      struct ext4_dir_entry_tail *t = (struct ext4_dir_entry_tail *)de;
++
++      if (!t->det_reserved_zero1 &&
++          le16_to_cpu(t->det_rec_len) ==
++              sizeof(struct ext4_dir_entry_tail) &&
++          !t->det_reserved_zero2 &&
++          t->det_reserved_ft == EXT4_FT_DIR_CSUM)
++              return 0;
++
++      while (extra_data_flags) {
++              if (extra_data_flags & 1) {
++                      dlen += *len + (dlen == 0);
++                      len += *len;
++              }
++              extra_data_flags >>= 1;
++      }
++      return dlen;
++}
++
+ #endif        /* __KERNEL__ */
+ #define EFSBADCRC     EBADMSG         /* Bad CRC detected */
+diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
+index 5ae8026..ecfa9b8 100644
+--- a/fs/ext4/fast_commit.c
++++ b/fs/ext4/fast_commit.c
+@@ -1544,7 +1544,7 @@ static int ext4_fc_replay_create(struct super_block *sb, struct ext4_fc_tl *tl,
+                       jbd_debug(1, "Dir %d not found.", darg.ino);
+                       goto out;
+               }
+-              ret = ext4_init_new_dir(NULL, dir, inode);
++              ret = ext4_init_new_dir(NULL, dir, inode, NULL, NULL);
+               iput(dir);
+               if (ret) {
+                       ret = 0;
+diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
+index 39a1ab1..46f2e1e 100644
+--- a/fs/ext4/inline.c
++++ b/fs/ext4/inline.c
+@@ -1036,7 +1036,7 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
+       struct ext4_dir_entry_2 *de;
+       err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
+-                              inline_size, fname, &de);
++                              inline_size, fname, &de, NULL);
+       if (err)
+               return err;
+@@ -1045,7 +1045,7 @@ static int ext4_add_dirent_to_inline(handle_t *handle,
+                                           EXT4_JTR_NONE);
+       if (err)
+               return err;
+-      ext4_insert_dentry(dir, inode, de, inline_size, fname);
++      ext4_insert_dentry(dir, inode, de, inline_size, fname, NULL);
+       ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
+@@ -1395,7 +1395,7 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
+                       fake.name_len = 1;
+                       strcpy(fake.name, ".");
+                       fake.rec_len = ext4_rec_len_to_disk(
+-                                        ext4_dir_rec_len(fake.name_len, NULL),
++                                        EXT4_DIR_ENTRY_LEN(&fake, NULL),
+                                         inline_size);
+                       ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
+                       de = &fake;
+@@ -1405,7 +1405,7 @@ int ext4_inlinedir_to_tree(struct file *dir_file,
+                       fake.name_len = 2;
+                       strcpy(fake.name, "..");
+                       fake.rec_len = ext4_rec_len_to_disk(
+-                                        ext4_dir_rec_len(fake.name_len, NULL),
++                                        EXT4_DIR_ENTRY_LEN(&fake, NULL),
+                                         inline_size);
+                       ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
+                       de = &fake;
+diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
+index 1f95773..9edb487 100644
+--- a/fs/ext4/namei.c
++++ b/fs/ext4/namei.c
+@@ -262,13 +262,14 @@ static unsigned dx_get_count(struct dx_entry *entries);
+ static unsigned dx_get_limit(struct dx_entry *entries);
+ static void dx_set_count(struct dx_entry *entries, unsigned value);
+ static void dx_set_limit(struct dx_entry *entries, unsigned value);
+-static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
++static inline unsigned dx_root_limit(struct inode *dir,
++              struct ext4_dir_entry_2 *dot_de, unsigned infosize);
+ static unsigned dx_node_limit(struct inode *dir);
+ static struct dx_frame *dx_probe(struct ext4_filename *fname,
+                                struct inode *dir,
+                                struct dx_hash_info *hinfo,
+                                struct dx_frame *frame);
+-static void dx_release(struct dx_frame *frames);
++static void dx_release(struct dx_frame *frames, struct inode *dir);
+ static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
+                      unsigned blocksize, struct dx_hash_info *hinfo,
+                      struct dx_map_entry map[]);
+@@ -408,22 +409,23 @@ static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
+ {
+       struct ext4_dir_entry *dp;
+       struct dx_root_info *root;
+-      int count_offset;
++      int count_offset, dot_rec_len, dotdot_rec_len;
+       if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
+               count_offset = 8;
+-      else if (le16_to_cpu(dirent->rec_len) == 12) {
+-              dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
++      else {
++              dot_rec_len = le16_to_cpu(dirent->rec_len);
++              dp = (struct ext4_dir_entry *)(((void *)dirent) + dot_rec_len);
+               if (le16_to_cpu(dp->rec_len) !=
+-                  EXT4_BLOCK_SIZE(inode->i_sb) - 12)
++                  EXT4_BLOCK_SIZE(inode->i_sb) - dot_rec_len)
+                       return NULL;
+-              root = (struct dx_root_info *)(((void *)dp + 12));
++              dotdot_rec_len = EXT4_DIR_ENTRY_LEN((struct ext4_dir_entry_2 *)dp, NULL);
++              root = (struct dx_root_info *)(((void *)dp + dotdot_rec_len));
+               if (root->reserved_zero ||
+                   root->info_length != sizeof(struct dx_root_info))
+                       return NULL;
+-              count_offset = 32;
+-      } else
+-              return NULL;
++              count_offset = 8 + dot_rec_len + dotdot_rec_len;
++      }
+       if (offset)
+               *offset = count_offset;
+@@ -526,13 +528,14 @@ ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
+  * Future: use high four bits of block for coalesce-on-delete flags
+  * Mask them off for now.
+  */
+-struct dx_root_info *dx_get_dx_info(struct ext4_dir_entry_2 *de)
++struct dx_root_info *dx_get_dx_info(struct ext4_dir_entry_2 *de, struct inode *i_dir)
+ {
++      BUG_ON(de->name_len != 1);
+       /* get dotdot first */
+-      de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_REC_LEN(1));
++      de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_ENTRY_LEN(de, i_dir));
+       /* dx root info is after dotdot entry */
+-      de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_REC_LEN(2));
++      de = (struct ext4_dir_entry_2 *)((char *)de + EXT4_DIR_ENTRY_LEN(de, i_dir));
+       return (struct dx_root_info *)de;
+ }
+@@ -577,11 +580,16 @@ static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
+       ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
+ }
+-static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
++static inline unsigned dx_root_limit(struct inode *dir,
++              struct ext4_dir_entry_2 *dot_de, unsigned infosize)
+ {
+-      unsigned int entry_space = dir->i_sb->s_blocksize -
+-                      ext4_dir_rec_len(1, NULL) -
+-                      ext4_dir_rec_len(2, NULL) - infosize;
++      struct ext4_dir_entry_2 *dotdot_de;
++      unsigned entry_space;
++
++      BUG_ON(dot_de->name_len != 1);
++      dotdot_de = ext4_next_entry(dot_de, dir->i_sb->s_blocksize);
++      entry_space = dir->i_sb->s_blocksize - EXT4_DIR_ENTRY_LEN(dot_de, NULL) -
++                       EXT4_DIR_ENTRY_LEN(dotdot_de, NULL) - infosize;
+       if (ext4_has_metadata_csum(dir->i_sb))
+               entry_space -= sizeof(struct dx_tail);
+@@ -699,7 +707,7 @@ static struct stats dx_show_leaf(struct inode *dir,
+                                      (unsigned) ((char *) de - base));
+ #endif
+                       }
+-                      space += ext4_dir_rec_len(de->name_len, dir);
++                      space += EXT4_DIR_ENTRY_LEN(de, dir);
+                       names++;
+               }
+               de = ext4_next_entry(de, size);
+@@ -791,7 +799,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
+       if (IS_ERR(frame->bh))
+               return (struct dx_frame *) frame->bh;
+-      info = dx_get_dx_info((struct ext4_dir_entry_2 *)frame->bh->b_data);
++      info = dx_get_dx_info((struct ext4_dir_entry_2 *)frame->bh->b_data, dir);
+       if (info->hash_version != DX_HASH_TEA &&
+           info->hash_version != DX_HASH_HALF_MD4 &&
+           info->hash_version != DX_HASH_LEGACY &&
+@@ -847,11 +855,14 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
+       entries = (struct dx_entry *)(((char *)info) + info->info_length);
+-      if (dx_get_limit(entries) != dx_root_limit(dir,
+-                                                 info->info_length)) {
++      if (dx_get_limit(entries) !=
++          dx_root_limit(dir, (struct ext4_dir_entry_2 *)frame->bh->b_data,
++                        info->info_length)) {
+               ext4_warning_inode(dir, "dx entry: limit %u != root limit %u",
+                                  dx_get_limit(entries),
+-                                 dx_root_limit(dir, info->info_length));
++                                 dx_root_limit(dir,
++                                        (struct ext4_dir_entry_2 *)frame->bh->b_data,
++                                        info->info_length));
+               goto fail;
+       }
+@@ -914,7 +925,7 @@ fail:
+       return ret_err;
+ }
+-static void dx_release(struct dx_frame *frames)
++static void dx_release(struct dx_frame *frames, struct inode *dir)
+ {
+       struct dx_root_info *info;
+       int i;
+@@ -923,7 +934,7 @@ static void dx_release(struct dx_frame *frames)
+       if (frames[0].bh == NULL)
+               return;
+-      info = dx_get_dx_info((struct ext4_dir_entry_2 *)frames[0].bh->b_data);
++      info = dx_get_dx_info((struct ext4_dir_entry_2 *)frames[0].bh->b_data, dir);
+       /* save local copy, "info" may be freed after brelse() */
+       indirect_levels = info->indirect_levels;
+       for (i = 0; i <= indirect_levels; i++) {
+@@ -1224,12 +1235,12 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
+                   (count && ((hashval & 1) == 0)))
+                       break;
+       }
+-      dx_release(frames);
++      dx_release(frames, dir);
+       dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
+                      "next hash: %x\n", count, *next_hash));
+       return count;
+ errout:
+-      dx_release(frames);
++      dx_release(frames, dir);
+       return (err);
+ }
+@@ -1755,7 +1766,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
+ errout:
+       dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name));
+ success:
+-      dx_release(frames);
++      dx_release(frames, dir);
+       return bh;
+ }
+@@ -1879,7 +1890,7 @@ dx_move_dirents(struct inode *dir, char *from, char *to,
+       while (count--) {
+               struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
+                                               (from + (map->offs<<2));
+-              rec_len = ext4_dir_rec_len(de->name_len, dir);
++              rec_len = EXT4_DIR_ENTRY_LEN(de, dir);
+               memcpy (to, de, rec_len);
+               ((struct ext4_dir_entry_2 *) to)->rec_len =
+@@ -1912,7 +1923,7 @@ static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
+       while ((char*)de < base + blocksize) {
+               next = ext4_next_entry(de, blocksize);
+               if (de->inode && de->name_len) {
+-                      rec_len = ext4_dir_rec_len(de->name_len, dir);
++                      rec_len = EXT4_DIR_ENTRY_LEN(de, dir);
+                       if (de > to)
+                               memmove(to, de, rec_len);
+                       to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
+@@ -2051,14 +2062,21 @@ int ext4_find_dest_de(struct inode *dir, struct inode *inode,
+                     struct buffer_head *bh,
+                     void *buf, int buf_size,
+                     struct ext4_filename *fname,
+-                    struct ext4_dir_entry_2 **dest_de)
++                    struct ext4_dir_entry_2 **dest_de,
++                    int *dlen)
+ {
+       struct ext4_dir_entry_2 *de;
+-      unsigned short reclen = ext4_dir_rec_len(fname_len(fname), dir);
++      unsigned short reclen;
+       int nlen, rlen;
+       unsigned int offset = 0;
+       char *top;
++      if (dlen) {
++              reclen = ext4_dir_rec_len(fname_len(fname) + *dlen, dir);
++              *dlen = 0;
++      } else {
++              reclen = ext4_dir_rec_len(fname_len(fname), dir);
++      }
+       de = (struct ext4_dir_entry_2 *)buf;
+       top = buf + buf_size - reclen;
+       while ((char *) de <= top) {
+@@ -2067,10 +2085,31 @@ int ext4_find_dest_de(struct inode *dir, struct inode *inode,
+                       return -EFSCORRUPTED;
+               if (ext4_match(dir, fname, de))
+                       return -EEXIST;
+-              nlen = ext4_dir_rec_len(de->name_len, dir);
++              nlen = EXT4_DIR_ENTRY_LEN(de, dir);
+               rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
+               if ((de->inode ? rlen - nlen : rlen) >= reclen)
+                       break;
++
++              /* Then for dotdot entries, check for the smaller space
++               * required for just the entry, no FID
++               */
++              if (fname_len(fname) == 2 && memcmp(fname_name(fname), "..", 2) == 0) {
++                      if ((de->inode ? rlen - nlen : rlen) >=
++                          ext4_dir_rec_len(fname_len(fname), dir)) {
++                              /* set dlen = 1 to indicate not
++                               * enough space store fid
++                               */
++                              if (dlen)
++                                      *dlen = 1;
++                              break;
++                      }
++                      /* The new ".." entry must be written over the
++                       * previous ".." entry, which is the first
++                       * entry traversed by this scan. If it doesn't
++                       * fit, something is badly wrong, so -EIO.
++                       */
++                      return -EIO;
++              }
+               de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
+               offset += rlen;
+       }
+@@ -2085,12 +2124,13 @@ void ext4_insert_dentry(struct inode *dir,
+                       struct inode *inode,
+                       struct ext4_dir_entry_2 *de,
+                       int buf_size,
+-                      struct ext4_filename *fname)
++                      struct ext4_filename *fname,
++                      void *data)
+ {
+       int nlen, rlen;
+-      nlen = ext4_dir_rec_len(de->name_len, dir);
++      nlen = EXT4_DIR_ENTRY_LEN(de, dir);
+       rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
+       if (de->inode) {
+               struct ext4_dir_entry_2 *de1 =
+@@ -2111,6 +2151,12 @@ void ext4_insert_dentry(struct inode *dir,
+               EXT4_DIRENT_HASHES(de)->minor_hash =
+                                               cpu_to_le32(hinfo->minor_hash);
+       }
++      if (data) {
++              de->name[fname_len(fname)] = 0;
++              memcpy(&de->name[fname_len(fname) + 1], data, *(char *)data);
++              de->file_type |= EXT4_DIRENT_LUFID;
++      }
++
+ }
+ /*
+@@ -2128,14 +2174,19 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
+ {
+       unsigned int    blocksize = dir->i_sb->s_blocksize;
+       int             csum_size = 0;
+-      int             err, err2;
++      int             err, err2, dlen = 0;
++      unsigned char   *data;
++      data = ext4_dentry_get_data(inode->i_sb, (struct ext4_dentry_param *)
++                                              EXT4_I(inode)->i_dirdata);
+       if (ext4_has_metadata_csum(inode->i_sb))
+               csum_size = sizeof(struct ext4_dir_entry_tail);
+       if (!de) {
++              if (data)
++                      dlen = (*data) + 1;
+               err = ext4_find_dest_de(dir, inode, bh, bh->b_data,
+-                                      blocksize - csum_size, fname, &de);
++                                      blocksize - csum_size, fname, &de, &dlen);
+               if (err)
+                       return err;
+       }
+@@ -2148,7 +2199,10 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
+       }
+       /* By now the buffer is marked for journaling */
+-      ext4_insert_dentry(dir, inode, de, blocksize, fname);
++      /* If writing the short form of "dotdot", don't add the data section */
++      if (dlen == 1)
++              data = NULL;
++      ext4_insert_dentry(dir, inode, de, blocksize, fname, data);
+       /*
+        * XXX shouldn't update any times until successful
+@@ -2246,7 +2300,7 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
+                                    blocksize);
+       /* initialize hashing info */
+-      dx_info = dx_get_dx_info(dot_de);
++      dx_info = dx_get_dx_info(dot_de, dir);
+       memset(dx_info, 0, sizeof(*dx_info));
+       dx_info->info_length = sizeof(*dx_info);
+       if (ext4_hash_in_dirent(dir))
+@@ -2257,7 +2311,8 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
+       entries = (void *)dx_info + sizeof(*dx_info);
+       dx_set_block(entries, 1);
+       dx_set_count(entries, 1);
+-      dx_set_limit(entries, dx_root_limit(dir, sizeof(*dx_info)));
++      dx_set_limit(entries, dx_root_limit(dir,
++                                       dot_de, sizeof(*dx_info)));
+       /* Initialize as for dx_probe */
+       fname->hinfo.hash_version = dx_info->hash_version;
+@@ -2298,7 +2353,7 @@ out_frames:
+        */
+       if (retval)
+               ext4_mark_inode_dirty(handle, dir);
+-      dx_release(frames);
++      dx_release(frames, dir);
+       brelse(bh2);
+       return retval;
+ }
+@@ -2311,6 +2366,8 @@ static int ext4_update_dotdot(handle_t *handle, struct dentry *dentry,
+       struct buffer_head *dir_block;
+       struct ext4_dir_entry_2 *de;
+       int len, journal = 0, err = 0;
++      int dlen = 0;
++      char *data;
+       if (IS_ERR(handle))
+               return PTR_ERR(handle);
+@@ -2326,21 +2383,26 @@ static int ext4_update_dotdot(handle_t *handle, struct dentry *dentry,
+       de = (struct ext4_dir_entry_2 *)dir_block->b_data;
+       /* the first item must be "." */
+-      assert(de->name_len == 1 && de->name[0] == '.');
++      ASSERT(de->name_len == 1 && de->name[0] == '.');
+       len = le16_to_cpu(de->rec_len);
+-      assert(len >= EXT4_DIR_REC_LEN(1));
+-      if (len > EXT4_DIR_REC_LEN(1)) {
++      ASSERT(len >= EXT4_DIR_REC_LEN(1, dir));
++      if (len > EXT4_DIR_REC_LEN(1, dir)) {
+               BUFFER_TRACE(dir_block, "get_write_access");
+               err = ext4_journal_get_write_access(handle, dir->i_sb, dir_block, EXT4_JTR_NONE);
+               if (err)
+                       goto out_journal;
+               journal = 1;
+-              de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(1));
++              de->rec_len = cpu_to_le16(EXT4_DIR_ENTRY_LEN(de, dir));
+       }
+-      len -= EXT4_DIR_REC_LEN(1);
+-      assert(len == 0 || len >= EXT4_DIR_REC_LEN(2));
++      len -= EXT4_DIR_ENTRY_LEN(de, NULL);
++      data = ext4_dentry_get_data(dir->i_sb,
++                      (struct ext4_dentry_param *)dentry->d_fsdata);
++      if (data)
++              dlen = *data + 1;
++      ASSERT(len == 0 || len >= EXT4_DIR_REC_LEN(2 + dlen, dir));
++
+       de = (struct ext4_dir_entry_2 *)
+                       ((char *) de + le16_to_cpu(de->rec_len));
+       if (!journal) {
+@@ -2354,10 +2416,15 @@ static int ext4_update_dotdot(handle_t *handle, struct dentry *dentry,
+       if (len > 0)
+               de->rec_len = cpu_to_le16(len);
+       else
+-              assert(le16_to_cpu(de->rec_len) >= EXT4_DIR_REC_LEN(2));
++              ASSERT(le16_to_cpu(de->rec_len) >= EXT4_DIR_REC_LEN(2, dir));
+       de->name_len = 2;
+       strcpy(de->name, "..");
+-      ext4_set_de_type(dir->i_sb, de, S_IFDIR);
++      if (data != NULL && ext4_get_dirent_data_len(de) >= dlen) {
++              de->name[2] = 0;
++              memcpy(&de->name[2 + 1], data, *data);
++              ext4_set_de_type(dir->i_sb, de, S_IFDIR);
++              de->file_type |= EXT4_DIRENT_LUFID;
++      }
+ out_journal:
+       if (journal) {
+@@ -2395,6 +2462,7 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
+       ext4_lblk_t block, blocks;
+       int     csum_size = 0;
++      EXT4_I(inode)->i_dirdata = dentry->d_fsdata;
+       if (ext4_has_metadata_csum(inode->i_sb))
+               csum_size = sizeof(struct ext4_dir_entry_tail);
+@@ -2637,7 +2705,7 @@ again:
+                       dx_set_count(entries, 1);
+                       dx_set_block(entries + 0, newblock);
+                       info = dx_get_dx_info((struct ext4_dir_entry_2 *)
+-                                            frames[0].bh->b_data);
++                                            frames[0].bh->b_data, dir);
+                       info->indirect_levels = 1;
+                       dxtrace(printk(KERN_DEBUG
+                                      "Creating %d level index...\n",
+@@ -2663,7 +2731,7 @@ journal_error:
+       ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
+ cleanup:
+       brelse(bh);
+-      dx_release(frames);
++      dx_release(frames, dir);
+       /* @restart is true means htree-path has been changed, we need to
+        * repeat dx_probe() to find out valid htree-path
+        */
+@@ -2966,38 +3034,73 @@ err_unlock_inode:
+       return err;
+ }
++struct tp_block {
++      struct inode *inode;
++      void *data1;
++      void *data2;
++};
++
+ struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
+                         struct ext4_dir_entry_2 *de,
+                         int blocksize, int csum_size,
+                         unsigned int parent_ino, int dotdot_real_len)
+ {
++      void *data1 = NULL, *data2 = NULL;
++      int dot_reclen = 0;
++
++      if (dotdot_real_len == 10) {
++              struct tp_block *tpb = (struct tp_block *)inode;
++              data1 = tpb->data1;
++              data2 = tpb->data2;
++              inode = tpb->inode;
++              dotdot_real_len = 0;
++      }
+       de->inode = cpu_to_le32(inode->i_ino);
+       de->name_len = 1;
+-      de->rec_len = ext4_rec_len_to_disk(ext4_dir_rec_len(de->name_len, NULL),
+-                                         blocksize);
+       strcpy(de->name, ".");
+       ext4_set_de_type(inode->i_sb, de, S_IFDIR);
++      /* get packed fid data*/
++      data1 = ext4_dentry_get_data(inode->i_sb,
++                              (struct ext4_dentry_param *) data1);
++      if (data1) {
++              de->name[1] = 0;
++              memcpy(&de->name[2], data1, *(char *) data1);
++              de->file_type |= EXT4_DIRENT_LUFID;
++      }
++      de->rec_len = cpu_to_le16(EXT4_DIR_ENTRY_LEN(de, NULL));
++
++      dot_reclen = cpu_to_le16(de->rec_len);
+       de = ext4_next_entry(de, blocksize);
+       de->inode = cpu_to_le32(parent_ino);
+       de->name_len = 2;
++
++      strcpy(de->name, "..");
++      ext4_set_de_type(inode->i_sb, de, S_IFDIR);
++      data2 = ext4_dentry_get_data(inode->i_sb,
++                      (struct ext4_dentry_param *) data2);
++      if (data2) {
++              de->name[2] = 0;
++              memcpy(&de->name[3], data2, *(char *) data2);
++              de->file_type |= EXT4_DIRENT_LUFID;
++      }
++
+       if (!dotdot_real_len)
+               de->rec_len = ext4_rec_len_to_disk(blocksize -
+-                                      (csum_size + ext4_dir_rec_len(1, NULL)),
+-                                      blocksize);
++                                      (csum_size + dot_reclen), blocksize);
+       else
+               de->rec_len = ext4_rec_len_to_disk(
+-                                      ext4_dir_rec_len(de->name_len, NULL),
++                                      EXT4_DIR_ENTRY_LEN(de, NULL),
+                                       blocksize);
+-      strcpy(de->name, "..");
+-      ext4_set_de_type(inode->i_sb, de, S_IFDIR);
+       return ext4_next_entry(de, blocksize);
+ }
+ int ext4_init_new_dir(handle_t *handle, struct inode *dir,
+-                           struct inode *inode)
++                           struct inode *inode,
++                           const void *data1, const void *data2)
+ {
++      struct tp_block param;
+       struct buffer_head *dir_block = NULL;
+       struct ext4_dir_entry_2 *de;
+       ext4_lblk_t block = 0;
+@@ -3021,7 +3124,11 @@ int ext4_init_new_dir(handle_t *handle, struct inode *dir,
+       if (IS_ERR(dir_block))
+               return PTR_ERR(dir_block);
+       de = (struct ext4_dir_entry_2 *)dir_block->b_data;
+-      ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
++      param.inode = inode;
++      param.data1 = (void *)data1;
++      param.data2 = (void *)data2;
++      ext4_init_dot_dotdot((struct inode *)(&param), de, blocksize,
++                           csum_size, dir->i_ino, 10);
+       set_nlink(inode, 2);
+       if (csum_size)
+               ext4_initialize_dirent_tail(dir_block, blocksize);
+@@ -3036,6 +3143,29 @@ out:
+       return err;
+ }
++/* Initialize @inode as a subdirectory of @dir, and add the
++ * "." and ".." entries into the first directory block. */
++int ext4_add_dot_dotdot(handle_t *handle, struct inode *dir,
++                      struct inode *inode,
++                      const void *data1, const void *data2)
++{
++      int rc;
++
++      if (IS_ERR(handle))
++              return PTR_ERR(handle);
++
++      if (IS_DIRSYNC(dir))
++              ext4_handle_sync(handle);
++
++      inode->i_op = &ext4_dir_inode_operations;
++      inode->i_fop = &ext4_dir_operations;
++      rc = ext4_init_new_dir(handle, dir, inode, data1, data2);
++      if (!rc)
++              rc = ext4_mark_inode_dirty(handle, inode);
++      return rc;
++}
++EXPORT_SYMBOL(ext4_add_dot_dotdot);
++
+ static int ext4_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
+                     struct dentry *dentry, umode_t mode)
+ {
+@@ -3063,7 +3193,7 @@ retry:
+       inode->i_op = &ext4_dir_inode_operations;
+       inode->i_fop = &ext4_dir_operations;
+-      err = ext4_init_new_dir(handle, dir, inode);
++      err = ext4_init_new_dir(handle, dir, inode, NULL, NULL);
+       if (err)
+               goto out_clear_inode;
+       err = ext4_mark_inode_dirty(handle, inode);
+diff --git a/fs/ext4/super.c b/fs/ext4/super.c
+index 8a95bd1..488162e 100644
+--- a/fs/ext4/super.c
++++ b/fs/ext4/super.c
+@@ -1692,7 +1692,7 @@ enum {
+       Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
+       Opt_inlinecrypt,
+       Opt_usrjquota, Opt_grpjquota, Opt_quota,
+-      Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
++      Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err, Opt_dirdata,
+       Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
+       Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
+       Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
+@@ -1808,6 +1808,7 @@ static const struct fs_parameter_spec ext4_param_specs[] = {
+       fsparam_u32     ("stripe",              Opt_stripe),
+       fsparam_flag    ("delalloc",            Opt_delalloc),
+       fsparam_flag    ("nodelalloc",          Opt_nodelalloc),
++      fsparam_flag    ("dirdata",             Opt_dirdata),
+       fsparam_flag    ("warn_on_error",       Opt_warn_on_error),
+       fsparam_flag    ("nowarn_on_error",     Opt_nowarn_on_error),
+       fsparam_u32     ("debug_want_extra_isize",
+@@ -1944,6 +1945,7 @@ static const struct mount_opts {
+                                                       MOPT_CLEAR | MOPT_Q},
+       {Opt_usrjquota, 0, MOPT_Q},
+       {Opt_grpjquota, 0, MOPT_Q},
++      {Opt_dirdata, EXT4_MOUNT_DIRDATA, MOPT_SET},
+       {Opt_jqfmt, 0, MOPT_QFMT},
+       {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
+       {Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
+-- 
+2.34.1
+
diff --git a/ldiskfs/kernel_patches/patches/rhel9/ext4-give-warning-with-dir-htree-growing.patch b/ldiskfs/kernel_patches/patches/rhel9/ext4-give-warning-with-dir-htree-growing.patch
new file mode 100644 (file)
index 0000000..c685f62
--- /dev/null
@@ -0,0 +1,169 @@
+Subject: [PATCH] rhel9/ext4-give-warning-with-dir-htree-growing
+
+---
+ fs/ext4/ext4.h  |  1 +
+ fs/ext4/namei.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++--
+ fs/ext4/super.c |  3 +++
+ fs/ext4/sysfs.c |  2 ++
+ 4 files changed, 72 insertions(+), 2 deletions(-)
+
+diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
+index cf9af1a..dddce26 100644
+--- a/fs/ext4/ext4.h
++++ b/fs/ext4/ext4.h
+@@ -1630,6 +1630,7 @@ struct ext4_sb_info {
+       unsigned int s_mb_group_prealloc;
+       unsigned int s_mb_max_inode_prealloc;
+       unsigned int s_max_dir_size_kb;
++      unsigned long s_warning_dir_size;
+       /* where last allocation was done - for stream allocation */
+       unsigned long s_mb_last_group;
+       unsigned long s_mb_last_start;
+diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
+index c772b3b..1cbee90 100644
+--- a/fs/ext4/namei.c
++++ b/fs/ext4/namei.c
+@@ -795,12 +795,20 @@ struct ext4_dir_lock_data {
+ #define ext4_htree_lock_data(l)       ((struct ext4_dir_lock_data *)(l)->lk_private)
+ #define ext4_find_entry(dir, name, dirent, inline) \
+                       ext4_find_entry_locked(dir, name, dirent, inline, NULL)
+-#define ext4_add_entry(handle, dentry, inode) \
+-                      ext4_add_entry_locked(handle, dentry, inode, NULL)
+ /* NB: ext4_lblk_t is 32 bits so we use high bits to identify invalid blk */
+ #define EXT4_HTREE_NODE_CHANGED       (0xcafeULL << 32)
++inline int ext4_add_entry(handle_t *handle, struct dentry *dentry,
++                        struct inode *inode)
++{
++      int ret = ext4_add_entry_locked(handle, dentry, inode, NULL);
++
++      if (ret == -ENOBUFS)
++              ret = 0;
++      return ret;
++}
++
+ static void ext4_htree_event_cb(void *target, void *event)
+ {
+       u64 *block = (u64 *)target;
+@@ -2777,6 +2785,54 @@ out:
+       return err;
+ }
++static unsigned long __ext4_max_dir_size(struct dx_frame *frames,
++                             struct dx_frame *frame, struct inode *dir)
++{
++      unsigned long max_dir_size;
++
++      if (EXT4_SB(dir->i_sb)->s_max_dir_size_kb) {
++              max_dir_size = EXT4_SB(dir->i_sb)->s_max_dir_size_kb << 10;
++      } else {
++              max_dir_size = EXT4_BLOCK_SIZE(dir->i_sb);
++              while (frame >= frames) {
++                      max_dir_size *= dx_get_limit(frame->entries);
++                      if (frame == frames)
++                              break;
++                      frame--;
++              }
++              /* use 75% of max dir size in average */
++              max_dir_size = max_dir_size / 4 * 3;
++      }
++      return max_dir_size;
++}
++
++/*
++ * With hash tree growing, it is easy to hit ENOSPC, but it is hard
++ * to predict when it will happen. let's give administrators warning
++ * when reaching 3/5 and 2/3 of limit
++ */
++static inline bool dir_size_in_warning_range(struct dx_frame *frames,
++                                           struct dx_frame *frame,
++                                           struct inode *dir)
++{
++      unsigned long size1, size2;
++      struct super_block *sb = dir->i_sb;
++
++      if (unlikely(!EXT4_SB(sb)->s_warning_dir_size))
++              EXT4_SB(sb)->s_warning_dir_size =
++                      __ext4_max_dir_size(frames, frame, dir);
++
++      size1 = EXT4_SB(sb)->s_warning_dir_size / 16 * 10;
++      size1 = size1 & ~(EXT4_BLOCK_SIZE(sb) - 1);
++      size2 = EXT4_SB(sb)->s_warning_dir_size / 16 * 11;
++      size2 = size2 & ~(EXT4_BLOCK_SIZE(sb) - 1);
++      if (in_range(dir->i_size, size1, EXT4_BLOCK_SIZE(sb)) ||
++          in_range(dir->i_size, size2, EXT4_BLOCK_SIZE(sb)))
++              return true;
++
++      return false;
++}
++
+ /*
+  *    ext4_add_entry()
+  *
+@@ -2919,6 +2975,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
+       struct ext4_dir_entry_2 *de;
+       int restart;
+       int err;
++      bool ret_warn = false;
+ again:
+       restart = 0;
+@@ -2947,6 +3004,11 @@ again:
+       /* Block full, should compress but for now just split */
+       dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
+                      dx_get_count(entries), dx_get_limit(entries)));
++
++      if (frame - frames + 1 >= ext4_dir_htree_level(sb) ||
++          EXT4_SB(sb)->s_warning_dir_size)
++              ret_warn = dir_size_in_warning_range(frames, frame, dir);
++
+       /* Need to split index? */
+       if (dx_get_count(entries) == dx_get_limit(entries)) {
+               ext4_lblk_t newblock;
+@@ -3111,6 +3173,8 @@ cleanup:
+        */
+       if (restart && err == 0)
+               goto again;
++      if (err == 0 && ret_warn)
++              err = -ENOBUFS;
+       return err;
+ }
+diff --git a/fs/ext4/super.c b/fs/ext4/super.c
+index cff307e..6f9effb 100644
+--- a/fs/ext4/super.c
++++ b/fs/ext4/super.c
+@@ -2912,6 +2912,9 @@ static int ext4_apply_options(struct fs_context *fc, struct super_block *sb)
+ #ifdef CONFIG_EXT4_DEBUG
+       APPLY(s_fc_debug_max_replay);
+ #endif
++      /* reset s_warning_dir_size and make it re-calculated */
++      if (ctx->spec & EXT4_SPEC_s_max_dir_size_kb)
++              sbi->s_warning_dir_size = 0;
+       ext4_apply_quota_options(fc, sb);
+diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
+index 8498e62..725aec4 100644
+--- a/fs/ext4/sysfs.c
++++ b/fs/ext4/sysfs.c
+@@ -210,6 +210,7 @@ EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
+ EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
+ EXT4_RW_ATTR_SBI_UI(max_dir_size, s_max_dir_size_kb);
+ EXT4_RW_ATTR_SBI_UI(max_dir_size_kb, s_max_dir_size_kb);
++EXT4_RW_ATTR_SBI_UI(warning_dir_size, s_warning_dir_size);
+ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
+ EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
+ EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
+@@ -263,6 +264,7 @@ static struct attribute *ext4_attrs[] = {
+       ATTR_LIST(inode_goal),
+       ATTR_LIST(max_dir_size),
+       ATTR_LIST(max_dir_size_kb),
++      ATTR_LIST(warning_dir_size),
+       ATTR_LIST(mb_stats),
+       ATTR_LIST(mb_max_to_scan),
+       ATTR_LIST(mb_min_to_scan),
+-- 
+2.25.1
+
diff --git a/ldiskfs/kernel_patches/patches/rhel9/ext4-max-dir-size.patch b/ldiskfs/kernel_patches/patches/rhel9/ext4-max-dir-size.patch
new file mode 100644 (file)
index 0000000..aac34c6
--- /dev/null
@@ -0,0 +1,51 @@
+Add a proc interface for max_dir_size.
+
+---
+ fs/ext4/sysfs.c | 10 +++++++++-
+ 1 file changed, 9 insertions(+), 1 deletion(-)
+
+diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
+index db8a217..8498e62 100644
+--- a/fs/ext4/sysfs.c
++++ b/fs/ext4/sysfs.c
+@@ -208,6 +208,8 @@ EXT4_ATTR_FUNC(sra_exceeded_retry_limit, 0444);
+ EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
+                ext4_sb_info, s_inode_readahead_blks);
+ EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
++EXT4_RW_ATTR_SBI_UI(max_dir_size, s_max_dir_size_kb);
++EXT4_RW_ATTR_SBI_UI(max_dir_size_kb, s_max_dir_size_kb);
+ EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
+ EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
+ EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
+@@ -259,6 +261,8 @@ static struct attribute *ext4_attrs[] = {
+       ATTR_LIST(sra_exceeded_retry_limit),
+       ATTR_LIST(inode_readahead_blks),
+       ATTR_LIST(inode_goal),
++      ATTR_LIST(max_dir_size),
++      ATTR_LIST(max_dir_size_kb),
+       ATTR_LIST(mb_stats),
+       ATTR_LIST(mb_max_to_scan),
+       ATTR_LIST(mb_min_to_scan),
+@@ -402,7 +406,9 @@ static ssize_t ext4_attr_show(struct kobject *kobj,
+                                       le32_to_cpup(ptr));
+               else
+                       return sysfs_emit(buf, "%u\n",
+-                                      *((unsigned int *) ptr));
++                                      strcmp("max_dir_size", a->attr.name) ?
++                                      *((unsigned int *) ptr) :
++                                      (*((unsigned int *) ptr)) << 10);
+       case attr_pointer_ul:
+               if (!ptr)
+                       return 0;
+@@ -465,6 +471,8 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
+               ret = kstrtoul(skip_spaces(buf), 0, &t);
+               if (ret)
+                       return ret;
++              if (strcmp("max_dir_size", a->attr.name) == 0)
++                      t >>= 10;
+               if (a->attr_ptr == ptr_ext4_super_block_offset)
+                       *((__le32 *) ptr) = cpu_to_le32(t);
+               else
+-- 
+2.25.1
+
diff --git a/ldiskfs/kernel_patches/patches/rhel9/ext4-pdirop.patch b/ldiskfs/kernel_patches/patches/rhel9/ext4-pdirop.patch
new file mode 100644 (file)
index 0000000..7cd32cc
--- /dev/null
@@ -0,0 +1,900 @@
+Subject: [PATCH] LU-50 ldiskfs: pdirops patch for ldiskfs
+
+ Single directory performance is a critical for HPC workloads.
+ In a typical use case an application creates a separate output file for each
+ node and task in a job. As nodes and tasks increase, hundreds of thousands of
+ files may be created in a single directory within a short window of time.
+ Today, both filename lookup and file system modifying operations (such as
+ create and unlink) are protected with a single lock for an entire ldiskfs
+ directory. PDO project will remove this bottleneck by introducing a parallel
+ locking mechanism for entire ldiskfs directories. This work will enable
+ multiple application threads to simultaneously lookup, create and unlink in
+ parallel.
+
+This patch contains:
+ - pdirops support for ldiskfs
+ - integrate with osd-ldiskfs
+---
+ fs/ext4/Makefile |   1 +
+ fs/ext4/ext4.h   |  78 ++++++++
+ fs/ext4/namei.c  | 454 ++++++++++++++++++++++++++++++++++++++++++-----
+ fs/ext4/super.c  |   1 +
+ 4 files changed, 494 insertions(+), 40 deletions(-)
+
+diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
+index 49e7af6..f7ced03 100644
+--- a/fs/ext4/Makefile
++++ b/fs/ext4/Makefile
+@@ -7,6 +7,7 @@ obj-$(CONFIG_EXT4_FS) += ext4.o
+ ext4-y        := balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \
+               extents_status.o file.o fsmap.o fsync.o hash.o ialloc.o \
++              htree_lock.o \
+               indirect.o inline.o inode.o ioctl.o mballoc.o migrate.o \
+               mmp.o move_extent.o namei.o page-io.o readpage.o resize.o \
+               super.o symlink.o sysfs.o xattr.o xattr_hurd.o xattr_trusted.o \
+diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
+index 024f203..21d83fc 100644
+--- a/fs/ext4/ext4.h
++++ b/fs/ext4/ext4.h
+@@ -28,6 +28,7 @@
+ #include <linux/mutex.h>
+ #include <linux/timer.h>
+ #include <linux/wait.h>
++#include <linux/htree_lock.h>
+ #include <linux/sched/signal.h>
+ #include <linux/blockgroup_lock.h>
+ #include <linux/percpu_counter.h>
+@@ -1020,6 +1021,9 @@ struct ext4_inode_info {
+       __u32   i_dtime;
+       ext4_fsblk_t    i_file_acl;
++      /* following fields for parallel directory operations -bzzz */
++      struct semaphore i_append_sem;
++
+       /*
+        * i_block_group is the number of the block group which contains
+        * this file's inode.  Constant across the lifetime of the inode,
+@@ -2504,6 +2508,72 @@ struct dx_hash_info
+  */
+ #define HASH_NB_ALWAYS                1
++/* assume name-hash is protected by upper layer */
++#define EXT4_HTREE_LOCK_HASH  0
++
++enum ext4_pdo_lk_types {
++#if EXT4_HTREE_LOCK_HASH
++      EXT4_LK_HASH,
++#endif
++      EXT4_LK_DX,             /* index block */
++      EXT4_LK_DE,             /* directory entry block */
++      EXT4_LK_SPIN,           /* spinlock */
++      EXT4_LK_MAX,
++};
++
++/* read-only bit */
++#define EXT4_LB_RO(b)         (1 << (b))
++/* read + write, high bits for writer */
++#define EXT4_LB_RW(b)         ((1 << (b)) | (1 << (EXT4_LK_MAX + (b))))
++
++enum ext4_pdo_lock_bits {
++      /* DX lock bits */
++      EXT4_LB_DX_RO           = EXT4_LB_RO(EXT4_LK_DX),
++      EXT4_LB_DX              = EXT4_LB_RW(EXT4_LK_DX),
++      /* DE lock bits */
++      EXT4_LB_DE_RO           = EXT4_LB_RO(EXT4_LK_DE),
++      EXT4_LB_DE              = EXT4_LB_RW(EXT4_LK_DE),
++      /* DX spinlock bits */
++      EXT4_LB_SPIN_RO         = EXT4_LB_RO(EXT4_LK_SPIN),
++      EXT4_LB_SPIN            = EXT4_LB_RW(EXT4_LK_SPIN),
++      /* accurate searching */
++      EXT4_LB_EXACT           = EXT4_LB_RO(EXT4_LK_MAX << 1),
++};
++
++enum ext4_pdo_lock_opc {
++      /* external */
++      EXT4_HLOCK_READDIR      = (EXT4_LB_DE_RO | EXT4_LB_DX_RO),
++      EXT4_HLOCK_LOOKUP       = (EXT4_LB_DE_RO | EXT4_LB_SPIN_RO |
++                                 EXT4_LB_EXACT),
++      EXT4_HLOCK_DEL          = (EXT4_LB_DE | EXT4_LB_SPIN_RO |
++                                 EXT4_LB_EXACT),
++      EXT4_HLOCK_ADD          = (EXT4_LB_DE | EXT4_LB_SPIN_RO),
++
++      /* internal */
++      EXT4_HLOCK_LOOKUP_SAFE  = (EXT4_LB_DE_RO | EXT4_LB_DX_RO |
++                                 EXT4_LB_EXACT),
++      EXT4_HLOCK_DEL_SAFE     = (EXT4_LB_DE | EXT4_LB_DX_RO | EXT4_LB_EXACT),
++      EXT4_HLOCK_SPLIT        = (EXT4_LB_DE | EXT4_LB_DX | EXT4_LB_SPIN),
++};
++
++extern struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits);
++#define ext4_htree_lock_head_free(lhead)      htree_lock_head_free(lhead)
++
++extern struct htree_lock *ext4_htree_lock_alloc(void);
++#define ext4_htree_lock_free(lck)             htree_lock_free(lck)
++
++extern void ext4_htree_lock(struct htree_lock *lck,
++                          struct htree_lock_head *lhead,
++                          struct inode *dir, unsigned flags);
++#define ext4_htree_unlock(lck)                  htree_unlock(lck)
++
++extern struct buffer_head *ext4_find_entry_locked(struct inode *dir,
++                                      const struct qstr *d_name,
++                                      struct ext4_dir_entry_2 **res_dir,
++                                      int *inlined, struct htree_lock *lck);
++extern int ext4_add_entry_locked(handle_t *handle, struct dentry *dentry,
++                    struct inode *inode, struct htree_lock *lck);
++
+ struct ext4_filename {
+       const struct qstr *usr_fname;
+       struct fscrypt_str disk_name;
+@@ -2882,12 +2952,20 @@ void ext4_insert_dentry(struct inode *dir, struct inode *inode,
+                       void *data);
+ static inline void ext4_update_dx_flag(struct inode *inode)
+ {
++      /* Disable it for ldiskfs, because going from a DX directory to
++       * a non-DX directory while it is in use will completely break
++       * the htree-locking.
++       * If we really want to support this operation in the future,
++       * we need to exclusively lock the directory at here which will
++       * increase complexity of code */
++#if 0
+       if (!ext4_has_feature_dir_index(inode->i_sb) &&
+           ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
+               /* ext4_iget() should have caught this... */
+               WARN_ON_ONCE(ext4_has_feature_metadata_csum(inode->i_sb));
+               ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
+       }
++#endif
+ }
+ static const unsigned char ext4_filetype_table[] = {
+       DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
+diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
+index 059bc08..7d25879 100644
+--- a/fs/ext4/namei.c
++++ b/fs/ext4/namei.c
+@@ -55,6 +55,7 @@ struct buffer_head *ext4_append(handle_t *handle,
+                                       ext4_lblk_t *block)
+ {
+       struct buffer_head *bh;
++      struct ext4_inode_info *ei = EXT4_I(inode);
+       int err;
+       if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
+@@ -62,16 +63,23 @@ struct buffer_head *ext4_append(handle_t *handle,
+                     EXT4_SB(inode->i_sb)->s_max_dir_size_kb)))
+               return ERR_PTR(-ENOSPC);
++      /* with parallel dir operations all appends
++      * have to be serialized -bzzz */
++      down(&ei->i_append_sem);
++
+       *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
+       bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
+-      if (IS_ERR(bh))
++      if (IS_ERR(bh)) {
++              up(&ei->i_append_sem);
+               return bh;
++      }
+       inode->i_size += inode->i_sb->s_blocksize;
+       EXT4_I(inode)->i_disksize = inode->i_size;
+       BUFFER_TRACE(bh, "get_write_access");
+       err = ext4_journal_get_write_access(handle, inode->i_sb, bh,
+                                           EXT4_JTR_NONE);
++      up(&ei->i_append_sem);
+       if (err) {
+               brelse(bh);
+               ext4_std_error(inode->i_sb, err);
+@@ -268,7 +276,8 @@ static unsigned dx_node_limit(struct inode *dir);
+ static struct dx_frame *dx_probe(struct ext4_filename *fname,
+                                struct inode *dir,
+                                struct dx_hash_info *hinfo,
+-                               struct dx_frame *frame);
++                               struct dx_frame *frame,
++                               struct htree_lock *lck);
+ static void dx_release(struct dx_frame *frames, struct inode *dir);
+ static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de,
+                      unsigned blocksize, struct dx_hash_info *hinfo,
+@@ -284,12 +293,13 @@ static void dx_insert_block(struct dx_frame *frame,
+ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
+                                struct dx_frame *frame,
+                                struct dx_frame *frames,
+-                               __u32 *start_hash);
++                               __u32 *start_hash, struct htree_lock *lck);
+ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
+               struct ext4_filename *fname,
+-              struct ext4_dir_entry_2 **res_dir);
++              struct ext4_dir_entry_2 **res_dir, struct htree_lock *lck);
+ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
+-                           struct inode *dir, struct inode *inode);
++                           struct inode *dir, struct inode *inode,
++                           struct htree_lock *lck);
+ /* checksumming functions */
+ void ext4_initialize_dirent_tail(struct buffer_head *bh,
+@@ -774,6 +784,227 @@ static inline void htree_rep_invariant_check(struct dx_entry *at,
+ }
+ #endif /* DX_DEBUG */
++/* private data for htree_lock */
++struct ext4_dir_lock_data {
++      unsigned                ld_flags;  /* bits-map for lock types */
++      unsigned                ld_count;  /* # entries of the last DX block */
++      struct dx_entry         ld_at_entry; /* copy of leaf dx_entry */
++      struct dx_entry         *ld_at;    /* position of leaf dx_entry */
++};
++
++#define ext4_htree_lock_data(l)       ((struct ext4_dir_lock_data *)(l)->lk_private)
++#define ext4_find_entry(dir, name, dirent, inline) \
++                      ext4_find_entry_locked(dir, name, dirent, inline, NULL)
++#define ext4_add_entry(handle, dentry, inode) \
++                      ext4_add_entry_locked(handle, dentry, inode, NULL)
++
++/* NB: ext4_lblk_t is 32 bits so we use high bits to identify invalid blk */
++#define EXT4_HTREE_NODE_CHANGED       (0xcafeULL << 32)
++
++static void ext4_htree_event_cb(void *target, void *event)
++{
++      u64 *block = (u64 *)target;
++
++      if (*block == dx_get_block((struct dx_entry *)event))
++              *block = EXT4_HTREE_NODE_CHANGED;
++}
++
++struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits)
++{
++      struct htree_lock_head *lhead;
++
++      lhead = htree_lock_head_alloc(EXT4_LK_MAX, hbits, 0);
++      if (lhead != NULL) {
++              htree_lock_event_attach(lhead, EXT4_LK_SPIN, HTREE_EVENT_WR,
++                                      ext4_htree_event_cb);
++      }
++      return lhead;
++}
++EXPORT_SYMBOL(ext4_htree_lock_head_alloc);
++
++struct htree_lock *ext4_htree_lock_alloc(void)
++{
++      return htree_lock_alloc(EXT4_LK_MAX,
++                              sizeof(struct ext4_dir_lock_data));
++}
++EXPORT_SYMBOL(ext4_htree_lock_alloc);
++
++static htree_lock_mode_t ext4_htree_mode(unsigned flags)
++{
++      switch (flags) {
++      default: /* 0 or unknown flags require EX lock */
++              return HTREE_LOCK_EX;
++      case EXT4_HLOCK_READDIR:
++              return HTREE_LOCK_PR;
++      case EXT4_HLOCK_LOOKUP:
++              return HTREE_LOCK_CR;
++      case EXT4_HLOCK_DEL:
++      case EXT4_HLOCK_ADD:
++              return HTREE_LOCK_CW;
++      }
++}
++
++/* return PR for read-only operations, otherwise return EX */
++static inline htree_lock_mode_t ext4_htree_safe_mode(unsigned flags)
++{
++      int writer = (flags & EXT4_LB_DE) == EXT4_LB_DE;
++
++      /* 0 requires EX lock */
++      return (flags == 0 || writer) ? HTREE_LOCK_EX : HTREE_LOCK_PR;
++}
++
++static int ext4_htree_safe_locked(struct htree_lock *lck)
++{
++      int writer;
++
++      if (lck == NULL || lck->lk_mode == HTREE_LOCK_EX)
++              return 1;
++
++      writer = (ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_DE) ==
++               EXT4_LB_DE;
++      if (writer) /* all readers & writers are excluded? */
++              return lck->lk_mode == HTREE_LOCK_EX;
++
++      /* all writers are excluded? */
++      return lck->lk_mode == HTREE_LOCK_PR ||
++             lck->lk_mode == HTREE_LOCK_PW ||
++             lck->lk_mode == HTREE_LOCK_EX;
++}
++
++/* relock htree_lock with EX mode if it's change operation, otherwise
++ * relock it with PR mode. It's noop if PDO is disabled. */
++static void ext4_htree_safe_relock(struct htree_lock *lck)
++{
++      if (!ext4_htree_safe_locked(lck)) {
++              unsigned flags = ext4_htree_lock_data(lck)->ld_flags;
++
++              htree_change_lock(lck, ext4_htree_safe_mode(flags));
++      }
++}
++
++void ext4_htree_lock(struct htree_lock *lck, struct htree_lock_head *lhead,
++                   struct inode *dir, unsigned flags)
++{
++      htree_lock_mode_t mode = is_dx(dir) ? ext4_htree_mode(flags) :
++                                            ext4_htree_safe_mode(flags);
++
++      ext4_htree_lock_data(lck)->ld_flags = flags;
++      htree_lock(lck, lhead, mode);
++      if (!is_dx(dir))
++              ext4_htree_safe_relock(lck); /* make sure it's safe locked */
++}
++EXPORT_SYMBOL(ext4_htree_lock);
++
++static int ext4_htree_node_lock(struct htree_lock *lck, struct dx_entry *at,
++                              unsigned lmask, int wait, void *ev)
++{
++      u32     key = (at == NULL) ? 0 : dx_get_block(at);
++      u32     mode;
++
++      /* NOOP if htree is well protected or caller doesn't require the lock */
++      if (ext4_htree_safe_locked(lck) ||
++         !(ext4_htree_lock_data(lck)->ld_flags & lmask))
++              return 1;
++
++      mode = (ext4_htree_lock_data(lck)->ld_flags & lmask) == lmask ?
++              HTREE_LOCK_PW : HTREE_LOCK_PR;
++      while (1) {
++              if (htree_node_lock_try(lck, mode, key, ffz(~lmask), wait, ev))
++                      return 1;
++              if (!(lmask & EXT4_LB_SPIN)) /* not a spinlock */
++                      return 0;
++              cpu_relax(); /* spin until granted */
++      }
++}
++
++static int ext4_htree_node_locked(struct htree_lock *lck, unsigned lmask)
++{
++      return ext4_htree_safe_locked(lck) ||
++             htree_node_is_granted(lck, ffz(~lmask));
++}
++
++static void ext4_htree_node_unlock(struct htree_lock *lck,
++                                 unsigned lmask, void *buf)
++{
++      /* NB: it's safe to call mutiple times or even it's not locked */
++      if (!ext4_htree_safe_locked(lck) &&
++           htree_node_is_granted(lck, ffz(~lmask)))
++              htree_node_unlock(lck, ffz(~lmask), buf);
++}
++
++#define ext4_htree_dx_lock(lck, key)          \
++      ext4_htree_node_lock(lck, key, EXT4_LB_DX, 1, NULL)
++#define ext4_htree_dx_lock_try(lck, key)      \
++      ext4_htree_node_lock(lck, key, EXT4_LB_DX, 0, NULL)
++#define ext4_htree_dx_unlock(lck)             \
++      ext4_htree_node_unlock(lck, EXT4_LB_DX, NULL)
++#define ext4_htree_dx_locked(lck)             \
++      ext4_htree_node_locked(lck, EXT4_LB_DX)
++
++static void ext4_htree_dx_need_lock(struct htree_lock *lck)
++{
++      struct ext4_dir_lock_data *ld;
++
++      if (ext4_htree_safe_locked(lck))
++              return;
++
++      ld = ext4_htree_lock_data(lck);
++      switch (ld->ld_flags) {
++      default:
++              return;
++      case EXT4_HLOCK_LOOKUP:
++              ld->ld_flags = EXT4_HLOCK_LOOKUP_SAFE;
++              return;
++      case EXT4_HLOCK_DEL:
++              ld->ld_flags = EXT4_HLOCK_DEL_SAFE;
++              return;
++      case EXT4_HLOCK_ADD:
++              ld->ld_flags = EXT4_HLOCK_SPLIT;
++              return;
++      }
++}
++
++#define ext4_htree_de_lock(lck, key)          \
++      ext4_htree_node_lock(lck, key, EXT4_LB_DE, 1, NULL)
++#define ext4_htree_de_unlock(lck)             \
++      ext4_htree_node_unlock(lck, EXT4_LB_DE, NULL)
++
++#define ext4_htree_spin_lock(lck, key, event) \
++      ext4_htree_node_lock(lck, key, EXT4_LB_SPIN, 0, event)
++#define ext4_htree_spin_unlock(lck)           \
++      ext4_htree_node_unlock(lck, EXT4_LB_SPIN, NULL)
++#define ext4_htree_spin_unlock_listen(lck, p) \
++      ext4_htree_node_unlock(lck, EXT4_LB_SPIN, p)
++
++static void ext4_htree_spin_stop_listen(struct htree_lock *lck)
++{
++      if (!ext4_htree_safe_locked(lck) &&
++          htree_node_is_listening(lck, ffz(~EXT4_LB_SPIN)))
++              htree_node_stop_listen(lck, ffz(~EXT4_LB_SPIN));
++}
++
++enum {
++      DX_HASH_COL_IGNORE,     /* ignore collision while probing frames */
++      DX_HASH_COL_YES,        /* there is collision and it does matter */
++      DX_HASH_COL_NO,         /* there is no collision */
++};
++
++static int dx_probe_hash_collision(struct htree_lock *lck,
++                                 struct dx_entry *entries,
++                                 struct dx_entry *at, u32 hash)
++{
++      if (!(lck && ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_EXACT)) {
++              return DX_HASH_COL_IGNORE; /* don't care about collision */
++
++      } else if (at == entries + dx_get_count(entries) - 1) {
++              return DX_HASH_COL_IGNORE; /* not in any leaf of this DX */
++
++      } else { /* hash collision? */
++              return ((dx_get_hash(at + 1) & ~1) == hash) ?
++                      DX_HASH_COL_YES : DX_HASH_COL_NO;
++      }
++}
++
+ /*
+  * Probe for a directory leaf block to search.
+  *
+@@ -785,10 +1016,11 @@ static inline void htree_rep_invariant_check(struct dx_entry *at,
+  */
+ static struct dx_frame *
+ dx_probe(struct ext4_filename *fname, struct inode *dir,
+-       struct dx_hash_info *hinfo, struct dx_frame *frame_in)
++       struct dx_hash_info *hinfo, struct dx_frame *frame_in,
++       struct htree_lock *lck)
+ {
+       unsigned count, indirect;
+-      struct dx_entry *at, *entries, *p, *q, *m;
++      struct dx_entry *at, *entries, *p, *q, *m, *dx = NULL;
+       struct dx_root_info *info;
+       struct dx_frame *frame = frame_in;
+       struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR);
+@@ -868,8 +1100,15 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
+       dxtrace(printk("Look up %x", hash));
+       while (1) {
++              if (indirect == 0) { /* the last index level */
++                      /* NB: ext4_htree_dx_lock() could be noop if
++                       * DX-lock flag is not set for current operation */
++                      ext4_htree_dx_lock(lck, dx);
++                      ext4_htree_spin_lock(lck, dx, NULL);
++              }
+               count = dx_get_count(entries);
+-              if (!count || count > dx_get_limit(entries)) {
++              if (count == 0 || count > dx_get_limit(entries)) {
++                      ext4_htree_spin_unlock(lck); /* release spin */
+                       ext4_warning_inode(dir,
+                                          "dx entry: count %u beyond limit %u",
+                                          count, dx_get_limit(entries));
+@@ -895,8 +1134,70 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
+                              dx_get_block(at)));
+               frame->entries = entries;
+               frame->at = at;
+-              if (!indirect--)
++
++              if (indirect == 0) { /* the last index level */
++                      struct ext4_dir_lock_data *ld;
++                      u64 myblock;
++
++                      /* By default we only lock DE-block, however, we will
++                       * also lock the last level DX-block if:
++                       * a) there is hash collision
++                       *    we will set DX-lock flag (a few lines below)
++                       *    and redo to lock DX-block
++                       *    see detail in dx_probe_hash_collision()
++                       * b) it's a retry from splitting
++                       *    we need to lock the last level DX-block so nobody
++                       *    else can split any leaf blocks under the same
++                       *    DX-block, see detail in ext4_dx_add_entry()
++                       */
++                      if (ext4_htree_dx_locked(lck)) {
++                              /* DX-block is locked, just lock DE-block
++                               * and return */
++                              ext4_htree_spin_unlock(lck);
++                              if (!ext4_htree_safe_locked(lck))
++                                      ext4_htree_de_lock(lck, frame->at);
++                              return frame;
++                      }
++                      /* it's pdirop and no DX lock */
++                      if (dx_probe_hash_collision(lck, entries, at, hash) ==
++                          DX_HASH_COL_YES) {
++                              /* found hash collision, set DX-lock flag
++                               * and retry to abtain DX-lock */
++                              ext4_htree_spin_unlock(lck);
++                              ext4_htree_dx_need_lock(lck);
++                              continue;
++                      }
++                      ld = ext4_htree_lock_data(lck);
++                      /* because I don't lock DX, so @at can't be trusted
++                       * after I release spinlock so I have to save it */
++                      ld->ld_at = at;
++                      ld->ld_at_entry = *at;
++                      ld->ld_count = dx_get_count(entries);
++
++                      frame->at = &ld->ld_at_entry;
++                      myblock = dx_get_block(at);
++
++                      /* NB: ordering locking */
++                      ext4_htree_spin_unlock_listen(lck, &myblock);
++                      /* other thread can split this DE-block because:
++                       * a) I don't have lock for the DE-block yet
++                       * b) I released spinlock on DX-block
++                       * if it happened I can detect it by listening
++                       * splitting event on this DE-block */
++                      ext4_htree_de_lock(lck, frame->at);
++                      ext4_htree_spin_stop_listen(lck);
++
++                      if (myblock == EXT4_HTREE_NODE_CHANGED) {
++                              /* someone split this DE-block before
++                               * I locked it, I need to retry and lock
++                               * valid DE-block */
++                              ext4_htree_de_unlock(lck);
++                              continue;
++                      }
+                       return frame;
++              }
++              dx = at;
++              indirect--;
+               frame++;
+               frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX);
+               if (IS_ERR(frame->bh)) {
+@@ -965,7 +1266,7 @@ static void dx_release(struct dx_frame *frames, struct inode *dir)
+ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
+                                struct dx_frame *frame,
+                                struct dx_frame *frames,
+-                               __u32 *start_hash)
++                               __u32 *start_hash, struct htree_lock *lck)
+ {
+       struct dx_frame *p;
+       struct buffer_head *bh;
+@@ -980,12 +1281,22 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
+        * this loop, num_frames indicates the number of interior
+        * nodes need to be read.
+        */
++      ext4_htree_de_unlock(lck);
+       while (1) {
+-              if (++(p->at) < p->entries + dx_get_count(p->entries))
+-                      break;
++              if (num_frames > 0 || ext4_htree_dx_locked(lck)) {
++                      /* num_frames > 0 :
++                       *   DX block
++                       * ext4_htree_dx_locked:
++                       *   frame->at is reliable pointer returned by dx_probe,
++                       *   otherwise dx_probe already knew no collision */
++                      if (++(p->at) < p->entries + dx_get_count(p->entries))
++                              break;
++              }
+               if (p == frames)
+                       return 0;
+               num_frames++;
++              if (num_frames == 1)
++                      ext4_htree_dx_unlock(lck);
+               p--;
+       }
+@@ -1008,6 +1319,13 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
+        * block so no check is necessary
+        */
+       while (num_frames--) {
++              if (num_frames == 0) {
++                      /* it's not always necessary, we just don't want to
++                       * detect hash collision again */
++                      ext4_htree_dx_need_lock(lck);
++                      ext4_htree_dx_lock(lck, p->at);
++              }
++
+               bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX);
+               if (IS_ERR(bh))
+                       return PTR_ERR(bh);
+@@ -1016,6 +1334,7 @@ static int ext4_htree_next_block(struct inode *dir, __u32 hash,
+               p->bh = bh;
+               p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
+       }
++      ext4_htree_de_lock(lck, p->at);
+       return 1;
+ }
+@@ -1177,10 +1496,10 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
+       }
+       hinfo.hash = start_hash;
+       hinfo.minor_hash = 0;
+-      frame = dx_probe(NULL, dir, &hinfo, frames);
++      /* assume it's PR locked */
++      frame = dx_probe(NULL, dir, &hinfo, frames, NULL);
+       if (IS_ERR(frame))
+               return PTR_ERR(frame);
+-
+       /* Add '.' and '..' from the htree header */
+       if (!start_hash && !start_minor_hash) {
+               de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
+@@ -1220,7 +1539,7 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
+               count += ret;
+               hashval = ~0;
+               ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
+-                                          frame, frames, &hashval);
++                                          frame, frames, &hashval, NULL);
+               *next_hash = hashval;
+               if (ret < 0) {
+                       err = ret;
+@@ -1533,7 +1852,7 @@ static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
+ static struct buffer_head *__ext4_find_entry(struct inode *dir,
+                                            struct ext4_filename *fname,
+                                            struct ext4_dir_entry_2 **res_dir,
+-                                           int *inlined)
++                                           int *inlined, struct htree_lock *lck)
+ {
+       struct super_block *sb;
+       struct buffer_head *bh_use[NAMEI_RA_SIZE];
+@@ -1575,7 +1894,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
+               goto restart;
+       }
+       if (is_dx(dir)) {
+-              ret = ext4_dx_find_entry(dir, fname, res_dir);
++              ret = ext4_dx_find_entry(dir, fname, res_dir, lck);
+               /*
+                * On success, or if the error was file not found,
+                * return.  Otherwise, fall back to doing a search the
+@@ -1585,6 +1904,7 @@ static struct buffer_head *__ext4_find_entry(struct inode *dir,
+                       goto cleanup_and_exit;
+               dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
+                              "falling back\n"));
++              ext4_htree_safe_relock(lck);
+               ret = NULL;
+       }
+       nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
+@@ -1675,10 +1995,10 @@ cleanup_and_exit:
+       return ret;
+ }
+-static struct buffer_head *ext4_find_entry(struct inode *dir,
++struct buffer_head *ext4_find_entry_locked(struct inode *dir,
+                                          const struct qstr *d_name,
+                                          struct ext4_dir_entry_2 **res_dir,
+-                                         int *inlined)
++                                         int *inlined, struct htree_lock *lck)
+ {
+       int err;
+       struct ext4_filename fname;
+@@ -1690,12 +2010,14 @@ static struct buffer_head *ext4_find_entry(struct inode *dir,
+       if (err)
+               return ERR_PTR(err);
+-      bh = __ext4_find_entry(dir, &fname, res_dir, inlined);
++      bh = __ext4_find_entry(dir, &fname, res_dir, inlined, lck);
+       ext4_fname_free_filename(&fname);
+       return bh;
+ }
++EXPORT_SYMBOL(ext4_find_entry_locked);
++
+ static struct buffer_head *ext4_lookup_entry(struct inode *dir,
+                                            struct dentry *dentry,
+                                            struct ext4_dir_entry_2 **res_dir)
+@@ -1711,7 +2033,7 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,
+       if (err)
+               return ERR_PTR(err);
+-      bh = __ext4_find_entry(dir, &fname, res_dir, NULL);
++      bh = __ext4_find_entry(dir, &fname, res_dir, NULL, NULL);
+       ext4_fname_free_filename(&fname);
+       return bh;
+@@ -1719,7 +2041,8 @@ static struct buffer_head *ext4_lookup_entry(struct inode *dir,
+ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
+                       struct ext4_filename *fname,
+-                      struct ext4_dir_entry_2 **res_dir)
++                      struct ext4_dir_entry_2 **res_dir,
++                      struct htree_lock *lck)
+ {
+       struct super_block * sb = dir->i_sb;
+       struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
+@@ -1730,7 +2053,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
+ #ifdef CONFIG_FS_ENCRYPTION
+       *res_dir = NULL;
+ #endif
+-      frame = dx_probe(fname, dir, NULL, frames);
++      frame = dx_probe(fname, dir, NULL, frames, lck);
+       if (IS_ERR(frame))
+               return (struct buffer_head *) frame;
+       do {
+@@ -1752,7 +2075,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
+               /* Check to see if we should continue to search */
+               retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame,
+-                                             frames, NULL);
++                                             frames, NULL, lck);
+               if (retval < 0) {
+                       ext4_warning_inode(dir,
+                               "error %d reading directory index block",
+@@ -1941,8 +2264,9 @@ static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
+  * Returns pointer to de in block into which the new entry will be inserted.
+  */
+ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
+-                      struct buffer_head **bh,struct dx_frame *frame,
+-                      struct dx_hash_info *hinfo)
++                      struct buffer_head **bh, struct dx_frame *frames,
++                      struct dx_frame *frame, struct dx_hash_info *hinfo,
++                      struct htree_lock *lck)
+ {
+       unsigned blocksize = dir->i_sb->s_blocksize;
+       unsigned count, continued;
+@@ -2015,8 +2339,14 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
+                                       hash2, split, count-split));
+       /* Fancy dance to stay within two buffers */
+-      de2 = dx_move_dirents(dir, data1, data2, map + split, count - split,
+-                            blocksize);
++      if (hinfo->hash < hash2) {
++              de2 = dx_move_dirents(dir, data1, data2, map + split,
++                                    count - split, blocksize);
++      } else {
++              /* make sure we will add entry to the same block which
++               * we have already locked */
++              de2 = dx_move_dirents(dir, data1, data2, map, split, blocksize);
++      }
+       de = dx_pack_dirents(dir, data1, blocksize);
+       de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
+                                          (char *) de,
+@@ -2034,12 +2364,21 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
+       dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2,
+                       blocksize, 1));
+-      /* Which block gets the new entry? */
+-      if (hinfo->hash >= hash2) {
+-              swap(*bh, bh2);
+-              de = de2;
++      ext4_htree_spin_lock(lck, frame > frames ? (frame - 1)->at : NULL,
++                           frame->at); /* notify block is being split */
++      if (hinfo->hash < hash2) {
++              dx_insert_block(frame, hash2 + continued, newblock);
++
++      } else {
++              /* switch block number */
++              dx_insert_block(frame, hash2 + continued,
++                              dx_get_block(frame->at));
++              dx_set_block(frame->at, newblock);
++              (frame->at)++;
+       }
+-      dx_insert_block(frame, hash2 + continued, newblock);
++      ext4_htree_spin_unlock(lck);
++      ext4_htree_dx_unlock(lck);
++
+       err = ext4_handle_dirty_dirblock(handle, dir, bh2);
+       if (err)
+               goto journal_error;
+@@ -2338,7 +2677,7 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
+       if (retval)
+               goto out_frames;
+-      de = do_split(handle,dir, &bh2, frame, &fname->hinfo);
++      de = do_split(handle, dir, &bh2, frames, frame, &fname->hinfo, NULL);
+       if (IS_ERR(de)) {
+               retval = PTR_ERR(de);
+               goto out_frames;
+@@ -2443,8 +2782,8 @@ out:
+  * may not sleep between calling this and putting something into
+  * the entry, as someone else might have used it while you slept.
+  */
+-static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
+-                        struct inode *inode)
++int ext4_add_entry_locked(handle_t *handle, struct dentry *dentry,
++                        struct inode *inode, struct htree_lock *lck)
+ {
+       struct inode *dir = d_inode(dentry->d_parent);
+       struct buffer_head *bh = NULL;
+@@ -2493,9 +2832,10 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
+               if (dentry->d_name.len == 2 &&
+                    memcmp(dentry->d_name.name, "..", 2) == 0)
+                        return ext4_update_dotdot(handle, dentry, inode);
+-              retval = ext4_dx_add_entry(handle, &fname, dir, inode);
++              retval = ext4_dx_add_entry(handle, &fname, dir, inode, lck);
+               if (!retval || (retval != ERR_BAD_DX_DIR))
+                       goto out;
++              ext4_htree_safe_relock(lck);
+               /* Can we just ignore htree data? */
+               if (ext4_has_metadata_csum(sb)) {
+                       EXT4_ERROR_INODE(dir,
+@@ -2558,12 +2898,14 @@ out:
+               ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
+       return retval;
+ }
++EXPORT_SYMBOL(ext4_add_entry_locked);
+ /*
+  * Returns 0 for success, or a negative error value
+  */
+ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
+-                           struct inode *dir, struct inode *inode)
++                           struct inode *dir, struct inode *inode,
++                           struct htree_lock *lck)
+ {
+       struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
+       struct dx_entry *entries, *at;
+@@ -2575,7 +2917,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
+ again:
+       restart = 0;
+-      frame = dx_probe(fname, dir, NULL, frames);
++      frame = dx_probe(fname, dir, NULL, frames, lck);
+       if (IS_ERR(frame))
+               return PTR_ERR(frame);
+       entries = frame->entries;
+@@ -2610,6 +2952,12 @@ again:
+               struct dx_node *node2;
+               struct buffer_head *bh2;
++              if (!ext4_htree_safe_locked(lck)) { /* retry with EX lock */
++                      ext4_htree_safe_relock(lck);
++                      restart = 1;
++                      goto cleanup;
++              }
++
+               while (frame > frames) {
+                       if (dx_get_count((frame - 1)->entries) <
+                           dx_get_limit((frame - 1)->entries)) {
+@@ -2713,8 +3061,32 @@ again:
+                       restart = 1;
+                       goto journal_error;
+               }
++      } else if (!ext4_htree_dx_locked(lck)) {
++              struct ext4_dir_lock_data *ld = ext4_htree_lock_data(lck);
++
++              /* not well protected, require DX lock */
++              ext4_htree_dx_need_lock(lck);
++              at = frame > frames ? (frame - 1)->at : NULL;
++
++              /* NB: no risk of deadlock because it's just a try.
++               *
++               * NB: we check ld_count for twice, the first time before
++               * having DX lock, the second time after holding DX lock.
++               *
++               * NB: We never free blocks for directory so far, which
++               * means value returned by dx_get_count() should equal to
++               * ld->ld_count if nobody split any DE-block under @at,
++               * and ld->ld_at still points to valid dx_entry. */
++              if ((ld->ld_count != dx_get_count(entries)) ||
++                  !ext4_htree_dx_lock_try(lck, at) ||
++                  (ld->ld_count != dx_get_count(entries))) {
++                      restart = 1;
++                      goto cleanup;
++              }
++              /* OK, I've got DX lock and nothing changed */
++              frame->at = ld->ld_at;
+       }
+-      de = do_split(handle, dir, &bh, frame, &fname->hinfo);
++      de = do_split(handle, dir, &bh, frames, frame, &fname->hinfo, lck);
+       if (IS_ERR(de)) {
+               err = PTR_ERR(de);
+               goto cleanup;
+@@ -2725,6 +3097,8 @@ again:
+ journal_error:
+       ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */
+ cleanup:
++      ext4_htree_dx_unlock(lck);
++      ext4_htree_de_unlock(lck);
+       brelse(bh);
+       dx_release(frames, dir);
+       /* @restart is true means htree-path has been changed, we need to
+diff --git a/fs/ext4/super.c b/fs/ext4/super.c
+index 5751080..a9372ee 100644
+--- a/fs/ext4/super.c
++++ b/fs/ext4/super.c
+@@ -1291,6 +1291,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
+       inode_set_iversion(&ei->vfs_inode, 1);
+       spin_lock_init(&ei->i_raw_lock);
++      sema_init(&ei->i_append_sem, 1);
+       INIT_LIST_HEAD(&ei->i_prealloc_list);
+       atomic_set(&ei->i_prealloc_active, 0);
+       spin_lock_init(&ei->i_prealloc_lock);
+-- 
+2.31.1
+
diff --git a/ldiskfs/kernel_patches/series/ldiskfs-5.14-rhel9.series b/ldiskfs/kernel_patches/series/ldiskfs-5.14-rhel9.series
new file mode 100644 (file)
index 0000000..9da50d2
--- /dev/null
@@ -0,0 +1,30 @@
+linux-5.16/ext4-inode-version.patch
+linux-5.4/ext4-lookup-dotdot.patch
+linux-5.14/ext4-print-inum-in-htree-warning.patch
+linux-5.14/ext4-prealloc.patch
+linux-5.16/ext4-osd-iop-common.patch
+linux-5.16/ext4-misc.patch
+linux-5.14/ext4-mballoc-extra-checks.patch
+sles15sp4/ext4-hash-indexed-dir-dotdot-update.patch
+linux-5.14/ext4-kill-dx-root.patch
+linux-5.8/ext4-mballoc-pa-free-mismatch.patch
+rhel9/ext4-data-in-dirent.patch
+rhel8/ext4-nocmtime.patch
+base/ext4-htree-lock.patch
+rhel9/ext4-pdirop.patch
+rhel9/ext4-max-dir-size.patch
+linux-5.16/ext4-corrupted-inode-block-bitmaps-handling-patches.patch
+rhel9/ext4-give-warning-with-dir-htree-growing.patch
+ubuntu18/ext4-jcb-optimization.patch
+linux-5.10/ext4-attach-jinode-in-writepages.patch
+rhel8/ext4-dont-check-before-replay.patch
+rhel7.6/ext4-use-GFP_NOFS-in-ext4_inode_attach_jinode.patch
+rhel7.6/ext4-export-orphan-add.patch
+linux-5.14/ext4-export-mb-stream-allocator-variables.patch
+ubuntu19/ext4-iget-with-flags.patch
+linux-5.14/export-ext4fs-dirhash-helper.patch
+linux-5.8/ext4-no-max-dir-size-limit-for-iam-objects.patch
+linux-5.14/ext4-ialloc-uid-gid-and-pass-owner-down.patch
+linux-5.14/ext4-projid-xattrs.patch
+base/ext4-delayed-iput.patch
+linux-5.14/ext4-xattr-disable-credits-check.patch