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