Skip to contentSkip to conversation

btrfs: Use list_last_entry in add_falloc_range

PATCH In review Nikolay Borisov 2 participants 2 messages May 31, 2021 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: Use list_last_entry in add_falloc_range

Instead of calling list_entry with head->prev simply call list_last_entry which makes it obvious which member of the list is being referred. This allows to remove the extra 'prev' pointer.

---
 fs/btrfs/file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/btrfs/file.c +3 -4
index e910cc2cd45c..2b28a3daa5a9 100644
@@ -3034,7 +3034,6 @@ struct falloc_range {
  */
 static int add_falloc_range(struct list_head *head, u64 start, u64 len)
 {
-	struct falloc_range *prev = NULL;
 	struct falloc_range *range = NULL;
 
 	if (list_empty(head))
@@ -3044,9 +3043,9 @@ static int add_falloc_range(struct list_head *head, u64 start, u64 len)
 	 * As fallocate iterate by bytenr order, we only need to check
 	 * the last range.
 	 */
-	prev = list_entry(head->prev, struct falloc_range, list);
-	if (prev->start + prev->len == start) {
-		prev->len += len;
+	range = list_last_entry(head, struct falloc_range, list);
+	if (range->start + range->len == start) {
+		range->len += len;
 		return 0;
 	}
 insert:
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Nikolay Borisov

Instead of calling list_entry with head->prev simply call list_last_entry which makes it obvious which member of the list is being referred. This allows to remove the extra 'prev' pointer.

Added to misc-next, thanks.

Quoted 19 lines
---
 fs/btrfs/file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/btrfs/file.c +0 -1
index e910cc2cd45c..2b28a3daa5a9 100644
@@ -3034,7 +3034,6 @@ struct falloc_range {
  */
 static int add_falloc_range(struct list_head *head, u64 start, u64 len)
 {
-	struct falloc_range *prev = NULL;
 	struct falloc_range *range = NULL;
 
 	if (list_empty(head))
@@ -3044,9 +3043,9 @@ static int add_falloc_range(struct list_head *head, u64 start, u64 len)
 	 * As fallocate iterate by bytenr order, we only need to check
 	 * the last range.

fs/btrfs/file.c static int add_falloc_range(struct list_head *head, u64 start, u64 len) · line 3051

Quoted 10 lines
fs/btrfs/file.c +3 -3
 	 */
-	prev = list_entry(head->prev, struct falloc_range, list);
-	if (prev->start + prev->len == start) {
-		prev->len += len;
+	range = list_last_entry(head, struct falloc_range, list);
+	if (range->start + range->len == start) {
+		range->len += len;
 		return 0;
 	}
 insert:

The function could be restructured a bit to get rid of the insert:
label, like:

	if (!list_empty(head)) {
		range = list_last(...)
		if (range->start ...) {
			return;
		}
	}
	range = kmalloc(...)
	<the rest>