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