Merge branch 'drm-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/airlied/drm-2.6
[linux-2.6] / fs / reiserfs / procfs.c
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 /* $Id: procfs.c,v 1.1.8.2 2001/07/15 17:08:42 god Exp $ */
12
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/time.h>
16 #include <linux/seq_file.h>
17 #include <asm/uaccess.h>
18 #include <linux/reiserfs_fs.h>
19 #include <linux/reiserfs_fs_sb.h>
20 #include <linux/smp_lock.h>
21 #include <linux/init.h>
22 #include <linux/proc_fs.h>
23
24 #if defined( REISERFS_PROC_INFO )
25
26 /*
27  * LOCKING:
28  *
29  * We rely on new Alexander Viro's super-block locking.
30  *
31  */
32
33 static int show_version(struct seq_file *m, struct super_block *sb)
34 {
35         char *format;
36
37         if (REISERFS_SB(sb)->s_properties & (1 << REISERFS_3_6)) {
38                 format = "3.6";
39         } else if (REISERFS_SB(sb)->s_properties & (1 << REISERFS_3_5)) {
40                 format = "3.5";
41         } else {
42                 format = "unknown";
43         }
44
45         seq_printf(m, "%s format\twith checks %s\n", format,
46 #if defined( CONFIG_REISERFS_CHECK )
47                    "on"
48 #else
49                    "off"
50 #endif
51             );
52         return 0;
53 }
54
55 int reiserfs_global_version_in_proc(char *buffer, char **start, off_t offset,
56                                     int count, int *eof, void *data)
57 {
58         *start = buffer;
59         *eof = 1;
60         return 0;
61 }
62
63 #define SF( x ) ( r -> x )
64 #define SFP( x ) SF( s_proc_info_data.x )
65 #define SFPL( x ) SFP( x[ level ] )
66 #define SFPF( x ) SFP( scan_bitmap.x )
67 #define SFPJ( x ) SFP( journal.x )
68
69 #define D2C( x ) le16_to_cpu( x )
70 #define D4C( x ) le32_to_cpu( x )
71 #define DF( x ) D2C( rs -> s_v1.x )
72 #define DFL( x ) D4C( rs -> s_v1.x )
73
74 #define objectid_map( s, rs ) (old_format_only (s) ?                            \
75                          (__le32 *)((struct reiserfs_super_block_v1 *)rs + 1) : \
76                          (__le32 *)(rs + 1))
77 #define MAP( i ) D4C( objectid_map( sb, rs )[ i ] )
78
79 #define DJF( x ) le32_to_cpu( rs -> x )
80 #define DJV( x ) le32_to_cpu( s_v1 -> x )
81 #define DJP( x ) le32_to_cpu( jp -> x )
82 #define JF( x ) ( r -> s_journal -> x )
83
84 static int show_super(struct seq_file *m, struct super_block *sb)
85 {
86         struct reiserfs_sb_info *r = REISERFS_SB(sb);
87
88         seq_printf(m, "state: \t%s\n"
89                    "mount options: \t%s%s%s%s%s%s%s%s%s%s%s\n"
90                    "gen. counter: \t%i\n"
91                    "s_kmallocs: \t%i\n"
92                    "s_disk_reads: \t%i\n"
93                    "s_disk_writes: \t%i\n"
94                    "s_fix_nodes: \t%i\n"
95                    "s_do_balance: \t%i\n"
96                    "s_unneeded_left_neighbor: \t%i\n"
97                    "s_good_search_by_key_reada: \t%i\n"
98                    "s_bmaps: \t%i\n"
99                    "s_bmaps_without_search: \t%i\n"
100                    "s_direct2indirect: \t%i\n"
101                    "s_indirect2direct: \t%i\n"
102                    "\n"
103                    "max_hash_collisions: \t%i\n"
104                    "breads: \t%lu\n"
105                    "bread_misses: \t%lu\n"
106                    "search_by_key: \t%lu\n"
107                    "search_by_key_fs_changed: \t%lu\n"
108                    "search_by_key_restarted: \t%lu\n"
109                    "insert_item_restarted: \t%lu\n"
110                    "paste_into_item_restarted: \t%lu\n"
111                    "cut_from_item_restarted: \t%lu\n"
112                    "delete_solid_item_restarted: \t%lu\n"
113                    "delete_item_restarted: \t%lu\n"
114                    "leaked_oid: \t%lu\n"
115                    "leaves_removable: \t%lu\n",
116                    SF(s_mount_state) == REISERFS_VALID_FS ?
117                    "REISERFS_VALID_FS" : "REISERFS_ERROR_FS",
118                    reiserfs_r5_hash(sb) ? "FORCE_R5 " : "",
119                    reiserfs_rupasov_hash(sb) ? "FORCE_RUPASOV " : "",
120                    reiserfs_tea_hash(sb) ? "FORCE_TEA " : "",
121                    reiserfs_hash_detect(sb) ? "DETECT_HASH " : "",
122                    reiserfs_no_border(sb) ? "NO_BORDER " : "BORDER ",
123                    reiserfs_no_unhashed_relocation(sb) ?
124                    "NO_UNHASHED_RELOCATION " : "",
125                    reiserfs_hashed_relocation(sb) ? "UNHASHED_RELOCATION " : "",
126                    reiserfs_test4(sb) ? "TEST4 " : "",
127                    have_large_tails(sb) ? "TAILS " : have_small_tails(sb) ?
128                    "SMALL_TAILS " : "NO_TAILS ",
129                    replay_only(sb) ? "REPLAY_ONLY " : "",
130                    convert_reiserfs(sb) ? "CONV " : "",
131                    atomic_read(&r->s_generation_counter), SF(s_kmallocs),
132                    SF(s_disk_reads), SF(s_disk_writes), SF(s_fix_nodes),
133                    SF(s_do_balance), SF(s_unneeded_left_neighbor),
134                    SF(s_good_search_by_key_reada), SF(s_bmaps),
135                    SF(s_bmaps_without_search), SF(s_direct2indirect),
136                    SF(s_indirect2direct), SFP(max_hash_collisions), SFP(breads),
137                    SFP(bread_miss), SFP(search_by_key),
138                    SFP(search_by_key_fs_changed), SFP(search_by_key_restarted),
139                    SFP(insert_item_restarted), SFP(paste_into_item_restarted),
140                    SFP(cut_from_item_restarted),
141                    SFP(delete_solid_item_restarted), SFP(delete_item_restarted),
142                    SFP(leaked_oid), SFP(leaves_removable));
143
144         return 0;
145 }
146
147 static int show_per_level(struct seq_file *m, struct super_block *sb)
148 {
149         struct reiserfs_sb_info *r = REISERFS_SB(sb);
150         int level;
151
152         seq_printf(m, "level\t"
153                    "     balances"
154                    " [sbk:  reads"
155                    "   fs_changed"
156                    "   restarted]"
157                    "   free space"
158                    "        items"
159                    "   can_remove"
160                    "         lnum"
161                    "         rnum"
162                    "       lbytes"
163                    "       rbytes"
164                    "     get_neig"
165                    " get_neig_res" "  need_l_neig" "  need_r_neig" "\n");
166
167         for (level = 0; level < MAX_HEIGHT; ++level) {
168                 seq_printf(m, "%i\t"
169                            " %12lu"
170                            " %12lu"
171                            " %12lu"
172                            " %12lu"
173                            " %12lu"
174                            " %12lu"
175                            " %12lu"
176                            " %12li"
177                            " %12li"
178                            " %12li"
179                            " %12li"
180                            " %12lu"
181                            " %12lu"
182                            " %12lu"
183                            " %12lu"
184                            "\n",
185                            level,
186                            SFPL(balance_at),
187                            SFPL(sbk_read_at),
188                            SFPL(sbk_fs_changed),
189                            SFPL(sbk_restarted),
190                            SFPL(free_at),
191                            SFPL(items_at),
192                            SFPL(can_node_be_removed),
193                            SFPL(lnum),
194                            SFPL(rnum),
195                            SFPL(lbytes),
196                            SFPL(rbytes),
197                            SFPL(get_neighbors),
198                            SFPL(get_neighbors_restart),
199                            SFPL(need_l_neighbor), SFPL(need_r_neighbor)
200                     );
201         }
202         return 0;
203 }
204
205 static int show_bitmap(struct seq_file *m, struct super_block *sb)
206 {
207         struct reiserfs_sb_info *r = REISERFS_SB(sb);
208
209         seq_printf(m, "free_block: %lu\n"
210                    "  scan_bitmap:"
211                    "          wait"
212                    "          bmap"
213                    "         retry"
214                    "        stolen"
215                    "  journal_hint"
216                    "journal_nohint"
217                    "\n"
218                    " %14lu"
219                    " %14lu"
220                    " %14lu"
221                    " %14lu"
222                    " %14lu"
223                    " %14lu"
224                    " %14lu"
225                    "\n",
226                    SFP(free_block),
227                    SFPF(call),
228                    SFPF(wait),
229                    SFPF(bmap),
230                    SFPF(retry),
231                    SFPF(stolen),
232                    SFPF(in_journal_hint), SFPF(in_journal_nohint));
233
234         return 0;
235 }
236
237 static int show_on_disk_super(struct seq_file *m, struct super_block *sb)
238 {
239         struct reiserfs_sb_info *sb_info = REISERFS_SB(sb);
240         struct reiserfs_super_block *rs = sb_info->s_rs;
241         int hash_code = DFL(s_hash_function_code);
242         __u32 flags = DJF(s_flags);
243
244         seq_printf(m, "block_count: \t%i\n"
245                    "free_blocks: \t%i\n"
246                    "root_block: \t%i\n"
247                    "blocksize: \t%i\n"
248                    "oid_maxsize: \t%i\n"
249                    "oid_cursize: \t%i\n"
250                    "umount_state: \t%i\n"
251                    "magic: \t%10.10s\n"
252                    "fs_state: \t%i\n"
253                    "hash: \t%s\n"
254                    "tree_height: \t%i\n"
255                    "bmap_nr: \t%i\n"
256                    "version: \t%i\n"
257                    "flags: \t%x[%s]\n"
258                    "reserved_for_journal: \t%i\n",
259                    DFL(s_block_count),
260                    DFL(s_free_blocks),
261                    DFL(s_root_block),
262                    DF(s_blocksize),
263                    DF(s_oid_maxsize),
264                    DF(s_oid_cursize),
265                    DF(s_umount_state),
266                    rs->s_v1.s_magic,
267                    DF(s_fs_state),
268                    hash_code == TEA_HASH ? "tea" :
269                    (hash_code == YURA_HASH) ? "rupasov" :
270                    (hash_code == R5_HASH) ? "r5" :
271                    (hash_code == UNSET_HASH) ? "unset" : "unknown",
272                    DF(s_tree_height),
273                    DF(s_bmap_nr),
274                    DF(s_version), flags, (flags & reiserfs_attrs_cleared)
275                    ? "attrs_cleared" : "", DF(s_reserved_for_journal));
276
277         return 0;
278 }
279
280 static int show_oidmap(struct seq_file *m, struct super_block *sb)
281 {
282         struct reiserfs_sb_info *sb_info = REISERFS_SB(sb);
283         struct reiserfs_super_block *rs = sb_info->s_rs;
284         unsigned int mapsize = le16_to_cpu(rs->s_v1.s_oid_cursize);
285         unsigned long total_used = 0;
286         int i;
287
288         for (i = 0; i < mapsize; ++i) {
289                 __u32 right;
290
291                 right = (i == mapsize - 1) ? MAX_KEY_OBJECTID : MAP(i + 1);
292                 seq_printf(m, "%s: [ %x .. %x )\n",
293                            (i & 1) ? "free" : "used", MAP(i), right);
294                 if (!(i & 1)) {
295                         total_used += right - MAP(i);
296                 }
297         }
298 #if defined( REISERFS_USE_OIDMAPF )
299         if (sb_info->oidmap.use_file && (sb_info->oidmap.mapf != NULL)) {
300                 loff_t size = sb_info->oidmap.mapf->f_dentry->d_inode->i_size;
301                 total_used += size / sizeof(reiserfs_oidinterval_d_t);
302         }
303 #endif
304         seq_printf(m, "total: \t%i [%i/%i] used: %lu [exact]\n",
305                    mapsize,
306                    mapsize, le16_to_cpu(rs->s_v1.s_oid_maxsize), total_used);
307         return 0;
308 }
309
310 static int show_journal(struct seq_file *m, struct super_block *sb)
311 {
312         struct reiserfs_sb_info *r = REISERFS_SB(sb);
313         struct reiserfs_super_block *rs = r->s_rs;
314         struct journal_params *jp = &rs->s_v1.s_journal;
315         char b[BDEVNAME_SIZE];
316
317         seq_printf(m,           /* on-disk fields */
318                    "jp_journal_1st_block: \t%i\n"
319                    "jp_journal_dev: \t%s[%x]\n"
320                    "jp_journal_size: \t%i\n"
321                    "jp_journal_trans_max: \t%i\n"
322                    "jp_journal_magic: \t%i\n"
323                    "jp_journal_max_batch: \t%i\n"
324                    "jp_journal_max_commit_age: \t%i\n"
325                    "jp_journal_max_trans_age: \t%i\n"
326                    /* incore fields */
327                    "j_1st_reserved_block: \t%i\n"
328                    "j_state: \t%li\n"
329                    "j_trans_id: \t%lu\n"
330                    "j_mount_id: \t%lu\n"
331                    "j_start: \t%lu\n"
332                    "j_len: \t%lu\n"
333                    "j_len_alloc: \t%lu\n"
334                    "j_wcount: \t%i\n"
335                    "j_bcount: \t%lu\n"
336                    "j_first_unflushed_offset: \t%lu\n"
337                    "j_last_flush_trans_id: \t%lu\n"
338                    "j_trans_start_time: \t%li\n"
339                    "j_list_bitmap_index: \t%i\n"
340                    "j_must_wait: \t%i\n"
341                    "j_next_full_flush: \t%i\n"
342                    "j_next_async_flush: \t%i\n"
343                    "j_cnode_used: \t%i\n" "j_cnode_free: \t%i\n" "\n"
344                    /* reiserfs_proc_info_data_t.journal fields */
345                    "in_journal: \t%12lu\n"
346                    "in_journal_bitmap: \t%12lu\n"
347                    "in_journal_reusable: \t%12lu\n"
348                    "lock_journal: \t%12lu\n"
349                    "lock_journal_wait: \t%12lu\n"
350                    "journal_begin: \t%12lu\n"
351                    "journal_relock_writers: \t%12lu\n"
352                    "journal_relock_wcount: \t%12lu\n"
353                    "mark_dirty: \t%12lu\n"
354                    "mark_dirty_already: \t%12lu\n"
355                    "mark_dirty_notjournal: \t%12lu\n"
356                    "restore_prepared: \t%12lu\n"
357                    "prepare: \t%12lu\n"
358                    "prepare_retry: \t%12lu\n",
359                    DJP(jp_journal_1st_block),
360                    bdevname(SB_JOURNAL(sb)->j_dev_bd, b),
361                    DJP(jp_journal_dev),
362                    DJP(jp_journal_size),
363                    DJP(jp_journal_trans_max),
364                    DJP(jp_journal_magic),
365                    DJP(jp_journal_max_batch),
366                    SB_JOURNAL(sb)->j_max_commit_age,
367                    DJP(jp_journal_max_trans_age),
368                    JF(j_1st_reserved_block),
369                    JF(j_state),
370                    JF(j_trans_id),
371                    JF(j_mount_id),
372                    JF(j_start),
373                    JF(j_len),
374                    JF(j_len_alloc),
375                    atomic_read(&r->s_journal->j_wcount),
376                    JF(j_bcount),
377                    JF(j_first_unflushed_offset),
378                    JF(j_last_flush_trans_id),
379                    JF(j_trans_start_time),
380                    JF(j_list_bitmap_index),
381                    JF(j_must_wait),
382                    JF(j_next_full_flush),
383                    JF(j_next_async_flush),
384                    JF(j_cnode_used),
385                    JF(j_cnode_free),
386                    SFPJ(in_journal),
387                    SFPJ(in_journal_bitmap),
388                    SFPJ(in_journal_reusable),
389                    SFPJ(lock_journal),
390                    SFPJ(lock_journal_wait),
391                    SFPJ(journal_being),
392                    SFPJ(journal_relock_writers),
393                    SFPJ(journal_relock_wcount),
394                    SFPJ(mark_dirty),
395                    SFPJ(mark_dirty_already),
396                    SFPJ(mark_dirty_notjournal),
397                    SFPJ(restore_prepared), SFPJ(prepare), SFPJ(prepare_retry)
398             );
399         return 0;
400 }
401
402 /* iterator */
403 static int test_sb(struct super_block *sb, void *data)
404 {
405         return data == sb;
406 }
407
408 static int set_sb(struct super_block *sb, void *data)
409 {
410         return -ENOENT;
411 }
412
413 static void *r_start(struct seq_file *m, loff_t * pos)
414 {
415         struct proc_dir_entry *de = m->private;
416         struct super_block *s = de->parent->data;
417         loff_t l = *pos;
418
419         if (l)
420                 return NULL;
421
422         if (IS_ERR(sget(&reiserfs_fs_type, test_sb, set_sb, s)))
423                 return NULL;
424
425         up_write(&s->s_umount);
426
427         if (de->deleted) {
428                 deactivate_super(s);
429                 return NULL;
430         }
431
432         return s;
433 }
434
435 static void *r_next(struct seq_file *m, void *v, loff_t * pos)
436 {
437         ++*pos;
438         if (v)
439                 deactivate_super(v);
440         return NULL;
441 }
442
443 static void r_stop(struct seq_file *m, void *v)
444 {
445         if (v)
446                 deactivate_super(v);
447 }
448
449 static int r_show(struct seq_file *m, void *v)
450 {
451         struct proc_dir_entry *de = m->private;
452         int (*show) (struct seq_file *, struct super_block *) = de->data;
453         return show(m, v);
454 }
455
456 static struct seq_operations r_ops = {
457         .start = r_start,
458         .next = r_next,
459         .stop = r_stop,
460         .show = r_show,
461 };
462
463 static int r_open(struct inode *inode, struct file *file)
464 {
465         int ret = seq_open(file, &r_ops);
466
467         if (!ret) {
468                 struct seq_file *m = file->private_data;
469                 m->private = PDE(inode);
470         }
471         return ret;
472 }
473
474 static struct file_operations r_file_operations = {
475         .open = r_open,
476         .read = seq_read,
477         .llseek = seq_lseek,
478         .release = seq_release,
479 };
480
481 static struct proc_dir_entry *proc_info_root = NULL;
482 static const char proc_info_root_name[] = "fs/reiserfs";
483
484 static void add_file(struct super_block *sb, char *name,
485                      int (*func) (struct seq_file *, struct super_block *))
486 {
487         struct proc_dir_entry *de;
488         de = create_proc_entry(name, 0, REISERFS_SB(sb)->procdir);
489         if (de) {
490                 de->data = func;
491                 de->proc_fops = &r_file_operations;
492         }
493 }
494
495 int reiserfs_proc_info_init(struct super_block *sb)
496 {
497         spin_lock_init(&__PINFO(sb).lock);
498         REISERFS_SB(sb)->procdir =
499             proc_mkdir(reiserfs_bdevname(sb), proc_info_root);
500         if (REISERFS_SB(sb)->procdir) {
501                 REISERFS_SB(sb)->procdir->owner = THIS_MODULE;
502                 REISERFS_SB(sb)->procdir->data = sb;
503                 add_file(sb, "version", show_version);
504                 add_file(sb, "super", show_super);
505                 add_file(sb, "per-level", show_per_level);
506                 add_file(sb, "bitmap", show_bitmap);
507                 add_file(sb, "on-disk-super", show_on_disk_super);
508                 add_file(sb, "oidmap", show_oidmap);
509                 add_file(sb, "journal", show_journal);
510                 return 0;
511         }
512         reiserfs_warning(sb, "reiserfs: cannot create /proc/%s/%s",
513                          proc_info_root_name, reiserfs_bdevname(sb));
514         return 1;
515 }
516
517 int reiserfs_proc_info_done(struct super_block *sb)
518 {
519         struct proc_dir_entry *de = REISERFS_SB(sb)->procdir;
520         if (de) {
521                 remove_proc_entry("journal", de);
522                 remove_proc_entry("oidmap", de);
523                 remove_proc_entry("on-disk-super", de);
524                 remove_proc_entry("bitmap", de);
525                 remove_proc_entry("per-level", de);
526                 remove_proc_entry("super", de);
527                 remove_proc_entry("version", de);
528         }
529         spin_lock(&__PINFO(sb).lock);
530         __PINFO(sb).exiting = 1;
531         spin_unlock(&__PINFO(sb).lock);
532         if (proc_info_root) {
533                 remove_proc_entry(reiserfs_bdevname(sb), proc_info_root);
534                 REISERFS_SB(sb)->procdir = NULL;
535         }
536         return 0;
537 }
538
539 struct proc_dir_entry *reiserfs_proc_register_global(char *name,
540                                                      read_proc_t * func)
541 {
542         return (proc_info_root) ? create_proc_read_entry(name, 0,
543                                                          proc_info_root,
544                                                          func, NULL) : NULL;
545 }
546
547 void reiserfs_proc_unregister_global(const char *name)
548 {
549         remove_proc_entry(name, proc_info_root);
550 }
551
552 int reiserfs_proc_info_global_init(void)
553 {
554         if (proc_info_root == NULL) {
555                 proc_info_root = proc_mkdir(proc_info_root_name, NULL);
556                 if (proc_info_root) {
557                         proc_info_root->owner = THIS_MODULE;
558                 } else {
559                         reiserfs_warning(NULL,
560                                          "reiserfs: cannot create /proc/%s",
561                                          proc_info_root_name);
562                         return 1;
563                 }
564         }
565         return 0;
566 }
567
568 int reiserfs_proc_info_global_done(void)
569 {
570         if (proc_info_root != NULL) {
571                 proc_info_root = NULL;
572                 remove_proc_entry(proc_info_root_name, NULL);
573         }
574         return 0;
575 }
576
577 /* REISERFS_PROC_INFO */
578 #else
579
580 int reiserfs_proc_info_init(struct super_block *sb)
581 {
582         return 0;
583 }
584 int reiserfs_proc_info_done(struct super_block *sb)
585 {
586         return 0;
587 }
588
589 struct proc_dir_entry *reiserfs_proc_register_global(char *name,
590                                                      read_proc_t * func)
591 {
592         return NULL;
593 }
594
595 void reiserfs_proc_unregister_global(const char *name)
596 {;
597 }
598
599 int reiserfs_proc_info_global_init(void)
600 {
601         return 0;
602 }
603 int reiserfs_proc_info_global_done(void)
604 {
605         return 0;
606 }
607
608 int reiserfs_global_version_in_proc(char *buffer, char **start,
609                                     off_t offset,
610                                     int count, int *eof, void *data)
611 {
612         return 0;
613 }
614
615 /* REISERFS_PROC_INFO */
616 #endif
617
618 /*
619  * $Log: procfs.c,v $
620  * Revision 1.1.8.2  2001/07/15 17:08:42  god
621  *  . use get_super() in procfs.c
622  *  . remove remove_save_link() from reiserfs_do_truncate()
623  *
624  * I accept terms and conditions stated in the Legal Agreement
625  * (available at http://www.namesys.com/legalese.html)
626  *
627  * Revision 1.1.8.1  2001/07/11 16:48:50  god
628  * proc info support
629  *
630  * I accept terms and conditions stated in the Legal Agreement
631  * (available at http://www.namesys.com/legalese.html)
632  *
633  */
634
635 /* 
636  * Make Linus happy.
637  * Local variables:
638  * c-indentation-style: "K&R"
639  * mode-name: "LC"
640  * c-basic-offset: 8
641  * tab-width: 8
642  * End:
643  */