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