Whamcloud - gitweb
LU-3319 procfs: provide framework for seq_file handling
[fs/lustre-release.git] / lustre / llite / lproc_llite.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
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 extern struct file_operations vvp_dump_pgcache_file_ops;
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,
64                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
65                                 OBD_STATFS_NODELAY);
66         if (!rc) {
67               *eof = 1;
68               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
69         }
70
71         return rc;
72 }
73
74 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
75                              int *eof, void *data)
76 {
77         struct super_block *sb = (struct super_block *)data;
78         struct obd_statfs osfs;
79         int rc;
80
81         LASSERT(sb != NULL);
82         rc = ll_statfs_internal(sb, &osfs,
83                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
84                                 OBD_STATFS_NODELAY);
85         if (!rc) {
86                 __u32 blk_size = osfs.os_bsize >> 10;
87                 __u64 result = osfs.os_blocks;
88
89                 while (blk_size >>= 1)
90                         result <<= 1;
91
92                 *eof = 1;
93                 rc = snprintf(page, count, LPU64"\n", result);
94         }
95         return rc;
96
97 }
98
99 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
100                             int *eof, void *data)
101 {
102         struct super_block *sb = (struct super_block *)data;
103         struct obd_statfs osfs;
104         int rc;
105
106         LASSERT(sb != NULL);
107         rc = ll_statfs_internal(sb, &osfs,
108                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
109                                 OBD_STATFS_NODELAY);
110         if (!rc) {
111                 __u32 blk_size = osfs.os_bsize >> 10;
112                 __u64 result = osfs.os_bfree;
113
114                 while (blk_size >>= 1)
115                         result <<= 1;
116
117                 *eof = 1;
118                 rc = snprintf(page, count, LPU64"\n", result);
119         }
120         return rc;
121 }
122
123 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
124                              int *eof, void *data)
125 {
126         struct super_block *sb = (struct super_block *)data;
127         struct obd_statfs osfs;
128         int rc;
129
130         LASSERT(sb != NULL);
131         rc = ll_statfs_internal(sb, &osfs,
132                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
133                                 OBD_STATFS_NODELAY);
134         if (!rc) {
135                 __u32 blk_size = osfs.os_bsize >> 10;
136                 __u64 result = osfs.os_bavail;
137
138                 while (blk_size >>= 1)
139                         result <<= 1;
140
141                 *eof = 1;
142                 rc = snprintf(page, count, LPU64"\n", result);
143         }
144         return rc;
145 }
146
147 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
148                             int *eof, void *data)
149 {
150         struct super_block *sb = (struct super_block *)data;
151         struct obd_statfs osfs;
152         int rc;
153
154         LASSERT(sb != NULL);
155         rc = ll_statfs_internal(sb, &osfs,
156                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
157                                 OBD_STATFS_NODELAY);
158         if (!rc) {
159                  *eof = 1;
160                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
161         }
162         return rc;
163 }
164
165 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
166                            int *eof, void *data)
167 {
168         struct super_block *sb = (struct super_block *)data;
169         struct obd_statfs osfs;
170         int rc;
171
172         LASSERT(sb != NULL);
173         rc = ll_statfs_internal(sb, &osfs,
174                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
175                                 OBD_STATFS_NODELAY);
176         if (!rc) {
177                  *eof = 1;
178                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
179         }
180         return rc;
181
182 }
183
184 static int ll_rd_client_type(char *page, char **start, off_t off, int count,
185                             int *eof, void *data)
186 {
187         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)data);
188         int rc;
189
190         LASSERT(sbi != NULL);
191
192         *eof = 1;
193         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
194                 rc = snprintf(page, count, "remote client\n");
195         else
196                 rc = snprintf(page, count, "local client\n");
197
198         return rc;
199 }
200
201 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
202                         int *eof, void *data)
203 {
204         struct super_block *sb = (struct super_block*)data;
205
206         LASSERT(sb != NULL);
207         *eof = 1;
208         return snprintf(page, count, "%s\n", sb->s_type->name);
209 }
210
211 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
212                          int *eof, void *data)
213 {
214         struct super_block *sb = (struct super_block *)data;
215
216         LASSERT(sb != NULL);
217         *eof = 1;
218         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
219 }
220
221 static int ll_rd_xattr_cache(char *page, char **start, off_t off,
222                                 int count, int *eof, void *data)
223 {
224         struct super_block *sb = (struct super_block *)data;
225         struct ll_sb_info *sbi = ll_s2sbi(sb);
226         int rc;
227
228         rc = snprintf(page, count, "%u\n", sbi->ll_xattr_cache_enabled);
229
230         return rc;
231 }
232
233 static int ll_wr_xattr_cache(struct file *file, const char *buffer,
234                                 unsigned long count, void *data)
235 {
236         struct super_block *sb = (struct super_block *)data;
237         struct ll_sb_info *sbi = ll_s2sbi(sb);
238         int val, rc;
239
240         rc = lprocfs_write_helper(buffer, count, &val);
241         if (rc)
242                 return rc;
243
244         if (val != 0 && val != 1)
245                 return -ERANGE;
246
247         if (val == 1 && !(sbi->ll_flags & LL_SBI_XATTR_CACHE))
248                 return -ENOTSUPP;
249
250         sbi->ll_xattr_cache_enabled = val;
251
252         return count;
253 }
254
255 static int ll_rd_site_stats(char *page, char **start, off_t off,
256                             int count, int *eof, void *data)
257 {
258         struct super_block *sb = data;
259
260         /*
261          * See description of statistical counters in struct cl_site, and
262          * struct lu_site.
263          */
264         return cl_site_stats_print(lu2cl_site(ll_s2sbi(sb)->ll_site),
265                                    page, count);
266 }
267
268 static int ll_rd_max_readahead_mb(char *page, char **start, off_t off,
269                                    int count, int *eof, void *data)
270 {
271         struct super_block *sb = data;
272         struct ll_sb_info *sbi = ll_s2sbi(sb);
273         long pages_number;
274         int mult;
275
276         spin_lock(&sbi->ll_lock);
277         pages_number = sbi->ll_ra_info.ra_max_pages;
278         spin_unlock(&sbi->ll_lock);
279
280         mult = 1 << (20 - PAGE_CACHE_SHIFT);
281         return lprocfs_read_frac_helper(page, count, pages_number, mult);
282 }
283
284 static int ll_wr_max_readahead_mb(struct file *file, const char *buffer,
285                                   unsigned long count, void *data)
286 {
287         struct super_block *sb = data;
288         struct ll_sb_info *sbi = ll_s2sbi(sb);
289         int mult, rc, pages_number;
290
291         mult = 1 << (20 - PAGE_CACHE_SHIFT);
292         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
293         if (rc)
294                 return rc;
295
296         if (pages_number < 0 || pages_number > totalram_pages / 2) {
297                 /* 1/2 of RAM */
298                 CERROR("can't set file readahead more than %lu MB\n",
299                        totalram_pages >> (20 - PAGE_CACHE_SHIFT + 1));
300                 return -ERANGE;
301         }
302
303         spin_lock(&sbi->ll_lock);
304         sbi->ll_ra_info.ra_max_pages = pages_number;
305         spin_unlock(&sbi->ll_lock);
306
307         return count;
308 }
309
310 static int ll_rd_max_readahead_per_file_mb(char *page, char **start, off_t off,
311                                            int count, int *eof, void *data)
312 {
313         struct super_block *sb = data;
314         struct ll_sb_info *sbi = ll_s2sbi(sb);
315         long pages_number;
316         int mult;
317
318         spin_lock(&sbi->ll_lock);
319         pages_number = sbi->ll_ra_info.ra_max_pages_per_file;
320         spin_unlock(&sbi->ll_lock);
321
322         mult = 1 << (20 - PAGE_CACHE_SHIFT);
323         return lprocfs_read_frac_helper(page, count, pages_number, mult);
324 }
325
326 static int ll_wr_max_readahead_per_file_mb(struct file *file, const char *buffer,
327                                           unsigned long count, void *data)
328 {
329         struct super_block *sb = data;
330         struct ll_sb_info *sbi = ll_s2sbi(sb);
331         int mult, rc, pages_number;
332
333         mult = 1 << (20 - PAGE_CACHE_SHIFT);
334         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
335         if (rc)
336                 return rc;
337
338         if (pages_number < 0 ||
339                 pages_number > sbi->ll_ra_info.ra_max_pages) {
340                 CERROR("can't set file readahead more than"
341                        "max_read_ahead_mb %lu MB\n",
342                        sbi->ll_ra_info.ra_max_pages);
343                 return -ERANGE;
344         }
345
346         spin_lock(&sbi->ll_lock);
347         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
348         spin_unlock(&sbi->ll_lock);
349
350         return count;
351 }
352
353 static int ll_rd_max_read_ahead_whole_mb(char *page, char **start, off_t off,
354                                          int count, int *eof, void *data)
355 {
356         struct super_block *sb = data;
357         struct ll_sb_info *sbi = ll_s2sbi(sb);
358         long pages_number;
359         int mult;
360
361         spin_lock(&sbi->ll_lock);
362         pages_number = sbi->ll_ra_info.ra_max_read_ahead_whole_pages;
363         spin_unlock(&sbi->ll_lock);
364
365         mult = 1 << (20 - PAGE_CACHE_SHIFT);
366         return lprocfs_read_frac_helper(page, count, pages_number, mult);
367 }
368
369 static int ll_wr_max_read_ahead_whole_mb(struct file *file, const char *buffer,
370                                          unsigned long count, void *data)
371 {
372         struct super_block *sb = data;
373         struct ll_sb_info *sbi = ll_s2sbi(sb);
374         int mult, rc, pages_number;
375
376         mult = 1 << (20 - PAGE_CACHE_SHIFT);
377         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
378         if (rc)
379                 return rc;
380
381         /* Cap this at the current max readahead window size, the readahead
382          * algorithm does this anyway so it's pointless to set it larger. */
383         if (pages_number < 0 ||
384             pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
385                 CERROR("can't set max_read_ahead_whole_mb more than "
386                        "max_read_ahead_per_file_mb: %lu\n",
387                         sbi->ll_ra_info.ra_max_pages_per_file >>
388                         (20 - PAGE_CACHE_SHIFT));
389                 return -ERANGE;
390         }
391
392         spin_lock(&sbi->ll_lock);
393         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
394         spin_unlock(&sbi->ll_lock);
395
396         return count;
397 }
398
399 static int ll_rd_max_cached_mb(char *page, char **start, off_t off,
400                                int count, int *eof, void *data)
401 {
402         struct super_block     *sb    = data;
403         struct ll_sb_info      *sbi   = ll_s2sbi(sb);
404         struct cl_client_cache *cache = &sbi->ll_cache;
405         int shift = 20 - PAGE_CACHE_SHIFT;
406         int max_cached_mb;
407         int unused_mb;
408
409         *eof = 1;
410         max_cached_mb = cache->ccc_lru_max >> shift;
411         unused_mb = cfs_atomic_read(&cache->ccc_lru_left) >> shift;
412         return snprintf(page, count,
413                         "users: %d\n"
414                         "max_cached_mb: %d\n"
415                         "used_mb: %d\n"
416                         "unused_mb: %d\n"
417                         "reclaim_count: %u\n",
418                         cfs_atomic_read(&cache->ccc_users),
419                         max_cached_mb,
420                         max_cached_mb - unused_mb,
421                         unused_mb,
422                         cache->ccc_lru_shrinkers);
423 }
424
425 static int ll_wr_max_cached_mb(struct file *file, const char *buffer,
426                                 unsigned long nob, void *data)
427 {
428         struct super_block *sb = data;
429         struct ll_sb_info *sbi = ll_s2sbi(sb);
430         struct cl_client_cache *cache = &sbi->ll_cache;
431         struct lu_env *env;
432         int refcheck;
433         int mult, rc, pages_number;
434         size_t count = nob;
435         int diff = 0;
436         int nrpages = 0;
437         ENTRY;
438
439         mult = 1 << (20 - PAGE_CACHE_SHIFT);
440         buffer = lprocfs_find_named_value(buffer, "max_cached_mb:", &count);
441         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
442         if (rc)
443                 RETURN(rc);
444
445         if (pages_number < 0 || pages_number > totalram_pages) {
446                 CERROR("%s: can't set max cache more than %lu MB\n",
447                        ll_get_fsname(sb, NULL, 0),
448                        totalram_pages >> (20 - PAGE_CACHE_SHIFT));
449                 RETURN(-ERANGE);
450         }
451
452         if (sbi->ll_dt_exp == NULL) /* being initialized */
453                 GOTO(out, rc = 0);
454
455         spin_lock(&sbi->ll_lock);
456         diff = pages_number - cache->ccc_lru_max;
457         spin_unlock(&sbi->ll_lock);
458
459         /* easy - add more LRU slots. */
460         if (diff >= 0) {
461                 cfs_atomic_add(diff, &cache->ccc_lru_left);
462                 GOTO(out, rc = 0);
463         }
464
465         env = cl_env_get(&refcheck);
466         if (IS_ERR(env))
467                 RETURN(rc);
468
469         diff = -diff;
470         while (diff > 0) {
471                 int tmp;
472
473                 /* reduce LRU budget from free slots. */
474                 do {
475                         int ov, nv;
476
477                         ov = cfs_atomic_read(&cache->ccc_lru_left);
478                         if (ov == 0)
479                                 break;
480
481                         nv = ov > diff ? ov - diff : 0;
482                         rc = cfs_atomic_cmpxchg(&cache->ccc_lru_left, ov, nv);
483                         if (likely(ov == rc)) {
484                                 diff -= ov - nv;
485                                 nrpages += ov - nv;
486                                 break;
487                         }
488                 } while (1);
489
490                 if (diff <= 0)
491                         break;
492
493                 /* difficult - have to ask OSCs to drop LRU slots. */
494                 tmp = diff << 1;
495                 rc = obd_set_info_async(env, sbi->ll_dt_exp,
496                                 sizeof(KEY_CACHE_LRU_SHRINK),
497                                 KEY_CACHE_LRU_SHRINK,
498                                 sizeof(tmp), &tmp, NULL);
499                 if (rc < 0)
500                         break;
501         }
502         cl_env_put(env, &refcheck);
503
504 out:
505         if (rc >= 0) {
506                 spin_lock(&sbi->ll_lock);
507                 cache->ccc_lru_max = pages_number;
508                 spin_unlock(&sbi->ll_lock);
509                 rc = count;
510         } else {
511                 cfs_atomic_add(nrpages, &cache->ccc_lru_left);
512         }
513         return rc;
514 }
515
516 static int ll_rd_checksum(char *page, char **start, off_t off,
517                           int count, int *eof, void *data)
518 {
519         struct super_block *sb = data;
520         struct ll_sb_info *sbi = ll_s2sbi(sb);
521
522         return snprintf(page, count, "%u\n",
523                         (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
524 }
525
526 static int ll_wr_checksum(struct file *file, const char *buffer,
527                           unsigned long count, void *data)
528 {
529         struct super_block *sb = data;
530         struct ll_sb_info *sbi = ll_s2sbi(sb);
531         int val, rc;
532
533         if (!sbi->ll_dt_exp)
534                 /* Not set up yet */
535                 return -EAGAIN;
536
537         rc = lprocfs_write_helper(buffer, count, &val);
538         if (rc)
539                 return rc;
540         if (val)
541                 sbi->ll_flags |= LL_SBI_CHECKSUM;
542         else
543                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
544
545         rc = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
546                                 KEY_CHECKSUM, sizeof(val), &val, NULL);
547         if (rc)
548                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
549
550         return count;
551 }
552
553 static int ll_rd_max_rw_chunk(char *page, char **start, off_t off,
554                           int count, int *eof, void *data)
555 {
556         struct super_block *sb = data;
557
558         return snprintf(page, count, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
559 }
560
561 static int ll_wr_max_rw_chunk(struct file *file, const char *buffer,
562                           unsigned long count, void *data)
563 {
564         struct super_block *sb = data;
565         int rc, val;
566
567         rc = lprocfs_write_helper(buffer, count, &val);
568         if (rc)
569                 return rc;
570         ll_s2sbi(sb)->ll_max_rw_chunk = val;
571         return count;
572 }
573
574 static int ll_rd_track_id(char *page, int count, void *data,
575                           enum stats_track_type type)
576 {
577         struct super_block *sb = data;
578
579         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
580                 return snprintf(page, count, "%d\n",
581                                 ll_s2sbi(sb)->ll_stats_track_id);
582
583         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
584                 return snprintf(page, count, "0 (all)\n");
585         } else {
586                 return snprintf(page, count, "untracked\n");
587         }
588 }
589
590 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
591                           enum stats_track_type type)
592 {
593         struct super_block *sb = data;
594         int rc, pid;
595
596         rc = lprocfs_write_helper(buffer, count, &pid);
597         if (rc)
598                 return rc;
599         ll_s2sbi(sb)->ll_stats_track_id = pid;
600         if (pid == 0)
601                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
602         else
603                 ll_s2sbi(sb)->ll_stats_track_type = type;
604         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
605         return count;
606 }
607
608 static int ll_rd_track_pid(char *page, char **start, off_t off,
609                           int count, int *eof, void *data)
610 {
611         return (ll_rd_track_id(page, count, data, STATS_TRACK_PID));
612 }
613
614 static int ll_wr_track_pid(struct file *file, const char *buffer,
615                           unsigned long count, void *data)
616 {
617         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PID));
618 }
619
620 static int ll_rd_track_ppid(char *page, char **start, off_t off,
621                           int count, int *eof, void *data)
622 {
623         return (ll_rd_track_id(page, count, data, STATS_TRACK_PPID));
624 }
625
626 static int ll_wr_track_ppid(struct file *file, const char *buffer,
627                           unsigned long count, void *data)
628 {
629         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PPID));
630 }
631
632 static int ll_rd_track_gid(char *page, char **start, off_t off,
633                           int count, int *eof, void *data)
634 {
635         return (ll_rd_track_id(page, count, data, STATS_TRACK_GID));
636 }
637
638 static int ll_wr_track_gid(struct file *file, const char *buffer,
639                           unsigned long count, void *data)
640 {
641         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_GID));
642 }
643
644 static int ll_rd_statahead_max(char *page, char **start, off_t off,
645                                int count, int *eof, void *data)
646 {
647         struct super_block *sb = data;
648         struct ll_sb_info *sbi = ll_s2sbi(sb);
649
650         return snprintf(page, count, "%u\n", sbi->ll_sa_max);
651 }
652
653 static int ll_wr_statahead_max(struct file *file, const char *buffer,
654                                unsigned long count, void *data)
655 {
656         struct super_block *sb = data;
657         struct ll_sb_info *sbi = ll_s2sbi(sb);
658         int val, rc;
659
660         rc = lprocfs_write_helper(buffer, count, &val);
661         if (rc)
662                 return rc;
663
664         if (val >= 0 && val <= LL_SA_RPC_MAX)
665                 sbi->ll_sa_max = val;
666         else
667                 CERROR("Bad statahead_max value %d. Valid values are in the "
668                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
669
670         return count;
671 }
672
673 static int ll_rd_statahead_agl(char *page, char **start, off_t off,
674                                int count, int *eof, void *data)
675 {
676         struct super_block *sb = data;
677         struct ll_sb_info *sbi = ll_s2sbi(sb);
678
679         return snprintf(page, count, "%u\n",
680                         sbi->ll_flags & LL_SBI_AGL_ENABLED ? 1 : 0);
681 }
682
683 static int ll_wr_statahead_agl(struct file *file, const char *buffer,
684                                unsigned long count, void *data)
685 {
686         struct super_block *sb = data;
687         struct ll_sb_info *sbi = ll_s2sbi(sb);
688         int val, rc;
689
690         rc = lprocfs_write_helper(buffer, count, &val);
691         if (rc)
692                 return rc;
693
694         if (val)
695                 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
696         else
697                 sbi->ll_flags &= ~LL_SBI_AGL_ENABLED;
698
699         return count;
700 }
701
702 static int ll_rd_statahead_stats(char *page, char **start, off_t off,
703                                  int count, int *eof, void *data)
704 {
705         struct super_block *sb = data;
706         struct ll_sb_info *sbi = ll_s2sbi(sb);
707
708         return snprintf(page, count,
709                         "statahead total: %u\n"
710                         "statahead wrong: %u\n"
711                         "agl total: %u\n",
712                         atomic_read(&sbi->ll_sa_total),
713                         atomic_read(&sbi->ll_sa_wrong),
714                         atomic_read(&sbi->ll_agl_total));
715 }
716
717 static int ll_rd_lazystatfs(char *page, char **start, off_t off,
718                             int count, int *eof, void *data)
719 {
720         struct super_block *sb = data;
721         struct ll_sb_info *sbi = ll_s2sbi(sb);
722
723         return snprintf(page, count, "%u\n",
724                         (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
725 }
726
727 static int ll_wr_lazystatfs(struct file *file, const char *buffer,
728                             unsigned long count, void *data)
729 {
730         struct super_block *sb = data;
731         struct ll_sb_info *sbi = ll_s2sbi(sb);
732         int val, rc;
733
734         rc = lprocfs_write_helper(buffer, count, &val);
735         if (rc)
736                 return rc;
737
738         if (val)
739                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
740         else
741                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
742
743         return count;
744 }
745
746 static int ll_rd_maxea_size(char *page, char **start, off_t off,
747                             int count, int *eof, void *data)
748 {
749         struct super_block *sb = data;
750         struct ll_sb_info *sbi = ll_s2sbi(sb);
751         unsigned int ealen;
752         int rc;
753
754         rc = ll_get_max_mdsize(sbi, &ealen);
755         if (rc)
756                 return rc;
757
758         return snprintf(page, count, "%u\n", ealen);
759 }
760
761 static int ll_rd_sbi_flags(char *page, char **start, off_t off,
762                                 int count, int *eof, void *data)
763 {
764         const char *str[] = LL_SBI_FLAGS;
765         struct super_block *sb = data;
766         int flags = ll_s2sbi(sb)->ll_flags;
767         int i = 0;
768         int rc = 0;
769
770         while (flags != 0) {
771                 if (ARRAY_SIZE(str) <= i) {
772                         CERROR("%s: Revise array LL_SBI_FLAGS to match sbi "
773                                 "flags please.\n", ll_get_fsname(sb, NULL, 0));
774                         return -EINVAL;
775                 }
776
777                 if (flags & 0x1)
778                         rc += snprintf(page + rc, count - rc, "%s ", str[i]);
779                 flags >>= 1;
780                 ++i;
781         }
782         if (rc > 0)
783                 rc += snprintf(page + rc, count - rc, "\b\n");
784         return rc;
785 }
786
787 static int ll_rd_unstable_stats(char *page, char **start, off_t off,
788                               int count, int *eof, void *data)
789 {
790         struct super_block      *sb    = data;
791         struct ll_sb_info       *sbi   = ll_s2sbi(sb);
792         struct cl_client_cache  *cache = &sbi->ll_cache;
793         int pages, mb, rc;
794
795         pages = cfs_atomic_read(&cache->ccc_unstable_nr);
796         mb    = (pages * PAGE_CACHE_SIZE) >> 20;
797
798         rc = snprintf(page, count, "unstable_pages: %8d\n"
799                                    "unstable_mb:    %8d\n", pages, mb);
800
801         return rc;
802 }
803
804 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
805         { "uuid",         ll_rd_sb_uuid,          0, 0 },
806         //{ "mntpt_path",   ll_rd_path,             0, 0 },
807         { "fstype",       ll_rd_fstype,           0, 0 },
808         { "site",         ll_rd_site_stats,       0, 0 },
809         { "blocksize",    ll_rd_blksize,          0, 0 },
810         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
811         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
812         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
813         { "filestotal",   ll_rd_filestotal,       0, 0 },
814         { "filesfree",    ll_rd_filesfree,        0, 0 },
815         { "client_type",  ll_rd_client_type,      0, 0 },
816         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
817         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
818                                ll_wr_max_readahead_mb, 0 },
819         { "max_read_ahead_per_file_mb", ll_rd_max_readahead_per_file_mb,
820                                         ll_wr_max_readahead_per_file_mb, 0 },
821         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
822                                      ll_wr_max_read_ahead_whole_mb, 0 },
823         { "max_cached_mb",    ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
824         { "checksum_pages",   ll_rd_checksum, ll_wr_checksum, 0 },
825         { "max_rw_chunk",     ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
826         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
827         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
828         { "stats_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
829         { "statahead_max",    ll_rd_statahead_max, ll_wr_statahead_max, 0 },
830         { "statahead_agl",    ll_rd_statahead_agl, ll_wr_statahead_agl, 0 },
831         { "statahead_stats",  ll_rd_statahead_stats, 0, 0 },
832         { "lazystatfs",       ll_rd_lazystatfs, ll_wr_lazystatfs, 0 },
833         { "max_easize",       ll_rd_maxea_size, 0, 0 },
834         { "sbi_flags",        ll_rd_sbi_flags, 0, 0 },
835         { "xattr_cache",      ll_rd_xattr_cache, ll_wr_xattr_cache, 0 },
836         { "unstable_stats",   ll_rd_unstable_stats, 0, 0},
837         { 0 }
838 };
839
840 #define MAX_STRING_SIZE 128
841
842 struct llite_file_opcode {
843         __u32       opcode;
844         __u32       type;
845         const char *opname;
846 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
847         /* file operation */
848         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
849         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
850         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
851                                    "read_bytes" },
852         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
853                                    "write_bytes" },
854         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
855                                    "brw_read" },
856         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
857                                    "brw_write" },
858         { LPROC_LL_OSC_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
859                                    "osc_read" },
860         { LPROC_LL_OSC_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
861                                    "osc_write" },
862         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
863         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
864         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
865         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
866         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
867         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
868         { LPROC_LL_READDIR,        LPROCFS_TYPE_REGS, "readdir" },
869         /* inode operation */
870         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
871         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
872         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
873         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
874         /* dir inode operation */
875         { LPROC_LL_CREATE,         LPROCFS_TYPE_REGS, "create" },
876         { LPROC_LL_LINK,           LPROCFS_TYPE_REGS, "link" },
877         { LPROC_LL_UNLINK,         LPROCFS_TYPE_REGS, "unlink" },
878         { LPROC_LL_SYMLINK,        LPROCFS_TYPE_REGS, "symlink" },
879         { LPROC_LL_MKDIR,          LPROCFS_TYPE_REGS, "mkdir" },
880         { LPROC_LL_RMDIR,          LPROCFS_TYPE_REGS, "rmdir" },
881         { LPROC_LL_MKNOD,          LPROCFS_TYPE_REGS, "mknod" },
882         { LPROC_LL_RENAME,         LPROCFS_TYPE_REGS, "rename" },
883         /* special inode operation */
884         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
885         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
886         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
887         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
888         { LPROC_LL_GETXATTR_HITS,  LPROCFS_TYPE_REGS, "getxattr_hits" },
889         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
890         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
891         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
892 };
893
894 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
895 {
896         if (!sbi->ll_stats)
897                 return;
898         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
899                 lprocfs_counter_add(sbi->ll_stats, op, count);
900         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
901                  sbi->ll_stats_track_id == current->pid)
902                 lprocfs_counter_add(sbi->ll_stats, op, count);
903         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
904                  sbi->ll_stats_track_id == current->parent->pid)
905                 lprocfs_counter_add(sbi->ll_stats, op, count);
906         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
907                  sbi->ll_stats_track_id == current_gid())
908                 lprocfs_counter_add(sbi->ll_stats, op, count);
909 }
910 EXPORT_SYMBOL(ll_stats_ops_tally);
911
912 static const char *ra_stat_string[] = {
913         [RA_STAT_HIT] = "hits",
914         [RA_STAT_MISS] = "misses",
915         [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
916         [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
917         [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
918         [RA_STAT_FAILED_MATCH] = "failed lock match",
919         [RA_STAT_DISCARDED] = "read but discarded",
920         [RA_STAT_ZERO_LEN] = "zero length file",
921         [RA_STAT_ZERO_WINDOW] = "zero size window",
922         [RA_STAT_EOF] = "read-ahead to EOF",
923         [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
924         [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
925 };
926
927
928 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
929                                 struct super_block *sb, char *osc, char *mdc)
930 {
931         struct lprocfs_vars lvars[2];
932         struct lustre_sb_info *lsi = s2lsi(sb);
933         struct ll_sb_info *sbi = ll_s2sbi(sb);
934         struct obd_device *obd;
935         char name[MAX_STRING_SIZE + 1], *ptr;
936         int err, id, len, rc;
937         ENTRY;
938
939         memset(lvars, 0, sizeof(lvars));
940
941         name[MAX_STRING_SIZE] = '\0';
942         lvars[0].name = name;
943
944         LASSERT(sbi != NULL);
945         LASSERT(mdc != NULL);
946         LASSERT(osc != NULL);
947
948         /* Get fsname */
949         len = strlen(lsi->lsi_lmd->lmd_profile);
950         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
951         if (ptr && (strcmp(ptr, "-client") == 0))
952                 len -= 7;
953
954         /* Mount info */
955         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
956                  lsi->lsi_lmd->lmd_profile, sb);
957
958         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
959         if (IS_ERR(sbi->ll_proc_root)) {
960                 err = PTR_ERR(sbi->ll_proc_root);
961                 sbi->ll_proc_root = NULL;
962                 RETURN(err);
963         }
964
965         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
966                                 &vvp_dump_pgcache_file_ops, sbi);
967         if (rc)
968                 CWARN("Error adding the dump_page_cache file\n");
969
970         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
971                                 &ll_rw_extents_stats_fops, sbi);
972         if (rc)
973                 CWARN("Error adding the extent_stats file\n");
974
975         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
976                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
977         if (rc)
978                 CWARN("Error adding the extents_stats_per_process file\n");
979
980         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
981                                 &ll_rw_offset_stats_fops, sbi);
982         if (rc)
983                 CWARN("Error adding the offset_stats file\n");
984
985         /* File operations stats */
986         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
987                                             LPROCFS_STATS_FLAG_NONE);
988         if (sbi->ll_stats == NULL)
989                 GOTO(out, err = -ENOMEM);
990         /* do counter init */
991         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
992                 __u32 type = llite_opcode_table[id].type;
993                 void *ptr = NULL;
994                 if (type & LPROCFS_TYPE_REGS)
995                         ptr = "regs";
996                 else if (type & LPROCFS_TYPE_BYTES)
997                         ptr = "bytes";
998                 else if (type & LPROCFS_TYPE_PAGES)
999                         ptr = "pages";
1000                 lprocfs_counter_init(sbi->ll_stats,
1001                                      llite_opcode_table[id].opcode,
1002                                      (type & LPROCFS_CNTR_AVGMINMAX),
1003                                      llite_opcode_table[id].opname, ptr);
1004         }
1005         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
1006         if (err)
1007                 GOTO(out, err);
1008
1009         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
1010                                                LPROCFS_STATS_FLAG_NONE);
1011         if (sbi->ll_ra_stats == NULL)
1012                 GOTO(out, err = -ENOMEM);
1013
1014         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
1015                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
1016                                      ra_stat_string[id], "pages");
1017         err = lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
1018                                      sbi->ll_ra_stats);
1019         if (err)
1020                 GOTO(out, err);
1021
1022
1023         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
1024         if (err)
1025                 GOTO(out, err);
1026
1027         /* MDC info */
1028         obd = class_name2obd(mdc);
1029
1030         LASSERT(obd != NULL);
1031         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1032         LASSERT(obd->obd_type->typ_name != NULL);
1033
1034         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
1035                  obd->obd_type->typ_name);
1036         lvars[0].read_fptr = lprocfs_rd_name;
1037         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1038         if (err)
1039                 GOTO(out, err);
1040
1041         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
1042         lvars[0].read_fptr = lprocfs_rd_uuid;
1043         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1044         if (err)
1045                 GOTO(out, err);
1046
1047         /* OSC */
1048         obd = class_name2obd(osc);
1049
1050         LASSERT(obd != NULL);
1051         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1052         LASSERT(obd->obd_type->typ_name != NULL);
1053
1054         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
1055                  obd->obd_type->typ_name);
1056         lvars[0].read_fptr = lprocfs_rd_name;
1057         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1058         if (err)
1059                 GOTO(out, err);
1060
1061         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
1062         lvars[0].read_fptr = lprocfs_rd_uuid;
1063         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1064 out:
1065         if (err) {
1066                 lprocfs_remove(&sbi->ll_proc_root);
1067                 lprocfs_free_stats(&sbi->ll_ra_stats);
1068                 lprocfs_free_stats(&sbi->ll_stats);
1069         }
1070         RETURN(err);
1071 }
1072
1073 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
1074 {
1075         if (sbi->ll_proc_root) {
1076                 lprocfs_remove(&sbi->ll_proc_root);
1077                 lprocfs_free_stats(&sbi->ll_ra_stats);
1078                 lprocfs_free_stats(&sbi->ll_stats);
1079         }
1080 }
1081 #undef MAX_STRING_SIZE
1082
1083 #define pct(a,b) (b ? a * 100 / b : 0)
1084
1085 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1086                                    struct seq_file *seq, int which)
1087 {
1088         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1089         unsigned long start, end, r, w;
1090         char *unitp = "KMGTPEZY";
1091         int i, units = 10;
1092         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1093
1094         read_cum = 0;
1095         write_cum = 0;
1096         start = 0;
1097
1098         for(i = 0; i < LL_HIST_MAX; i++) {
1099                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1100                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1101         }
1102
1103         for(i = 0; i < LL_HIST_MAX; i++) {
1104                 r = pp_info->pp_r_hist.oh_buckets[i];
1105                 w = pp_info->pp_w_hist.oh_buckets[i];
1106                 read_cum += r;
1107                 write_cum += w;
1108                 end = 1 << (i + LL_HIST_START - units);
1109                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1110                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1111                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1112                            r, pct(r, read_tot), pct(read_cum, read_tot),
1113                            w, pct(w, write_tot), pct(write_cum, write_tot));
1114                 start = end;
1115                 if (start == 1<<10) {
1116                         start = 1;
1117                         units += 10;
1118                         unitp++;
1119                 }
1120                 if (read_cum == read_tot && write_cum == write_tot)
1121                         break;
1122         }
1123 }
1124
1125 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1126 {
1127         struct timeval now;
1128         struct ll_sb_info *sbi = seq->private;
1129         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1130         int k;
1131
1132         do_gettimeofday(&now);
1133
1134         if (!sbi->ll_rw_stats_on) {
1135                 seq_printf(seq, "disabled\n"
1136                                 "write anything in this file to activate, "
1137                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1138                 return 0;
1139         }
1140         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1141                    now.tv_sec, now.tv_usec);
1142         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1143         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1144                    "extents", "calls", "%", "cum%",
1145                    "calls", "%", "cum%");
1146         spin_lock(&sbi->ll_pp_extent_lock);
1147         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1148                 if (io_extents->pp_extents[k].pid != 0) {
1149                         seq_printf(seq, "\nPID: %d\n",
1150                                    io_extents->pp_extents[k].pid);
1151                         ll_display_extents_info(io_extents, seq, k);
1152                 }
1153         }
1154         spin_unlock(&sbi->ll_pp_extent_lock);
1155         return 0;
1156 }
1157
1158 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1159                                                 const char *buf, size_t len,
1160                                                 loff_t *off)
1161 {
1162         struct seq_file *seq = file->private_data;
1163         struct ll_sb_info *sbi = seq->private;
1164         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1165         int i;
1166         int value = 1, rc = 0;
1167
1168         rc = lprocfs_write_helper(buf, len, &value);
1169         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1170                        strcmp(buf, "Disabled") == 0))
1171                 value = 0;
1172
1173         if (value == 0)
1174                 sbi->ll_rw_stats_on = 0;
1175         else
1176                 sbi->ll_rw_stats_on = 1;
1177
1178         spin_lock(&sbi->ll_pp_extent_lock);
1179         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1180                 io_extents->pp_extents[i].pid = 0;
1181                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1182                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1183         }
1184         spin_unlock(&sbi->ll_pp_extent_lock);
1185         return len;
1186 }
1187
1188 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1189
1190 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1191 {
1192         struct timeval now;
1193         struct ll_sb_info *sbi = seq->private;
1194         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1195
1196         do_gettimeofday(&now);
1197
1198         if (!sbi->ll_rw_stats_on) {
1199                 seq_printf(seq, "disabled\n"
1200                                 "write anything in this file to activate, "
1201                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1202                 return 0;
1203         }
1204         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1205                    now.tv_sec, now.tv_usec);
1206
1207         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1208         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1209                    "extents", "calls", "%", "cum%",
1210                    "calls", "%", "cum%");
1211         spin_lock(&sbi->ll_lock);
1212         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1213         spin_unlock(&sbi->ll_lock);
1214
1215         return 0;
1216 }
1217
1218 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1219                                         size_t len, 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         spin_lock(&sbi->ll_pp_extent_lock);
1237         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
1238                 io_extents->pp_extents[i].pid = 0;
1239                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1240                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1241         }
1242         spin_unlock(&sbi->ll_pp_extent_lock);
1243
1244         return len;
1245 }
1246
1247 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1248
1249 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
1250                        struct ll_file_data *file, loff_t pos,
1251                        size_t count, int rw)
1252 {
1253         int i, cur = -1;
1254         struct ll_rw_process_info *process;
1255         struct ll_rw_process_info *offset;
1256         int *off_count = &sbi->ll_rw_offset_entry_count;
1257         int *process_count = &sbi->ll_offset_process_count;
1258         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1259
1260         if(!sbi->ll_rw_stats_on)
1261                 return;
1262         process = sbi->ll_rw_process_info;
1263         offset = sbi->ll_rw_offset_info;
1264
1265         spin_lock(&sbi->ll_pp_extent_lock);
1266         /* Extent statistics */
1267         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1268                 if(io_extents->pp_extents[i].pid == pid) {
1269                         cur = i;
1270                         break;
1271                 }
1272         }
1273
1274         if (cur == -1) {
1275                 /* new process */
1276                 sbi->ll_extent_process_count =
1277                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1278                 cur = sbi->ll_extent_process_count;
1279                 io_extents->pp_extents[cur].pid = pid;
1280                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1281                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1282         }
1283
1284         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1285              (i < (LL_HIST_MAX - 1)); i++);
1286         if (rw == 0) {
1287                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1288                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1289         } else {
1290                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1291                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1292         }
1293         spin_unlock(&sbi->ll_pp_extent_lock);
1294
1295         spin_lock(&sbi->ll_process_lock);
1296         /* Offset statistics */
1297         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1298                 if (process[i].rw_pid == pid) {
1299                         if (process[i].rw_last_file != file) {
1300                                 process[i].rw_range_start = pos;
1301                                 process[i].rw_last_file_pos = pos + count;
1302                                 process[i].rw_smallest_extent = count;
1303                                 process[i].rw_largest_extent = count;
1304                                 process[i].rw_offset = 0;
1305                                 process[i].rw_last_file = file;
1306                                 spin_unlock(&sbi->ll_process_lock);
1307                                 return;
1308                         }
1309                         if (process[i].rw_last_file_pos != pos) {
1310                                 *off_count =
1311                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1312                                 offset[*off_count].rw_op = process[i].rw_op;
1313                                 offset[*off_count].rw_pid = pid;
1314                                 offset[*off_count].rw_range_start =
1315                                         process[i].rw_range_start;
1316                                 offset[*off_count].rw_range_end =
1317                                         process[i].rw_last_file_pos;
1318                                 offset[*off_count].rw_smallest_extent =
1319                                         process[i].rw_smallest_extent;
1320                                 offset[*off_count].rw_largest_extent =
1321                                         process[i].rw_largest_extent;
1322                                 offset[*off_count].rw_offset =
1323                                         process[i].rw_offset;
1324                                 process[i].rw_op = rw;
1325                                 process[i].rw_range_start = pos;
1326                                 process[i].rw_smallest_extent = count;
1327                                 process[i].rw_largest_extent = count;
1328                                 process[i].rw_offset = pos -
1329                                         process[i].rw_last_file_pos;
1330                         }
1331                         if(process[i].rw_smallest_extent > count)
1332                                 process[i].rw_smallest_extent = count;
1333                         if(process[i].rw_largest_extent < count)
1334                                 process[i].rw_largest_extent = count;
1335                         process[i].rw_last_file_pos = pos + count;
1336                         spin_unlock(&sbi->ll_process_lock);
1337                         return;
1338                 }
1339         }
1340         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1341         process[*process_count].rw_pid = pid;
1342         process[*process_count].rw_op = rw;
1343         process[*process_count].rw_range_start = pos;
1344         process[*process_count].rw_last_file_pos = pos + count;
1345         process[*process_count].rw_smallest_extent = count;
1346         process[*process_count].rw_largest_extent = count;
1347         process[*process_count].rw_offset = 0;
1348         process[*process_count].rw_last_file = file;
1349         spin_unlock(&sbi->ll_process_lock);
1350 }
1351
1352 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1353 {
1354         struct timeval now;
1355         struct ll_sb_info *sbi = seq->private;
1356         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1357         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1358         int i;
1359
1360         do_gettimeofday(&now);
1361
1362         if (!sbi->ll_rw_stats_on) {
1363                 seq_printf(seq, "disabled\n"
1364                                 "write anything in this file to activate, "
1365                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1366                 return 0;
1367         }
1368         spin_lock(&sbi->ll_process_lock);
1369
1370         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1371                    now.tv_sec, now.tv_usec);
1372         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1373                    "R/W", "PID", "RANGE START", "RANGE END",
1374                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1375
1376         /* We stored the discontiguous offsets here; print them first */
1377         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1378                 if (offset[i].rw_pid != 0)
1379                         seq_printf(seq,
1380                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1381                                    offset[i].rw_op == READ ? 'R' : 'W',
1382                                    offset[i].rw_pid,
1383                                    offset[i].rw_range_start,
1384                                    offset[i].rw_range_end,
1385                                    (unsigned long)offset[i].rw_smallest_extent,
1386                                    (unsigned long)offset[i].rw_largest_extent,
1387                                    offset[i].rw_offset);
1388         }
1389
1390         /* Then print the current offsets for each process */
1391         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1392                 if (process[i].rw_pid != 0)
1393                         seq_printf(seq,
1394                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1395                                    process[i].rw_op == READ ? 'R' : 'W',
1396                                    process[i].rw_pid,
1397                                    process[i].rw_range_start,
1398                                    process[i].rw_last_file_pos,
1399                                    (unsigned long)process[i].rw_smallest_extent,
1400                                    (unsigned long)process[i].rw_largest_extent,
1401                                    process[i].rw_offset);
1402         }
1403         spin_unlock(&sbi->ll_process_lock);
1404
1405         return 0;
1406 }
1407
1408 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1409                                        size_t len, loff_t *off)
1410 {
1411         struct seq_file *seq = file->private_data;
1412         struct ll_sb_info *sbi = seq->private;
1413         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1414         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1415         int value = 1, rc = 0;
1416
1417         rc = lprocfs_write_helper(buf, len, &value);
1418
1419         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1420                            strcmp(buf, "Disabled") == 0))
1421                 value = 0;
1422
1423         if (value == 0)
1424                 sbi->ll_rw_stats_on = 0;
1425         else
1426                 sbi->ll_rw_stats_on = 1;
1427
1428         spin_lock(&sbi->ll_process_lock);
1429         sbi->ll_offset_process_count = 0;
1430         sbi->ll_rw_offset_entry_count = 0;
1431         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1432                LL_PROCESS_HIST_MAX);
1433         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1434                LL_OFFSET_HIST_MAX);
1435         spin_unlock(&sbi->ll_process_lock);
1436
1437         return len;
1438 }
1439
1440 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1441
1442 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1443 {
1444     lvars->module_vars  = NULL;
1445     lvars->obd_vars     = lprocfs_llite_obd_vars;
1446 }
1447 #endif /* LPROCFS */