--- /dev/null
+From 30479f31d44d47ed00ae0c7453d9b253537005b2 Mon Sep 17 00:00:00 2001
+From: Boris Burkov <boris@bur.io>
+Date: Fri, 19 Jul 2024 16:49:08 -0700
+Subject: btrfs: fix qgroup reserve leaks in cow_file_range
+
+From: Boris Burkov <boris@bur.io>
+
+commit 30479f31d44d47ed00ae0c7453d9b253537005b2 upstream.
+
+In the buffered write path, the dirty page owns the qgroup reserve until
+it creates an ordered_extent.
+
+Therefore, any errors that occur before the ordered_extent is created
+must free that reservation, or else the space is leaked. The fstest
+generic/475 exercises various IO error paths, and is able to trigger
+errors in cow_file_range where we fail to get to allocating the ordered
+extent. Note that because we *do* clear delalloc, we are likely to
+remove the inode from the delalloc list, so the inodes/pages to not have
+invalidate/launder called on them in the commit abort path.
+
+This results in failures at the unmount stage of the test that look like:
+
+ BTRFS: error (device dm-8 state EA) in cleanup_transaction:2018: errno=-5 IO failure
+ BTRFS: error (device dm-8 state EA) in btrfs_replace_file_extents:2416: errno=-5 IO failure
+ BTRFS warning (device dm-8 state EA): qgroup 0/5 has unreleased space, type 0 rsv 28672
+ ------------[ cut here ]------------
+ WARNING: CPU: 3 PID: 22588 at fs/btrfs/disk-io.c:4333 close_ctree+0x222/0x4d0 [btrfs]
+ Modules linked in: btrfs blake2b_generic libcrc32c xor zstd_compress raid6_pq
+ CPU: 3 PID: 22588 Comm: umount Kdump: loaded Tainted: G W 6.10.0-rc7-gab56fde445b8 #21
+ Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Arch Linux 1.16.3-1-1 04/01/2014
+ RIP: 0010:close_ctree+0x222/0x4d0 [btrfs]
+ RSP: 0018:ffffb4465283be00 EFLAGS: 00010202
+ RAX: 0000000000000001 RBX: ffffa1a1818e1000 RCX: 0000000000000001
+ RDX: 0000000000000000 RSI: ffffb4465283bbe0 RDI: ffffa1a19374fcb8
+ RBP: ffffa1a1818e13c0 R08: 0000000100028b16 R09: 0000000000000000
+ R10: 0000000000000003 R11: 0000000000000003 R12: ffffa1a18ad7972c
+ R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
+ FS: 00007f9168312b80(0000) GS:ffffa1a4afcc0000(0000) knlGS:0000000000000000
+ CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
+ CR2: 00007f91683c9140 CR3: 000000010acaa000 CR4: 00000000000006f0
+ Call Trace:
+ <TASK>
+ ? close_ctree+0x222/0x4d0 [btrfs]
+ ? __warn.cold+0x8e/0xea
+ ? close_ctree+0x222/0x4d0 [btrfs]
+ ? report_bug+0xff/0x140
+ ? handle_bug+0x3b/0x70
+ ? exc_invalid_op+0x17/0x70
+ ? asm_exc_invalid_op+0x1a/0x20
+ ? close_ctree+0x222/0x4d0 [btrfs]
+ generic_shutdown_super+0x70/0x160
+ kill_anon_super+0x11/0x40
+ btrfs_kill_super+0x11/0x20 [btrfs]
+ deactivate_locked_super+0x2e/0xa0
+ cleanup_mnt+0xb5/0x150
+ task_work_run+0x57/0x80
+ syscall_exit_to_user_mode+0x121/0x130
+ do_syscall_64+0xab/0x1a0
+ entry_SYSCALL_64_after_hwframe+0x77/0x7f
+ RIP: 0033:0x7f916847a887
+ ---[ end trace 0000000000000000 ]---
+ BTRFS error (device dm-8 state EA): qgroup reserved space leaked
+
+Cases 2 and 3 in the out_reserve path both pertain to this type of leak
+and must free the reserved qgroup data. Because it is already an error
+path, I opted not to handle the possible errors in
+btrfs_free_qgroup_data.
+
+Reviewed-by: Qu Wenruo <wqu@suse.com>
+Signed-off-by: Boris Burkov <boris@bur.io>
+Signed-off-by: David Sterba <dsterba@suse.com>
+[Minor conflict resolved due to code context change.]
+Signed-off-by: Jianqi Ren <jianqi.ren.cn@windriver.com>
+Signed-off-by: He Zhe <zhe.he@windriver.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ fs/btrfs/inode.c | 6 +++++-
+ 1 file changed, 5 insertions(+), 1 deletion(-)
+
+--- a/fs/btrfs/inode.c
++++ b/fs/btrfs/inode.c
+@@ -1428,6 +1428,7 @@ out_unlock:
+ locked_page,
+ clear_bits,
+ page_ops);
++ btrfs_qgroup_free_data(inode, NULL, start, cur_alloc_size, NULL);
+ start += cur_alloc_size;
+ }
+
+@@ -1441,6 +1442,7 @@ out_unlock:
+ clear_bits |= EXTENT_CLEAR_DATA_RESV;
+ extent_clear_unlock_delalloc(inode, start, end, locked_page,
+ clear_bits, page_ops);
++ btrfs_qgroup_free_data(inode, NULL, start, cur_alloc_size, NULL);
+ }
+ return ret;
+ }
+@@ -2168,13 +2170,15 @@ error:
+ if (nocow)
+ btrfs_dec_nocow_writers(bg);
+
+- if (ret && cur_offset < end)
++ if (ret && cur_offset < end) {
+ extent_clear_unlock_delalloc(inode, cur_offset, end,
+ locked_page, EXTENT_LOCKED |
+ EXTENT_DELALLOC | EXTENT_DEFRAG |
+ EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
+ PAGE_START_WRITEBACK |
+ PAGE_END_WRITEBACK);
++ btrfs_qgroup_free_data(inode, NULL, cur_offset, end - cur_offset + 1, NULL);
++ }
+ btrfs_free_path(path);
+ return ret;
+ }
--- /dev/null
+From 2bbc4a45e5eb6b868357c1045bf6f38f6ba576e0 Mon Sep 17 00:00:00 2001
+From: Johannes Thumshirn <johannes.thumshirn@wdc.com>
+Date: Mon, 17 Mar 2025 12:24:58 +0100
+Subject: btrfs: zoned: fix zone activation with missing devices
+
+From: Johannes Thumshirn <johannes.thumshirn@wdc.com>
+
+commit 2bbc4a45e5eb6b868357c1045bf6f38f6ba576e0 upstream.
+
+If btrfs_zone_activate() is called with a filesystem that has missing
+devices (e.g. a RAID file system mounted in degraded mode) it is accessing
+the btrfs_device::zone_info pointer, which will not be set if the device in
+question is missing.
+
+Check if the device is present (by checking if it has a valid block
+device pointer associated) and if not, skip zone activation for it.
+
+Fixes: f9a912a3c45f ("btrfs: zoned: make zone activation multi stripe capable")
+CC: stable@vger.kernel.org # 6.1+
+Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>
+Reviewed-by: Anand Jain <anand.jain@oracle.com>
+Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
+Reviewed-by: David Sterba <dsterba@suse.com>
+Signed-off-by: David Sterba <dsterba@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ fs/btrfs/zoned.c | 3 +++
+ 1 file changed, 3 insertions(+)
+
+--- a/fs/btrfs/zoned.c
++++ b/fs/btrfs/zoned.c
+@@ -1909,6 +1909,9 @@ bool btrfs_zone_activate(struct btrfs_bl
+ device = map->stripes[i].dev;
+ physical = map->stripes[i].physical;
+
++ if (!device->bdev)
++ continue;
++
+ if (device->zone_info->max_active_zones == 0)
+ continue;
+
--- /dev/null
+From 35fec1089ebb5617f85884d3fa6a699ce6337a75 Mon Sep 17 00:00:00 2001
+From: Johannes Thumshirn <johannes.thumshirn@wdc.com>
+Date: Mon, 17 Mar 2025 12:24:59 +0100
+Subject: btrfs: zoned: fix zone finishing with missing devices
+
+From: Johannes Thumshirn <johannes.thumshirn@wdc.com>
+
+commit 35fec1089ebb5617f85884d3fa6a699ce6337a75 upstream.
+
+If do_zone_finish() is called with a filesystem that has missing devices
+(e.g. a RAID file system mounted in degraded mode) it is accessing the
+btrfs_device::zone_info pointer, which will not be set if the device
+in question is missing.
+
+Check if the device is present (by checking if it has a valid block device
+pointer associated) and if not, skip zone finishing for it.
+
+Fixes: 4dcbb8ab31c1 ("btrfs: zoned: make zone finishing multi stripe capable")
+CC: stable@vger.kernel.org # 6.1+
+Reviewed-by: Naohiro Aota <naohiro.aota@wdc.com>
+Reviewed-by: Anand Jain <anand.jain@oracle.com>
+Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
+Reviewed-by: David Sterba <dsterba@suse.com>
+Signed-off-by: David Sterba <dsterba@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ fs/btrfs/zoned.c | 3 +++
+ 1 file changed, 3 insertions(+)
+
+--- a/fs/btrfs/zoned.c
++++ b/fs/btrfs/zoned.c
+@@ -2055,6 +2055,9 @@ static int do_zone_finish(struct btrfs_b
+ struct btrfs_device *device = map->stripes[i].dev;
+ const u64 physical = map->stripes[i].physical;
+
++ if (!device->bdev)
++ continue;
++
+ if (device->zone_info->max_active_zones == 0)
+ continue;
+
--- /dev/null
+From a0d3c8bcb9206ac207c7ad3182027c6b0a1319bb Mon Sep 17 00:00:00 2001
+From: Yuli Wang <wangyuli@uniontech.com>
+Date: Sat, 8 Mar 2025 13:51:32 +0800
+Subject: LoongArch: Eliminate superfluous get_numa_distances_cnt()
+
+From: Yuli Wang <wangyuli@uniontech.com>
+
+commit a0d3c8bcb9206ac207c7ad3182027c6b0a1319bb upstream.
+
+In LoongArch, get_numa_distances_cnt() isn't in use, resulting in a
+compiler warning.
+
+Fix follow errors with clang-18 when W=1e:
+
+arch/loongarch/kernel/acpi.c:259:28: error: unused function 'get_numa_distances_cnt' [-Werror,-Wunused-function]
+ 259 | static inline unsigned int get_numa_distances_cnt(struct acpi_table_slit *slit)
+ | ^~~~~~~~~~~~~~~~~~~~~~
+1 error generated.
+
+Link: https://lore.kernel.org/all/Z7bHPVUH4lAezk0E@kernel.org/
+Signed-off-by: Yuli Wang <wangyuli@uniontech.com>
+Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ arch/loongarch/kernel/acpi.c | 12 ------------
+ 1 file changed, 12 deletions(-)
+
+--- a/arch/loongarch/kernel/acpi.c
++++ b/arch/loongarch/kernel/acpi.c
+@@ -173,18 +173,6 @@ static __init int setup_node(int pxm)
+ return acpi_map_pxm_to_node(pxm);
+ }
+
+-/*
+- * Callback for SLIT parsing. pxm_to_node() returns NUMA_NO_NODE for
+- * I/O localities since SRAT does not list them. I/O localities are
+- * not supported at this point.
+- */
+-unsigned int numa_distance_cnt;
+-
+-static inline unsigned int get_numa_distances_cnt(struct acpi_table_slit *slit)
+-{
+- return slit->locality_count;
+-}
+-
+ void __init numa_set_distance(int from, int to, int distance)
+ {
+ if ((u8)distance != distance || (from == to && distance != LOCAL_DISTANCE)) {