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