Whamcloud - gitweb
LU-3321 osc: to drop LRU pages with cl_lru_work
[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 count, 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         int diff = 0;
435         int nrpages = 0;
436         ENTRY;
437
438         mult = 1 << (20 - PAGE_CACHE_SHIFT);
439         buffer = lprocfs_find_named_value(buffer, "max_cached_mb:", &count);
440         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
441         if (rc)
442                 RETURN(rc);
443
444         if (pages_number < 0 || pages_number > totalram_pages) {
445                 CERROR("%s: can't set max cache more than %lu MB\n",
446                        ll_get_fsname(sb, NULL, 0),
447                        totalram_pages >> (20 - PAGE_CACHE_SHIFT));
448                 RETURN(-ERANGE);
449         }
450
451         if (sbi->ll_dt_exp == NULL) /* being initialized */
452                 GOTO(out, rc = 0);
453
454         spin_lock(&sbi->ll_lock);
455         diff = pages_number - cache->ccc_lru_max;
456         spin_unlock(&sbi->ll_lock);
457
458         /* easy - add more LRU slots. */
459         if (diff >= 0) {
460                 cfs_atomic_add(diff, &cache->ccc_lru_left);
461                 GOTO(out, rc = 0);
462         }
463
464         env = cl_env_get(&refcheck);
465         if (IS_ERR(env))
466                 RETURN(rc);
467
468         diff = -diff;
469         while (diff > 0) {
470                 int tmp;
471
472                 /* reduce LRU budget from free slots. */
473                 do {
474                         int ov, nv;
475
476                         ov = cfs_atomic_read(&cache->ccc_lru_left);
477                         if (ov == 0)
478                                 break;
479
480                         nv = ov > diff ? ov - diff : 0;
481                         rc = cfs_atomic_cmpxchg(&cache->ccc_lru_left, ov, nv);
482                         if (likely(ov == rc)) {
483                                 diff -= ov - nv;
484                                 nrpages += ov - nv;
485                                 break;
486                         }
487                 } while (1);
488
489                 if (diff <= 0)
490                         break;
491
492                 /* difficult - have to ask OSCs to drop LRU slots. */
493                 tmp = diff << 1;
494                 rc = obd_set_info_async(env, sbi->ll_dt_exp,
495                                 sizeof(KEY_CACHE_LRU_SHRINK),
496                                 KEY_CACHE_LRU_SHRINK,
497                                 sizeof(tmp), &tmp, NULL);
498                 if (rc < 0)
499                         break;
500         }
501         cl_env_put(env, &refcheck);
502
503 out:
504         if (rc >= 0) {
505                 spin_lock(&sbi->ll_lock);
506                 cache->ccc_lru_max = pages_number;
507                 spin_unlock(&sbi->ll_lock);
508                 rc = count;
509         } else {
510                 cfs_atomic_add(nrpages, &cache->ccc_lru_left);
511         }
512         return rc;
513 }
514
515 static int ll_rd_checksum(char *page, char **start, off_t off,
516                           int count, int *eof, void *data)
517 {
518         struct super_block *sb = data;
519         struct ll_sb_info *sbi = ll_s2sbi(sb);
520
521         return snprintf(page, count, "%u\n",
522                         (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
523 }
524
525 static int ll_wr_checksum(struct file *file, const char *buffer,
526                           unsigned long count, void *data)
527 {
528         struct super_block *sb = data;
529         struct ll_sb_info *sbi = ll_s2sbi(sb);
530         int val, rc;
531
532         if (!sbi->ll_dt_exp)
533                 /* Not set up yet */
534                 return -EAGAIN;
535
536         rc = lprocfs_write_helper(buffer, count, &val);
537         if (rc)
538                 return rc;
539         if (val)
540                 sbi->ll_flags |= LL_SBI_CHECKSUM;
541         else
542                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
543
544         rc = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
545                                 KEY_CHECKSUM, sizeof(val), &val, NULL);
546         if (rc)
547                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
548
549         return count;
550 }
551
552 static int ll_rd_max_rw_chunk(char *page, char **start, off_t off,
553                           int count, int *eof, void *data)
554 {
555         struct super_block *sb = data;
556
557         return snprintf(page, count, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
558 }
559
560 static int ll_wr_max_rw_chunk(struct file *file, const char *buffer,
561                           unsigned long count, void *data)
562 {
563         struct super_block *sb = data;
564         int rc, val;
565
566         rc = lprocfs_write_helper(buffer, count, &val);
567         if (rc)
568                 return rc;
569         ll_s2sbi(sb)->ll_max_rw_chunk = val;
570         return count;
571 }
572
573 static int ll_rd_track_id(char *page, int count, void *data,
574                           enum stats_track_type type)
575 {
576         struct super_block *sb = data;
577
578         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
579                 return snprintf(page, count, "%d\n",
580                                 ll_s2sbi(sb)->ll_stats_track_id);
581
582         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
583                 return snprintf(page, count, "0 (all)\n");
584         } else {
585                 return snprintf(page, count, "untracked\n");
586         }
587 }
588
589 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
590                           enum stats_track_type type)
591 {
592         struct super_block *sb = data;
593         int rc, pid;
594
595         rc = lprocfs_write_helper(buffer, count, &pid);
596         if (rc)
597                 return rc;
598         ll_s2sbi(sb)->ll_stats_track_id = pid;
599         if (pid == 0)
600                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
601         else
602                 ll_s2sbi(sb)->ll_stats_track_type = type;
603         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
604         return count;
605 }
606
607 static int ll_rd_track_pid(char *page, char **start, off_t off,
608                           int count, int *eof, void *data)
609 {
610         return (ll_rd_track_id(page, count, data, STATS_TRACK_PID));
611 }
612
613 static int ll_wr_track_pid(struct file *file, const char *buffer,
614                           unsigned long count, void *data)
615 {
616         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PID));
617 }
618
619 static int ll_rd_track_ppid(char *page, char **start, off_t off,
620                           int count, int *eof, void *data)
621 {
622         return (ll_rd_track_id(page, count, data, STATS_TRACK_PPID));
623 }
624
625 static int ll_wr_track_ppid(struct file *file, const char *buffer,
626                           unsigned long count, void *data)
627 {
628         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PPID));
629 }
630
631 static int ll_rd_track_gid(char *page, char **start, off_t off,
632                           int count, int *eof, void *data)
633 {
634         return (ll_rd_track_id(page, count, data, STATS_TRACK_GID));
635 }
636
637 static int ll_wr_track_gid(struct file *file, const char *buffer,
638                           unsigned long count, void *data)
639 {
640         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_GID));
641 }
642
643 static int ll_rd_statahead_max(char *page, char **start, off_t off,
644                                int count, int *eof, void *data)
645 {
646         struct super_block *sb = data;
647         struct ll_sb_info *sbi = ll_s2sbi(sb);
648
649         return snprintf(page, count, "%u\n", sbi->ll_sa_max);
650 }
651
652 static int ll_wr_statahead_max(struct file *file, const char *buffer,
653                                unsigned long count, void *data)
654 {
655         struct super_block *sb = data;
656         struct ll_sb_info *sbi = ll_s2sbi(sb);
657         int val, rc;
658
659         rc = lprocfs_write_helper(buffer, count, &val);
660         if (rc)
661                 return rc;
662
663         if (val >= 0 && val <= LL_SA_RPC_MAX)
664                 sbi->ll_sa_max = val;
665         else
666                 CERROR("Bad statahead_max value %d. Valid values are in the "
667                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
668
669         return count;
670 }
671
672 static int ll_rd_statahead_agl(char *page, char **start, off_t off,
673                                int count, int *eof, void *data)
674 {
675         struct super_block *sb = data;
676         struct ll_sb_info *sbi = ll_s2sbi(sb);
677
678         return snprintf(page, count, "%u\n",
679                         sbi->ll_flags & LL_SBI_AGL_ENABLED ? 1 : 0);
680 }
681
682 static int ll_wr_statahead_agl(struct file *file, const char *buffer,
683                                unsigned long count, void *data)
684 {
685         struct super_block *sb = data;
686         struct ll_sb_info *sbi = ll_s2sbi(sb);
687         int val, rc;
688
689         rc = lprocfs_write_helper(buffer, count, &val);
690         if (rc)
691                 return rc;
692
693         if (val)
694                 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
695         else
696                 sbi->ll_flags &= ~LL_SBI_AGL_ENABLED;
697
698         return count;
699 }
700
701 static int ll_rd_statahead_stats(char *page, char **start, off_t off,
702                                  int count, int *eof, void *data)
703 {
704         struct super_block *sb = data;
705         struct ll_sb_info *sbi = ll_s2sbi(sb);
706
707         return snprintf(page, count,
708                         "statahead total: %u\n"
709                         "statahead wrong: %u\n"
710                         "agl total: %u\n",
711                         atomic_read(&sbi->ll_sa_total),
712                         atomic_read(&sbi->ll_sa_wrong),
713                         atomic_read(&sbi->ll_agl_total));
714 }
715
716 static int ll_rd_lazystatfs(char *page, char **start, off_t off,
717                             int count, int *eof, void *data)
718 {
719         struct super_block *sb = data;
720         struct ll_sb_info *sbi = ll_s2sbi(sb);
721
722         return snprintf(page, count, "%u\n",
723                         (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
724 }
725
726 static int ll_wr_lazystatfs(struct file *file, const char *buffer,
727                             unsigned long count, void *data)
728 {
729         struct super_block *sb = data;
730         struct ll_sb_info *sbi = ll_s2sbi(sb);
731         int val, rc;
732
733         rc = lprocfs_write_helper(buffer, count, &val);
734         if (rc)
735                 return rc;
736
737         if (val)
738                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
739         else
740                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
741
742         return count;
743 }
744
745 static int ll_rd_maxea_size(char *page, char **start, off_t off,
746                             int count, int *eof, void *data)
747 {
748         struct super_block *sb = data;
749         struct ll_sb_info *sbi = ll_s2sbi(sb);
750         unsigned int ealen;
751         int rc;
752
753         rc = ll_get_max_mdsize(sbi, &ealen);
754         if (rc)
755                 return rc;
756
757         return snprintf(page, count, "%u\n", ealen);
758 }
759
760 static int ll_rd_sbi_flags(char *page, char **start, off_t off,
761                                 int count, int *eof, void *data)
762 {
763         const char *str[] = LL_SBI_FLAGS;
764         struct super_block *sb = data;
765         int flags = ll_s2sbi(sb)->ll_flags;
766         int i = 0;
767         int rc = 0;
768
769         while (flags != 0) {
770                 if (ARRAY_SIZE(str) <= i) {
771                         CERROR("%s: Revise array LL_SBI_FLAGS to match sbi "
772                                 "flags please.\n", ll_get_fsname(sb, NULL, 0));
773                         return -EINVAL;
774                 }
775
776                 if (flags & 0x1)
777                         rc += snprintf(page + rc, count - rc, "%s ", str[i]);
778                 flags >>= 1;
779                 ++i;
780         }
781         if (rc > 0)
782                 rc += snprintf(page + rc, count - rc, "\b\n");
783         return rc;
784 }
785
786 static int ll_rd_unstable_stats(char *page, char **start, off_t off,
787                               int count, int *eof, void *data)
788 {
789         struct super_block      *sb    = data;
790         struct ll_sb_info       *sbi   = ll_s2sbi(sb);
791         struct cl_client_cache  *cache = &sbi->ll_cache;
792         int pages, mb, rc;
793
794         pages = cfs_atomic_read(&cache->ccc_unstable_nr);
795         mb    = (pages * PAGE_CACHE_SIZE) >> 20;
796
797         rc = snprintf(page, count, "unstable_pages: %8d\n"
798                                    "unstable_mb:    %8d\n", pages, mb);
799
800         return rc;
801 }
802
803 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
804         { "uuid",         ll_rd_sb_uuid,          0, 0 },
805         //{ "mntpt_path",   ll_rd_path,             0, 0 },
806         { "fstype",       ll_rd_fstype,           0, 0 },
807         { "site",         ll_rd_site_stats,       0, 0 },
808         { "blocksize",    ll_rd_blksize,          0, 0 },
809         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
810         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
811         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
812         { "filestotal",   ll_rd_filestotal,       0, 0 },
813         { "filesfree",    ll_rd_filesfree,        0, 0 },
814         { "client_type",  ll_rd_client_type,      0, 0 },
815         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
816         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
817                                ll_wr_max_readahead_mb, 0 },
818         { "max_read_ahead_per_file_mb", ll_rd_max_readahead_per_file_mb,
819                                         ll_wr_max_readahead_per_file_mb, 0 },
820         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
821                                      ll_wr_max_read_ahead_whole_mb, 0 },
822         { "max_cached_mb",    ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
823         { "checksum_pages",   ll_rd_checksum, ll_wr_checksum, 0 },
824         { "max_rw_chunk",     ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
825         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
826         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
827         { "stats_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
828         { "statahead_max",    ll_rd_statahead_max, ll_wr_statahead_max, 0 },
829         { "statahead_agl",    ll_rd_statahead_agl, ll_wr_statahead_agl, 0 },
830         { "statahead_stats",  ll_rd_statahead_stats, 0, 0 },
831         { "lazystatfs",       ll_rd_lazystatfs, ll_wr_lazystatfs, 0 },
832         { "max_easize",       ll_rd_maxea_size, 0, 0 },
833         { "sbi_flags",        ll_rd_sbi_flags, 0, 0 },
834         { "xattr_cache",      ll_rd_xattr_cache, ll_wr_xattr_cache, 0 },
835         { "unstable_stats",   ll_rd_unstable_stats, 0, 0},
836         { 0 }
837 };
838
839 #define MAX_STRING_SIZE 128
840
841 struct llite_file_opcode {
842         __u32       opcode;
843         __u32       type;
844         const char *opname;
845 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
846         /* file operation */
847         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
848         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
849         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
850                                    "read_bytes" },
851         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
852                                    "write_bytes" },
853         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
854                                    "brw_read" },
855         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
856                                    "brw_write" },
857         { LPROC_LL_OSC_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
858                                    "osc_read" },
859         { LPROC_LL_OSC_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
860                                    "osc_write" },
861         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
862         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
863         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
864         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
865         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
866         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
867         { LPROC_LL_READDIR,        LPROCFS_TYPE_REGS, "readdir" },
868         /* inode operation */
869         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
870         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
871         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
872         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
873         /* dir inode operation */
874         { LPROC_LL_CREATE,         LPROCFS_TYPE_REGS, "create" },
875         { LPROC_LL_LINK,           LPROCFS_TYPE_REGS, "link" },
876         { LPROC_LL_UNLINK,         LPROCFS_TYPE_REGS, "unlink" },
877         { LPROC_LL_SYMLINK,        LPROCFS_TYPE_REGS, "symlink" },
878         { LPROC_LL_MKDIR,          LPROCFS_TYPE_REGS, "mkdir" },
879         { LPROC_LL_RMDIR,          LPROCFS_TYPE_REGS, "rmdir" },
880         { LPROC_LL_MKNOD,          LPROCFS_TYPE_REGS, "mknod" },
881         { LPROC_LL_RENAME,         LPROCFS_TYPE_REGS, "rename" },
882         /* special inode operation */
883         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
884         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
885         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
886         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
887         { LPROC_LL_GETXATTR_HITS,  LPROCFS_TYPE_REGS, "getxattr_hits" },
888         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
889         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
890         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
891 };
892
893 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
894 {
895         if (!sbi->ll_stats)
896                 return;
897         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
898                 lprocfs_counter_add(sbi->ll_stats, op, count);
899         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
900                  sbi->ll_stats_track_id == current->pid)
901                 lprocfs_counter_add(sbi->ll_stats, op, count);
902         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
903                  sbi->ll_stats_track_id == current->parent->pid)
904                 lprocfs_counter_add(sbi->ll_stats, op, count);
905         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
906                  sbi->ll_stats_track_id == current_gid())
907                 lprocfs_counter_add(sbi->ll_stats, op, count);
908 }
909 EXPORT_SYMBOL(ll_stats_ops_tally);
910
911 static const char *ra_stat_string[] = {
912         [RA_STAT_HIT] = "hits",
913         [RA_STAT_MISS] = "misses",
914         [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
915         [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
916         [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
917         [RA_STAT_FAILED_MATCH] = "failed lock match",
918         [RA_STAT_DISCARDED] = "read but discarded",
919         [RA_STAT_ZERO_LEN] = "zero length file",
920         [RA_STAT_ZERO_WINDOW] = "zero size window",
921         [RA_STAT_EOF] = "read-ahead to EOF",
922         [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
923         [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
924 };
925
926
927 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
928                                 struct super_block *sb, char *osc, char *mdc)
929 {
930         struct lprocfs_vars lvars[2];
931         struct lustre_sb_info *lsi = s2lsi(sb);
932         struct ll_sb_info *sbi = ll_s2sbi(sb);
933         struct obd_device *obd;
934         char name[MAX_STRING_SIZE + 1], *ptr;
935         int err, id, len, rc;
936         ENTRY;
937
938         memset(lvars, 0, sizeof(lvars));
939
940         name[MAX_STRING_SIZE] = '\0';
941         lvars[0].name = name;
942
943         LASSERT(sbi != NULL);
944         LASSERT(mdc != NULL);
945         LASSERT(osc != NULL);
946
947         /* Get fsname */
948         len = strlen(lsi->lsi_lmd->lmd_profile);
949         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
950         if (ptr && (strcmp(ptr, "-client") == 0))
951                 len -= 7;
952
953         /* Mount info */
954         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
955                  lsi->lsi_lmd->lmd_profile, sb);
956
957         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
958         if (IS_ERR(sbi->ll_proc_root)) {
959                 err = PTR_ERR(sbi->ll_proc_root);
960                 sbi->ll_proc_root = NULL;
961                 RETURN(err);
962         }
963
964         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
965                                 &vvp_dump_pgcache_file_ops, sbi);
966         if (rc)
967                 CWARN("Error adding the dump_page_cache file\n");
968
969         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
970                                 &ll_rw_extents_stats_fops, sbi);
971         if (rc)
972                 CWARN("Error adding the extent_stats file\n");
973
974         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
975                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
976         if (rc)
977                 CWARN("Error adding the extents_stats_per_process file\n");
978
979         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
980                                 &ll_rw_offset_stats_fops, sbi);
981         if (rc)
982                 CWARN("Error adding the offset_stats file\n");
983
984         /* File operations stats */
985         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
986                                             LPROCFS_STATS_FLAG_NONE);
987         if (sbi->ll_stats == NULL)
988                 GOTO(out, err = -ENOMEM);
989         /* do counter init */
990         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
991                 __u32 type = llite_opcode_table[id].type;
992                 void *ptr = NULL;
993                 if (type & LPROCFS_TYPE_REGS)
994                         ptr = "regs";
995                 else if (type & LPROCFS_TYPE_BYTES)
996                         ptr = "bytes";
997                 else if (type & LPROCFS_TYPE_PAGES)
998                         ptr = "pages";
999                 lprocfs_counter_init(sbi->ll_stats,
1000                                      llite_opcode_table[id].opcode,
1001                                      (type & LPROCFS_CNTR_AVGMINMAX),
1002                                      llite_opcode_table[id].opname, ptr);
1003         }
1004         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
1005         if (err)
1006                 GOTO(out, err);
1007
1008         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
1009                                                LPROCFS_STATS_FLAG_NONE);
1010         if (sbi->ll_ra_stats == NULL)
1011                 GOTO(out, err = -ENOMEM);
1012
1013         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
1014                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
1015                                      ra_stat_string[id], "pages");
1016         err = lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
1017                                      sbi->ll_ra_stats);
1018         if (err)
1019                 GOTO(out, err);
1020
1021
1022         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
1023         if (err)
1024                 GOTO(out, err);
1025
1026         /* MDC info */
1027         obd = class_name2obd(mdc);
1028
1029         LASSERT(obd != NULL);
1030         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1031         LASSERT(obd->obd_type->typ_name != NULL);
1032
1033         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
1034                  obd->obd_type->typ_name);
1035         lvars[0].read_fptr = lprocfs_rd_name;
1036         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1037         if (err)
1038                 GOTO(out, err);
1039
1040         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
1041         lvars[0].read_fptr = lprocfs_rd_uuid;
1042         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1043         if (err)
1044                 GOTO(out, err);
1045
1046         /* OSC */
1047         obd = class_name2obd(osc);
1048
1049         LASSERT(obd != NULL);
1050         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1051         LASSERT(obd->obd_type->typ_name != NULL);
1052
1053         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
1054                  obd->obd_type->typ_name);
1055         lvars[0].read_fptr = lprocfs_rd_name;
1056         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1057         if (err)
1058                 GOTO(out, err);
1059
1060         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
1061         lvars[0].read_fptr = lprocfs_rd_uuid;
1062         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1063 out:
1064         if (err) {
1065                 lprocfs_remove(&sbi->ll_proc_root);
1066                 lprocfs_free_stats(&sbi->ll_ra_stats);
1067                 lprocfs_free_stats(&sbi->ll_stats);
1068         }
1069         RETURN(err);
1070 }
1071
1072 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
1073 {
1074         if (sbi->ll_proc_root) {
1075                 lprocfs_remove(&sbi->ll_proc_root);
1076                 lprocfs_free_stats(&sbi->ll_ra_stats);
1077                 lprocfs_free_stats(&sbi->ll_stats);
1078         }
1079 }
1080 #undef MAX_STRING_SIZE
1081
1082 #define pct(a,b) (b ? a * 100 / b : 0)
1083
1084 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1085                                    struct seq_file *seq, int which)
1086 {
1087         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1088         unsigned long start, end, r, w;
1089         char *unitp = "KMGTPEZY";
1090         int i, units = 10;
1091         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1092
1093         read_cum = 0;
1094         write_cum = 0;
1095         start = 0;
1096
1097         for(i = 0; i < LL_HIST_MAX; i++) {
1098                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1099                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1100         }
1101
1102         for(i = 0; i < LL_HIST_MAX; i++) {
1103                 r = pp_info->pp_r_hist.oh_buckets[i];
1104                 w = pp_info->pp_w_hist.oh_buckets[i];
1105                 read_cum += r;
1106                 write_cum += w;
1107                 end = 1 << (i + LL_HIST_START - units);
1108                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1109                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1110                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1111                            r, pct(r, read_tot), pct(read_cum, read_tot),
1112                            w, pct(w, write_tot), pct(write_cum, write_tot));
1113                 start = end;
1114                 if (start == 1<<10) {
1115                         start = 1;
1116                         units += 10;
1117                         unitp++;
1118                 }
1119                 if (read_cum == read_tot && write_cum == write_tot)
1120                         break;
1121         }
1122 }
1123
1124 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1125 {
1126         struct timeval now;
1127         struct ll_sb_info *sbi = seq->private;
1128         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1129         int k;
1130
1131         do_gettimeofday(&now);
1132
1133         if (!sbi->ll_rw_stats_on) {
1134                 seq_printf(seq, "disabled\n"
1135                                 "write anything in this file to activate, "
1136                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1137                 return 0;
1138         }
1139         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1140                    now.tv_sec, now.tv_usec);
1141         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1142         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1143                    "extents", "calls", "%", "cum%",
1144                    "calls", "%", "cum%");
1145         spin_lock(&sbi->ll_pp_extent_lock);
1146         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1147                 if (io_extents->pp_extents[k].pid != 0) {
1148                         seq_printf(seq, "\nPID: %d\n",
1149                                    io_extents->pp_extents[k].pid);
1150                         ll_display_extents_info(io_extents, seq, k);
1151                 }
1152         }
1153         spin_unlock(&sbi->ll_pp_extent_lock);
1154         return 0;
1155 }
1156
1157 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1158                                                 const char *buf, size_t len,
1159                                                 loff_t *off)
1160 {
1161         struct seq_file *seq = file->private_data;
1162         struct ll_sb_info *sbi = seq->private;
1163         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1164         int i;
1165         int value = 1, rc = 0;
1166
1167         rc = lprocfs_write_helper(buf, len, &value);
1168         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1169                        strcmp(buf, "Disabled") == 0))
1170                 value = 0;
1171
1172         if (value == 0)
1173                 sbi->ll_rw_stats_on = 0;
1174         else
1175                 sbi->ll_rw_stats_on = 1;
1176
1177         spin_lock(&sbi->ll_pp_extent_lock);
1178         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1179                 io_extents->pp_extents[i].pid = 0;
1180                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1181                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1182         }
1183         spin_unlock(&sbi->ll_pp_extent_lock);
1184         return len;
1185 }
1186
1187 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1188
1189 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1190 {
1191         struct timeval now;
1192         struct ll_sb_info *sbi = seq->private;
1193         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1194
1195         do_gettimeofday(&now);
1196
1197         if (!sbi->ll_rw_stats_on) {
1198                 seq_printf(seq, "disabled\n"
1199                                 "write anything in this file to activate, "
1200                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1201                 return 0;
1202         }
1203         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1204                    now.tv_sec, now.tv_usec);
1205
1206         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1207         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1208                    "extents", "calls", "%", "cum%",
1209                    "calls", "%", "cum%");
1210         spin_lock(&sbi->ll_lock);
1211         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1212         spin_unlock(&sbi->ll_lock);
1213
1214         return 0;
1215 }
1216
1217 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1218                                         size_t len, loff_t *off)
1219 {
1220         struct seq_file *seq = file->private_data;
1221         struct ll_sb_info *sbi = seq->private;
1222         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1223         int i;
1224         int value = 1, rc = 0;
1225
1226         rc = lprocfs_write_helper(buf, len, &value);
1227         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1228                        strcmp(buf, "Disabled") == 0))
1229                 value = 0;
1230
1231         if (value == 0)
1232                 sbi->ll_rw_stats_on = 0;
1233         else
1234                 sbi->ll_rw_stats_on = 1;
1235         spin_lock(&sbi->ll_pp_extent_lock);
1236         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
1237                 io_extents->pp_extents[i].pid = 0;
1238                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1239                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1240         }
1241         spin_unlock(&sbi->ll_pp_extent_lock);
1242
1243         return len;
1244 }
1245
1246 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1247
1248 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
1249                        struct ll_file_data *file, loff_t pos,
1250                        size_t count, int rw)
1251 {
1252         int i, cur = -1;
1253         struct ll_rw_process_info *process;
1254         struct ll_rw_process_info *offset;
1255         int *off_count = &sbi->ll_rw_offset_entry_count;
1256         int *process_count = &sbi->ll_offset_process_count;
1257         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1258
1259         if(!sbi->ll_rw_stats_on)
1260                 return;
1261         process = sbi->ll_rw_process_info;
1262         offset = sbi->ll_rw_offset_info;
1263
1264         spin_lock(&sbi->ll_pp_extent_lock);
1265         /* Extent statistics */
1266         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1267                 if(io_extents->pp_extents[i].pid == pid) {
1268                         cur = i;
1269                         break;
1270                 }
1271         }
1272
1273         if (cur == -1) {
1274                 /* new process */
1275                 sbi->ll_extent_process_count =
1276                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1277                 cur = sbi->ll_extent_process_count;
1278                 io_extents->pp_extents[cur].pid = pid;
1279                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1280                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1281         }
1282
1283         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1284              (i < (LL_HIST_MAX - 1)); i++);
1285         if (rw == 0) {
1286                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1287                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1288         } else {
1289                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1290                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1291         }
1292         spin_unlock(&sbi->ll_pp_extent_lock);
1293
1294         spin_lock(&sbi->ll_process_lock);
1295         /* Offset statistics */
1296         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1297                 if (process[i].rw_pid == pid) {
1298                         if (process[i].rw_last_file != file) {
1299                                 process[i].rw_range_start = pos;
1300                                 process[i].rw_last_file_pos = pos + count;
1301                                 process[i].rw_smallest_extent = count;
1302                                 process[i].rw_largest_extent = count;
1303                                 process[i].rw_offset = 0;
1304                                 process[i].rw_last_file = file;
1305                                 spin_unlock(&sbi->ll_process_lock);
1306                                 return;
1307                         }
1308                         if (process[i].rw_last_file_pos != pos) {
1309                                 *off_count =
1310                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1311                                 offset[*off_count].rw_op = process[i].rw_op;
1312                                 offset[*off_count].rw_pid = pid;
1313                                 offset[*off_count].rw_range_start =
1314                                         process[i].rw_range_start;
1315                                 offset[*off_count].rw_range_end =
1316                                         process[i].rw_last_file_pos;
1317                                 offset[*off_count].rw_smallest_extent =
1318                                         process[i].rw_smallest_extent;
1319                                 offset[*off_count].rw_largest_extent =
1320                                         process[i].rw_largest_extent;
1321                                 offset[*off_count].rw_offset =
1322                                         process[i].rw_offset;
1323                                 process[i].rw_op = rw;
1324                                 process[i].rw_range_start = pos;
1325                                 process[i].rw_smallest_extent = count;
1326                                 process[i].rw_largest_extent = count;
1327                                 process[i].rw_offset = pos -
1328                                         process[i].rw_last_file_pos;
1329                         }
1330                         if(process[i].rw_smallest_extent > count)
1331                                 process[i].rw_smallest_extent = count;
1332                         if(process[i].rw_largest_extent < count)
1333                                 process[i].rw_largest_extent = count;
1334                         process[i].rw_last_file_pos = pos + count;
1335                         spin_unlock(&sbi->ll_process_lock);
1336                         return;
1337                 }
1338         }
1339         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1340         process[*process_count].rw_pid = pid;
1341         process[*process_count].rw_op = rw;
1342         process[*process_count].rw_range_start = pos;
1343         process[*process_count].rw_last_file_pos = pos + count;
1344         process[*process_count].rw_smallest_extent = count;
1345         process[*process_count].rw_largest_extent = count;
1346         process[*process_count].rw_offset = 0;
1347         process[*process_count].rw_last_file = file;
1348         spin_unlock(&sbi->ll_process_lock);
1349 }
1350
1351 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1352 {
1353         struct timeval now;
1354         struct ll_sb_info *sbi = seq->private;
1355         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1356         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1357         int i;
1358
1359         do_gettimeofday(&now);
1360
1361         if (!sbi->ll_rw_stats_on) {
1362                 seq_printf(seq, "disabled\n"
1363                                 "write anything in this file to activate, "
1364                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1365                 return 0;
1366         }
1367         spin_lock(&sbi->ll_process_lock);
1368
1369         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1370                    now.tv_sec, now.tv_usec);
1371         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1372                    "R/W", "PID", "RANGE START", "RANGE END",
1373                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1374
1375         /* We stored the discontiguous offsets here; print them first */
1376         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1377                 if (offset[i].rw_pid != 0)
1378                         seq_printf(seq,
1379                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1380                                    offset[i].rw_op == READ ? 'R' : 'W',
1381                                    offset[i].rw_pid,
1382                                    offset[i].rw_range_start,
1383                                    offset[i].rw_range_end,
1384                                    (unsigned long)offset[i].rw_smallest_extent,
1385                                    (unsigned long)offset[i].rw_largest_extent,
1386                                    offset[i].rw_offset);
1387         }
1388
1389         /* Then print the current offsets for each process */
1390         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1391                 if (process[i].rw_pid != 0)
1392                         seq_printf(seq,
1393                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1394                                    process[i].rw_op == READ ? 'R' : 'W',
1395                                    process[i].rw_pid,
1396                                    process[i].rw_range_start,
1397                                    process[i].rw_last_file_pos,
1398                                    (unsigned long)process[i].rw_smallest_extent,
1399                                    (unsigned long)process[i].rw_largest_extent,
1400                                    process[i].rw_offset);
1401         }
1402         spin_unlock(&sbi->ll_process_lock);
1403
1404         return 0;
1405 }
1406
1407 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1408                                        size_t len, loff_t *off)
1409 {
1410         struct seq_file *seq = file->private_data;
1411         struct ll_sb_info *sbi = seq->private;
1412         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1413         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1414         int value = 1, rc = 0;
1415
1416         rc = lprocfs_write_helper(buf, len, &value);
1417
1418         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1419                            strcmp(buf, "Disabled") == 0))
1420                 value = 0;
1421
1422         if (value == 0)
1423                 sbi->ll_rw_stats_on = 0;
1424         else
1425                 sbi->ll_rw_stats_on = 1;
1426
1427         spin_lock(&sbi->ll_process_lock);
1428         sbi->ll_offset_process_count = 0;
1429         sbi->ll_rw_offset_entry_count = 0;
1430         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1431                LL_PROCESS_HIST_MAX);
1432         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1433                LL_OFFSET_HIST_MAX);
1434         spin_unlock(&sbi->ll_process_lock);
1435
1436         return len;
1437 }
1438
1439 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1440
1441 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1442 {
1443     lvars->module_vars  = NULL;
1444     lvars->obd_vars     = lprocfs_llite_obd_vars;
1445 }
1446 #endif /* LPROCFS */