8c8a0fa093b234bfff57f2424c9edde04d237c44
[firefly-linux-kernel-4.4.55.git] / drivers / staging / lustre / 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) 2011, 2012, 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 "../include/lustre_lite.h"
39 #include "../include/lprocfs_status.h"
40 #include <linux/seq_file.h>
41 #include "../include/obd_support.h"
42
43 #include "llite_internal.h"
44 #include "vvp_internal.h"
45
46 /* /proc/lustre/llite mount point registration */
47 static struct file_operations ll_rw_extents_stats_fops;
48 static struct file_operations ll_rw_extents_stats_pp_fops;
49 static struct file_operations ll_rw_offset_stats_fops;
50
51 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
52                               char *buf)
53 {
54         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
55                                               ll_kobj);
56         struct obd_statfs osfs;
57         int rc;
58
59         rc = ll_statfs_internal(sbi->ll_sb, &osfs,
60                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
61                                 OBD_STATFS_NODELAY);
62         if (!rc)
63                 return sprintf(buf, "%u\n", osfs.os_bsize);
64
65         return rc;
66 }
67 LUSTRE_RO_ATTR(blocksize);
68
69 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
70                                 char *buf)
71 {
72         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
73                                               ll_kobj);
74         struct obd_statfs osfs;
75         int rc;
76
77         rc = ll_statfs_internal(sbi->ll_sb, &osfs,
78                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
79                                 OBD_STATFS_NODELAY);
80         if (!rc) {
81                 __u32 blk_size = osfs.os_bsize >> 10;
82                 __u64 result = osfs.os_blocks;
83
84                 while (blk_size >>= 1)
85                         result <<= 1;
86
87                 rc = sprintf(buf, "%llu\n", result);
88         }
89
90         return rc;
91 }
92 LUSTRE_RO_ATTR(kbytestotal);
93
94 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
95                                char *buf)
96 {
97         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
98                                               ll_kobj);
99         struct obd_statfs osfs;
100         int rc;
101
102         rc = ll_statfs_internal(sbi->ll_sb, &osfs,
103                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
104                                 OBD_STATFS_NODELAY);
105         if (!rc) {
106                 __u32 blk_size = osfs.os_bsize >> 10;
107                 __u64 result = osfs.os_bfree;
108
109                 while (blk_size >>= 1)
110                         result <<= 1;
111
112                 rc = sprintf(buf, "%llu\n", result);
113         }
114
115         return rc;
116 }
117 LUSTRE_RO_ATTR(kbytesfree);
118
119 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
120                                 char *buf)
121 {
122         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
123                                               ll_kobj);
124         struct obd_statfs osfs;
125         int rc;
126
127         rc = ll_statfs_internal(sbi->ll_sb, &osfs,
128                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
129                                 OBD_STATFS_NODELAY);
130         if (!rc) {
131                 __u32 blk_size = osfs.os_bsize >> 10;
132                 __u64 result = osfs.os_bavail;
133
134                 while (blk_size >>= 1)
135                         result <<= 1;
136
137                 rc = sprintf(buf, "%llu\n", result);
138         }
139
140         return rc;
141 }
142 LUSTRE_RO_ATTR(kbytesavail);
143
144 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
145                                char *buf)
146 {
147         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
148                                               ll_kobj);
149         struct obd_statfs osfs;
150         int rc;
151
152         rc = ll_statfs_internal(sbi->ll_sb, &osfs,
153                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
154                                 OBD_STATFS_NODELAY);
155         if (!rc)
156                 return sprintf(buf, "%llu\n", osfs.os_files);
157
158         return rc;
159 }
160 LUSTRE_RO_ATTR(filestotal);
161
162 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
163                               char *buf)
164 {
165         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
166                                               ll_kobj);
167         struct obd_statfs osfs;
168         int rc;
169
170         rc = ll_statfs_internal(sbi->ll_sb, &osfs,
171                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
172                                 OBD_STATFS_NODELAY);
173         if (!rc)
174                 return sprintf(buf, "%llu\n", osfs.os_ffree);
175
176         return rc;
177 }
178 LUSTRE_RO_ATTR(filesfree);
179
180 static ssize_t client_type_show(struct kobject *kobj, struct attribute *attr,
181                                 char *buf)
182 {
183         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
184                                               ll_kobj);
185
186         return sprintf(buf, "%s client\n",
187                         sbi->ll_flags & LL_SBI_RMT_CLIENT ? "remote" : "local");
188 }
189 LUSTRE_RO_ATTR(client_type);
190
191 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
192                            char *buf)
193 {
194         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
195                                               ll_kobj);
196
197         return sprintf(buf, "%s\n", sbi->ll_sb->s_type->name);
198 }
199 LUSTRE_RO_ATTR(fstype);
200
201 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
202                          char *buf)
203 {
204         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
205                                               ll_kobj);
206
207         return sprintf(buf, "%s\n", sbi->ll_sb_uuid.uuid);
208 }
209 LUSTRE_RO_ATTR(uuid);
210
211 static int ll_site_stats_seq_show(struct seq_file *m, void *v)
212 {
213         struct super_block *sb = m->private;
214
215         /*
216          * See description of statistical counters in struct cl_site, and
217          * struct lu_site.
218          */
219         return cl_site_stats_print(lu2cl_site(ll_s2sbi(sb)->ll_site), m);
220 }
221 LPROC_SEQ_FOPS_RO(ll_site_stats);
222
223 static ssize_t max_read_ahead_mb_show(struct kobject *kobj,
224                                       struct attribute *attr, char *buf)
225 {
226         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
227                                               ll_kobj);
228         long pages_number;
229         int mult;
230
231         spin_lock(&sbi->ll_lock);
232         pages_number = sbi->ll_ra_info.ra_max_pages;
233         spin_unlock(&sbi->ll_lock);
234
235         mult = 1 << (20 - PAGE_CACHE_SHIFT);
236         return lprocfs_read_frac_helper(buf, PAGE_SIZE, pages_number, mult);
237 }
238
239 static ssize_t max_read_ahead_mb_store(struct kobject *kobj,
240                                        struct attribute *attr,
241                                        const char *buffer,
242                                        size_t count)
243 {
244         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
245                                               ll_kobj);
246         int rc;
247         unsigned long pages_number;
248
249         rc = kstrtoul(buffer, 10, &pages_number);
250         if (rc)
251                 return rc;
252
253         pages_number *= 1 << (20 - PAGE_CACHE_SHIFT); /* MB -> pages */
254
255         if (pages_number > totalram_pages / 2) {
256
257                 CERROR("can't set file readahead more than %lu MB\n",
258                        totalram_pages >> (20 - PAGE_CACHE_SHIFT + 1)); /*1/2 of RAM*/
259                 return -ERANGE;
260         }
261
262         spin_lock(&sbi->ll_lock);
263         sbi->ll_ra_info.ra_max_pages = pages_number;
264         spin_unlock(&sbi->ll_lock);
265
266         return count;
267 }
268 LUSTRE_RW_ATTR(max_read_ahead_mb);
269
270 static ssize_t max_read_ahead_per_file_mb_show(struct kobject *kobj,
271                                                struct attribute *attr,
272                                                char *buf)
273 {
274         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
275                                               ll_kobj);
276         long pages_number;
277         int mult;
278
279         spin_lock(&sbi->ll_lock);
280         pages_number = sbi->ll_ra_info.ra_max_pages_per_file;
281         spin_unlock(&sbi->ll_lock);
282
283         mult = 1 << (20 - PAGE_CACHE_SHIFT);
284         return lprocfs_read_frac_helper(buf, PAGE_SIZE, pages_number, mult);
285 }
286
287 static ssize_t max_read_ahead_per_file_mb_store(struct kobject *kobj,
288                                                 struct attribute *attr,
289                                                 const char *buffer,
290                                                 size_t count)
291 {
292         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
293                                               ll_kobj);
294         int rc;
295         unsigned long pages_number;
296
297         rc = kstrtoul(buffer, 10, &pages_number);
298         if (rc)
299                 return rc;
300
301         if (pages_number > sbi->ll_ra_info.ra_max_pages) {
302                 CERROR("can't set file readahead more than max_read_ahead_mb %lu MB\n",
303                        sbi->ll_ra_info.ra_max_pages);
304                 return -ERANGE;
305         }
306
307         spin_lock(&sbi->ll_lock);
308         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
309         spin_unlock(&sbi->ll_lock);
310
311         return count;
312 }
313 LUSTRE_RW_ATTR(max_read_ahead_per_file_mb);
314
315 static int ll_max_read_ahead_whole_mb_seq_show(struct seq_file *m, void *unused)
316 {
317         struct super_block *sb = m->private;
318         struct ll_sb_info *sbi = ll_s2sbi(sb);
319         long pages_number;
320         int mult;
321
322         spin_lock(&sbi->ll_lock);
323         pages_number = sbi->ll_ra_info.ra_max_read_ahead_whole_pages;
324         spin_unlock(&sbi->ll_lock);
325
326         mult = 1 << (20 - PAGE_CACHE_SHIFT);
327         return lprocfs_seq_read_frac_helper(m, pages_number, mult);
328 }
329
330 static ssize_t ll_max_read_ahead_whole_mb_seq_write(struct file *file,
331                                                 const char __user *buffer,
332                                                 size_t count, loff_t *off)
333 {
334         struct super_block *sb = ((struct seq_file *)file->private_data)->private;
335         struct ll_sb_info *sbi = ll_s2sbi(sb);
336         int mult, rc, pages_number;
337
338         mult = 1 << (20 - PAGE_CACHE_SHIFT);
339         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
340         if (rc)
341                 return rc;
342
343         /* Cap this at the current max readahead window size, the readahead
344          * algorithm does this anyway so it's pointless to set it larger. */
345         if (pages_number < 0 ||
346             pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
347                 CERROR("can't set max_read_ahead_whole_mb more than max_read_ahead_per_file_mb: %lu\n",
348                        sbi->ll_ra_info.ra_max_pages_per_file >> (20 - PAGE_CACHE_SHIFT));
349                 return -ERANGE;
350         }
351
352         spin_lock(&sbi->ll_lock);
353         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
354         spin_unlock(&sbi->ll_lock);
355
356         return count;
357 }
358 LPROC_SEQ_FOPS(ll_max_read_ahead_whole_mb);
359
360 static int ll_max_cached_mb_seq_show(struct seq_file *m, void *v)
361 {
362         struct super_block     *sb    = m->private;
363         struct ll_sb_info      *sbi   = ll_s2sbi(sb);
364         struct cl_client_cache *cache = &sbi->ll_cache;
365         int shift = 20 - PAGE_CACHE_SHIFT;
366         int max_cached_mb;
367         int unused_mb;
368
369         max_cached_mb = cache->ccc_lru_max >> shift;
370         unused_mb = atomic_read(&cache->ccc_lru_left) >> shift;
371         seq_printf(m,
372                    "users: %d\n"
373                    "max_cached_mb: %d\n"
374                    "used_mb: %d\n"
375                    "unused_mb: %d\n"
376                    "reclaim_count: %u\n",
377                    atomic_read(&cache->ccc_users),
378                    max_cached_mb,
379                    max_cached_mb - unused_mb,
380                    unused_mb,
381                    cache->ccc_lru_shrinkers);
382         return 0;
383 }
384
385 static ssize_t ll_max_cached_mb_seq_write(struct file *file,
386                                           const char __user *buffer,
387                                           size_t count, loff_t *off)
388 {
389         struct super_block *sb = ((struct seq_file *)file->private_data)->private;
390         struct ll_sb_info *sbi = ll_s2sbi(sb);
391         struct cl_client_cache *cache = &sbi->ll_cache;
392         int mult, rc, pages_number;
393         int diff = 0;
394         int nrpages = 0;
395         char kernbuf[128];
396
397         if (count >= sizeof(kernbuf))
398                 return -EINVAL;
399
400         if (copy_from_user(kernbuf, buffer, count))
401                 return -EFAULT;
402         kernbuf[count] = 0;
403
404         mult = 1 << (20 - PAGE_CACHE_SHIFT);
405         buffer += lprocfs_find_named_value(kernbuf, "max_cached_mb:", &count) -
406                   kernbuf;
407         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
408         if (rc)
409                 return rc;
410
411         if (pages_number < 0 || pages_number > totalram_pages) {
412                 CERROR("%s: can't set max cache more than %lu MB\n",
413                        ll_get_fsname(sb, NULL, 0),
414                        totalram_pages >> (20 - PAGE_CACHE_SHIFT));
415                 return -ERANGE;
416         }
417
418         spin_lock(&sbi->ll_lock);
419         diff = pages_number - cache->ccc_lru_max;
420         spin_unlock(&sbi->ll_lock);
421
422         /* easy - add more LRU slots. */
423         if (diff >= 0) {
424                 atomic_add(diff, &cache->ccc_lru_left);
425                 rc = 0;
426                 goto out;
427         }
428
429         diff = -diff;
430         while (diff > 0) {
431                 int tmp;
432
433                 /* reduce LRU budget from free slots. */
434                 do {
435                         int ov, nv;
436
437                         ov = atomic_read(&cache->ccc_lru_left);
438                         if (ov == 0)
439                                 break;
440
441                         nv = ov > diff ? ov - diff : 0;
442                         rc = atomic_cmpxchg(&cache->ccc_lru_left, ov, nv);
443                         if (likely(ov == rc)) {
444                                 diff -= ov - nv;
445                                 nrpages += ov - nv;
446                                 break;
447                         }
448                 } while (1);
449
450                 if (diff <= 0)
451                         break;
452
453                 if (sbi->ll_dt_exp == NULL) { /* being initialized */
454                         rc = -ENODEV;
455                         break;
456                 }
457
458                 /* difficult - have to ask OSCs to drop LRU slots. */
459                 tmp = diff << 1;
460                 rc = obd_set_info_async(NULL, sbi->ll_dt_exp,
461                                 sizeof(KEY_CACHE_LRU_SHRINK),
462                                 KEY_CACHE_LRU_SHRINK,
463                                 sizeof(tmp), &tmp, NULL);
464                 if (rc < 0)
465                         break;
466         }
467
468 out:
469         if (rc >= 0) {
470                 spin_lock(&sbi->ll_lock);
471                 cache->ccc_lru_max = pages_number;
472                 spin_unlock(&sbi->ll_lock);
473                 rc = count;
474         } else {
475                 atomic_add(nrpages, &cache->ccc_lru_left);
476         }
477         return rc;
478 }
479 LPROC_SEQ_FOPS(ll_max_cached_mb);
480
481 static int ll_checksum_seq_show(struct seq_file *m, void *v)
482 {
483         struct super_block *sb = m->private;
484         struct ll_sb_info *sbi = ll_s2sbi(sb);
485
486         seq_printf(m, "%u\n", (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
487         return 0;
488 }
489
490 static ssize_t ll_checksum_seq_write(struct file *file,
491                                      const char __user *buffer,
492                                      size_t count, loff_t *off)
493 {
494         struct super_block *sb = ((struct seq_file *)file->private_data)->private;
495         struct ll_sb_info *sbi = ll_s2sbi(sb);
496         int val, rc;
497
498         if (!sbi->ll_dt_exp)
499                 /* Not set up yet */
500                 return -EAGAIN;
501
502         rc = lprocfs_write_helper(buffer, count, &val);
503         if (rc)
504                 return rc;
505         if (val)
506                 sbi->ll_flags |= LL_SBI_CHECKSUM;
507         else
508                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
509
510         rc = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
511                                 KEY_CHECKSUM, sizeof(val), &val, NULL);
512         if (rc)
513                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
514
515         return count;
516 }
517 LPROC_SEQ_FOPS(ll_checksum);
518
519 static int ll_max_rw_chunk_seq_show(struct seq_file *m, void *v)
520 {
521         struct super_block *sb = m->private;
522
523         seq_printf(m, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
524         return 0;
525 }
526
527 static ssize_t ll_max_rw_chunk_seq_write(struct file *file,
528                                          const char __user *buffer,
529                                          size_t count, loff_t *off)
530 {
531         struct super_block *sb = ((struct seq_file *)file->private_data)->private;
532         int rc, val;
533
534         rc = lprocfs_write_helper(buffer, count, &val);
535         if (rc)
536                 return rc;
537         ll_s2sbi(sb)->ll_max_rw_chunk = val;
538         return count;
539 }
540 LPROC_SEQ_FOPS(ll_max_rw_chunk);
541
542 static int ll_rd_track_id(struct seq_file *m, enum stats_track_type type)
543 {
544         struct super_block *sb = m->private;
545
546         if (ll_s2sbi(sb)->ll_stats_track_type == type)
547                 seq_printf(m, "%d\n", ll_s2sbi(sb)->ll_stats_track_id);
548         else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL)
549                 seq_puts(m, "0 (all)\n");
550         else
551                 seq_puts(m, "untracked\n");
552
553         return 0;
554 }
555
556 static int ll_wr_track_id(const char __user *buffer, unsigned long count,
557                           void *data, enum stats_track_type type)
558 {
559         struct super_block *sb = data;
560         int rc, pid;
561
562         rc = lprocfs_write_helper(buffer, count, &pid);
563         if (rc)
564                 return rc;
565         ll_s2sbi(sb)->ll_stats_track_id = pid;
566         if (pid == 0)
567                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
568         else
569                 ll_s2sbi(sb)->ll_stats_track_type = type;
570         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
571         return count;
572 }
573
574 static int ll_track_pid_seq_show(struct seq_file *m, void *v)
575 {
576         return ll_rd_track_id(m, STATS_TRACK_PID);
577 }
578
579 static ssize_t ll_track_pid_seq_write(struct file *file,
580                                       const char __user *buffer,
581                                       size_t count, loff_t *off)
582 {
583         struct seq_file *seq = file->private_data;
584         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_PID);
585 }
586 LPROC_SEQ_FOPS(ll_track_pid);
587
588 static int ll_track_ppid_seq_show(struct seq_file *m, void *v)
589 {
590         return ll_rd_track_id(m, STATS_TRACK_PPID);
591 }
592
593 static ssize_t ll_track_ppid_seq_write(struct file *file,
594                                        const char __user *buffer,
595                                        size_t count, loff_t *off)
596 {
597         struct seq_file *seq = file->private_data;
598         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_PPID);
599 }
600 LPROC_SEQ_FOPS(ll_track_ppid);
601
602 static int ll_track_gid_seq_show(struct seq_file *m, void *v)
603 {
604         return ll_rd_track_id(m, STATS_TRACK_GID);
605 }
606
607 static ssize_t ll_track_gid_seq_write(struct file *file,
608                                       const char __user *buffer,
609                                       size_t count, loff_t *off)
610 {
611         struct seq_file *seq = file->private_data;
612         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_GID);
613 }
614 LPROC_SEQ_FOPS(ll_track_gid);
615
616 static int ll_statahead_max_seq_show(struct seq_file *m, void *v)
617 {
618         struct super_block *sb = m->private;
619         struct ll_sb_info *sbi = ll_s2sbi(sb);
620
621         seq_printf(m, "%u\n", sbi->ll_sa_max);
622         return 0;
623 }
624
625 static ssize_t ll_statahead_max_seq_write(struct file *file,
626                                           const char __user *buffer,
627                                           size_t count, loff_t *off)
628 {
629         struct super_block *sb = ((struct seq_file *)file->private_data)->private;
630         struct ll_sb_info *sbi = ll_s2sbi(sb);
631         int val, rc;
632
633         rc = lprocfs_write_helper(buffer, count, &val);
634         if (rc)
635                 return rc;
636
637         if (val >= 0 && val <= LL_SA_RPC_MAX)
638                 sbi->ll_sa_max = val;
639         else
640                 CERROR("Bad statahead_max value %d. Valid values are in the range [0, %d]\n",
641                        val, LL_SA_RPC_MAX);
642
643         return count;
644 }
645 LPROC_SEQ_FOPS(ll_statahead_max);
646
647 static int ll_statahead_agl_seq_show(struct seq_file *m, void *v)
648 {
649         struct super_block *sb = m->private;
650         struct ll_sb_info *sbi = ll_s2sbi(sb);
651
652         seq_printf(m, "%u\n", sbi->ll_flags & LL_SBI_AGL_ENABLED ? 1 : 0);
653         return 0;
654 }
655
656 static ssize_t ll_statahead_agl_seq_write(struct file *file,
657                                           const char __user *buffer,
658                                           size_t count, loff_t *off)
659 {
660         struct super_block *sb = ((struct seq_file *)file->private_data)->private;
661         struct ll_sb_info *sbi = ll_s2sbi(sb);
662         int val, rc;
663
664         rc = lprocfs_write_helper(buffer, count, &val);
665         if (rc)
666                 return rc;
667
668         if (val)
669                 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
670         else
671                 sbi->ll_flags &= ~LL_SBI_AGL_ENABLED;
672
673         return count;
674 }
675 LPROC_SEQ_FOPS(ll_statahead_agl);
676
677 static int ll_statahead_stats_seq_show(struct seq_file *m, void *v)
678 {
679         struct super_block *sb = m->private;
680         struct ll_sb_info *sbi = ll_s2sbi(sb);
681
682         seq_printf(m,
683                    "statahead total: %u\n"
684                    "statahead wrong: %u\n"
685                    "agl total: %u\n",
686                    atomic_read(&sbi->ll_sa_total),
687                    atomic_read(&sbi->ll_sa_wrong),
688                    atomic_read(&sbi->ll_agl_total));
689         return 0;
690 }
691 LPROC_SEQ_FOPS_RO(ll_statahead_stats);
692
693 static int ll_lazystatfs_seq_show(struct seq_file *m, void *v)
694 {
695         struct super_block *sb = m->private;
696         struct ll_sb_info *sbi = ll_s2sbi(sb);
697
698         seq_printf(m, "%u\n", sbi->ll_flags & LL_SBI_LAZYSTATFS ? 1 : 0);
699         return 0;
700 }
701
702 static ssize_t ll_lazystatfs_seq_write(struct file *file,
703                                        const char __user *buffer,
704                                        size_t count, loff_t *off)
705 {
706         struct super_block *sb = ((struct seq_file *)file->private_data)->private;
707         struct ll_sb_info *sbi = ll_s2sbi(sb);
708         int val, rc;
709
710         rc = lprocfs_write_helper(buffer, count, &val);
711         if (rc)
712                 return rc;
713
714         if (val)
715                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
716         else
717                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
718
719         return count;
720 }
721 LPROC_SEQ_FOPS(ll_lazystatfs);
722
723 static int ll_max_easize_seq_show(struct seq_file *m, void *v)
724 {
725         struct super_block *sb = m->private;
726         struct ll_sb_info *sbi = ll_s2sbi(sb);
727         unsigned int ealen;
728         int rc;
729
730         rc = ll_get_max_mdsize(sbi, &ealen);
731         if (rc)
732                 return rc;
733
734         seq_printf(m, "%u\n", ealen);
735         return 0;
736 }
737 LPROC_SEQ_FOPS_RO(ll_max_easize);
738
739 static int ll_default_easize_seq_show(struct seq_file *m, void *v)
740 {
741         struct super_block *sb = m->private;
742         struct ll_sb_info *sbi = ll_s2sbi(sb);
743         unsigned int ealen;
744         int rc;
745
746         rc = ll_get_default_mdsize(sbi, &ealen);
747         if (rc)
748                 return rc;
749
750         seq_printf(m, "%u\n", ealen);
751         return 0;
752 }
753 LPROC_SEQ_FOPS_RO(ll_default_easize);
754
755 static int ll_max_cookiesize_seq_show(struct seq_file *m, void *v)
756 {
757         struct super_block *sb = m->private;
758         struct ll_sb_info *sbi = ll_s2sbi(sb);
759         unsigned int cookielen;
760         int rc;
761
762         rc = ll_get_max_cookiesize(sbi, &cookielen);
763         if (rc)
764                 return rc;
765
766         seq_printf(m, "%u\n", cookielen);
767         return 0;
768 }
769 LPROC_SEQ_FOPS_RO(ll_max_cookiesize);
770
771 static int ll_default_cookiesize_seq_show(struct seq_file *m, void *v)
772 {
773         struct super_block *sb = m->private;
774         struct ll_sb_info *sbi = ll_s2sbi(sb);
775         unsigned int cookielen;
776         int rc;
777
778         rc = ll_get_default_cookiesize(sbi, &cookielen);
779         if (rc)
780                 return rc;
781
782         seq_printf(m, "%u\n", cookielen);
783         return 0;
784 }
785 LPROC_SEQ_FOPS_RO(ll_default_cookiesize);
786
787 static int ll_sbi_flags_seq_show(struct seq_file *m, void *v)
788 {
789         const char *str[] = LL_SBI_FLAGS;
790         struct super_block *sb = m->private;
791         int flags = ll_s2sbi(sb)->ll_flags;
792         int i = 0;
793
794         while (flags != 0) {
795                 if (ARRAY_SIZE(str) <= i) {
796                         CERROR("%s: Revise array LL_SBI_FLAGS to match sbi flags please.\n",
797                                ll_get_fsname(sb, NULL, 0));
798                         return -EINVAL;
799                 }
800
801                 if (flags & 0x1)
802                         seq_printf(m, "%s ", str[i]);
803                 flags >>= 1;
804                 ++i;
805         }
806         seq_printf(m, "\b\n");
807         return 0;
808 }
809 LPROC_SEQ_FOPS_RO(ll_sbi_flags);
810
811 static int ll_xattr_cache_seq_show(struct seq_file *m, void *v)
812 {
813         struct super_block *sb = m->private;
814         struct ll_sb_info *sbi = ll_s2sbi(sb);
815
816         seq_printf(m, "%u\n", sbi->ll_xattr_cache_enabled);
817
818         return 0;
819 }
820
821 static ssize_t ll_xattr_cache_seq_write(struct file *file,
822                                         const char __user *buffer,
823                                         size_t count, loff_t *off)
824 {
825         struct seq_file *seq = file->private_data;
826         struct super_block *sb = seq->private;
827         struct ll_sb_info *sbi = ll_s2sbi(sb);
828         int val, rc;
829
830         rc = lprocfs_write_helper(buffer, count, &val);
831         if (rc)
832                 return rc;
833
834         if (val != 0 && val != 1)
835                 return -ERANGE;
836
837         if (val == 1 && !(sbi->ll_flags & LL_SBI_XATTR_CACHE))
838                 return -ENOTSUPP;
839
840         sbi->ll_xattr_cache_enabled = val;
841
842         return count;
843 }
844 LPROC_SEQ_FOPS(ll_xattr_cache);
845
846 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
847         /* { "mntpt_path",   ll_rd_path,             0, 0 }, */
848         { "site",         &ll_site_stats_fops,    NULL, 0 },
849         /* { "filegroups",   lprocfs_rd_filegroups,  0, 0 }, */
850         { "max_read_ahead_whole_mb", &ll_max_read_ahead_whole_mb_fops, NULL },
851         { "max_cached_mb",    &ll_max_cached_mb_fops, NULL },
852         { "checksum_pages",   &ll_checksum_fops, NULL },
853         { "max_rw_chunk",     &ll_max_rw_chunk_fops, NULL },
854         { "stats_track_pid",  &ll_track_pid_fops, NULL },
855         { "stats_track_ppid", &ll_track_ppid_fops, NULL },
856         { "stats_track_gid",  &ll_track_gid_fops, NULL },
857         { "statahead_max",    &ll_statahead_max_fops, NULL },
858         { "statahead_agl",    &ll_statahead_agl_fops, NULL },
859         { "statahead_stats",  &ll_statahead_stats_fops, NULL, 0 },
860         { "lazystatfs",       &ll_lazystatfs_fops, NULL },
861         { "max_easize",       &ll_max_easize_fops, NULL, 0 },
862         { "default_easize",   &ll_default_easize_fops, NULL, 0 },
863         { "max_cookiesize",   &ll_max_cookiesize_fops, NULL, 0 },
864         { "default_cookiesize", &ll_default_cookiesize_fops, NULL, 0 },
865         { "sbi_flags",        &ll_sbi_flags_fops, NULL, 0 },
866         { "xattr_cache",      &ll_xattr_cache_fops, NULL, 0 },
867         { NULL }
868 };
869
870 #define MAX_STRING_SIZE 128
871
872 static struct attribute *llite_attrs[] = {
873         &lustre_attr_blocksize.attr,
874         &lustre_attr_kbytestotal.attr,
875         &lustre_attr_kbytesfree.attr,
876         &lustre_attr_kbytesavail.attr,
877         &lustre_attr_filestotal.attr,
878         &lustre_attr_filesfree.attr,
879         &lustre_attr_client_type.attr,
880         &lustre_attr_fstype.attr,
881         &lustre_attr_uuid.attr,
882         &lustre_attr_max_read_ahead_mb.attr,
883         &lustre_attr_max_read_ahead_per_file_mb.attr,
884         NULL,
885 };
886
887 static void llite_sb_release(struct kobject *kobj)
888 {
889         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
890                                               ll_kobj);
891         complete(&sbi->ll_kobj_unregister);
892 }
893
894 static struct kobj_type llite_ktype = {
895         .default_attrs  = llite_attrs,
896         .sysfs_ops      = &lustre_sysfs_ops,
897         .release        = llite_sb_release,
898 };
899
900 static const struct llite_file_opcode {
901         __u32       opcode;
902         __u32       type;
903         const char *opname;
904 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
905         /* file operation */
906         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
907         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
908         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
909                                    "read_bytes" },
910         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
911                                    "write_bytes" },
912         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
913                                    "brw_read" },
914         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
915                                    "brw_write" },
916         { LPROC_LL_OSC_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
917                                    "osc_read" },
918         { LPROC_LL_OSC_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
919                                    "osc_write" },
920         { LPROC_LL_IOCTL,         LPROCFS_TYPE_REGS, "ioctl" },
921         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
922         { LPROC_LL_RELEASE,     LPROCFS_TYPE_REGS, "close" },
923         { LPROC_LL_MAP,     LPROCFS_TYPE_REGS, "mmap" },
924         { LPROC_LL_LLSEEK,       LPROCFS_TYPE_REGS, "seek" },
925         { LPROC_LL_FSYNC,         LPROCFS_TYPE_REGS, "fsync" },
926         { LPROC_LL_READDIR,     LPROCFS_TYPE_REGS, "readdir" },
927         /* inode operation */
928         { LPROC_LL_SETATTR,     LPROCFS_TYPE_REGS, "setattr" },
929         { LPROC_LL_TRUNC,         LPROCFS_TYPE_REGS, "truncate" },
930         { LPROC_LL_FLOCK,         LPROCFS_TYPE_REGS, "flock" },
931         { LPROC_LL_GETATTR,     LPROCFS_TYPE_REGS, "getattr" },
932         /* dir inode operation */
933         { LPROC_LL_CREATE,       LPROCFS_TYPE_REGS, "create" },
934         { LPROC_LL_LINK,           LPROCFS_TYPE_REGS, "link" },
935         { LPROC_LL_UNLINK,       LPROCFS_TYPE_REGS, "unlink" },
936         { LPROC_LL_SYMLINK,     LPROCFS_TYPE_REGS, "symlink" },
937         { LPROC_LL_MKDIR,         LPROCFS_TYPE_REGS, "mkdir" },
938         { LPROC_LL_RMDIR,         LPROCFS_TYPE_REGS, "rmdir" },
939         { LPROC_LL_MKNOD,         LPROCFS_TYPE_REGS, "mknod" },
940         { LPROC_LL_RENAME,       LPROCFS_TYPE_REGS, "rename" },
941         /* special inode operation */
942         { LPROC_LL_STAFS,         LPROCFS_TYPE_REGS, "statfs" },
943         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
944         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
945         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
946         { LPROC_LL_GETXATTR_HITS,  LPROCFS_TYPE_REGS, "getxattr_hits" },
947         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
948         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
949         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
950 };
951
952 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
953 {
954         if (!sbi->ll_stats)
955                 return;
956         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
957                 lprocfs_counter_add(sbi->ll_stats, op, count);
958         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
959                  sbi->ll_stats_track_id == current->pid)
960                 lprocfs_counter_add(sbi->ll_stats, op, count);
961         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
962                  sbi->ll_stats_track_id == current->real_parent->pid)
963                 lprocfs_counter_add(sbi->ll_stats, op, count);
964         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
965                  sbi->ll_stats_track_id ==
966                         from_kgid(&init_user_ns, current_gid()))
967                 lprocfs_counter_add(sbi->ll_stats, op, count);
968 }
969 EXPORT_SYMBOL(ll_stats_ops_tally);
970
971 static const char *ra_stat_string[] = {
972         [RA_STAT_HIT] = "hits",
973         [RA_STAT_MISS] = "misses",
974         [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
975         [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
976         [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
977         [RA_STAT_FAILED_MATCH] = "failed lock match",
978         [RA_STAT_DISCARDED] = "read but discarded",
979         [RA_STAT_ZERO_LEN] = "zero length file",
980         [RA_STAT_ZERO_WINDOW] = "zero size window",
981         [RA_STAT_EOF] = "read-ahead to EOF",
982         [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
983         [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
984 };
985
986 LPROC_SEQ_FOPS_RO_TYPE(llite, name);
987 LPROC_SEQ_FOPS_RO_TYPE(llite, uuid);
988
989 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
990                                 struct super_block *sb, char *osc, char *mdc)
991 {
992         struct lprocfs_vars lvars[2];
993         struct lustre_sb_info *lsi = s2lsi(sb);
994         struct ll_sb_info *sbi = ll_s2sbi(sb);
995         struct obd_device *obd;
996         struct proc_dir_entry *dir;
997         char name[MAX_STRING_SIZE + 1], *ptr;
998         int err, id, len, rc;
999
1000         memset(lvars, 0, sizeof(lvars));
1001
1002         name[MAX_STRING_SIZE] = '\0';
1003         lvars[0].name = name;
1004
1005         LASSERT(sbi != NULL);
1006         LASSERT(mdc != NULL);
1007         LASSERT(osc != NULL);
1008
1009         /* Get fsname */
1010         len = strlen(lsi->lsi_lmd->lmd_profile);
1011         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
1012         if (ptr && (strcmp(ptr, "-client") == 0))
1013                 len -= 7;
1014
1015         /* Mount info */
1016         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
1017                  lsi->lsi_lmd->lmd_profile, sb);
1018
1019         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
1020         if (IS_ERR(sbi->ll_proc_root)) {
1021                 err = PTR_ERR(sbi->ll_proc_root);
1022                 sbi->ll_proc_root = NULL;
1023                 return err;
1024         }
1025
1026         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
1027                                 &vvp_dump_pgcache_file_ops, sbi);
1028         if (rc)
1029                 CWARN("Error adding the dump_page_cache file\n");
1030
1031         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
1032                                 &ll_rw_extents_stats_fops, sbi);
1033         if (rc)
1034                 CWARN("Error adding the extent_stats file\n");
1035
1036         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
1037                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
1038         if (rc)
1039                 CWARN("Error adding the extents_stats_per_process file\n");
1040
1041         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
1042                                 &ll_rw_offset_stats_fops, sbi);
1043         if (rc)
1044                 CWARN("Error adding the offset_stats file\n");
1045
1046         /* File operations stats */
1047         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
1048                                             LPROCFS_STATS_FLAG_NONE);
1049         if (sbi->ll_stats == NULL) {
1050                 err = -ENOMEM;
1051                 goto out;
1052         }
1053         /* do counter init */
1054         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
1055                 __u32 type = llite_opcode_table[id].type;
1056                 void *ptr = NULL;
1057                 if (type & LPROCFS_TYPE_REGS)
1058                         ptr = "regs";
1059                 else if (type & LPROCFS_TYPE_BYTES)
1060                         ptr = "bytes";
1061                 else if (type & LPROCFS_TYPE_PAGES)
1062                         ptr = "pages";
1063                 lprocfs_counter_init(sbi->ll_stats,
1064                                      llite_opcode_table[id].opcode,
1065                                      (type & LPROCFS_CNTR_AVGMINMAX),
1066                                      llite_opcode_table[id].opname, ptr);
1067         }
1068         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
1069         if (err)
1070                 goto out;
1071
1072         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
1073                                                LPROCFS_STATS_FLAG_NONE);
1074         if (sbi->ll_ra_stats == NULL) {
1075                 err = -ENOMEM;
1076                 goto out;
1077         }
1078
1079         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
1080                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
1081                                      ra_stat_string[id], "pages");
1082         err = lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
1083                                      sbi->ll_ra_stats);
1084         if (err)
1085                 goto out;
1086
1087
1088         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
1089         if (err)
1090                 goto out;
1091
1092         sbi->ll_kobj.kset = llite_kset;
1093         init_completion(&sbi->ll_kobj_unregister);
1094         err = kobject_init_and_add(&sbi->ll_kobj, &llite_ktype, NULL,
1095                                    "%s", name);
1096         if (err)
1097                 goto out;
1098
1099         /* MDC info */
1100         obd = class_name2obd(mdc);
1101
1102         LASSERT(obd != NULL);
1103         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1104         LASSERT(obd->obd_type->typ_name != NULL);
1105
1106         dir = proc_mkdir(obd->obd_type->typ_name, sbi->ll_proc_root);
1107         if (dir == NULL) {
1108                 err = -ENOMEM;
1109                 goto out;
1110         }
1111
1112         snprintf(name, MAX_STRING_SIZE, "common_name");
1113         lvars[0].fops = &llite_name_fops;
1114         err = lprocfs_add_vars(dir, lvars, obd);
1115         if (err)
1116                 goto out;
1117
1118         snprintf(name, MAX_STRING_SIZE, "uuid");
1119         lvars[0].fops = &llite_uuid_fops;
1120         err = lprocfs_add_vars(dir, lvars, obd);
1121         if (err)
1122                 goto out;
1123
1124         /* OSC */
1125         obd = class_name2obd(osc);
1126
1127         LASSERT(obd != NULL);
1128         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1129         LASSERT(obd->obd_type->typ_name != NULL);
1130
1131         dir = proc_mkdir(obd->obd_type->typ_name, sbi->ll_proc_root);
1132         if (dir == NULL) {
1133                 err = -ENOMEM;
1134                 goto out;
1135         }
1136
1137         snprintf(name, MAX_STRING_SIZE, "common_name");
1138         lvars[0].fops = &llite_name_fops;
1139         err = lprocfs_add_vars(dir, lvars, obd);
1140         if (err)
1141                 goto out;
1142
1143         snprintf(name, MAX_STRING_SIZE, "uuid");
1144         lvars[0].fops = &llite_uuid_fops;
1145         err = lprocfs_add_vars(dir, lvars, obd);
1146 out:
1147         if (err) {
1148                 lprocfs_remove(&sbi->ll_proc_root);
1149                 lprocfs_free_stats(&sbi->ll_ra_stats);
1150                 lprocfs_free_stats(&sbi->ll_stats);
1151         }
1152         return err;
1153 }
1154
1155 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
1156 {
1157         if (sbi->ll_proc_root) {
1158                 lprocfs_remove(&sbi->ll_proc_root);
1159                 kobject_put(&sbi->ll_kobj);
1160                 wait_for_completion(&sbi->ll_kobj_unregister);
1161                 lprocfs_free_stats(&sbi->ll_ra_stats);
1162                 lprocfs_free_stats(&sbi->ll_stats);
1163         }
1164 }
1165 #undef MAX_STRING_SIZE
1166
1167 #define pct(a, b) (b ? a * 100 / b : 0)
1168
1169 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1170                                    struct seq_file *seq, int which)
1171 {
1172         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1173         unsigned long start, end, r, w;
1174         char *unitp = "KMGTPEZY";
1175         int i, units = 10;
1176         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1177
1178         read_cum = 0;
1179         write_cum = 0;
1180         start = 0;
1181
1182         for (i = 0; i < LL_HIST_MAX; i++) {
1183                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1184                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1185         }
1186
1187         for (i = 0; i < LL_HIST_MAX; i++) {
1188                 r = pp_info->pp_r_hist.oh_buckets[i];
1189                 w = pp_info->pp_w_hist.oh_buckets[i];
1190                 read_cum += r;
1191                 write_cum += w;
1192                 end = 1 << (i + LL_HIST_START - units);
1193                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | %14lu %4lu %4lu\n",
1194                            start, *unitp, end, *unitp,
1195                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1196                            r, pct(r, read_tot), pct(read_cum, read_tot),
1197                            w, pct(w, write_tot), pct(write_cum, write_tot));
1198                 start = end;
1199                 if (start == 1<<10) {
1200                         start = 1;
1201                         units += 10;
1202                         unitp++;
1203                 }
1204                 if (read_cum == read_tot && write_cum == write_tot)
1205                         break;
1206         }
1207 }
1208
1209 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1210 {
1211         struct timeval now;
1212         struct ll_sb_info *sbi = seq->private;
1213         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1214         int k;
1215
1216         do_gettimeofday(&now);
1217
1218         if (!sbi->ll_rw_stats_on) {
1219                 seq_printf(seq, "disabled\n"
1220                            "write anything in this file to activate, then 0 or \"[D/d]isabled\" to deactivate\n");
1221                 return 0;
1222         }
1223         seq_printf(seq, "snapshot_time:  %lu.%lu (secs.usecs)\n",
1224                    now.tv_sec, (unsigned long)now.tv_usec);
1225         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1226         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1227                    "extents", "calls", "%", "cum%",
1228                    "calls", "%", "cum%");
1229         spin_lock(&sbi->ll_pp_extent_lock);
1230         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1231                 if (io_extents->pp_extents[k].pid != 0) {
1232                         seq_printf(seq, "\nPID: %d\n",
1233                                    io_extents->pp_extents[k].pid);
1234                         ll_display_extents_info(io_extents, seq, k);
1235                 }
1236         }
1237         spin_unlock(&sbi->ll_pp_extent_lock);
1238         return 0;
1239 }
1240
1241 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1242                                                 const char __user *buf,
1243                                                 size_t len,
1244                                                 loff_t *off)
1245 {
1246         struct seq_file *seq = file->private_data;
1247         struct ll_sb_info *sbi = seq->private;
1248         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1249         int i;
1250         int value = 1, rc = 0;
1251
1252         if (len == 0)
1253                 return -EINVAL;
1254
1255         rc = lprocfs_write_helper(buf, len, &value);
1256         if (rc < 0 && len < 16) {
1257                 char kernbuf[16];
1258
1259                 if (copy_from_user(kernbuf, buf, len))
1260                         return -EFAULT;
1261                 kernbuf[len] = 0;
1262
1263                 if (kernbuf[len - 1] == '\n')
1264                         kernbuf[len - 1] = 0;
1265
1266                 if (strcmp(kernbuf, "disabled") == 0 ||
1267                     strcmp(kernbuf, "Disabled") == 0)
1268                         value = 0;
1269         }
1270
1271         if (value == 0)
1272                 sbi->ll_rw_stats_on = 0;
1273         else
1274                 sbi->ll_rw_stats_on = 1;
1275
1276         spin_lock(&sbi->ll_pp_extent_lock);
1277         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1278                 io_extents->pp_extents[i].pid = 0;
1279                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1280                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1281         }
1282         spin_unlock(&sbi->ll_pp_extent_lock);
1283         return len;
1284 }
1285
1286 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1287
1288 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1289 {
1290         struct timeval now;
1291         struct ll_sb_info *sbi = seq->private;
1292         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1293
1294         do_gettimeofday(&now);
1295
1296         if (!sbi->ll_rw_stats_on) {
1297                 seq_printf(seq, "disabled\n"
1298                            "write anything in this file to activate, then 0 or \"[D/d]isabled\" to deactivate\n");
1299                 return 0;
1300         }
1301         seq_printf(seq, "snapshot_time:  %lu.%lu (secs.usecs)\n",
1302                    now.tv_sec, (unsigned long)now.tv_usec);
1303
1304         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1305         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1306                    "extents", "calls", "%", "cum%",
1307                    "calls", "%", "cum%");
1308         spin_lock(&sbi->ll_lock);
1309         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1310         spin_unlock(&sbi->ll_lock);
1311
1312         return 0;
1313 }
1314
1315 static ssize_t ll_rw_extents_stats_seq_write(struct file *file,
1316                                              const char __user *buf,
1317                                              size_t len, loff_t *off)
1318 {
1319         struct seq_file *seq = file->private_data;
1320         struct ll_sb_info *sbi = seq->private;
1321         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1322         int i;
1323         int value = 1, rc = 0;
1324
1325         if (len == 0)
1326                 return -EINVAL;
1327
1328         rc = lprocfs_write_helper(buf, len, &value);
1329         if (rc < 0 && len < 16) {
1330                 char kernbuf[16];
1331
1332                 if (copy_from_user(kernbuf, buf, len))
1333                         return -EFAULT;
1334                 kernbuf[len] = 0;
1335
1336                 if (kernbuf[len - 1] == '\n')
1337                         kernbuf[len - 1] = 0;
1338
1339                 if (strcmp(kernbuf, "disabled") == 0 ||
1340                     strcmp(kernbuf, "Disabled") == 0)
1341                         value = 0;
1342         }
1343
1344         if (value == 0)
1345                 sbi->ll_rw_stats_on = 0;
1346         else
1347                 sbi->ll_rw_stats_on = 1;
1348
1349         spin_lock(&sbi->ll_pp_extent_lock);
1350         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
1351                 io_extents->pp_extents[i].pid = 0;
1352                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1353                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1354         }
1355         spin_unlock(&sbi->ll_pp_extent_lock);
1356
1357         return len;
1358 }
1359 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1360
1361 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
1362                        struct ll_file_data *file, loff_t pos,
1363                        size_t count, int rw)
1364 {
1365         int i, cur = -1;
1366         struct ll_rw_process_info *process;
1367         struct ll_rw_process_info *offset;
1368         int *off_count = &sbi->ll_rw_offset_entry_count;
1369         int *process_count = &sbi->ll_offset_process_count;
1370         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1371
1372         if (!sbi->ll_rw_stats_on)
1373                 return;
1374         process = sbi->ll_rw_process_info;
1375         offset = sbi->ll_rw_offset_info;
1376
1377         spin_lock(&sbi->ll_pp_extent_lock);
1378         /* Extent statistics */
1379         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1380                 if (io_extents->pp_extents[i].pid == pid) {
1381                         cur = i;
1382                         break;
1383                 }
1384         }
1385
1386         if (cur == -1) {
1387                 /* new process */
1388                 sbi->ll_extent_process_count =
1389                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1390                 cur = sbi->ll_extent_process_count;
1391                 io_extents->pp_extents[cur].pid = pid;
1392                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1393                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1394         }
1395
1396         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1397              (i < (LL_HIST_MAX - 1)); i++);
1398         if (rw == 0) {
1399                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1400                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1401         } else {
1402                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1403                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1404         }
1405         spin_unlock(&sbi->ll_pp_extent_lock);
1406
1407         spin_lock(&sbi->ll_process_lock);
1408         /* Offset statistics */
1409         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1410                 if (process[i].rw_pid == pid) {
1411                         if (process[i].rw_last_file != file) {
1412                                 process[i].rw_range_start = pos;
1413                                 process[i].rw_last_file_pos = pos + count;
1414                                 process[i].rw_smallest_extent = count;
1415                                 process[i].rw_largest_extent = count;
1416                                 process[i].rw_offset = 0;
1417                                 process[i].rw_last_file = file;
1418                                 spin_unlock(&sbi->ll_process_lock);
1419                                 return;
1420                         }
1421                         if (process[i].rw_last_file_pos != pos) {
1422                                 *off_count =
1423                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1424                                 offset[*off_count].rw_op = process[i].rw_op;
1425                                 offset[*off_count].rw_pid = pid;
1426                                 offset[*off_count].rw_range_start =
1427                                         process[i].rw_range_start;
1428                                 offset[*off_count].rw_range_end =
1429                                         process[i].rw_last_file_pos;
1430                                 offset[*off_count].rw_smallest_extent =
1431                                         process[i].rw_smallest_extent;
1432                                 offset[*off_count].rw_largest_extent =
1433                                         process[i].rw_largest_extent;
1434                                 offset[*off_count].rw_offset =
1435                                         process[i].rw_offset;
1436                                 process[i].rw_op = rw;
1437                                 process[i].rw_range_start = pos;
1438                                 process[i].rw_smallest_extent = count;
1439                                 process[i].rw_largest_extent = count;
1440                                 process[i].rw_offset = pos -
1441                                         process[i].rw_last_file_pos;
1442                         }
1443                         if (process[i].rw_smallest_extent > count)
1444                                 process[i].rw_smallest_extent = count;
1445                         if (process[i].rw_largest_extent < count)
1446                                 process[i].rw_largest_extent = count;
1447                         process[i].rw_last_file_pos = pos + count;
1448                         spin_unlock(&sbi->ll_process_lock);
1449                         return;
1450                 }
1451         }
1452         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1453         process[*process_count].rw_pid = pid;
1454         process[*process_count].rw_op = rw;
1455         process[*process_count].rw_range_start = pos;
1456         process[*process_count].rw_last_file_pos = pos + count;
1457         process[*process_count].rw_smallest_extent = count;
1458         process[*process_count].rw_largest_extent = count;
1459         process[*process_count].rw_offset = 0;
1460         process[*process_count].rw_last_file = file;
1461         spin_unlock(&sbi->ll_process_lock);
1462 }
1463
1464 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1465 {
1466         struct timeval now;
1467         struct ll_sb_info *sbi = seq->private;
1468         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1469         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1470         int i;
1471
1472         do_gettimeofday(&now);
1473
1474         if (!sbi->ll_rw_stats_on) {
1475                 seq_printf(seq, "disabled\n"
1476                            "write anything in this file to activate, then 0 or \"[D/d]isabled\" to deactivate\n");
1477                 return 0;
1478         }
1479         spin_lock(&sbi->ll_process_lock);
1480
1481         seq_printf(seq, "snapshot_time:  %lu.%lu (secs.usecs)\n",
1482                    now.tv_sec, (unsigned long)now.tv_usec);
1483         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1484                    "R/W", "PID", "RANGE START", "RANGE END",
1485                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1486         /* We stored the discontiguous offsets here; print them first */
1487         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1488                 if (offset[i].rw_pid != 0)
1489                         seq_printf(seq,
1490                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1491                                    offset[i].rw_op == READ ? 'R' : 'W',
1492                                    offset[i].rw_pid,
1493                                    offset[i].rw_range_start,
1494                                    offset[i].rw_range_end,
1495                                    (unsigned long)offset[i].rw_smallest_extent,
1496                                    (unsigned long)offset[i].rw_largest_extent,
1497                                    offset[i].rw_offset);
1498         }
1499         /* Then print the current offsets for each process */
1500         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1501                 if (process[i].rw_pid != 0)
1502                         seq_printf(seq,
1503                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1504                                    process[i].rw_op == READ ? 'R' : 'W',
1505                                    process[i].rw_pid,
1506                                    process[i].rw_range_start,
1507                                    process[i].rw_last_file_pos,
1508                                    (unsigned long)process[i].rw_smallest_extent,
1509                                    (unsigned long)process[i].rw_largest_extent,
1510                                    process[i].rw_offset);
1511         }
1512         spin_unlock(&sbi->ll_process_lock);
1513
1514         return 0;
1515 }
1516
1517 static ssize_t ll_rw_offset_stats_seq_write(struct file *file,
1518                                             const char __user *buf,
1519                                             size_t len, loff_t *off)
1520 {
1521         struct seq_file *seq = file->private_data;
1522         struct ll_sb_info *sbi = seq->private;
1523         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1524         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1525         int value = 1, rc = 0;
1526
1527         if (len == 0)
1528                 return -EINVAL;
1529
1530         rc = lprocfs_write_helper(buf, len, &value);
1531
1532         if (rc < 0 && len < 16) {
1533                 char kernbuf[16];
1534
1535                 if (copy_from_user(kernbuf, buf, len))
1536                         return -EFAULT;
1537                 kernbuf[len] = 0;
1538
1539                 if (kernbuf[len - 1] == '\n')
1540                         kernbuf[len - 1] = 0;
1541
1542                 if (strcmp(kernbuf, "disabled") == 0 ||
1543                     strcmp(kernbuf, "Disabled") == 0)
1544                         value = 0;
1545         }
1546
1547         if (value == 0)
1548                 sbi->ll_rw_stats_on = 0;
1549         else
1550                 sbi->ll_rw_stats_on = 1;
1551
1552         spin_lock(&sbi->ll_process_lock);
1553         sbi->ll_offset_process_count = 0;
1554         sbi->ll_rw_offset_entry_count = 0;
1555         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1556                LL_PROCESS_HIST_MAX);
1557         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1558                LL_OFFSET_HIST_MAX);
1559         spin_unlock(&sbi->ll_process_lock);
1560
1561         return len;
1562 }
1563
1564 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1565
1566 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1567 {
1568     lvars->module_vars  = NULL;
1569     lvars->obd_vars     = lprocfs_llite_obd_vars;
1570 }