xref: /openbmc/linux/fs/reiserfs/procfs.c (revision 76426e23)
1 /* -*- linux-c -*- */
2 
3 /* fs/reiserfs/procfs.c */
4 
5 /*
6  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
7  */
8 
9 /* proc info support a la one created by Sizif@Botik.RU for PGC */
10 
11 #include <linux/module.h>
12 #include <linux/time.h>
13 #include <linux/seq_file.h>
14 #include <linux/uaccess.h>
15 #include "reiserfs.h"
16 #include <linux/init.h>
17 #include <linux/proc_fs.h>
18 
19 /*
20  * LOCKING:
21  *
22  * These guys are evicted from procfs as the very first step in ->kill_sb().
23  *
24  */
25 
26 static int show_version(struct seq_file *m, void *unused)
27 {
28 	struct super_block *sb = m->private;
29 	char *format;
30 
31 	if (REISERFS_SB(sb)->s_properties & (1 << REISERFS_3_6)) {
32 		format = "3.6";
33 	} else if (REISERFS_SB(sb)->s_properties & (1 << REISERFS_3_5)) {
34 		format = "3.5";
35 	} else {
36 		format = "unknown";
37 	}
38 
39 	seq_printf(m, "%s format\twith checks %s\n", format,
40 #if defined( CONFIG_REISERFS_CHECK )
41 		   "on"
42 #else
43 		   "off"
44 #endif
45 	    );
46 	return 0;
47 }
48 
49 #define SF( x ) ( r -> x )
50 #define SFP( x ) SF( s_proc_info_data.x )
51 #define SFPL( x ) SFP( x[ level ] )
52 #define SFPF( x ) SFP( scan_bitmap.x )
53 #define SFPJ( x ) SFP( journal.x )
54 
55 #define D2C( x ) le16_to_cpu( x )
56 #define D4C( x ) le32_to_cpu( x )
57 #define DF( x ) D2C( rs -> s_v1.x )
58 #define DFL( x ) D4C( rs -> s_v1.x )
59 
60 #define objectid_map( s, rs ) (old_format_only (s) ?				\
61                          (__le32 *)((struct reiserfs_super_block_v1 *)rs + 1) :	\
62 			 (__le32 *)(rs + 1))
63 #define MAP( i ) D4C( objectid_map( sb, rs )[ i ] )
64 
65 #define DJF( x ) le32_to_cpu( rs -> x )
66 #define DJP( x ) le32_to_cpu( jp -> x )
67 #define JF( x ) ( r -> s_journal -> x )
68 
69 static int show_super(struct seq_file *m, void *unused)
70 {
71 	struct super_block *sb = m->private;
72 	struct reiserfs_sb_info *r = REISERFS_SB(sb);
73 
74 	seq_printf(m, "state: \t%s\n"
75 		   "mount options: \t%s%s%s%s%s%s%s%s%s%s%s\n"
76 		   "gen. counter: \t%i\n"
77 		   "s_disk_reads: \t%i\n"
78 		   "s_disk_writes: \t%i\n"
79 		   "s_fix_nodes: \t%i\n"
80 		   "s_do_balance: \t%i\n"
81 		   "s_unneeded_left_neighbor: \t%i\n"
82 		   "s_good_search_by_key_reada: \t%i\n"
83 		   "s_bmaps: \t%i\n"
84 		   "s_bmaps_without_search: \t%i\n"
85 		   "s_direct2indirect: \t%i\n"
86 		   "s_indirect2direct: \t%i\n"
87 		   "\n"
88 		   "max_hash_collisions: \t%i\n"
89 		   "breads: \t%lu\n"
90 		   "bread_misses: \t%lu\n"
91 		   "search_by_key: \t%lu\n"
92 		   "search_by_key_fs_changed: \t%lu\n"
93 		   "search_by_key_restarted: \t%lu\n"
94 		   "insert_item_restarted: \t%lu\n"
95 		   "paste_into_item_restarted: \t%lu\n"
96 		   "cut_from_item_restarted: \t%lu\n"
97 		   "delete_solid_item_restarted: \t%lu\n"
98 		   "delete_item_restarted: \t%lu\n"
99 		   "leaked_oid: \t%lu\n"
100 		   "leaves_removable: \t%lu\n",
101 		   SF(s_mount_state) == REISERFS_VALID_FS ?
102 		   "REISERFS_VALID_FS" : "REISERFS_ERROR_FS",
103 		   reiserfs_r5_hash(sb) ? "FORCE_R5 " : "",
104 		   reiserfs_rupasov_hash(sb) ? "FORCE_RUPASOV " : "",
105 		   reiserfs_tea_hash(sb) ? "FORCE_TEA " : "",
106 		   reiserfs_hash_detect(sb) ? "DETECT_HASH " : "",
107 		   reiserfs_no_border(sb) ? "NO_BORDER " : "BORDER ",
108 		   reiserfs_no_unhashed_relocation(sb) ?
109 		   "NO_UNHASHED_RELOCATION " : "",
110 		   reiserfs_hashed_relocation(sb) ? "UNHASHED_RELOCATION " : "",
111 		   reiserfs_test4(sb) ? "TEST4 " : "",
112 		   have_large_tails(sb) ? "TAILS " : have_small_tails(sb) ?
113 		   "SMALL_TAILS " : "NO_TAILS ",
114 		   replay_only(sb) ? "REPLAY_ONLY " : "",
115 		   convert_reiserfs(sb) ? "CONV " : "",
116 		   atomic_read(&r->s_generation_counter),
117 		   SF(s_disk_reads), SF(s_disk_writes), SF(s_fix_nodes),
118 		   SF(s_do_balance), SF(s_unneeded_left_neighbor),
119 		   SF(s_good_search_by_key_reada), SF(s_bmaps),
120 		   SF(s_bmaps_without_search), SF(s_direct2indirect),
121 		   SF(s_indirect2direct), SFP(max_hash_collisions), SFP(breads),
122 		   SFP(bread_miss), SFP(search_by_key),
123 		   SFP(search_by_key_fs_changed), SFP(search_by_key_restarted),
124 		   SFP(insert_item_restarted), SFP(paste_into_item_restarted),
125 		   SFP(cut_from_item_restarted),
126 		   SFP(delete_solid_item_restarted), SFP(delete_item_restarted),
127 		   SFP(leaked_oid), SFP(leaves_removable));
128 
129 	return 0;
130 }
131 
132 static int show_per_level(struct seq_file *m, void *unused)
133 {
134 	struct super_block *sb = m->private;
135 	struct reiserfs_sb_info *r = REISERFS_SB(sb);
136 	int level;
137 
138 	seq_printf(m, "level\t"
139 		   "     balances"
140 		   " [sbk:  reads"
141 		   "   fs_changed"
142 		   "   restarted]"
143 		   "   free space"
144 		   "        items"
145 		   "   can_remove"
146 		   "         lnum"
147 		   "         rnum"
148 		   "       lbytes"
149 		   "       rbytes"
150 		   "     get_neig"
151 		   " get_neig_res" "  need_l_neig" "  need_r_neig" "\n");
152 
153 	for (level = 0; level < MAX_HEIGHT; ++level) {
154 		seq_printf(m, "%i\t"
155 			   " %12lu"
156 			   " %12lu"
157 			   " %12lu"
158 			   " %12lu"
159 			   " %12lu"
160 			   " %12lu"
161 			   " %12lu"
162 			   " %12li"
163 			   " %12li"
164 			   " %12li"
165 			   " %12li"
166 			   " %12lu"
167 			   " %12lu"
168 			   " %12lu"
169 			   " %12lu"
170 			   "\n",
171 			   level,
172 			   SFPL(balance_at),
173 			   SFPL(sbk_read_at),
174 			   SFPL(sbk_fs_changed),
175 			   SFPL(sbk_restarted),
176 			   SFPL(free_at),
177 			   SFPL(items_at),
178 			   SFPL(can_node_be_removed),
179 			   SFPL(lnum),
180 			   SFPL(rnum),
181 			   SFPL(lbytes),
182 			   SFPL(rbytes),
183 			   SFPL(get_neighbors),
184 			   SFPL(get_neighbors_restart),
185 			   SFPL(need_l_neighbor), SFPL(need_r_neighbor)
186 		    );
187 	}
188 	return 0;
189 }
190 
191 static int show_bitmap(struct seq_file *m, void *unused)
192 {
193 	struct super_block *sb = m->private;
194 	struct reiserfs_sb_info *r = REISERFS_SB(sb);
195 
196 	seq_printf(m, "free_block: %lu\n"
197 		   "  scan_bitmap:"
198 		   "          wait"
199 		   "          bmap"
200 		   "         retry"
201 		   "        stolen"
202 		   "  journal_hint"
203 		   "journal_nohint"
204 		   "\n"
205 		   " %14lu"
206 		   " %14lu"
207 		   " %14lu"
208 		   " %14lu"
209 		   " %14lu"
210 		   " %14lu"
211 		   " %14lu"
212 		   "\n",
213 		   SFP(free_block),
214 		   SFPF(call),
215 		   SFPF(wait),
216 		   SFPF(bmap),
217 		   SFPF(retry),
218 		   SFPF(stolen),
219 		   SFPF(in_journal_hint), SFPF(in_journal_nohint));
220 
221 	return 0;
222 }
223 
224 static int show_on_disk_super(struct seq_file *m, void *unused)
225 {
226 	struct super_block *sb = m->private;
227 	struct reiserfs_sb_info *sb_info = REISERFS_SB(sb);
228 	struct reiserfs_super_block *rs = sb_info->s_rs;
229 	int hash_code = DFL(s_hash_function_code);
230 	__u32 flags = DJF(s_flags);
231 
232 	seq_printf(m, "block_count: \t%i\n"
233 		   "free_blocks: \t%i\n"
234 		   "root_block: \t%i\n"
235 		   "blocksize: \t%i\n"
236 		   "oid_maxsize: \t%i\n"
237 		   "oid_cursize: \t%i\n"
238 		   "umount_state: \t%i\n"
239 		   "magic: \t%10.10s\n"
240 		   "fs_state: \t%i\n"
241 		   "hash: \t%s\n"
242 		   "tree_height: \t%i\n"
243 		   "bmap_nr: \t%i\n"
244 		   "version: \t%i\n"
245 		   "flags: \t%x[%s]\n"
246 		   "reserved_for_journal: \t%i\n",
247 		   DFL(s_block_count),
248 		   DFL(s_free_blocks),
249 		   DFL(s_root_block),
250 		   DF(s_blocksize),
251 		   DF(s_oid_maxsize),
252 		   DF(s_oid_cursize),
253 		   DF(s_umount_state),
254 		   rs->s_v1.s_magic,
255 		   DF(s_fs_state),
256 		   hash_code == TEA_HASH ? "tea" :
257 		   (hash_code == YURA_HASH) ? "rupasov" :
258 		   (hash_code == R5_HASH) ? "r5" :
259 		   (hash_code == UNSET_HASH) ? "unset" : "unknown",
260 		   DF(s_tree_height),
261 		   DF(s_bmap_nr),
262 		   DF(s_version), flags, (flags & reiserfs_attrs_cleared)
263 		   ? "attrs_cleared" : "", DF(s_reserved_for_journal));
264 
265 	return 0;
266 }
267 
268 static int show_oidmap(struct seq_file *m, void *unused)
269 {
270 	struct super_block *sb = m->private;
271 	struct reiserfs_sb_info *sb_info = REISERFS_SB(sb);
272 	struct reiserfs_super_block *rs = sb_info->s_rs;
273 	unsigned int mapsize = le16_to_cpu(rs->s_v1.s_oid_cursize);
274 	unsigned long total_used = 0;
275 	int i;
276 
277 	for (i = 0; i < mapsize; ++i) {
278 		__u32 right;
279 
280 		right = (i == mapsize - 1) ? MAX_KEY_OBJECTID : MAP(i + 1);
281 		seq_printf(m, "%s: [ %x .. %x )\n",
282 			   (i & 1) ? "free" : "used", MAP(i), right);
283 		if (!(i & 1)) {
284 			total_used += right - MAP(i);
285 		}
286 	}
287 #if defined( REISERFS_USE_OIDMAPF )
288 	if (sb_info->oidmap.use_file && (sb_info->oidmap.mapf != NULL)) {
289 		loff_t size = file_inode(sb_info->oidmap.mapf)->i_size;
290 		total_used += size / sizeof(reiserfs_oidinterval_d_t);
291 	}
292 #endif
293 	seq_printf(m, "total: \t%i [%i/%i] used: %lu [exact]\n",
294 		   mapsize,
295 		   mapsize, le16_to_cpu(rs->s_v1.s_oid_maxsize), total_used);
296 	return 0;
297 }
298 
299 static time64_t ktime_mono_to_real_seconds(time64_t mono)
300 {
301 	ktime_t kt = ktime_set(mono, NSEC_PER_SEC/2);
302 
303 	return ktime_divns(ktime_mono_to_real(kt), NSEC_PER_SEC);
304 }
305 
306 static int show_journal(struct seq_file *m, void *unused)
307 {
308 	struct super_block *sb = m->private;
309 	struct reiserfs_sb_info *r = REISERFS_SB(sb);
310 	struct reiserfs_super_block *rs = r->s_rs;
311 	struct journal_params *jp = &rs->s_v1.s_journal;
312 
313 	seq_printf(m,		/* on-disk fields */
314 		   "jp_journal_1st_block: \t%i\n"
315 		   "jp_journal_dev: \t%pg[%x]\n"
316 		   "jp_journal_size: \t%i\n"
317 		   "jp_journal_trans_max: \t%i\n"
318 		   "jp_journal_magic: \t%i\n"
319 		   "jp_journal_max_batch: \t%i\n"
320 		   "jp_journal_max_commit_age: \t%i\n"
321 		   "jp_journal_max_trans_age: \t%i\n"
322 		   /* incore fields */
323 		   "j_1st_reserved_block: \t%i\n"
324 		   "j_state: \t%li\n"
325 		   "j_trans_id: \t%u\n"
326 		   "j_mount_id: \t%lu\n"
327 		   "j_start: \t%lu\n"
328 		   "j_len: \t%lu\n"
329 		   "j_len_alloc: \t%lu\n"
330 		   "j_wcount: \t%i\n"
331 		   "j_bcount: \t%lu\n"
332 		   "j_first_unflushed_offset: \t%lu\n"
333 		   "j_last_flush_trans_id: \t%u\n"
334 		   "j_trans_start_time: \t%lli\n"
335 		   "j_list_bitmap_index: \t%i\n"
336 		   "j_must_wait: \t%i\n"
337 		   "j_next_full_flush: \t%i\n"
338 		   "j_next_async_flush: \t%i\n"
339 		   "j_cnode_used: \t%i\n" "j_cnode_free: \t%i\n" "\n"
340 		   /* reiserfs_proc_info_data_t.journal fields */
341 		   "in_journal: \t%12lu\n"
342 		   "in_journal_bitmap: \t%12lu\n"
343 		   "in_journal_reusable: \t%12lu\n"
344 		   "lock_journal: \t%12lu\n"
345 		   "lock_journal_wait: \t%12lu\n"
346 		   "journal_begin: \t%12lu\n"
347 		   "journal_relock_writers: \t%12lu\n"
348 		   "journal_relock_wcount: \t%12lu\n"
349 		   "mark_dirty: \t%12lu\n"
350 		   "mark_dirty_already: \t%12lu\n"
351 		   "mark_dirty_notjournal: \t%12lu\n"
352 		   "restore_prepared: \t%12lu\n"
353 		   "prepare: \t%12lu\n"
354 		   "prepare_retry: \t%12lu\n",
355 		   DJP(jp_journal_1st_block),
356 		   SB_JOURNAL(sb)->j_dev_bd,
357 		   DJP(jp_journal_dev),
358 		   DJP(jp_journal_size),
359 		   DJP(jp_journal_trans_max),
360 		   DJP(jp_journal_magic),
361 		   DJP(jp_journal_max_batch),
362 		   SB_JOURNAL(sb)->j_max_commit_age,
363 		   DJP(jp_journal_max_trans_age),
364 		   JF(j_1st_reserved_block),
365 		   JF(j_state),
366 		   JF(j_trans_id),
367 		   JF(j_mount_id),
368 		   JF(j_start),
369 		   JF(j_len),
370 		   JF(j_len_alloc),
371 		   atomic_read(&r->s_journal->j_wcount),
372 		   JF(j_bcount),
373 		   JF(j_first_unflushed_offset),
374 		   JF(j_last_flush_trans_id),
375 		   ktime_mono_to_real_seconds(JF(j_trans_start_time)),
376 		   JF(j_list_bitmap_index),
377 		   JF(j_must_wait),
378 		   JF(j_next_full_flush),
379 		   JF(j_next_async_flush),
380 		   JF(j_cnode_used),
381 		   JF(j_cnode_free),
382 		   SFPJ(in_journal),
383 		   SFPJ(in_journal_bitmap),
384 		   SFPJ(in_journal_reusable),
385 		   SFPJ(lock_journal),
386 		   SFPJ(lock_journal_wait),
387 		   SFPJ(journal_being),
388 		   SFPJ(journal_relock_writers),
389 		   SFPJ(journal_relock_wcount),
390 		   SFPJ(mark_dirty),
391 		   SFPJ(mark_dirty_already),
392 		   SFPJ(mark_dirty_notjournal),
393 		   SFPJ(restore_prepared), SFPJ(prepare), SFPJ(prepare_retry)
394 	    );
395 	return 0;
396 }
397 
398 static struct proc_dir_entry *proc_info_root = NULL;
399 static const char proc_info_root_name[] = "fs/reiserfs";
400 
401 static void add_file(struct super_block *sb, char *name,
402 		     int (*func) (struct seq_file *, void *))
403 {
404 	proc_create_single_data(name, 0, REISERFS_SB(sb)->procdir, func, sb);
405 }
406 
407 int reiserfs_proc_info_init(struct super_block *sb)
408 {
409 	char b[BDEVNAME_SIZE];
410 	char *s;
411 
412 	/* Some block devices use /'s */
413 	strlcpy(b, sb->s_id, BDEVNAME_SIZE);
414 	s = strchr(b, '/');
415 	if (s)
416 		*s = '!';
417 
418 	spin_lock_init(&__PINFO(sb).lock);
419 	REISERFS_SB(sb)->procdir = proc_mkdir_data(b, 0, proc_info_root, sb);
420 	if (REISERFS_SB(sb)->procdir) {
421 		add_file(sb, "version", show_version);
422 		add_file(sb, "super", show_super);
423 		add_file(sb, "per-level", show_per_level);
424 		add_file(sb, "bitmap", show_bitmap);
425 		add_file(sb, "on-disk-super", show_on_disk_super);
426 		add_file(sb, "oidmap", show_oidmap);
427 		add_file(sb, "journal", show_journal);
428 		return 0;
429 	}
430 	reiserfs_warning(sb, "cannot create /proc/%s/%s",
431 			 proc_info_root_name, b);
432 	return 1;
433 }
434 
435 int reiserfs_proc_info_done(struct super_block *sb)
436 {
437 	struct proc_dir_entry *de = REISERFS_SB(sb)->procdir;
438 	if (de) {
439 		char b[BDEVNAME_SIZE];
440 		char *s;
441 
442 		/* Some block devices use /'s */
443 		strlcpy(b, sb->s_id, BDEVNAME_SIZE);
444 		s = strchr(b, '/');
445 		if (s)
446 			*s = '!';
447 
448 		remove_proc_subtree(b, proc_info_root);
449 		REISERFS_SB(sb)->procdir = NULL;
450 	}
451 	return 0;
452 }
453 
454 int reiserfs_proc_info_global_init(void)
455 {
456 	if (proc_info_root == NULL) {
457 		proc_info_root = proc_mkdir(proc_info_root_name, NULL);
458 		if (!proc_info_root) {
459 			reiserfs_warning(NULL, "cannot create /proc/%s",
460 					 proc_info_root_name);
461 			return 1;
462 		}
463 	}
464 	return 0;
465 }
466 
467 int reiserfs_proc_info_global_done(void)
468 {
469 	if (proc_info_root != NULL) {
470 		proc_info_root = NULL;
471 		remove_proc_entry(proc_info_root_name, NULL);
472 	}
473 	return 0;
474 }
475 /*
476  * Revision 1.1.8.2  2001/07/15 17:08:42  god
477  *  . use get_super() in procfs.c
478  *  . remove remove_save_link() from reiserfs_do_truncate()
479  *
480  * I accept terms and conditions stated in the Legal Agreement
481  * (available at http://www.namesys.com/legalese.html)
482  *
483  * Revision 1.1.8.1  2001/07/11 16:48:50  god
484  * proc info support
485  *
486  * I accept terms and conditions stated in the Legal Agreement
487  * (available at http://www.namesys.com/legalese.html)
488  *
489  */
490 
491 /*
492  * Make Linus happy.
493  * Local variables:
494  * c-indentation-style: "K&R"
495  * mode-name: "LC"
496  * c-basic-offset: 8
497  * tab-width: 8
498  * End:
499  */
500