xref: /linux/fs/f2fs/debug.c (revision 145b04e5)
10a8165d7SJaegeuk Kim /*
2902829aaSGreg Kroah-Hartman  * f2fs debugging statistics
3902829aaSGreg Kroah-Hartman  *
4902829aaSGreg Kroah-Hartman  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5902829aaSGreg Kroah-Hartman  *             http://www.samsung.com/
6902829aaSGreg Kroah-Hartman  * Copyright (c) 2012 Linux Foundation
7902829aaSGreg Kroah-Hartman  * Copyright (c) 2012 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
8902829aaSGreg Kroah-Hartman  *
9902829aaSGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
10902829aaSGreg Kroah-Hartman  * it under the terms of the GNU General Public License version 2 as
11902829aaSGreg Kroah-Hartman  * published by the Free Software Foundation.
12902829aaSGreg Kroah-Hartman  */
13902829aaSGreg Kroah-Hartman 
14902829aaSGreg Kroah-Hartman #include <linux/fs.h>
15902829aaSGreg Kroah-Hartman #include <linux/backing-dev.h>
16902829aaSGreg Kroah-Hartman #include <linux/f2fs_fs.h>
17902829aaSGreg Kroah-Hartman #include <linux/blkdev.h>
18902829aaSGreg Kroah-Hartman #include <linux/debugfs.h>
19902829aaSGreg Kroah-Hartman #include <linux/seq_file.h>
20902829aaSGreg Kroah-Hartman 
21902829aaSGreg Kroah-Hartman #include "f2fs.h"
22902829aaSGreg Kroah-Hartman #include "node.h"
23902829aaSGreg Kroah-Hartman #include "segment.h"
24902829aaSGreg Kroah-Hartman #include "gc.h"
25902829aaSGreg Kroah-Hartman 
26902829aaSGreg Kroah-Hartman static LIST_HEAD(f2fs_stat_list);
27902829aaSGreg Kroah-Hartman static struct dentry *debugfs_root;
2866af62ceSmajianpeng static DEFINE_MUTEX(f2fs_stat_mutex);
29902829aaSGreg Kroah-Hartman 
3025ca923bSJaegeuk Kim static void update_general_status(struct f2fs_sb_info *sbi)
31902829aaSGreg Kroah-Hartman {
32902829aaSGreg Kroah-Hartman 	struct f2fs_stat_info *si = sbi->stat_info;
33902829aaSGreg Kroah-Hartman 	int i;
34902829aaSGreg Kroah-Hartman 
35902829aaSGreg Kroah-Hartman 	/* valid check of the segment numbers */
36902829aaSGreg Kroah-Hartman 	si->hit_ext = sbi->read_hit_ext;
37902829aaSGreg Kroah-Hartman 	si->total_ext = sbi->total_hit_ext;
38902829aaSGreg Kroah-Hartman 	si->ndirty_node = get_pages(sbi, F2FS_DIRTY_NODES);
39902829aaSGreg Kroah-Hartman 	si->ndirty_dent = get_pages(sbi, F2FS_DIRTY_DENTS);
40902829aaSGreg Kroah-Hartman 	si->ndirty_dirs = sbi->n_dirty_dirs;
41902829aaSGreg Kroah-Hartman 	si->ndirty_meta = get_pages(sbi, F2FS_DIRTY_META);
42902829aaSGreg Kroah-Hartman 	si->total_count = (int)sbi->user_block_count / sbi->blocks_per_seg;
43902829aaSGreg Kroah-Hartman 	si->rsvd_segs = reserved_segments(sbi);
44902829aaSGreg Kroah-Hartman 	si->overp_segs = overprovision_segments(sbi);
45902829aaSGreg Kroah-Hartman 	si->valid_count = valid_user_blocks(sbi);
46902829aaSGreg Kroah-Hartman 	si->valid_node_count = valid_node_count(sbi);
47902829aaSGreg Kroah-Hartman 	si->valid_inode_count = valid_inode_count(sbi);
48902829aaSGreg Kroah-Hartman 	si->utilization = utilization(sbi);
49902829aaSGreg Kroah-Hartman 
50902829aaSGreg Kroah-Hartman 	si->free_segs = free_segments(sbi);
51902829aaSGreg Kroah-Hartman 	si->free_secs = free_sections(sbi);
52902829aaSGreg Kroah-Hartman 	si->prefree_count = prefree_segments(sbi);
53902829aaSGreg Kroah-Hartman 	si->dirty_count = dirty_segments(sbi);
54902829aaSGreg Kroah-Hartman 	si->node_pages = sbi->node_inode->i_mapping->nrpages;
55902829aaSGreg Kroah-Hartman 	si->meta_pages = sbi->meta_inode->i_mapping->nrpages;
56902829aaSGreg Kroah-Hartman 	si->nats = NM_I(sbi)->nat_cnt;
57902829aaSGreg Kroah-Hartman 	si->sits = SIT_I(sbi)->dirty_sentries;
58902829aaSGreg Kroah-Hartman 	si->fnids = NM_I(sbi)->fcnt;
59902829aaSGreg Kroah-Hartman 	si->bg_gc = sbi->bg_gc;
60902829aaSGreg Kroah-Hartman 	si->util_free = (int)(free_user_blocks(sbi) >> sbi->log_blocks_per_seg)
61902829aaSGreg Kroah-Hartman 		* 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
62902829aaSGreg Kroah-Hartman 		/ 2;
63902829aaSGreg Kroah-Hartman 	si->util_valid = (int)(written_block_count(sbi) >>
64902829aaSGreg Kroah-Hartman 						sbi->log_blocks_per_seg)
65902829aaSGreg Kroah-Hartman 		* 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
66902829aaSGreg Kroah-Hartman 		/ 2;
67902829aaSGreg Kroah-Hartman 	si->util_invalid = 50 - si->util_free - si->util_valid;
68902829aaSGreg Kroah-Hartman 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_NODE; i++) {
69902829aaSGreg Kroah-Hartman 		struct curseg_info *curseg = CURSEG_I(sbi, i);
70902829aaSGreg Kroah-Hartman 		si->curseg[i] = curseg->segno;
71902829aaSGreg Kroah-Hartman 		si->cursec[i] = curseg->segno / sbi->segs_per_sec;
72902829aaSGreg Kroah-Hartman 		si->curzone[i] = si->cursec[i] / sbi->secs_per_zone;
73902829aaSGreg Kroah-Hartman 	}
74902829aaSGreg Kroah-Hartman 
75902829aaSGreg Kroah-Hartman 	for (i = 0; i < 2; i++) {
76902829aaSGreg Kroah-Hartman 		si->segment_count[i] = sbi->segment_count[i];
77902829aaSGreg Kroah-Hartman 		si->block_count[i] = sbi->block_count[i];
78902829aaSGreg Kroah-Hartman 	}
79902829aaSGreg Kroah-Hartman }
80902829aaSGreg Kroah-Hartman 
810a8165d7SJaegeuk Kim /*
82902829aaSGreg Kroah-Hartman  * This function calculates BDF of every segments
83902829aaSGreg Kroah-Hartman  */
84902829aaSGreg Kroah-Hartman static void update_sit_info(struct f2fs_sb_info *sbi)
85902829aaSGreg Kroah-Hartman {
86902829aaSGreg Kroah-Hartman 	struct f2fs_stat_info *si = sbi->stat_info;
87902829aaSGreg Kroah-Hartman 	unsigned int blks_per_sec, hblks_per_sec, total_vblocks, bimodal, dist;
88902829aaSGreg Kroah-Hartman 	struct sit_info *sit_i = SIT_I(sbi);
89902829aaSGreg Kroah-Hartman 	unsigned int segno, vblocks;
90902829aaSGreg Kroah-Hartman 	int ndirty = 0;
91902829aaSGreg Kroah-Hartman 
92902829aaSGreg Kroah-Hartman 	bimodal = 0;
93902829aaSGreg Kroah-Hartman 	total_vblocks = 0;
94902829aaSGreg Kroah-Hartman 	blks_per_sec = sbi->segs_per_sec * (1 << sbi->log_blocks_per_seg);
95902829aaSGreg Kroah-Hartman 	hblks_per_sec = blks_per_sec / 2;
96902829aaSGreg Kroah-Hartman 	mutex_lock(&sit_i->sentry_lock);
97902829aaSGreg Kroah-Hartman 	for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
98902829aaSGreg Kroah-Hartman 		vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec);
99902829aaSGreg Kroah-Hartman 		dist = abs(vblocks - hblks_per_sec);
100902829aaSGreg Kroah-Hartman 		bimodal += dist * dist;
101902829aaSGreg Kroah-Hartman 
102902829aaSGreg Kroah-Hartman 		if (vblocks > 0 && vblocks < blks_per_sec) {
103902829aaSGreg Kroah-Hartman 			total_vblocks += vblocks;
104902829aaSGreg Kroah-Hartman 			ndirty++;
105902829aaSGreg Kroah-Hartman 		}
106902829aaSGreg Kroah-Hartman 	}
107902829aaSGreg Kroah-Hartman 	mutex_unlock(&sit_i->sentry_lock);
10853cf9522SJaegeuk Kim 	dist = TOTAL_SECS(sbi) * hblks_per_sec * hblks_per_sec / 100;
109902829aaSGreg Kroah-Hartman 	si->bimodal = bimodal / dist;
110902829aaSGreg Kroah-Hartman 	if (si->dirty_count)
111902829aaSGreg Kroah-Hartman 		si->avg_vblocks = total_vblocks / ndirty;
112902829aaSGreg Kroah-Hartman 	else
113902829aaSGreg Kroah-Hartman 		si->avg_vblocks = 0;
114902829aaSGreg Kroah-Hartman }
115902829aaSGreg Kroah-Hartman 
1160a8165d7SJaegeuk Kim /*
117902829aaSGreg Kroah-Hartman  * This function calculates memory footprint.
118902829aaSGreg Kroah-Hartman  */
119902829aaSGreg Kroah-Hartman static void update_mem_info(struct f2fs_sb_info *sbi)
120902829aaSGreg Kroah-Hartman {
121902829aaSGreg Kroah-Hartman 	struct f2fs_stat_info *si = sbi->stat_info;
122902829aaSGreg Kroah-Hartman 	unsigned npages;
123902829aaSGreg Kroah-Hartman 
124902829aaSGreg Kroah-Hartman 	if (si->base_mem)
125902829aaSGreg Kroah-Hartman 		goto get_cache;
126902829aaSGreg Kroah-Hartman 
127902829aaSGreg Kroah-Hartman 	si->base_mem = sizeof(struct f2fs_sb_info) + sbi->sb->s_blocksize;
128902829aaSGreg Kroah-Hartman 	si->base_mem += 2 * sizeof(struct f2fs_inode_info);
129902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(*sbi->ckpt);
130902829aaSGreg Kroah-Hartman 
131902829aaSGreg Kroah-Hartman 	/* build sm */
132902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(struct f2fs_sm_info);
133902829aaSGreg Kroah-Hartman 
134902829aaSGreg Kroah-Hartman 	/* build sit */
135902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(struct sit_info);
136902829aaSGreg Kroah-Hartman 	si->base_mem += TOTAL_SEGS(sbi) * sizeof(struct seg_entry);
137902829aaSGreg Kroah-Hartman 	si->base_mem += f2fs_bitmap_size(TOTAL_SEGS(sbi));
138902829aaSGreg Kroah-Hartman 	si->base_mem += 2 * SIT_VBLOCK_MAP_SIZE * TOTAL_SEGS(sbi);
139902829aaSGreg Kroah-Hartman 	if (sbi->segs_per_sec > 1)
14053cf9522SJaegeuk Kim 		si->base_mem += TOTAL_SECS(sbi) * sizeof(struct sec_entry);
141902829aaSGreg Kroah-Hartman 	si->base_mem += __bitmap_size(sbi, SIT_BITMAP);
142902829aaSGreg Kroah-Hartman 
143902829aaSGreg Kroah-Hartman 	/* build free segmap */
144902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(struct free_segmap_info);
145902829aaSGreg Kroah-Hartman 	si->base_mem += f2fs_bitmap_size(TOTAL_SEGS(sbi));
14653cf9522SJaegeuk Kim 	si->base_mem += f2fs_bitmap_size(TOTAL_SECS(sbi));
147902829aaSGreg Kroah-Hartman 
148902829aaSGreg Kroah-Hartman 	/* build curseg */
149902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(struct curseg_info) * NR_CURSEG_TYPE;
150902829aaSGreg Kroah-Hartman 	si->base_mem += PAGE_CACHE_SIZE * NR_CURSEG_TYPE;
151902829aaSGreg Kroah-Hartman 
152902829aaSGreg Kroah-Hartman 	/* build dirty segmap */
153902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(struct dirty_seglist_info);
154902829aaSGreg Kroah-Hartman 	si->base_mem += NR_DIRTY_TYPE * f2fs_bitmap_size(TOTAL_SEGS(sbi));
1555ec4e49fSJaegeuk Kim 	si->base_mem += f2fs_bitmap_size(TOTAL_SECS(sbi));
156902829aaSGreg Kroah-Hartman 
157902829aaSGreg Kroah-Hartman 	/* buld nm */
158902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(struct f2fs_nm_info);
159902829aaSGreg Kroah-Hartman 	si->base_mem += __bitmap_size(sbi, NAT_BITMAP);
160902829aaSGreg Kroah-Hartman 
161902829aaSGreg Kroah-Hartman 	/* build gc */
162902829aaSGreg Kroah-Hartman 	si->base_mem += sizeof(struct f2fs_gc_kthread);
163902829aaSGreg Kroah-Hartman 
164902829aaSGreg Kroah-Hartman get_cache:
165902829aaSGreg Kroah-Hartman 	/* free nids */
166902829aaSGreg Kroah-Hartman 	si->cache_mem = NM_I(sbi)->fcnt;
167902829aaSGreg Kroah-Hartman 	si->cache_mem += NM_I(sbi)->nat_cnt;
168902829aaSGreg Kroah-Hartman 	npages = sbi->node_inode->i_mapping->nrpages;
169902829aaSGreg Kroah-Hartman 	si->cache_mem += npages << PAGE_CACHE_SHIFT;
170902829aaSGreg Kroah-Hartman 	npages = sbi->meta_inode->i_mapping->nrpages;
171902829aaSGreg Kroah-Hartman 	si->cache_mem += npages << PAGE_CACHE_SHIFT;
172902829aaSGreg Kroah-Hartman 	si->cache_mem += sbi->n_orphans * sizeof(struct orphan_inode_entry);
173902829aaSGreg Kroah-Hartman 	si->cache_mem += sbi->n_dirty_dirs * sizeof(struct dir_inode_entry);
174902829aaSGreg Kroah-Hartman }
175902829aaSGreg Kroah-Hartman 
176902829aaSGreg Kroah-Hartman static int stat_show(struct seq_file *s, void *v)
177902829aaSGreg Kroah-Hartman {
178*145b04e5Smajianpeng 	struct f2fs_stat_info *si;
179902829aaSGreg Kroah-Hartman 	int i = 0;
180902829aaSGreg Kroah-Hartman 	int j;
181902829aaSGreg Kroah-Hartman 
18266af62ceSmajianpeng 	mutex_lock(&f2fs_stat_mutex);
183*145b04e5Smajianpeng 	list_for_each_entry(si, &f2fs_stat_list, stat_list) {
184f83759e2Smajianpeng 		char devname[BDEVNAME_SIZE];
185902829aaSGreg Kroah-Hartman 
186902829aaSGreg Kroah-Hartman 		update_general_status(si->sbi);
187902829aaSGreg Kroah-Hartman 
188f83759e2Smajianpeng 		seq_printf(s, "\n=====[ partition info(%s). #%d ]=====\n",
189f83759e2Smajianpeng 			bdevname(si->sbi->sb->s_bdev, devname), i++);
1907880ceedSHuajun Li 		seq_printf(s, "[SB: 1] [CP: 2] [SIT: %d] [NAT: %d] ",
1917880ceedSHuajun Li 			   si->sit_area_segs, si->nat_area_segs);
192902829aaSGreg Kroah-Hartman 		seq_printf(s, "[SSA: %d] [MAIN: %d",
193902829aaSGreg Kroah-Hartman 			   si->ssa_area_segs, si->main_area_segs);
194902829aaSGreg Kroah-Hartman 		seq_printf(s, "(OverProv:%d Resv:%d)]\n\n",
195902829aaSGreg Kroah-Hartman 			   si->overp_segs, si->rsvd_segs);
196902829aaSGreg Kroah-Hartman 		seq_printf(s, "Utilization: %d%% (%d valid blocks)\n",
197902829aaSGreg Kroah-Hartman 			   si->utilization, si->valid_count);
198902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - Node: %u (Inode: %u, ",
199902829aaSGreg Kroah-Hartman 			   si->valid_node_count, si->valid_inode_count);
200902829aaSGreg Kroah-Hartman 		seq_printf(s, "Other: %u)\n  - Data: %u\n",
201902829aaSGreg Kroah-Hartman 			   si->valid_node_count - si->valid_inode_count,
202902829aaSGreg Kroah-Hartman 			   si->valid_count - si->valid_node_count);
203902829aaSGreg Kroah-Hartman 		seq_printf(s, "\nMain area: %d segs, %d secs %d zones\n",
204902829aaSGreg Kroah-Hartman 			   si->main_area_segs, si->main_area_sections,
205902829aaSGreg Kroah-Hartman 			   si->main_area_zones);
206902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - COLD  data: %d, %d, %d\n",
207902829aaSGreg Kroah-Hartman 			   si->curseg[CURSEG_COLD_DATA],
208902829aaSGreg Kroah-Hartman 			   si->cursec[CURSEG_COLD_DATA],
209902829aaSGreg Kroah-Hartman 			   si->curzone[CURSEG_COLD_DATA]);
210902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - WARM  data: %d, %d, %d\n",
211902829aaSGreg Kroah-Hartman 			   si->curseg[CURSEG_WARM_DATA],
212902829aaSGreg Kroah-Hartman 			   si->cursec[CURSEG_WARM_DATA],
213902829aaSGreg Kroah-Hartman 			   si->curzone[CURSEG_WARM_DATA]);
214902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - HOT   data: %d, %d, %d\n",
215902829aaSGreg Kroah-Hartman 			   si->curseg[CURSEG_HOT_DATA],
216902829aaSGreg Kroah-Hartman 			   si->cursec[CURSEG_HOT_DATA],
217902829aaSGreg Kroah-Hartman 			   si->curzone[CURSEG_HOT_DATA]);
218902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - Dir   dnode: %d, %d, %d\n",
219902829aaSGreg Kroah-Hartman 			   si->curseg[CURSEG_HOT_NODE],
220902829aaSGreg Kroah-Hartman 			   si->cursec[CURSEG_HOT_NODE],
221902829aaSGreg Kroah-Hartman 			   si->curzone[CURSEG_HOT_NODE]);
222902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - File   dnode: %d, %d, %d\n",
223902829aaSGreg Kroah-Hartman 			   si->curseg[CURSEG_WARM_NODE],
224902829aaSGreg Kroah-Hartman 			   si->cursec[CURSEG_WARM_NODE],
225902829aaSGreg Kroah-Hartman 			   si->curzone[CURSEG_WARM_NODE]);
226902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - Indir nodes: %d, %d, %d\n",
227902829aaSGreg Kroah-Hartman 			   si->curseg[CURSEG_COLD_NODE],
228902829aaSGreg Kroah-Hartman 			   si->cursec[CURSEG_COLD_NODE],
229902829aaSGreg Kroah-Hartman 			   si->curzone[CURSEG_COLD_NODE]);
230902829aaSGreg Kroah-Hartman 		seq_printf(s, "\n  - Valid: %d\n  - Dirty: %d\n",
231902829aaSGreg Kroah-Hartman 			   si->main_area_segs - si->dirty_count -
232902829aaSGreg Kroah-Hartman 			   si->prefree_count - si->free_segs,
233902829aaSGreg Kroah-Hartman 			   si->dirty_count);
234902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - Prefree: %d\n  - Free: %d (%d)\n\n",
235902829aaSGreg Kroah-Hartman 			   si->prefree_count, si->free_segs, si->free_secs);
236902829aaSGreg Kroah-Hartman 		seq_printf(s, "GC calls: %d (BG: %d)\n",
237902829aaSGreg Kroah-Hartman 			   si->call_count, si->bg_gc);
238902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - data segments : %d\n", si->data_segs);
239902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - node segments : %d\n", si->node_segs);
240902829aaSGreg Kroah-Hartman 		seq_printf(s, "Try to move %d blocks\n", si->tot_blks);
241902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - data blocks : %d\n", si->data_blks);
242902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - node blocks : %d\n", si->node_blks);
243902829aaSGreg Kroah-Hartman 		seq_printf(s, "\nExtent Hit Ratio: %d / %d\n",
244902829aaSGreg Kroah-Hartman 			   si->hit_ext, si->total_ext);
245902829aaSGreg Kroah-Hartman 		seq_printf(s, "\nBalancing F2FS Async:\n");
246902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - nodes %4d in %4d\n",
247902829aaSGreg Kroah-Hartman 			   si->ndirty_node, si->node_pages);
248902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - dents %4d in dirs:%4d\n",
249902829aaSGreg Kroah-Hartman 			   si->ndirty_dent, si->ndirty_dirs);
250902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - meta %4d in %4d\n",
251902829aaSGreg Kroah-Hartman 			   si->ndirty_meta, si->meta_pages);
252902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - NATs %5d > %lu\n",
253902829aaSGreg Kroah-Hartman 			   si->nats, NM_WOUT_THRESHOLD);
254902829aaSGreg Kroah-Hartman 		seq_printf(s, "  - SITs: %5d\n  - free_nids: %5d\n",
255902829aaSGreg Kroah-Hartman 			   si->sits, si->fnids);
256902829aaSGreg Kroah-Hartman 		seq_printf(s, "\nDistribution of User Blocks:");
257902829aaSGreg Kroah-Hartman 		seq_printf(s, " [ valid | invalid | free ]\n");
258902829aaSGreg Kroah-Hartman 		seq_printf(s, "  [");
259902829aaSGreg Kroah-Hartman 
260902829aaSGreg Kroah-Hartman 		for (j = 0; j < si->util_valid; j++)
261902829aaSGreg Kroah-Hartman 			seq_printf(s, "-");
262902829aaSGreg Kroah-Hartman 		seq_printf(s, "|");
263902829aaSGreg Kroah-Hartman 
264902829aaSGreg Kroah-Hartman 		for (j = 0; j < si->util_invalid; j++)
265902829aaSGreg Kroah-Hartman 			seq_printf(s, "-");
266902829aaSGreg Kroah-Hartman 		seq_printf(s, "|");
267902829aaSGreg Kroah-Hartman 
268902829aaSGreg Kroah-Hartman 		for (j = 0; j < si->util_free; j++)
269902829aaSGreg Kroah-Hartman 			seq_printf(s, "-");
270902829aaSGreg Kroah-Hartman 		seq_printf(s, "]\n\n");
271902829aaSGreg Kroah-Hartman 		seq_printf(s, "SSR: %u blocks in %u segments\n",
272902829aaSGreg Kroah-Hartman 			   si->block_count[SSR], si->segment_count[SSR]);
273902829aaSGreg Kroah-Hartman 		seq_printf(s, "LFS: %u blocks in %u segments\n",
274902829aaSGreg Kroah-Hartman 			   si->block_count[LFS], si->segment_count[LFS]);
275902829aaSGreg Kroah-Hartman 
276902829aaSGreg Kroah-Hartman 		/* segment usage info */
277902829aaSGreg Kroah-Hartman 		update_sit_info(si->sbi);
278902829aaSGreg Kroah-Hartman 		seq_printf(s, "\nBDF: %u, avg. vblocks: %u\n",
279902829aaSGreg Kroah-Hartman 			   si->bimodal, si->avg_vblocks);
280902829aaSGreg Kroah-Hartman 
281902829aaSGreg Kroah-Hartman 		/* memory footprint */
282902829aaSGreg Kroah-Hartman 		update_mem_info(si->sbi);
283902829aaSGreg Kroah-Hartman 		seq_printf(s, "\nMemory: %u KB = static: %u + cached: %u\n",
284902829aaSGreg Kroah-Hartman 				(si->base_mem + si->cache_mem) >> 10,
285902829aaSGreg Kroah-Hartman 				si->base_mem >> 10, si->cache_mem >> 10);
286902829aaSGreg Kroah-Hartman 	}
28766af62ceSmajianpeng 	mutex_unlock(&f2fs_stat_mutex);
288902829aaSGreg Kroah-Hartman 	return 0;
289902829aaSGreg Kroah-Hartman }
290902829aaSGreg Kroah-Hartman 
291902829aaSGreg Kroah-Hartman static int stat_open(struct inode *inode, struct file *file)
292902829aaSGreg Kroah-Hartman {
293902829aaSGreg Kroah-Hartman 	return single_open(file, stat_show, inode->i_private);
294902829aaSGreg Kroah-Hartman }
295902829aaSGreg Kroah-Hartman 
296902829aaSGreg Kroah-Hartman static const struct file_operations stat_fops = {
297902829aaSGreg Kroah-Hartman 	.open = stat_open,
298902829aaSGreg Kroah-Hartman 	.read = seq_read,
299902829aaSGreg Kroah-Hartman 	.llseek = seq_lseek,
300902829aaSGreg Kroah-Hartman 	.release = single_release,
301902829aaSGreg Kroah-Hartman };
302902829aaSGreg Kroah-Hartman 
3034589d25dSNamjae Jeon int f2fs_build_stats(struct f2fs_sb_info *sbi)
304902829aaSGreg Kroah-Hartman {
305902829aaSGreg Kroah-Hartman 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
306902829aaSGreg Kroah-Hartman 	struct f2fs_stat_info *si;
307902829aaSGreg Kroah-Hartman 
308902829aaSGreg Kroah-Hartman 	sbi->stat_info = kzalloc(sizeof(struct f2fs_stat_info), GFP_KERNEL);
309902829aaSGreg Kroah-Hartman 	if (!sbi->stat_info)
310902829aaSGreg Kroah-Hartman 		return -ENOMEM;
311902829aaSGreg Kroah-Hartman 
312902829aaSGreg Kroah-Hartman 	si = sbi->stat_info;
313902829aaSGreg Kroah-Hartman 	si->all_area_segs = le32_to_cpu(raw_super->segment_count);
314902829aaSGreg Kroah-Hartman 	si->sit_area_segs = le32_to_cpu(raw_super->segment_count_sit);
315902829aaSGreg Kroah-Hartman 	si->nat_area_segs = le32_to_cpu(raw_super->segment_count_nat);
316902829aaSGreg Kroah-Hartman 	si->ssa_area_segs = le32_to_cpu(raw_super->segment_count_ssa);
317902829aaSGreg Kroah-Hartman 	si->main_area_segs = le32_to_cpu(raw_super->segment_count_main);
318902829aaSGreg Kroah-Hartman 	si->main_area_sections = le32_to_cpu(raw_super->section_count);
319902829aaSGreg Kroah-Hartman 	si->main_area_zones = si->main_area_sections /
320902829aaSGreg Kroah-Hartman 				le32_to_cpu(raw_super->secs_per_zone);
321902829aaSGreg Kroah-Hartman 	si->sbi = sbi;
32266af62ceSmajianpeng 
32366af62ceSmajianpeng 	mutex_lock(&f2fs_stat_mutex);
32466af62ceSmajianpeng 	list_add_tail(&si->stat_list, &f2fs_stat_list);
32566af62ceSmajianpeng 	mutex_unlock(&f2fs_stat_mutex);
32666af62ceSmajianpeng 
327902829aaSGreg Kroah-Hartman 	return 0;
328902829aaSGreg Kroah-Hartman }
329902829aaSGreg Kroah-Hartman 
330902829aaSGreg Kroah-Hartman void f2fs_destroy_stats(struct f2fs_sb_info *sbi)
331902829aaSGreg Kroah-Hartman {
332902829aaSGreg Kroah-Hartman 	struct f2fs_stat_info *si = sbi->stat_info;
333902829aaSGreg Kroah-Hartman 
33466af62ceSmajianpeng 	mutex_lock(&f2fs_stat_mutex);
335902829aaSGreg Kroah-Hartman 	list_del(&si->stat_list);
33666af62ceSmajianpeng 	mutex_unlock(&f2fs_stat_mutex);
33766af62ceSmajianpeng 
338902829aaSGreg Kroah-Hartman 	kfree(sbi->stat_info);
339902829aaSGreg Kroah-Hartman }
340902829aaSGreg Kroah-Hartman 
3414589d25dSNamjae Jeon void __init f2fs_create_root_stats(void)
3424589d25dSNamjae Jeon {
3434589d25dSNamjae Jeon 	debugfs_root = debugfs_create_dir("f2fs", NULL);
3444589d25dSNamjae Jeon 	if (debugfs_root)
3454589d25dSNamjae Jeon 		debugfs_create_file("status", S_IRUGO, debugfs_root,
3464589d25dSNamjae Jeon 					 NULL, &stat_fops);
3474589d25dSNamjae Jeon }
3484589d25dSNamjae Jeon 
3494589d25dSNamjae Jeon void f2fs_destroy_root_stats(void)
350902829aaSGreg Kroah-Hartman {
351902829aaSGreg Kroah-Hartman 	debugfs_remove_recursive(debugfs_root);
352902829aaSGreg Kroah-Hartman 	debugfs_root = NULL;
353902829aaSGreg Kroah-Hartman }
354