Whamcloud - gitweb
Branch b1_8
[fs/lustre-release.git] / lustre / llite / lproc_llite.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #define DEBUG_SUBSYSTEM S_LLITE
37
38 #include <linux/version.h>
39 #include <lustre_lite.h>
40 #include <lprocfs_status.h>
41 #include <linux/seq_file.h>
42 #include <obd_support.h>
43
44 #include "llite_internal.h"
45
46 struct proc_dir_entry *proc_lustre_fs_root;
47
48 #ifdef LPROCFS
49 /* /proc/lustre/llite mount point registration */
50 struct file_operations llite_dump_pgcache_fops;
51 struct file_operations ll_rw_extents_stats_fops;
52 struct file_operations ll_rw_extents_stats_pp_fops;
53 struct file_operations ll_rw_offset_stats_fops;
54
55 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
56                          int *eof, void *data)
57 {
58         struct super_block *sb = (struct super_block *)data;
59         struct obd_statfs osfs;
60         int rc;
61
62         LASSERT(sb != NULL);
63         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
64                                 OBD_STATFS_NODELAY);
65         if (!rc) {
66               *eof = 1;
67               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
68         }
69
70         return rc;
71 }
72
73 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
74                              int *eof, void *data)
75 {
76         struct super_block *sb = (struct super_block *)data;
77         struct obd_statfs osfs;
78         int rc;
79
80         LASSERT(sb != NULL);
81         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
82                                 OBD_STATFS_NODELAY);
83         if (!rc) {
84                 __u32 blk_size = osfs.os_bsize >> 10;
85                 __u64 result = osfs.os_blocks;
86
87                 while (blk_size >>= 1)
88                         result <<= 1;
89
90                 *eof = 1;
91                 rc = snprintf(page, count, LPU64"\n", result);
92         }
93         return rc;
94
95 }
96
97 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
98                             int *eof, void *data)
99 {
100         struct super_block *sb = (struct super_block *)data;
101         struct obd_statfs osfs;
102         int rc;
103
104         LASSERT(sb != NULL);
105         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
106                                 OBD_STATFS_NODELAY);
107         if (!rc) {
108                 __u32 blk_size = osfs.os_bsize >> 10;
109                 __u64 result = osfs.os_bfree;
110
111                 while (blk_size >>= 1)
112                         result <<= 1;
113
114                 *eof = 1;
115                 rc = snprintf(page, count, LPU64"\n", result);
116         }
117         return rc;
118 }
119
120 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
121                              int *eof, void *data)
122 {
123         struct super_block *sb = (struct super_block *)data;
124         struct obd_statfs osfs;
125         int rc;
126
127         LASSERT(sb != NULL);
128         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
129                                 OBD_STATFS_NODELAY);
130         if (!rc) {
131                 __u32 blk_size = osfs.os_bsize >> 10;
132                 __u64 result = osfs.os_bavail;
133
134                 while (blk_size >>= 1)
135                         result <<= 1;
136
137                 *eof = 1;
138                 rc = snprintf(page, count, LPU64"\n", result);
139         }
140         return rc;
141 }
142
143 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
144                             int *eof, void *data)
145 {
146         struct super_block *sb = (struct super_block *)data;
147         struct obd_statfs osfs;
148         int rc;
149
150         LASSERT(sb != NULL);
151         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
152                                 OBD_STATFS_NODELAY);
153         if (!rc) {
154                  *eof = 1;
155                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
156         }
157         return rc;
158 }
159
160 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
161                            int *eof, void *data)
162 {
163         struct super_block *sb = (struct super_block *)data;
164         struct obd_statfs osfs;
165         int rc;
166
167         LASSERT(sb != NULL);
168         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
169                                 OBD_STATFS_NODELAY);
170         if (!rc) {
171                  *eof = 1;
172                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
173         }
174         return rc;
175
176 }
177
178 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
179                         int *eof, void *data)
180 {
181         struct super_block *sb = (struct super_block*)data;
182
183         LASSERT(sb != NULL);
184         *eof = 1;
185         return snprintf(page, count, "%s\n", sb->s_type->name);
186 }
187
188 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
189                          int *eof, void *data)
190 {
191         struct super_block *sb = (struct super_block *)data;
192
193         LASSERT(sb != NULL);
194         *eof = 1;
195         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
196 }
197
198 static int ll_rd_max_readahead_mb(char *page, char **start, off_t off,
199                                    int count, int *eof, void *data)
200 {
201         struct super_block *sb = data;
202         struct ll_sb_info *sbi = ll_s2sbi(sb);
203         long pages_number;
204         int mult;
205
206         spin_lock(&sbi->ll_lock);
207         pages_number = sbi->ll_ra_info.ra_max_pages;
208         spin_unlock(&sbi->ll_lock);
209
210         mult = 1 << (20 - CFS_PAGE_SHIFT);
211         return lprocfs_read_frac_helper(page, count, pages_number, mult);
212 }
213
214 static int ll_wr_max_readahead_mb(struct file *file, const char *buffer,
215                                    unsigned long count, void *data)
216 {
217         struct super_block *sb = data;
218         struct ll_sb_info *sbi = ll_s2sbi(sb);
219         int mult, rc, pages_number;
220
221         mult = 1 << (20 - CFS_PAGE_SHIFT);
222         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
223         if (rc)
224                 return rc;
225
226         if (pages_number < 0 || pages_number > num_physpages / 2) {
227                 CERROR("can't set file readahead more than %lu MB\n",
228                         num_physpages >> (20 - CFS_PAGE_SHIFT + 1)); /*1/2 of RAM*/
229                 return -ERANGE;
230         }
231
232         spin_lock(&sbi->ll_lock);
233         sbi->ll_ra_info.ra_max_pages = pages_number;
234         spin_unlock(&sbi->ll_lock);
235
236         return count;
237 }
238
239 static int ll_rd_max_readahead_per_file_mb(char *page, char **start, off_t off,
240                                           int count, int *eof, void *data)
241 {
242         struct super_block *sb = data;
243         struct ll_sb_info *sbi = ll_s2sbi(sb);
244         long pages_number;
245         int mult;
246
247         spin_lock(&sbi->ll_lock);
248         pages_number = sbi->ll_ra_info.ra_max_pages_per_file;
249         spin_unlock(&sbi->ll_lock);
250
251         mult = 1 << (20 - CFS_PAGE_SHIFT);
252         return lprocfs_read_frac_helper(page, count, pages_number, mult);
253 }
254
255 static int ll_wr_max_readahead_per_file_mb(struct file *file, const char *buffer,
256                                           unsigned long count, void *data)
257 {
258         struct super_block *sb = data;
259         struct ll_sb_info *sbi = ll_s2sbi(sb);
260         int mult, rc, pages_number;
261
262         mult = 1 << (20 - CFS_PAGE_SHIFT);
263         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
264         if (rc)
265                 return rc;
266
267         if (pages_number < 0 ||
268                 pages_number > sbi->ll_ra_info.ra_max_pages) {
269                 CERROR("can't set file readahead more than"
270                        "max_read_ahead_mb %lu MB\n", sbi->ll_ra_info.ra_max_pages);
271                 return -ERANGE;
272         }
273
274         spin_lock(&sbi->ll_lock);
275         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
276         spin_unlock(&sbi->ll_lock);
277
278         return count;
279 }
280
281 static int ll_rd_max_read_ahead_whole_mb(char *page, char **start, off_t off,
282                                        int count, int *eof, void *data)
283 {
284         struct super_block *sb = data;
285         struct ll_sb_info *sbi = ll_s2sbi(sb);
286         long pages_number;
287         int mult;
288
289         spin_lock(&sbi->ll_lock);
290         pages_number = sbi->ll_ra_info.ra_max_read_ahead_whole_pages;
291         spin_unlock(&sbi->ll_lock);
292
293         mult = 1 << (20 - CFS_PAGE_SHIFT);
294         return lprocfs_read_frac_helper(page, count, pages_number, mult);
295 }
296
297 static int ll_wr_max_read_ahead_whole_mb(struct file *file, const char *buffer,
298                                        unsigned long count, void *data)
299 {
300         struct super_block *sb = data;
301         struct ll_sb_info *sbi = ll_s2sbi(sb);
302         int mult, rc, pages_number;
303
304         mult = 1 << (20 - CFS_PAGE_SHIFT);
305         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
306         if (rc)
307                 return rc;
308
309         /* Cap this at the current max readahead window size, the readahead
310          * algorithm does this anyway so it's pointless to set it larger. */
311         if (pages_number < 0 ||
312             pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
313                 CERROR("can't set max_read_ahead_whole_mb more than "
314                        "max_read_ahead_per_file_mb: %lu\n",
315                         sbi->ll_ra_info.ra_max_pages_per_file >> (20 - CFS_PAGE_SHIFT));
316                 return -ERANGE;
317         }
318
319         spin_lock(&sbi->ll_lock);
320         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
321         spin_unlock(&sbi->ll_lock);
322
323         return count;
324 }
325
326 static int ll_rd_max_cached_mb(char *page, char **start, off_t off,
327                                int count, int *eof, void *data)
328 {
329         struct super_block *sb = data;
330         struct ll_sb_info *sbi = ll_s2sbi(sb);
331         long pages_number;
332         int mult;
333
334         spin_lock(&sbi->ll_lock);
335         pages_number = sbi->ll_async_page_max;
336         spin_unlock(&sbi->ll_lock);
337
338         mult = 1 << (20 - CFS_PAGE_SHIFT);
339         return lprocfs_read_frac_helper(page, count, pages_number, mult);;
340 }
341
342 static int ll_wr_max_cached_mb(struct file *file, const char *buffer,
343                                   unsigned long count, void *data)
344 {
345         struct super_block *sb = data;
346         struct ll_sb_info *sbi = ll_s2sbi(sb);
347         unsigned long budget;
348         int mult, rc, pages_number, cpu;
349
350         mult = 1 << (20 - CFS_PAGE_SHIFT);
351         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
352         if (rc)
353                 return rc;
354
355         if (pages_number < 0 || pages_number > num_physpages) {
356                 CERROR("can't set max cache more than %lu MB\n",
357                         num_physpages >> (20 - CFS_PAGE_SHIFT));
358                 return -ERANGE;
359         }
360
361         spin_lock(&sbi->ll_lock);
362         sbi->ll_async_page_max = pages_number ;
363         spin_unlock(&sbi->ll_lock);
364
365         if (!sbi->ll_osc_exp)
366                 /* Not set up yet, don't call llap_shrink_cache */
367                 return count;
368
369         spin_lock(&sbi->ll_async_page_reblnc_lock);
370         budget = sbi->ll_async_page_max / num_online_cpus();
371         for_each_online_cpu(cpu)
372                 LL_PGLIST_DATA_CPU(sbi, cpu)->llpd_budget = budget;
373         spin_unlock(&sbi->ll_async_page_reblnc_lock);
374
375         if (lcounter_read_positive(&sbi->ll_async_page_count) >=
376             sbi->ll_async_page_max)
377                 llap_shrink_cache(sbi, -1);
378
379         return count;
380 }
381
382 static int ll_rd_pgcache_balance(char *page, char **start, off_t off,
383                                  int count, int *eof, void *data)
384 {
385         struct super_block *sb = data;
386         struct ll_sb_info *sbi = ll_s2sbi(sb);
387         struct ll_pglist_data *pd;
388         unsigned long total_budget = 0;
389         int n = 0, cpu;
390
391         n += snprintf(page +n, count - n, "cpu\tpage count\tbudget"
392                       "\t\treblnc count\tgen\thit\tmiss\tcross\n");
393         for_each_online_cpu(cpu) {
394                 pd = LL_PGLIST_DATA_CPU(sbi, cpu);
395                 n += snprintf(page + n, count - n,
396                               "%d\t%-8lu\t%-8lu\t%-8lu\t%lu\t%lu\t%lu\t%lu\n",
397                               cpu, pd->llpd_count, pd->llpd_budget,
398                               pd->llpd_reblnc_count, pd->llpd_gen,
399                               pd->llpd_hit, pd->llpd_miss, pd->llpd_cross);
400                 total_budget += pd->llpd_budget;
401         }
402         n += snprintf(page + n, count - n,
403                       "Total budget: %lu, page max: %lu, rebalance cnt: %lu\n",
404                       total_budget, sbi->ll_async_page_max,
405                       sbi->ll_async_page_reblnc_count);
406         *eof = 1;
407         return n;
408 }
409
410 static int ll_rd_checksum(char *page, char **start, off_t off,
411                           int count, int *eof, void *data)
412 {
413         struct super_block *sb = data;
414         struct ll_sb_info *sbi = ll_s2sbi(sb);
415
416         return snprintf(page, count, "%u\n",
417                         (sbi->ll_flags & LL_SBI_LLITE_CHECKSUM) ? 1 : 0);
418 }
419
420 static int ll_wr_checksum(struct file *file, const char *buffer,
421                           unsigned long count, void *data)
422 {
423         struct super_block *sb = data;
424         struct ll_sb_info *sbi = ll_s2sbi(sb);
425         int val, rc;
426
427         if (!sbi->ll_osc_exp)
428                 /* Not set up yet */
429                 return -EAGAIN;
430
431         rc = lprocfs_write_helper(buffer, count, &val);
432         if (rc)
433                 return rc;
434         if (val)
435                 sbi->ll_flags |=  (LL_SBI_LLITE_CHECKSUM|LL_SBI_DATA_CHECKSUM);
436         else
437                 sbi->ll_flags &= ~(LL_SBI_LLITE_CHECKSUM|LL_SBI_DATA_CHECKSUM);
438
439         rc = obd_set_info_async(sbi->ll_osc_exp, sizeof(KEY_CHECKSUM), KEY_CHECKSUM,
440                                 sizeof(val), &val, NULL);
441         if (rc)
442                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
443
444         return count;
445 }
446
447 static int ll_rd_max_rw_chunk(char *page, char **start, off_t off,
448                           int count, int *eof, void *data)
449 {
450         struct super_block *sb = data;
451
452         return snprintf(page, count, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
453 }
454
455 static int ll_wr_max_rw_chunk(struct file *file, const char *buffer,
456                           unsigned long count, void *data)
457 {
458         struct super_block *sb = data;
459         int rc, val;
460
461         rc = lprocfs_write_helper(buffer, count, &val);
462         if (rc)
463                 return rc;
464         ll_s2sbi(sb)->ll_max_rw_chunk = val;
465         return count;
466 }
467
468 static int ll_rd_track_id(char *page, int count, void *data,
469                           enum stats_track_type type)
470 {
471         struct super_block *sb = data;
472
473         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
474                 return snprintf(page, count, "%d\n",
475                                 ll_s2sbi(sb)->ll_stats_track_id);
476
477         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
478                 return snprintf(page, count, "0 (all)\n");
479         } else {
480                 return snprintf(page, count, "untracked\n");
481         }
482 }
483
484 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
485                           enum stats_track_type type)
486 {
487         struct super_block *sb = data;
488         int rc, pid;
489
490         rc = lprocfs_write_helper(buffer, count, &pid);
491         if (rc)
492                 return rc;
493         ll_s2sbi(sb)->ll_stats_track_id = pid;
494         if (pid == 0)
495                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
496         else
497                 ll_s2sbi(sb)->ll_stats_track_type = type;
498         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
499         return count;
500 }
501
502 static int ll_rd_track_pid(char *page, char **start, off_t off,
503                           int count, int *eof, void *data)
504 {
505         return (ll_rd_track_id(page, count, data, STATS_TRACK_PID));
506 }
507
508 static int ll_wr_track_pid(struct file *file, const char *buffer,
509                           unsigned long count, void *data)
510 {
511         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PID));
512 }
513
514 static int ll_rd_track_ppid(char *page, char **start, off_t off,
515                           int count, int *eof, void *data)
516 {
517         return (ll_rd_track_id(page, count, data, STATS_TRACK_PPID));
518 }
519
520 static int ll_wr_track_ppid(struct file *file, const char *buffer,
521                           unsigned long count, void *data)
522 {
523         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PPID));
524 }
525
526 static int ll_rd_track_gid(char *page, char **start, off_t off,
527                           int count, int *eof, void *data)
528 {
529         return (ll_rd_track_id(page, count, data, STATS_TRACK_GID));
530 }
531
532 static int ll_wr_track_gid(struct file *file, const char *buffer,
533                           unsigned long count, void *data)
534 {
535         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_GID));
536 }
537
538 static int ll_rd_contention_time(char *page, char **start, off_t off,
539                                  int count, int *eof, void *data)
540 {
541         struct super_block *sb = data;
542
543         *eof = 1;
544         return snprintf(page, count, "%u\n", ll_s2sbi(sb)->ll_contention_time);
545
546 }
547
548 static int ll_wr_contention_time(struct file *file, const char *buffer,
549                                  unsigned long count, void *data)
550 {
551         struct super_block *sb = data;
552         struct ll_sb_info *sbi = ll_s2sbi(sb);
553
554         return lprocfs_write_helper(buffer, count,&sbi->ll_contention_time) ?:
555                 count;
556 }
557
558 static int ll_rd_lockless_truncate(char *page, char **start, off_t off,
559                                    int count, int *eof, void *data)
560 {
561         struct super_block *sb = data;
562
563         *eof = 1;
564         return snprintf(page, count, "%u\n",
565                         ll_s2sbi(sb)->ll_lockless_truncate_enable);
566 }
567
568 static int ll_wr_lockless_truncate(struct file *file, const char *buffer,
569                                    unsigned long count, void *data)
570 {
571         struct super_block *sb = data;
572         struct ll_sb_info *sbi = ll_s2sbi(sb);
573
574         return lprocfs_write_helper(buffer, count,
575                                     &sbi->ll_lockless_truncate_enable)
576                 ?: count;
577 }
578
579 static int ll_rd_statahead_max(char *page, char **start, off_t off,
580                                int count, int *eof, void *data)
581 {
582         struct super_block *sb = data;
583         struct ll_sb_info *sbi = ll_s2sbi(sb);
584
585         return snprintf(page, count, "%u\n", sbi->ll_sa_max);
586 }
587
588 static int ll_wr_statahead_max(struct file *file, const char *buffer,
589                                unsigned long count, void *data)
590 {
591         struct super_block *sb = data;
592         struct ll_sb_info *sbi = ll_s2sbi(sb);
593         int val, rc;
594
595         rc = lprocfs_write_helper(buffer, count, &val);
596         if (rc)
597                 return rc;
598
599         if (val >= 0 && val <= LL_SA_RPC_MAX)
600                 sbi->ll_sa_max = val;
601         else
602                 CERROR("Bad statahead_max value %d. Valid values are in the "
603                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
604
605         return count;
606 }
607
608 static int ll_rd_statahead_stats(char *page, char **start, off_t off,
609                                  int count, int *eof, void *data)
610 {
611         struct super_block *sb = data;
612         struct ll_sb_info *sbi = ll_s2sbi(sb);
613
614         return snprintf(page, count,
615                         "statahead wrong: %u\n"
616                         "statahead total: %u\n"
617                         "ls blocked:      %llu\n"
618                         "ls cached:       %llu\n"
619                         "hit count:       %llu\n"
620                         "miss count:      %llu\n",
621                         sbi->ll_sa_wrong,
622                         sbi->ll_sa_total,
623                         sbi->ll_sa_blocked,
624                         sbi->ll_sa_cached,
625                         sbi->ll_sa_hit,
626                         sbi->ll_sa_miss);
627 }
628
629 static int ll_rd_lazystatfs(char *page, char **start, off_t off,
630                             int count, int *eof, void *data)
631 {
632         struct super_block *sb = data;
633         struct ll_sb_info *sbi = ll_s2sbi(sb);
634
635         return snprintf(page, count, "%u\n",
636                         (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
637 }
638
639 static int ll_wr_lazystatfs(struct file *file, const char *buffer,
640                             unsigned long count, void *data)
641 {
642         struct super_block *sb = data;
643         struct ll_sb_info *sbi = ll_s2sbi(sb);
644         int val, rc;
645
646         rc = lprocfs_write_helper(buffer, count, &val);
647         if (rc)
648                 return rc;
649
650         if (val)
651                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
652         else
653                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
654
655         return count;
656 }
657
658 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
659         { "uuid",         ll_rd_sb_uuid,          0, 0 },
660         //{ "mntpt_path",   ll_rd_path,             0, 0 },
661         { "fstype",       ll_rd_fstype,           0, 0 },
662         { "blocksize",    ll_rd_blksize,          0, 0 },
663         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
664         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
665         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
666         { "filestotal",   ll_rd_filestotal,       0, 0 },
667         { "filesfree",    ll_rd_filesfree,        0, 0 },
668         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
669         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
670                                ll_wr_max_readahead_mb, 0 },
671         { "max_read_ahead_per_file_mb", ll_rd_max_readahead_per_file_mb,
672                                         ll_wr_max_readahead_per_file_mb, 0 },
673         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
674                                      ll_wr_max_read_ahead_whole_mb, 0 },
675         { "max_cached_mb",  ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
676         { "pgcache_balance",ll_rd_pgcache_balance, 0, 0 },
677         { "checksum_pages", ll_rd_checksum, ll_wr_checksum, 0 },
678         { "max_rw_chunk",   ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
679         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
680         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
681         { "stats_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
682         { "contention_seconds", ll_rd_contention_time,
683                                 ll_wr_contention_time, 0},
684         { "lockless_truncate", ll_rd_lockless_truncate,
685                                ll_wr_lockless_truncate, 0},
686         { "statahead_max",      ll_rd_statahead_max, ll_wr_statahead_max, 0 },
687         { "statahead_stats",    ll_rd_statahead_stats, 0, 0 },
688         { "lazystatfs",         ll_rd_lazystatfs, ll_wr_lazystatfs, 0 },
689         { 0 }
690 };
691
692 #define MAX_STRING_SIZE 128
693
694 struct llite_file_opcode {
695         __u32       opcode;
696         __u32       type;
697         const char *opname;
698 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
699         /* file operation */
700         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
701         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
702         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
703                                    "writeback_from_writepage" },
704         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
705                                    "writeback_from_pressure" },
706         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
707                                    "writeback_ok_pages" },
708         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
709                                    "writeback_failed_pages" },
710         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
711                                    "read_bytes" },
712         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
713                                    "write_bytes" },
714         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
715                                    "brw_read" },
716         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
717                                    "brw_write" },
718
719         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
720         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
721         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
722         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
723         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
724         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
725         /* inode operation */
726         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
727         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
728         { LPROC_LL_LOCKLESS_TRUNC, LPROCFS_TYPE_REGS, "lockless_truncate" },
729         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
730         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
731         /* special inode operation */
732         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
733         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
734         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
735         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
736         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
737         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
738         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
739         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
740                                    "direct_read" },
741         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
742                                    "direct_write" },
743         { LPROC_LL_LOCKLESS_READ,  LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
744                                    "lockless_read_bytes" },
745         { LPROC_LL_LOCKLESS_WRITE, LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
746                                    "lockless_write_bytes" },
747
748 };
749
750 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
751 {
752         if (!sbi->ll_stats)
753                 return;
754         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
755                 lprocfs_counter_add(sbi->ll_stats, op, count);
756         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
757                  sbi->ll_stats_track_id == current->pid)
758                 lprocfs_counter_add(sbi->ll_stats, op, count);
759         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
760                  sbi->ll_stats_track_id == current->p_pptr->pid)
761                 lprocfs_counter_add(sbi->ll_stats, op, count);
762         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
763                  sbi->ll_stats_track_id == current->gid)
764                 lprocfs_counter_add(sbi->ll_stats, op, count);
765 }
766 EXPORT_SYMBOL(ll_stats_ops_tally);
767
768 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
769                                 struct super_block *sb, char *osc, char *mdc)
770 {
771         struct lprocfs_vars lvars[2];
772         struct lustre_sb_info *lsi = s2lsi(sb);
773         struct ll_sb_info *sbi = ll_s2sbi(sb);
774         struct obd_device *obd;
775         char name[MAX_STRING_SIZE + 1], *ptr;
776         int err, id, len;
777         struct proc_dir_entry *entry;
778         static const char *ra_stats_string[] = LL_RA_STAT_STRINGS;
779         ENTRY;
780
781         memset(lvars, 0, sizeof(lvars));
782
783         name[MAX_STRING_SIZE] = '\0';
784         lvars[0].name = name;
785
786         LASSERT(sbi != NULL);
787         LASSERT(mdc != NULL);
788         LASSERT(osc != NULL);
789
790         /* Get fsname */
791         len = strlen(lsi->lsi_lmd->lmd_profile);
792         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
793         if (ptr && (strcmp(ptr, "-client") == 0))
794                 len -= 7;
795
796         /* Mount info */
797         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
798                  lsi->lsi_lmd->lmd_profile, sb);
799
800         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
801         if (IS_ERR(sbi->ll_proc_root)) {
802                 err = PTR_ERR(sbi->ll_proc_root);
803                 sbi->ll_proc_root = NULL;
804                 RETURN(err);
805         }
806
807         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
808         if (entry == NULL)
809                 GOTO(out, err = -ENOMEM);
810         entry->proc_fops = &llite_dump_pgcache_fops;
811         entry->data = sbi;
812
813         sbi->ll_ra_stats = lprocfs_alloc_stats(LL_RA_STAT,
814                                                LPROCFS_STATS_FLAG_NONE);
815         for (id = 0; id < LL_RA_STAT; id++)
816                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
817                         ra_stats_string[id], "pages");
818         lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
819                 sbi->ll_ra_stats);
820
821         entry = create_proc_entry("extents_stats", 0644, sbi->ll_proc_root);
822         if (entry == NULL)
823                  GOTO(out, err = -ENOMEM);
824         entry->proc_fops = &ll_rw_extents_stats_fops;
825         entry->data = sbi;
826
827         entry = create_proc_entry("extents_stats_per_process", 0644,
828                                   sbi->ll_proc_root);
829         if (entry == NULL)
830                  GOTO(out, err = -ENOMEM);
831         entry->proc_fops = &ll_rw_extents_stats_pp_fops;
832         entry->data = sbi;
833
834         entry = create_proc_entry("offset_stats", 0644, sbi->ll_proc_root);
835         if (entry == NULL)
836                 GOTO(out, err = -ENOMEM);
837         entry->proc_fops = &ll_rw_offset_stats_fops;
838         entry->data = sbi;
839
840         /* File operations stats */
841         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
842                                             LPROCFS_STATS_FLAG_NONE);
843         if (sbi->ll_stats == NULL)
844                 GOTO(out, err = -ENOMEM);
845         /* do counter init */
846         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
847                 __u32 type = llite_opcode_table[id].type;
848                 void *ptr = NULL;
849                 if (type & LPROCFS_TYPE_REGS)
850                         ptr = "regs";
851                 else if (type & LPROCFS_TYPE_BYTES)
852                         ptr = "bytes";
853                 else if (type & LPROCFS_TYPE_PAGES)
854                         ptr = "pages";
855                 lprocfs_counter_init(sbi->ll_stats,
856                                      llite_opcode_table[id].opcode,
857                                      (type & LPROCFS_CNTR_AVGMINMAX),
858                                      llite_opcode_table[id].opname, ptr);
859         }
860         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
861         if (err)
862                 GOTO(out, err);
863
864         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
865         if (err)
866                 GOTO(out, err);
867
868         /* MDC info */
869         obd = class_name2obd(mdc);
870
871         LASSERT(obd != NULL);
872         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
873         LASSERT(obd->obd_type->typ_name != NULL);
874
875         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
876                  obd->obd_type->typ_name);
877         lvars[0].read_fptr = lprocfs_rd_name;
878         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
879         if (err)
880                 GOTO(out, err);
881
882         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
883         lvars[0].read_fptr = lprocfs_rd_uuid;
884         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
885         if (err)
886                 GOTO(out, err);
887
888         /* OSC */
889         obd = class_name2obd(osc);
890
891         LASSERT(obd != NULL);
892         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
893         LASSERT(obd->obd_type->typ_name != NULL);
894
895         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
896                  obd->obd_type->typ_name);
897         lvars[0].read_fptr = lprocfs_rd_name;
898         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
899         if (err)
900                 GOTO(out, err);
901
902         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
903         lvars[0].read_fptr = lprocfs_rd_uuid;
904         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
905 out:
906         if (err) {
907                 lprocfs_remove(&sbi->ll_proc_root);
908                 lprocfs_free_stats(&sbi->ll_stats);
909         }
910         RETURN(err);
911 }
912
913 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
914 {
915         if (sbi->ll_proc_root) {
916                 lprocfs_remove(&sbi->ll_proc_root);
917                 lprocfs_free_stats(&sbi->ll_ra_stats);
918                 lprocfs_free_stats(&sbi->ll_stats);
919         }
920 }
921 #undef MAX_STRING_SIZE
922
923 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
924                 if (test_bit(PG_##flag, &(page)->flags)) {              \
925                         if (!has_flags)                                 \
926                                 has_flags = 1;                          \
927                         else                                            \
928                                 seq_putc(seq, '|');                     \
929                         seq_puts(seq, #flag);                           \
930                 }                                                       \
931         } while(0);
932
933 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
934 {
935         struct ll_async_page *dummy_llap = seq->private;
936
937         if (dummy_llap->llap_magic == 2)
938                 return NULL;
939
940         return (void *)1;
941 }
942
943 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
944 {
945         struct ll_async_page *llap, *dummy_llap = seq->private;
946         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
947         struct ll_pglist_data *pd;
948         int cpu = dummy_llap->llap_pglist_cpu;
949
950         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
951          * it in our own state */
952         if (dummy_llap->llap_magic == 0) {
953                 seq_printf(seq, "gener |  llap  cookie  origin wq du wb | page "
954                                 "inode index count [ page flags ]\n");
955                 return 0;
956         }
957
958         pd = ll_pglist_cpu_lock(sbi, cpu);
959         llap = llite_pglist_next_llap(&pd->llpd_list,
960                                       &dummy_llap->llap_pglist_item);
961         if (llap != NULL)  {
962                 int has_flags = 0, i;
963                 struct page *page = llap->llap_page;
964                 unsigned long gen = 0UL;
965
966                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
967                          llap->llap_origin);
968
969                 for_each_online_cpu(i)
970                          gen += LL_PGLIST_DATA_CPU(sbi, i)->llpd_gen;
971
972                 seq_printf(seq," %5lu | %p %p %s %s %s %s | %p %lu/%u(%p) "
973                            "%lu %u [",
974                            gen,
975                            llap, llap->llap_cookie,
976                            llap_origins[llap->llap_origin],
977                            llap->llap_write_queued ? "wq" : "- ",
978                            llap->llap_defer_uptodate ? "du" : "- ",
979                            PageWriteback(page) ? "wb" : "-",
980                            page, page->mapping->host->i_ino,
981                            page->mapping->host->i_generation,
982                            page->mapping->host, page->index,
983                            page_count(page));
984                 seq_page_flag(seq, page, locked, has_flags);
985                 seq_page_flag(seq, page, error, has_flags);
986                 seq_page_flag(seq, page, referenced, has_flags);
987                 seq_page_flag(seq, page, uptodate, has_flags);
988                 seq_page_flag(seq, page, dirty, has_flags);
989 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,12))
990                 seq_page_flag(seq, page, highmem, has_flags);
991 #endif
992                 seq_page_flag(seq, page, writeback, has_flags);
993                 if (!has_flags)
994                         seq_puts(seq, "-]\n");
995                 else
996                         seq_puts(seq, "]\n");
997         }
998         ll_pglist_cpu_unlock(sbi, cpu);
999
1000         return 0;
1001 }
1002
1003 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v,
1004                                          loff_t *pos)
1005 {
1006         struct ll_async_page *llap, *dummy_llap = seq->private;
1007         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
1008         struct ll_pglist_data *pd, *next;
1009         int cpu = dummy_llap->llap_pglist_cpu;
1010
1011         /* bail if we just displayed the banner */
1012         if (dummy_llap->llap_magic == 0) {
1013                 dummy_llap->llap_magic = 1;
1014                 return dummy_llap;
1015         }
1016
1017         /* we've just displayed the llap that is after us in the list.
1018          * we advance to a position beyond it, returning null if there
1019          * isn't another llap in the list beyond that new position. */
1020         pd = ll_pglist_cpu_lock(sbi, cpu);
1021         llap = llite_pglist_next_llap(&pd->llpd_list,
1022                         &dummy_llap->llap_pglist_item);
1023         list_del_init(&dummy_llap->llap_pglist_item);
1024         if (llap) {
1025                 list_add(&dummy_llap->llap_pglist_item,&llap->llap_pglist_item);
1026                 llap = llite_pglist_next_llap(&pd->llpd_list,
1027                                 &dummy_llap->llap_pglist_item);
1028         }
1029         if (llap == NULL) {
1030                 int i = cpu + 1;
1031                 for (next = NULL; i < num_possible_cpus(); i++, next = NULL) {
1032                         next = ll_pglist_cpu_lock(sbi, i);
1033                         if (!list_empty(&next->llpd_list))
1034                                 break;
1035                         ll_pglist_cpu_unlock(sbi, i);
1036                 }
1037                 if (next != NULL) {
1038                         list_move(&dummy_llap->llap_pglist_item,
1039                                   &next->llpd_list);
1040                         dummy_llap->llap_pglist_cpu = i;
1041                         ll_pglist_cpu_unlock(sbi, cpu);
1042                         llap = llite_pglist_next_llap(&next->llpd_list,
1043                                         &dummy_llap->llap_pglist_item);
1044                         LASSERT(llap);
1045                         cpu = i;
1046                 }
1047         }
1048         ll_pglist_cpu_unlock(sbi, cpu);
1049
1050         ++*pos;
1051         if (llap == NULL) {
1052                 dummy_llap->llap_magic = 2;
1053                 return NULL;
1054         }
1055         return dummy_llap;
1056 }
1057
1058 static void null_stop(struct seq_file *seq, void *v)
1059 {
1060 }
1061
1062 struct seq_operations llite_dump_pgcache_seq_sops = {
1063         .start = llite_dump_pgcache_seq_start,
1064         .stop = null_stop,
1065         .next = llite_dump_pgcache_seq_next,
1066         .show = llite_dump_pgcache_seq_show,
1067 };
1068
1069 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
1070  * while we walk the entire list, and we don't want to have to seek into
1071  * the right position in the list as an app advances with many syscalls.  we
1072  * allocate a dummy llap and hang it off file->private.  its position in
1073  * the list records where the app is currently displaying.  this way our
1074  * seq .start and .stop don't actually do anything.  .next returns null
1075  * when the dummy hits the end of the list which eventually leads to .release
1076  * where we tear down.  this kind of displaying is super-racey, so we put
1077  * a generation counter on the list so the output shows when the list
1078  * changes between reads.
1079  */
1080 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
1081 {
1082         struct proc_dir_entry *dp = PDE(inode);
1083         struct ll_async_page *dummy_llap;
1084         struct seq_file *seq;
1085         struct ll_sb_info *sbi = dp->data;
1086         struct ll_pglist_data *pd;
1087         int rc = -ENOMEM;
1088
1089         LPROCFS_ENTRY_AND_CHECK(dp);
1090
1091         OBD_ALLOC_PTR_WAIT(dummy_llap);
1092         if (dummy_llap == NULL)
1093                 GOTO(out, rc);
1094
1095         dummy_llap->llap_page = NULL;
1096         dummy_llap->llap_cookie = sbi;
1097         dummy_llap->llap_magic = 0;
1098         dummy_llap->llap_pglist_cpu = 0;
1099
1100         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
1101         if (rc) {
1102                 OBD_FREE(dummy_llap, sizeof(*dummy_llap));
1103                 GOTO(out, rc);
1104         }
1105         seq = file->private_data;
1106         seq->private = dummy_llap;
1107
1108         pd = ll_pglist_cpu_lock(sbi, 0);
1109         list_add(&dummy_llap->llap_pglist_item, &pd->llpd_list);
1110         ll_pglist_cpu_unlock(sbi, 0);
1111
1112 out:
1113         if (rc)
1114                 LPROCFS_EXIT();
1115         return rc;
1116 }
1117
1118 static int llite_dump_pgcache_seq_release(struct inode *inode,
1119                                           struct file *file)
1120 {
1121         struct seq_file *seq = file->private_data;
1122         struct ll_async_page *dummy_llap = seq->private;
1123         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
1124         int cpu = dummy_llap->llap_pglist_cpu;
1125
1126         ll_pglist_cpu_lock(sbi, cpu);
1127         if (!list_empty(&dummy_llap->llap_pglist_item))
1128                 list_del_init(&dummy_llap->llap_pglist_item);
1129         ll_pglist_cpu_unlock(sbi, cpu);
1130         OBD_FREE(dummy_llap, sizeof(*dummy_llap));
1131
1132         return lprocfs_seq_release(inode, file);
1133 }
1134
1135 struct file_operations llite_dump_pgcache_fops = {
1136         .owner   = THIS_MODULE,
1137         .open    = llite_dump_pgcache_seq_open,
1138         .read    = seq_read,
1139         .release = llite_dump_pgcache_seq_release,
1140 };
1141
1142 #define pct(a,b) (b ? a * 100 / b : 0)
1143
1144 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1145                                    struct seq_file *seq, int which)
1146 {
1147         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1148         unsigned long start, end, r, w;
1149         char *unitp = "KMGTPEZY";
1150         int i, units = 10;
1151         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1152
1153         read_cum = 0;
1154         write_cum = 0;
1155         start = 0;
1156
1157         for(i = 0; i < LL_HIST_MAX; i++) {
1158                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1159                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1160         }
1161
1162         for(i = 0; i < LL_HIST_MAX; i++) {
1163                 r = pp_info->pp_r_hist.oh_buckets[i];
1164                 w = pp_info->pp_w_hist.oh_buckets[i];
1165                 read_cum += r;
1166                 write_cum += w;
1167                 end = 1 << (i + LL_HIST_START - units);
1168                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1169                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1170                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1171                            r, pct(r, read_tot), pct(read_cum, read_tot),
1172                            w, pct(w, write_tot), pct(write_cum, write_tot));
1173                 start = end;
1174                 if (start == 1<<10) {
1175                         start = 1;
1176                         units += 10;
1177                         unitp++;
1178                 }
1179                 if (read_cum == read_tot && write_cum == write_tot)
1180                         break;
1181         }
1182 }
1183
1184 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1185 {
1186         struct timeval now;
1187         struct ll_sb_info *sbi = seq->private;
1188         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1189         int k;
1190
1191         do_gettimeofday(&now);
1192
1193         if (!sbi->ll_rw_stats_on) {
1194                 seq_printf(seq, "disabled\n"
1195                                 "write anything in this file to activate, "
1196                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1197                 return 0;
1198         }
1199         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1200                    now.tv_sec, now.tv_usec);
1201         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1202         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1203                    "extents", "calls", "%", "cum%",
1204                    "calls", "%", "cum%");
1205         spin_lock(&sbi->ll_pp_extent_lock);
1206         for(k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1207                 if(io_extents->pp_extents[k].pid != 0) {
1208                         seq_printf(seq, "\nPID: %d\n",
1209                                    io_extents->pp_extents[k].pid);
1210                         ll_display_extents_info(io_extents, seq, k);
1211                 }
1212         }
1213         spin_unlock(&sbi->ll_pp_extent_lock);
1214         return 0;
1215 }
1216
1217 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1218                                                 const char *buf, size_t len,
1219                                                 loff_t *off)
1220 {
1221         struct seq_file *seq = file->private_data;
1222         struct ll_sb_info *sbi = seq->private;
1223         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1224         int i;
1225         int value = 1, rc = 0;
1226
1227         rc = lprocfs_write_helper(buf, len, &value);
1228         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1229                        strcmp(buf, "Disabled") == 0))
1230                 value = 0;
1231
1232         if (value == 0)
1233                 sbi->ll_rw_stats_on = 0;
1234         else
1235                 sbi->ll_rw_stats_on = 1;
1236
1237         spin_lock(&sbi->ll_pp_extent_lock);
1238         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1239                 io_extents->pp_extents[i].pid = 0;
1240                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1241                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1242         }
1243         spin_unlock(&sbi->ll_pp_extent_lock);
1244         return len;
1245 }
1246
1247 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1248
1249 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1250 {
1251         struct timeval now;
1252         struct ll_sb_info *sbi = seq->private;
1253         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1254
1255         do_gettimeofday(&now);
1256
1257         if (!sbi->ll_rw_stats_on) {
1258                 seq_printf(seq, "disabled\n"
1259                                 "write anything in this file to activate, "
1260                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1261                 return 0;
1262         }
1263         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1264                    now.tv_sec, now.tv_usec);
1265
1266         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1267         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1268                    "extents", "calls", "%", "cum%",
1269                    "calls", "%", "cum%");
1270         spin_lock(&sbi->ll_lock);
1271         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1272         spin_unlock(&sbi->ll_lock);
1273
1274         return 0;
1275 }
1276
1277 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1278                                         size_t len, loff_t *off)
1279 {
1280         struct seq_file *seq = file->private_data;
1281         struct ll_sb_info *sbi = seq->private;
1282         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1283         int i;
1284         int value = 1, rc = 0;
1285
1286         rc = lprocfs_write_helper(buf, len, &value);
1287         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1288                        strcmp(buf, "Disabled") == 0))
1289                 value = 0;
1290
1291         if (value == 0)
1292                 sbi->ll_rw_stats_on = 0;
1293         else
1294                 sbi->ll_rw_stats_on = 1;
1295         spin_lock(&sbi->ll_pp_extent_lock);
1296         for(i = 0; i <= LL_PROCESS_HIST_MAX; i++)
1297         {
1298                 io_extents->pp_extents[i].pid = 0;
1299                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1300                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1301         }
1302         spin_unlock(&sbi->ll_pp_extent_lock);
1303
1304         return len;
1305 }
1306
1307 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1308
1309 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid, struct file
1310                                *file, size_t count, int rw)
1311 {
1312         int i, cur = -1;
1313         struct ll_rw_process_info *process;
1314         struct ll_rw_process_info *offset;
1315         int *off_count = &sbi->ll_rw_offset_entry_count;
1316         int *process_count = &sbi->ll_offset_process_count;
1317         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1318
1319         if(!sbi->ll_rw_stats_on)
1320                 return;
1321         process = sbi->ll_rw_process_info;
1322         offset = sbi->ll_rw_offset_info;
1323
1324         spin_lock(&sbi->ll_pp_extent_lock);
1325         /* Extent statistics */
1326         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1327                 if(io_extents->pp_extents[i].pid == pid) {
1328                         cur = i;
1329                         break;
1330                 }
1331         }
1332
1333         if (cur == -1) {
1334                 /* new process */
1335                 sbi->ll_extent_process_count =
1336                         (sbi->ll_extent_process_count+1) % LL_PROCESS_HIST_MAX;
1337                 cur = sbi->ll_extent_process_count;
1338                 io_extents->pp_extents[cur].pid = pid;
1339                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1340                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1341         }
1342
1343         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1344              (i < (LL_HIST_MAX - 1)); i++);
1345         if (rw == 0) {
1346                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1347                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1348         } else {
1349                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1350                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1351         }
1352         spin_unlock(&sbi->ll_pp_extent_lock);
1353
1354         spin_lock(&sbi->ll_process_lock);
1355         /* Offset statistics */
1356         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1357                 if (process[i].rw_pid == pid) {
1358                         if (process[i].rw_last_file != file) {
1359                                 process[i].rw_range_start = file->f_pos;
1360                                 process[i].rw_last_file_pos =
1361                                                         file->f_pos + count;
1362                                 process[i].rw_smallest_extent = count;
1363                                 process[i].rw_largest_extent = count;
1364                                 process[i].rw_offset = 0;
1365                                 process[i].rw_last_file = file;
1366                                 spin_unlock(&sbi->ll_process_lock);
1367                                 return;
1368                         }
1369                         if (process[i].rw_last_file_pos != file->f_pos) {
1370                                 *off_count =
1371                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1372                                 offset[*off_count].rw_op = process[i].rw_op;
1373                                 offset[*off_count].rw_pid = pid;
1374                                 offset[*off_count].rw_range_start =
1375                                         process[i].rw_range_start;
1376                                 offset[*off_count].rw_range_end =
1377                                         process[i].rw_last_file_pos;
1378                                 offset[*off_count].rw_smallest_extent =
1379                                         process[i].rw_smallest_extent;
1380                                 offset[*off_count].rw_largest_extent =
1381                                         process[i].rw_largest_extent;
1382                                 offset[*off_count].rw_offset =
1383                                         process[i].rw_offset;
1384                                 process[i].rw_op = rw;
1385                                 process[i].rw_range_start = file->f_pos;
1386                                 process[i].rw_smallest_extent = count;
1387                                 process[i].rw_largest_extent = count;
1388                                 process[i].rw_offset = file->f_pos -
1389                                         process[i].rw_last_file_pos;
1390                         }
1391                         if(process[i].rw_smallest_extent > count)
1392                                 process[i].rw_smallest_extent = count;
1393                         if(process[i].rw_largest_extent < count)
1394                                 process[i].rw_largest_extent = count;
1395                         process[i].rw_last_file_pos = file->f_pos + count;
1396                         spin_unlock(&sbi->ll_process_lock);
1397                         return;
1398                 }
1399         }
1400         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1401         process[*process_count].rw_pid = pid;
1402         process[*process_count].rw_op = rw;
1403         process[*process_count].rw_range_start = file->f_pos;
1404         process[*process_count].rw_last_file_pos = file->f_pos + count;
1405         process[*process_count].rw_smallest_extent = count;
1406         process[*process_count].rw_largest_extent = count;
1407         process[*process_count].rw_offset = 0;
1408         process[*process_count].rw_last_file = file;
1409         spin_unlock(&sbi->ll_process_lock);
1410 }
1411
1412 char lpszt[] = LPSZ;
1413
1414 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1415 {
1416         struct timeval now;
1417         struct ll_sb_info *sbi = seq->private;
1418         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1419         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1420         char format[50];
1421         int i;
1422
1423         do_gettimeofday(&now);
1424
1425         if (!sbi->ll_rw_stats_on) {
1426                 seq_printf(seq, "disabled\n"
1427                                 "write anything in this file to activate, "
1428                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1429                 return 0;
1430         }
1431         spin_lock(&sbi->ll_process_lock);
1432
1433         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1434                    now.tv_sec, now.tv_usec);
1435         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1436                    "R/W", "PID", "RANGE START", "RANGE END",
1437                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1438         sprintf(format, "%s%s%s%s%s\n",
1439                 "%3c %10d %14Lu %14Lu %17", lpszt+1, " %17", lpszt+1, " %14Ld");
1440         /* We stored the discontiguous offsets here; print them first */
1441         for(i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1442                 if (offset[i].rw_pid != 0)
1443                         /* Is there a way to snip the '%' off of LPSZ? */
1444                         seq_printf(seq, format,
1445                                    offset[i].rw_op ? 'W' : 'R',
1446                                    offset[i].rw_pid,
1447                                    offset[i].rw_range_start,
1448                                    offset[i].rw_range_end,
1449                                    offset[i].rw_smallest_extent,
1450                                    offset[i].rw_largest_extent,
1451                                    offset[i].rw_offset);
1452         }
1453         /* Then print the current offsets for each process */
1454         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1455                 if (process[i].rw_pid != 0)
1456                         seq_printf(seq, format,
1457                                    process[i].rw_op ? 'W' : 'R',
1458                                    process[i].rw_pid,
1459                                    process[i].rw_range_start,
1460                                    process[i].rw_last_file_pos,
1461                                    process[i].rw_smallest_extent,
1462                                    process[i].rw_largest_extent,
1463                                    process[i].rw_offset);
1464         }
1465         spin_unlock(&sbi->ll_process_lock);
1466
1467         return 0;
1468 }
1469
1470 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1471                                        size_t len, loff_t *off)
1472 {
1473         struct seq_file *seq = file->private_data;
1474         struct ll_sb_info *sbi = seq->private;
1475         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1476         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1477         int value = 1, rc = 0;
1478
1479         rc = lprocfs_write_helper(buf, len, &value);
1480
1481         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1482                            strcmp(buf, "Disabled") == 0))
1483                 value = 0;
1484
1485         if (value == 0)
1486                 sbi->ll_rw_stats_on = 0;
1487         else
1488                 sbi->ll_rw_stats_on = 1;
1489
1490         spin_lock(&sbi->ll_process_lock);
1491         sbi->ll_offset_process_count = 0;
1492         sbi->ll_rw_offset_entry_count = 0;
1493         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1494                LL_PROCESS_HIST_MAX);
1495         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1496                LL_OFFSET_HIST_MAX);
1497         spin_unlock(&sbi->ll_process_lock);
1498
1499         return len;
1500 }
1501
1502 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1503
1504 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1505 {
1506     lvars->module_vars  = NULL;
1507     lvars->obd_vars     = lprocfs_llite_obd_vars;
1508 }
1509 #endif /* LPROCFS */