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