return em;
 }
 
-static u32 get_extent_max_capacity(const struct extent_map *em)
+static u32 get_extent_max_capacity(const struct btrfs_fs_info *fs_info,
+                                  const struct extent_map *em)
 {
        if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
                return BTRFS_MAX_COMPRESSED;
-       return BTRFS_MAX_EXTENT_SIZE;
+       return fs_info->max_extent_size;
 }
 
 static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
                                     u32 extent_thresh, u64 newer_than, bool locked)
 {
+       struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
        struct extent_map *next;
        bool ret = false;
 
         * If the next extent is at its max capacity, defragging current extent
         * makes no sense, as the total number of extents won't change.
         */
-       if (next->len >= get_extent_max_capacity(em))
+       if (next->len >= get_extent_max_capacity(fs_info, em))
                goto out;
        /* Skip older extent */
        if (next->generation < newer_than)
                                  bool locked, struct list_head *target_list,
                                  u64 *last_scanned_ret)
 {
+       struct btrfs_fs_info *fs_info = inode->root->fs_info;
        bool last_is_target = false;
        u64 cur = start;
        int ret = 0;
                 * Skip extents already at its max capacity, this is mostly for
                 * compressed extents, which max cap is only 128K.
                 */
-               if (em->len >= get_extent_max_capacity(em))
+               if (em->len >= get_extent_max_capacity(fs_info, em))
                        goto next;
 
                /*