diff mbox series

[v3,09/11] btrfs: cleanup duplicated parameters related to btrfs_alloc_ordered_extent

Message ID 48c8d44318239d9b8ba7fe4906901529f1a5a2f5.1716440169.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: extent-map: unify the members with btrfs_ordered_extent | expand

Commit Message

Qu Wenruo May 23, 2024, 5:03 a.m. UTC
All parameters after @filepos of btrfs_alloc_ordered_extent() can be
replaced with btrfs_file_extent structure.

This patch does the cleanup, meanwhile some points to note:

- Move btrfs_file_extent structure to ordered-data.h
  The structure is needed by both btrfs_alloc_ordered_extent() and
  can_nocow_extent(), but since btrfs_inode.h includes
  ordered-data.h, so we need to move the structure to ordered-data.h.

- Move the special handling of NOCOW/PREALLOC into
  btrfs_alloc_ordered_extent()
  This is to allow btrfs_split_ordered_extent() to properly split them
  for DIO.
  For now just move the handling into btrfs_alloc_ordered_extent() to
  simplify the callers.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/btrfs_inode.h  | 14 -----------
 fs/btrfs/inode.c        | 56 ++++++++---------------------------------
 fs/btrfs/ordered-data.c | 34 ++++++++++++++++++++-----
 fs/btrfs/ordered-data.h | 19 +++++++++++---
 4 files changed, 54 insertions(+), 69 deletions(-)

Comments

Filipe Manana May 23, 2024, 6:17 p.m. UTC | #1
On Thu, May 23, 2024 at 6:04 AM Qu Wenruo <wqu@suse.com> wrote:
>
> All parameters after @filepos of btrfs_alloc_ordered_extent() can be
> replaced with btrfs_file_extent structure.
>
> This patch does the cleanup, meanwhile some points to note:
>
> - Move btrfs_file_extent structure to ordered-data.h
>   The structure is needed by both btrfs_alloc_ordered_extent() and
>   can_nocow_extent(), but since btrfs_inode.h includes
>   ordered-data.h, so we need to move the structure to ordered-data.h.
>
> - Move the special handling of NOCOW/PREALLOC into
>   btrfs_alloc_ordered_extent()
>   This is to allow btrfs_split_ordered_extent() to properly split them
>   for DIO.
>   For now just move the handling into btrfs_alloc_ordered_extent() to
>   simplify the callers.
>
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  fs/btrfs/btrfs_inode.h  | 14 -----------
>  fs/btrfs/inode.c        | 56 ++++++++---------------------------------
>  fs/btrfs/ordered-data.c | 34 ++++++++++++++++++++-----
>  fs/btrfs/ordered-data.h | 19 +++++++++++---
>  4 files changed, 54 insertions(+), 69 deletions(-)
>
> diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
> index dbc85efdf68a..97ce56a60672 100644
> --- a/fs/btrfs/btrfs_inode.h
> +++ b/fs/btrfs/btrfs_inode.h
> @@ -514,20 +514,6 @@ int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
>                             u32 pgoff, u8 *csum, const u8 * const csum_expected);
>  bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
>                         u32 bio_offset, struct bio_vec *bv);
> -
> -/*
> - * This represents details about the target file extent item of a write
> - * operation.
> - */
> -struct btrfs_file_extent {
> -       u64 disk_bytenr;
> -       u64 disk_num_bytes;
> -       u64 num_bytes;
> -       u64 ram_bytes;
> -       u64 offset;
> -       u8 compression;
> -};
> -
>  noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
>                               struct btrfs_file_extent *file_extent,
>                               bool nowait, bool strict);
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 445c19d96d10..35f03149b777 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -1220,14 +1220,8 @@ static void submit_one_async_extent(struct async_chunk *async_chunk,
>         }
>         free_extent_map(em);
>
> -       ordered = btrfs_alloc_ordered_extent(inode, start,      /* file_offset */
> -                                      async_extent->ram_size,  /* num_bytes */
> -                                      async_extent->ram_size,  /* ram_bytes */
> -                                      ins.objectid,            /* disk_bytenr */
> -                                      ins.offset,              /* disk_num_bytes */
> -                                      0,                       /* offset */
> -                                      1 << BTRFS_ORDERED_COMPRESSED,
> -                                      async_extent->compress_type);
> +       ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
> +                                      1 << BTRFS_ORDERED_COMPRESSED);
>         if (IS_ERR(ordered)) {
>                 btrfs_drop_extent_map_range(inode, start, end, false);
>                 ret = PTR_ERR(ordered);
> @@ -1463,10 +1457,8 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
>                 }
>                 free_extent_map(em);
>
> -               ordered = btrfs_alloc_ordered_extent(inode, start, ram_size,
> -                                       ram_size, ins.objectid, cur_alloc_size,
> -                                       0, 1 << BTRFS_ORDERED_REGULAR,
> -                                       BTRFS_COMPRESS_NONE);
> +               ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
> +                                                    1 << BTRFS_ORDERED_REGULAR);
>                 if (IS_ERR(ordered)) {
>                         unlock_extent(&inode->io_tree, start,
>                                       start + ram_size - 1, &cached);
> @@ -2191,15 +2183,10 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
>                 }
>
>                 ordered = btrfs_alloc_ordered_extent(inode, cur_offset,
> -                               nocow_args.file_extent.num_bytes,
> -                               nocow_args.file_extent.num_bytes,
> -                               nocow_args.file_extent.disk_bytenr +
> -                               nocow_args.file_extent.offset,
> -                               nocow_args.file_extent.num_bytes, 0,
> +                               &nocow_args.file_extent,
>                                 is_prealloc
>                                 ? (1 << BTRFS_ORDERED_PREALLOC)
> -                               : (1 << BTRFS_ORDERED_NOCOW),
> -                               BTRFS_COMPRESS_NONE);
> +                               : (1 << BTRFS_ORDERED_NOCOW));
>                 btrfs_dec_nocow_writers(nocow_bg);
>                 if (IS_ERR(ordered)) {
>                         if (is_prealloc) {
> @@ -7054,29 +7041,9 @@ static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
>                         goto out;
>         }
>
> -       /*
> -        * For regular writes, file_extent->offset is always 0,
> -        * thus we really only need file_extent->disk_bytenr, every other length
> -        * (disk_num_bytes/ram_bytes) should match @len and
> -        * file_extent->num_bytes.
> -        *
> -        * For NOCOW, we don't really care about the numbers except
> -        * @start and @len, as we won't insert a file extent
> -        * item at all.
> -        *
> -        * For PREALLOC, we do not use ordered extent members, but
> -        * btrfs_mark_extent_written() handles everything.
> -        *
> -        * So here we always passing 0 as offset for the ordered extent,
> -        * or btrfs_split_ordered_extent() can not handle it correctly.
> -        */
> -       ordered = btrfs_alloc_ordered_extent(inode, start, len, len,
> -                                            file_extent->disk_bytenr +
> -                                            file_extent->offset,
> -                                            len, 0,
> +       ordered = btrfs_alloc_ordered_extent(inode, start, file_extent,
>                                              (1 << type) |
> -                                            (1 << BTRFS_ORDERED_DIRECT),
> -                                            BTRFS_COMPRESS_NONE);
> +                                            (1 << BTRFS_ORDERED_DIRECT));
>         if (IS_ERR(ordered)) {
>                 if (em) {
>                         free_extent_map(em);
> @@ -10396,12 +10363,9 @@ ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
>         }
>         free_extent_map(em);
>
> -       ordered = btrfs_alloc_ordered_extent(inode, start, num_bytes, ram_bytes,
> -                                      ins.objectid, ins.offset,
> -                                      encoded->unencoded_offset,
> +       ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
>                                        (1 << BTRFS_ORDERED_ENCODED) |
> -                                      (1 << BTRFS_ORDERED_COMPRESSED),
> -                                      compression);
> +                                      (1 << BTRFS_ORDERED_COMPRESSED));
>         if (IS_ERR(ordered)) {
>                 btrfs_drop_extent_map_range(inode, start, end, false);
>                 ret = PTR_ERR(ordered);
> diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
> index d446d89c2c34..5c2fb0a7c5c8 100644
> --- a/fs/btrfs/ordered-data.c
> +++ b/fs/btrfs/ordered-data.c
> @@ -264,17 +264,39 @@ static void insert_ordered_extent(struct btrfs_ordered_extent *entry)
>   */
>  struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
>                         struct btrfs_inode *inode, u64 file_offset,
> -                       u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
> -                       u64 disk_num_bytes, u64 offset, unsigned long flags,
> -                       int compress_type)
> +                       struct btrfs_file_extent *file_extent,

Btw, this can be made const.

> +                       unsigned long flags)
>  {
>         struct btrfs_ordered_extent *entry;
>
>         ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
>
> -       entry = alloc_ordered_extent(inode, file_offset, num_bytes, ram_bytes,
> -                                    disk_bytenr, disk_num_bytes, offset, flags,
> -                                    compress_type);
> +       /*
> +        * For regular writes, we just use the members in @file_extent.
> +        *
> +        * For NOCOW, we don't really care about the numbers except
> +        * @start and file_extent->num_bytes, as we won't insert a file extent
> +        * item at all.
> +        *
> +        * For PREALLOC, we do not use ordered extent members, but
> +        * btrfs_mark_extent_written() handles everything.
> +        *
> +        * So here we always passing 0 as offset for NOCOW/PREALLOC ordered
> +        * extents, or btrfs_split_ordered_extent() can not handle it correctly.
> +        */
> +       if (flags & ((1 << BTRFS_ORDERED_NOCOW) | (1 << BTRFS_ORDERED_PREALLOC)))
> +               entry = alloc_ordered_extent(inode, file_offset,
> +                               file_extent->num_bytes, file_extent->num_bytes,
> +                               file_extent->disk_bytenr + file_extent->offset,
> +                               file_extent->num_bytes, 0, flags,
> +                               file_extent->compression);
> +       else
> +               entry = alloc_ordered_extent(inode, file_offset,
> +                               file_extent->num_bytes, file_extent->ram_bytes,
> +                               file_extent->disk_bytenr,
> +                               file_extent->disk_num_bytes,
> +                               file_extent->offset, flags,
> +                               file_extent->compression);
>         if (!IS_ERR(entry))
>                 insert_ordered_extent(entry);
>         return entry;
> diff --git a/fs/btrfs/ordered-data.h b/fs/btrfs/ordered-data.h
> index 2ec329e2f0f3..31e65f2f4990 100644
> --- a/fs/btrfs/ordered-data.h
> +++ b/fs/btrfs/ordered-data.h
> @@ -171,11 +171,24 @@ void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
>  bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
>                                     struct btrfs_ordered_extent **cached,
>                                     u64 file_offset, u64 io_size);
> +
> +/*
> + * This represents details about the target file extent item of a write
> + * operation.
> + */
> +struct btrfs_file_extent {
> +       u64 disk_bytenr;
> +       u64 disk_num_bytes;
> +       u64 num_bytes;
> +       u64 ram_bytes;
> +       u64 offset;
> +       u8 compression;
> +};
> +
>  struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
>                         struct btrfs_inode *inode, u64 file_offset,
> -                       u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
> -                       u64 disk_num_bytes, u64 offset, unsigned long flags,
> -                       int compress_type);
> +                       struct btrfs_file_extent *file_extent,

Same here, const.

Otherwise it looks good, thanks.

Reviewed-by: Filipe Manana <fdmanana@suse.com>

> +                       unsigned long flags);
>  void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
>                            struct btrfs_ordered_sum *sum);
>  struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
> --
> 2.45.1
>
>
diff mbox series

Patch

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index dbc85efdf68a..97ce56a60672 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -514,20 +514,6 @@  int btrfs_check_sector_csum(struct btrfs_fs_info *fs_info, struct page *page,
 			    u32 pgoff, u8 *csum, const u8 * const csum_expected);
 bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev,
 			u32 bio_offset, struct bio_vec *bv);
-
-/*
- * This represents details about the target file extent item of a write
- * operation.
- */
-struct btrfs_file_extent {
-	u64 disk_bytenr;
-	u64 disk_num_bytes;
-	u64 num_bytes;
-	u64 ram_bytes;
-	u64 offset;
-	u8 compression;
-};
-
 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
 			      struct btrfs_file_extent *file_extent,
 			      bool nowait, bool strict);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 445c19d96d10..35f03149b777 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1220,14 +1220,8 @@  static void submit_one_async_extent(struct async_chunk *async_chunk,
 	}
 	free_extent_map(em);
 
-	ordered = btrfs_alloc_ordered_extent(inode, start,	/* file_offset */
-				       async_extent->ram_size,	/* num_bytes */
-				       async_extent->ram_size,	/* ram_bytes */
-				       ins.objectid,		/* disk_bytenr */
-				       ins.offset,		/* disk_num_bytes */
-				       0,			/* offset */
-				       1 << BTRFS_ORDERED_COMPRESSED,
-				       async_extent->compress_type);
+	ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
+				       1 << BTRFS_ORDERED_COMPRESSED);
 	if (IS_ERR(ordered)) {
 		btrfs_drop_extent_map_range(inode, start, end, false);
 		ret = PTR_ERR(ordered);
@@ -1463,10 +1457,8 @@  static noinline int cow_file_range(struct btrfs_inode *inode,
 		}
 		free_extent_map(em);
 
-		ordered = btrfs_alloc_ordered_extent(inode, start, ram_size,
-					ram_size, ins.objectid, cur_alloc_size,
-					0, 1 << BTRFS_ORDERED_REGULAR,
-					BTRFS_COMPRESS_NONE);
+		ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
+						     1 << BTRFS_ORDERED_REGULAR);
 		if (IS_ERR(ordered)) {
 			unlock_extent(&inode->io_tree, start,
 				      start + ram_size - 1, &cached);
@@ -2191,15 +2183,10 @@  static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
 		}
 
 		ordered = btrfs_alloc_ordered_extent(inode, cur_offset,
-				nocow_args.file_extent.num_bytes,
-				nocow_args.file_extent.num_bytes,
-				nocow_args.file_extent.disk_bytenr +
-				nocow_args.file_extent.offset,
-				nocow_args.file_extent.num_bytes, 0,
+				&nocow_args.file_extent,
 				is_prealloc
 				? (1 << BTRFS_ORDERED_PREALLOC)
-				: (1 << BTRFS_ORDERED_NOCOW),
-				BTRFS_COMPRESS_NONE);
+				: (1 << BTRFS_ORDERED_NOCOW));
 		btrfs_dec_nocow_writers(nocow_bg);
 		if (IS_ERR(ordered)) {
 			if (is_prealloc) {
@@ -7054,29 +7041,9 @@  static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
 			goto out;
 	}
 
-	/*
-	 * For regular writes, file_extent->offset is always 0,
-	 * thus we really only need file_extent->disk_bytenr, every other length
-	 * (disk_num_bytes/ram_bytes) should match @len and
-	 * file_extent->num_bytes.
-	 *
-	 * For NOCOW, we don't really care about the numbers except
-	 * @start and @len, as we won't insert a file extent
-	 * item at all.
-	 *
-	 * For PREALLOC, we do not use ordered extent members, but
-	 * btrfs_mark_extent_written() handles everything.
-	 *
-	 * So here we always passing 0 as offset for the ordered extent,
-	 * or btrfs_split_ordered_extent() can not handle it correctly.
-	 */
-	ordered = btrfs_alloc_ordered_extent(inode, start, len, len,
-					     file_extent->disk_bytenr +
-					     file_extent->offset,
-					     len, 0,
+	ordered = btrfs_alloc_ordered_extent(inode, start, file_extent,
 					     (1 << type) |
-					     (1 << BTRFS_ORDERED_DIRECT),
-					     BTRFS_COMPRESS_NONE);
+					     (1 << BTRFS_ORDERED_DIRECT));
 	if (IS_ERR(ordered)) {
 		if (em) {
 			free_extent_map(em);
@@ -10396,12 +10363,9 @@  ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
 	}
 	free_extent_map(em);
 
-	ordered = btrfs_alloc_ordered_extent(inode, start, num_bytes, ram_bytes,
-				       ins.objectid, ins.offset,
-				       encoded->unencoded_offset,
+	ordered = btrfs_alloc_ordered_extent(inode, start, &file_extent,
 				       (1 << BTRFS_ORDERED_ENCODED) |
-				       (1 << BTRFS_ORDERED_COMPRESSED),
-				       compression);
+				       (1 << BTRFS_ORDERED_COMPRESSED));
 	if (IS_ERR(ordered)) {
 		btrfs_drop_extent_map_range(inode, start, end, false);
 		ret = PTR_ERR(ordered);
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index d446d89c2c34..5c2fb0a7c5c8 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -264,17 +264,39 @@  static void insert_ordered_extent(struct btrfs_ordered_extent *entry)
  */
 struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
 			struct btrfs_inode *inode, u64 file_offset,
-			u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
-			u64 disk_num_bytes, u64 offset, unsigned long flags,
-			int compress_type)
+			struct btrfs_file_extent *file_extent,
+			unsigned long flags)
 {
 	struct btrfs_ordered_extent *entry;
 
 	ASSERT((flags & ~BTRFS_ORDERED_TYPE_FLAGS) == 0);
 
-	entry = alloc_ordered_extent(inode, file_offset, num_bytes, ram_bytes,
-				     disk_bytenr, disk_num_bytes, offset, flags,
-				     compress_type);
+	/*
+	 * For regular writes, we just use the members in @file_extent.
+	 *
+	 * For NOCOW, we don't really care about the numbers except
+	 * @start and file_extent->num_bytes, as we won't insert a file extent
+	 * item at all.
+	 *
+	 * For PREALLOC, we do not use ordered extent members, but
+	 * btrfs_mark_extent_written() handles everything.
+	 *
+	 * So here we always passing 0 as offset for NOCOW/PREALLOC ordered
+	 * extents, or btrfs_split_ordered_extent() can not handle it correctly.
+	 */
+	if (flags & ((1 << BTRFS_ORDERED_NOCOW) | (1 << BTRFS_ORDERED_PREALLOC)))
+		entry = alloc_ordered_extent(inode, file_offset,
+				file_extent->num_bytes, file_extent->num_bytes,
+				file_extent->disk_bytenr + file_extent->offset,
+				file_extent->num_bytes, 0, flags,
+				file_extent->compression);
+	else
+		entry = alloc_ordered_extent(inode, file_offset,
+				file_extent->num_bytes, file_extent->ram_bytes,
+				file_extent->disk_bytenr,
+				file_extent->disk_num_bytes,
+				file_extent->offset, flags,
+				file_extent->compression);
 	if (!IS_ERR(entry))
 		insert_ordered_extent(entry);
 	return entry;
diff --git a/fs/btrfs/ordered-data.h b/fs/btrfs/ordered-data.h
index 2ec329e2f0f3..31e65f2f4990 100644
--- a/fs/btrfs/ordered-data.h
+++ b/fs/btrfs/ordered-data.h
@@ -171,11 +171,24 @@  void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode,
 bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
 				    struct btrfs_ordered_extent **cached,
 				    u64 file_offset, u64 io_size);
+
+/*
+ * This represents details about the target file extent item of a write
+ * operation.
+ */
+struct btrfs_file_extent {
+	u64 disk_bytenr;
+	u64 disk_num_bytes;
+	u64 num_bytes;
+	u64 ram_bytes;
+	u64 offset;
+	u8 compression;
+};
+
 struct btrfs_ordered_extent *btrfs_alloc_ordered_extent(
 			struct btrfs_inode *inode, u64 file_offset,
-			u64 num_bytes, u64 ram_bytes, u64 disk_bytenr,
-			u64 disk_num_bytes, u64 offset, unsigned long flags,
-			int compress_type);
+			struct btrfs_file_extent *file_extent,
+			unsigned long flags);
 void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
 			   struct btrfs_ordered_sum *sum);
 struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,