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