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