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