1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023-2025 Christoph Hellwig.
4  * Copyright (c) 2024-2025, Western Digital Corporation or its affiliates.
5  */
6 #include "xfs.h"
7 #include "xfs_shared.h"
8 #include "xfs_format.h"
9 #include "xfs_trans_resv.h"
10 #include "xfs_mount.h"
11 #include "xfs_inode.h"
12 #include "xfs_rtgroup.h"
13 #include "xfs_zone_alloc.h"
14 #include "xfs_zone_priv.h"
15 
16 static const char xfs_write_hint_shorthand[6][16] = {
17 	"NOT_SET", "NONE", "SHORT", "MEDIUM", "LONG", "EXTREME"};
18 
19 static inline const char *
xfs_write_hint_to_str(uint8_t write_hint)20 xfs_write_hint_to_str(
21 	uint8_t			write_hint)
22 {
23 	if (write_hint > WRITE_LIFE_EXTREME)
24 		return "UNKNOWN";
25 	return xfs_write_hint_shorthand[write_hint];
26 }
27 
28 static void
xfs_show_open_zone(struct seq_file * m,struct xfs_open_zone * oz)29 xfs_show_open_zone(
30 	struct seq_file		*m,
31 	struct xfs_open_zone	*oz)
32 {
33 	seq_printf(m, "\t  zone %d, wp %u, written %u, used %u, hint %s\n",
34 		rtg_rgno(oz->oz_rtg),
35 		oz->oz_write_pointer, oz->oz_written,
36 		rtg_rmap(oz->oz_rtg)->i_used_blocks,
37 		xfs_write_hint_to_str(oz->oz_write_hint));
38 }
39 
40 static void
xfs_show_full_zone_used_distribution(struct seq_file * m,struct xfs_mount * mp)41 xfs_show_full_zone_used_distribution(
42 	struct seq_file         *m,
43 	struct xfs_mount        *mp)
44 {
45 	struct xfs_zone_info	*zi = mp->m_zone_info;
46 	unsigned int		reclaimable = 0, full, i;
47 
48 	spin_lock(&zi->zi_used_buckets_lock);
49 	for (i = 0; i < XFS_ZONE_USED_BUCKETS; i++) {
50 		unsigned int entries = zi->zi_used_bucket_entries[i];
51 
52 		seq_printf(m, "\t  %2u..%2u%%: %u\n",
53 				i * (100 / XFS_ZONE_USED_BUCKETS),
54 				(i + 1) * (100 / XFS_ZONE_USED_BUCKETS) - 1,
55 				entries);
56 		reclaimable += entries;
57 	}
58 	spin_unlock(&zi->zi_used_buckets_lock);
59 
60 	full = mp->m_sb.sb_rgcount;
61 	if (zi->zi_open_gc_zone)
62 		full--;
63 	full -= zi->zi_nr_open_zones;
64 	full -= atomic_read(&zi->zi_nr_free_zones);
65 	full -= reclaimable;
66 
67 	seq_printf(m, "\t     100%%: %u\n", full);
68 }
69 
70 void
xfs_zoned_show_stats(struct seq_file * m,struct xfs_mount * mp)71 xfs_zoned_show_stats(
72 	struct seq_file		*m,
73 	struct xfs_mount	*mp)
74 {
75 	struct xfs_zone_info	*zi = mp->m_zone_info;
76 	struct xfs_open_zone	*oz;
77 
78 	seq_puts(m, "\n");
79 
80 	seq_printf(m, "\tuser free RT blocks: %lld\n",
81 		xfs_sum_freecounter(mp, XC_FREE_RTEXTENTS));
82 	seq_printf(m, "\treserved free RT blocks: %lld\n",
83 		mp->m_free[XC_FREE_RTEXTENTS].res_avail);
84 	seq_printf(m, "\tuser available RT blocks: %lld\n",
85 		xfs_sum_freecounter(mp, XC_FREE_RTAVAILABLE));
86 	seq_printf(m, "\treserved available RT blocks: %lld\n",
87 		mp->m_free[XC_FREE_RTAVAILABLE].res_avail);
88 	seq_printf(m, "\tRT reservations required: %d\n",
89 		!list_empty_careful(&zi->zi_reclaim_reservations));
90 	seq_printf(m, "\tRT GC required: %d\n",
91 		xfs_zoned_need_gc(mp));
92 
93 	seq_printf(m, "\tfree zones: %d\n", atomic_read(&zi->zi_nr_free_zones));
94 	seq_puts(m, "\topen zones:\n");
95 	spin_lock(&zi->zi_open_zones_lock);
96 	list_for_each_entry(oz, &zi->zi_open_zones, oz_entry)
97 		xfs_show_open_zone(m, oz);
98 	if (zi->zi_open_gc_zone) {
99 		seq_puts(m, "\topen gc zone:\n");
100 		xfs_show_open_zone(m, zi->zi_open_gc_zone);
101 	}
102 	spin_unlock(&zi->zi_open_zones_lock);
103 	seq_puts(m, "\tused blocks distribution (fully written zones):\n");
104 	xfs_show_full_zone_used_distribution(m, mp);
105 }
106