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