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